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