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>
13 #include <lib-ui/curses.h>
14 #include <lib-mx/mx.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;
42 ssize_t this_one_len = 0, headers_len = 0;
46 if (ftello (in) != off_start)
47 fseeko (in, off_start, 0);
53 (CH_REORDER | CH_WEED | CH_MIME | CH_DECODE | CH_PREFIX |
54 CH_WEED_DELIVERED)) == 0) {
55 /* Without these flags to complicate things
56 * we can do a more efficient line to line copying
58 while (ftello (in) < off_end) {
59 eol = strchr (buf, '\n');
61 if ((fgets (buf, sizeof (buf), in)) == NULL)
64 /* Is it the begining of a header? */
65 if (eol && buf[0] != ' ' && buf[0] != '\t') {
67 if (!from && m_strncmp("From ", buf, 5) == 0) {
68 if ((flags & CH_FROM) == 0)
72 else if (flags & (CH_NOQFROM) &&
73 ascii_strncasecmp (">From ", buf, 6) == 0)
76 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
77 break; /* end of header */
79 switch (mime_which_token(buf, (strchr(buf, ':') ?: buf) - buf)) {
82 if (flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS))
86 case MIME_CONTENT_LENGTH:
88 if (flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN))
93 if (flags & CH_UPDATE_REFS)
97 case MIME_IN_REPLY_TO:
98 if (flags & CH_UPDATE_IRT)
108 if (!ignore && fputs (buf, out) == EOF)
118 /* We are going to read and collect the headers in an array
119 * so we are able to do re-ordering.
120 * First count the number of entries in the array
122 if (flags & CH_REORDER) {
123 for (t = HeaderOrderList; t; t = t->next) {
128 headers = p_new(char *, hdr_count);
130 /* Read all the headers into the array */
131 while (ftello (in) < off_end) {
132 eol = strchr (buf, '\n');
135 if ((fgets (buf, sizeof (buf), in)) == NULL)
138 /* Is it the begining of a header? */
139 if (eol && buf[0] != ' ' && buf[0] != '\t') {
141 /* set curline to 1 for To:/Cc:/Bcc: and 0 otherwise */
142 curline = (flags & CH_WEED) && (m_strncmp("To:", buf, 3) == 0 ||
143 m_strncmp("Cc:", buf, 3) == 0 ||
144 m_strncmp("Bcc:", buf, 4) == 0);
146 /* Do we have anything pending? */
148 if (flags & CH_DECODE) {
149 if (!address_header_decode (&this_one))
150 rfc2047_decode (&this_one);
154 headers[x] = this_one;
156 headers_len = m_strlen(headers[x]) + m_strlen(this_one) + 1;
157 p_realloc(&headers[x], headers_len);
158 m_strcat(headers[x], headers_len, this_one);
167 if (!from && m_strncmp("From ", buf, 5) == 0) {
168 if ((flags & CH_FROM) == 0)
170 this_is_from = from = 1;
172 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
173 break; /* end of header */
175 /* note: CH_FROM takes precedence over header weeding. */
176 if (!((flags & CH_FROM) && (flags & CH_FORCE_FROM) && this_is_from) &&
177 (flags & CH_WEED) && string_list_contains(Ignore, buf, "*")
178 && !string_list_contains(UnIgnore, buf, "*"))
181 switch (mime_which_token(buf, (strchr(buf, ':') ?: buf) - buf)) {
182 case MIME_DELIVERED_TO:
183 if (flags & CH_WEED_DELIVERED)
189 if (flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS))
193 case MIME_CONTENT_LENGTH:
195 if (flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN))
199 case MIME_CONTENT_TRANSFER_ENCODING:
200 case MIME_CONTENT_TYPE:
201 case MIME_MIME_VERSION:
206 case MIME_REFERENCES:
207 if (flags & CH_UPDATE_REFS)
211 case MIME_IN_REPLY_TO:
212 if (flags & CH_UPDATE_IRT)
220 /* Find x -- the array entry where this header is to be saved */
221 if (flags & CH_REORDER) {
222 for (t = HeaderOrderList, x = 0; (t); t = t->next, x++) {
223 if (!ascii_strncasecmp (buf, t->data, m_strlen(t->data))) {
230 } /* If beginning of header */
234 this_one = m_strdup(buf);
235 /* we do want to see all lines if this header doesn't feature
236 * abbreviations (curline is 0), $max_display_recips is 0 and
237 * while the number hasn't reached $max_display_recips yet */
238 else if (curline == 0 || MaxDispRecips == 0 || ++curline <= MaxDispRecips) {
239 this_one_len = m_strlen(this_one) + m_strlen(buf) + 1;
240 p_realloc(&this_one, this_one_len);
241 m_strcat(this_one, this_one_len, buf);
242 /* only for the first line which doesn't exeeds
243 * $max_display_recips: abbreviate it */
244 } else if (curline == MaxDispRecips+1) {
245 this_one_len = m_strlen(this_one) + 5;
246 p_realloc(&this_one, this_one_len);
247 m_strcat(this_one, this_one_len, " ...");
250 } /* while (ftello (in) < off_end) */
252 /* Do we have anything pending? -- XXX, same code as in above in the loop. */
254 if (flags & CH_DECODE) {
255 if (!address_header_decode (&this_one))
256 rfc2047_decode (&this_one);
260 headers[x] = this_one;
262 headers_len = m_strlen(headers[x]) + m_strlen(this_one) + 1;
263 p_realloc(&headers[x], headers_len);
264 m_strcat(headers[x], headers_len, this_one);
271 /* Now output the headers in order */
272 for (x = 0; x < hdr_count; x++) {
274 /* We couldn't do the prefixing when reading because RFC 2047
275 * decoding may have concatenated lines.
277 if (flags & CH_PREFIX) {
278 char *ch = headers[x];
279 int print_prefix = 1;
283 if (fputs (prefix, out) == EOF) {
290 if (*ch == '\n' && ch[1])
293 if (putc (*ch++, out) == EOF) {
302 if (fputs (headers[x], out) == EOF) {
310 /* Free in a separate loop to be sure that all headers are freed
311 * in case of error. */
312 for (x = 0; x < hdr_count; x++)
313 p_delete(&headers[x]);
322 CH_DECODE RFC2047 header decoding
323 CH_FROM retain the "From " message separator
324 CH_FORCE_FROM give CH_FROM precedence over CH_WEED
325 CH_MIME ignore MIME fields
326 CH_NOLEN don't write Content-Length: and Lines:
327 CH_NONEWLINE don't output a newline after the header
328 CH_NOSTATUS ignore the Status: and X-Status:
329 CH_PREFIX quote header with $indent_str
330 CH_REORDER output header in order specified by `hdr_order'
331 CH_TXTPLAIN generate text/plain MIME headers [hack alert.]
332 CH_UPDATE write new Status: and X-Status:
333 CH_UPDATE_LEN write new Content-Length: and Lines:
334 CH_XMIT ignore Lines: and Content-Length:
335 CH_WEED do header weeding
336 CH_NOQFROM ignore ">From " line
337 CH_UPDATE_IRT update the In-Reply-To: header
338 CH_UPDATE_REFS update the References: header
341 string to use if CH_PREFIX is set
345 mutt_copy_header (FILE * in, HEADER * h, FILE * out, int flags,
351 flags |= (h->env->irt_changed ? CH_UPDATE_IRT : 0) |
352 (h->env->refs_changed ? CH_UPDATE_REFS : 0);
354 if (mutt_copy_hdr (in, out, h->offset, h->content->offset, flags, prefix) ==
358 if (flags & CH_TXTPLAIN) {
361 fputs ("MIME-Version: 1.0\n", out);
362 fputs ("Content-Transfer-Encoding: 8bit\n", out);
363 fputs ("Content-Type: text/plain; charset=", out);
364 charset_canonicalize(chsbuf, sizeof (chsbuf), MCharset.charset);
365 rfc822_strcpy(buffer, sizeof(buffer), chsbuf, MimeSpecials);
369 if (ferror (out) != 0 || feof (out) != 0)
374 if (flags & CH_UPDATE) {
375 if ((flags & CH_NOSTATUS) == 0) {
376 if (h->env->irt_changed && h->env->in_reply_to) {
377 string_list_t *listp = h->env->in_reply_to;
379 if (fputs ("In-Reply-To: ", out) == EOF)
382 for (; listp; listp = listp->next)
383 if ((fputs (listp->data, out) == EOF) || (fputc (' ', out) == EOF))
386 if (fputc ('\n', out) == EOF)
390 if (h->env->refs_changed && h->env->references) {
391 string_list_t *listp = h->env->references, *refs = NULL, *t;
393 if (fputs ("References: ", out) == EOF)
396 /* Mutt stores references in reverse order, thus we create
397 * a reordered refs list that we can put in the headers */
398 for (; listp; listp = listp->next, refs = t) {
399 t = p_new(string_list_t, 1);
400 t->data = listp->data;
404 for (; refs; refs = refs->next)
405 if ((fputs (refs->data, out) == EOF) || (fputc (' ', out) == EOF))
408 /* clearing refs from memory */
409 for (t = refs; refs; refs = t->next, t = refs)
412 if (fputc ('\n', out) == EOF)
416 if (h->old || h->read) {
417 if (fputs ("Status: ", out) == EOF)
421 if (fputs ("RO", out) == EOF)
425 if (fputc ('O', out) == EOF)
429 if (fputc ('\n', out) == EOF)
433 if (h->flagged || h->replied) {
434 if (fputs ("X-Status: ", out) == EOF)
438 if (fputc ('A', out) == EOF)
443 if (fputc ('F', out) == EOF)
447 if (fputc ('\n', out) == EOF)
453 if (flags & CH_UPDATE_LEN && (flags & CH_NOLEN) == 0) {
454 fprintf (out, "Content-Length: %zu\n", h->content->length);
455 if (h->lines != 0 || h->content->length == 0)
456 fprintf (out, "Lines: %d\n", h->lines);
459 if ((flags & CH_NONEWLINE) == 0) {
460 if (flags & CH_PREFIX)
462 if (fputc ('\n', out) == EOF) /* add header terminator */
466 if (ferror (out) || feof (out))
472 /* Count the number of lines and bytes to be deleted in this body*/
473 static int count_delete_lines (FILE * fp, BODY * b, off_t *length,
481 fseeko (fp, b->offset, SEEK_SET);
482 for (l = b->length; l; l--) {
490 *length -= b->length - (84 + datelen);
491 /* Count the number of digits exceeding the first one to write the size */
492 for (l = 10; b->length >= l; l *= 10)
496 for (b = b->parts; b; b = b->next)
497 dellines += count_delete_lines (fp, b, length, datelen);
502 /* make a copy of a message
504 * fpout where to write output
505 * fpin where to get input
506 * hdr header of message being copied
507 * body structure of message being copied
509 * M_CM_NOHEADER don't copy header
510 * M_CM_PREFIX quote header and body
511 * M_CM_DECODE decode message body to text/plain
512 * M_CM_DISPLAY displaying output to the user
513 * M_CM_PRINTING printing the message
514 * M_CM_UPDATE update structures in memory after syncing
515 * M_CM_DECODE_PGP used for decoding PGP messages
516 * M_CM_CHARCONV perform character set conversion
517 * chflags flags to mutt_copy_header()
521 _mutt_copy_message (FILE * fpout, FILE * fpin, HEADER * hdr, BODY * body,
522 int flags, int chflags)
526 off_t new_offset = -1;
529 if (flags & M_CM_PREFIX) {
530 if (option (OPTTEXTFLOWED))
531 m_strcpy(prefix, sizeof(prefix), ">");
533 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
537 if ((flags & M_CM_NOHEADER) == 0) {
538 if (flags & M_CM_PREFIX)
539 chflags |= CH_PREFIX;
541 else if (hdr->attach_del && (chflags & CH_UPDATE_LEN)) {
543 off_t new_length = body->length;
546 mutt_make_date (date, sizeof (date));
547 date[5] = date[m_strlen(date) - 1] = '\"';
549 /* Count the number of lines and bytes to be deleted */
550 fseeko (fpin, body->offset, SEEK_SET);
551 new_lines = hdr->lines -
552 count_delete_lines (fpin, body, &new_length, m_strlen(date));
554 /* Copy the headers */
555 if (mutt_copy_header (fpin, hdr, fpout,
556 chflags | CH_NOLEN | CH_NONEWLINE, NULL))
558 fprintf (fpout, "Content-Length: %zu\n", new_length);
562 fprintf (fpout, "Lines: %d\n\n", new_lines);
563 if (ferror (fpout) || feof (fpout))
565 new_offset = ftello (fpout);
568 fseeko (fpin, body->offset, SEEK_SET);
569 if (copy_delete_attach (body, fpin, fpout, date))
572 /* Update original message if we are sync'ing a mailfolder */
573 if (flags & M_CM_UPDATE) {
575 hdr->lines = new_lines;
576 body->offset = new_offset;
578 /* update the total size of the mailbox to reflect this deletion */
579 Context->size -= body->length - new_length;
581 * if the message is visible, update the visible size of the mailbox
584 if (Context->v2r[hdr->msgno] != -1)
585 Context->vsize -= body->length - new_length;
587 body->length = new_length;
588 body_list_wipe(&body->parts);
594 if (mutt_copy_header (fpin, hdr, fpout, chflags,
595 (chflags & CH_PREFIX) ? prefix : NULL) == -1)
598 new_offset = ftello (fpout);
601 if (flags & M_CM_DECODE) {
602 /* now make a text/plain version of the message */
606 if (flags & M_CM_PREFIX)
608 if (flags & M_CM_DISPLAY)
609 s.flags |= M_DISPLAY;
610 if (flags & M_CM_PRINTING)
611 s.flags |= M_PRINTING;
612 if (flags & M_CM_WEED)
614 if (flags & M_CM_CHARCONV)
615 s.flags |= M_CHARCONV;
616 if (flags & M_CM_REPLYING)
617 s.flags |= M_REPLYING;
619 if (flags & M_CM_VERIFY)
622 rc = mutt_body_handler (body, &s);
624 else if ((flags & M_CM_DECODE_CRYPT) && (hdr->security & ENCRYPT)) {
628 if ((flags & M_CM_DECODE_PGP) && (hdr->security & APPLICATION_PGP) &&
629 hdr->content->type == TYPEMULTIPART) {
630 if (crypt_pgp_decrypt_mime (fpin, &fp, hdr->content, &cur))
632 fputs ("MIME-Version: 1.0\n", fpout);
635 if ((flags & M_CM_DECODE_SMIME) && (hdr->security & APPLICATION_SMIME)
636 && hdr->content->type == TYPEAPPLICATION) {
637 if (crypt_smime_decrypt_mime (fpin, &fp, hdr->content, &cur))
641 mutt_write_mime_header (cur, fpout);
644 fseeko (fp, cur->offset, 0);
645 if (mutt_copy_bytes (fp, fpout, cur->length) == -1) {
647 body_list_wipe(&cur);
650 body_list_wipe(&cur);
654 fseeko (fpin, body->offset, 0);
655 if (flags & M_CM_PREFIX) {
657 size_t bytes = body->length;
659 fputs (prefix, fpout);
661 while ((c = fgetc (fpin)) != EOF && bytes--) {
664 fputs (prefix, fpout);
668 else if (mutt_copy_bytes (fpin, fpout, body->length) == -1)
672 if ((flags & M_CM_UPDATE) && (flags & M_CM_NOHEADER) == 0
673 && new_offset != -1) {
674 body->offset = new_offset;
675 body_list_wipe(&body->parts);
682 mutt_copy_message (FILE * fpout, CONTEXT * src, HEADER * hdr, int flags,
688 if (!(msg = mx_open_message (src, hdr->msgno)))
691 r = _mutt_copy_message(fpout, msg->fp, hdr, hdr->content, flags,
693 if (!r && (ferror(fpout) || feof (fpout))) {
696 mx_close_message (&msg);
700 /* appends a copy of the given message to a mailbox
702 * dest destination mailbox
703 * fpin where to get input
705 * hdr message being copied
706 * body structure of message being copied
707 * flags mutt_copy_message() flags
708 * chflags mutt_copy_header() flags
712 _mutt_append_message (CONTEXT * dest, FILE * fpin, CONTEXT * src __attribute__ ((unused)),
713 HEADER * hdr, BODY * body, int flags, int chflags) {
718 fseeko(fpin, hdr->offset, 0);
719 if (fgets (buf, sizeof (buf), fpin) == NULL)
721 if ((msg = mx_open_new_message (dest, hdr, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL)
723 if (dest->magic == M_MBOX || dest->magic == M_MMDF)
724 chflags |= CH_FROM | CH_FORCE_FROM;
725 chflags |= (dest->magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
726 r = _mutt_copy_message (msg->fp, fpin, hdr, body, flags, chflags);
727 if (mx_commit_message (msg, dest) != 0)
730 mx_close_message (&msg);
735 mutt_append_message (CONTEXT * dest, CONTEXT * src, HEADER * hdr, int cmflags,
741 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
744 _mutt_append_message (dest, msg->fp, src, hdr, hdr->content, cmflags,
746 mx_close_message (&msg);
751 * This function copies a message body, while deleting _in_the_copy_
752 * any attachments which are marked for deletion.
753 * Nothing is changed in the original message -- this is left to the caller.
755 * The function will return 0 on success and -1 on failure.
757 static int copy_delete_attach (BODY * b, FILE * fpin, FILE * fpout,
762 for (part = b->parts; part; part = part->next) {
763 if (part->deleted || part->parts) {
764 /* Copy till start of this part */
765 if (mutt_copy_bytes (fpin, fpout, part->hdr_offset - ftello (fpin)))
770 "Content-Type: message/external-body; access-type=x-mutt-deleted;\n"
771 "\texpiration=%s; length=%zu\n"
772 "\n", date + 5, part->length);
776 /* Copy the original mime headers */
777 if (mutt_copy_bytes (fpin, fpout, part->offset - ftello (fpin)))
780 /* Skip the deleted body */
781 fseeko (fpin, part->offset + part->length, SEEK_SET);
784 if (copy_delete_attach (part, fpin, fpout, date))
790 /* Copy the last parts */
791 if (mutt_copy_bytes (fpin, fpout, b->offset + b->length - ftello (fpin)))
798 * This function is the equivalent of mutt_write_address_list(),
799 * but writes to a buffer instead of writing to a stream.
800 * mutt_write_address_list could be re-used if we wouldn't store
801 * all the decoded headers in a huge array, first.
806 static void format_address_header (char **h, address_t * a)
808 char buf[HUGE_STRING];
812 int l, linelen, buflen, count;
814 linelen = m_strlen(*h);
815 buflen = linelen + 3;
818 p_realloc(h, buflen);
819 for (count = 0; a; a = a->next, count++) {
820 address_t *tmp = a->next;
823 *buf = *cbuf = *c2buf = '\0';
824 rfc822_addrcat(buf, sizeof (buf), a, 0);
828 if (count && linelen + l > 74) {
829 m_strcpy(cbuf, sizeof(cbuf), "\n\t");
834 m_strcpy(cbuf, sizeof(cbuf), " ");
839 if (!a->group && a->next && a->next->mailbox) {
842 m_strcpy(c2buf, sizeof(c2buf), ",");
845 buflen += l + m_strlen(cbuf) + m_strlen(c2buf);
846 p_realloc(h, buflen);
847 m_strcat(*h, buflen, cbuf);
848 m_strcat(*h, buflen, buf);
849 m_strcat(*h, buflen, c2buf);
852 /* Space for this was allocated in the beginning of this function. */
853 m_strcat(*h, buflen, "\n");
856 static int address_header_decode(char **h)
865 switch (mime_which_token(s, p - s)) {
866 case MIME_RETURN_PATH:
873 case MIME_MAIL_FOLLOWUP_TO:
881 a = rfc822_parse_adrlist(NULL, p);
885 mutt_addrlist_to_local(a);
886 rfc2047_decode_adrlist(a);
888 *h = p_dupstr(s, p - s);
889 format_address_header(h, a);
890 address_list_wipe(&a);