]> granicus.if.org Git - neomutt/blobdiff - editmsg.c
merge: light refactoring
[neomutt] / editmsg.c
index 835d16234e77f16ef11fe4d1356c4ca3db120ca8..cdd7e1fa86a60efdee41497e15d016fa2cc633ef 100644 (file)
--- a/editmsg.c
+++ b/editmsg.c
 #include <time.h>
 #include <unistd.h>
 #include "mutt/mutt.h"
-#include "config/lib.h"
 #include "email/lib.h"
+#include "core/lib.h"
 #include "mutt.h"
 #include "context.h"
 #include "copy.h"
 #include "curs_lib.h"
 #include "globals.h"
-#include "mailbox.h"
 #include "muttlib.h"
 #include "mx.h"
 #include "protos.h"
  */
 static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
 {
-  char fname[PATH_MAX];
   char buf[256];
   int rc;
+  FILE *fp = NULL;
   struct stat sb;
   bool old_append = m->append;
 
-  mutt_mktemp(fname, sizeof(fname));
+  struct Buffer *fname = mutt_buffer_pool_get();
+  mutt_buffer_mktemp(fname);
 
   enum MailboxType omagic = C_MboxType;
   C_MboxType = MUTT_MBOX;
 
-  struct Mailbox *m_fname = mx_path_resolve(fname);
+  struct Mailbox *m_fname = mx_path_resolve(mutt_b2s(fname));
   struct Context *ctx_tmp = mx_mbox_open(m_fname, MUTT_NEWFOLDER);
 
   C_MboxType = omagic;
@@ -78,12 +78,14 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
   if (!ctx_tmp)
   {
     mutt_error(_("could not create temporary folder: %s"), strerror(errno));
+    mutt_buffer_pool_release(&fname);
     mailbox_free(&m_fname);
     return -1;
   }
 
   const CopyHeaderFlags chflags =
-      CH_NOLEN | (((m->magic == MUTT_MBOX) || (m->magic == MUTT_MMDF)) ? CH_NO_FLAGS : CH_NOSTATUS);
+      CH_NOLEN |
+      (((m->magic == MUTT_MBOX) || (m->magic == MUTT_MMDF)) ? CH_NO_FLAGS : CH_NOSTATUS);
   rc = mutt_append_message(ctx_tmp->mailbox, m, e, MUTT_CM_NO_FLAGS, chflags);
   int oerrno = errno;
 
@@ -95,10 +97,10 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
     goto bail;
   }
 
-  rc = stat(fname, &sb);
+  rc = stat(mutt_b2s(fname), &sb);
   if (rc == -1)
   {
-    mutt_error(_("Can't stat %s: %s"), fname, strerror(errno));
+    mutt_error(_("Can't stat %s: %s"), mutt_b2s(fname), strerror(errno));
     goto bail;
   }
 
@@ -107,7 +109,7 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
    * the message separator, and not the body of the message.  If we fail to
    * remove it, the message will grow by one line each time the user edits
    * the message.  */
-  if ((sb.st_size != 0) && (truncate(fname, sb.st_size - 1) == -1))
+  if ((sb.st_size != 0) && (truncate(mutt_b2s(fname), sb.st_size - 1) == -1))
   {
     mutt_error(_("could not truncate temporary mail folder: %s"), strerror(errno));
     goto bail;
@@ -116,25 +118,25 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
   if (action == EVM_VIEW)
   {
     /* remove write permissions */
-    rc = mutt_file_chmod_rm_stat(fname, S_IWUSR | S_IWGRP | S_IWOTH, &sb);
+    rc = mutt_file_chmod_rm_stat(mutt_b2s(fname), S_IWUSR | S_IWGRP | S_IWOTH, &sb);
     if (rc == -1)
     {
       mutt_debug(LL_DEBUG1, "Could not remove write permissions of %s: %s",
-                 fname, strerror(errno));
+                 mutt_b2s(fname), strerror(errno));
       /* Do not bail out here as we are checking afterwards if we should adopt
        * changes of the temporary file. */
     }
   }
 
   /* Do not reuse the stat sb here as it is outdated. */
-  time_t mtime = mutt_file_decrease_mtime(fname, NULL);
+  time_t mtime = mutt_file_decrease_mtime(mutt_b2s(fname), NULL);
 
-  mutt_edit_file(NONULL(C_Editor), fname);
+  mutt_edit_file(NONULL(C_Editor), mutt_b2s(fname));
 
-  rc = stat(fname, &sb);
+  rc = stat(mutt_b2s(fname), &sb);
   if (rc == -1)
   {
-    mutt_error(_("Can't stat %s: %s"), fname, strerror(errno));
+    mutt_error(_("Can't stat %s: %s"), mutt_b2s(fname), strerror(errno));
     goto bail;
   }
 
@@ -166,7 +168,7 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
     goto bail;
   }
 
-  FILE *fp = fopen(fname, "r");
+  fp = fopen(mutt_b2s(fname), "r");
   if (!fp)
   {
     rc = -1;
@@ -174,7 +176,7 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
     goto bail;
   }
 
-  struct Context *ctx_app = mx_mbox_open(m, MUTT_OPEN_NO_FLAGS);
+  struct Context *ctx_app = mx_mbox_open(m, MUTT_APPEND | MUTT_QUIET);
   if (!ctx_app)
   {
     rc = -1;
@@ -183,9 +185,6 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
     goto bail;
   }
 
-  old_append = m->append;
-  m->append = true;
-
   MsgOpenFlags of = MUTT_MSG_NO_FLAGS;
   CopyHeaderFlags cf =
       (((ctx_app->mailbox->magic == MUTT_MBOX) || (ctx_app->mailbox->magic == MUTT_MMDF)) ?
@@ -218,7 +217,7 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
     goto bail;
   }
 
-  rc = mutt_copy_hdr(fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf, NULL);
+  rc = mutt_copy_hdr(fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf, NULL, 0);
   if (rc == 0)
   {
     fputc('\n', msg->fp);
@@ -234,7 +233,7 @@ bail:
   mutt_file_fclose(&fp);
 
   if (rc >= 0)
-    unlink(fname);
+    unlink(mutt_b2s(fname));
 
   if (rc == 0)
   {
@@ -246,9 +245,11 @@ bail:
       mutt_set_flag(m, e, MUTT_TAG, false);
   }
   else if (rc == -1)
-    mutt_message(_("Error. Preserving temporary file: %s"), fname);
+    mutt_message(_("Error. Preserving temporary file: %s"), mutt_b2s(fname));
 
   m->append = old_append;
+
+  mutt_buffer_pool_release(&fname);
   return rc;
 }