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.
25 #include "mutt_crypt.h"
26 #include "mutt_idna.h"
31 #include <unistd.h> /* needed for SEEK_SET under SunOS 4.1.4 */
33 static int address_header_decode (char **str);
34 static int copy_delete_attach (BODY *b, FILE *fpin, FILE *fpout, char *date);
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().
40 mutt_copy_hdr (FILE *in, FILE *out, long off_start, long off_end, int flags,
46 char buf[STRING]; /* should be long enough to get most fields in one pass */
52 char *this_one = NULL;
55 if (ftell (in) != off_start)
56 fseek (in, off_start, 0);
61 if ((flags & (CH_REORDER | CH_WEED | CH_MIME | CH_DECODE | CH_PREFIX | CH_WEED_DELIVERED)) == 0)
63 /* Without these flags to complicate things
64 * we can do a more efficient line to line copying
66 while (ftell (in) < off_end)
68 nl = strchr (buf, '\n');
70 if ((fgets (buf, sizeof (buf), in)) == NULL)
73 /* Is it the begining of a header? */
74 if (nl && buf[0] != ' ' && buf[0] != '\t')
77 if (!from && mutt_strncmp ("From ", buf, 5) == 0)
79 if ((flags & CH_FROM) == 0)
83 else if (flags & (CH_NOQFROM) &&
84 ascii_strncasecmp (">From ", buf, 6) == 0)
87 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
88 break; /* end of header */
90 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
91 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
92 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
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))
101 if (!ignore && fputs (buf, out) == EOF)
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
115 if (flags & CH_REORDER)
117 for (t = HeaderOrderList; t; t = t->next)
119 dprint(1, (debugfile, "Reorder list: %s\n", t->data));
124 dprint (1, (debugfile, "WEED is %s\n", (flags & CH_WEED) ? "Set" : "Not"));
126 headers = safe_calloc (hdr_count, sizeof (char *));
128 /* Read all the headers into the array */
129 while (ftell (in) < off_end)
131 nl = strchr (buf, '\n');
134 if ((fgets (buf, sizeof (buf), in)) == NULL)
137 /* Is it the begining of a header? */
138 if (nl && buf[0] != ' ' && buf[0] != '\t')
140 /* Do we have anything pending? */
143 if (flags & CH_DECODE)
145 if (!address_header_decode (&this_one))
146 rfc2047_decode (&this_one);
150 headers[x] = this_one;
153 safe_realloc (&headers[x], mutt_strlen (headers[x]) +
154 mutt_strlen (this_one) + sizeof (char));
155 strcat (headers[x], this_one); /* __STRCAT_CHECKED__ */
164 if (!from && mutt_strncmp ("From ", buf, 5) == 0)
166 if ((flags & CH_FROM) == 0)
168 this_is_from = from = 1;
170 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
171 break; /* end of header */
173 /* note: CH_FROM takes precedence over header weeding. */
174 if (!((flags & CH_FROM) && (flags & CH_FORCE_FROM) && this_is_from) &&
176 mutt_matches_ignore (buf, Ignore) &&
177 !mutt_matches_ignore (buf, UnIgnore))
179 if ((flags & CH_WEED_DELIVERED) &&
180 ascii_strncasecmp ("Delivered-To:", buf, 13) == 0)
182 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
183 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
184 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
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))
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))
197 /* Find x -- the array entry where this header is to be saved */
198 if (flags & CH_REORDER)
200 for (t = HeaderOrderList, x = 0 ; (t) ; t = t->next, x++)
202 if (!ascii_strncasecmp (buf, t->data, mutt_strlen (t->data)))
204 dprint(2, (debugfile, "Reorder: %s matches %s\n", t->data, buf));
211 } /* If beginning of header */
215 dprint (2, (debugfile, "Reorder: x = %d; hdr_count = %d\n", x, hdr_count));
217 this_one = safe_strdup (buf);
220 safe_realloc (&this_one,
221 mutt_strlen (this_one) + mutt_strlen (buf) + sizeof (char));
222 strcat (this_one, buf); /* __STRCAT_CHECKED__ */
225 } /* while (ftell (in) < off_end) */
227 /* Do we have anything pending? -- XXX, same code as in above in the loop. */
230 if (flags & CH_DECODE)
232 if (!address_header_decode (&this_one))
233 rfc2047_decode (&this_one);
237 headers[x] = this_one;
240 safe_realloc (&headers[x], mutt_strlen (headers[x]) +
241 mutt_strlen (this_one) + sizeof (char));
242 strcat (headers[x], this_one); /* __STRCAT_CHECKED__ */
249 /* Now output the headers in order */
250 for (x = 0; x < hdr_count; x++)
255 if (flags & CH_DECODE)
256 rfc2047_decode (&headers[x]);
259 /* We couldn't do the prefixing when reading because RFC 2047
260 * decoding may have concatenated lines.
262 if (flags & CH_PREFIX)
264 char *ch = headers[x];
265 int print_prefix = 1;
271 if (fputs (prefix, out) == EOF)
279 if (*ch == '\n' && ch[1])
282 if (putc (*ch++, out) == EOF)
293 if (fputs (headers[x], out) == EOF)
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++)
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
331 string to use if CH_PREFIX is set
335 mutt_copy_header (FILE *in, HEADER *h, FILE *out, int flags, const char *prefix)
337 char buffer[SHORT_STRING];
339 if (mutt_copy_hdr (in, out, h->offset, h->content->offset, flags, prefix) == -1)
342 if (flags & CH_TXTPLAIN)
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);
353 if (ferror (out) != 0 || feof (out) != 0)
358 if (flags & CH_UPDATE)
360 if ((flags & CH_NOSTATUS) == 0)
362 if (h->old || h->read)
364 if (fputs ("Status: ", out) == EOF)
369 if (fputs ("RO", out) == EOF)
374 if (fputc ('O', out) == EOF)
378 if (fputc ('\n', out) == EOF)
382 if (h->flagged || h->replied)
384 if (fputs ("X-Status: ", out) == EOF)
389 if (fputc ('A', out) == EOF)
395 if (fputc ('F', out) == EOF)
399 if (fputc ('\n', out) == EOF)
405 if (flags & CH_UPDATE_LEN &&
406 (flags & CH_NOLEN) == 0)
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);
413 if ((flags & CH_NONEWLINE) == 0)
415 if (flags & CH_PREFIX)
417 if (fputc ('\n', out) == EOF) /* add header terminator */
421 if (ferror (out) || feof (out))
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)
436 fseek (fp, b->offset, SEEK_SET);
437 for (l = b->length ; l ; l --)
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)
453 for (b = b->parts ; b ; b = b->next)
454 dellines += count_delete_lines (fp, b, length, datelen);
459 /* make a copy of a message
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
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()
478 _mutt_copy_message (FILE *fpout, FILE *fpin, HEADER *hdr, BODY *body,
479 int flags, int chflags)
481 char prefix[SHORT_STRING];
483 long new_offset = -1;
485 if (flags & M_CM_PREFIX)
487 if (option (OPTTEXTFLOWED))
488 strfcpy (prefix, ">", sizeof (prefix));
490 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context, hdr, 0);
493 if ((flags & M_CM_NOHEADER) == 0)
495 if (flags & M_CM_PREFIX)
496 chflags |= CH_PREFIX;
498 else if (hdr->attach_del && (chflags & CH_UPDATE_LEN))
501 long new_length = body->length;
502 char date[SHORT_STRING];
504 mutt_make_date (date, sizeof (date));
505 date[5] = date[mutt_strlen (date) - 1] = '\"';
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));
512 /* Copy the headers */
513 if (mutt_copy_header (fpin, hdr, fpout,
514 chflags | CH_NOLEN | CH_NONEWLINE, NULL))
516 fprintf (fpout, "Content-Length: %ld\n", new_length);
520 fprintf (fpout, "Lines: %d\n\n", new_lines);
521 if (ferror (fpout) || feof (fpout))
523 new_offset = ftell (fpout);
526 fseek (fpin, body->offset, SEEK_SET);
527 if (copy_delete_attach (body, fpin, fpout, date))
532 long fail = ((ftell (fpout) - new_offset) - new_length);
536 mutt_error ("The length calculation was wrong by %ld bytes", fail);
543 /* Update original message if we are sync'ing a mailfolder */
544 if (flags & M_CM_UPDATE)
547 hdr->lines = new_lines;
548 body->offset = new_offset;
550 /* update the total size of the mailbox to reflect this deletion */
551 Context->size -= body->length - new_length;
553 * if the message is visible, update the visible size of the mailbox
556 if (Context->v2r[hdr->msgno] != -1)
557 Context->vsize -= body->length - new_length;
559 body->length = new_length;
560 mutt_free_body (&body->parts);
566 if (mutt_copy_header (fpin, hdr, fpout, chflags,
567 (chflags & CH_PREFIX) ? prefix : NULL) == -1)
570 new_offset = ftell (fpout);
573 if (flags & M_CM_DECODE)
575 /* now make a text/plain version of the message */
576 memset (&s, 0, sizeof (STATE));
579 if (flags & M_CM_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)
587 if (flags & M_CM_CHARCONV)
588 s.flags |= M_CHARCONV;
590 if (WithCrypto && flags & M_CM_VERIFY)
593 mutt_body_handler (body, &s);
596 && (flags & M_CM_DECODE_CRYPT) && (hdr->security & ENCRYPT))
601 if ((WithCrypto & APPLICATION_PGP)
602 && (flags & M_CM_DECODE_PGP) && (hdr->security & APPLICATION_PGP) &&
603 hdr->content->type == TYPEMULTIPART)
605 if (crypt_pgp_decrypt_mime (fpin, &fp, hdr->content, &cur))
607 fputs ("Mime-Version: 1.0\n", fpout);
610 if ((WithCrypto & APPLICATION_SMIME)
611 && (flags & M_CM_DECODE_SMIME) && (hdr->security & APPLICATION_SMIME)
612 && hdr->content->type == TYPEAPPLICATION)
614 if (crypt_smime_decrypt_mime (fpin, &fp, hdr->content, &cur))
618 mutt_write_mime_header (cur, fpout);
621 fseek (fp, cur->offset, 0);
622 if (mutt_copy_bytes (fp, fpout, cur->length) == -1)
625 mutt_free_body (&cur);
628 mutt_free_body (&cur);
633 fseek (fpin, body->offset, 0);
634 if (flags & M_CM_PREFIX)
637 size_t bytes = body->length;
639 fputs(prefix, fpout);
641 while((c = fgetc(fpin)) != EOF && bytes--)
646 fputs(prefix, fpout);
650 else if (mutt_copy_bytes (fpin, fpout, body->length) == -1)
654 if ((flags & M_CM_UPDATE) && (flags & M_CM_NOHEADER) == 0
657 body->offset = new_offset;
658 mutt_free_body (&body->parts);
665 mutt_copy_message (FILE *fpout, CONTEXT *src, HEADER *hdr, int flags,
671 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
673 if ((r = _mutt_copy_message (fpout, msg->fp, hdr, hdr->content, flags, chflags)) == 0
674 && (ferror (fpout) || feof (fpout)))
676 dprint (1, (debugfile, "_mutt_copy_message failed to detect EOF!\n"));
679 mx_close_message (&msg);
683 /* appends a copy of the given message to a mailbox
685 * dest destination mailbox
686 * fpin where to get input
688 * hdr message being copied
689 * body structure of message being copied
690 * flags mutt_copy_message() flags
691 * chflags mutt_copy_header() flags
695 _mutt_append_message (CONTEXT *dest, FILE *fpin, CONTEXT *src, HEADER *hdr,
696 BODY *body, int flags, int chflags)
701 if ((msg = mx_open_new_message (dest, hdr, (src->magic == M_MBOX || src->magic == M_MMDF) ? 0 : M_ADD_FROM)) == NULL)
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)
710 mx_close_message (&msg);
715 mutt_append_message (CONTEXT *dest, CONTEXT *src, HEADER *hdr, int cmflags,
721 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
723 r = _mutt_append_message (dest, msg->fp, src, hdr, hdr->content, cmflags, chflags);
724 mx_close_message (&msg);
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.
733 * The function will return 0 on success and -1 on failure.
735 static int copy_delete_attach (BODY *b, FILE *fpin, FILE *fpout, char *date)
739 for (part = b->parts ; part ; part = part->next)
741 if (part->deleted || part->parts)
743 /* Copy till start of this part */
744 if (mutt_copy_bytes (fpin, fpout, part->hdr_offset - ftell (fpin)))
750 "Content-Type: message/external-body; access-type=x-mutt-deleted;\n"
751 "\texpiration=%s; length=%ld\n"
752 "\n", date + 5, part->length);
756 /* Copy the original mime headers */
757 if (mutt_copy_bytes (fpin, fpout, part->offset - ftell (fpin)))
760 /* Skip the deleted body */
761 fseek (fpin, part->offset + part->length, SEEK_SET);
765 if (copy_delete_attach (part, fpin, fpout, date))
771 /* Copy the last parts */
772 if (mutt_copy_bytes (fpin, fpout, b->offset + b->length - ftell (fpin)))
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.
787 static void format_address_header (char **h, ADDRESS *a)
789 char buf[HUGE_STRING];
793 int l, linelen, buflen, count;
794 linelen = mutt_strlen (*h);
795 buflen = linelen + 3;
798 safe_realloc (h, buflen);
799 for (count = 0; a; a = a->next, count++)
801 ADDRESS *tmp = a->next;
803 *buf = *cbuf = *c2buf = '\0';
804 rfc822_write_address (buf, sizeof (buf), a, 0);
807 l = mutt_strlen (buf);
808 if (count && linelen + l > 74)
810 strcpy (cbuf, "\n\t"); /* __STRCPY_CHECKED__ */
817 strcpy (cbuf, " "); /* __STRCPY_CHECKED__ */
822 if (!a->group && a->next && a->next->mailbox)
826 strcpy (c2buf, ","); /* __STRCPY_CHECKED__ */
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__ */
836 /* Space for this was allocated in the beginning of this function. */
837 strcat (*h, "\n"); /* __STRCAT_CHECKED__ */
840 static int address_header_decode (char **h)
847 switch (tolower ((unsigned char) *s))
851 if (ascii_strncasecmp (s, "return-path:", 12) == 0)
856 else if (ascii_strncasecmp (s, "reply-to:", 9) == 0)
865 if (ascii_strncasecmp (s, "from:", 5))
872 if (ascii_strncasecmp (s, "cc:", 3))
880 if (ascii_strncasecmp (s, "bcc:", 4))
887 if (ascii_strncasecmp (s, "sender:", 7))
894 if (ascii_strncasecmp (s, "to:", 3))
901 if (ascii_strncasecmp (s, "mail-followup-to:", 17))
909 if ((a = rfc822_parse_adrlist (a, s + l + 1)) == NULL)
912 mutt_addrlist_to_local (a);
913 rfc2047_decode_adrlist (a);
915 *h = safe_calloc (1, l + 2);
917 strfcpy (*h, s, l + 1);
919 format_address_header (h, a);
921 rfc822_free_address (&a);