2 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
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.
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.
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.
29 #include "mutt_crypt.h"
30 #include "mutt_idna.h"
35 #include <unistd.h> /* needed for SEEK_SET under SunOS 4.1.4 */
37 static int address_header_decode (char **str);
38 static int copy_delete_attach (BODY *b, FILE *fpin, FILE *fpout, char *date);
40 /* Ok, the only reason for not merging this with mutt_copy_header()
41 * below is to avoid creating a HEADER structure in message_handler().
44 mutt_copy_hdr (FILE *in, FILE *out, long off_start, long off_end, int flags,
50 char buf[STRING]; /* should be long enough to get most fields in one pass */
56 char *this_one = NULL;
59 if (ftell (in) != off_start)
60 fseek (in, off_start, 0);
65 if ((flags & (CH_REORDER | CH_WEED | CH_MIME | CH_DECODE | CH_PREFIX | CH_WEED_DELIVERED)) == 0)
67 /* Without these flags to complicate things
68 * we can do a more efficient line to line copying
70 while (ftell (in) < off_end)
72 nl = strchr (buf, '\n');
74 if ((fgets (buf, sizeof (buf), in)) == NULL)
77 /* Is it the begining of a header? */
78 if (nl && buf[0] != ' ' && buf[0] != '\t')
81 if (!from && mutt_strncmp ("From ", buf, 5) == 0)
83 if ((flags & CH_FROM) == 0)
87 else if (flags & (CH_NOQFROM) &&
88 ascii_strncasecmp (">From ", buf, 6) == 0)
91 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
92 break; /* end of header */
94 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
95 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
96 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
98 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
99 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
100 ascii_strncasecmp ("Lines:", buf, 6) == 0))
102 if ((flags & CH_UPDATE_REFS) &&
103 ascii_strncasecmp ("References:", buf, 11) == 0)
105 if ((flags & CH_UPDATE_IRT) &&
106 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
111 if (!ignore && fputs (buf, out) == EOF)
121 /* We are going to read and collect the headers in an array
122 * so we are able to do re-ordering.
123 * First count the number of entries in the array
125 if (flags & CH_REORDER)
127 for (t = HeaderOrderList; t; t = t->next)
129 dprint(1, (debugfile, "Reorder list: %s\n", t->data));
134 dprint (1, (debugfile, "WEED is %s\n", (flags & CH_WEED) ? "Set" : "Not"));
136 headers = safe_calloc (hdr_count, sizeof (char *));
138 /* Read all the headers into the array */
139 while (ftell (in) < off_end)
141 nl = strchr (buf, '\n');
144 if ((fgets (buf, sizeof (buf), in)) == NULL)
147 /* Is it the begining of a header? */
148 if (nl && buf[0] != ' ' && buf[0] != '\t')
150 /* Do we have anything pending? */
153 if (flags & CH_DECODE)
155 if (!address_header_decode (&this_one))
156 rfc2047_decode (&this_one);
160 headers[x] = this_one;
163 safe_realloc (&headers[x], mutt_strlen (headers[x]) +
164 mutt_strlen (this_one) + sizeof (char));
165 strcat (headers[x], this_one); /* __STRCAT_CHECKED__ */
174 if (!from && mutt_strncmp ("From ", buf, 5) == 0)
176 if ((flags & CH_FROM) == 0)
178 this_is_from = from = 1;
180 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
181 break; /* end of header */
183 /* note: CH_FROM takes precedence over header weeding. */
184 if (!((flags & CH_FROM) && (flags & CH_FORCE_FROM) && this_is_from) &&
186 mutt_matches_ignore (buf, Ignore) &&
187 !mutt_matches_ignore (buf, UnIgnore))
189 if ((flags & CH_WEED_DELIVERED) &&
190 ascii_strncasecmp ("Delivered-To:", buf, 13) == 0)
192 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
193 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
194 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
196 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
197 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
198 ascii_strncasecmp ("Lines:", buf, 6) == 0))
200 if ((flags & CH_MIME) &&
201 ((ascii_strncasecmp ("content-", buf, 8) == 0 &&
202 (ascii_strncasecmp ("transfer-encoding:", buf + 8, 18) == 0 ||
203 ascii_strncasecmp ("type:", buf + 8, 5) == 0)) ||
204 ascii_strncasecmp ("mime-version:", buf, 13) == 0))
206 if ((flags & CH_UPDATE_REFS) &&
207 ascii_strncasecmp ("References:", buf, 11) == 0)
209 if ((flags & CH_UPDATE_IRT) &&
210 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
213 /* Find x -- the array entry where this header is to be saved */
214 if (flags & CH_REORDER)
216 for (t = HeaderOrderList, x = 0 ; (t) ; t = t->next, x++)
218 if (!ascii_strncasecmp (buf, t->data, mutt_strlen (t->data)))
220 dprint(2, (debugfile, "Reorder: %s matches %s\n", t->data, buf));
227 } /* If beginning of header */
231 dprint (2, (debugfile, "Reorder: x = %d; hdr_count = %d\n", x, hdr_count));
233 this_one = safe_strdup (buf);
236 safe_realloc (&this_one,
237 mutt_strlen (this_one) + mutt_strlen (buf) + sizeof (char));
238 strcat (this_one, buf); /* __STRCAT_CHECKED__ */
241 } /* while (ftell (in) < off_end) */
243 /* Do we have anything pending? -- XXX, same code as in above in the loop. */
246 if (flags & CH_DECODE)
248 if (!address_header_decode (&this_one))
249 rfc2047_decode (&this_one);
253 headers[x] = this_one;
256 safe_realloc (&headers[x], mutt_strlen (headers[x]) +
257 mutt_strlen (this_one) + sizeof (char));
258 strcat (headers[x], this_one); /* __STRCAT_CHECKED__ */
265 /* Now output the headers in order */
266 for (x = 0; x < hdr_count; x++)
271 if (flags & CH_DECODE)
272 rfc2047_decode (&headers[x]);
275 /* We couldn't do the prefixing when reading because RFC 2047
276 * decoding may have concatenated lines.
278 if (flags & CH_PREFIX)
280 char *ch = headers[x];
281 int print_prefix = 1;
287 if (fputs (prefix, out) == EOF)
295 if (*ch == '\n' && ch[1])
298 if (putc (*ch++, out) == EOF)
309 if (fputs (headers[x], out) == EOF)
318 /* Free in a separate loop to be sure that all headers are freed
319 * in case of error. */
320 for (x = 0; x < hdr_count; x++)
330 CH_DECODE RFC2047 header decoding
331 CH_FROM retain the "From " message separator
332 CH_FORCE_FROM give CH_FROM precedence over CH_WEED
333 CH_MIME ignore MIME fields
334 CH_NOLEN don't write Content-Length: and Lines:
335 CH_NONEWLINE don't output a newline after the header
336 CH_NOSTATUS ignore the Status: and X-Status:
337 CH_PREFIX quote header with $indent_str
338 CH_REORDER output header in order specified by `hdr_order'
339 CH_TXTPLAIN generate text/plain MIME headers [hack alert.]
340 CH_UPDATE write new Status: and X-Status:
341 CH_UPDATE_LEN write new Content-Length: and Lines:
342 CH_XMIT ignore Lines: and Content-Length:
343 CH_WEED do header weeding
344 CH_NOQFROM ignore ">From " line
345 CH_UPDATE_IRT update the In-Reply-To: header
346 CH_UPDATE_REFS update the References: header
349 string to use if CH_PREFIX is set
353 mutt_copy_header (FILE *in, HEADER *h, FILE *out, int flags, const char *prefix)
355 char buffer[SHORT_STRING];
357 flags |= (h->irt_changed ? CH_UPDATE_IRT : 0)
358 | (h->refs_changed ? CH_UPDATE_REFS : 0);
360 if (mutt_copy_hdr (in, out, h->offset, h->content->offset, flags, prefix) == -1)
363 if (flags & CH_TXTPLAIN)
365 char chsbuf[SHORT_STRING];
366 fputs ("Mime-Version: 1.0\n", out);
367 fputs ("Content-Transfer-Encoding: 8bit\n", out);
368 fputs ("Content-Type: text/plain; charset=", out);
369 mutt_canonical_charset (chsbuf, sizeof (chsbuf), Charset ? Charset : "us-ascii");
370 rfc822_cat(buffer, sizeof(buffer), chsbuf, MimeSpecials);
374 if (ferror (out) != 0 || feof (out) != 0)
379 if (flags & CH_UPDATE)
381 if ((flags & CH_NOSTATUS) == 0)
383 if (h->irt_changed && h->new_env->in_reply_to)
385 LIST *listp = h->new_env->in_reply_to;
387 if (fputs ("In-Reply-To: ", out) == EOF)
390 for (; listp; listp = listp->next)
391 if ((fputs (listp->data, out) == EOF) || (fputc (' ', out) == EOF))
394 if (fputc ('\n', out) == EOF)
398 if (h->refs_changed && h->new_env->references)
400 LIST *listp = h->new_env->references, *refs = NULL, *t;
402 if (fputs ("References: ", out) == EOF)
405 /* Mutt stores references in reverse order, thus we create
406 * a reordered refs list that we can put in the headers */
407 for (; listp; listp = listp->next, refs = t)
409 t = (LIST *)safe_malloc (sizeof (LIST));
410 t->data = listp->data;
414 for (; refs; refs = refs->next)
415 if ((fputs (refs->data, out) == EOF) || (fputc (' ', out) == EOF))
418 /* clearing refs from memory */
419 for (t = refs; refs; refs = t->next, t = refs)
420 safe_free ((void **)&refs);
422 if (fputc ('\n', out) == EOF)
426 if (h->old || h->read)
428 if (fputs ("Status: ", out) == EOF)
433 if (fputs ("RO", out) == EOF)
438 if (fputc ('O', out) == EOF)
442 if (fputc ('\n', out) == EOF)
446 if (h->flagged || h->replied)
448 if (fputs ("X-Status: ", out) == EOF)
453 if (fputc ('A', out) == EOF)
459 if (fputc ('F', out) == EOF)
463 if (fputc ('\n', out) == EOF)
469 if (flags & CH_UPDATE_LEN &&
470 (flags & CH_NOLEN) == 0)
472 fprintf (out, "Content-Length: %ld\n", h->content->length);
473 if (h->lines != 0 || h->content->length == 0)
474 fprintf (out, "Lines: %d\n", h->lines);
477 if ((flags & CH_NONEWLINE) == 0)
479 if (flags & CH_PREFIX)
481 if (fputc ('\n', out) == EOF) /* add header terminator */
485 if (ferror (out) || feof (out))
491 /* Count the number of lines and bytes to be deleted in this body*/
492 static int count_delete_lines (FILE *fp, BODY *b, long *length, size_t datelen)
500 fseek (fp, b->offset, SEEK_SET);
501 for (l = b->length ; l ; l --)
510 *length -= b->length - (84 + datelen);
511 /* Count the number of digits exceeding the first one to write the size */
512 for (l = 10 ; b->length >= l ; l *= 10)
517 for (b = b->parts ; b ; b = b->next)
518 dellines += count_delete_lines (fp, b, length, datelen);
523 /* make a copy of a message
525 * fpout where to write output
526 * fpin where to get input
527 * hdr header of message being copied
528 * body structure of message being copied
530 * M_CM_NOHEADER don't copy header
531 * M_CM_PREFIX quote header and body
532 * M_CM_DECODE decode message body to text/plain
533 * M_CM_DISPLAY displaying output to the user
534 * M_CM_PRINTING printing the message
535 * M_CM_UPDATE update structures in memory after syncing
536 * M_CM_DECODE_PGP used for decoding PGP messages
537 * M_CM_CHARCONV perform character set conversion
538 * chflags flags to mutt_copy_header()
542 _mutt_copy_message (FILE *fpout, FILE *fpin, HEADER *hdr, BODY *body,
543 int flags, int chflags)
545 char prefix[SHORT_STRING];
547 long new_offset = -1;
549 if (flags & M_CM_PREFIX)
551 if (option (OPTTEXTFLOWED))
552 strfcpy (prefix, ">", sizeof (prefix));
554 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context, hdr, 0);
557 if ((flags & M_CM_NOHEADER) == 0)
559 if (flags & M_CM_PREFIX)
560 chflags |= CH_PREFIX;
562 else if (hdr->attach_del && (chflags & CH_UPDATE_LEN))
565 long new_length = body->length;
566 char date[SHORT_STRING];
568 mutt_make_date (date, sizeof (date));
569 date[5] = date[mutt_strlen (date) - 1] = '\"';
571 /* Count the number of lines and bytes to be deleted */
572 fseek (fpin, body->offset, SEEK_SET);
573 new_lines = hdr->lines -
574 count_delete_lines (fpin, body, &new_length, mutt_strlen (date));
576 /* Copy the headers */
577 if (mutt_copy_header (fpin, hdr, fpout,
578 chflags | CH_NOLEN | CH_NONEWLINE, NULL))
580 fprintf (fpout, "Content-Length: %ld\n", new_length);
584 fprintf (fpout, "Lines: %d\n\n", new_lines);
585 if (ferror (fpout) || feof (fpout))
587 new_offset = ftell (fpout);
590 fseek (fpin, body->offset, SEEK_SET);
591 if (copy_delete_attach (body, fpin, fpout, date))
596 long fail = ((ftell (fpout) - new_offset) - new_length);
600 mutt_error ("The length calculation was wrong by %ld bytes", fail);
607 /* Update original message if we are sync'ing a mailfolder */
608 if (flags & M_CM_UPDATE)
611 hdr->lines = new_lines;
612 body->offset = new_offset;
614 /* update the total size of the mailbox to reflect this deletion */
615 Context->size -= body->length - new_length;
617 * if the message is visible, update the visible size of the mailbox
620 if (Context->v2r[hdr->msgno] != -1)
621 Context->vsize -= body->length - new_length;
623 body->length = new_length;
624 mutt_free_body (&body->parts);
630 if (mutt_copy_header (fpin, hdr, fpout, chflags,
631 (chflags & CH_PREFIX) ? prefix : NULL) == -1)
634 new_offset = ftell (fpout);
637 if (flags & M_CM_DECODE)
639 /* now make a text/plain version of the message */
640 memset (&s, 0, sizeof (STATE));
643 if (flags & M_CM_PREFIX)
645 if (flags & M_CM_DISPLAY)
646 s.flags |= M_DISPLAY;
647 if (flags & M_CM_PRINTING)
648 s.flags |= M_PRINTING;
649 if (flags & M_CM_WEED)
651 if (flags & M_CM_CHARCONV)
652 s.flags |= M_CHARCONV;
653 if (flags & M_CM_REPLYING)
654 s.flags |= M_REPLYING;
656 if (WithCrypto && flags & M_CM_VERIFY)
659 mutt_body_handler (body, &s);
662 && (flags & M_CM_DECODE_CRYPT) && (hdr->security & ENCRYPT))
667 if ((WithCrypto & APPLICATION_PGP)
668 && (flags & M_CM_DECODE_PGP) && (hdr->security & APPLICATION_PGP) &&
669 hdr->content->type == TYPEMULTIPART)
671 if (crypt_pgp_decrypt_mime (fpin, &fp, hdr->content, &cur))
673 fputs ("Mime-Version: 1.0\n", fpout);
676 if ((WithCrypto & APPLICATION_SMIME)
677 && (flags & M_CM_DECODE_SMIME) && (hdr->security & APPLICATION_SMIME)
678 && hdr->content->type == TYPEAPPLICATION)
680 if (crypt_smime_decrypt_mime (fpin, &fp, hdr->content, &cur))
684 mutt_write_mime_header (cur, fpout);
687 fseek (fp, cur->offset, 0);
688 if (mutt_copy_bytes (fp, fpout, cur->length) == -1)
691 mutt_free_body (&cur);
694 mutt_free_body (&cur);
699 fseek (fpin, body->offset, 0);
700 if (flags & M_CM_PREFIX)
703 size_t bytes = body->length;
705 fputs(prefix, fpout);
707 while((c = fgetc(fpin)) != EOF && bytes--)
712 fputs(prefix, fpout);
716 else if (mutt_copy_bytes (fpin, fpout, body->length) == -1)
720 if ((flags & M_CM_UPDATE) && (flags & M_CM_NOHEADER) == 0
723 body->offset = new_offset;
724 mutt_free_body (&body->parts);
731 mutt_copy_message (FILE *fpout, CONTEXT *src, HEADER *hdr, int flags,
737 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
739 if ((r = _mutt_copy_message (fpout, msg->fp, hdr, hdr->content, flags, chflags)) == 0
740 && (ferror (fpout) || feof (fpout)))
742 dprint (1, (debugfile, "_mutt_copy_message failed to detect EOF!\n"));
745 mx_close_message (&msg);
749 /* appends a copy of the given message to a mailbox
751 * dest destination mailbox
752 * fpin where to get input
754 * hdr message being copied
755 * body structure of message being copied
756 * flags mutt_copy_message() flags
757 * chflags mutt_copy_header() flags
761 _mutt_append_message (CONTEXT *dest, FILE *fpin, CONTEXT *src, HEADER *hdr,
762 BODY *body, int flags, int chflags)
767 if ((msg = mx_open_new_message (dest, hdr, (src->magic == M_MBOX || src->magic == M_MMDF) ? 0 : M_ADD_FROM)) == NULL)
769 if (dest->magic == M_MBOX || dest->magic == M_MMDF)
770 chflags |= CH_FROM | CH_FORCE_FROM;
771 chflags |= (dest->magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
772 r = _mutt_copy_message (msg->fp, fpin, hdr, body, flags, chflags);
773 if (mx_commit_message (msg, dest) != 0)
776 mx_close_message (&msg);
781 mutt_append_message (CONTEXT *dest, CONTEXT *src, HEADER *hdr, int cmflags,
787 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
789 r = _mutt_append_message (dest, msg->fp, src, hdr, hdr->content, cmflags, chflags);
790 mx_close_message (&msg);
795 * This function copies a message body, while deleting _in_the_copy_
796 * any attachments which are marked for deletion.
797 * Nothing is changed in the original message -- this is left to the caller.
799 * The function will return 0 on success and -1 on failure.
801 static int copy_delete_attach (BODY *b, FILE *fpin, FILE *fpout, char *date)
805 for (part = b->parts ; part ; part = part->next)
807 if (part->deleted || part->parts)
809 /* Copy till start of this part */
810 if (mutt_copy_bytes (fpin, fpout, part->hdr_offset - ftell (fpin)))
816 "Content-Type: message/external-body; access-type=x-mutt-deleted;\n"
817 "\texpiration=%s; length=%ld\n"
818 "\n", date + 5, part->length);
822 /* Copy the original mime headers */
823 if (mutt_copy_bytes (fpin, fpout, part->offset - ftell (fpin)))
826 /* Skip the deleted body */
827 fseek (fpin, part->offset + part->length, SEEK_SET);
831 if (copy_delete_attach (part, fpin, fpout, date))
837 /* Copy the last parts */
838 if (mutt_copy_bytes (fpin, fpout, b->offset + b->length - ftell (fpin)))
845 * This function is the equivalent of mutt_write_address_list(),
846 * but writes to a buffer instead of writing to a stream.
847 * mutt_write_address_list could be re-used if we wouldn't store
848 * all the decoded headers in a huge array, first.
853 static void format_address_header (char **h, ADDRESS *a)
855 char buf[HUGE_STRING];
859 int l, linelen, buflen, count;
860 linelen = mutt_strlen (*h);
861 buflen = linelen + 3;
864 safe_realloc (h, buflen);
865 for (count = 0; a; a = a->next, count++)
867 ADDRESS *tmp = a->next;
869 *buf = *cbuf = *c2buf = '\0';
870 rfc822_write_address (buf, sizeof (buf), a, 0);
873 l = mutt_strlen (buf);
874 if (count && linelen + l > 74)
876 strcpy (cbuf, "\n\t"); /* __STRCPY_CHECKED__ */
883 strcpy (cbuf, " "); /* __STRCPY_CHECKED__ */
888 if (!a->group && a->next && a->next->mailbox)
892 strcpy (c2buf, ","); /* __STRCPY_CHECKED__ */
895 buflen += l + mutt_strlen (cbuf) + mutt_strlen (c2buf);
896 safe_realloc (h, buflen);
897 strcat (*h, cbuf); /* __STRCAT_CHECKED__ */
898 strcat (*h, buf); /* __STRCAT_CHECKED__ */
899 strcat (*h, c2buf); /* __STRCAT_CHECKED__ */
902 /* Space for this was allocated in the beginning of this function. */
903 strcat (*h, "\n"); /* __STRCAT_CHECKED__ */
906 static int address_header_decode (char **h)
913 switch (tolower ((unsigned char) *s))
917 if (ascii_strncasecmp (s, "return-path:", 12) == 0)
922 else if (ascii_strncasecmp (s, "reply-to:", 9) == 0)
931 if (ascii_strncasecmp (s, "from:", 5))
938 if (ascii_strncasecmp (s, "cc:", 3))
946 if (ascii_strncasecmp (s, "bcc:", 4))
953 if (ascii_strncasecmp (s, "sender:", 7))
960 if (ascii_strncasecmp (s, "to:", 3))
967 if (ascii_strncasecmp (s, "mail-followup-to:", 17))
975 if ((a = rfc822_parse_adrlist (a, s + l)) == NULL)
978 mutt_addrlist_to_local (a);
979 rfc2047_decode_adrlist (a);
981 *h = safe_calloc (1, l + 2);
983 strfcpy (*h, s, l + 1);
985 format_address_header (h, a);
987 rfc822_free_address (&a);