]> granicus.if.org Git - neomutt/commitdiff
bounce_message: factor out Context
authorRichard Russon <rich@flatcap.org>
Tue, 8 Jan 2019 18:21:02 +0000 (18:21 +0000)
committerRichard Russon <rich@flatcap.org>
Fri, 8 Feb 2019 14:34:07 +0000 (14:34 +0000)
commands.c
commands.h
index.c
pager.c
recvattach.c
recvcmd.c
recvcmd.h
sendlib.c

index cb3ea615bb1d7139fe67e7a5424bdedb59304c4d..53fd3c2c9c03459297e14c89d9de4369cc6da034 100644 (file)
@@ -363,48 +363,34 @@ int mutt_display_message(struct Email *cur)
 
 /**
  * ci_bounce_message - Bounce an email
- * @param e Email to bounce
+ * @param m  Mailbox
+ * @param el List of Emails to bounce
  */
-void ci_bounce_message(struct Email *e)
+void ci_bounce_message(struct Mailbox *m, struct EmailList *el)
 {
+  if (!m || !el || STAILQ_EMPTY(el))
+    return;
+
   char prompt[SHORT_STRING];
   char scratch[SHORT_STRING];
   char buf[HUGE_STRING] = { 0 };
   struct Address *addr = NULL;
   char *err = NULL;
   int rc;
-  int msgcount; // for L10N with ngettext
+  int msg_count = 0;
 
-  /* RFC5322 mandates a From: header, so warn before bouncing
-   * messages without one */
-  if (e)
+  struct EmailNode *en = NULL;
+  STAILQ_FOREACH(en, el, entries)
   {
-    msgcount = 1;
-    if (!e->env->from)
-    {
+    /* RFC5322 mandates a From: header,
+     * so warn before bouncing messages without one */
+    if (!en->email->env->from)
       mutt_error(_("Warning: message contains no From: header"));
-    }
-  }
-  else if (Context)
-  {
-    msgcount = 0; // count the precise number of messages.
-    for (rc = 0; rc < Context->mailbox->msg_count; rc++)
-    {
-      if (message_is_tagged(Context, rc) && !Context->mailbox->emails[rc]->env->from)
-      {
-        msgcount++;
-        if (!Context->mailbox->emails[rc]->env->from)
-        {
-          mutt_error(_("Warning: message contains no From: header"));
-          break;
-        }
-      }
-    }
+
+    msg_count++;
   }
-  else
-    msgcount = 0;
 
-  if (e)
+  if (msg_count == 1)
     mutt_str_strfcpy(prompt, _("Bounce message to: "), sizeof(prompt));
   else
     mutt_str_strfcpy(prompt, _("Bounce tagged messages to: "), sizeof(prompt));
@@ -435,7 +421,7 @@ void ci_bounce_message(struct Email *e)
 
 #define EXTRA_SPACE (15 + 7 + 2)
   snprintf(scratch, sizeof(scratch),
-           ngettext("Bounce message to %s", "Bounce messages to %s", msgcount), buf);
+           ngettext("Bounce message to %s", "Bounce messages to %s", msg_count), buf);
 
   if (mutt_strwidth(prompt) > MuttMessageWindow->cols - EXTRA_SPACE)
   {
@@ -450,17 +436,33 @@ void ci_bounce_message(struct Email *e)
   {
     mutt_addr_free(&addr);
     mutt_window_clearline(MuttMessageWindow, 0);
-    mutt_message(ngettext("Message not bounced", "Messages not bounced", msgcount));
+    mutt_message(ngettext("Message not bounced", "Messages not bounced", msg_count));
     return;
   }
 
   mutt_window_clearline(MuttMessageWindow, 0);
 
-  rc = mutt_bounce_message(NULL, e, addr);
+  struct Message *msg = NULL;
+  STAILQ_FOREACH(en, el, entries)
+  {
+    msg = mx_msg_open(m, en->email->msgno);
+    if (!msg)
+    {
+      rc = -1;
+      break;
+    }
+
+    rc = mutt_bounce_message(msg->fp, en->email, addr);
+    mx_msg_close(m, &msg);
+
+    if (rc < 0)
+      break;
+  }
+
   mutt_addr_free(&addr);
   /* If no error, or background, display message. */
   if ((rc == 0) || (rc == S_BKG))
-    mutt_message(ngettext("Message bounced", "Messages bounced", msgcount));
+    mutt_message(ngettext("Message bounced", "Messages bounced", msg_count));
 }
 
 /**
@@ -490,7 +492,7 @@ static void pipe_set_flags(bool decode, bool print, int *cmflags, int *chflags)
 
 /**
  * pipe_msg - Pipe a message
- * @param e      Email
+ * @param e      Email to pipe
  * @param fp     File to write to
  * @param decode If true, decode the message
  * @param print  If true, message is for printing
index 6d18b0969114cb88d0b46b59139ed2fad2522c0f..d6ecf22244df76ab916d8e5fc1fda19c5331181f 100644 (file)
@@ -41,7 +41,7 @@ extern bool          PrintDecode;
 extern bool          PrintSplit;
 extern bool          PromptAfter;
 
-void ci_bounce_message(struct Email *e);
+void ci_bounce_message(struct Mailbox *m, struct EmailList *el);
 void mutt_check_stats(void);
 bool mutt_check_traditional_pgp(struct Email *e, int *redraw);
 void mutt_display_address(struct Envelope *env);
diff --git a/index.c b/index.c
index 20cf45d649d49d1e4d8e17862712a8a00fc96985..4d82406d0cec134a62f31600f760fdb93e33499e 100644 (file)
--- a/index.c
+++ b/index.c
@@ -2919,12 +2919,17 @@ int mutt_index_menu(void)
          */
 
       case OP_BOUNCE_MESSAGE:
-
+      {
         CHECK_ATTACH;
         CHECK_MSGCOUNT;
         CHECK_VISIBLE;
-        ci_bounce_message(tag ? NULL : CUR_EMAIL);
+
+        struct EmailList el = STAILQ_HEAD_INITIALIZER(el);
+        el_add_tagged(&el, Context, CUR_EMAIL, tag);
+        ci_bounce_message(Context->mailbox, &el);
+        el_free(&el);
         break;
+      }
 
       case OP_CREATE_ALIAS:
 
diff --git a/pager.c b/pager.c
index 65f1f7307f58e7ad1a99fa272f807f77f26c30a7..16ba39169b4980d0689ed48956def46c2b946c71 100644 (file)
--- a/pager.c
+++ b/pager.c
@@ -2930,13 +2930,21 @@ int mutt_pager(const char *banner, const char *fname, int flags, struct Pager *e
          */
 
       case OP_BOUNCE_MESSAGE:
+      {
+        struct Mailbox *m = Context ? Context->mailbox : NULL;
         CHECK_MODE(IsEmail(extra) || IsMsgAttach(extra))
         CHECK_ATTACH;
         if (IsMsgAttach(extra))
-          mutt_attach_bounce(extra->fp, extra->actx, extra->bdy);
+          mutt_attach_bounce(m, extra->fp, extra->actx, extra->bdy);
         else
-          ci_bounce_message(extra->email);
+        {
+          struct EmailList el = STAILQ_HEAD_INITIALIZER(el);
+          el_add_email(&el, extra->email);
+          ci_bounce_message(m, &el);
+          el_free(&el);
+        }
         break;
+      }
 
       case OP_RESEND:
         CHECK_MODE(IsEmail(extra) || IsMsgAttach(extra))
index dc102a9f9ca99ec80e9a3b285bbab29dce90e832..e8c3992a6cf09faaf4638b1e9e3de1876e46e588 100644 (file)
@@ -1526,7 +1526,7 @@ void mutt_view_attachments(struct Email *e)
 
       case OP_BOUNCE_MESSAGE:
         CHECK_ATTACH;
-        mutt_attach_bounce(CURATTACH->fp, actx,
+        mutt_attach_bounce(m, CURATTACH->fp, actx,
                            menu->tagprefix ? NULL : CURATTACH->content);
         menu->redraw = REDRAW_FULL;
         break;
index 6c632fa52e5619c20b901950c55182fca481ae9d..674113938265cc536a3dcc405390ce9789c86819 100644 (file)
--- a/recvcmd.c
+++ b/recvcmd.c
@@ -156,12 +156,16 @@ static short count_tagged_children(struct AttachCtx *actx, short i)
 
 /**
  * mutt_attach_bounce - Bounce function, from the attachment menu
+ * @param m    Mailbox
  * @param fp   Handle of message
  * @param actx Attachment context
  * @param cur  Body of email
  */
-void mutt_attach_bounce(FILE *fp, struct AttachCtx *actx, struct Body *cur)
+void mutt_attach_bounce(struct Mailbox *m, FILE *fp, struct AttachCtx *actx, struct Body *cur)
 {
+  if (!m || !fp || !actx)
+    return;
+
   char prompt[STRING];
   char buf[HUGE_STRING];
   char *err = NULL;
index 08a47464d1c780c517fc55fce42f164e38fc152b..99d8aa773738df9b17d9c066d387a8d9ac1f4e3e 100644 (file)
--- a/recvcmd.h
+++ b/recvcmd.h
@@ -32,7 +32,7 @@ struct Email;
 /* These Config Variables are only used in recvcmd.c */
 extern unsigned char MimeForwardRest;
 
-void mutt_attach_bounce(FILE *fp, struct AttachCtx *actx, struct Body *cur);
+void mutt_attach_bounce(struct Mailbox *m, FILE *fp, struct AttachCtx *actx, struct Body *cur);
 void mutt_attach_resend(FILE *fp, struct AttachCtx *actx, struct Body *cur);
 void mutt_attach_forward(FILE *fp, struct Email *e, struct AttachCtx *actx, struct Body *cur, int flags);
 void mutt_attach_reply(FILE *fp, struct Email *e, struct AttachCtx *actx, struct Body *cur, int flags);
index 42094d674f9b0db96b2818734d2146e892b58f42..6dfd20135ef2b1ce7be9eccd4e8f78e0c6c16c85 100644 (file)
--- a/sendlib.c
+++ b/sendlib.c
@@ -2956,34 +2956,18 @@ void mutt_unprepare_envelope(struct Envelope *env)
 static int bounce_message(FILE *fp, struct Email *e, struct Address *to,
                           const char *resent_from, struct Address *env_from)
 {
-  int rc = 0;
-  FILE *f = NULL;
-  char tempfile[PATH_MAX];
-  struct Message *msg = NULL;
-
   if (!e)
-  {
-    /* Try to bounce each message out, aborting if we get any failures. */
-    for (int i = 0; i < Context->mailbox->msg_count; i++)
-      if (message_is_tagged(Context, i))
-        rc |= bounce_message(fp, Context->mailbox->emails[i], to, resent_from, env_from);
-    return rc;
-  }
-
-  /* If we failed to open a message, return with error */
-  if (!fp && !(msg = mx_msg_open(Context->mailbox, e->msgno)))
     return -1;
 
-  if (!fp)
-    fp = msg->fp;
+  int rc = 0;
+  char tempfile[PATH_MAX];
 
   mutt_mktemp(tempfile, sizeof(tempfile));
-  f = mutt_file_fopen(tempfile, "w");
+  FILE *f = mutt_file_fopen(tempfile, "w");
   if (f)
   {
     char date[SHORT_STRING];
     int ch_flags = CH_XMIT | CH_NONEWLINE | CH_NOQFROM;
-    char *msgid_str = NULL;
 
     if (!BounceDelivered)
       ch_flags |= CH_WEED_DELIVERED;
@@ -2991,14 +2975,14 @@ static int bounce_message(FILE *fp, struct Email *e, struct Address *to,
     fseeko(fp, e->offset, SEEK_SET);
     fprintf(f, "Resent-From: %s", resent_from);
     fprintf(f, "\nResent-%s", mutt_date_make_date(date, sizeof(date)));
-    msgid_str = gen_msgid();
+    char *msgid_str = gen_msgid();
     fprintf(f, "Resent-Message-ID: %s\n", msgid_str);
+    FREE(&msgid_str);
     fputs("Resent-To: ", f);
     mutt_write_address_list(to, f, 11, 0);
     mutt_copy_header(fp, e, f, ch_flags, NULL);
     fputc('\n', f);
     mutt_file_copy_bytes(fp, f, e->content->length);
-    FREE(&msgid_str);
     if (mutt_file_fclose(&f) != 0)
     {
       mutt_perror(tempfile);
@@ -3009,14 +2993,11 @@ static int bounce_message(FILE *fp, struct Email *e, struct Address *to,
     if (SmtpUrl)
       rc = mutt_smtp_send(env_from, to, NULL, NULL, tempfile, e->content->encoding == ENC_8BIT);
     else
-#endif /* USE_SMTP */
+#endif
       rc = mutt_invoke_sendmail(env_from, to, NULL, NULL, tempfile,
                                 e->content->encoding == ENC_8BIT);
   }
 
-  if (msg)
-    mx_msg_close(Context->mailbox, &msg);
-
   return rc;
 }
 
@@ -3030,6 +3011,9 @@ static int bounce_message(FILE *fp, struct Email *e, struct Address *to,
  */
 int mutt_bounce_message(FILE *fp, struct Email *e, struct Address *to)
 {
+  if (!fp || !e || !to)
+    return -1;
+
   const char *fqdn = mutt_fqdn(true);
   char resent_from[STRING];
   char *err = NULL;