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.
19 #include <lib-lib/lib-lib.h>
21 #include <lib-mime/mime.h>
27 #include <lib-crypt/crypt.h>
28 #include "mutt_idna.h"
30 static int address_header_decode (char **str);
31 static int copy_delete_attach (BODY * b, FILE * fpin, FILE * fpout,
34 /* Ok, the only reason for not merging this with mutt_copy_header()
35 * below is to avoid creating a HEADER structure in message_handler().
38 mutt_copy_hdr (FILE* in, FILE* out, off_t off_start, off_t off_end,
39 int flags, const char *prefix) {
43 char buf[STRING]; /* should be long enough to get most fields in one pass */
49 char *this_one = NULL;
53 if (ftello (in) != off_start)
54 fseeko (in, off_start, 0);
60 (CH_REORDER | CH_WEED | CH_MIME | CH_DECODE | CH_PREFIX |
61 CH_WEED_DELIVERED)) == 0) {
62 /* Without these flags to complicate things
63 * we can do a more efficient line to line copying
65 while (ftello (in) < off_end) {
66 nl = strchr (buf, '\n');
68 if ((fgets (buf, sizeof (buf), in)) == NULL)
71 /* Is it the begining of a header? */
72 if (nl && buf[0] != ' ' && buf[0] != '\t') {
74 if (!from && m_strncmp("From ", buf, 5) == 0) {
75 if ((flags & CH_FROM) == 0)
79 else if (flags & (CH_NOQFROM) &&
80 ascii_strncasecmp (">From ", buf, 6) == 0)
83 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
84 break; /* end of header */
86 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
87 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
88 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
90 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
91 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
92 ascii_strncasecmp ("Lines:", buf, 6) == 0))
94 if ((flags & CH_UPDATE_REFS) &&
95 ascii_strncasecmp ("References:", buf, 11) == 0)
97 if ((flags & CH_UPDATE_IRT) &&
98 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
103 if (!ignore && fputs (buf, out) == EOF)
113 /* We are going to read and collect the headers in an array
114 * so we are able to do re-ordering.
115 * First count the number of entries in the array
117 if (flags & CH_REORDER) {
118 for (t = HeaderOrderList; t; t = t->next) {
123 headers = p_new(char *, hdr_count);
125 /* Read all the headers into the array */
126 while (ftello (in) < off_end) {
127 nl = strchr (buf, '\n');
130 if ((fgets (buf, sizeof (buf), in)) == NULL)
133 /* Is it the begining of a header? */
134 if (nl && buf[0] != ' ' && buf[0] != '\t') {
136 /* set curline to 1 for To:/Cc:/Bcc: and 0 otherwise */
137 curline = (flags & CH_WEED) && (ascii_strncmp ("To:", buf, 3) == 0 ||
138 ascii_strncmp ("Cc:", buf, 3) == 0 ||
139 ascii_strncmp ("Bcc:", buf, 4) == 0);
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 p_realloc(&headers[x], m_strlen(headers[x]) + m_strlen(this_one) + 1);
152 strcat(headers[x], this_one); /* __STRCAT_CHECKED__ */
161 if (!from && m_strncmp("From ", buf, 5) == 0) {
162 if ((flags & CH_FROM) == 0)
164 this_is_from = from = 1;
166 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
167 break; /* end of header */
169 /* note: CH_FROM takes precedence over header weeding. */
170 if (!((flags & CH_FROM) && (flags & CH_FORCE_FROM) && this_is_from) &&
172 mutt_matches_ignore (buf, Ignore) &&
173 !mutt_matches_ignore (buf, UnIgnore))
175 if ((flags & CH_WEED_DELIVERED) &&
176 ascii_strncasecmp ("Delivered-To:", buf, 13) == 0)
178 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
179 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
180 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
182 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
183 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
184 ascii_strncasecmp ("Lines:", buf, 6) == 0))
186 if ((flags & CH_MIME) &&
187 ((ascii_strncasecmp ("content-", buf, 8) == 0 &&
188 (ascii_strncasecmp ("transfer-encoding:", buf + 8, 18) == 0 ||
189 ascii_strncasecmp ("type:", buf + 8, 5) == 0)) ||
190 ascii_strncasecmp ("mime-version:", buf, 13) == 0))
192 if ((flags & CH_UPDATE_REFS) &&
193 ascii_strncasecmp ("References:", buf, 11) == 0)
195 if ((flags & CH_UPDATE_IRT) &&
196 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
199 /* Find x -- the array entry where this header is to be saved */
200 if (flags & CH_REORDER) {
201 for (t = HeaderOrderList, x = 0; (t); t = t->next, x++) {
202 if (!ascii_strncasecmp (buf, t->data, m_strlen(t->data))) {
209 } /* If beginning of header */
213 this_one = m_strdup(buf);
214 /* we do want to see all lines if this header doesn't feature
215 * abbreviations (curline is 0), $max_display_recips is 0 and
216 * while the number hasn't reached $max_display_recips yet */
217 else if (curline == 0 || MaxDispRecips == 0 || ++curline <= MaxDispRecips) {
218 p_realloc(&this_one, m_strlen(this_one) + m_strlen(buf) + 1);
219 strcat (this_one, buf); /* __STRCAT_CHECKED__ */
220 /* only for the first line which doesn't exeeds
221 * $max_display_recips: abbreviate it */
222 } else if (curline == MaxDispRecips+1) {
223 p_realloc(&this_one, m_strlen(this_one) + 5);
224 strcat (this_one, " ...");
227 } /* while (ftello (in) < off_end) */
229 /* Do we have anything pending? -- XXX, same code as in above in the loop. */
231 if (flags & CH_DECODE) {
232 if (!address_header_decode (&this_one))
233 rfc2047_decode (&this_one);
237 headers[x] = this_one;
239 p_realloc(&headers[x], m_strlen(headers[x]) + m_strlen(this_one) + 1);
240 strcat (headers[x], this_one); /* __STRCAT_CHECKED__ */
247 /* Now output the headers in order */
248 for (x = 0; x < hdr_count; x++) {
251 if (flags & CH_DECODE)
252 rfc2047_decode (&headers[x]);
255 /* We couldn't do the prefixing when reading because RFC 2047
256 * decoding may have concatenated lines.
258 if (flags & CH_PREFIX) {
259 char *ch = headers[x];
260 int print_prefix = 1;
264 if (fputs (prefix, out) == EOF) {
271 if (*ch == '\n' && ch[1])
274 if (putc (*ch++, out) == EOF) {
283 if (fputs (headers[x], out) == EOF) {
291 /* Free in a separate loop to be sure that all headers are freed
292 * in case of error. */
293 for (x = 0; x < hdr_count; x++)
294 p_delete(&headers[x]);
303 CH_DECODE RFC2047 header decoding
304 CH_FROM retain the "From " message separator
305 CH_FORCE_FROM give CH_FROM precedence over CH_WEED
306 CH_MIME ignore MIME fields
307 CH_NOLEN don't write Content-Length: and Lines:
308 CH_NONEWLINE don't output a newline after the header
309 CH_NOSTATUS ignore the Status: and X-Status:
310 CH_PREFIX quote header with $indent_str
311 CH_REORDER output header in order specified by `hdr_order'
312 CH_TXTPLAIN generate text/plain MIME headers [hack alert.]
313 CH_UPDATE write new Status: and X-Status:
314 CH_UPDATE_LEN write new Content-Length: and Lines:
315 CH_XMIT ignore Lines: and Content-Length:
316 CH_WEED do header weeding
317 CH_NOQFROM ignore ">From " line
318 CH_UPDATE_IRT update the In-Reply-To: header
319 CH_UPDATE_REFS update the References: header
322 string to use if CH_PREFIX is set
326 mutt_copy_header (FILE * in, HEADER * h, FILE * out, int flags,
329 char buffer[SHORT_STRING];
332 flags |= (h->env->irt_changed ? CH_UPDATE_IRT : 0) |
333 (h->env->refs_changed ? CH_UPDATE_REFS : 0);
335 if (mutt_copy_hdr (in, out, h->offset, h->content->offset, flags, prefix) ==
339 if (flags & CH_TXTPLAIN) {
340 char chsbuf[SHORT_STRING];
342 fputs ("MIME-Version: 1.0\n", out);
343 fputs ("Content-Transfer-Encoding: 8bit\n", out);
344 fputs ("Content-Type: text/plain; charset=", out);
345 charset_canonicalize(chsbuf, sizeof (chsbuf), Charset);
346 rfc822_strcpy(buffer, sizeof(buffer), chsbuf, MimeSpecials);
350 if (ferror (out) != 0 || feof (out) != 0)
355 if (flags & CH_UPDATE) {
356 if ((flags & CH_NOSTATUS) == 0) {
357 if (h->env->irt_changed && h->env->in_reply_to) {
358 string_list_t *listp = h->env->in_reply_to;
360 if (fputs ("In-Reply-To: ", out) == EOF)
363 for (; listp; listp = listp->next)
364 if ((fputs (listp->data, out) == EOF) || (fputc (' ', out) == EOF))
367 if (fputc ('\n', out) == EOF)
371 if (h->env->refs_changed && h->env->references) {
372 string_list_t *listp = h->env->references, *refs = NULL, *t;
374 if (fputs ("References: ", out) == EOF)
377 /* Mutt stores references in reverse order, thus we create
378 * a reordered refs list that we can put in the headers */
379 for (; listp; listp = listp->next, refs = t) {
380 t = p_new(string_list_t, 1);
381 t->data = listp->data;
385 for (; refs; refs = refs->next)
386 if ((fputs (refs->data, out) == EOF) || (fputc (' ', out) == EOF))
389 /* clearing refs from memory */
390 for (t = refs; refs; refs = t->next, t = refs)
393 if (fputc ('\n', out) == EOF)
397 if (h->old || h->read) {
398 if (fputs ("Status: ", out) == EOF)
402 if (fputs ("RO", out) == EOF)
406 if (fputc ('O', out) == EOF)
410 if (fputc ('\n', out) == EOF)
414 if (h->flagged || h->replied) {
415 if (fputs ("X-Status: ", out) == EOF)
419 if (fputc ('A', out) == EOF)
424 if (fputc ('F', out) == EOF)
428 if (fputc ('\n', out) == EOF)
434 if (flags & CH_UPDATE_LEN && (flags & CH_NOLEN) == 0) {
435 fprintf (out, "Content-Length: %lld\n", h->content->length);
436 if (h->lines != 0 || h->content->length == 0)
437 fprintf (out, "Lines: %d\n", h->lines);
440 if ((flags & CH_NONEWLINE) == 0) {
441 if (flags & CH_PREFIX)
443 if (fputc ('\n', out) == EOF) /* add header terminator */
447 if (ferror (out) || feof (out))
453 /* Count the number of lines and bytes to be deleted in this body*/
454 static int count_delete_lines (FILE * fp, BODY * b, off_t *length,
462 fseeko (fp, b->offset, SEEK_SET);
463 for (l = b->length; l; l--) {
471 *length -= b->length - (84 + datelen);
472 /* Count the number of digits exceeding the first one to write the size */
473 for (l = 10; b->length >= l; l *= 10)
477 for (b = b->parts; b; b = b->next)
478 dellines += count_delete_lines (fp, b, length, datelen);
483 /* make a copy of a message
485 * fpout where to write output
486 * fpin where to get input
487 * hdr header of message being copied
488 * body structure of message being copied
490 * M_CM_NOHEADER don't copy header
491 * M_CM_PREFIX quote header and body
492 * M_CM_DECODE decode message body to text/plain
493 * M_CM_DISPLAY displaying output to the user
494 * M_CM_PRINTING printing the message
495 * M_CM_UPDATE update structures in memory after syncing
496 * M_CM_DECODE_PGP used for decoding PGP messages
497 * M_CM_CHARCONV perform character set conversion
498 * chflags flags to mutt_copy_header()
502 _mutt_copy_message (FILE * fpout, FILE * fpin, HEADER * hdr, BODY * body,
503 int flags, int chflags)
505 char prefix[SHORT_STRING];
507 off_t new_offset = -1;
510 if (flags & M_CM_PREFIX) {
511 if (option (OPTTEXTFLOWED))
512 m_strcpy(prefix, sizeof(prefix), ">");
514 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
518 if ((flags & M_CM_NOHEADER) == 0) {
519 if (flags & M_CM_PREFIX)
520 chflags |= CH_PREFIX;
522 else if (hdr->attach_del && (chflags & CH_UPDATE_LEN)) {
524 off_t new_length = body->length;
525 char date[SHORT_STRING];
527 mutt_make_date (date, sizeof (date));
528 date[5] = date[m_strlen(date) - 1] = '\"';
530 /* Count the number of lines and bytes to be deleted */
531 fseeko (fpin, body->offset, SEEK_SET);
532 new_lines = hdr->lines -
533 count_delete_lines (fpin, body, &new_length, m_strlen(date));
535 /* Copy the headers */
536 if (mutt_copy_header (fpin, hdr, fpout,
537 chflags | CH_NOLEN | CH_NONEWLINE, NULL))
539 fprintf (fpout, "Content-Length: %lld\n", new_length);
543 fprintf (fpout, "Lines: %d\n\n", new_lines);
544 if (ferror (fpout) || feof (fpout))
546 new_offset = ftello (fpout);
549 fseeko (fpin, body->offset, SEEK_SET);
550 if (copy_delete_attach (body, fpin, fpout, date))
553 /* Update original message if we are sync'ing a mailfolder */
554 if (flags & M_CM_UPDATE) {
556 hdr->lines = new_lines;
557 body->offset = new_offset;
559 /* update the total size of the mailbox to reflect this deletion */
560 Context->size -= body->length - new_length;
562 * if the message is visible, update the visible size of the mailbox
565 if (Context->v2r[hdr->msgno] != -1)
566 Context->vsize -= body->length - new_length;
568 body->length = new_length;
569 body_list_wipe(&body->parts);
575 if (mutt_copy_header (fpin, hdr, fpout, chflags,
576 (chflags & CH_PREFIX) ? prefix : NULL) == -1)
579 new_offset = ftello (fpout);
582 if (flags & M_CM_DECODE) {
583 /* now make a text/plain version of the message */
587 if (flags & M_CM_PREFIX)
589 if (flags & M_CM_DISPLAY)
590 s.flags |= M_DISPLAY;
591 if (flags & M_CM_PRINTING)
592 s.flags |= M_PRINTING;
593 if (flags & M_CM_WEED)
595 if (flags & M_CM_CHARCONV)
596 s.flags |= M_CHARCONV;
597 if (flags & M_CM_REPLYING)
598 s.flags |= M_REPLYING;
600 if (flags & M_CM_VERIFY)
603 rc = mutt_body_handler (body, &s);
605 else if ((flags & M_CM_DECODE_CRYPT) && (hdr->security & ENCRYPT)) {
609 if ((flags & M_CM_DECODE_PGP) && (hdr->security & APPLICATION_PGP) &&
610 hdr->content->type == TYPEMULTIPART) {
611 if (crypt_pgp_decrypt_mime (fpin, &fp, hdr->content, &cur))
613 fputs ("MIME-Version: 1.0\n", fpout);
616 if ((flags & M_CM_DECODE_SMIME) && (hdr->security & APPLICATION_SMIME)
617 && hdr->content->type == TYPEAPPLICATION) {
618 if (crypt_smime_decrypt_mime (fpin, &fp, hdr->content, &cur))
622 mutt_write_mime_header (cur, fpout);
625 fseeko (fp, cur->offset, 0);
626 if (mutt_copy_bytes (fp, fpout, cur->length) == -1) {
628 body_list_wipe(&cur);
631 body_list_wipe(&cur);
635 fseeko (fpin, body->offset, 0);
636 if (flags & M_CM_PREFIX) {
638 size_t bytes = body->length;
640 fputs (prefix, fpout);
642 while ((c = fgetc (fpin)) != EOF && bytes--) {
645 fputs (prefix, fpout);
649 else if (mutt_copy_bytes (fpin, fpout, body->length) == -1)
653 if ((flags & M_CM_UPDATE) && (flags & M_CM_NOHEADER) == 0
654 && new_offset != -1) {
655 body->offset = new_offset;
656 body_list_wipe(&body->parts);
663 mutt_copy_message (FILE * fpout, CONTEXT * src, HEADER * hdr, int flags,
669 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
672 _mutt_copy_message (fpout, msg->fp, hdr, hdr->content, flags,
673 chflags)) == 0 && (ferror (fpout)
677 mx_close_message (&msg);
681 /* appends a copy of the given message to a mailbox
683 * dest destination mailbox
684 * fpin where to get input
686 * hdr message being copied
687 * body structure of message being copied
688 * flags mutt_copy_message() flags
689 * chflags mutt_copy_header() flags
693 _mutt_append_message (CONTEXT * dest, FILE * fpin, CONTEXT * src __attribute__ ((unused)),
694 HEADER * hdr, BODY * body, int flags, int chflags) {
699 fseeko(fpin, hdr->offset, 0);
700 if (fgets (buf, sizeof (buf), fpin) == NULL)
702 if ((msg = mx_open_new_message (dest, hdr, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL)
704 if (dest->magic == M_MBOX || dest->magic == M_MMDF)
705 chflags |= CH_FROM | CH_FORCE_FROM;
706 chflags |= (dest->magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
707 r = _mutt_copy_message (msg->fp, fpin, hdr, body, flags, chflags);
708 if (mx_commit_message (msg, dest) != 0)
711 mx_close_message (&msg);
716 mutt_append_message (CONTEXT * dest, CONTEXT * src, HEADER * hdr, int cmflags,
722 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
725 _mutt_append_message (dest, msg->fp, src, hdr, hdr->content, cmflags,
727 mx_close_message (&msg);
732 * This function copies a message body, while deleting _in_the_copy_
733 * any attachments which are marked for deletion.
734 * Nothing is changed in the original message -- this is left to the caller.
736 * The function will return 0 on success and -1 on failure.
738 static int copy_delete_attach (BODY * b, FILE * fpin, FILE * fpout,
743 for (part = b->parts; part; part = part->next) {
744 if (part->deleted || part->parts) {
745 /* Copy till start of this part */
746 if (mutt_copy_bytes (fpin, fpout, part->hdr_offset - ftello (fpin)))
751 "Content-Type: message/external-body; access-type=x-mutt-deleted;\n"
752 "\texpiration=%s; length=%lld\n"
753 "\n", date + 5, part->length);
757 /* Copy the original mime headers */
758 if (mutt_copy_bytes (fpin, fpout, part->offset - ftello (fpin)))
761 /* Skip the deleted body */
762 fseeko (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 - ftello (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_t * a)
789 char buf[HUGE_STRING];
793 int l, linelen, buflen, count;
795 linelen = m_strlen(*h);
796 buflen = linelen + 3;
799 p_realloc(h, buflen);
800 for (count = 0; a; a = a->next, count++) {
801 address_t *tmp = a->next;
804 *buf = *cbuf = *c2buf = '\0';
805 rfc822_write_address (buf, sizeof (buf), a, 0);
809 if (count && linelen + l > 74) {
810 strcpy (cbuf, "\n\t"); /* __STRCPY_CHECKED__ */
815 strcpy (cbuf, " "); /* __STRCPY_CHECKED__ */
820 if (!a->group && a->next && a->next->mailbox) {
823 strcpy (c2buf, ","); /* __STRCPY_CHECKED__ */
826 buflen += l + m_strlen(cbuf) + m_strlen(c2buf);
827 p_realloc(h, buflen);
828 strcat (*h, cbuf); /* __STRCAT_CHECKED__ */
829 strcat (*h, buf); /* __STRCAT_CHECKED__ */
830 strcat (*h, c2buf); /* __STRCAT_CHECKED__ */
833 /* Space for this was allocated in the beginning of this function. */
834 strcat (*h, "\n"); /* __STRCAT_CHECKED__ */
837 static int address_header_decode (char **h)
844 switch (tolower ((unsigned char) *s)) {
847 if (ascii_strncasecmp (s, "return-path:", 12) == 0) {
851 else if (ascii_strncasecmp (s, "reply-to:", 9) == 0) {
859 if (ascii_strncasecmp (s, "from:", 5))
866 if (ascii_strncasecmp (s, "cc:", 3))
874 if (ascii_strncasecmp (s, "bcc:", 4))
881 if (ascii_strncasecmp (s, "sender:", 7))
888 if (ascii_strncasecmp (s, "to:", 3))
895 if (ascii_strncasecmp (s, "mail-followup-to:", 17))
904 if ((a = rfc822_parse_adrlist (a, s + l)) == NULL)
907 mutt_addrlist_to_local (a);
908 rfc2047_decode_adrlist (a);
910 *h = p_dupstr(s, l + 1);
912 format_address_header (h, a);
914 address_list_wipe(&a);