2 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include "mutt_crypt.h"
30 #include "mutt_idna.h"
31 #include "mutt_curses.h"
36 #include <unistd.h> /* needed for SEEK_SET under SunOS 4.1.4 */
38 static int address_header_decode (char **str);
39 static int copy_delete_attach (BODY *b, FILE *fpin, FILE *fpout, char *date);
41 /* Ok, the only reason for not merging this with mutt_copy_header()
42 * below is to avoid creating a HEADER structure in message_handler().
43 * Also, this one will wrap headers much more aggressively than the other one.
46 mutt_copy_hdr (FILE *in, FILE *out, LOFF_T off_start, LOFF_T off_end, int flags,
52 char buf[LONG_STRING]; /* should be long enough to get most fields in one pass */
58 char *this_one = NULL;
59 size_t this_one_len = 0;
62 if (ftello (in) != off_start)
63 fseeko (in, off_start, 0);
68 if ((flags & (CH_REORDER | CH_WEED | CH_MIME | CH_DECODE | CH_PREFIX | CH_WEED_DELIVERED)) == 0)
70 /* Without these flags to complicate things
71 * we can do a more efficient line to line copying
73 while (ftello (in) < off_end)
75 nl = strchr (buf, '\n');
77 if ((fgets (buf, sizeof (buf), in)) == NULL)
80 /* Is it the begining of a header? */
81 if (nl && buf[0] != ' ' && buf[0] != '\t')
84 if (!from && mutt_strncmp ("From ", buf, 5) == 0)
86 if ((flags & CH_FROM) == 0)
90 else if (flags & (CH_NOQFROM) &&
91 ascii_strncasecmp (">From ", buf, 6) == 0)
94 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
95 break; /* end of header */
97 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
98 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
99 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
101 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
102 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
103 ascii_strncasecmp ("Lines:", buf, 6) == 0))
105 if ((flags & CH_UPDATE_REFS) &&
106 ascii_strncasecmp ("References:", buf, 11) == 0)
108 if ((flags & CH_UPDATE_IRT) &&
109 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
114 if (!ignore && fputs (buf, out) == EOF)
124 /* We are going to read and collect the headers in an array
125 * so we are able to do re-ordering.
126 * First count the number of entries in the array
128 if (flags & CH_REORDER)
130 for (t = HeaderOrderList; t; t = t->next)
132 dprint(3, (debugfile, "Reorder list: %s\n", t->data));
137 dprint (1, (debugfile, "WEED is %s\n", (flags & CH_WEED) ? "Set" : "Not"));
139 headers = safe_calloc (hdr_count, sizeof (char *));
141 /* Read all the headers into the array */
142 while (ftello (in) < off_end)
144 nl = strchr (buf, '\n');
147 if ((fgets (buf, sizeof (buf), in)) == NULL)
150 /* Is it the begining of a header? */
151 if (nl && buf[0] != ' ' && buf[0] != '\t')
153 /* Do we have anything pending? */
156 if (flags & CH_DECODE)
158 if (!address_header_decode (&this_one))
159 rfc2047_decode (&this_one);
160 this_one_len = mutt_strlen (this_one);
164 headers[x] = this_one;
167 int hlen = mutt_strlen (headers[x]);
169 safe_realloc (&headers[x], hlen + this_one_len + sizeof (char));
170 strcat (headers[x] + hlen, this_one); /* __STRCAT_CHECKED__ */
179 if (!from && mutt_strncmp ("From ", buf, 5) == 0)
181 if ((flags & CH_FROM) == 0)
183 this_is_from = from = 1;
185 else if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
186 break; /* end of header */
188 /* note: CH_FROM takes precedence over header weeding. */
189 if (!((flags & CH_FROM) && (flags & CH_FORCE_FROM) && this_is_from) &&
191 mutt_matches_ignore (buf, Ignore) &&
192 !mutt_matches_ignore (buf, UnIgnore))
194 if ((flags & CH_WEED_DELIVERED) &&
195 ascii_strncasecmp ("Delivered-To:", buf, 13) == 0)
197 if ((flags & (CH_UPDATE | CH_XMIT | CH_NOSTATUS)) &&
198 (ascii_strncasecmp ("Status:", buf, 7) == 0 ||
199 ascii_strncasecmp ("X-Status:", buf, 9) == 0))
201 if ((flags & (CH_UPDATE_LEN | CH_XMIT | CH_NOLEN)) &&
202 (ascii_strncasecmp ("Content-Length:", buf, 15) == 0 ||
203 ascii_strncasecmp ("Lines:", buf, 6) == 0))
205 if ((flags & CH_MIME) &&
206 ((ascii_strncasecmp ("content-", buf, 8) == 0 &&
207 (ascii_strncasecmp ("transfer-encoding:", buf + 8, 18) == 0 ||
208 ascii_strncasecmp ("type:", buf + 8, 5) == 0)) ||
209 ascii_strncasecmp ("mime-version:", buf, 13) == 0))
211 if ((flags & CH_UPDATE_REFS) &&
212 ascii_strncasecmp ("References:", buf, 11) == 0)
214 if ((flags & CH_UPDATE_IRT) &&
215 ascii_strncasecmp ("In-Reply-To:", buf, 12) == 0)
218 /* Find x -- the array entry where this header is to be saved */
219 if (flags & CH_REORDER)
221 for (t = HeaderOrderList, x = 0 ; (t) ; t = t->next, x++)
223 if (!ascii_strncasecmp (buf, t->data, mutt_strlen (t->data)))
225 dprint(2, (debugfile, "Reorder: %s matches %s\n", t->data, buf));
232 } /* If beginning of header */
236 dprint (2, (debugfile, "Reorder: x = %d; hdr_count = %d\n", x, hdr_count));
238 this_one = safe_strdup (buf);
239 this_one_len = mutt_strlen (this_one);
241 int blen = mutt_strlen (buf);
243 safe_realloc (&this_one, this_one_len + blen + sizeof (char));
244 strcat (this_one + this_one_len, buf); /* __STRCAT_CHECKED__ */
245 this_one_len += blen;
248 } /* while (ftello (in) < off_end) */
250 /* Do we have anything pending? -- XXX, same code as in above in the loop. */
253 if (flags & CH_DECODE)
255 if (!address_header_decode (&this_one))
256 rfc2047_decode (&this_one);
260 headers[x] = this_one;
263 int hlen = mutt_strlen (headers[x]);
265 safe_realloc (&headers[x], hlen + this_one_len + sizeof (char));
266 strcat (headers[x] + hlen, this_one); /* __STRCAT_CHECKED__ */
273 /* Now output the headers in order */
274 for (x = 0; x < hdr_count; x++)
279 if (flags & CH_DECODE)
280 rfc2047_decode (&headers[x]);
283 /* We couldn't do the prefixing when reading because RFC 2047
284 * decoding may have concatenated lines.
287 if (flags & (CH_DECODE|CH_PREFIX))
289 if (mutt_write_one_header (out, 0, headers[x],
290 flags & CH_PREFIX ? prefix : 0, mutt_term_width (Wrap)) == -1)
298 if (fputs (headers[x], out) == EOF)
307 /* Free in a separate loop to be sure that all headers are freed
308 * in case of error. */
309 for (x = 0; x < hdr_count; x++)
319 CH_DECODE RFC2047 header decoding
320 CH_FROM retain the "From " message separator
321 CH_FORCE_FROM give CH_FROM precedence over CH_WEED
322 CH_MIME ignore MIME fields
323 CH_NOLEN don't write Content-Length: and Lines:
324 CH_NONEWLINE don't output a newline after the header
325 CH_NOSTATUS ignore the Status: and X-Status:
326 CH_PREFIX quote header with $indent_str
327 CH_REORDER output header in order specified by `hdr_order'
328 CH_TXTPLAIN generate text/plain MIME headers [hack alert.]
329 CH_UPDATE write new Status: and X-Status:
330 CH_UPDATE_LEN write new Content-Length: and Lines:
331 CH_XMIT ignore Lines: and Content-Length:
332 CH_WEED do header weeding
333 CH_NOQFROM ignore ">From " line
334 CH_UPDATE_IRT update the In-Reply-To: header
335 CH_UPDATE_REFS update the References: header
338 string to use if CH_PREFIX is set
342 mutt_copy_header (FILE *in, HEADER *h, FILE *out, int flags, const char *prefix)
344 char buffer[SHORT_STRING];
347 flags |= (h->env->irt_changed ? CH_UPDATE_IRT : 0)
348 | (h->env->refs_changed ? CH_UPDATE_REFS : 0);
350 if (mutt_copy_hdr (in, out, h->offset, h->content->offset, flags, prefix) == -1)
353 if (flags & CH_TXTPLAIN)
355 char chsbuf[SHORT_STRING];
356 fputs ("MIME-Version: 1.0\n", out);
357 fputs ("Content-Transfer-Encoding: 8bit\n", out);
358 fputs ("Content-Type: text/plain; charset=", out);
359 mutt_canonical_charset (chsbuf, sizeof (chsbuf), Charset ? Charset : "us-ascii");
360 rfc822_cat(buffer, sizeof(buffer), chsbuf, MimeSpecials);
365 if ((flags & CH_UPDATE_IRT) && h->env->in_reply_to)
367 LIST *listp = h->env->in_reply_to;
368 fputs ("In-Reply-To:", out);
369 for (; listp; listp = listp->next)
372 fputs (listp->data, out);
377 if ((flags & CH_UPDATE_REFS) && h->env->references)
379 fputs ("References:", out);
380 mutt_write_references (h->env->references, out, 0);
384 if ((flags & CH_UPDATE) && (flags & CH_NOSTATUS) == 0)
386 if (h->old || h->read)
388 fputs ("Status: ", out);
396 if (h->flagged || h->replied)
398 fputs ("X-Status: ", out);
407 if (flags & CH_UPDATE_LEN &&
408 (flags & CH_NOLEN) == 0)
410 fprintf (out, "Content-Length: " OFF_T_FMT "\n", h->content->length);
411 if (h->lines != 0 || h->content->length == 0)
412 fprintf (out, "Lines: %d\n", h->lines);
415 if ((flags & CH_NONEWLINE) == 0)
417 if (flags & CH_PREFIX)
419 fputc ('\n', out); /* add header terminator */
422 if (ferror (out) || feof (out))
428 /* Count the number of lines and bytes to be deleted in this body*/
429 static int count_delete_lines (FILE *fp, BODY *b, LOFF_T *length, size_t datelen)
437 fseeko (fp, b->offset, SEEK_SET);
438 for (l = b->length ; l ; l --)
447 *length -= b->length - (84 + datelen);
448 /* Count the number of digits exceeding the first one to write the size */
449 for (l = 10 ; b->length >= l ; l *= 10)
454 for (b = b->parts ; b ; b = b->next)
455 dellines += count_delete_lines (fp, b, length, datelen);
460 /* make a copy of a message
462 * fpout where to write output
463 * fpin where to get input
464 * hdr header of message being copied
465 * body structure of message being copied
467 * M_CM_NOHEADER don't copy header
468 * M_CM_PREFIX quote header and body
469 * M_CM_DECODE decode message body to text/plain
470 * M_CM_DISPLAY displaying output to the user
471 * M_CM_PRINTING printing the message
472 * M_CM_UPDATE update structures in memory after syncing
473 * M_CM_DECODE_PGP used for decoding PGP messages
474 * M_CM_CHARCONV perform character set conversion
475 * chflags flags to mutt_copy_header()
479 _mutt_copy_message (FILE *fpout, FILE *fpin, HEADER *hdr, BODY *body,
480 int flags, int chflags)
482 char prefix[SHORT_STRING];
484 LOFF_T new_offset = -1;
487 if (flags & M_CM_PREFIX)
489 if (option (OPTTEXTFLOWED))
490 strfcpy (prefix, ">", sizeof (prefix));
492 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context, hdr, 0);
495 if ((flags & M_CM_NOHEADER) == 0)
497 if (flags & M_CM_PREFIX)
498 chflags |= CH_PREFIX;
500 else if (hdr->attach_del && (chflags & CH_UPDATE_LEN))
503 LOFF_T new_length = body->length;
504 char date[SHORT_STRING];
506 mutt_make_date (date, sizeof (date));
507 date[5] = date[mutt_strlen (date) - 1] = '\"';
509 /* Count the number of lines and bytes to be deleted */
510 fseeko (fpin, body->offset, SEEK_SET);
511 new_lines = hdr->lines -
512 count_delete_lines (fpin, body, &new_length, mutt_strlen (date));
514 /* Copy the headers */
515 if (mutt_copy_header (fpin, hdr, fpout,
516 chflags | CH_NOLEN | CH_NONEWLINE, NULL))
518 fprintf (fpout, "Content-Length: " OFF_T_FMT "\n", new_length);
522 fprintf (fpout, "Lines: %d\n", new_lines);
525 if (ferror (fpout) || feof (fpout))
527 new_offset = ftello (fpout);
530 fseeko (fpin, body->offset, SEEK_SET);
531 if (copy_delete_attach (body, fpin, fpout, date))
536 LOFF_T fail = ((ftello (fpout) - new_offset) - new_length);
540 mutt_error ("The length calculation was wrong by %ld bytes", fail);
547 /* Update original message if we are sync'ing a mailfolder */
548 if (flags & M_CM_UPDATE)
551 hdr->lines = new_lines;
552 body->offset = new_offset;
554 /* update the total size of the mailbox to reflect this deletion */
555 Context->size -= body->length - new_length;
557 * if the message is visible, update the visible size of the mailbox
560 if (Context->v2r[hdr->msgno] != -1)
561 Context->vsize -= body->length - new_length;
563 body->length = new_length;
564 mutt_free_body (&body->parts);
570 if (mutt_copy_header (fpin, hdr, fpout, chflags,
571 (chflags & CH_PREFIX) ? prefix : NULL) == -1)
574 new_offset = ftello (fpout);
577 if (flags & M_CM_DECODE)
579 /* now make a text/plain version of the message */
580 memset (&s, 0, sizeof (STATE));
583 if (flags & M_CM_PREFIX)
585 if (flags & M_CM_DISPLAY)
586 s.flags |= M_DISPLAY;
587 if (flags & M_CM_PRINTING)
588 s.flags |= M_PRINTING;
589 if (flags & M_CM_WEED)
591 if (flags & M_CM_CHARCONV)
592 s.flags |= M_CHARCONV;
593 if (flags & M_CM_REPLYING)
594 s.flags |= M_REPLYING;
596 if (WithCrypto && flags & M_CM_VERIFY)
599 rc = mutt_body_handler (body, &s);
602 && (flags & M_CM_DECODE_CRYPT) && (hdr->security & ENCRYPT))
607 if ((WithCrypto & APPLICATION_PGP)
608 && (flags & M_CM_DECODE_PGP) && (hdr->security & APPLICATION_PGP) &&
609 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 ((WithCrypto & APPLICATION_SMIME)
617 && (flags & M_CM_DECODE_SMIME) && (hdr->security & APPLICATION_SMIME)
618 && hdr->content->type == TYPEAPPLICATION)
620 if (crypt_smime_decrypt_mime (fpin, &fp, hdr->content, &cur))
626 mutt_error (_("No decryption engine available for message"));
630 mutt_write_mime_header (cur, fpout);
633 fseeko (fp, cur->offset, 0);
634 if (mutt_copy_bytes (fp, fpout, cur->length) == -1)
637 mutt_free_body (&cur);
640 mutt_free_body (&cur);
645 fseeko (fpin, body->offset, 0);
646 if (flags & M_CM_PREFIX)
649 size_t bytes = body->length;
651 fputs(prefix, fpout);
653 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
669 body->offset = new_offset;
670 mutt_free_body (&body->parts);
676 /* should be made to return -1 on fatal errors, and 1 on non-fatal errors
677 * like partial decode, where it is worth displaying as much as possible */
679 mutt_copy_message (FILE *fpout, CONTEXT *src, HEADER *hdr, int flags,
685 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
687 if ((r = _mutt_copy_message (fpout, msg->fp, hdr, hdr->content, flags, chflags)) == 0
688 && (ferror (fpout) || feof (fpout)))
690 dprint (1, (debugfile, "_mutt_copy_message failed to detect EOF!\n"));
693 mx_close_message (&msg);
697 /* appends a copy of the given message to a mailbox
699 * dest destination mailbox
700 * fpin where to get input
702 * hdr message being copied
703 * body structure of message being copied
704 * flags mutt_copy_message() flags
705 * chflags mutt_copy_header() flags
709 _mutt_append_message (CONTEXT *dest, FILE *fpin, CONTEXT *src, HEADER *hdr,
710 BODY *body, int flags, int chflags)
716 fseeko (fpin, hdr->offset, 0);
717 if (fgets (buf, sizeof (buf), fpin) == NULL)
720 if ((msg = mx_open_new_message (dest, hdr, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL)
722 if (dest->magic == M_MBOX || dest->magic == M_MMDF)
723 chflags |= CH_FROM | CH_FORCE_FROM;
724 chflags |= (dest->magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
725 r = _mutt_copy_message (msg->fp, fpin, hdr, body, flags, chflags);
726 if (mx_commit_message (msg, dest) != 0)
729 mx_close_message (&msg);
734 mutt_append_message (CONTEXT *dest, CONTEXT *src, HEADER *hdr, int cmflags,
740 if ((msg = mx_open_message (src, hdr->msgno)) == NULL)
742 r = _mutt_append_message (dest, msg->fp, src, hdr, hdr->content, cmflags, chflags);
743 mx_close_message (&msg);
748 * This function copies a message body, while deleting _in_the_copy_
749 * any attachments which are marked for deletion.
750 * Nothing is changed in the original message -- this is left to the caller.
752 * The function will return 0 on success and -1 on failure.
754 static int copy_delete_attach (BODY *b, FILE *fpin, FILE *fpout, char *date)
758 for (part = b->parts ; part ; part = part->next)
760 if (part->deleted || part->parts)
762 /* Copy till start of this part */
763 if (mutt_copy_bytes (fpin, fpout, part->hdr_offset - ftello (fpin)))
769 "Content-Type: message/external-body; access-type=x-mutt-deleted;\n"
770 "\texpiration=%s; length=" OFF_T_FMT "\n"
771 "\n", date + 5, part->length);
775 /* Copy the original mime headers */
776 if (mutt_copy_bytes (fpin, fpout, part->offset - ftello (fpin)))
779 /* Skip the deleted body */
780 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 *a)
808 char buf[HUGE_STRING];
812 int l, linelen, buflen, count, cbuflen, c2buflen, plen;
814 linelen = mutt_strlen (*h);
816 buflen = linelen + 3;
818 safe_realloc (h, buflen);
819 for (count = 0; a; a = a->next, count++)
821 ADDRESS *tmp = a->next;
823 *buf = *cbuf = *c2buf = '\0';
824 l = rfc822_write_address (buf, sizeof (buf), a, 0);
827 if (count && linelen + l > 74)
829 strcpy (cbuf, "\n\t"); /* __STRCPY_CHECKED__ */
836 strcpy (cbuf, " "); /* __STRCPY_CHECKED__ */
841 if (!a->group && a->next && a->next->mailbox)
845 strcpy (c2buf, ","); /* __STRCPY_CHECKED__ */
848 cbuflen = mutt_strlen (cbuf);
849 c2buflen = mutt_strlen (c2buf);
850 buflen += l + cbuflen + c2buflen;
851 safe_realloc (h, buflen);
853 strcat (p + plen, cbuf); /* __STRCAT_CHECKED__ */
855 strcat (p + plen, buf); /* __STRCAT_CHECKED__ */
857 strcat (p + plen, c2buf); /* __STRCAT_CHECKED__ */
861 /* Space for this was allocated in the beginning of this function. */
862 strcat (p + plen, "\n"); /* __STRCAT_CHECKED__ */
865 static int address_header_decode (char **h)
872 switch (tolower ((unsigned char) *s))
876 if (ascii_strncasecmp (s, "return-path:", 12) == 0)
881 else if (ascii_strncasecmp (s, "reply-to:", 9) == 0)
890 if (ascii_strncasecmp (s, "from:", 5))
897 if (ascii_strncasecmp (s, "cc:", 3))
905 if (ascii_strncasecmp (s, "bcc:", 4))
912 if (ascii_strncasecmp (s, "sender:", 7))
919 if (ascii_strncasecmp (s, "to:", 3))
926 if (ascii_strncasecmp (s, "mail-followup-to:", 17))
934 if ((a = rfc822_parse_adrlist (a, s + l)) == NULL)
937 mutt_addrlist_to_local (a);
938 rfc2047_decode_adrlist (a);
940 *h = safe_calloc (1, l + 2);
942 strfcpy (*h, s, l + 1);
944 format_address_header (h, a);
946 rfc822_free_address (&a);