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.
21 #include "mutt_crypt.h"
22 #include "mutt_idna.h"
26 #include "lib/debug.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,
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().
41 mutt_copy_hdr (FILE * in, FILE * out, long off_start, long off_end, int flags,
47 char buf[STRING]; /* should be long enough to get most fields in one pass */
53 char *this_one = NULL;
56 if (ftell (in) != off_start)
57 fseek (in, off_start, 0);
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
68 while (ftell (in) < off_end) {
69 nl = strchr (buf, '\n');
71 if ((fgets (buf, sizeof (buf), in)) == NULL)
74 /* Is it the begining of a header? */
75 if (nl && buf[0] != ' ' && buf[0] != '\t') {
77 if (!from && str_ncmp ("From ", buf, 5) == 0) {
78 if ((flags & CH_FROM) == 0)
82 else if (flags & (CH_NOQFROM) &&
83 ascii_strncasecmp (">From ", buf, 6) == 0)
86 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
87 break; /* end of header */
89 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
90 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
91 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
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))
97 if ((flags & CH_UPDATE_REFS) &&
98 ascii_strncasecmp ("References:", buf, 11) == 0)
100 if ((flags & CH_UPDATE_IRT) &&
101 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
106 if (!ignore && fputs (buf, out) == EOF)
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
120 if (flags & CH_REORDER) {
121 for (t = HeaderOrderList; t; t = t->next) {
122 debug_print (1, ("Reorder list: %s\n", t->data));
127 debug_print (1, ("WEED is %s\n", (flags & CH_WEED) ? "Set" : "Not"));
129 headers = mem_calloc (hdr_count, sizeof (char *));
131 /* Read all the headers into the array */
132 while (ftell (in) < off_end) {
133 nl = strchr (buf, '\n');
136 if ((fgets (buf, sizeof (buf), in)) == NULL)
139 /* Is it the begining of a header? */
140 if (nl && buf[0] != ' ' && buf[0] != '\t') {
141 /* Do we have anything pending? */
143 if (flags & CH_DECODE) {
144 if (!address_header_decode (&this_one))
145 rfc2047_decode (&this_one);
149 headers[x] = this_one;
151 mem_realloc (&headers[x], str_len (headers[x]) +
152 str_len (this_one) + sizeof (char));
153 strcat (headers[x], this_one); /* __STRCAT_CHECKED__ */
154 mem_free (&this_one);
162 if (!from && str_ncmp ("From ", buf, 5) == 0) {
163 if ((flags & CH_FROM) == 0)
165 this_is_from = from = 1;
167 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
168 break; /* end of header */
170 /* note: CH_FROM takes precedence over header weeding. */
171 if (!((flags & CH_FROM) && (flags & CH_FORCE_FROM) && this_is_from) &&
173 mutt_matches_ignore (buf, Ignore) &&
174 !mutt_matches_ignore (buf, UnIgnore))
176 if ((flags & CH_WEED_DELIVERED) &&
177 ascii_strncasecmp ("Delivered-To:", buf, 13) == 0)
179 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
180 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
181 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
183 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
184 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
185 ascii_strncasecmp ("Lines:", buf, 6) == 0))
187 if ((flags & CH_MIME) &&
188 ((ascii_strncasecmp ("content-", buf, 8) == 0 &&
189 (ascii_strncasecmp ("transfer-encoding:", buf + 8, 18) == 0 ||
190 ascii_strncasecmp ("type:", buf + 8, 5) == 0)) ||
191 ascii_strncasecmp ("mime-version:", buf, 13) == 0))
193 if ((flags & CH_UPDATE_REFS) &&
194 ascii_strncasecmp ("References:", buf, 11) == 0)
196 if ((flags & CH_UPDATE_IRT) &&
197 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
200 /* Find x -- the array entry where this header is to be saved */
201 if (flags & CH_REORDER) {
202 for (t = HeaderOrderList, x = 0; (t); t = t->next, x++) {
203 if (!ascii_strncasecmp (buf, t->data, str_len (t->data))) {
204 debug_print (2, ("Reorder: %s matches %s\n", t->data, buf));
211 } /* If beginning of header */
214 debug_print (2, ("Reorder: x = %d; hdr_count = %d\n", x, hdr_count));
216 this_one = str_dup (buf);
218 mem_realloc (&this_one,
219 str_len (this_one) + str_len (buf) +
221 strcat (this_one, buf); /* __STRCAT_CHECKED__ */
224 } /* while (ftell (in) < off_end) */
226 /* Do we have anything pending? -- XXX, same code as in above in the loop. */
228 if (flags & CH_DECODE) {
229 if (!address_header_decode (&this_one))
230 rfc2047_decode (&this_one);
234 headers[x] = this_one;
236 mem_realloc (&headers[x], str_len (headers[x]) +
237 str_len (this_one) + sizeof (char));
238 strcat (headers[x], this_one); /* __STRCAT_CHECKED__ */
239 mem_free (&this_one);
245 /* Now output the headers in order */
246 for (x = 0; x < hdr_count; x++) {
249 if (flags & CH_DECODE)
250 rfc2047_decode (&headers[x]);
253 /* We couldn't do the prefixing when reading because RFC 2047
254 * decoding may have concatenated lines.
256 if (flags & CH_PREFIX) {
257 char *ch = headers[x];
258 int print_prefix = 1;
262 if (fputs (prefix, out) == EOF) {
269 if (*ch == '\n' && ch[1])
272 if (putc (*ch++, out) == EOF) {
281 if (fputs (headers[x], out) == EOF) {
289 /* Free in a separate loop to be sure that all headers are freed
290 * in case of error. */
291 for (x = 0; x < hdr_count; x++)
292 mem_free (&headers[x]);
301 CH_DECODE RFC2047 header decoding
302 CH_FROM retain the "From " message separator
303 CH_FORCE_FROM give CH_FROM precedence over CH_WEED
304 CH_MIME ignore MIME fields
305 CH_NOLEN don't write Content-Length: and Lines:
306 CH_NONEWLINE don't output a newline after the header
307 CH_NOSTATUS ignore the Status: and X-Status:
308 CH_PREFIX quote header with $indent_str
309 CH_REORDER output header in order specified by `hdr_order'
310 CH_TXTPLAIN generate text/plain MIME headers [hack alert.]
311 CH_UPDATE write new Status: and X-Status:
312 CH_UPDATE_LEN write new Content-Length: and Lines:
313 CH_XMIT ignore Lines: and Content-Length:
314 CH_WEED do header weeding
315 CH_NOQFROM ignore ">From " line
316 CH_UPDATE_IRT update the In-Reply-To: header
317 CH_UPDATE_REFS update the References: header
320 string to use if CH_PREFIX is set
324 mutt_copy_header (FILE * in, HEADER * h, FILE * out, int flags,
327 char buffer[SHORT_STRING];
330 flags |= (h->env->irt_changed ? CH_UPDATE_IRT : 0) |
331 (h->env->refs_changed ? CH_UPDATE_REFS : 0);
333 if (mutt_copy_hdr (in, out, h->offset, h->content->offset, flags, prefix) ==
337 if (flags & CH_TXTPLAIN) {
338 char chsbuf[SHORT_STRING];
340 fputs ("Mime-Version: 1.0\n", out);
341 fputs ("Content-Transfer-Encoding: 8bit\n", out);
342 fputs ("Content-Type: text/plain; charset=", out);
343 mutt_canonical_charset (chsbuf, sizeof (chsbuf),
344 Charset ? Charset : "us-ascii");
345 rfc822_cat (buffer, sizeof (buffer), chsbuf, MimeSpecials);
349 if (ferror (out) != 0 || feof (out) != 0)
354 if (flags & CH_UPDATE) {
355 if ((flags & CH_NOSTATUS) == 0) {
356 if (h->env->irt_changed && h->env->in_reply_to) {
357 LIST *listp = h->env->in_reply_to;
359 if (fputs ("In-Reply-To: ", out) == EOF)
362 for (; listp; listp = listp->next)
363 if ((fputs (listp->data, out) == EOF) || (fputc (' ', out) == EOF))
366 if (fputc ('\n', out) == EOF)
370 if (h->env->refs_changed && h->env->references) {
371 LIST *listp = h->env->references, *refs = NULL, *t;
373 if (fputs ("References: ", out) == EOF)
376 /* Mutt stores references in reverse order, thus we create
377 * a reordered refs list that we can put in the headers */
378 for (; listp; listp = listp->next, refs = t) {
379 t = (LIST *) mem_malloc (sizeof (LIST));
380 t->data = listp->data;
384 for (; refs; refs = refs->next)
385 if ((fputs (refs->data, out) == EOF) || (fputc (' ', out) == EOF))
388 /* clearing refs from memory */
389 for (t = refs; refs; refs = t->next, t = refs)
392 if (fputc ('\n', out) == EOF)
396 if (h->old || h->read) {
397 if (fputs ("Status: ", out) == EOF)
401 if (fputs ("RO", out) == EOF)
405 if (fputc ('O', out) == EOF)
409 if (fputc ('\n', out) == EOF)
413 if (h->flagged || h->replied) {
414 if (fputs ("X-Status: ", out) == EOF)
418 if (fputc ('A', out) == EOF)
423 if (fputc ('F', out) == EOF)
427 if (fputc ('\n', out) == EOF)
433 if (flags & CH_UPDATE_LEN && (flags & CH_NOLEN) == 0) {
434 fprintf (out, "Content-Length: %ld\n", h->content->length);
435 if (h->lines != 0 || h->content->length == 0)
436 fprintf (out, "Lines: %d\n", h->lines);
439 if ((flags & CH_NONEWLINE) == 0) {
440 if (flags & CH_PREFIX)
442 if (fputc ('\n', out) == EOF) /* add header terminator */
446 if (ferror (out) || feof (out))
452 /* Count the number of lines and bytes to be deleted in this body*/
453 static int count_delete_lines (FILE * fp, BODY * b, long *length,
461 fseek (fp, b->offset, SEEK_SET);
462 for (l = b->length; l; l--) {
470 *length -= b->length - (84 + datelen);
471 /* Count the number of digits exceeding the first one to write the size */
472 for (l = 10; b->length >= l; l *= 10)
476 for (b = b->parts; b; b = b->next)
477 dellines += count_delete_lines (fp, b, length, datelen);
482 /* make a copy of a message
484 * fpout where to write output
485 * fpin where to get input
486 * hdr header of message being copied
487 * body structure of message being copied
489 * M_CM_NOHEADER don't copy header
490 * M_CM_PREFIX quote header and body
491 * M_CM_DECODE decode message body to text/plain
492 * M_CM_DISPLAY displaying output to the user
493 * M_CM_PRINTING printing the message
494 * M_CM_UPDATE update structures in memory after syncing
495 * M_CM_DECODE_PGP used for decoding PGP messages
496 * M_CM_CHARCONV perform character set conversion
497 * chflags flags to mutt_copy_header()
501 _mutt_copy_message (FILE * fpout, FILE * fpin, HEADER * hdr, BODY * body,
502 int flags, int chflags)
504 char prefix[SHORT_STRING];
506 long new_offset = -1;
508 if (flags & M_CM_PREFIX) {
509 if (option (OPTTEXTFLOWED))
510 strfcpy (prefix, ">", sizeof (prefix));
512 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
516 if ((flags & M_CM_NOHEADER) == 0) {
517 if (flags & M_CM_PREFIX)
518 chflags |= CH_PREFIX;
520 else if (hdr->attach_del && (chflags & CH_UPDATE_LEN)) {
522 long new_length = body->length;
523 char date[SHORT_STRING];
525 mutt_make_date (date, sizeof (date));
526 date[5] = date[str_len (date) - 1] = '\"';
528 /* Count the number of lines and bytes to be deleted */
529 fseek (fpin, body->offset, SEEK_SET);
530 new_lines = hdr->lines -
531 count_delete_lines (fpin, body, &new_length, str_len (date));
533 /* Copy the headers */
534 if (mutt_copy_header (fpin, hdr, fpout,
535 chflags | CH_NOLEN | CH_NONEWLINE, NULL))
537 fprintf (fpout, "Content-Length: %ld\n", new_length);
541 fprintf (fpout, "Lines: %d\n\n", new_lines);
542 if (ferror (fpout) || feof (fpout))
544 new_offset = ftell (fpout);
547 fseek (fpin, body->offset, SEEK_SET);
548 if (copy_delete_attach (body, fpin, fpout, date))
553 long fail = ((ftell (fpout) - new_offset) - new_length);
556 mutt_error ("The length calculation was wrong by %ld bytes", fail);
563 /* Update original message if we are sync'ing a mailfolder */
564 if (flags & M_CM_UPDATE) {
566 hdr->lines = new_lines;
567 body->offset = new_offset;
569 /* update the total size of the mailbox to reflect this deletion */
570 Context->size -= body->length - new_length;
572 * if the message is visible, update the visible size of the mailbox
575 if (Context->v2r[hdr->msgno] != -1)
576 Context->vsize -= body->length - new_length;
578 body->length = new_length;
579 mutt_free_body (&body->parts);
585 if (mutt_copy_header (fpin, hdr, fpout, chflags,
586 (chflags & CH_PREFIX) ? prefix : NULL) == -1)
589 new_offset = ftell (fpout);
592 if (flags & M_CM_DECODE) {
593 /* now make a text/plain version of the message */
594 memset (&s, 0, sizeof (STATE));
597 if (flags & M_CM_PREFIX)
599 if (flags & M_CM_DISPLAY)
600 s.flags |= M_DISPLAY;
601 if (flags & M_CM_PRINTING)
602 s.flags |= M_PRINTING;
603 if (flags & M_CM_WEED)
605 if (flags & M_CM_CHARCONV)
606 s.flags |= M_CHARCONV;
607 if (flags & M_CM_REPLYING)
608 s.flags |= M_REPLYING;
610 if (WithCrypto && flags & M_CM_VERIFY)
613 mutt_body_handler (body, &s);
616 && (flags & M_CM_DECODE_CRYPT) && (hdr->security & ENCRYPT)) {
620 if ((WithCrypto & APPLICATION_PGP)
621 && (flags & M_CM_DECODE_PGP) && (hdr->security & APPLICATION_PGP) &&
622 hdr->content->type == TYPEMULTIPART) {
623 if (crypt_pgp_decrypt_mime (fpin, &fp, hdr->content, &cur))
625 fputs ("Mime-Version: 1.0\n", fpout);
628 if ((WithCrypto & APPLICATION_SMIME)
629 && (flags & M_CM_DECODE_SMIME) && (hdr->security & APPLICATION_SMIME)
630 && hdr->content->type == TYPEAPPLICATION) {
631 if (crypt_smime_decrypt_mime (fpin, &fp, hdr->content, &cur))
635 mutt_write_mime_header (cur, fpout);
638 fseek (fp, cur->offset, 0);
639 if (mutt_copy_bytes (fp, fpout, cur->length) == -1) {
641 mutt_free_body (&cur);
644 mutt_free_body (&cur);
648 fseek (fpin, body->offset, 0);
649 if (flags & M_CM_PREFIX) {
651 size_t bytes = body->length;
653 fputs (prefix, fpout);
655 while ((c = fgetc (fpin)) != EOF && bytes--) {
658 fputs (prefix, fpout);
662 else if (mutt_copy_bytes (fpin, fpout, body->length) == -1)
666 if ((flags & M_CM_UPDATE) && (flags & M_CM_NOHEADER) == 0
667 && new_offset != -1) {
668 body->offset = new_offset;
669 mutt_free_body (&body->parts);
676 mutt_copy_message (FILE * fpout, CONTEXT * src, HEADER * hdr, int flags,
682 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
685 _mutt_copy_message (fpout, msg->fp, hdr, hdr->content, flags,
686 chflags)) == 0 && (ferror (fpout)
688 debug_print (1, ("_mutt_copy_message failed to detect EOF!\n"));
691 mx_close_message (&msg);
695 /* appends a copy of the given message to a mailbox
697 * dest destination mailbox
698 * fpin where to get input
700 * hdr message being copied
701 * body structure of message being copied
702 * flags mutt_copy_message() flags
703 * chflags mutt_copy_header() flags
707 _mutt_append_message (CONTEXT * dest, FILE * fpin, CONTEXT * src,
708 HEADER * hdr, BODY * body, int flags, int chflags) {
713 fseek(fpin, hdr->offset, 0);
714 if (fgets (buf, sizeof (buf), fpin) == NULL)
716 if ((msg = mx_open_new_message (dest, hdr, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL)
718 if (dest->magic == M_MBOX || dest->magic == M_MMDF)
719 chflags |= CH_FROM | CH_FORCE_FROM;
720 chflags |= (dest->magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
721 r = _mutt_copy_message (msg->fp, fpin, hdr, body, flags, chflags);
722 if (mx_commit_message (msg, dest) != 0)
725 mx_close_message (&msg);
730 mutt_append_message (CONTEXT * dest, CONTEXT * src, HEADER * hdr, int cmflags,
736 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
739 _mutt_append_message (dest, msg->fp, src, hdr, hdr->content, cmflags,
741 mx_close_message (&msg);
746 * This function copies a message body, while deleting _in_the_copy_
747 * any attachments which are marked for deletion.
748 * Nothing is changed in the original message -- this is left to the caller.
750 * The function will return 0 on success and -1 on failure.
752 static int copy_delete_attach (BODY * b, FILE * fpin, FILE * fpout,
757 for (part = b->parts; part; part = part->next) {
758 if (part->deleted || part->parts) {
759 /* Copy till start of this part */
760 if (mutt_copy_bytes (fpin, fpout, part->hdr_offset - ftell (fpin)))
765 "Content-Type: message/external-body; access-type=x-mutt-deleted;\n"
766 "\texpiration=%s; length=%ld\n"
767 "\n", date + 5, part->length);
771 /* Copy the original mime headers */
772 if (mutt_copy_bytes (fpin, fpout, part->offset - ftell (fpin)))
775 /* Skip the deleted body */
776 fseek (fpin, part->offset + part->length, SEEK_SET);
779 if (copy_delete_attach (part, fpin, fpout, date))
785 /* Copy the last parts */
786 if (mutt_copy_bytes (fpin, fpout, b->offset + b->length - ftell (fpin)))
793 * This function is the equivalent of mutt_write_address_list(),
794 * but writes to a buffer instead of writing to a stream.
795 * mutt_write_address_list could be re-used if we wouldn't store
796 * all the decoded headers in a huge array, first.
801 static void format_address_header (char **h, ADDRESS * a)
803 char buf[HUGE_STRING];
807 int l, linelen, buflen, count;
809 linelen = str_len (*h);
810 buflen = linelen + 3;
813 mem_realloc (h, buflen);
814 for (count = 0; a; a = a->next, count++) {
815 ADDRESS *tmp = a->next;
818 *buf = *cbuf = *c2buf = '\0';
819 rfc822_write_address (buf, sizeof (buf), a, 0);
823 if (count && linelen + l > 74) {
824 strcpy (cbuf, "\n\t"); /* __STRCPY_CHECKED__ */
829 strcpy (cbuf, " "); /* __STRCPY_CHECKED__ */
834 if (!a->group && a->next && a->next->mailbox) {
837 strcpy (c2buf, ","); /* __STRCPY_CHECKED__ */
840 buflen += l + str_len (cbuf) + str_len (c2buf);
841 mem_realloc (h, buflen);
842 strcat (*h, cbuf); /* __STRCAT_CHECKED__ */
843 strcat (*h, buf); /* __STRCAT_CHECKED__ */
844 strcat (*h, c2buf); /* __STRCAT_CHECKED__ */
847 /* Space for this was allocated in the beginning of this function. */
848 strcat (*h, "\n"); /* __STRCAT_CHECKED__ */
851 static int address_header_decode (char **h)
858 switch (tolower ((unsigned char) *s)) {
861 if (ascii_strncasecmp (s, "return-path:", 12) == 0) {
865 else if (ascii_strncasecmp (s, "reply-to:", 9) == 0) {
873 if (ascii_strncasecmp (s, "from:", 5))
880 if (ascii_strncasecmp (s, "cc:", 3))
888 if (ascii_strncasecmp (s, "bcc:", 4))
895 if (ascii_strncasecmp (s, "sender:", 7))
902 if (ascii_strncasecmp (s, "to:", 3))
909 if (ascii_strncasecmp (s, "mail-followup-to:", 17))
918 if ((a = rfc822_parse_adrlist (a, s + l)) == NULL)
921 mutt_addrlist_to_local (a);
922 rfc2047_decode_adrlist (a);
924 *h = mem_calloc (1, l + 2);
926 strfcpy (*h, s, l + 1);
928 format_address_header (h, a);
930 rfc822_free_address (&a);