]> granicus.if.org Git - neomutt/blobdiff - editmsg.c
merge: light refactoring
[neomutt] / editmsg.c
index eed210915b8069312348be36b4cab07f669c76d1..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[STRING];
+  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 = MboxType;
-  MboxType = MUTT_MBOX;
-  struct Mailbox *m_fname = mx_path_resolve(fname);
-  struct Context *tmpctx = mx_mbox_open(m_fname, NULL, MUTT_NEWFOLDER);
-  MboxType = omagic;
+  enum MailboxType omagic = C_MboxType;
+  C_MboxType = MUTT_MBOX;
 
-  if (!tmpctx)
+  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 (!ctx_tmp)
   {
     mutt_error(_("could not create temporary folder: %s"), strerror(errno));
+    mutt_buffer_pool_release(&fname);
     mailbox_free(&m_fname);
     return -1;
   }
 
-  const int chflags =
-      CH_NOLEN | ((m->magic == MUTT_MBOX || m->magic == MUTT_MMDF) ? 0 : CH_NOSTATUS);
-  rc = mutt_append_message(tmpctx->mailbox, m, e, 0, chflags);
+  const CopyHeaderFlags 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)
   {
@@ -92,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;
   }
 
@@ -103,9 +108,8 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
    * truncate the last newline in the temp file, which is logically part of
    * 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))
+   * the message.  */
+  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;
@@ -114,24 +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(1, "Could not remove write permissions of %s: %s", fname, strerror(errno));
+      mutt_debug(LL_DEBUG1, "Could not remove write permissions of %s: %s",
+                 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(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;
   }
 
@@ -163,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;
@@ -171,8 +176,8 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
     goto bail;
   }
 
-  tmpctx = mx_mbox_open(m, NULL, 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) */
@@ -180,14 +185,15 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
     goto bail;
   }
 
-  int of = 0;
-  int cf = (((tmpctx->mailbox->magic == MUTT_MBOX) || (tmpctx->mailbox->magic == MUTT_MMDF)) ?
-                0 :
-                CH_NOSTATUS);
+  MsgOpenFlags of = MUTT_MSG_NO_FLAGS;
+  CopyHeaderFlags cf =
+      (((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
@@ -200,47 +206,50 @@ 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)
   {
-    mutt_set_flag(m, e, MUTT_DELETE, 1);
-    mutt_set_flag(m, e, MUTT_PURGE, 1);
-    mutt_set_flag(m, e, MUTT_READ, 1);
+    mutt_set_flag(m, e, MUTT_DELETE, true);
+    mutt_set_flag(m, e, MUTT_PURGE, true);
+    mutt_set_flag(m, e, MUTT_READ, true);
 
-    if (DeleteUntag)
-      mutt_set_flag(m, e, MUTT_TAG, 0);
+    if (C_DeleteUntag)
+      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;
 }