<<< Date Index >>>     <<< Thread Index >>>

[PATCH 2 of 2] Add sendbox support to bounce path



# HG changeset patch
# User agriffis@xxxxxxxxxxxxxxx
# Date 1155133915 14400
# Node ID db5c1f541f34725cdef29275e30d27968cee6f0a
# Parent  68b578d136acaa26471c0cf6c3d405c13142dfea
Add sendbox support to bounce path

diff -r 68b578d136ac -r db5c1f541f34 copy.c
--- a/copy.c    Wed Aug 09 10:30:33 2006 -0400
+++ b/copy.c    Wed Aug 09 10:31:55 2006 -0400
@@ -62,7 +62,7 @@ mutt_copy_hdr (FILE *in, FILE *out, LOFF
   buf[0] = '\n';
   buf[1] = 0;
 
-  if ((flags & (CH_REORDER | CH_WEED | CH_MIME | CH_DECODE | CH_PREFIX | 
CH_WEED_DELIVERED)) == 0)
+  if ((flags & (CH_REORDER | CH_WEED | CH_MIME | CH_DECODE | CH_PREFIX | 
CH_WEED_DELIVERED | CH_WEED_RESENT_TO)) == 0)
   {
     /* Without these flags to complicate things
      * we can do a more efficient line to line copying
@@ -188,6 +188,9 @@ mutt_copy_hdr (FILE *in, FILE *out, LOFF
        continue;
       if ((flags & CH_WEED_DELIVERED) &&
          ascii_strncasecmp ("Delivered-To:", buf, 13) == 0)
+       continue;
+      if ((flags & CH_WEED_RESENT_TO) &&
+         ascii_strncasecmp ("Resent-To:", buf, 10) == 0)
        continue;
       if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
          (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
diff -r 68b578d136ac -r db5c1f541f34 mutt.h
--- a/mutt.h    Wed Aug 09 10:30:33 2006 -0400
+++ b/mutt.h    Wed Aug 09 10:31:55 2006 -0400
@@ -92,6 +92,7 @@
 #define CH_NOQFROM     (1<<15) /* give CH_FROM precedence over CH_WEED? */
 #define CH_UPDATE_IRT  (1<<16) /* update In-Reply-To: */
 #define CH_UPDATE_REFS (1<<17) /* update References: */
+#define CH_WEED_RESENT_TO (1<<18) /* weed Resent-* headers */
 
 /* flags for mutt_enter_string() */
 #define  M_ALIAS   1      /* do alias "completion" by calling up the 
alias-menu */
diff -r 68b578d136ac -r db5c1f541f34 sendlib.c
--- a/sendlib.c Wed Aug 09 10:30:33 2006 -0400
+++ b/sendlib.c Wed Aug 09 10:31:55 2006 -0400
@@ -2136,52 +2136,102 @@ void mutt_unprepare_envelope (ENVELOPE *
   rfc2047_decode (&env->subject);
 }
 
-static int _mutt_bounce_message (FILE *fp, HEADER *h, ADDRESS *to, const char 
*resent_from,
-                                 ADDRESS *env_from)
+static int _mutt_bounce_message (FILE *in, HEADER *hdr, ADDRESS *to,
+                                const char *resent_from, ADDRESS *env_from)
 {
   int i, ret = 0;
-  FILE *f;
-  char date[SHORT_STRING], tempfile[_POSIX_PATH_MAX];
-  MESSAGE *msg = NULL;
-
-  if (!h)
-  {
-         /* Try to bounce each message out, aborting if we get any failures. */
+  FILE *out;
+  char date[SHORT_STRING];
+  char tempfile[_POSIX_PATH_MAX];
+  MESSAGE *msg = NULL, *smsg = NULL;
+  CONTEXT sctx;
+  int ch_flags;
+
+  if (!hdr)
+  {
+    /* Try to bounce each message out, aborting if we get any failures. */
     for (i=0; i<Context->msgcount; i++)
       if (Context->hdrs[i]->tagged)
-        ret |= _mutt_bounce_message (fp, Context->hdrs[i], to, resent_from, 
env_from);
+        ret |= _mutt_bounce_message (in, Context->hdrs[i], to, resent_from,
+                                    env_from);
     return ret;
   }
 
   /* If we failed to open a message, return with error */
-  if (!fp && (msg = mx_open_message (Context, h->msgno)) == NULL)
-    return -1;
-
-  if (!fp) fp = msg->fp;
-
-  mutt_mktemp (tempfile);
-  if ((f = safe_fopen (tempfile, "w")) != NULL)
-  {
-    int ch_flags = CH_XMIT | CH_NONEWLINE | CH_NOQFROM;
-    
-    if (!option (OPTBOUNCEDELIVERED))
-      ch_flags |= CH_WEED_DELIVERED;
-    
-    fseeko (fp, h->offset, 0);
-    fprintf (f, "Resent-From: %s", resent_from);
-    fprintf (f, "\nResent-%s", mutt_make_date (date, sizeof(date)));
-    fprintf (f, "Resent-Message-ID: %s\n", mutt_gen_msgid());
-    fputs ("Resent-To: ", f);
-    mutt_write_address_list (to, f, 11, 0);
-    mutt_copy_header (fp, h, f, ch_flags, NULL);
-    fputc ('\n', f);
-    mutt_copy_bytes (fp, f, h->content->length);
-    fclose (f);
-
+  if (!in) {
+    if ((msg = mx_open_message (Context, hdr->msgno)) == NULL)
+      return -1;
+    in = msg->fp;
+  }
+
+  ch_flags = CH_XMIT | CH_NONEWLINE | CH_NOQFROM;
+  if (!option (OPTBOUNCEDELIVERED))
+    ch_flags |= CH_WEED_DELIVERED;
+
+  if (option (OPTUSESENDBOX)) {
+    /* some imap servers (e.g. courier) can send mail by saving to a special
+     * folder, see http://www.inter7.com/courierimap/INSTALL.html#imapsend
+     */
+    if (mx_open_mailbox (NONULL (Sendbox), M_APPEND | M_QUIET, &sctx) == NULL)
+    {
+      dprint (1, (debugfile, "_mutt_bounce_message(): unable to open mailbox 
%s"
+                 "in append-mode, aborting.\n", NONULL (Sendbox)));
+      ret = -1;
+      goto close_msg;
+    }
+    if ((smsg = mx_open_new_message (&sctx, hdr, M_ADD_FROM)) == NULL)
+    {
+      dprint (1, (debugfile, "_mutt_bounce_message(): mx_open_new_message "
+                 "failed in %s, aborting.\n", NONULL (Sendbox)));
+      mx_close_mailbox (&sctx, NULL);
+      ret = -1;
+      goto close_msg;
+    }
+    out = smsg->fp;
+
+    /* when using sendbox, Resent-To: headers will be unioned by the MTA to
+     * determine the recipient, so weed any old ones
+     */
+    ch_flags |= CH_WEED_RESENT_TO;
+  } else {
+    /* normal send path, create a temporary message which is the original
+     * message with Resent header fields prepended
+     */
+    mutt_mktemp (tempfile);
+    if ((out = safe_fopen (tempfile, "w")) == NULL) {
+      mutt_perror (tempfile);
+      ret = -1;
+      goto close_msg;
+    }
+  }
+
+  /* prepend the Resent header fields */
+  fprintf (out, "Resent-From: %s", resent_from);
+  fprintf (out, "\nResent-%s", mutt_make_date (date, sizeof(date)));
+  fprintf (out, "Resent-Message-ID: %s\n", mutt_gen_msgid());
+  fputs ("Resent-To: ", out);
+  mutt_write_address_list (to, out, 11, 0);
+
+  /* copy original message */
+  fseeko (in, hdr->offset, 0);
+  mutt_copy_header (in, hdr, out, ch_flags, NULL);
+  fputc ('\n', out);
+  mutt_copy_bytes (in, out, hdr->content->length);
+
+  if (smsg) {
+    /* complete sending via Sendbox */
+    if (mx_commit_message (smsg, &sctx) != 0)
+       ret = -1;
+    mx_close_message (&smsg);
+    mx_close_mailbox (&sctx, NULL);
+  } else {
+    /* complete normal send */
+    fclose (out);
     ret = mutt_invoke_sendmail (env_from, to, NULL, NULL, tempfile,
-                               h->content->encoding == ENC8BIT);
-  }
-
+                               hdr->content->encoding == ENC8BIT);
+  }
+
+close_msg:
   if (msg)
     mx_close_message (&msg);