Default it to /var/tmp, so that files will survive an unfortunately
timed computer reboot.

Create two new functions for generating temp filenames in $tmpdraftdir:
 - mutt_adv_mktemp_draft() for template-style naming
 - mutt_buffer_mktemp_draft() for random naming with a "mutt" prefix.

Modify compose temp files to use $tmpdraftdir functions.

Handle the most common cases:
  - compose and compose with $edit_headers
  - postponed messages
  - resend-message
  - -H draft files via command line argument
  - -i include body files via command line argument.

Also change attachment menu forwarding and replying, which generate a
tempfile passed in as the body, to use $tmpdraftdir.
---
 globals.h  |  1 +
 headers.c  |  2 +-
 init.h     | 10 +++++++++-
 main.c     |  2 +-
 muttlib.c  | 13 +++++++------
 postpone.c |  2 +-
 protos.h   | 13 +++++++++----
 recvcmd.c  |  6 +++---
 send.c     |  2 +-
 9 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/globals.h b/globals.h
index 0675348f..03e43c02 100644
--- a/globals.h
+++ b/globals.h
@@ -171,6 +171,7 @@ WHERE char *SslVerifyHostOverride;
 WHERE mbchar_table *StChars;
 WHERE char *Status;
 WHERE char *Tempdir;
+WHERE char *TempDraftDir;
 WHERE mbchar_table *Tochars;
 WHERE char *TrashPath;
 WHERE char *TSStatusFormat;
diff --git a/headers.c b/headers.c
index dbec2f1d..09202742 100644
--- a/headers.c
+++ b/headers.c
@@ -47,7 +47,7 @@ int mutt_edit_headers (const char *editor,
   if (flags != MUTT_EDIT_HEADERS_RESUME)
   {
     sctx->tempfile = mutt_buffer_new ();
-    mutt_buffer_mktemp (sctx->tempfile);
+    mutt_buffer_mktemp_draft (sctx->tempfile);
     if ((ofp = safe_fopen (mutt_b2s (sctx->tempfile), "w")) == NULL)
     {
       mutt_perror (mutt_b2s (sctx->tempfile));
diff --git a/init.h b/init.h
index 29d54b34..c50164e7 100644
--- a/init.h
+++ b/init.h
@@ -4696,10 +4696,18 @@ struct option_t MuttVars[] = {
   /*
   ** .pp
   ** This variable allows you to specify where Mutt will place its
-  ** temporary files needed for displaying and composing messages.  If
+  ** temporary files needed for displaying messages.  If
   ** this variable is not set, the environment variable \fC$$$TMPDIR\fP is
   ** used.  If \fC$$$TMPDIR\fP is not set then ``\fC/tmp\fP'' is used.
   */
+  { "tmpdraftdir",           DT_PATH, R_NONE, {.p=&TempDraftDir}, 
{.p="/var/tmp"} },
+  /*
+  ** .pp
+  ** This variable allows you to specify where Mutt will place its
+  ** temporary files when composing messages.  It defaults to
+  ** ``\fC/var/tmp\fP'' so that the files will survive a reboot in the
+  ** event of a crash or other unplanned computer shutdown.
+  */
   { "to_chars",         DT_MBCHARTBL, R_BOTH, {.p=&Tochars}, {.p=" +TCFL"} },
   /*
   ** .pp
diff --git a/main.c b/main.c
index d4bc77d4..cb32f93e 100644
--- a/main.c
+++ b/main.c
@@ -1093,7 +1093,7 @@ int main (int argc, char **argv, char **environ)
       if (!edit_infile)
       {
         tempfile = mutt_buffer_new ();
-        mutt_buffer_mktemp (tempfile);
+        mutt_buffer_mktemp_draft (tempfile);
 
         if ((fout = safe_fopen (mutt_b2s (tempfile), "w")) == NULL)
         {
diff --git a/muttlib.c b/muttlib.c
index 2022ee61..494c27d0 100644
--- a/muttlib.c
+++ b/muttlib.c
@@ -64,7 +64,7 @@ BODY *mutt_new_body (void)
  * Renamed to mutt_adv_mktemp so I only have to change where it's
  * called, and not all possible cases.
  */
-void mutt_adv_mktemp (BUFFER *buf)
+void _mutt_adv_mktemp (BUFFER *buf, const char *tempdir)
 {
   BUFFER *prefix = NULL;
   char *suffix;
@@ -72,14 +72,14 @@ void mutt_adv_mktemp (BUFFER *buf)
 
   if (!(buf->data && buf->data[0]))
   {
-    mutt_buffer_mktemp (buf);
+    _mutt_buffer_mktemp_pfx_sfx (buf, tempdir, "mutt", NULL);
   }
   else
   {
     prefix = mutt_buffer_pool_get ();
     mutt_buffer_strcpy (prefix, buf->data);
     mutt_sanitize_filename (prefix->data, MUTT_SANITIZE_ALLOW_8BIT);
-    mutt_buffer_printf (buf, "%s/%s", NONULL (Tempdir), mutt_b2s (prefix));
+    mutt_buffer_printf (buf, "%s/%s", NONULL (tempdir), mutt_b2s (prefix));
     if (lstat (mutt_b2s (buf), &sb) == -1 && errno == ENOENT)
       goto out;
 
@@ -88,21 +88,22 @@ void mutt_adv_mktemp (BUFFER *buf)
       *suffix = 0;
       ++suffix;
     }
-    _mutt_buffer_mktemp_pfx_sfx (buf, mutt_b2s (prefix), suffix);
+    _mutt_buffer_mktemp_pfx_sfx (buf, tempdir, mutt_b2s (prefix), suffix);
 
   out:
     mutt_buffer_pool_release (&prefix);
   }
 }
 
-void _mutt_buffer_mktemp (BUFFER *buf, const char *prefix, const char *suffix,
+void _mutt_buffer_mktemp (BUFFER *buf, const char *tempdir,
+                          const char *prefix, const char *suffix,
                           const char *src, int line)
 {
   RANDOM64 random64;
   mutt_random_bytes(random64.char_array, sizeof(random64));
 
   mutt_buffer_printf (buf, "%s/%s-%s-%d-%d-%"PRIu64"%s%s",
-                      NONULL (Tempdir), NONULL (prefix), NONULL (Hostname),
+                      NONULL (tempdir), NONULL (prefix), NONULL (Hostname),
                       (int) getuid (), (int) getpid (), random64.int_64,
                       suffix ? "." : "", NONULL (suffix));
   muttdbg(3, "%s:%d: mutt_mktemp returns \"%s\".", src, line, mutt_b2s (buf));
diff --git a/postpone.c b/postpone.c
index 44b31db7..4c38234a 100644
--- a/postpone.c
+++ b/postpone.c
@@ -729,7 +729,7 @@ int mutt_prepare_template (FILE *fp, CONTEXT *ctx, HEADER 
*newhdr, HEADER *hdr,
       mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
     }
 
-    mutt_adv_mktemp (file);
+    mutt_adv_mktemp_draft (file);
     if ((s.fpout = safe_fopen (mutt_b2s (file), "w")) == NULL)
       goto bail;
 
diff --git a/protos.h b/protos.h
index 2c6b1aef..90a28b67 100644
--- a/protos.h
+++ b/protos.h
@@ -172,10 +172,15 @@ group_t *mutt_pattern_group (const char *);
 
 REGEXP *mutt_compile_regexp (const char *, int);
 
-void mutt_adv_mktemp (BUFFER *);
-#define mutt_buffer_mktemp(a) _mutt_buffer_mktemp_pfx_sfx (a, "mutt", NULL)
-#define _mutt_buffer_mktemp_pfx_sfx(a,b,c)  _mutt_buffer_mktemp (a, b, c, 
__FILE__, __LINE__)
-void _mutt_buffer_mktemp (BUFFER *, const char *, const char *, const char *, 
int);
+#define mutt_adv_mktemp(a) _mutt_adv_mktemp (a, Tempdir)
+#define mutt_adv_mktemp_draft(a) _mutt_adv_mktemp (a, TempDraftDir)
+void _mutt_adv_mktemp (BUFFER *, const char *);
+
+#define mutt_buffer_mktemp(a) _mutt_buffer_mktemp_pfx_sfx (a, Tempdir, "mutt", 
NULL)
+#define mutt_buffer_mktemp_draft(a) _mutt_buffer_mktemp_pfx_sfx (a, 
TempDraftDir, "mutt", NULL)
+
+#define _mutt_buffer_mktemp_pfx_sfx(a,b,c,d)  _mutt_buffer_mktemp (a, b, c, d, 
__FILE__, __LINE__)
+void _mutt_buffer_mktemp (BUFFER *, const char *, const char *, const char *, 
const char *, int);
 
 void mutt_account_hook (const char* url);
 void mutt_alias_menu (char *, size_t, ALIAS *);
diff --git a/recvcmd.c b/recvcmd.c
index c319a7ab..9f696348 100644
--- a/recvcmd.c
+++ b/recvcmd.c
@@ -486,7 +486,7 @@ static void attach_forward_bodies (FILE * fp, HEADER * hdr,
   mutt_make_forward_subject (tmphdr->env, Context, parent_hdr);
 
   tmpbody = mutt_buffer_pool_get ();
-  mutt_buffer_mktemp (tmpbody);
+  mutt_buffer_mktemp_draft (tmpbody);
   if ((tmpfp = safe_fopen (mutt_b2s (tmpbody), "w")) == NULL)
   {
     mutt_error (_("Can't open temporary file %s."), mutt_b2s (tmpbody));
@@ -667,7 +667,7 @@ static void attach_forward_msgs (FILE * fp, HEADER * hdr,
 
     /* no MIME encapsulation */
 
-    mutt_buffer_mktemp (tmpbody);
+    mutt_buffer_mktemp_draft (tmpbody);
     if (!(tmpfp = safe_fopen (mutt_b2s (tmpbody), "w")))
     {
       mutt_error (_("Can't create %s."), mutt_b2s (tmpbody));
@@ -969,7 +969,7 @@ void mutt_attach_reply (FILE * fp, HEADER * hdr,
     goto cleanup;
 
   tmpbody = mutt_buffer_pool_get ();
-  mutt_buffer_mktemp (tmpbody);
+  mutt_buffer_mktemp_draft (tmpbody);
   if ((tmpfp = safe_fopen (mutt_b2s (tmpbody), "w")) == NULL)
   {
     mutt_error (_("Can't create %s."), mutt_b2s (tmpbody));
diff --git a/send.c b/send.c
index 6658fe7d..bb267d03 100644
--- a/send.c
+++ b/send.c
@@ -1931,7 +1931,7 @@ static int send_message_setup (SEND_CONTEXT *sctx, const 
char *tempfile,
       if (!tempfile)
       {
         tmpbuffer = mutt_buffer_pool_get ();
-        mutt_buffer_mktemp (tmpbuffer);
+        mutt_buffer_mktemp_draft (tmpbuffer);
         tempfp = safe_fopen (mutt_b2s (tmpbuffer), "w+");
         sctx->msg->content->filename = safe_strdup (mutt_b2s (tmpbuffer));
         mutt_buffer_pool_release (&tmpbuffer);
-- 
2.53.0

Reply via email to