]> granicus.if.org Git - neomutt/blobdiff - editmsg.c
merge: light refactoring
[neomutt] / editmsg.c
index 2fecc9ff110290c252baf8bf5100c17329a08f3f..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 Context *tmpctx = mx_mbox_open(m_fname, MUTT_NEWFOLDER);
+  struct Mailbox *m_fname = mx_path_resolve(mutt_b2s(fname));
+  struct Context *ctx_tmp = mx_mbox_open(m_fname, MUTT_NEWFOLDER);
 
   C_MboxType = omagic;
 
-  if (!tmpctx)
+  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) ? 0 : CH_NOSTATUS);
-  rc = mutt_append_message(tmpctx->mailbox, m, e, 0, chflags);
+      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;
 
-  mx_mbox_close(&tmpctx);
+  mx_mbox_close(&ctx_tmp);
 
   if (rc == -1)
   {
@@ -94,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;
   }
 
@@ -106,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;
@@ -115,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;
   }
 
@@ -165,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;
@@ -173,8 +176,8 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
     goto bail;
   }
 
-  tmpctx = mx_mbox_open(m, MUTT_APPEND);
-  if (!tmpctx)
+  struct Context *ctx_app = mx_mbox_open(m, MUTT_APPEND | MUTT_QUIET);
+  if (!ctx_app)
   {
     rc = -1;
     /* L10N: %s is from strerror(errno) */
@@ -184,13 +187,13 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
 
   MsgOpenFlags of = MUTT_MSG_NO_FLAGS;
   CopyHeaderFlags cf =
-      (((tmpctx->mailbox->magic == MUTT_MBOX) || (tmpctx->mailbox->magic == MUTT_MMDF)) ?
+      (((ctx_app->mailbox->magic == MUTT_MBOX) || (ctx_app->mailbox->magic == MUTT_MMDF)) ?
            CH_NO_FLAGS :
            CH_NOSTATUS);
 
   if (fgets(buf, sizeof(buf), fp) && is_from(buf, NULL, 0, NULL))
   {
-    if ((tmpctx->mailbox->magic == MUTT_MBOX) || (tmpctx->mailbox->magic == MUTT_MMDF))
+    if ((ctx_app->mailbox->magic == MUTT_MBOX) || (ctx_app->mailbox->magic == MUTT_MMDF))
       cf = CH_FROM | CH_FORCE_FROM;
   }
   else
@@ -203,34 +206,34 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
   bool o_old = e->old;
   e->read = false;
   e->old = false;
-  struct Message *msg = mx_msg_open_new(tmpctx->mailbox, e, of);
+  struct Message *msg = mx_msg_open_new(ctx_app->mailbox, e, of);
   e->read = o_read;
   e->old = o_old;
 
   if (!msg)
   {
     mutt_error(_("Can't append to folder: %s"), strerror(errno));
-    mx_mbox_close(&tmpctx);
+    mx_mbox_close(&ctx_app);
     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);
     mutt_file_copy_stream(fp, msg->fp);
   }
 
-  rc = mx_msg_commit(tmpctx->mailbox, msg);
-  mx_msg_close(tmpctx->mailbox, &msg);
+  rc = mx_msg_commit(ctx_app->mailbox, msg);
+  mx_msg_close(ctx_app->mailbox, &msg);
 
-  mx_mbox_close(&tmpctx);
+  mx_mbox_close(&ctx_app);
 
 bail:
   mutt_file_fclose(&fp);
 
   if (rc >= 0)
-    unlink(fname);
+    unlink(mutt_b2s(fname));
 
   if (rc == 0)
   {
@@ -242,8 +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;
 }