2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
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.
20 #include "mutt_crypt.h"
21 #include "mutt_idna.h"
26 #include <unistd.h> /* needed for SEEK_SET under SunOS 4.1.4 */
28 static int address_header_decode (char **str);
29 static int copy_delete_attach (BODY * b, FILE * fpin, FILE * fpout,
32 /* Ok, the only reason for not merging this with mutt_copy_header()
33 * below is to avoid creating a HEADER structure in message_handler().
36 mutt_copy_hdr (FILE * in, FILE * out, long off_start, long off_end, int flags,
42 char buf[STRING]; /* should be long enough to get most fields in one pass */
48 char *this_one = NULL;
51 if (ftell (in) != off_start)
52 fseek (in, off_start, 0);
58 (CH_REORDER | CH_WEED | CH_MIME | CH_DECODE | CH_PREFIX |
59 CH_WEED_DELIVERED)) == 0) {
60 /* Without these flags to complicate things
61 * we can do a more efficient line to line copying
63 while (ftell (in) < off_end) {
64 nl = strchr (buf, '\n');
66 if ((fgets (buf, sizeof (buf), in)) == NULL)
69 /* Is it the begining of a header? */
70 if (nl && buf[0] != ' ' && buf[0] != '\t') {
72 if (!from && mutt_strncmp ("From ", buf, 5) == 0) {
73 if ((flags & CH_FROM) == 0)
77 else if (flags & (CH_NOQFROM) &&
78 ascii_strncasecmp (">From ", buf, 6) == 0)
81 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
82 break; /* end of header */
84 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
85 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
86 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
88 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
89 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
90 ascii_strncasecmp ("Lines:", buf, 6) == 0))
92 if ((flags & CH_UPDATE_REFS) &&
93 ascii_strncasecmp ("References:", buf, 11) == 0)
95 if ((flags & CH_UPDATE_IRT) &&
96 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 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) {
116 for (t = HeaderOrderList; t; t = t->next) {
117 dprint (1, (debugfile, "Reorder list: %s\n", t->data));
122 dprint (1, (debugfile, "WEED is %s\n", (flags & CH_WEED) ? "Set" : "Not"));
124 headers = safe_calloc (hdr_count, sizeof (char *));
126 /* Read all the headers into the array */
127 while (ftell (in) < off_end) {
128 nl = strchr (buf, '\n');
131 if ((fgets (buf, sizeof (buf), in)) == NULL)
134 /* Is it the begining of a header? */
135 if (nl && buf[0] != ' ' && buf[0] != '\t') {
136 /* Do we have anything pending? */
138 if (flags & CH_DECODE) {
139 if (!address_header_decode (&this_one))
140 rfc2047_decode (&this_one);
144 headers[x] = this_one;
146 safe_realloc (&headers[x], mutt_strlen (headers[x]) +
147 mutt_strlen (this_one) + sizeof (char));
148 strcat (headers[x], this_one); /* __STRCAT_CHECKED__ */
157 if (!from && mutt_strncmp ("From ", buf, 5) == 0) {
158 if ((flags & CH_FROM) == 0)
160 this_is_from = from = 1;
162 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
163 break; /* end of header */
165 /* note: CH_FROM takes precedence over header weeding. */
166 if (!((flags & CH_FROM) && (flags & CH_FORCE_FROM) && this_is_from) &&
168 mutt_matches_ignore (buf, Ignore) &&
169 !mutt_matches_ignore (buf, UnIgnore))
171 if ((flags & CH_WEED_DELIVERED) &&
172 ascii_strncasecmp ("Delivered-To:", buf, 13) == 0)
174 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
175 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
176 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
178 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
179 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
180 ascii_strncasecmp ("Lines:", buf, 6) == 0))
182 if ((flags & CH_MIME) &&
183 ((ascii_strncasecmp ("content-", buf, 8) == 0 &&
184 (ascii_strncasecmp ("transfer-encoding:", buf + 8, 18) == 0 ||
185 ascii_strncasecmp ("type:", buf + 8, 5) == 0)) ||
186 ascii_strncasecmp ("mime-version:", buf, 13) == 0))
188 if ((flags & CH_UPDATE_REFS) &&
189 ascii_strncasecmp ("References:", buf, 11) == 0)
191 if ((flags & CH_UPDATE_IRT) &&
192 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
195 /* Find x -- the array entry where this header is to be saved */
196 if (flags & CH_REORDER) {
197 for (t = HeaderOrderList, x = 0; (t); t = t->next, x++) {
198 if (!ascii_strncasecmp (buf, t->data, mutt_strlen (t->data))) {
199 dprint (2, (debugfile, "Reorder: %s matches %s\n", t->data, buf));
206 } /* If beginning of header */
210 (debugfile, "Reorder: x = %d; hdr_count = %d\n", x, hdr_count));
212 this_one = safe_strdup (buf);
214 safe_realloc (&this_one,
215 mutt_strlen (this_one) + mutt_strlen (buf) +
217 strcat (this_one, buf); /* __STRCAT_CHECKED__ */
220 } /* while (ftell (in) < off_end) */
222 /* Do we have anything pending? -- XXX, same code as in above in the loop. */
224 if (flags & CH_DECODE) {
225 if (!address_header_decode (&this_one))
226 rfc2047_decode (&this_one);
230 headers[x] = this_one;
232 safe_realloc (&headers[x], mutt_strlen (headers[x]) +
233 mutt_strlen (this_one) + sizeof (char));
234 strcat (headers[x], this_one); /* __STRCAT_CHECKED__ */
241 /* Now output the headers in order */
242 for (x = 0; x < hdr_count; x++) {
245 if (flags & CH_DECODE)
246 rfc2047_decode (&headers[x]);
249 /* We couldn't do the prefixing when reading because RFC 2047
250 * decoding may have concatenated lines.
252 if (flags & CH_PREFIX) {
253 char *ch = headers[x];
254 int print_prefix = 1;
258 if (fputs (prefix, out) == EOF) {
265 if (*ch == '\n' && ch[1])
268 if (putc (*ch++, out) == EOF) {
277 if (fputs (headers[x], out) == EOF) {
285 /* Free in a separate loop to be sure that all headers are freed
286 * in case of error. */
287 for (x = 0; x < hdr_count; x++)
297 CH_DECODE RFC2047 header decoding
298 CH_FROM retain the "From " message separator
299 CH_FORCE_FROM give CH_FROM precedence over CH_WEED
300 CH_MIME ignore MIME fields
301 CH_NOLEN don't write Content-Length: and Lines:
302 CH_NONEWLINE don't output a newline after the header
303 CH_NOSTATUS ignore the Status: and X-Status:
304 CH_PREFIX quote header with $indent_str
305 CH_REORDER output header in order specified by `hdr_order'
306 CH_TXTPLAIN generate text/plain MIME headers [hack alert.]
307 CH_UPDATE write new Status: and X-Status:
308 CH_UPDATE_LEN write new Content-Length: and Lines:
309 CH_XMIT ignore Lines: and Content-Length:
310 CH_WEED do header weeding
311 CH_NOQFROM ignore ">From " line
312 CH_UPDATE_IRT update the In-Reply-To: header
313 CH_UPDATE_REFS update the References: header
316 string to use if CH_PREFIX is set
320 mutt_copy_header (FILE * in, HEADER * h, FILE * out, int flags,
323 char buffer[SHORT_STRING];
325 flags |= (h->irt_changed ? CH_UPDATE_IRT : 0)
326 | (h->refs_changed ? CH_UPDATE_REFS : 0);
328 if (mutt_copy_hdr (in, out, h->offset, h->content->offset, flags, prefix) ==
332 if (flags & CH_TXTPLAIN) {
333 char chsbuf[SHORT_STRING];
335 fputs ("Mime-Version: 1.0\n", out);
336 fputs ("Content-Transfer-Encoding: 8bit\n", out);
337 fputs ("Content-Type: text/plain; charset=", out);
338 mutt_canonical_charset (chsbuf, sizeof (chsbuf),
339 Charset ? Charset : "us-ascii");
340 rfc822_cat (buffer, sizeof (buffer), chsbuf, MimeSpecials);
344 if (ferror (out) != 0 || feof (out) != 0)
349 if (flags & CH_UPDATE) {
350 if ((flags & CH_NOSTATUS) == 0) {
351 if (h->irt_changed && h->new_env->in_reply_to) {
352 LIST *listp = h->new_env->in_reply_to;
354 if (fputs ("In-Reply-To: ", out) == EOF)
357 for (; listp; listp = listp->next)
358 if ((fputs (listp->data, out) == EOF) || (fputc (' ', out) == EOF))
361 if (fputc ('\n', out) == EOF)
365 if (h->refs_changed && h->new_env->references) {
366 LIST *listp = h->new_env->references, *refs = NULL, *t;
368 if (fputs ("References: ", out) == EOF)
371 /* Mutt stores references in reverse order, thus we create
372 * a reordered refs list that we can put in the headers */
373 for (; listp; listp = listp->next, refs = t) {
374 t = (LIST *) safe_malloc (sizeof (LIST));
375 t->data = listp->data;
379 for (; refs; refs = refs->next)
380 if ((fputs (refs->data, out) == EOF) || (fputc (' ', out) == EOF))
383 /* clearing refs from memory */
384 for (t = refs; refs; refs = t->next, t = refs)
387 if (fputc ('\n', out) == EOF)
391 if (h->old || h->read) {
392 if (fputs ("Status: ", out) == EOF)
396 if (fputs ("RO", out) == EOF)
400 if (fputc ('O', out) == EOF)
404 if (fputc ('\n', out) == EOF)
408 if (h->flagged || h->replied) {
409 if (fputs ("X-Status: ", out) == EOF)
413 if (fputc ('A', out) == EOF)
418 if (fputc ('F', out) == EOF)
422 if (fputc ('\n', out) == EOF)
428 if (flags & CH_UPDATE_LEN && (flags & CH_NOLEN) == 0) {
429 fprintf (out, "Content-Length: %ld\n", h->content->length);
430 if (h->lines != 0 || h->content->length == 0)
431 fprintf (out, "Lines: %d\n", h->lines);
434 if ((flags & CH_NONEWLINE) == 0) {
435 if (flags & CH_PREFIX)
437 if (fputc ('\n', out) == EOF) /* add header terminator */
441 if (ferror (out) || feof (out))
447 /* Count the number of lines and bytes to be deleted in this body*/
448 static int count_delete_lines (FILE * fp, BODY * b, long *length,
456 fseek (fp, b->offset, SEEK_SET);
457 for (l = b->length; l; l--) {
465 *length -= b->length - (84 + datelen);
466 /* Count the number of digits exceeding the first one to write the size */
467 for (l = 10; b->length >= l; l *= 10)
471 for (b = b->parts; b; b = b->next)
472 dellines += count_delete_lines (fp, b, length, datelen);
477 /* make a copy of a message
479 * fpout where to write output
480 * fpin where to get input
481 * hdr header of message being copied
482 * body structure of message being copied
484 * M_CM_NOHEADER don't copy header
485 * M_CM_PREFIX quote header and body
486 * M_CM_DECODE decode message body to text/plain
487 * M_CM_DISPLAY displaying output to the user
488 * M_CM_PRINTING printing the message
489 * M_CM_UPDATE update structures in memory after syncing
490 * M_CM_DECODE_PGP used for decoding PGP messages
491 * M_CM_CHARCONV perform character set conversion
492 * chflags flags to mutt_copy_header()
496 _mutt_copy_message (FILE * fpout, FILE * fpin, HEADER * hdr, BODY * body,
497 int flags, int chflags)
499 char prefix[SHORT_STRING];
501 long new_offset = -1;
503 if (flags & M_CM_PREFIX) {
504 if (option (OPTTEXTFLOWED))
505 strfcpy (prefix, ">", sizeof (prefix));
507 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
511 if ((flags & M_CM_NOHEADER) == 0) {
512 if (flags & M_CM_PREFIX)
513 chflags |= CH_PREFIX;
515 else if (hdr->attach_del && (chflags & CH_UPDATE_LEN)) {
517 long new_length = body->length;
518 char date[SHORT_STRING];
520 mutt_make_date (date, sizeof (date));
521 date[5] = date[mutt_strlen (date) - 1] = '\"';
523 /* Count the number of lines and bytes to be deleted */
524 fseek (fpin, body->offset, SEEK_SET);
525 new_lines = hdr->lines -
526 count_delete_lines (fpin, body, &new_length, mutt_strlen (date));
528 /* Copy the headers */
529 if (mutt_copy_header (fpin, hdr, fpout,
530 chflags | CH_NOLEN | CH_NONEWLINE, NULL))
532 fprintf (fpout, "Content-Length: %ld\n", new_length);
536 fprintf (fpout, "Lines: %d\n\n", new_lines);
537 if (ferror (fpout) || feof (fpout))
539 new_offset = ftell (fpout);
542 fseek (fpin, body->offset, SEEK_SET);
543 if (copy_delete_attach (body, fpin, fpout, date))
548 long fail = ((ftell (fpout) - new_offset) - new_length);
551 mutt_error ("The length calculation was wrong by %ld bytes", fail);
558 /* Update original message if we are sync'ing a mailfolder */
559 if (flags & M_CM_UPDATE) {
561 hdr->lines = new_lines;
562 body->offset = new_offset;
564 /* update the total size of the mailbox to reflect this deletion */
565 Context->size -= body->length - new_length;
567 * if the message is visible, update the visible size of the mailbox
570 if (Context->v2r[hdr->msgno] != -1)
571 Context->vsize -= body->length - new_length;
573 body->length = new_length;
574 mutt_free_body (&body->parts);
580 if (mutt_copy_header (fpin, hdr, fpout, chflags,
581 (chflags & CH_PREFIX) ? prefix : NULL) == -1)
584 new_offset = ftell (fpout);
587 if (flags & M_CM_DECODE) {
588 /* now make a text/plain version of the message */
589 memset (&s, 0, sizeof (STATE));
592 if (flags & M_CM_PREFIX)
594 if (flags & M_CM_DISPLAY)
595 s.flags |= M_DISPLAY;
596 if (flags & M_CM_PRINTING)
597 s.flags |= M_PRINTING;
598 if (flags & M_CM_WEED)
600 if (flags & M_CM_CHARCONV)
601 s.flags |= M_CHARCONV;
602 if (flags & M_CM_REPLYING)
603 s.flags |= M_REPLYING;
605 if (WithCrypto && flags & M_CM_VERIFY)
608 mutt_body_handler (body, &s);
611 && (flags & M_CM_DECODE_CRYPT) && (hdr->security & ENCRYPT)) {
615 if ((WithCrypto & APPLICATION_PGP)
616 && (flags & M_CM_DECODE_PGP) && (hdr->security & APPLICATION_PGP) &&
617 hdr->content->type == TYPEMULTIPART) {
618 if (crypt_pgp_decrypt_mime (fpin, &fp, hdr->content, &cur))
620 fputs ("Mime-Version: 1.0\n", fpout);
623 if ((WithCrypto & APPLICATION_SMIME)
624 && (flags & M_CM_DECODE_SMIME) && (hdr->security & APPLICATION_SMIME)
625 && hdr->content->type == TYPEAPPLICATION) {
626 if (crypt_smime_decrypt_mime (fpin, &fp, hdr->content, &cur))
630 mutt_write_mime_header (cur, fpout);
633 fseek (fp, cur->offset, 0);
634 if (mutt_copy_bytes (fp, fpout, cur->length) == -1) {
636 mutt_free_body (&cur);
639 mutt_free_body (&cur);
643 fseek (fpin, body->offset, 0);
644 if (flags & M_CM_PREFIX) {
646 size_t bytes = body->length;
648 fputs (prefix, fpout);
650 while ((c = fgetc (fpin)) != EOF && bytes--) {
653 fputs (prefix, fpout);
657 else if (mutt_copy_bytes (fpin, fpout, body->length) == -1)
661 if ((flags & M_CM_UPDATE) && (flags & M_CM_NOHEADER) == 0
662 && new_offset != -1) {
663 body->offset = new_offset;
664 mutt_free_body (&body->parts);
671 mutt_copy_message (FILE * fpout, CONTEXT * src, HEADER * hdr, int flags,
677 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
680 _mutt_copy_message (fpout, msg->fp, hdr, hdr->content, flags,
681 chflags)) == 0 && (ferror (fpout)
683 dprint (1, (debugfile, "_mutt_copy_message failed to detect EOF!\n"));
686 mx_close_message (&msg);
690 /* appends a copy of the given message to a mailbox
692 * dest destination mailbox
693 * fpin where to get input
695 * hdr message being copied
696 * body structure of message being copied
697 * flags mutt_copy_message() flags
698 * chflags mutt_copy_header() flags
702 _mutt_append_message (CONTEXT * dest, FILE * fpin, CONTEXT * src,
703 HEADER * hdr, BODY * body, int flags, int chflags)
709 mx_open_new_message (dest, hdr,
710 (src->magic == M_MBOX
711 || src->magic == M_MMDF) ? 0 : M_ADD_FROM)) ==
714 if (dest->magic == M_MBOX || dest->magic == M_MMDF)
715 chflags |= CH_FROM | CH_FORCE_FROM;
716 chflags |= (dest->magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
717 r = _mutt_copy_message (msg->fp, fpin, hdr, body, flags, chflags);
718 if (mx_commit_message (msg, dest) != 0)
721 mx_close_message (&msg);
726 mutt_append_message (CONTEXT * dest, CONTEXT * src, HEADER * hdr, int cmflags,
732 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
735 _mutt_append_message (dest, msg->fp, src, hdr, hdr->content, cmflags,
737 mx_close_message (&msg);
742 * This function copies a message body, while deleting _in_the_copy_
743 * any attachments which are marked for deletion.
744 * Nothing is changed in the original message -- this is left to the caller.
746 * The function will return 0 on success and -1 on failure.
748 static int copy_delete_attach (BODY * b, FILE * fpin, FILE * fpout,
753 for (part = b->parts; part; part = part->next) {
754 if (part->deleted || part->parts) {
755 /* Copy till start of this part */
756 if (mutt_copy_bytes (fpin, fpout, part->hdr_offset - ftell (fpin)))
761 "Content-Type: message/external-body; access-type=x-mutt-deleted;\n"
762 "\texpiration=%s; length=%ld\n"
763 "\n", date + 5, part->length);
767 /* Copy the original mime headers */
768 if (mutt_copy_bytes (fpin, fpout, part->offset - ftell (fpin)))
771 /* Skip the deleted body */
772 fseek (fpin, part->offset + part->length, SEEK_SET);
775 if (copy_delete_attach (part, fpin, fpout, date))
781 /* Copy the last parts */
782 if (mutt_copy_bytes (fpin, fpout, b->offset + b->length - ftell (fpin)))
789 * This function is the equivalent of mutt_write_address_list(),
790 * but writes to a buffer instead of writing to a stream.
791 * mutt_write_address_list could be re-used if we wouldn't store
792 * all the decoded headers in a huge array, first.
797 static void format_address_header (char **h, ADDRESS * a)
799 char buf[HUGE_STRING];
803 int l, linelen, buflen, count;
805 linelen = mutt_strlen (*h);
806 buflen = linelen + 3;
809 safe_realloc (h, buflen);
810 for (count = 0; a; a = a->next, count++) {
811 ADDRESS *tmp = a->next;
814 *buf = *cbuf = *c2buf = '\0';
815 rfc822_write_address (buf, sizeof (buf), a, 0);
818 l = mutt_strlen (buf);
819 if (count && linelen + l > 74) {
820 strcpy (cbuf, "\n\t"); /* __STRCPY_CHECKED__ */
825 strcpy (cbuf, " "); /* __STRCPY_CHECKED__ */
830 if (!a->group && a->next && a->next->mailbox) {
833 strcpy (c2buf, ","); /* __STRCPY_CHECKED__ */
836 buflen += l + mutt_strlen (cbuf) + mutt_strlen (c2buf);
837 safe_realloc (h, buflen);
838 strcat (*h, cbuf); /* __STRCAT_CHECKED__ */
839 strcat (*h, buf); /* __STRCAT_CHECKED__ */
840 strcat (*h, c2buf); /* __STRCAT_CHECKED__ */
843 /* Space for this was allocated in the beginning of this function. */
844 strcat (*h, "\n"); /* __STRCAT_CHECKED__ */
847 static int address_header_decode (char **h)
854 switch (tolower ((unsigned char) *s)) {
857 if (ascii_strncasecmp (s, "return-path:", 12) == 0) {
861 else if (ascii_strncasecmp (s, "reply-to:", 9) == 0) {
869 if (ascii_strncasecmp (s, "from:", 5))
876 if (ascii_strncasecmp (s, "cc:", 3))
884 if (ascii_strncasecmp (s, "bcc:", 4))
891 if (ascii_strncasecmp (s, "sender:", 7))
898 if (ascii_strncasecmp (s, "to:", 3))
905 if (ascii_strncasecmp (s, "mail-followup-to:", 17))
914 if ((a = rfc822_parse_adrlist (a, s + l)) == NULL)
917 mutt_addrlist_to_local (a);
918 rfc2047_decode_adrlist (a);
920 *h = safe_calloc (1, l + 2);
922 strfcpy (*h, s, l + 1);
924 format_address_header (h, a);
926 rfc822_free_address (&a);