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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-mime/mime.h>
18 #include <lib-crypt/crypt.h>
19 #include <lib-ui/curses.h>
20 #include "mutt_idna.h"
22 static int address_header_decode (char **str);
23 static int copy_delete_attach (BODY * b, FILE * fpin, FILE * fpout,
26 /* Ok, the only reason for not merging this with mutt_copy_header()
27 * below is to avoid creating a HEADER structure in message_handler().
30 mutt_copy_hdr (FILE* in, FILE* out, off_t off_start, off_t off_end,
31 int flags, const char *prefix) {
35 char buf[STRING]; /* should be long enough to get most fields in one pass */
41 char *this_one = NULL;
45 if (ftello (in) != off_start)
46 fseeko (in, off_start, 0);
52 (CH_REORDER | CH_WEED | CH_MIME | CH_DECODE | CH_PREFIX |
53 CH_WEED_DELIVERED)) == 0) {
54 /* Without these flags to complicate things
55 * we can do a more efficient line to line copying
57 while (ftello (in) < off_end) {
58 eol = strchr (buf, '\n');
60 if ((fgets (buf, sizeof (buf), in)) == NULL)
63 /* Is it the begining of a header? */
64 if (eol && buf[0] != ' ' && buf[0] != '\t') {
66 if (!from && m_strncmp("From ", buf, 5) == 0) {
67 if ((flags & CH_FROM) == 0)
71 else if (flags & (CH_NOQFROM) &&
72 ascii_strncasecmp (">From ", buf, 6) == 0)
75 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
76 break; /* end of header */
78 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
79 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
80 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
82 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
83 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
84 ascii_strncasecmp ("Lines:", buf, 6) == 0))
86 if ((flags & CH_UPDATE_REFS) &&
87 ascii_strncasecmp ("References:", buf, 11) == 0)
89 if ((flags & CH_UPDATE_IRT) &&
90 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
95 if (!ignore && fputs (buf, out) == EOF)
105 /* We are going to read and collect the headers in an array
106 * so we are able to do re-ordering.
107 * First count the number of entries in the array
109 if (flags & CH_REORDER) {
110 for (t = HeaderOrderList; t; t = t->next) {
115 headers = p_new(char *, hdr_count);
117 /* Read all the headers into the array */
118 while (ftello (in) < off_end) {
119 eol = strchr (buf, '\n');
122 if ((fgets (buf, sizeof (buf), in)) == NULL)
125 /* Is it the begining of a header? */
126 if (eol && buf[0] != ' ' && buf[0] != '\t') {
128 /* set curline to 1 for To:/Cc:/Bcc: and 0 otherwise */
129 curline = (flags & CH_WEED) && (m_strncmp("To:", buf, 3) == 0 ||
130 m_strncmp("Cc:", buf, 3) == 0 ||
131 m_strncmp("Bcc:", buf, 4) == 0);
133 /* Do we have anything pending? */
135 if (flags & CH_DECODE) {
136 if (!address_header_decode (&this_one))
137 rfc2047_decode (&this_one);
141 headers[x] = this_one;
143 p_realloc(&headers[x], m_strlen(headers[x]) + m_strlen(this_one) + 1);
144 strcat(headers[x], this_one); /* __STRCAT_CHECKED__ */
153 if (!from && m_strncmp("From ", buf, 5) == 0) {
154 if ((flags & CH_FROM) == 0)
156 this_is_from = from = 1;
158 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
159 break; /* end of header */
161 /* note: CH_FROM takes precedence over header weeding. */
162 if (!((flags & CH_FROM) && (flags & CH_FORCE_FROM) && this_is_from) &&
164 mutt_matches_ignore (buf, Ignore) &&
165 !mutt_matches_ignore (buf, UnIgnore))
167 if ((flags & CH_WEED_DELIVERED) &&
168 ascii_strncasecmp ("Delivered-To:", buf, 13) == 0)
170 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
171 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
172 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
174 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
175 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
176 ascii_strncasecmp ("Lines:", buf, 6) == 0))
178 if ((flags & CH_MIME) &&
179 ((ascii_strncasecmp ("content-", buf, 8) == 0 &&
180 (ascii_strncasecmp ("transfer-encoding:", buf + 8, 18) == 0 ||
181 ascii_strncasecmp ("type:", buf + 8, 5) == 0)) ||
182 ascii_strncasecmp ("mime-version:", buf, 13) == 0))
184 if ((flags & CH_UPDATE_REFS) &&
185 ascii_strncasecmp ("References:", buf, 11) == 0)
187 if ((flags & CH_UPDATE_IRT) &&
188 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
191 /* Find x -- the array entry where this header is to be saved */
192 if (flags & CH_REORDER) {
193 for (t = HeaderOrderList, x = 0; (t); t = t->next, x++) {
194 if (!ascii_strncasecmp (buf, t->data, m_strlen(t->data))) {
201 } /* If beginning of header */
205 this_one = m_strdup(buf);
206 /* we do want to see all lines if this header doesn't feature
207 * abbreviations (curline is 0), $max_display_recips is 0 and
208 * while the number hasn't reached $max_display_recips yet */
209 else if (curline == 0 || MaxDispRecips == 0 || ++curline <= MaxDispRecips) {
210 p_realloc(&this_one, m_strlen(this_one) + m_strlen(buf) + 1);
211 strcat (this_one, buf); /* __STRCAT_CHECKED__ */
212 /* only for the first line which doesn't exeeds
213 * $max_display_recips: abbreviate it */
214 } else if (curline == MaxDispRecips+1) {
215 p_realloc(&this_one, m_strlen(this_one) + 5);
216 strcat (this_one, " ...");
219 } /* while (ftello (in) < off_end) */
221 /* Do we have anything pending? -- XXX, same code as in above in the loop. */
223 if (flags & CH_DECODE) {
224 if (!address_header_decode (&this_one))
225 rfc2047_decode (&this_one);
229 headers[x] = this_one;
231 p_realloc(&headers[x], m_strlen(headers[x]) + m_strlen(this_one) + 1);
232 strcat (headers[x], this_one); /* __STRCAT_CHECKED__ */
239 /* Now output the headers in order */
240 for (x = 0; x < hdr_count; x++) {
243 if (flags & CH_DECODE)
244 rfc2047_decode (&headers[x]);
247 /* We couldn't do the prefixing when reading because RFC 2047
248 * decoding may have concatenated lines.
250 if (flags & CH_PREFIX) {
251 char *ch = headers[x];
252 int print_prefix = 1;
256 if (fputs (prefix, out) == EOF) {
263 if (*ch == '\n' && ch[1])
266 if (putc (*ch++, out) == EOF) {
275 if (fputs (headers[x], out) == EOF) {
283 /* Free in a separate loop to be sure that all headers are freed
284 * in case of error. */
285 for (x = 0; x < hdr_count; x++)
286 p_delete(&headers[x]);
295 CH_DECODE RFC2047 header decoding
296 CH_FROM retain the "From " message separator
297 CH_FORCE_FROM give CH_FROM precedence over CH_WEED
298 CH_MIME ignore MIME fields
299 CH_NOLEN don't write Content-Length: and Lines:
300 CH_NONEWLINE don't output a newline after the header
301 CH_NOSTATUS ignore the Status: and X-Status:
302 CH_PREFIX quote header with $indent_str
303 CH_REORDER output header in order specified by `hdr_order'
304 CH_TXTPLAIN generate text/plain MIME headers [hack alert.]
305 CH_UPDATE write new Status: and X-Status:
306 CH_UPDATE_LEN write new Content-Length: and Lines:
307 CH_XMIT ignore Lines: and Content-Length:
308 CH_WEED do header weeding
309 CH_NOQFROM ignore ">From " line
310 CH_UPDATE_IRT update the In-Reply-To: header
311 CH_UPDATE_REFS update the References: header
314 string to use if CH_PREFIX is set
318 mutt_copy_header (FILE * in, HEADER * h, FILE * out, int flags,
321 char buffer[SHORT_STRING];
324 flags |= (h->env->irt_changed ? CH_UPDATE_IRT : 0) |
325 (h->env->refs_changed ? CH_UPDATE_REFS : 0);
327 if (mutt_copy_hdr (in, out, h->offset, h->content->offset, flags, prefix) ==
331 if (flags & CH_TXTPLAIN) {
332 char chsbuf[SHORT_STRING];
334 fputs ("MIME-Version: 1.0\n", out);
335 fputs ("Content-Transfer-Encoding: 8bit\n", out);
336 fputs ("Content-Type: text/plain; charset=", out);
337 charset_canonicalize(chsbuf, sizeof (chsbuf), Charset);
338 rfc822_strcpy(buffer, sizeof(buffer), chsbuf, MimeSpecials);
342 if (ferror (out) != 0 || feof (out) != 0)
347 if (flags & CH_UPDATE) {
348 if ((flags & CH_NOSTATUS) == 0) {
349 if (h->env->irt_changed && h->env->in_reply_to) {
350 string_list_t *listp = h->env->in_reply_to;
352 if (fputs ("In-Reply-To: ", out) == EOF)
355 for (; listp; listp = listp->next)
356 if ((fputs (listp->data, out) == EOF) || (fputc (' ', out) == EOF))
359 if (fputc ('\n', out) == EOF)
363 if (h->env->refs_changed && h->env->references) {
364 string_list_t *listp = h->env->references, *refs = NULL, *t;
366 if (fputs ("References: ", out) == EOF)
369 /* Mutt stores references in reverse order, thus we create
370 * a reordered refs list that we can put in the headers */
371 for (; listp; listp = listp->next, refs = t) {
372 t = p_new(string_list_t, 1);
373 t->data = listp->data;
377 for (; refs; refs = refs->next)
378 if ((fputs (refs->data, out) == EOF) || (fputc (' ', out) == EOF))
381 /* clearing refs from memory */
382 for (t = refs; refs; refs = t->next, t = refs)
385 if (fputc ('\n', out) == EOF)
389 if (h->old || h->read) {
390 if (fputs ("Status: ", out) == EOF)
394 if (fputs ("RO", out) == EOF)
398 if (fputc ('O', out) == EOF)
402 if (fputc ('\n', out) == EOF)
406 if (h->flagged || h->replied) {
407 if (fputs ("X-Status: ", out) == EOF)
411 if (fputc ('A', out) == EOF)
416 if (fputc ('F', out) == EOF)
420 if (fputc ('\n', out) == EOF)
426 if (flags & CH_UPDATE_LEN && (flags & CH_NOLEN) == 0) {
427 fprintf (out, "Content-Length: %lld\n", h->content->length);
428 if (h->lines != 0 || h->content->length == 0)
429 fprintf (out, "Lines: %d\n", h->lines);
432 if ((flags & CH_NONEWLINE) == 0) {
433 if (flags & CH_PREFIX)
435 if (fputc ('\n', out) == EOF) /* add header terminator */
439 if (ferror (out) || feof (out))
445 /* Count the number of lines and bytes to be deleted in this body*/
446 static int count_delete_lines (FILE * fp, BODY * b, off_t *length,
454 fseeko (fp, b->offset, SEEK_SET);
455 for (l = b->length; l; l--) {
463 *length -= b->length - (84 + datelen);
464 /* Count the number of digits exceeding the first one to write the size */
465 for (l = 10; b->length >= l; l *= 10)
469 for (b = b->parts; b; b = b->next)
470 dellines += count_delete_lines (fp, b, length, datelen);
475 /* make a copy of a message
477 * fpout where to write output
478 * fpin where to get input
479 * hdr header of message being copied
480 * body structure of message being copied
482 * M_CM_NOHEADER don't copy header
483 * M_CM_PREFIX quote header and body
484 * M_CM_DECODE decode message body to text/plain
485 * M_CM_DISPLAY displaying output to the user
486 * M_CM_PRINTING printing the message
487 * M_CM_UPDATE update structures in memory after syncing
488 * M_CM_DECODE_PGP used for decoding PGP messages
489 * M_CM_CHARCONV perform character set conversion
490 * chflags flags to mutt_copy_header()
494 _mutt_copy_message (FILE * fpout, FILE * fpin, HEADER * hdr, BODY * body,
495 int flags, int chflags)
497 char prefix[SHORT_STRING];
499 off_t new_offset = -1;
502 if (flags & M_CM_PREFIX) {
503 if (option (OPTTEXTFLOWED))
504 m_strcpy(prefix, sizeof(prefix), ">");
506 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
510 if ((flags & M_CM_NOHEADER) == 0) {
511 if (flags & M_CM_PREFIX)
512 chflags |= CH_PREFIX;
514 else if (hdr->attach_del && (chflags & CH_UPDATE_LEN)) {
516 off_t new_length = body->length;
517 char date[SHORT_STRING];
519 mutt_make_date (date, sizeof (date));
520 date[5] = date[m_strlen(date) - 1] = '\"';
522 /* Count the number of lines and bytes to be deleted */
523 fseeko (fpin, body->offset, SEEK_SET);
524 new_lines = hdr->lines -
525 count_delete_lines (fpin, body, &new_length, m_strlen(date));
527 /* Copy the headers */
528 if (mutt_copy_header (fpin, hdr, fpout,
529 chflags | CH_NOLEN | CH_NONEWLINE, NULL))
531 fprintf (fpout, "Content-Length: %lld\n", new_length);
535 fprintf (fpout, "Lines: %d\n\n", new_lines);
536 if (ferror (fpout) || feof (fpout))
538 new_offset = ftello (fpout);
541 fseeko (fpin, body->offset, SEEK_SET);
542 if (copy_delete_attach (body, fpin, fpout, date))
545 /* Update original message if we are sync'ing a mailfolder */
546 if (flags & M_CM_UPDATE) {
548 hdr->lines = new_lines;
549 body->offset = new_offset;
551 /* update the total size of the mailbox to reflect this deletion */
552 Context->size -= body->length - new_length;
554 * if the message is visible, update the visible size of the mailbox
557 if (Context->v2r[hdr->msgno] != -1)
558 Context->vsize -= body->length - new_length;
560 body->length = new_length;
561 body_list_wipe(&body->parts);
567 if (mutt_copy_header (fpin, hdr, fpout, chflags,
568 (chflags & CH_PREFIX) ? prefix : NULL) == -1)
571 new_offset = ftello (fpout);
574 if (flags & M_CM_DECODE) {
575 /* now make a text/plain version of the message */
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;
589 if (flags & M_CM_REPLYING)
590 s.flags |= M_REPLYING;
592 if (flags & M_CM_VERIFY)
595 rc = mutt_body_handler (body, &s);
597 else if ((flags & M_CM_DECODE_CRYPT) && (hdr->security & ENCRYPT)) {
601 if ((flags & M_CM_DECODE_PGP) && (hdr->security & APPLICATION_PGP) &&
602 hdr->content->type == TYPEMULTIPART) {
603 if (crypt_pgp_decrypt_mime (fpin, &fp, hdr->content, &cur))
605 fputs ("MIME-Version: 1.0\n", fpout);
608 if ((flags & M_CM_DECODE_SMIME) && (hdr->security & APPLICATION_SMIME)
609 && hdr->content->type == TYPEAPPLICATION) {
610 if (crypt_smime_decrypt_mime (fpin, &fp, hdr->content, &cur))
614 mutt_write_mime_header (cur, fpout);
617 fseeko (fp, cur->offset, 0);
618 if (mutt_copy_bytes (fp, fpout, cur->length) == -1) {
620 body_list_wipe(&cur);
623 body_list_wipe(&cur);
627 fseeko (fpin, body->offset, 0);
628 if (flags & M_CM_PREFIX) {
630 size_t bytes = body->length;
632 fputs (prefix, fpout);
634 while ((c = fgetc (fpin)) != EOF && bytes--) {
637 fputs (prefix, fpout);
641 else if (mutt_copy_bytes (fpin, fpout, body->length) == -1)
645 if ((flags & M_CM_UPDATE) && (flags & M_CM_NOHEADER) == 0
646 && new_offset != -1) {
647 body->offset = new_offset;
648 body_list_wipe(&body->parts);
655 mutt_copy_message (FILE * fpout, CONTEXT * src, HEADER * hdr, int flags,
661 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
664 _mutt_copy_message (fpout, msg->fp, hdr, hdr->content, flags,
665 chflags)) == 0 && (ferror (fpout)
669 mx_close_message (&msg);
673 /* appends a copy of the given message to a mailbox
675 * dest destination mailbox
676 * fpin where to get input
678 * hdr message being copied
679 * body structure of message being copied
680 * flags mutt_copy_message() flags
681 * chflags mutt_copy_header() flags
685 _mutt_append_message (CONTEXT * dest, FILE * fpin, CONTEXT * src __attribute__ ((unused)),
686 HEADER * hdr, BODY * body, int flags, int chflags) {
691 fseeko(fpin, hdr->offset, 0);
692 if (fgets (buf, sizeof (buf), fpin) == NULL)
694 if ((msg = mx_open_new_message (dest, hdr, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL)
696 if (dest->magic == M_MBOX || dest->magic == M_MMDF)
697 chflags |= CH_FROM | CH_FORCE_FROM;
698 chflags |= (dest->magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
699 r = _mutt_copy_message (msg->fp, fpin, hdr, body, flags, chflags);
700 if (mx_commit_message (msg, dest) != 0)
703 mx_close_message (&msg);
708 mutt_append_message (CONTEXT * dest, CONTEXT * src, HEADER * hdr, int cmflags,
714 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
717 _mutt_append_message (dest, msg->fp, src, hdr, hdr->content, cmflags,
719 mx_close_message (&msg);
724 * This function copies a message body, while deleting _in_the_copy_
725 * any attachments which are marked for deletion.
726 * Nothing is changed in the original message -- this is left to the caller.
728 * The function will return 0 on success and -1 on failure.
730 static int copy_delete_attach (BODY * b, FILE * fpin, FILE * fpout,
735 for (part = b->parts; part; part = part->next) {
736 if (part->deleted || part->parts) {
737 /* Copy till start of this part */
738 if (mutt_copy_bytes (fpin, fpout, part->hdr_offset - ftello (fpin)))
743 "Content-Type: message/external-body; access-type=x-mutt-deleted;\n"
744 "\texpiration=%s; length=%lld\n"
745 "\n", date + 5, part->length);
749 /* Copy the original mime headers */
750 if (mutt_copy_bytes (fpin, fpout, part->offset - ftello (fpin)))
753 /* Skip the deleted body */
754 fseeko (fpin, part->offset + part->length, SEEK_SET);
757 if (copy_delete_attach (part, fpin, fpout, date))
763 /* Copy the last parts */
764 if (mutt_copy_bytes (fpin, fpout, b->offset + b->length - ftello (fpin)))
771 * This function is the equivalent of mutt_write_address_list(),
772 * but writes to a buffer instead of writing to a stream.
773 * mutt_write_address_list could be re-used if we wouldn't store
774 * all the decoded headers in a huge array, first.
779 static void format_address_header (char **h, address_t * a)
781 char buf[HUGE_STRING];
785 int l, linelen, buflen, count;
787 linelen = m_strlen(*h);
788 buflen = linelen + 3;
791 p_realloc(h, buflen);
792 for (count = 0; a; a = a->next, count++) {
793 address_t *tmp = a->next;
796 *buf = *cbuf = *c2buf = '\0';
797 rfc822_write_address (buf, sizeof (buf), a, 0);
801 if (count && linelen + l > 74) {
802 strcpy (cbuf, "\n\t"); /* __STRCPY_CHECKED__ */
807 strcpy (cbuf, " "); /* __STRCPY_CHECKED__ */
812 if (!a->group && a->next && a->next->mailbox) {
815 strcpy (c2buf, ","); /* __STRCPY_CHECKED__ */
818 buflen += l + m_strlen(cbuf) + m_strlen(c2buf);
819 p_realloc(h, buflen);
820 strcat (*h, cbuf); /* __STRCAT_CHECKED__ */
821 strcat (*h, buf); /* __STRCAT_CHECKED__ */
822 strcat (*h, c2buf); /* __STRCAT_CHECKED__ */
825 /* Space for this was allocated in the beginning of this function. */
826 strcat (*h, "\n"); /* __STRCAT_CHECKED__ */
829 static int address_header_decode (char **h)
836 switch (tolower ((unsigned char) *s)) {
839 if (ascii_strncasecmp (s, "return-path:", 12) == 0) {
843 else if (ascii_strncasecmp (s, "reply-to:", 9) == 0) {
851 if (ascii_strncasecmp (s, "from:", 5))
858 if (ascii_strncasecmp (s, "cc:", 3))
866 if (ascii_strncasecmp (s, "bcc:", 4))
873 if (ascii_strncasecmp (s, "sender:", 7))
880 if (ascii_strncasecmp (s, "to:", 3))
887 if (ascii_strncasecmp (s, "mail-followup-to:", 17))
896 if ((a = rfc822_parse_adrlist (a, s + l)) == NULL)
899 mutt_addrlist_to_local (a);
900 rfc2047_decode_adrlist (a);
902 *h = p_dupstr(s, l + 1);
904 format_address_header (h, a);
906 address_list_wipe(&a);