Rocco Rutte:
[apps/madmutt.git] / copy.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include "mutt.h"
15 #include "ascii.h"
16 #include "handler.h"
17 #include "mx.h"
18 #include "copy.h"
19 #include "rfc2047.h"
20 #include "mime.h"
21 #include "mutt_crypt.h"
22 #include "mutt_idna.h"
23
24 #include "lib/mem.h"
25 #include "lib/str.h"
26 #include "lib/debug.h"
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <unistd.h>             /* needed for SEEK_SET under SunOS 4.1.4 */
32
33 static int address_header_decode (char **str);
34 static int copy_delete_attach (BODY * b, FILE * fpin, FILE * fpout,
35                                char *date);
36
37 /* Ok, the only reason for not merging this with mutt_copy_header()
38  * below is to avoid creating a HEADER structure in message_handler().
39  */
40 int
41 mutt_copy_hdr (FILE* in, FILE* out, LOFF_T off_start, LOFF_T off_end,
42                int flags, const char *prefix) {
43   int from = 0;
44   int this_is_from;
45   int ignore = 0;
46   char buf[STRING];             /* should be long enough to get most fields in one pass */
47   char *nl;
48   LIST *t;
49   char **headers;
50   int hdr_count;
51   int x;
52   char *this_one = NULL;
53   int error;
54   int curline = 0;
55
56   if (ftello (in) != off_start)
57     fseeko (in, off_start, 0);
58
59   buf[0] = '\n';
60   buf[1] = 0;
61
62   if ((flags &
63        (CH_REORDER | CH_WEED | CH_MIME | CH_DECODE | CH_PREFIX |
64         CH_WEED_DELIVERED)) == 0) {
65     /* Without these flags to complicate things
66      * we can do a more efficient line to line copying
67      */
68     while (ftello (in) < off_end) {
69       nl = strchr (buf, '\n');
70
71       if ((fgets (buf, sizeof (buf), in)) == NULL)
72         break;
73
74       /* Is it the begining of a header? */
75       if (nl && buf[0] != ' ' && buf[0] != '\t') {
76         ignore = 1;
77         if (!from && str_ncmp ("From ", buf, 5) == 0) {
78           if ((flags & CH_FROM) == 0)
79             continue;
80           from = 1;
81         }
82         else if (flags & (CH_NOQFROM) &&
83                  ascii_strncasecmp (">From ", buf, 6) == 0)
84           continue;
85
86         else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
87           break;                /* end of header */
88
89         if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
90             (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
91              ascii_strncasecmp ("X-Status:", buf, 9) == 0))
92           continue;
93         if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
94             (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
95              ascii_strncasecmp ("Lines:", buf, 6) == 0))
96           continue;
97         if ((flags & CH_UPDATE_REFS) &&
98             ascii_strncasecmp ("References:", buf, 11) == 0)
99           continue;
100         if ((flags & CH_UPDATE_IRT) &&
101             ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
102           continue;
103         ignore = 0;
104       }
105
106       if (!ignore && fputs (buf, out) == EOF)
107         return (-1);
108     }
109     return 0;
110   }
111
112   hdr_count = 1;
113   x = 0;
114   error = FALSE;
115
116   /* We are going to read and collect the headers in an array
117    * so we are able to do re-ordering.
118    * First count the number of entries in the array
119    */
120   if (flags & CH_REORDER) {
121     for (t = HeaderOrderList; t; t = t->next) {
122       debug_print (1, ("Reorder list: %s\n", t->data));
123       hdr_count++;
124     }
125   }
126
127   debug_print (1, ("WEED is %s\n", (flags & CH_WEED) ? "Set" : "Not"));
128
129   headers = mem_calloc (hdr_count, sizeof (char *));
130
131   /* Read all the headers into the array */
132   while (ftello (in) < off_end) {
133     nl = strchr (buf, '\n');
134
135     /* Read a line */
136     if ((fgets (buf, sizeof (buf), in)) == NULL)
137       break;
138
139     /* Is it the begining of a header? */
140     if (nl && buf[0] != ' ' && buf[0] != '\t') {
141
142       /* set curline to 1 for To:/Cc:/Bcc: and 0 otherwise */
143       curline = (flags & CH_WEED) && (ascii_strncmp ("To:", buf, 3) == 0 ||
144                                       ascii_strncmp ("Cc:", buf, 3) == 0 ||
145                                       ascii_strncmp ("Bcc:", buf, 4) == 0);
146
147       /* Do we have anything pending? */
148       if (this_one) {
149         if (flags & CH_DECODE) {
150           if (!address_header_decode (&this_one))
151             rfc2047_decode (&this_one);
152         }
153
154         if (!headers[x])
155           headers[x] = this_one;
156         else {
157           mem_realloc (&headers[x], str_len (headers[x]) +
158                         str_len (this_one) + sizeof (char));
159           strcat (headers[x], this_one);        /* __STRCAT_CHECKED__ */
160           mem_free (&this_one);
161         }
162
163         this_one = NULL;
164       }
165
166       ignore = 1;
167       this_is_from = 0;
168       if (!from && str_ncmp ("From ", buf, 5) == 0) {
169         if ((flags & CH_FROM) == 0)
170           continue;
171         this_is_from = from = 1;
172       }
173       else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
174         break;                  /* end of header */
175
176       /* note: CH_FROM takes precedence over header weeding. */
177       if (!((flags & CH_FROM) && (flags & CH_FORCE_FROM) && this_is_from) &&
178           (flags & CH_WEED) &&
179           mutt_matches_ignore (buf, Ignore) &&
180           !mutt_matches_ignore (buf, UnIgnore))
181         continue;
182       if ((flags & CH_WEED_DELIVERED) &&
183           ascii_strncasecmp ("Delivered-To:", buf, 13) == 0)
184         continue;
185       if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
186           (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
187            ascii_strncasecmp ("X-Status:", buf, 9) == 0))
188         continue;
189       if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
190           (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
191            ascii_strncasecmp ("Lines:", buf, 6) == 0))
192         continue;
193       if ((flags & CH_MIME) &&
194           ((ascii_strncasecmp ("content-", buf, 8) == 0 &&
195             (ascii_strncasecmp ("transfer-encoding:", buf + 8, 18) == 0 ||
196              ascii_strncasecmp ("type:", buf + 8, 5) == 0)) ||
197            ascii_strncasecmp ("mime-version:", buf, 13) == 0))
198         continue;
199       if ((flags & CH_UPDATE_REFS) &&
200           ascii_strncasecmp ("References:", buf, 11) == 0)
201         continue;
202       if ((flags & CH_UPDATE_IRT) &&
203           ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
204         continue;
205
206       /* Find x -- the array entry where this header is to be saved */
207       if (flags & CH_REORDER) {
208         for (t = HeaderOrderList, x = 0; (t); t = t->next, x++) {
209           if (!ascii_strncasecmp (buf, t->data, str_len (t->data))) {
210             debug_print (2, ("Reorder: %s matches %s\n", t->data, buf));
211             break;
212           }
213         }
214       }
215
216       ignore = 0;
217     }                           /* If beginning of header */
218
219     if (!ignore) {
220       debug_print (2, ("Reorder: x = %d; hdr_count = %d\n", x, hdr_count));
221       if (!this_one)
222         this_one = str_dup (buf);
223       /* we do want to see all lines if this header doesn't feature
224        * abbreviations (curline is 0), $max_display_recips is 0 and
225        * while the number hasn't reached $max_display_recips yet */
226       else if (curline == 0 || MaxDispRecips == 0 || ++curline <= MaxDispRecips) {
227         mem_realloc (&this_one,
228                       str_len (this_one) + str_len (buf) +
229                       sizeof (char));
230         strcat (this_one, buf); /* __STRCAT_CHECKED__ */
231       /* only for the first line which doesn't exeeds
232        * $max_display_recips: abbreviate it */
233       } else if (curline == MaxDispRecips+1) {
234         mem_realloc (&this_one, str_len (this_one) + 5);
235         strcat (this_one, " ...");
236       }
237     }
238   }                             /* while (ftello (in) < off_end) */
239
240   /* Do we have anything pending?  -- XXX, same code as in above in the loop. */
241   if (this_one) {
242     if (flags & CH_DECODE) {
243       if (!address_header_decode (&this_one))
244         rfc2047_decode (&this_one);
245     }
246
247     if (!headers[x])
248       headers[x] = this_one;
249     else {
250       mem_realloc (&headers[x], str_len (headers[x]) +
251                     str_len (this_one) + sizeof (char));
252       strcat (headers[x], this_one);    /* __STRCAT_CHECKED__ */
253       mem_free (&this_one);
254     }
255
256     this_one = NULL;
257   }
258
259   /* Now output the headers in order */
260   for (x = 0; x < hdr_count; x++) {
261     if (headers[x]) {
262 #if 0
263       if (flags & CH_DECODE)
264         rfc2047_decode (&headers[x]);
265 #endif
266
267       /* We couldn't do the prefixing when reading because RFC 2047
268        * decoding may have concatenated lines.
269        */
270       if (flags & CH_PREFIX) {
271         char *ch = headers[x];
272         int print_prefix = 1;
273
274         while (*ch) {
275           if (print_prefix) {
276             if (fputs (prefix, out) == EOF) {
277               error = TRUE;
278               break;
279             }
280             print_prefix = 0;
281           }
282
283           if (*ch == '\n' && ch[1])
284             print_prefix = 1;
285
286           if (putc (*ch++, out) == EOF) {
287             error = TRUE;
288             break;
289           }
290         }
291         if (error)
292           break;
293       }
294       else {
295         if (fputs (headers[x], out) == EOF) {
296           error = TRUE;
297           break;
298         }
299       }
300     }
301   }
302
303   /* Free in a separate loop to be sure that all headers are freed
304    * in case of error. */
305   for (x = 0; x < hdr_count; x++)
306     mem_free (&headers[x]);
307   mem_free (&headers);
308
309   if (error)
310     return (-1);
311   return (0);
312 }
313
314 /* flags
315         CH_DECODE       RFC2047 header decoding
316         CH_FROM         retain the "From " message separator
317         CH_FORCE_FROM   give CH_FROM precedence over CH_WEED
318         CH_MIME         ignore MIME fields
319         CH_NOLEN        don't write Content-Length: and Lines:
320         CH_NONEWLINE    don't output a newline after the header
321         CH_NOSTATUS     ignore the Status: and X-Status:
322         CH_PREFIX       quote header with $indent_str
323         CH_REORDER      output header in order specified by `hdr_order'
324         CH_TXTPLAIN     generate text/plain MIME headers [hack alert.]
325         CH_UPDATE       write new Status: and X-Status:
326         CH_UPDATE_LEN   write new Content-Length: and Lines:
327         CH_XMIT         ignore Lines: and Content-Length:
328         CH_WEED         do header weeding
329         CH_NOQFROM      ignore ">From " line
330         CH_UPDATE_IRT   update the In-Reply-To: header
331         CH_UPDATE_REFS  update the References: header
332
333    prefix
334         string to use if CH_PREFIX is set
335  */
336
337 int
338 mutt_copy_header (FILE * in, HEADER * h, FILE * out, int flags,
339                   const char *prefix)
340 {
341   char buffer[SHORT_STRING];
342
343   if (h->env)
344     flags |= (h->env->irt_changed ? CH_UPDATE_IRT : 0) |
345       (h->env->refs_changed ? CH_UPDATE_REFS : 0);
346
347   if (mutt_copy_hdr (in, out, h->offset, h->content->offset, flags, prefix) ==
348       -1)
349     return (-1);
350
351   if (flags & CH_TXTPLAIN) {
352     char chsbuf[SHORT_STRING];
353
354     fputs ("MIME-Version: 1.0\n", out);
355     fputs ("Content-Transfer-Encoding: 8bit\n", out);
356     fputs ("Content-Type: text/plain; charset=", out);
357     mutt_canonical_charset (chsbuf, sizeof (chsbuf),
358                             Charset ? Charset : "us-ascii");
359     rfc822_cat (buffer, sizeof (buffer), chsbuf, MimeSpecials);
360     fputs (buffer, out);
361     fputc ('\n', out);
362
363     if (ferror (out) != 0 || feof (out) != 0)
364       return -1;
365
366   }
367
368   if (flags & CH_UPDATE) {
369     if ((flags & CH_NOSTATUS) == 0) {
370       if (h->env->irt_changed && h->env->in_reply_to) {
371         LIST *listp = h->env->in_reply_to;
372
373         if (fputs ("In-Reply-To: ", out) == EOF)
374           return (-1);
375
376         for (; listp; listp = listp->next)
377           if ((fputs (listp->data, out) == EOF) || (fputc (' ', out) == EOF))
378             return (-1);
379
380         if (fputc ('\n', out) == EOF)
381           return (-1);
382       }
383
384       if (h->env->refs_changed && h->env->references) {
385         LIST *listp = h->env->references, *refs = NULL, *t;
386
387         if (fputs ("References: ", out) == EOF)
388           return (-1);
389
390         /* Mutt stores references in reverse order, thus we create
391          * a reordered refs list that we can put in the headers */
392         for (; listp; listp = listp->next, refs = t) {
393           t = (LIST *) mem_malloc (sizeof (LIST));
394           t->data = listp->data;
395           t->next = refs;
396         }
397
398         for (; refs; refs = refs->next)
399           if ((fputs (refs->data, out) == EOF) || (fputc (' ', out) == EOF))
400             return (-1);
401
402         /* clearing refs from memory */
403         for (t = refs; refs; refs = t->next, t = refs)
404           mem_free (&refs);
405
406         if (fputc ('\n', out) == EOF)
407           return (-1);
408       }
409
410       if (h->old || h->read) {
411         if (fputs ("Status: ", out) == EOF)
412           return (-1);
413
414         if (h->read) {
415           if (fputs ("RO", out) == EOF)
416             return (-1);
417         }
418         else if (h->old) {
419           if (fputc ('O', out) == EOF)
420             return (-1);
421         }
422
423         if (fputc ('\n', out) == EOF)
424           return (-1);
425       }
426
427       if (h->flagged || h->replied) {
428         if (fputs ("X-Status: ", out) == EOF)
429           return (-1);
430
431         if (h->replied) {
432           if (fputc ('A', out) == EOF)
433             return (-1);
434         }
435
436         if (h->flagged) {
437           if (fputc ('F', out) == EOF)
438             return (-1);
439         }
440
441         if (fputc ('\n', out) == EOF)
442           return (-1);
443       }
444     }
445   }
446
447   if (flags & CH_UPDATE_LEN && (flags & CH_NOLEN) == 0) {
448     fprintf (out, "Content-Length: " OFF_T_FMT "\n", h->content->length);
449     if (h->lines != 0 || h->content->length == 0)
450       fprintf (out, "Lines: %d\n", h->lines);
451   }
452
453   if ((flags & CH_NONEWLINE) == 0) {
454     if (flags & CH_PREFIX)
455       fputs (prefix, out);
456     if (fputc ('\n', out) == EOF)       /* add header terminator */
457       return (-1);
458   }
459
460   if (ferror (out) || feof (out))
461     return -1;
462
463   return (0);
464 }
465
466 /* Count the number of lines and bytes to be deleted in this body*/
467 static int count_delete_lines (FILE * fp, BODY * b, LOFF_T *length,
468                                size_t datelen)
469 {
470   int dellines = 0;
471   long l;
472   int ch;
473
474   if (b->deleted) {
475     fseeko (fp, b->offset, SEEK_SET);
476     for (l = b->length; l; l--) {
477       ch = getc (fp);
478       if (ch == EOF)
479         break;
480       if (ch == '\n')
481         dellines++;
482     }
483     dellines -= 3;
484     *length -= b->length - (84 + datelen);
485     /* Count the number of digits exceeding the first one to write the size */
486     for (l = 10; b->length >= l; l *= 10)
487       (*length)++;
488   }
489   else {
490     for (b = b->parts; b; b = b->next)
491       dellines += count_delete_lines (fp, b, length, datelen);
492   }
493   return dellines;
494 }
495
496 /* make a copy of a message
497  * 
498  * fpout        where to write output
499  * fpin         where to get input
500  * hdr          header of message being copied
501  * body         structure of message being copied
502  * flags
503  *      M_CM_NOHEADER   don't copy header
504  *      M_CM_PREFIX     quote header and body
505  *      M_CM_DECODE     decode message body to text/plain
506  *      M_CM_DISPLAY    displaying output to the user
507  *      M_CM_PRINTING   printing the message
508  *      M_CM_UPDATE     update structures in memory after syncing
509  *      M_CM_DECODE_PGP used for decoding PGP messages
510  *      M_CM_CHARCONV   perform character set conversion 
511  * chflags      flags to mutt_copy_header()
512  */
513
514 int
515 _mutt_copy_message (FILE * fpout, FILE * fpin, HEADER * hdr, BODY * body,
516                     int flags, int chflags)
517 {
518   char prefix[SHORT_STRING];
519   STATE s;
520   LOFF_T new_offset = -1;
521   int rc = 0;
522
523   if (flags & M_CM_PREFIX) {
524     if (option (OPTTEXTFLOWED))
525       strfcpy (prefix, ">", sizeof (prefix));
526     else
527       _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
528                          hdr, 0);
529   }
530
531   if ((flags & M_CM_NOHEADER) == 0) {
532     if (flags & M_CM_PREFIX)
533       chflags |= CH_PREFIX;
534
535     else if (hdr->attach_del && (chflags & CH_UPDATE_LEN)) {
536       int new_lines;
537       LOFF_T new_length = body->length;
538       char date[SHORT_STRING];
539
540       mutt_make_date (date, sizeof (date));
541       date[5] = date[str_len (date) - 1] = '\"';
542
543       /* Count the number of lines and bytes to be deleted */
544       fseeko (fpin, body->offset, SEEK_SET);
545       new_lines = hdr->lines -
546         count_delete_lines (fpin, body, &new_length, str_len (date));
547
548       /* Copy the headers */
549       if (mutt_copy_header (fpin, hdr, fpout,
550                             chflags | CH_NOLEN | CH_NONEWLINE, NULL))
551         return -1;
552       fprintf (fpout, "Content-Length: " OFF_T_FMT "\n", new_length);
553       if (new_lines <= 0)
554         new_lines = 0;
555       else
556         fprintf (fpout, "Lines: %d\n\n", new_lines);
557       if (ferror (fpout) || feof (fpout))
558         return -1;
559       new_offset = ftello (fpout);
560
561       /* Copy the body */
562       fseeko (fpin, body->offset, SEEK_SET);
563       if (copy_delete_attach (body, fpin, fpout, date))
564         return -1;
565
566 #ifdef DEBUG
567       {
568         LOFF_T fail = ((ftello (fpout) - new_offset) - new_length);
569
570         if (fail) {
571           mutt_error ("The length calculation was wrong by %ld bytes", fail);
572           new_length += fail;
573           mutt_sleep (1);
574         }
575       }
576 #endif
577
578       /* Update original message if we are sync'ing a mailfolder */
579       if (flags & M_CM_UPDATE) {
580         hdr->attach_del = 0;
581         hdr->lines = new_lines;
582         body->offset = new_offset;
583
584         /* update the total size of the mailbox to reflect this deletion */
585         Context->size -= body->length - new_length;
586         /*
587          * if the message is visible, update the visible size of the mailbox
588          * as well.
589          */
590         if (Context->v2r[hdr->msgno] != -1)
591           Context->vsize -= body->length - new_length;
592
593         body->length = new_length;
594         mutt_free_body (&body->parts);
595       }
596
597       return 0;
598     }
599
600     if (mutt_copy_header (fpin, hdr, fpout, chflags,
601                           (chflags & CH_PREFIX) ? prefix : NULL) == -1)
602       return -1;
603
604     new_offset = ftello (fpout);
605   }
606
607   if (flags & M_CM_DECODE) {
608     /* now make a text/plain version of the message */
609     memset (&s, 0, sizeof (STATE));
610     s.fpin = fpin;
611     s.fpout = fpout;
612     if (flags & M_CM_PREFIX)
613       s.prefix = prefix;
614     if (flags & M_CM_DISPLAY)
615       s.flags |= M_DISPLAY;
616     if (flags & M_CM_PRINTING)
617       s.flags |= M_PRINTING;
618     if (flags & M_CM_WEED)
619       s.flags |= M_WEED;
620     if (flags & M_CM_CHARCONV)
621       s.flags |= M_CHARCONV;
622     if (flags & M_CM_REPLYING)
623       s.flags |= M_REPLYING;
624
625     if (WithCrypto && flags & M_CM_VERIFY)
626       s.flags |= M_VERIFY;
627
628     rc = mutt_body_handler (body, &s);
629   }
630   else if (WithCrypto
631            && (flags & M_CM_DECODE_CRYPT) && (hdr->security & ENCRYPT)) {
632     BODY *cur;
633     FILE *fp;
634
635     if ((WithCrypto & APPLICATION_PGP)
636         && (flags & M_CM_DECODE_PGP) && (hdr->security & APPLICATION_PGP) &&
637         hdr->content->type == TYPEMULTIPART) {
638       if (crypt_pgp_decrypt_mime (fpin, &fp, hdr->content, &cur))
639         return (-1);
640       fputs ("MIME-Version: 1.0\n", fpout);
641     }
642
643     if ((WithCrypto & APPLICATION_SMIME)
644         && (flags & M_CM_DECODE_SMIME) && (hdr->security & APPLICATION_SMIME)
645         && hdr->content->type == TYPEAPPLICATION) {
646       if (crypt_smime_decrypt_mime (fpin, &fp, hdr->content, &cur))
647         return (-1);
648     }
649
650     mutt_write_mime_header (cur, fpout);
651     fputc ('\n', fpout);
652
653     fseeko (fp, cur->offset, 0);
654     if (mutt_copy_bytes (fp, fpout, cur->length) == -1) {
655       fclose (fp);
656       mutt_free_body (&cur);
657       return (-1);
658     }
659     mutt_free_body (&cur);
660     fclose (fp);
661   }
662   else {
663     fseeko (fpin, body->offset, 0);
664     if (flags & M_CM_PREFIX) {
665       int c;
666       size_t bytes = body->length;
667
668       fputs (prefix, fpout);
669
670       while ((c = fgetc (fpin)) != EOF && bytes--) {
671         fputc (c, fpout);
672         if (c == '\n') {
673           fputs (prefix, fpout);
674         }
675       }
676     }
677     else if (mutt_copy_bytes (fpin, fpout, body->length) == -1)
678       return -1;
679   }
680
681   if ((flags & M_CM_UPDATE) && (flags & M_CM_NOHEADER) == 0
682       && new_offset != -1) {
683     body->offset = new_offset;
684     mutt_free_body (&body->parts);
685   }
686
687   return rc;
688 }
689
690 int
691 mutt_copy_message (FILE * fpout, CONTEXT * src, HEADER * hdr, int flags,
692                    int chflags)
693 {
694   MESSAGE *msg;
695   int r;
696
697   if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
698     return -1;
699   if ((r =
700        _mutt_copy_message (fpout, msg->fp, hdr, hdr->content, flags,
701                            chflags)) == 0 && (ferror (fpout)
702                                               || feof (fpout))) {
703     debug_print (1, ("_mutt_copy_message failed to detect EOF!\n"));
704     r = -1;
705   }
706   mx_close_message (&msg);
707   return r;
708 }
709
710 /* appends a copy of the given message to a mailbox
711  *
712  * dest         destination mailbox
713  * fpin         where to get input
714  * src          source mailbox
715  * hdr          message being copied
716  * body         structure of message being copied
717  * flags        mutt_copy_message() flags
718  * chflags      mutt_copy_header() flags
719  */
720
721 int
722 _mutt_append_message (CONTEXT * dest, FILE * fpin, CONTEXT * src,
723                       HEADER * hdr, BODY * body, int flags, int chflags) {
724   char buf[STRING];
725   MESSAGE *msg;
726   int r;
727
728   fseeko(fpin, hdr->offset, 0);
729   if (fgets (buf, sizeof (buf), fpin) == NULL)
730     return (-1);
731   if ((msg = mx_open_new_message (dest, hdr, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL)
732     return (-1);
733   if (dest->magic == M_MBOX || dest->magic == M_MMDF)
734     chflags |= CH_FROM | CH_FORCE_FROM;
735   chflags |= (dest->magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
736   r = _mutt_copy_message (msg->fp, fpin, hdr, body, flags, chflags);
737   if (mx_commit_message (msg, dest) != 0)
738     r = -1;
739
740   mx_close_message (&msg);
741   return r;
742 }
743
744 int
745 mutt_append_message (CONTEXT * dest, CONTEXT * src, HEADER * hdr, int cmflags,
746                      int chflags)
747 {
748   MESSAGE *msg;
749   int r;
750
751   if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
752     return -1;
753   r =
754     _mutt_append_message (dest, msg->fp, src, hdr, hdr->content, cmflags,
755                           chflags);
756   mx_close_message (&msg);
757   return r;
758 }
759
760 /*
761  * This function copies a message body, while deleting _in_the_copy_
762  * any attachments which are marked for deletion.
763  * Nothing is changed in the original message -- this is left to the caller.
764  *
765  * The function will return 0 on success and -1 on failure.
766  */
767 static int copy_delete_attach (BODY * b, FILE * fpin, FILE * fpout,
768                                char *date)
769 {
770   BODY *part;
771
772   for (part = b->parts; part; part = part->next) {
773     if (part->deleted || part->parts) {
774       /* Copy till start of this part */
775       if (mutt_copy_bytes (fpin, fpout, part->hdr_offset - ftello (fpin)))
776         return -1;
777
778       if (part->deleted) {
779         fprintf (fpout,
780                  "Content-Type: message/external-body; access-type=x-mutt-deleted;\n"
781                  "\texpiration=%s; length=" OFF_T_FMT "\n"
782                  "\n", date + 5, part->length);
783         if (ferror (fpout))
784           return -1;
785
786         /* Copy the original mime headers */
787         if (mutt_copy_bytes (fpin, fpout, part->offset - ftello (fpin)))
788           return -1;
789
790         /* Skip the deleted body */
791         fseeko (fpin, part->offset + part->length, SEEK_SET);
792       }
793       else {
794         if (copy_delete_attach (part, fpin, fpout, date))
795           return -1;
796       }
797     }
798   }
799
800   /* Copy the last parts */
801   if (mutt_copy_bytes (fpin, fpout, b->offset + b->length - ftello (fpin)))
802     return -1;
803
804   return 0;
805 }
806
807 /* 
808  * This function is the equivalent of mutt_write_address_list(),
809  * but writes to a buffer instead of writing to a stream.
810  * mutt_write_address_list could be re-used if we wouldn't store
811  * all the decoded headers in a huge array, first. 
812  *
813  * XXX - fix that. 
814  */
815
816 static void format_address_header (char **h, ADDRESS * a)
817 {
818   char buf[HUGE_STRING];
819   char cbuf[STRING];
820   char c2buf[STRING];
821
822   int l, linelen, buflen, count;
823
824   linelen = str_len (*h);
825   buflen = linelen + 3;
826
827
828   mem_realloc (h, buflen);
829   for (count = 0; a; a = a->next, count++) {
830     ADDRESS *tmp = a->next;
831
832     a->next = NULL;
833     *buf = *cbuf = *c2buf = '\0';
834     rfc822_write_address (buf, sizeof (buf), a, 0);
835     a->next = tmp;
836
837     l = str_len (buf);
838     if (count && linelen + l > 74) {
839       strcpy (cbuf, "\n\t");    /* __STRCPY_CHECKED__ */
840       linelen = l + 8;
841     }
842     else {
843       if (a->mailbox) {
844         strcpy (cbuf, " ");     /* __STRCPY_CHECKED__ */
845         linelen++;
846       }
847       linelen += l;
848     }
849     if (!a->group && a->next && a->next->mailbox) {
850       linelen++;
851       buflen++;
852       strcpy (c2buf, ",");      /* __STRCPY_CHECKED__ */
853     }
854
855     buflen += l + str_len (cbuf) + str_len (c2buf);
856     mem_realloc (h, buflen);
857     strcat (*h, cbuf);          /* __STRCAT_CHECKED__ */
858     strcat (*h, buf);           /* __STRCAT_CHECKED__ */
859     strcat (*h, c2buf);         /* __STRCAT_CHECKED__ */
860   }
861
862   /* Space for this was allocated in the beginning of this function. */
863   strcat (*h, "\n");            /* __STRCAT_CHECKED__ */
864 }
865
866 static int address_header_decode (char **h)
867 {
868   char *s = *h;
869   int l;
870
871   ADDRESS *a = NULL;
872
873   switch (tolower ((unsigned char) *s)) {
874   case 'r':
875     {
876       if (ascii_strncasecmp (s, "return-path:", 12) == 0) {
877         l = 12;
878         break;
879       }
880       else if (ascii_strncasecmp (s, "reply-to:", 9) == 0) {
881         l = 9;
882         break;
883       }
884       return 0;
885     }
886   case 'f':
887     {
888       if (ascii_strncasecmp (s, "from:", 5))
889         return 0;
890       l = 5;
891       break;
892     }
893   case 'c':
894     {
895       if (ascii_strncasecmp (s, "cc:", 3))
896         return 0;
897       l = 3;
898       break;
899
900     }
901   case 'b':
902     {
903       if (ascii_strncasecmp (s, "bcc:", 4))
904         return 0;
905       l = 4;
906       break;
907     }
908   case 's':
909     {
910       if (ascii_strncasecmp (s, "sender:", 7))
911         return 0;
912       l = 7;
913       break;
914     }
915   case 't':
916     {
917       if (ascii_strncasecmp (s, "to:", 3))
918         return 0;
919       l = 3;
920       break;
921     }
922   case 'm':
923     {
924       if (ascii_strncasecmp (s, "mail-followup-to:", 17))
925         return 0;
926       l = 17;
927       break;
928     }
929   default:
930     return 0;
931   }
932
933   if ((a = rfc822_parse_adrlist (a, s + l)) == NULL)
934     return 0;
935
936   mutt_addrlist_to_local (a);
937   rfc2047_decode_adrlist (a);
938
939   *h = mem_calloc (1, l + 2);
940
941   strfcpy (*h, s, l + 1);
942
943   format_address_header (h, a);
944
945   rfc822_free_address (&a);
946
947   mem_free (&s);
948   return 1;
949 }