aboutsummaryrefslogtreecommitdiff
blob: 44dc99e39cdb8e365a88f315be3b3d8c89c22b92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
diff -urN mutt-1.5.21.orig//compose.c mutt-1.5.21//compose.c
--- mutt-1.5.21.orig//compose.c	2011-06-26 20:17:58.671443063 -0500
+++ mutt-1.5.21//compose.c	2011-06-26 20:16:57.061738838 -0500
@@ -1370,7 +1370,7 @@
          if (msg->content->next)
            msg->content = mutt_make_multipart (msg->content);
 
-         if (mutt_write_fcc (fname, msg, NULL, 0, NULL) < 0)
+         if (mutt_write_fcc (fname, msg) < 0)
            msg->content = mutt_remove_multipart (msg->content);
          else
            mutt_message _("Message written.");
diff -urN mutt-1.5.21.orig//protos.h mutt-1.5.21//protos.h
--- mutt-1.5.21.orig//protos.h	2011-06-26 20:17:59.051441238 -0500
+++ mutt-1.5.21//protos.h	2011-06-26 20:16:57.071738790 -0500
@@ -367,7 +367,8 @@
 void mutt_update_num_postponed (void);
 int mutt_wait_filter (pid_t);
 int mutt_which_case (const char *);
-int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int, char *);
+int mutt_write_fcc (const char *path, HEADER *hdr);
+int mutt_write_postponed (const char *path, HEADER *hdr, const char *msgid, char *fcc);
 int mutt_write_mime_body (BODY *, FILE *);
 int mutt_write_mime_header (BODY *, FILE *);
 int mutt_write_one_header (FILE *fp, const char *tag, const char *value, const char *pfx, int wraplen, int flags);
diff -urN mutt-1.5.21.orig//send.c mutt-1.5.21//send.c
--- mutt-1.5.21.orig//send.c	2011-06-26 20:17:59.051441238 -0500
+++ mutt-1.5.21//send.c	2011-06-26 20:16:57.071738790 -0500
@@ -1684,7 +1684,7 @@
       mutt_prepare_envelope (msg->env, 0);
       mutt_env_to_idna (msg->env, NULL, NULL);	/* Handle bad IDNAs the next time. */
 
-      if (!Postponed || mutt_write_fcc (NONULL (Postponed), msg, (cur && (flags & SENDREPLY)) ? cur->env->message_id : NULL, 1, fcc) < 0)
+      if (!Postponed || mutt_write_postponed (NONULL (Postponed), msg, (cur && (flags & SENDREPLY)) ? cur->env->message_id : NULL, fcc) < 0)
       {
 	msg->content = mutt_remove_multipart (msg->content);
 	decode_descriptions (msg->content);
@@ -1882,7 +1882,7 @@
        * message was first postponed.
        */
       msg->received = time (NULL);
-      if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1)
+      if (mutt_write_fcc (fcc, msg) == -1)
       {
 	/*
 	 * Error writing FCC, we should abort sending.
diff -urN mutt-1.5.21.orig//sendlib.c mutt-1.5.21//sendlib.c
--- mutt-1.5.21.orig//sendlib.c	2011-06-26 20:17:59.051441238 -0500
+++ mutt-1.5.21//sendlib.c	2011-06-26 20:17:17.151642453 -0500
@@ -2575,6 +2575,165 @@
   rfc2047_decode (&env->x_label);
 }
 
+static int count_length_lines (FILE *fp, LOFF_T *content_lengthp, int *linesp)
+{
+  char sasha[LONG_STRING];
+  int lines = 0;
+
+  /* count the number of lines */
+  while (fgets (sasha, sizeof (sasha), fp) != NULL)
+    lines++;
+  if (ferror (fp))
+    return -1;
+
+  *linesp = lines;
+  *content_lengthp = (LOFF_T) ftello (fp);
+
+  return 0;
+}
+
+/* state maintained between mutt_start_message and
+ * mutt_finish_message, opaque to callers.
+ */
+struct mutt_message_handle
+{
+  FILE *tempfp;
+  char tempfile[_POSIX_PATH_MAX];
+  LOFF_T content_length;
+  int lines;
+  struct stat st;
+  int passthru;
+};
+
+static struct mutt_message_handle *
+mutt_start_message (const char *path, HEADER *hdr, 
+                    CONTEXT *ctxp, MESSAGE **msgp, int passthru)
+{
+  struct mutt_message_handle *mh;
+
+  if (mx_open_mailbox (path, M_APPEND | M_QUIET, ctxp) == NULL)
+  {
+    dprint (1, (debugfile, "mutt_start_message: unable to open mailbox %s"
+                "in append-mode, aborting.\n", path));
+    return NULL;
+  }
+
+  mh = safe_calloc (1, sizeof (*mh));
+  mh->passthru = passthru;
+
+  if (ctxp->magic == M_MMDF || ctxp->magic == M_MBOX)
+  {
+    /* remember new mail status before appending message */
+    stat (path, &mh->st);
+
+    if (!passthru) {
+      /* need to add a Content-Length field to avoid problems
+       * where a line in the message body begins with "From "
+       */
+      mutt_mktemp (mh->tempfile, sizeof (mh->tempfile));
+      mh->tempfp = safe_fopen (mh->tempfile, "w+");
+      if (!mh->tempfp)
+      {
+        mutt_perror (mh->tempfile);
+        dprint (1, (debugfile, "mutt_start_message: failed "
+                    "to open tempfile, aborting.\n"));
+        goto error_out;
+      }
+
+      mutt_write_mime_body (hdr->content, mh->tempfp);
+
+      /* make sure the last line ends with a newline.  Emacs doesn't ensure
+       * this will happen, and it can cause problems parsing the mailbox   
+       * later.
+       */
+      fseek (mh->tempfp, -1, 2);
+      if (fgetc (mh->tempfp) != '\n')
+      {
+        fseek (mh->tempfp, 0, 2);
+        fputc ('\n', mh->tempfp);
+      }
+      if (fflush (mh->tempfp) || ferror (mh->tempfp))
+      {
+        dprint (1, (debugfile, "mutt_start_message: failed "
+                    "to write tempfile, aborting.\n"));
+        goto error_out;
+      }
+
+      rewind (mh->tempfp);
+
+      if (count_length_lines (mh->tempfp, &mh->content_length, &mh->lines))
+      {
+        mutt_perror (mh->tempfile);
+        dprint (1, (debugfile, "mutt_start_message: failed "
+                    "to count message length, aborting.\n"));
+        goto error_out;
+      }
+    }
+  }
+
+  if ((*msgp = mx_open_new_message (ctxp, hdr, M_ADD_FROM)) == NULL)
+  {
+    dprint (1, (debugfile, "mutt_start_message: mx_open_new_message "
+                "failed in %s, aborting.\n", path));
+    goto error_out;
+  }
+  return mh;
+
+error_out:
+  /* error message already provided, just clean up */
+  if (mh->tempfp)
+  {
+    safe_fclose (&mh->tempfp);
+    unlink (mh->tempfile);
+  }
+  FREE (&mh);
+  mx_close_mailbox (ctxp, NULL);
+  return NULL;
+}
+
+static int mutt_finish_message (struct mutt_message_handle *mh,
+                                const char *path, HEADER *hdr,
+                                CONTEXT *ctxp, MESSAGE **msgp, int skip_buffy)
+{
+  int ret = 0;
+
+  if (mh->tempfp)
+  {
+    fprintf ((*msgp)->fp, "Content-Length: " OFF_T_FMT "\n", mh->content_length);
+    fprintf ((*msgp)->fp, "Lines: %d\n\n", mh->lines); /* NOTE double newline */
+
+    /* copy the body */
+    rewind (mh->tempfp);
+    ret = mutt_copy_stream (mh->tempfp, (*msgp)->fp);
+    if (safe_fclose (&mh->tempfp))
+      ret = -1;
+
+    /* if there was an error, leave the temp version */
+    if (!ret)
+      unlink (mh->tempfile);
+  }
+  else
+  {
+    if (!mh->passthru)
+    {
+      fputc ('\n', (*msgp)->fp); /* finish off the header */
+      ret = mutt_write_mime_body (hdr->content, (*msgp)->fp);
+    }
+    skip_buffy = 1; /* only needed for file-based mailboxes */
+  }
+
+  ret |= mx_commit_message (*msgp, ctxp);
+  mx_close_message (msgp);
+  mx_close_mailbox (ctxp, NULL);
+
+  if (!skip_buffy)
+    mutt_buffy_cleanup (path, &mh->st);
+
+  FREE (&mh);
+
+  return ret;
+}
+
 static int _mutt_bounce_message (FILE *fp, HEADER *h, ADDRESS *to, const char *resent_from,
 				  ADDRESS *env_from)
 {
@@ -2740,81 +2899,60 @@
   }
 }
 
-int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post, char *fcc)
+int mutt_write_fcc (const char *path, HEADER *hdr)
 {
+  struct mutt_message_handle *mh;
   CONTEXT f;
   MESSAGE *msg;
-  char tempfile[_POSIX_PATH_MAX];
-  FILE *tempfp = NULL;
-  int r, need_buffy_cleanup = 0;
-  struct stat st;
-  char buf[SHORT_STRING];
 
-  if (post)
-    set_noconv_flags (hdr->content, 1);
+  hdr->read = 1; /* make sure to put it in the `cur' directory (maildir) */
 
-  if (mx_open_mailbox (path, M_APPEND | M_QUIET, &f) == NULL)
-  {
-    dprint (1, (debugfile, "mutt_write_fcc(): unable to open mailbox %s in append-mode, aborting.\n",
-		path));
-    return (-1);
-  }
+  mh = mutt_start_message (path, hdr, &f, &msg, 0);
+  if (!mh)
+    return -1;
 
-  /* We need to add a Content-Length field to avoid problems where a line in
-   * the message body begins with "From "
-   */
-  if (f.magic == M_MMDF || f.magic == M_MBOX)
-  {
-    mutt_mktemp (tempfile, sizeof (tempfile));
-    if ((tempfp = safe_fopen (tempfile, "w+")) == NULL)
-    {
-      mutt_perror (tempfile);
-      mx_close_mailbox (&f, NULL);
-      return (-1);
-    }
-    /* remember new mail status before appending message */
-    need_buffy_cleanup = 1;
-    stat (path, &st);
-  }
+  mutt_write_rfc822_header (msg->fp, hdr->env, hdr->content, 0, 0, 1);
+  fprintf (msg->fp, "Status: RO\n");
+
+  return mutt_finish_message (mh, path, hdr, &f, &msg, 0);
+}
+
+int mutt_write_postponed (const char *path, HEADER *hdr, const char *msgid, char *fcc)
+{
+  struct mutt_message_handle *mh;
+  CONTEXT f;
+  MESSAGE *msg;
+  int ret;
+
+  hdr->read = 0; /* make sure to put it in the `new' directory (maildir) */
+  set_noconv_flags (hdr->content, 1);
 
-  hdr->read = !post; /* make sure to put it in the `cur' directory (maildir) */
-  if ((msg = mx_open_new_message (&f, hdr, M_ADD_FROM)) == NULL)
+  mh = mutt_start_message (path, hdr, &f, &msg, 0);
+  if (!mh)
   {
-    mx_close_mailbox (&f, NULL);
-    return (-1);
+    set_noconv_flags (hdr->content, 0);
+    return -1;
   }
 
-  /* post == 1 => postpone message. Set mode = -1 in mutt_write_rfc822_header()
-   * post == 0 => Normal mode. Set mode = 0 in mutt_write_rfc822_header()
-   * */
-  mutt_write_rfc822_header (msg->fp, hdr->env, hdr->content, post ? -post : 0, 0, 1);
+  mutt_write_rfc822_header (msg->fp, hdr->env, hdr->content, -1, 0, 1);
 
-  /* (postponment) if this was a reply of some sort, <msgid> contians the
+  /* if this was a reply of some sort, <msgid> contains the
    * Message-ID: of message replied to.  Save it using a special X-Mutt-
    * header so it can be picked up if the message is recalled at a later
    * point in time.  This will allow the message to be marked as replied if
    * the same mailbox is still open.
    */
-  if (post && msgid)
+  if (msgid)
     fprintf (msg->fp, "X-Mutt-References: %s\n", msgid);
 
-  /* (postponment) save the Fcc: using a special X-Mutt- header so that
+  /* save the Fcc: using a special X-Mutt- header so that
    * it can be picked up when the message is recalled
    */
-  if (post && fcc)
+  if (fcc)
     fprintf (msg->fp, "X-Mutt-Fcc: %s\n", fcc);
 
-  if (f.magic == M_MMDF || f.magic == M_MBOX)
-    fprintf (msg->fp, "Status: RO\n");
-
-  /* mutt_write_rfc822_header() only writes out a Date: header with
-   * mode == 0, i.e. _not_ postponment; so write out one ourself */
-  if (post)
-    fprintf (msg->fp, "%s", mutt_make_date (buf, sizeof (buf)));
-
-  /* (postponment) if the mail is to be signed or encrypted, save this info */
-  if ((WithCrypto & APPLICATION_PGP)
-      && post && (hdr->security & APPLICATION_PGP))
+  /* if the mail is to be signed or encrypted, save this info */
+  if ((WithCrypto & APPLICATION_PGP) && (hdr->security & APPLICATION_PGP))
   {
     fputs ("X-Mutt-PGP: ", msg->fp);
     if (hdr->security & ENCRYPT)
@@ -2830,9 +2968,8 @@
     fputc ('\n', msg->fp);
   }
 
-  /* (postponment) if the mail is to be signed or encrypted, save this info */
-  if ((WithCrypto & APPLICATION_SMIME)
-      && post && (hdr->security & APPLICATION_SMIME))
+  /* if the mail is to be signed or encrypted, save this info */
+  if ((WithCrypto & APPLICATION_SMIME) && (hdr->security & APPLICATION_SMIME))
   {
     fputs ("X-Mutt-SMIME: ", msg->fp);
     if (hdr->security & ENCRYPT) {
@@ -2851,86 +2988,22 @@
   }
 
 #ifdef MIXMASTER
-  /* (postponement) if the mail is to be sent through a mixmaster
+  /* if the mail is to be sent through a mixmaster
    * chain, save that information
    */
-
-  if (post && hdr->chain && hdr->chain)
+  if (hdr->chain)
   {
     LIST *p;
-
     fputs ("X-Mutt-Mix:", msg->fp);
     for (p = hdr->chain; p; p = p->next)
       fprintf (msg->fp, " %s", (char *) p->data);
-
     fputc ('\n', msg->fp);
   }
 #endif
 
-  if (tempfp)
-  {
-    char sasha[LONG_STRING];
-    int lines = 0;
-
-    mutt_write_mime_body (hdr->content, tempfp);
-
-    /* make sure the last line ends with a newline.  Emacs doesn't ensure
-     * this will happen, and it can cause problems parsing the mailbox
-     * later.
-     */
-    fseek (tempfp, -1, 2);
-    if (fgetc (tempfp) != '\n')
-    {
-      fseek (tempfp, 0, 2);
-      fputc ('\n', tempfp);
-    }
-
-    fflush (tempfp);
-    if (ferror (tempfp))
-    {
-      dprint (1, (debugfile, "mutt_write_fcc(): %s: write failed.\n", tempfile));
-      safe_fclose (&tempfp);
-      unlink (tempfile);
-      mx_commit_message (msg, &f);	/* XXX - really? */
-      mx_close_message (&msg);
-      mx_close_mailbox (&f, NULL);
-      return -1;
-    }
-
-    /* count the number of lines */
-    rewind (tempfp);
-    while (fgets (sasha, sizeof (sasha), tempfp) != NULL)
-      lines++;
-    fprintf (msg->fp, "Content-Length: " OFF_T_FMT "\n", (LOFF_T) ftello (tempfp));
-    fprintf (msg->fp, "Lines: %d\n\n", lines);
-
-    /* copy the body and clean up */
-    rewind (tempfp);
-    r = mutt_copy_stream (tempfp, msg->fp);
-    if (fclose (tempfp) != 0)
-      r = -1;
-    /* if there was an error, leave the temp version */
-    if (!r)
-      unlink (tempfile);
-  }
-  else
-  {
-    fputc ('\n', msg->fp); /* finish off the header */
-    r = mutt_write_mime_body (hdr->content, msg->fp);
-  }
-
-  if (mx_commit_message (msg, &f) != 0)
-    r = -1;
-  mx_close_message (&msg);
-  mx_close_mailbox (&f, NULL);
-
-  if (!post && need_buffy_cleanup)
-    mutt_buffy_cleanup (path, &st);
-
-  if (post)
-    set_noconv_flags (hdr->content, 0);
-
-  return r;
+  ret = mutt_finish_message (mh, path, hdr, &f, &msg, 0);
+  set_noconv_flags (hdr->content, 0);
+  return ret;
 }
 
 #ifdef USE_SENDBOX