3 * Miscellaneous functions for sending an email
6 * Copyright (C) 1996-2002,2009-2012 Michael R. Elkins <me@mutt.org>
9 * This program is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 2 of the License, or (at your option) any later
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
24 * @page sendlib Miscellaneous functions for sending an email
26 * Miscellaneous functions for sending an email
44 #include "mutt/mutt.h"
45 #include "address/lib.h"
46 #include "config/lib.h"
47 #include "email/lib.h"
58 #include "mutt_parse.h"
59 #include "mutt_window.h"
62 #include "ncrypt/ncrypt.h"
68 #include "nntp/nntp.h"
70 #ifdef HAVE_SYSEXITS_H
76 /* These Config Variables are only used in sendlib.c */
77 bool C_Allow8bit; ///< Config: Allow 8-bit messages, don't use quoted-printable or base64
78 char *C_AttachCharset; ///< Config: When attaching files, use one of these character sets
79 bool C_BounceDelivered; ///< Config: Add 'Delivered-To' to bounced messages
80 bool C_EncodeFrom; ///< Config: Encode 'From ' as 'quote-printable' at the beginning of lines
81 bool C_ForwardDecrypt; ///< Config: Decrypt the message when forwarding it
82 bool C_HiddenHost; ///< Config: Don't use the hostname, just the domain, when generating the message id
83 char *C_Inews; ///< Config: (nntp) External command to post news articles
84 bool C_MimeForwardDecode; ///< Config: Decode the forwarded message before attaching it
85 bool C_MimeSubject; ///< Config: (nntp) Encode the article subject in base64
86 char *C_MimeTypeQueryCommand; ///< Config: External command to determine the MIME type of an attachment
87 bool C_MimeTypeQueryFirst; ///< Config: Run the #C_MimeTypeQueryCommand before the mime.types lookup
88 char *C_Sendmail; ///< Config: External command to send email
89 short C_SendmailWait; ///< Config: Time to wait for sendmail to finish
90 bool C_Use8bitmime; ///< Config: Use 8-bit messages and ESMTP to send messages
91 bool C_UseEnvelopeFrom; ///< Config: Set the envelope sender of the message
92 bool C_UserAgent; ///< Config: Add a 'User-Agent' head to outgoing mail
93 short C_WrapHeaders; ///< Config: Width to wrap headers in outgoing messages
96 * encode_quoted - Encode text as quoted printable
97 * @param fc Cursor for converting a file's encoding
98 * @param fp_out File to store the result
99 * @param istext Is the input text?
101 static void encode_quoted(struct FgetConv *fc, FILE *fp_out, bool istext)
104 char line[77], savechar;
106 while ((c = mutt_ch_fgetconv(fc)) != EOF)
108 /* Wrap the line if needed. */
109 if ((linelen == 76) && ((istext && (c != '\n')) || !istext))
111 /* If the last character is "quoted", then be sure to move all three
112 * characters to the next line. Otherwise, just move the last
114 if (line[linelen - 3] == '=')
116 line[linelen - 3] = 0;
118 fputs("=\n", fp_out);
121 line[1] = line[linelen - 2];
122 line[2] = line[linelen - 1];
127 savechar = line[linelen - 1];
128 line[linelen - 1] = '=';
137 /* Escape lines that begin with/only contain "the message separator". */
138 if ((linelen == 4) && mutt_str_startswith(line, "From", CASE_MATCH))
140 mutt_str_strfcpy(line, "=46rom", sizeof(line));
143 else if ((linelen == 4) && mutt_str_startswith(line, "from", CASE_MATCH))
145 mutt_str_strfcpy(line, "=66rom", sizeof(line));
148 else if ((linelen == 1) && (line[0] == '.'))
150 mutt_str_strfcpy(line, "=2E", sizeof(line));
154 if ((c == '\n') && istext)
156 /* Check to make sure there is no trailing space on this line. */
157 if ((linelen > 0) && ((line[linelen - 1] == ' ') || (line[linelen - 1] == '\t')))
161 sprintf(line + linelen - 1, "=%2.2X", (unsigned char) line[linelen - 1]);
166 int savechar2 = line[linelen - 1];
168 line[linelen - 1] = '=';
171 fprintf(fp_out, "\n=%2.2X", (unsigned char) savechar2);
182 else if ((c != 9) && ((c < 32) || (c > 126) || (c == '=')))
184 /* Check to make sure there is enough room for the quoted character.
185 * If not, wrap to the next line. */
188 line[linelen++] = '=';
194 sprintf(line + linelen, "=%2.2X", (unsigned char) c);
199 /* Don't worry about wrapping the line here. That will happen during
200 * the next iteration when I'll also know what the next character is. */
205 /* Take care of anything left in the buffer */
208 if ((line[linelen - 1] == ' ') || (line[linelen - 1] == '\t'))
210 /* take care of trailing whitespace */
212 sprintf(line + linelen - 1, "=%2.2X", (unsigned char) line[linelen - 1]);
215 savechar = line[linelen - 1];
216 line[linelen - 1] = '=';
220 sprintf(line, "=%2.2X", (unsigned char) savechar);
230 * struct B64Context - Cursor for the Base64 conversion
240 * b64_init - Set up the base64 conversion
241 * @param bctx Cursor for the base64 conversion
244 static int b64_init(struct B64Context *bctx)
246 memset(bctx->buffer, '\0', sizeof(bctx->buffer));
254 * b64_flush - Save the bytes to the file
255 * @param bctx Cursor for the base64 conversion
256 * @param fp_out File to save the output
258 static void b64_flush(struct B64Context *bctx, FILE *fp_out)
260 /* for some reasons, mutt_b64_encode expects the
261 * output buffer to be larger than 10B */
268 if (bctx->linelen >= 72)
274 /* ret should always be equal to 4 here, because bctx->size
275 * is a value between 1 and 3 (included), but let's not hardcode it
276 * and prefer the return value of the function */
277 ret = mutt_b64_encode(bctx->buffer, bctx->size, encoded, sizeof(encoded));
278 for (size_t i = 0; i < ret; i++)
280 fputc(encoded[i], fp_out);
288 * b64_putc - Base64-encode one character
289 * @param bctx Cursor for the base64 conversion
290 * @param c Character to encode
291 * @param fp_out File to save the output
293 static void b64_putc(struct B64Context *bctx, char c, FILE *fp_out)
296 b64_flush(bctx, fp_out);
298 bctx->buffer[bctx->size++] = c;
302 * encode_base64 - Base64-encode some data
303 * @param fc Cursor for converting a file's encoding
304 * @param fp_out File to store the result
305 * @param istext Is the input text?
307 static void encode_base64(struct FgetConv *fc, FILE *fp_out, int istext)
309 struct B64Context bctx;
314 while ((ch = mutt_ch_fgetconv(fc)) != EOF)
321 if (istext && (ch == '\n') && (ch1 != '\r'))
322 b64_putc(&bctx, '\r', fp_out);
323 b64_putc(&bctx, ch, fp_out);
326 b64_flush(&bctx, fp_out);
331 * encode_8bit - Write the data as raw 8-bit data
332 * @param fc Cursor for converting a file's encoding
333 * @param fp_out File to store the result
335 static void encode_8bit(struct FgetConv *fc, FILE *fp_out)
339 while ((ch = mutt_ch_fgetconv(fc)) != EOF)
351 * mutt_write_mime_header - Create a MIME header
353 * @param fp File to write to
357 int mutt_write_mime_header(struct Body *a, FILE *fp)
361 char buf[256] = { 0 };
363 fprintf(fp, "Content-Type: %s/%s", TYPE(a), a->subtype);
365 if (!TAILQ_EMPTY(&a->parameter))
367 len = 25 + mutt_str_strlen(a->subtype); /* approximate len. of content-type */
369 struct Parameter *np = NULL;
370 TAILQ_FOREACH(np, &a->parameter, entries)
372 if (!np->attribute || !np->value)
375 struct ParameterList param_conts = rfc2231_encode_string(np->attribute, np->value);
376 struct Parameter *cont = NULL;
377 TAILQ_FOREACH(cont, ¶m_conts, entries)
382 mutt_addr_cat(buf, sizeof(buf), cont->value, MimeSpecials);
384 /* Dirty hack to make messages readable by Outlook Express
385 * for the Mac: force quotes around the boundary parameter
386 * even when they aren't needed.
388 if (!mutt_str_strcasecmp(cont->attribute, "boundary") &&
389 !mutt_str_strcmp(buf, cont->value))
390 snprintf(buf, sizeof(buf), "\"%s\"", cont->value);
392 tmplen = mutt_str_strlen(buf) + mutt_str_strlen(cont->attribute) + 1;
393 if (len + tmplen + 2 > 76)
404 fprintf(fp, "%s=%s", cont->attribute, buf);
407 mutt_param_free(¶m_conts);
414 fprintf(fp, "Content-Language: %s\n", a->language);
417 fprintf(fp, "Content-Description: %s\n", a->description);
419 if (a->disposition != DISP_NONE)
421 const char *dispstr[] = { "inline", "attachment", "form-data" };
423 if (a->disposition < sizeof(dispstr) / sizeof(char *))
425 fprintf(fp, "Content-Disposition: %s", dispstr[a->disposition]);
426 len = 21 + mutt_str_strlen(dispstr[a->disposition]);
428 if (a->use_disp && (a->disposition != DISP_INLINE))
430 char *fn = a->d_filename;
436 /* Strip off the leading path... */
437 char *t = strrchr(fn, '/');
443 struct ParameterList param_conts = rfc2231_encode_string("filename", t);
444 struct Parameter *cont = NULL;
445 TAILQ_FOREACH(cont, ¶m_conts, entries)
449 mutt_addr_cat(buf, sizeof(buf), cont->value, MimeSpecials);
451 tmplen = mutt_str_strlen(buf) + mutt_str_strlen(cont->attribute) + 1;
452 if (len + tmplen + 2 > 76)
463 fprintf(fp, "%s=%s", cont->attribute, buf);
466 mutt_param_free(¶m_conts);
474 mutt_debug(LL_DEBUG1, "ERROR: invalid content-disposition %d\n", a->disposition);
478 if (a->encoding != ENC_7BIT)
479 fprintf(fp, "Content-Transfer-Encoding: %s\n", ENCODING(a->encoding));
481 if (C_CryptProtectedHeadersWrite && a->mime_headers)
482 mutt_rfc822_write_header(fp, a->mime_headers, NULL, MUTT_WRITE_HEADER_MIME, false, false);
484 /* Do NOT add the terminator here!!! */
485 return ferror(fp) ? -1 : 0;
489 * write_as_text_part - Should the Body be written as a text MIME part
490 * @param b Email to examine
491 * @retval true If the Body should be written as text
493 static bool write_as_text_part(struct Body *b)
495 return mutt_is_text_part(b) ||
496 (((WithCrypto & APPLICATION_PGP) != 0) && mutt_is_application_pgp(b));
500 * mutt_write_mime_body - Write a MIME part
501 * @param a Body to use
502 * @param fp File to write to
506 int mutt_write_mime_body(struct Body *a, FILE *fp)
509 struct FgetConv *fc = NULL;
511 if (a->type == TYPE_MULTIPART)
513 /* First, find the boundary to use */
514 const char *p = mutt_param_get(&a->parameter, "boundary");
517 mutt_debug(LL_DEBUG1, "no boundary parameter found\n");
518 mutt_error(_("No boundary parameter found [report this error]"));
522 mutt_str_strfcpy(boundary, p, sizeof(boundary));
524 for (struct Body *t = a->parts; t; t = t->next)
526 fprintf(fp, "\n--%s\n", boundary);
527 if (mutt_write_mime_header(t, fp) == -1)
530 if (mutt_write_mime_body(t, fp) == -1)
533 fprintf(fp, "\n--%s--\n", boundary);
534 return ferror(fp) ? -1 : 0;
537 /* This is pretty gross, but it's the best solution for now... */
538 if (((WithCrypto & APPLICATION_PGP) != 0) && (a->type == TYPE_APPLICATION) &&
539 (mutt_str_strcmp(a->subtype, "pgp-encrypted") == 0))
541 fputs("Version: 1\n", fp);
545 fp_in = fopen(a->filename, "r");
548 mutt_debug(LL_DEBUG1, "%s no longer exists\n", a->filename);
549 mutt_error(_("%s no longer exists"), a->filename);
553 if ((a->type == TYPE_TEXT) && (!a->noconv))
555 char send_charset[128];
556 fc = mutt_ch_fgetconv_open(
558 mutt_body_get_charset(a, send_charset, sizeof(send_charset)), 0);
561 fc = mutt_ch_fgetconv_open(fp_in, 0, 0, 0);
563 mutt_sig_allow_interrupt(1);
564 if (a->encoding == ENC_QUOTED_PRINTABLE)
565 encode_quoted(fc, fp, write_as_text_part(a));
566 else if (a->encoding == ENC_BASE64)
567 encode_base64(fc, fp, write_as_text_part(a));
568 else if ((a->type == TYPE_TEXT) && (!a->noconv))
571 mutt_file_copy_stream(fp_in, fp);
572 mutt_sig_allow_interrupt(0);
574 mutt_ch_fgetconv_close(&fc);
575 mutt_file_fclose(&fp_in);
582 return ferror(fp) ? -1 : 0;
586 * mutt_generate_boundary - Create a unique boundary id for a MIME part
587 * @param parm MIME part
589 void mutt_generate_boundary(struct ParameterList *parm)
591 char rs[MUTT_RANDTAG_LEN + 1];
593 mutt_rand_base32(rs, sizeof(rs) - 1);
594 rs[MUTT_RANDTAG_LEN] = 0;
595 mutt_param_set(parm, "boundary", rs);
599 * struct ContentState - Info about the body of an email
611 * update_content_info - Cache some info about an email
612 * @param info Info about an Attachment
613 * @param s Info about the Body of an email
614 * @param buf Buffer for the result
615 * @param buflen Length of the buffer
617 static void update_content_info(struct Content *info, struct ContentState *s,
618 char *buf, size_t buflen)
621 int whitespace = s->whitespace;
623 int linelen = s->linelen;
624 bool was_cr = s->was_cr;
626 if (!buf) /* This signals EOF */
630 if (linelen > info->linemax)
631 info->linemax = linelen;
636 for (; buflen; buf++, buflen--)
653 if (linelen > info->linemax)
654 info->linemax = linelen;
670 if (linelen > info->linemax)
671 info->linemax = linelen;
685 else if ((ch == '\t') || (ch == '\f'))
695 else if ((ch < 32) || (ch == 127))
701 if ((ch == 'F') || (ch == 'f'))
712 if ((linelen == 2) && (ch != 'r'))
714 else if ((linelen == 3) && (ch != 'o'))
716 else if (linelen == 4)
730 if ((ch != ' ') && (ch != '\t'))
735 s->whitespace = whitespace;
737 s->linelen = linelen;
742 * convert_file_to - Change the encoding of a file
743 * @param[in] fp File to convert
744 * @param[in] fromcode Original encoding
745 * @param[in] ncodes Number of target encodings
746 * @param[in] tocodes List of target encodings
747 * @param[out] tocode Chosen encoding
748 * @param[in] info Encoding information
749 * @retval -1 Error, no conversion was possible
750 * @retval >0 Success, number of bytes converted
752 * Find the best charset conversion of the file from fromcode into one
753 * of the tocodes. If successful, set *tocode and Content *info and
754 * return the number of characters converted inexactly.
756 * We convert via UTF-8 in order to avoid the condition -1(EINVAL),
757 * which would otherwise prevent us from knowing the number of inexact
758 * conversions. Where the candidate target charset is UTF-8 we avoid
759 * doing the second conversion because iconv_open("UTF-8", "UTF-8")
760 * fails with some libraries.
762 * We assume that the output from iconv is never more than 4 times as
763 * long as the input for any pair of charsets we might be interested
766 static size_t convert_file_to(FILE *fp, const char *fromcode, int ncodes,
767 const char **tocodes, int *tocode, struct Content *info)
769 char bufi[256], bufu[512], bufo[4 * sizeof(bufi)];
772 const iconv_t cd1 = mutt_ch_iconv_open("utf-8", fromcode, 0);
773 if (cd1 == (iconv_t)(-1))
776 iconv_t *cd = mutt_mem_calloc(ncodes, sizeof(iconv_t));
777 size_t *score = mutt_mem_calloc(ncodes, sizeof(size_t));
778 struct ContentState *states = mutt_mem_calloc(ncodes, sizeof(struct ContentState));
779 struct Content *infos = mutt_mem_calloc(ncodes, sizeof(struct Content));
781 for (int i = 0; i < ncodes; i++)
783 if (mutt_str_strcasecmp(tocodes[i], "utf-8") != 0)
784 cd[i] = mutt_ch_iconv_open(tocodes[i], "utf-8", 0);
787 /* Special case for conversion to UTF-8 */
788 cd[i] = (iconv_t)(-1);
789 score[i] = (size_t)(-1);
797 /* Try to fill input buffer */
798 size_t n = fread(bufi + ibl, 1, sizeof(bufi) - ibl, fp);
801 /* Convert to UTF-8 */
802 const char *ib = bufi;
804 size_t obl = sizeof(bufu);
805 n = iconv(cd1, (ICONV_CONST char **) (ibl ? &ib : 0), &ibl, &ob, &obl);
806 /* assert(n == (size_t)(-1) || !n); */
807 if ((n == (size_t)(-1)) && (((errno != EINVAL) && (errno != E2BIG)) || (ib == bufi)))
809 /* assert(errno == EILSEQ || (errno == EINVAL && ib == bufi && ibl < sizeof(bufi))); */
813 const size_t ubl1 = ob - bufu;
815 /* Convert from UTF-8 */
816 for (int i = 0; i < ncodes; i++)
818 if ((cd[i] != (iconv_t)(-1)) && (score[i] != (size_t)(-1)))
820 const char *ub = bufu;
824 n = iconv(cd[i], (ICONV_CONST char **) ((ibl || ubl) ? &ub : 0), &ubl, &ob, &obl);
825 if (n == (size_t)(-1))
827 /* assert(errno == E2BIG || (BUGGY_ICONV && (errno == EILSEQ || errno == ENOENT))); */
828 score[i] = (size_t)(-1);
833 update_content_info(&infos[i], &states[i], bufo, ob - bufo);
836 else if ((cd[i] == (iconv_t)(-1)) && (score[i] == (size_t)(-1)))
838 /* Special case for conversion to UTF-8 */
839 update_content_info(&infos[i], &states[i], bufu, ubl1);
845 /* Save unused input */
846 memmove(bufi, ib, ibl);
848 else if (!ubl1 && (ib < bufi + sizeof(bufi)))
857 /* Find best score */
859 for (int i = 0; i < ncodes; i++)
861 if ((cd[i] == (iconv_t)(-1)) && (score[i] == (size_t)(-1)))
863 /* Special case for conversion to UTF-8 */
868 else if ((cd[i] == (iconv_t)(-1)) || (score[i] == (size_t)(-1)))
870 else if ((ret == (size_t)(-1)) || (score[i] < ret))
878 if (ret != (size_t)(-1))
880 memcpy(info, &infos[*tocode], sizeof(struct Content));
881 update_content_info(info, &states[*tocode], 0, 0); /* EOF */
885 for (int i = 0; i < ncodes; i++)
886 if (cd[i] != (iconv_t)(-1))
899 * convert_file_from_to - Convert a file between encodings
900 * @param[in] fp File to read from
901 * @param[in] fromcodes Charsets to try converting FROM
902 * @param[in] tocodes Charsets to try converting TO
903 * @param[out] fromcode From charset selected
904 * @param[out] tocode To charset selected
905 * @param[out] info Info about the file
906 * @retval num Characters converted
907 * @retval -1 Error (as a size_t)
909 * Find the first of the fromcodes that gives a valid conversion and the best
910 * charset conversion of the file into one of the tocodes. If successful, set
911 * *fromcode and *tocode to dynamically allocated strings, set Content *info,
912 * and return the number of characters converted inexactly. If no conversion
913 * was possible, return -1.
915 * Both fromcodes and tocodes may be colon-separated lists of charsets.
916 * However, if fromcode is zero then fromcodes is assumed to be the name of a
917 * single charset even if it contains a colon.
919 static size_t convert_file_from_to(FILE *fp, const char *fromcodes, const char *tocodes,
920 char **fromcode, char **tocode, struct Content *info)
924 const char *c = NULL, *c1 = NULL;
928 /* Count the tocodes */
930 for (c = tocodes; c; c = c1 ? c1 + 1 : 0)
939 tcode = mutt_mem_malloc(ncodes * sizeof(char *));
940 for (c = tocodes, i = 0; c; c = c1 ? c1 + 1 : 0, i++)
945 tcode[i] = mutt_str_substr_dup(c, c1);
951 /* Try each fromcode in turn */
952 for (c = fromcodes; c; c = c1 ? c1 + 1 : 0)
957 fcode = mutt_str_substr_dup(c, c1);
959 ret = convert_file_to(fp, fcode, ncodes, (const char **) tcode, &cn, info);
960 if (ret != (size_t)(-1))
972 /* There is only one fromcode */
973 ret = convert_file_to(fp, fromcodes, ncodes, (const char **) tcode, &cn, info);
974 if (ret != (size_t)(-1))
982 for (i = 0; i < ncodes; i++)
991 * mutt_get_content_info - Analyze file to determine MIME encoding to use
992 * @param fname File to examine
993 * @param b Body to update
994 * @retval ptr Newly allocated Content
996 * Also set the body charset, sometimes, or not.
998 struct Content *mutt_get_content_info(const char *fname, struct Body *b)
1000 struct Content *info = NULL;
1001 struct ContentState state = { 0 };
1003 char *fromcode = NULL;
1004 char *tocode = NULL;
1011 fname = b->filename;
1013 if (stat(fname, &sb) == -1)
1015 mutt_error(_("Can't stat %s: %s"), fname, strerror(errno));
1019 if (!S_ISREG(sb.st_mode))
1021 mutt_error(_("%s isn't a regular file"), fname);
1025 fp = fopen(fname, "r");
1028 mutt_debug(LL_DEBUG1, "%s: %s (errno %d)\n", fname, strerror(errno), errno);
1032 info = mutt_mem_calloc(1, sizeof(struct Content));
1034 if (b && (b->type == TYPE_TEXT) && (!b->noconv && !b->force_charset))
1036 char *chs = mutt_param_get(&b->parameter, "charset");
1037 char *fchs = b->use_disp ?
1038 ((C_AttachCharset && *C_AttachCharset) ? C_AttachCharset : C_Charset) :
1040 if (C_Charset && (chs || C_SendCharset) &&
1041 (convert_file_from_to(fp, fchs, chs ? chs : C_SendCharset, &fromcode,
1042 &tocode, info) != (size_t)(-1)))
1047 mutt_ch_canonical_charset(chsbuf, sizeof(chsbuf), tocode);
1048 mutt_param_set(&b->parameter, "charset", chsbuf);
1051 b->charset = fromcode;
1053 mutt_file_fclose(&fp);
1059 while ((r = fread(buf, 1, sizeof(buf), fp)))
1060 update_content_info(info, &state, buf, r);
1061 update_content_info(info, &state, 0, 0);
1063 mutt_file_fclose(&fp);
1065 if (b && (b->type == TYPE_TEXT) && (!b->noconv && !b->force_charset))
1067 mutt_param_set(&b->parameter, "charset",
1070 C_Charset && !mutt_ch_is_us_ascii(C_Charset) ? C_Charset : "unknown-8bit"));
1077 * mutt_lookup_mime_type - Find the MIME type for an attachment
1078 * @param att Email with attachment
1079 * @param path Path to attachment
1080 * @retval num MIME type, e.g. #TYPE_IMAGE
1082 * Given a file at 'path', see if there is a registered MIME type.
1083 * Returns the major MIME type, and copies the subtype to "d". First look
1084 * in a system mime.types if we can find one, then look for ~/.mime.types.
1085 * The longest match is used so that we can match 'ps.gz' when 'gz' also
1088 int mutt_lookup_mime_type(struct Body *att, const char *path)
1091 char *p = NULL, *q = NULL, *ct = NULL;
1093 char subtype[256], xtype[256];
1094 int szf, sze, cur_sze;
1096 bool found_mimetypes = false;
1103 szf = mutt_str_strlen(path);
1105 for (int count = 0; count < 4; count++)
1107 /* can't use strtok() because we use it in an inner loop below, so use
1108 * a switch statement here instead. */
1111 /* last file with last entry to match wins type/xtype */
1113 /* check default unix mimetypes location first */
1114 mutt_str_strfcpy(buf, "/etc/mime.types", sizeof(buf));
1117 mutt_str_strfcpy(buf, SYSCONFDIR "/mime.types", sizeof(buf));
1120 mutt_str_strfcpy(buf, PKGDATADIR "/mime.types", sizeof(buf));
1123 snprintf(buf, sizeof(buf), "%s/.mime.types", NONULL(HomeDir));
1126 mutt_debug(LL_DEBUG1, "Internal error, count = %d\n", count);
1127 goto bye; /* shouldn't happen */
1130 fp = fopen(buf, "r");
1133 found_mimetypes = true;
1135 while (fgets(buf, sizeof(buf) - 1, fp))
1137 /* weed out any comments */
1138 p = strchr(buf, '#');
1142 /* remove any leading space. */
1146 /* position on the next field in this line */
1147 p = strpbrk(ct, " \t");
1153 /* cycle through the file extensions */
1154 while ((p = strtok(p, " \t\n")))
1156 sze = mutt_str_strlen(p);
1157 if ((sze > cur_sze) && (szf >= sze) &&
1158 ((mutt_str_strcasecmp(path + szf - sze, p) == 0) ||
1159 (mutt_str_strcasecmp(path + szf - sze, p) == 0)) &&
1160 ((szf == sze) || (path[szf - sze - 1] == '.')))
1162 /* get the content-type */
1164 p = strchr(ct, '/');
1167 /* malformed line, just skip it. */
1172 for (q = p; *q && !IS_SPACE(*q); q++)
1175 mutt_str_substr_cpy(subtype, p, q, sizeof(subtype));
1177 type = mutt_check_mime_type(ct);
1178 if (type == TYPE_OTHER)
1179 mutt_str_strfcpy(xtype, ct, sizeof(xtype));
1186 mutt_file_fclose(&fp);
1192 /* no mime.types file found */
1193 if (!found_mimetypes)
1195 mutt_error(_("Could not find any mime.types file."));
1198 if ((type != TYPE_OTHER) || (*xtype != '\0'))
1201 mutt_str_replace(&att->subtype, subtype);
1202 mutt_str_replace(&att->xtype, xtype);
1209 * transform_to_7bit - Convert MIME parts to 7-bit
1210 * @param a Body of the email
1211 * @param fp_in File to read
1213 static void transform_to_7bit(struct Body *a, FILE *fp_in)
1216 struct State s = { 0 };
1219 for (; a; a = a->next)
1221 if (a->type == TYPE_MULTIPART)
1223 if (a->encoding != ENC_7BIT)
1224 a->encoding = ENC_7BIT;
1226 transform_to_7bit(a->parts, fp_in);
1228 else if (mutt_is_message_type(a->type, a->subtype))
1230 mutt_message_to_7bit(a, fp_in);
1235 a->force_charset = true;
1237 mutt_mktemp(buf, sizeof(buf));
1238 s.fp_out = mutt_file_fopen(buf, "w");
1241 mutt_perror("fopen");
1245 mutt_decode_attachment(a, &s);
1246 mutt_file_fclose(&s.fp_out);
1247 FREE(&a->d_filename);
1248 a->d_filename = a->filename;
1249 a->filename = mutt_str_strdup(buf);
1251 if (stat(a->filename, &sb) == -1)
1253 mutt_perror("stat");
1256 a->length = sb.st_size;
1258 mutt_update_encoding(a);
1259 if (a->encoding == ENC_8BIT)
1260 a->encoding = ENC_QUOTED_PRINTABLE;
1261 else if (a->encoding == ENC_BINARY)
1262 a->encoding = ENC_BASE64;
1268 * mutt_message_to_7bit - Convert an email's MIME parts to 7-bit
1269 * @param a Body of the email
1270 * @param fp File to read (OPTIONAL)
1272 void mutt_message_to_7bit(struct Body *a, FILE *fp)
1274 char temp[PATH_MAX];
1277 FILE *fp_out = NULL;
1280 if (!a->filename && fp)
1282 else if (!a->filename || !(fp_in = fopen(a->filename, "r")))
1284 mutt_error(_("Could not open %s"), a->filename ? a->filename : "(null)");
1290 if (stat(a->filename, &sb) == -1)
1292 mutt_perror("stat");
1293 mutt_file_fclose(&fp_in);
1295 a->length = sb.st_size;
1298 mutt_mktemp(temp, sizeof(temp));
1299 fp_out = mutt_file_fopen(temp, "w+");
1302 mutt_perror("fopen");
1309 fseeko(fp_in, a->offset, SEEK_SET);
1310 a->parts = mutt_rfc822_parse_message(fp_in, a);
1312 transform_to_7bit(a->parts, fp_in);
1314 mutt_copy_hdr(fp_in, fp_out, a->offset, a->offset + a->length,
1315 CH_MIME | CH_NONEWLINE | CH_XMIT, NULL);
1317 fputs("MIME-Version: 1.0\n", fp_out);
1318 mutt_write_mime_header(a->parts, fp_out);
1319 fputc('\n', fp_out);
1320 mutt_write_mime_body(a->parts, fp_out);
1325 if (fp_in && (fp_in != fp))
1326 mutt_file_fclose(&fp_in);
1328 mutt_file_fclose(&fp_out);
1332 a->encoding = ENC_7BIT;
1333 FREE(&a->d_filename);
1334 a->d_filename = a->filename;
1335 if (a->filename && a->unlink)
1336 unlink(a->filename);
1337 a->filename = mutt_str_strdup(temp);
1339 if (stat(a->filename, &sb) == -1)
1341 mutt_perror("stat");
1344 a->length = sb.st_size;
1345 mutt_body_free(&a->parts);
1346 a->email->content = NULL;
1350 * set_encoding - determine which Content-Transfer-Encoding to use
1351 * @param[in] b Body of email
1352 * @param[out] info Info about the email
1354 static void set_encoding(struct Body *b, struct Content *info)
1356 if (b->type == TYPE_TEXT)
1358 char send_charset[128];
1359 char *chsname = mutt_body_get_charset(b, send_charset, sizeof(send_charset));
1360 if ((info->lobin && !mutt_str_startswith(chsname, "iso-2022", CASE_IGNORE)) ||
1361 (info->linemax > 990) || (info->from && C_EncodeFrom))
1363 b->encoding = ENC_QUOTED_PRINTABLE;
1365 else if (info->hibin)
1367 b->encoding = C_Allow8bit ? ENC_8BIT : ENC_QUOTED_PRINTABLE;
1371 b->encoding = ENC_7BIT;
1374 else if ((b->type == TYPE_MESSAGE) || (b->type == TYPE_MULTIPART))
1376 if (info->lobin || info->hibin)
1378 if (C_Allow8bit && !info->lobin)
1379 b->encoding = ENC_8BIT;
1381 mutt_message_to_7bit(b, NULL);
1384 b->encoding = ENC_7BIT;
1386 else if ((b->type == TYPE_APPLICATION) &&
1387 (mutt_str_strcasecmp(b->subtype, "pgp-keys") == 0))
1389 b->encoding = ENC_7BIT;
1393 /* Determine which encoding is smaller */
1394 if (1.33 * (float) (info->lobin + info->hibin + info->ascii) <
1395 3.0 * (float) (info->lobin + info->hibin) + (float) info->ascii)
1397 b->encoding = ENC_BASE64;
1401 b->encoding = ENC_QUOTED_PRINTABLE;
1407 * mutt_stamp_attachment - Timestamp an Attachment
1408 * @param a Attachment
1410 void mutt_stamp_attachment(struct Body *a)
1412 a->stamp = time(NULL);
1416 * mutt_body_get_charset - Get a body's character set
1417 * @param b Body to examine
1418 * @param buf Buffer for the result
1419 * @param buflen Length of the buffer
1420 * @retval ptr Buffer containing character set
1421 * @retval NULL On error, or if not a text type
1423 char *mutt_body_get_charset(struct Body *b, char *buf, size_t buflen)
1427 if (b && (b->type != TYPE_TEXT))
1431 p = mutt_param_get(&b->parameter, "charset");
1434 mutt_ch_canonical_charset(buf, buflen, p);
1436 mutt_str_strfcpy(buf, "us-ascii", buflen);
1442 * mutt_update_encoding - Update the encoding type
1443 * @param a Body to update
1445 * Assumes called from send mode where Body->filename points to actual file
1447 void mutt_update_encoding(struct Body *a)
1449 struct Content *info = NULL;
1452 /* override noconv when it's us-ascii */
1453 if (mutt_ch_is_us_ascii(mutt_body_get_charset(a, chsbuf, sizeof(chsbuf))))
1456 if (!a->force_charset && !a->noconv)
1457 mutt_param_delete(&a->parameter, "charset");
1459 info = mutt_get_content_info(a->filename, a);
1463 set_encoding(a, info);
1464 mutt_stamp_attachment(a);
1471 * mutt_make_message_attach - Create a message attachment
1474 * @param attach_msg true if attaching a message
1475 * @retval ptr Newly allocated Body
1476 * @retval NULL Error
1478 struct Body *mutt_make_message_attach(struct Mailbox *m, struct Email *e, bool attach_msg)
1481 struct Body *body = NULL;
1483 CopyMessageFlags cmflags;
1484 SecurityFlags pgp = WithCrypto ? e->security : SEC_NO_FLAGS;
1488 if ((C_MimeForwardDecode || C_ForwardDecrypt) && (e->security & SEC_ENCRYPT))
1490 if (!crypt_valid_passphrase(e->security))
1495 mutt_mktemp(buf, sizeof(buf));
1496 fp = mutt_file_fopen(buf, "w+");
1500 body = mutt_body_new();
1501 body->type = TYPE_MESSAGE;
1502 body->subtype = mutt_str_strdup("rfc822");
1503 body->filename = mutt_str_strdup(buf);
1504 body->unlink = true;
1505 body->use_disp = false;
1506 body->disposition = DISP_INLINE;
1507 body->noconv = true;
1509 mutt_parse_mime_message(m, e);
1511 CopyHeaderFlags chflags = CH_XMIT;
1512 cmflags = MUTT_CM_NO_FLAGS;
1514 /* If we are attaching a message, ignore C_MimeForwardDecode */
1515 if (!attach_msg && C_MimeForwardDecode)
1517 chflags |= CH_MIME | CH_TXTPLAIN;
1518 cmflags = MUTT_CM_DECODE | MUTT_CM_CHARCONV;
1519 if (WithCrypto & APPLICATION_PGP)
1520 pgp &= ~PGP_ENCRYPT;
1521 if (WithCrypto & APPLICATION_SMIME)
1522 pgp &= ~SMIME_ENCRYPT;
1524 else if ((WithCrypto != 0) && C_ForwardDecrypt && (e->security & SEC_ENCRYPT))
1526 if (((WithCrypto & APPLICATION_PGP) != 0) && mutt_is_multipart_encrypted(e->content))
1528 chflags |= CH_MIME | CH_NONEWLINE;
1529 cmflags = MUTT_CM_DECODE_PGP;
1530 pgp &= ~PGP_ENCRYPT;
1532 else if (((WithCrypto & APPLICATION_PGP) != 0) &&
1533 ((mutt_is_application_pgp(e->content) & PGP_ENCRYPT) == PGP_ENCRYPT))
1535 chflags |= CH_MIME | CH_TXTPLAIN;
1536 cmflags = MUTT_CM_DECODE | MUTT_CM_CHARCONV;
1537 pgp &= ~PGP_ENCRYPT;
1539 else if (((WithCrypto & APPLICATION_SMIME) != 0) &&
1540 ((mutt_is_application_smime(e->content) & SMIME_ENCRYPT) == SMIME_ENCRYPT))
1542 chflags |= CH_MIME | CH_TXTPLAIN;
1543 cmflags = MUTT_CM_DECODE | MUTT_CM_CHARCONV;
1544 pgp &= ~SMIME_ENCRYPT;
1548 mutt_copy_message_ctx(fp, m, e, cmflags, chflags);
1553 body->email = mutt_email_new();
1554 body->email->offset = 0;
1555 /* we don't need the user headers here */
1556 body->email->env = mutt_rfc822_read_header(fp, body->email, false, false);
1558 body->email->security = pgp;
1559 mutt_update_encoding(body);
1560 body->parts = body->email->content;
1562 mutt_file_fclose(&fp);
1568 * run_mime_type_query - Run an external command to determine the MIME type
1569 * @param att Attachment
1571 * The command in $mime_type_query_command is run.
1573 static void run_mime_type_query(struct Body *att)
1580 struct Buffer *cmd = mutt_buffer_pool_get();
1582 mutt_buffer_file_expand_fmt_quote(cmd, C_MimeTypeQueryCommand, att->filename);
1584 pid = mutt_create_filter(mutt_b2s(cmd), NULL, &fp, &fp_err);
1587 mutt_error(_("Error running \"%s\""), mutt_b2s(cmd));
1588 mutt_buffer_pool_release(&cmd);
1591 mutt_buffer_pool_release(&cmd);
1593 buf = mutt_file_read_line(buf, &buflen, fp, &dummy, 0);
1596 if (strchr(buf, '/'))
1597 mutt_parse_content_type(buf, att);
1601 mutt_file_fclose(&fp);
1602 mutt_file_fclose(&fp_err);
1603 mutt_wait_filter(pid);
1607 * mutt_make_file_attach - Create a file attachment
1608 * @param path File to attach
1609 * @retval ptr Newly allocated Body
1610 * @retval NULL Error
1612 struct Body *mutt_make_file_attach(const char *path)
1614 struct Body *att = mutt_body_new();
1615 att->filename = mutt_str_strdup(path);
1617 if (C_MimeTypeQueryCommand && *C_MimeTypeQueryCommand && C_MimeTypeQueryFirst)
1618 run_mime_type_query(att);
1620 /* Attempt to determine the appropriate content-type based on the filename
1623 mutt_lookup_mime_type(att, path);
1625 if (!att->subtype && C_MimeTypeQueryCommand && *C_MimeTypeQueryCommand && !C_MimeTypeQueryFirst)
1627 run_mime_type_query(att);
1630 struct Content *info = mutt_get_content_info(path, att);
1633 mutt_body_free(&att);
1639 if ((info->nulbin == 0) &&
1640 ((info->lobin == 0) || ((info->lobin + info->hibin + info->ascii) / info->lobin >= 10)))
1642 /* Statistically speaking, there should be more than 10% "lobin"
1643 * chars if this is really a binary file... */
1644 att->type = TYPE_TEXT;
1645 att->subtype = mutt_str_strdup("plain");
1649 att->type = TYPE_APPLICATION;
1650 att->subtype = mutt_str_strdup("octet-stream");
1655 mutt_update_encoding(att);
1660 * get_toplevel_encoding - Find the most restrictive encoding type
1661 * @param a Body to examine
1662 * @retval num Encoding type, e.g. #ENC_7BIT
1664 static int get_toplevel_encoding(struct Body *a)
1668 for (; a; a = a->next)
1670 if (a->encoding == ENC_BINARY)
1672 else if (a->encoding == ENC_8BIT)
1680 * check_boundary - check for duplicate boundary
1681 * @param boundary Boundary to look for
1682 * @param b Body parts to check
1683 * @retval true if duplicate found
1685 static bool check_boundary(const char *boundary, struct Body *b)
1689 if (b->parts && check_boundary(boundary, b->parts))
1692 if (b->next && check_boundary(boundary, b->next))
1695 p = mutt_param_get(&b->parameter, "boundary");
1696 if (p && (mutt_str_strcmp(p, boundary) == 0))
1704 * mutt_make_multipart - Create a multipart email
1705 * @param b Body of email to start
1706 * @retval ptr Newly allocated Body
1708 struct Body *mutt_make_multipart(struct Body *b)
1710 struct Body *new = mutt_body_new();
1711 new->type = TYPE_MULTIPART;
1712 new->subtype = mutt_str_strdup("mixed");
1713 new->encoding = get_toplevel_encoding(b);
1716 mutt_generate_boundary(&new->parameter);
1717 if (check_boundary(mutt_param_get(&new->parameter, "boundary"), b))
1718 mutt_param_delete(&new->parameter, "boundary");
1719 } while (!mutt_param_get(&new->parameter, "boundary"));
1720 new->use_disp = false;
1721 new->disposition = DISP_INLINE;
1728 * mutt_remove_multipart - Extract the multipart body if it exists
1729 * @param b Body to alter
1730 * @retval ptr The parts of the Body
1732 * @note The original Body is freed
1734 struct Body *mutt_remove_multipart(struct Body *b)
1736 struct Body *t = NULL;
1749 * mutt_write_address_list - wrapper around mutt_write_address()
1750 * @param addr Address list
1751 * @param fp File to write to
1752 * @param linelen Line length to use
1753 * @param display True if these addresses will be displayed to the user
1755 * So we can handle very large recipient lists without needing a huge temporary
1758 void mutt_write_address_list(struct Address *addr, FILE *fp, int linelen, bool display)
1760 struct Address *tmp = NULL;
1769 mutt_addr_write(buf, sizeof(buf), addr, display);
1770 size_t len = mutt_str_strlen(buf);
1771 if (count && (linelen + len > 74))
1774 linelen = len + 8; /* tab is usually about 8 spaces... */
1778 if (count && addr->mailbox)
1787 if (!addr->group && addr->next && addr->next->mailbox)
1799 * mutt_write_references - Add the message references to a list
1800 * @param r String List of references
1801 * @param fp File to write to
1802 * @param trim Trim the list to at most this many items
1804 * Write the list in reverse because they are stored in reverse order when
1805 * parsed to speed up threading.
1807 void mutt_write_references(const struct ListHead *r, FILE *fp, size_t trim)
1809 struct ListNode *np = NULL;
1812 STAILQ_FOREACH(np, r, entries)
1814 if (++length == trim)
1818 struct ListNode **ref = mutt_mem_calloc(length, sizeof(struct ListNode *));
1820 // store in reverse order
1821 size_t tmp = length;
1822 STAILQ_FOREACH(np, r, entries)
1829 for (size_t i = 0; i < length; i++)
1832 fputs(ref[i]->data, fp);
1833 if (i != length - 1)
1841 * print_val - Add pieces to an email header, wrapping where necessary
1842 * @param fp File to write to
1843 * @param pfx Prefix for headers
1844 * @param value Text to be added
1845 * @param chflags Flags, see #CopyHeaderFlags
1846 * @param col Column that this text starts at
1848 * @retval -1 Failure
1850 static int print_val(FILE *fp, const char *pfx, const char *value,
1851 CopyHeaderFlags chflags, size_t col)
1853 while (value && *value)
1855 if (fputc(*value, fp) == EOF)
1857 /* corner-case: break words longer than 998 chars by force,
1858 * mandated by RFC5322 */
1859 if (!(chflags & CH_DISPLAY) && (++col >= 998))
1861 if (fputs("\n ", fp) < 0)
1867 if (*(value + 1) && pfx && *pfx && (fputs(pfx, fp) == EOF))
1869 /* for display, turn folding spaces into folding tabs */
1870 if ((chflags & CH_DISPLAY) && ((*(value + 1) == ' ') || (*(value + 1) == '\t')))
1873 while (*value && ((*value == ' ') || (*value == '\t')))
1875 if (fputc('\t', fp) == EOF)
1886 * fold_one_header - Fold one header line
1887 * @param fp File to write to
1888 * @param tag Header key, e.g. "From"
1889 * @param value Header value
1890 * @param pfx Prefix for header
1891 * @param wraplen Column to wrap at
1892 * @param chflags Flags, see #CopyHeaderFlags
1894 * @retval -1 Failure
1896 static int fold_one_header(FILE *fp, const char *tag, const char *value,
1897 const char *pfx, int wraplen, CopyHeaderFlags chflags)
1899 const char *p = value;
1900 char buf[8192] = "";
1901 int first = 1, col = 0, l = 0;
1902 const bool display = (chflags & CH_DISPLAY);
1904 mutt_debug(5, "pfx=[%s], tag=[%s], flags=%d value=[%s]\n", pfx, tag, chflags,
1907 if (tag && *tag && (fprintf(fp, "%s%s: ", NONULL(pfx), tag) < 0))
1909 col = mutt_str_strlen(tag) + ((tag && *tag) ? 2 : 0) + mutt_str_strlen(pfx);
1915 /* find the next word and place it in 'buf'. it may start with
1916 * whitespace we can fold before */
1917 const char *next = mutt_str_find_word(p);
1918 l = MIN(sizeof(buf) - 1, next - p);
1922 /* determine width: character cells for display, bytes for sending
1923 * (we get pure ascii only) */
1924 const int w = mutt_mb_width(buf, col, display);
1925 const int enc = mutt_str_startswith(buf, "=?", CASE_MATCH);
1927 mutt_debug(5, "word=[%s], col=%d, w=%d, next=[0x0%x]\n", buf, col, w, *next);
1929 /* insert a folding \n before the current word's lwsp except for
1930 * header name, first word on a line (word longer than wrap width)
1931 * and encoded words */
1932 if (!first && !enc && col && (col + w >= wraplen))
1934 col = mutt_str_strlen(pfx);
1936 if (fprintf(fp, "\n%s", NONULL(pfx)) <= 0)
1940 /* print the actual word; for display, ignore leading ws for word
1941 * and fold with tab for readability */
1942 if (display && fold)
1945 while (*pc && ((*pc == ' ') || (*pc == '\t')))
1950 if (fputc('\t', fp) == EOF)
1952 if (print_val(fp, pfx, pc, chflags, col) < 0)
1956 else if (print_val(fp, pfx, buf, chflags, col) < 0)
1960 /* if the current word ends in \n, ignore all its trailing spaces
1961 * and reset column; this prevents us from putting only spaces (or
1962 * even none) on a line if the trailing spaces are located at our
1963 * current line width
1964 * XXX this covers ASCII space only, for display we probably
1965 * want something like iswspace() here */
1966 const char *sp = next;
1967 while (*sp && ((*sp == ' ') || (*sp == '\t')))
1979 /* if we have printed something but didn't \n-terminate it, do it
1980 * except the last word we printed ended in \n already */
1981 if (col && ((l == 0) || (buf[l - 1] != '\n')))
1982 if (putc('\n', fp) == EOF)
1989 * unfold_header - Unfold a wrapped email header
1990 * @param s String to process
1991 * @retval ptr Unfolded string
1993 * @note The string is altered in-place
1995 static char *unfold_header(char *s)
1997 char *p = s, *q = s;
2001 /* remove CRLF prior to FWSP, turn \t into ' ' */
2002 if ((*p == '\r') && *(p + 1) && (*(p + 1) == '\n') && *(p + 2) &&
2003 ((*(p + 2) == ' ') || (*(p + 2) == '\t')))
2009 /* remove LF prior to FWSP, turn \t into ' ' */
2010 else if ((*p == '\n') && *(p + 1) && ((*(p + 1) == ' ') || (*(p + 1) == '\t')))
2025 * write_one_header - Write out one header line
2026 * @param fp File to write to
2027 * @param pfxw Width of prefix string
2028 * @param max Max width
2029 * @param wraplen Column to wrap at
2030 * @param pfx Prefix for header
2031 * @param start Start of header line
2032 * @param end End of header line
2033 * @param chflags Flags, see #CopyHeaderFlags
2035 * @retval -1 Failure
2037 static int write_one_header(FILE *fp, int pfxw, int max, int wraplen, const char *pfx,
2038 const char *start, const char *end, CopyHeaderFlags chflags)
2040 char *tagbuf = NULL, *valbuf = NULL, *t = NULL;
2041 bool is_from = ((end - start) > 5) && mutt_str_startswith(start, "from ", CASE_IGNORE);
2043 /* only pass through folding machinery if necessary for sending,
2044 * never wrap From_ headers on sending */
2045 if (!(chflags & CH_DISPLAY) && ((pfxw + max <= wraplen) || is_from))
2047 valbuf = mutt_str_substr_dup(start, end);
2048 mutt_debug(5, "buf[%s%s] short enough, max width = %d <= %d\n", NONULL(pfx),
2049 valbuf, max, wraplen);
2052 if (fputs(pfx, fp) == EOF)
2059 t = strchr(valbuf, ':');
2062 mutt_debug(LL_DEBUG1, "#1 warning: header not in 'key: value' format!\n");
2066 if (print_val(fp, pfx, valbuf, chflags, mutt_str_strlen(pfx)) < 0)
2075 t = strchr(start, ':');
2076 if (!t || (t > end))
2078 mutt_debug(LL_DEBUG1, "#2 warning: header not in 'key: value' format!\n");
2084 valbuf = mutt_str_substr_dup(start, end);
2088 tagbuf = mutt_str_substr_dup(start, t);
2089 /* skip over the colon separating the header field name and value */
2092 /* skip over any leading whitespace (WSP, as defined in RFC5322)
2093 * NOTE: mutt_str_skip_email_wsp() does the wrong thing here.
2094 * See tickets 3609 and 3716. */
2095 while ((*t == ' ') || (*t == '\t'))
2098 valbuf = mutt_str_substr_dup(t, end);
2100 mutt_debug(LL_DEBUG2, "buf[%s%s] too long, max width = %d > %d\n",
2101 NONULL(pfx), NONULL(valbuf), max, wraplen);
2102 if (fold_one_header(fp, tagbuf, valbuf, pfx, wraplen, chflags) < 0)
2115 * mutt_write_one_header - Write one header line to a file
2116 * @param fp File to write to
2117 * @param tag Header key, e.g. "From"
2118 * @param value Header value
2119 * @param pfx Prefix for header
2120 * @param wraplen Column to wrap at
2121 * @param chflags Flags, see #CopyHeaderFlags
2123 * @retval -1 Failure
2125 * split several headers into individual ones and call write_one_header
2128 int mutt_write_one_header(FILE *fp, const char *tag, const char *value,
2129 const char *pfx, int wraplen, CopyHeaderFlags chflags)
2131 char *p = (char *) value, *last = NULL, *line = NULL;
2132 int max = 0, w, rc = -1;
2133 int pfxw = mutt_strwidth(pfx);
2134 char *v = mutt_str_strdup(value);
2135 bool display = (chflags & CH_DISPLAY);
2137 if (!display || C_Weed)
2138 v = unfold_header(v);
2140 /* when not displaying, use sane wrap value */
2143 if ((C_WrapHeaders < 78) || (C_WrapHeaders > 998))
2146 wraplen = C_WrapHeaders;
2148 else if ((wraplen <= 0) || (wraplen > MuttIndexWindow->cols))
2149 wraplen = MuttIndexWindow->cols;
2153 /* if header is short enough, simply print it */
2154 if (!display && (mutt_strwidth(tag) + 2 + pfxw + mutt_strwidth(v) <= wraplen))
2156 mutt_debug(5, "buf[%s%s: %s] is short enough\n", NONULL(pfx), tag, v);
2157 if (fprintf(fp, "%s%s: %s\n", NONULL(pfx), tag, v) <= 0)
2164 rc = fold_one_header(fp, tag, v, pfx, wraplen, chflags);
2174 p = strchr(p, '\n');
2176 /* find maximum line width in current header */
2179 w = mutt_mb_width(line, 0, display);
2189 if ((*p != ' ') && (*p != '\t'))
2191 if (write_one_header(fp, pfxw, max, wraplen, pfx, last, p, chflags) < 0)
2199 if (write_one_header(fp, pfxw, max, wraplen, pfx, last, p, chflags) < 0)
2210 * mutt_rfc822_write_header - Write out one RFC822 header line
2211 * @param fp File to write to
2212 * @param env Envelope of email
2213 * @param attach Attachment
2214 * @param mode Mode, see #MuttWriteHeaderMode
2215 * @param privacy If true, remove headers that might identify the user
2216 * @param hide_protected_subject If true, replace subject header
2218 * @retval -1 Failure
2220 * Note: all RFC2047 encoding should be done outside of this routine, except
2221 * for the "real name." This will allow this routine to be used more than
2222 * once, if necessary.
2224 * Likewise, all IDN processing should happen outside of this routine.
2226 * privacy true => will omit any headers which may identify the user.
2227 * Output generated is suitable for being sent through
2228 * anonymous remailer chains.
2230 * hide_protected_subject: replaces the Subject header with
2231 * $crypt_protected_headers_subject in NORMAL or POSTPONE mode.
2233 int mutt_rfc822_write_header(FILE *fp, struct Envelope *env,
2234 struct Body *attach, enum MuttWriteHeaderMode mode,
2235 bool privacy, bool hide_protected_subject)
2238 char *p = NULL, *q = NULL;
2239 bool has_agent = false; /* user defined user-agent header field exists */
2241 if ((mode == MUTT_WRITE_HEADER_NORMAL) && !privacy)
2242 fputs(mutt_date_make_date(buf, sizeof(buf)), fp);
2244 /* UseFrom is not consulted here so that we can still write a From:
2245 * field if the user sets it with the 'my_hdr' command */
2246 if (env->from && !privacy)
2249 mutt_addr_write(buf, sizeof(buf), env->from, false);
2250 fprintf(fp, "From: %s\n", buf);
2253 if (env->sender && !privacy)
2256 mutt_addr_write(buf, sizeof(buf), env->sender, false);
2257 fprintf(fp, "Sender: %s\n", buf);
2263 mutt_write_address_list(env->to, fp, 4, 0);
2265 else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
2274 mutt_write_address_list(env->cc, fp, 4, 0);
2276 else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
2284 if ((mode == MUTT_WRITE_HEADER_POSTPONE) || (mode == MUTT_WRITE_HEADER_EDITHDRS) ||
2285 ((mode == MUTT_WRITE_HEADER_NORMAL) && C_WriteBcc))
2288 mutt_write_address_list(env->bcc, fp, 5, 0);
2291 else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
2295 fputs("Bcc:\n", fp);
2298 if (env->newsgroups)
2299 fprintf(fp, "Newsgroups: %s\n", env->newsgroups);
2300 else if ((mode == MUTT_WRITE_HEADER_EDITHDRS) && OptNewsSend)
2301 fputs("Newsgroups:\n", fp);
2303 if (env->followup_to)
2304 fprintf(fp, "Followup-To: %s\n", env->followup_to);
2305 else if ((mode == MUTT_WRITE_HEADER_EDITHDRS) && OptNewsSend)
2306 fputs("Followup-To:\n", fp);
2308 if (env->x_comment_to)
2309 fprintf(fp, "X-Comment-To: %s\n", env->x_comment_to);
2310 else if ((mode == MUTT_WRITE_HEADER_EDITHDRS) && OptNewsSend && C_XCommentTo)
2311 fputs("X-Comment-To:\n", fp);
2316 if (hide_protected_subject &&
2317 ((mode == MUTT_WRITE_HEADER_NORMAL) || (mode == MUTT_WRITE_HEADER_POSTPONE)))
2318 mutt_write_one_header(fp, "Subject", C_CryptProtectedHeadersSubject, NULL, 0, CH_NO_FLAGS);
2320 mutt_write_one_header(fp, "Subject", env->subject, NULL, 0, CH_NO_FLAGS);
2322 else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
2323 fputs("Subject:\n", fp);
2325 /* save message id if the user has set it */
2326 if (env->message_id && !privacy)
2327 fprintf(fp, "Message-ID: %s\n", env->message_id);
2331 fputs("Reply-To: ", fp);
2332 mutt_write_address_list(env->reply_to, fp, 10, 0);
2334 else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
2335 fputs("Reply-To:\n", fp);
2337 if (env->mail_followup_to)
2342 fputs("Mail-Followup-To: ", fp);
2343 mutt_write_address_list(env->mail_followup_to, fp, 18, 0);
2346 if ((mode == MUTT_WRITE_HEADER_NORMAL) || (mode == MUTT_WRITE_HEADER_POSTPONE))
2348 if (!STAILQ_EMPTY(&env->references))
2350 fputs("References:", fp);
2351 mutt_write_references(&env->references, fp, 10);
2355 /* Add the MIME headers */
2356 fputs("MIME-Version: 1.0\n", fp);
2357 mutt_write_mime_header(attach, fp);
2360 if (!STAILQ_EMPTY(&env->in_reply_to))
2362 fputs("In-Reply-To:", fp);
2363 mutt_write_references(&env->in_reply_to, fp, 0);
2367 /* Add any user defined headers */
2368 struct ListNode *tmp = NULL;
2369 STAILQ_FOREACH(tmp, &env->userhdrs, entries)
2371 p = strchr(tmp->data, ':');
2378 p = mutt_str_skip_email_wsp(p + 1);
2382 continue; /* don't emit empty fields. */
2385 /* check to see if the user has overridden the user-agent field */
2386 if (mutt_str_startswith(tmp->data, "user-agent", CASE_IGNORE))
2396 mutt_write_one_header(fp, tmp->data, p, NULL, 0, CH_NO_FLAGS);
2401 if ((mode == MUTT_WRITE_HEADER_NORMAL) && !privacy && C_UserAgent && !has_agent)
2403 /* Add a vanity header */
2404 fprintf(fp, "User-Agent: NeoMutt/%s%s\n", PACKAGE_VERSION, GitVer);
2407 return (ferror(fp) == 0) ? 0 : -1;
2411 * encode_headers - RFC2047-encode a list of headers
2412 * @param h String List of headers
2414 * The strings are encoded in-place.
2416 static void encode_headers(struct ListHead *h)
2422 struct ListNode *np = NULL;
2423 STAILQ_FOREACH(np, h, entries)
2425 p = strchr(np->data, ':');
2430 p = mutt_str_skip_email_wsp(p + 1);
2431 tmp = mutt_str_strdup(p);
2436 rfc2047_encode(&tmp, NULL, i + 2, C_SendCharset);
2437 mutt_mem_realloc(&np->data, i + 2 + mutt_str_strlen(tmp) + 1);
2439 sprintf(np->data + i + 2, "%s", tmp);
2446 * mutt_fqdn - Get the Fully-Qualified Domain Name
2447 * @param may_hide_host If true, hide the hostname (leaving just the domain)
2448 * @retval ptr string pointer into Hostname
2449 * @retval NULL Error, e.g no Hostname
2451 * @warning Do not free the returned pointer
2453 const char *mutt_fqdn(bool may_hide_host)
2455 if (!C_Hostname || (C_Hostname[0] == '@'))
2458 char *p = C_Hostname;
2460 if (may_hide_host && C_HiddenHost)
2462 p = strchr(C_Hostname, '.');
2466 // sanity check: don't hide the host if the fqdn is something like example.com
2467 if (!p || !strchr(p, '.'))
2475 * gen_msgid - Generate a unique Message ID
2476 * @retval ptr Message ID
2478 * @note The caller should free the string
2480 static char *gen_msgid(void)
2483 unsigned char rndid[MUTT_RANDTAG_LEN + 1];
2485 mutt_rand_base32(rndid, sizeof(rndid) - 1);
2486 rndid[MUTT_RANDTAG_LEN] = 0;
2487 const char *fqdn = mutt_fqdn(false);
2489 fqdn = NONULL(ShortHostname);
2491 struct tm tm = mutt_date_gmtime(MUTT_DATE_NOW);
2492 snprintf(buf, sizeof(buf), "<%d%02d%02d%02d%02d%02d.%s@%s>", tm.tm_year + 1900,
2493 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, rndid, fqdn);
2494 return mutt_str_strdup(buf);
2498 * alarm_handler - Async notification of an alarm signal
2499 * @param sig Signal, (SIGALRM)
2501 static void alarm_handler(int sig)
2507 * send_msg - invoke sendmail in a subshell
2508 * @param[in] path Path to program to execute
2509 * @param[in] args Arguments to pass to program
2510 * @param[in] msg Temp file containing message to send
2511 * @param[out] tempfile If sendmail is put in the background, this points
2512 * to the temporary file containing the stdout of the
2513 * child process. If it is NULL, stderr and stdout
2514 * are not redirected.
2516 * @retval >0 Failure, return code from sendmail
2518 static int send_msg(const char *path, char **args, const char *msg, char **tempfile)
2523 mutt_sig_block_system();
2526 /* we also don't want to be stopped right now */
2527 sigaddset(&set, SIGTSTP);
2528 sigprocmask(SIG_BLOCK, &set, NULL);
2530 if ((C_SendmailWait >= 0) && tempfile)
2534 mutt_mktemp(tmp, sizeof(tmp));
2535 *tempfile = mutt_str_strdup(tmp);
2541 struct sigaction act, oldalrm;
2543 /* save parent's ID before setsid() */
2544 pid_t ppid = getppid();
2546 /* we want the delivery to continue even after the main process dies,
2547 * so we put ourselves into another session right away */
2550 /* next we close all open files */
2553 for (int fd = tempfile ? 1 : 3; fd < OPEN_MAX; fd++)
2555 #elif defined(_POSIX_OPEN_MAX)
2556 for (int fd = tempfile ? 1 : 3; fd < _POSIX_OPEN_MAX; fd++)
2566 /* now the second fork() */
2570 /* "msg" will be opened as stdin */
2571 if (open(msg, O_RDONLY, 0) < 0)
2578 if ((C_SendmailWait >= 0) && tempfile && *tempfile)
2580 /* *tempfile will be opened as stdout */
2581 if (open(*tempfile, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0600) < 0)
2583 /* redirect stderr to *tempfile too */
2589 if (open("/dev/null", O_WRONLY | O_APPEND) < 0) /* stdout */
2591 if (open("/dev/null", O_RDWR | O_APPEND) < 0) /* stderr */
2595 /* execvpe is a glibc extension */
2596 /* execvpe (path, args, mutt_envlist_getlist()); */
2607 /* C_SendmailWait > 0: interrupt waitpid() after C_SendmailWait seconds
2608 * C_SendmailWait = 0: wait forever
2609 * C_SendmailWait < 0: don't wait */
2610 if (C_SendmailWait > 0)
2613 act.sa_handler = alarm_handler;
2615 /* need to make sure waitpid() is interrupted on SIGALRM */
2616 act.sa_flags = SA_INTERRUPT;
2620 sigemptyset(&act.sa_mask);
2621 sigaction(SIGALRM, &act, &oldalrm);
2622 alarm(C_SendmailWait);
2624 else if (C_SendmailWait < 0)
2625 _exit(0xff & EX_OK);
2627 if (waitpid(pid, &st, 0) > 0)
2629 st = WIFEXITED(st) ? WEXITSTATUS(st) : S_ERR;
2630 if (C_SendmailWait && (st == (0xff & EX_OK)) && tempfile && *tempfile)
2632 unlink(*tempfile); /* no longer needed */
2638 st = ((C_SendmailWait > 0) && (errno == EINTR) && SigAlrm) ? S_BKG : S_ERR;
2639 if ((C_SendmailWait > 0) && tempfile && *tempfile)
2646 if (C_SendmailWait > 0)
2648 /* reset alarm; not really needed, but... */
2650 sigaction(SIGALRM, &oldalrm, NULL);
2653 if ((kill(ppid, 0) == -1) && (errno == ESRCH) && tempfile && *tempfile)
2655 /* the parent is already dead */
2663 sigprocmask(SIG_UNBLOCK, &set, NULL);
2665 if ((pid != -1) && (waitpid(pid, &st, 0) > 0))
2666 st = WIFEXITED(st) ? WEXITSTATUS(st) : S_ERR; /* return child status */
2668 st = S_ERR; /* error */
2670 mutt_sig_unblock_system(true);
2676 * add_args - Add an Address to a dynamic array
2677 * @param[out] args Array to add to
2678 * @param[out] argslen Number of entries in array
2679 * @param[out] argsmax Allocated size of the array
2680 * @param[in] addr Address to add
2681 * @retval ptr Updated array
2683 static char **add_args(char **args, size_t *argslen, size_t *argsmax, struct Address *addr)
2685 for (; addr; addr = addr->next)
2687 /* weed out group mailboxes, since those are for display only */
2688 if (addr->mailbox && !addr->group)
2690 if (*argslen == *argsmax)
2691 mutt_mem_realloc(&args, (*argsmax += 5) * sizeof(char *));
2692 args[(*argslen)++] = addr->mailbox;
2699 * add_option - Add a string to a dynamic array
2700 * @param[out] args Array to add to
2701 * @param[out] argslen Number of entries in array
2702 * @param[out] argsmax Allocated size of the array
2703 * @param[in] s string to add
2704 * @retval ptr Updated array
2706 * @note The array may be realloc()'d
2708 static char **add_option(char **args, size_t *argslen, size_t *argsmax, char *s)
2710 if (*argslen == *argsmax)
2711 mutt_mem_realloc(&args, (*argsmax += 5) * sizeof(char *));
2712 args[(*argslen)++] = s;
2717 * mutt_invoke_sendmail - Run sendmail
2718 * @param from The sender
2719 * @param to Recipients
2720 * @param cc Recipients
2721 * @param bcc Recipients
2722 * @param msg File containing message
2723 * @param eightbit Message contains 8bit chars
2725 * @retval -1 Failure
2727 int mutt_invoke_sendmail(struct Address *from, struct Address *to, struct Address *cc,
2728 struct Address *bcc, const char *msg, int eightbit)
2730 char *ps = NULL, *path = NULL, *s = NULL, *childout = NULL;
2732 size_t argslen = 0, argsmax = 0;
2733 char **extra_args = NULL;
2741 mutt_expando_format(cmd, sizeof(cmd), 0, MuttIndexWindow->cols,
2742 NONULL(C_Inews), nntp_format_str, 0, MUTT_FORMAT_NO_FLAGS);
2745 i = nntp_post(Context->mailbox, msg);
2750 s = mutt_str_strdup(cmd);
2754 s = mutt_str_strdup(C_Sendmail);
2756 /* ensure that $sendmail is set to avoid a crash. http://dev.mutt.org/trac/ticket/3548 */
2759 mutt_error(_("$sendmail must be set in order to send mail"));
2765 while ((ps = strtok(ps, " ")))
2767 if (argslen == argsmax)
2768 mutt_mem_realloc(&args, sizeof(char *) * (argsmax += 5));
2772 if (mutt_str_strcmp(ps, "--") == 0)
2774 args[argslen++] = ps;
2778 path = mutt_str_strdup(ps);
2779 ps = strrchr(ps, '/');
2784 args[argslen++] = ps;
2794 size_t extra_argslen = 0;
2795 /* If C_Sendmail contained a "--", we save the recipients to append to
2796 * args after other possible options added below. */
2800 size_t extra_argsmax = 0;
2801 while ((ps = strtok(ps, " ")))
2803 if (extra_argslen == extra_argsmax)
2804 mutt_mem_realloc(&extra_args, sizeof(char *) * (extra_argsmax += 5));
2806 extra_args[extra_argslen++] = ps;
2811 if (eightbit && C_Use8bitmime)
2812 args = add_option(args, &argslen, &argsmax, "-B8BITMIME");
2814 if (C_UseEnvelopeFrom)
2816 if (C_EnvelopeFromAddress)
2818 args = add_option(args, &argslen, &argsmax, "-f");
2819 args = add_args(args, &argslen, &argsmax, C_EnvelopeFromAddress);
2821 else if (from && !from->next)
2823 args = add_option(args, &argslen, &argsmax, "-f");
2824 args = add_args(args, &argslen, &argsmax, from);
2830 args = add_option(args, &argslen, &argsmax, "-N");
2831 args = add_option(args, &argslen, &argsmax, C_DsnNotify);
2835 args = add_option(args, &argslen, &argsmax, "-R");
2836 args = add_option(args, &argslen, &argsmax, C_DsnReturn);
2838 args = add_option(args, &argslen, &argsmax, "--");
2839 for (i = 0; i < extra_argslen; i++)
2840 args = add_option(args, &argslen, &argsmax, extra_args[i]);
2841 args = add_args(args, &argslen, &argsmax, to);
2842 args = add_args(args, &argslen, &argsmax, cc);
2843 args = add_args(args, &argslen, &argsmax, bcc);
2848 if (argslen == argsmax)
2849 mutt_mem_realloc(&args, sizeof(char *) * (++argsmax));
2851 args[argslen++] = NULL;
2853 /* Some user's $sendmail command uses gpg for password decryption,
2854 * and is set up to prompt using ncurses pinentry. If we
2855 * mutt_endwin() it leaves other users staring at a blank screen.
2856 * So instead, just force a hard redraw on the next refresh. */
2858 mutt_need_hard_redraw();
2860 i = send_msg(path, args, msg, OptNoCurses ? NULL : &childout);
2861 if (i != (EX_OK & 0xff))
2865 const char *e = mutt_str_sysexit(i);
2866 mutt_error(_("Error sending message, child exited %d (%s)"), i, NONULL(e));
2871 if ((stat(childout, &st) == 0) && (st.st_size > 0))
2872 mutt_do_pager(_("Output of the delivery process"), childout,
2873 MUTT_PAGER_NO_FLAGS, NULL);
2886 if (i == (EX_OK & 0xff))
2888 else if (i == S_BKG)
2896 * mutt_prepare_envelope - Prepare an email header
2897 * @param env Envelope to prepare
2898 * @param final true if this email is going to be sent (not postponed)
2900 * Encode all the headers prior to sending the email.
2902 * For postponing (!final) do the necessary encodings only
2904 void mutt_prepare_envelope(struct Envelope *env, bool final)
2908 if (env->bcc && !(env->to || env->cc))
2910 /* some MTA's will put an Apparently-To: header field showing the Bcc:
2911 * recipients if there is no To: or Cc: field, so attempt to suppress
2912 * it by using an empty To: field. */
2913 env->to = mutt_addr_new();
2915 env->to->next = mutt_addr_new();
2919 mutt_addr_cat(buf, sizeof(buf), "undisclosed-recipients", AddressSpecials);
2921 env->to->mailbox = mutt_str_strdup(buf);
2924 mutt_set_followup_to(env);
2926 if (!env->message_id)
2927 env->message_id = gen_msgid();
2930 /* Take care of 8-bit => 7-bit conversion. */
2931 rfc2047_encode_envelope(env);
2932 encode_headers(&env->userhdrs);
2936 * mutt_unprepare_envelope - Undo the encodings of mutt_prepare_envelope()
2937 * @param env Envelope to unprepare
2939 * Decode all the headers of an email, e.g. when the sending failed or was
2942 void mutt_unprepare_envelope(struct Envelope *env)
2944 struct ListNode *item = NULL;
2945 STAILQ_FOREACH(item, &env->userhdrs, entries)
2947 rfc2047_decode(&item->data);
2950 mutt_addr_free(&env->mail_followup_to);
2952 /* back conversions */
2953 rfc2047_decode_envelope(env);
2957 * bounce_message - Bounce an email message
2958 * @param fp Handle of message
2960 * @param to Address to bounce to
2961 * @param resent_from Address of new sender
2962 * @param env_from Envelope of original sender
2964 * @retval -1 Failure
2966 static int bounce_message(FILE *fp, struct Email *e, struct Address *to,
2967 const char *resent_from, struct Address *env_from)
2973 char tempfile[PATH_MAX];
2975 mutt_mktemp(tempfile, sizeof(tempfile));
2976 FILE *fp_tmp = mutt_file_fopen(tempfile, "w");
2980 CopyHeaderFlags chflags = CH_XMIT | CH_NONEWLINE | CH_NOQFROM;
2982 if (!C_BounceDelivered)
2983 chflags |= CH_WEED_DELIVERED;
2985 fseeko(fp, e->offset, SEEK_SET);
2986 fprintf(fp_tmp, "Resent-From: %s", resent_from);
2987 fprintf(fp_tmp, "\nResent-%s", mutt_date_make_date(date, sizeof(date)));
2988 char *msgid_str = gen_msgid();
2989 fprintf(fp_tmp, "Resent-Message-ID: %s\n", msgid_str);
2991 fputs("Resent-To: ", fp_tmp);
2992 mutt_write_address_list(to, fp_tmp, 11, 0);
2993 mutt_copy_header(fp, e, fp_tmp, chflags, NULL);
2994 fputc('\n', fp_tmp);
2995 mutt_file_copy_bytes(fp, fp_tmp, e->content->length);
2996 if (mutt_file_fclose(&fp_tmp) != 0)
2998 mutt_perror(tempfile);
3004 rc = mutt_smtp_send(env_from, to, NULL, NULL, tempfile, e->content->encoding == ENC_8BIT);
3007 rc = mutt_invoke_sendmail(env_from, to, NULL, NULL, tempfile,
3008 e->content->encoding == ENC_8BIT);
3015 * mutt_bounce_message - Bounce an email message
3016 * @param fp Handle of message
3018 * @param to Address to bounce to
3020 * @retval -1 Failure
3022 int mutt_bounce_message(FILE *fp, struct Email *e, struct Address *to)
3024 if (!fp || !e || !to)
3027 const char *fqdn = mutt_fqdn(true);
3028 char resent_from[256];
3031 resent_from[0] = '\0';
3032 struct Address *from = mutt_default_from();
3034 /* mutt_default_from() does not use $realname if the real name is not set
3035 * in $from, so we add it here. The reason it is not added in
3036 * mutt_default_from() is that during normal sending, we execute
3037 * send-hooks and set the realname last so that it can be changed based
3038 * upon message criteria. */
3039 if (!from->personal)
3040 from->personal = mutt_str_strdup(C_Realname);
3043 mutt_addr_qualify(from, fqdn);
3045 rfc2047_encode_addrlist(from, "Resent-From");
3046 if (mutt_addrlist_to_intl(from, &err))
3048 mutt_error(_("Bad IDN %s while preparing resent-from"), err);
3050 mutt_addr_free(&from);
3053 mutt_addr_write(resent_from, sizeof(resent_from), from, false);
3056 OptNewsSend = false;
3059 /* prepare recipient list. idna conversion appears to happen before this
3060 * function is called, since the user receives confirmation of the address
3061 * list being bounced to. */
3062 struct Address *resent_to = mutt_addr_copy_list(to, false);
3063 rfc2047_encode_addrlist(resent_to, "Resent-To");
3065 int rc = bounce_message(fp, e, resent_to, resent_from, from);
3067 mutt_addr_free(&resent_to);
3068 mutt_addr_free(&from);
3074 * set_noconv_flags - Set/reset the "x-mutt-noconv" flag
3075 * @param b Body of email
3076 * @param flag If true, set the flag, otherwise remove it
3078 static void set_noconv_flags(struct Body *b, bool flag)
3080 for (; b; b = b->next)
3082 if ((b->type == TYPE_MESSAGE) || (b->type == TYPE_MULTIPART))
3083 set_noconv_flags(b->parts, flag);
3084 else if ((b->type == TYPE_TEXT) && b->noconv)
3087 mutt_param_set(&b->parameter, "x-mutt-noconv", "yes");
3089 mutt_param_delete(&b->parameter, "x-mutt-noconv");
3095 * mutt_write_multiple_fcc - Handle FCC with multiple, comma separated entries
3096 * @param[in] path Path to mailboxes (comma separated)
3097 * @param[in] e Email
3098 * @param[in] msgid Message id
3099 * @param[in] post If true, postpone message
3100 * @param[in] fcc fcc setting to save (postpone only)
3101 * @param[out] finalpath Final path of email
3103 * @retval -1 Failure
3105 int mutt_write_multiple_fcc(const char *path, struct Email *e, const char *msgid,
3106 bool post, char *fcc, char **finalpath)
3108 char fcc_tok[PATH_MAX];
3109 char fcc_expanded[PATH_MAX];
3111 mutt_str_strfcpy(fcc_tok, path, sizeof(fcc_tok));
3113 char *tok = strtok(fcc_tok, ",");
3117 mutt_debug(LL_DEBUG1, "Fcc: initial mailbox = '%s'\n", tok);
3118 /* mutt_expand_path already called above for the first token */
3119 int status = mutt_write_fcc(tok, e, msgid, post, fcc, finalpath);
3123 while ((tok = strtok(NULL, ",")))
3128 /* Only call mutt_expand_path if tok has some data */
3129 mutt_debug(LL_DEBUG1, "Fcc: additional mailbox token = '%s'\n", tok);
3130 mutt_str_strfcpy(fcc_expanded, tok, sizeof(fcc_expanded));
3131 mutt_expand_path(fcc_expanded, sizeof(fcc_expanded));
3132 mutt_debug(LL_DEBUG1, " Additional mailbox expanded = '%s'\n", fcc_expanded);
3133 status = mutt_write_fcc(fcc_expanded, e, msgid, post, fcc, finalpath);
3142 * mutt_write_fcc - Write email to FCC mailbox
3143 * @param[in] path Path to mailbox
3144 * @param[in] e Email
3145 * @param[in] msgid Message id
3146 * @param[in] post If true, postpone message
3147 * @param[in] fcc fcc setting to save (postpone only)
3148 * @param[out] finalpath Final path of email
3150 * @retval -1 Failure
3152 int mutt_write_fcc(const char *path, struct Email *e, const char *msgid,
3153 bool post, char *fcc, char **finalpath)
3155 struct Message *msg = NULL;
3156 char tempfile[PATH_MAX];
3157 FILE *fp_tmp = NULL;
3159 bool need_mailbox_cleanup = false;
3162 MsgOpenFlags onm_flags;
3165 set_noconv_flags(e->content, true);
3167 #ifdef RECORD_FOLDER_HOOK
3168 mutt_folder_hook(path, NULL);
3170 struct Mailbox *m_fcc = mx_path_resolve(path);
3171 bool old_append = m_fcc->append;
3172 struct Context *ctx_fcc = mx_mbox_open(m_fcc, MUTT_APPEND | MUTT_QUIET);
3175 mutt_debug(LL_DEBUG1, "unable to open mailbox %s in append-mode, aborting\n", path);
3176 mailbox_free(&m_fcc);
3180 /* We need to add a Content-Length field to avoid problems where a line in
3181 * the message body begins with "From " */
3182 if ((ctx_fcc->mailbox->magic == MUTT_MMDF) || (ctx_fcc->mailbox->magic == MUTT_MBOX))
3184 mutt_mktemp(tempfile, sizeof(tempfile));
3185 fp_tmp = mutt_file_fopen(tempfile, "w+");
3188 mutt_perror(tempfile);
3189 mx_mbox_close(&ctx_fcc);
3192 /* remember new mail status before appending message */
3193 need_mailbox_cleanup = true;
3197 e->read = !post; /* make sure to put it in the 'cur' directory (maildir) */
3198 onm_flags = MUTT_ADD_FROM;
3200 onm_flags |= MUTT_SET_DRAFT;
3201 msg = mx_msg_open_new(ctx_fcc->mailbox, e, onm_flags);
3204 mutt_file_fclose(&fp_tmp);
3205 mx_mbox_close(&ctx_fcc);
3209 /* post == 1 => postpone message.
3210 * post == 0 => Normal mode. */
3211 mutt_rfc822_write_header(
3212 msg->fp, e->env, e->content, post ? MUTT_WRITE_HEADER_POSTPONE : MUTT_WRITE_HEADER_NORMAL,
3213 false, C_CryptProtectedHeadersRead && mutt_should_hide_protected_subject(e));
3215 /* (postponement) if this was a reply of some sort, <msgid> contains the
3216 * Message-ID: of message replied to. Save it using a special X-Mutt-
3217 * header so it can be picked up if the message is recalled at a later
3218 * point in time. This will allow the message to be marked as replied if
3219 * the same mailbox is still open. */
3221 fprintf(msg->fp, "X-Mutt-References: %s\n", msgid);
3223 /* (postponement) save the Fcc: using a special X-Mutt- header so that
3224 * it can be picked up when the message is recalled */
3226 fprintf(msg->fp, "X-Mutt-Fcc: %s\n", fcc);
3228 if ((ctx_fcc->mailbox->magic == MUTT_MMDF) || (ctx_fcc->mailbox->magic == MUTT_MBOX))
3229 fprintf(msg->fp, "Status: RO\n");
3231 /* mutt_rfc822_write_header() only writes out a Date: header with
3232 * mode == 0, i.e. _not_ postponement; so write out one ourself */
3234 fprintf(msg->fp, "%s", mutt_date_make_date(buf, sizeof(buf)));
3236 /* (postponement) if the mail is to be signed or encrypted, save this info */
3237 if (((WithCrypto & APPLICATION_PGP) != 0) && post && (e->security & APPLICATION_PGP))
3239 fputs("X-Mutt-PGP: ", msg->fp);
3240 if (e->security & SEC_ENCRYPT)
3241 fputc('E', msg->fp);
3242 if (e->security & SEC_OPPENCRYPT)
3243 fputc('O', msg->fp);
3244 if (e->security & SEC_SIGN)
3246 fputc('S', msg->fp);
3247 if (C_PgpSignAs && *C_PgpSignAs)
3248 fprintf(msg->fp, "<%s>", C_PgpSignAs);
3250 if (e->security & SEC_INLINE)
3251 fputc('I', msg->fp);
3252 fputc('\n', msg->fp);
3255 /* (postponement) if the mail is to be signed or encrypted, save this info */
3256 if (((WithCrypto & APPLICATION_SMIME) != 0) && post && (e->security & APPLICATION_SMIME))
3258 fputs("X-Mutt-SMIME: ", msg->fp);
3259 if (e->security & SEC_ENCRYPT)
3261 fputc('E', msg->fp);
3262 if (C_SmimeEncryptWith && *C_SmimeEncryptWith)
3263 fprintf(msg->fp, "C<%s>", C_SmimeEncryptWith);
3265 if (e->security & SEC_OPPENCRYPT)
3266 fputc('O', msg->fp);
3267 if (e->security & SEC_SIGN)
3269 fputc('S', msg->fp);
3270 if (C_SmimeSignAs && *C_SmimeSignAs)
3271 fprintf(msg->fp, "<%s>", C_SmimeSignAs);
3273 if (e->security & SEC_INLINE)
3274 fputc('I', msg->fp);
3275 fputc('\n', msg->fp);
3279 /* (postponement) if the mail is to be sent through a mixmaster
3280 * chain, save that information */
3282 if (post && !STAILQ_EMPTY(&e->chain))
3284 fputs("X-Mutt-Mix:", msg->fp);
3285 struct ListNode *p = NULL;
3286 STAILQ_FOREACH(p, &e->chain, entries)
3288 fprintf(msg->fp, " %s", (char *) p->data);
3291 fputc('\n', msg->fp);
3297 mutt_write_mime_body(e->content, fp_tmp);
3299 /* make sure the last line ends with a newline. Emacs doesn't ensure this
3300 * will happen, and it can cause problems parsing the mailbox later. */
3301 fseek(fp_tmp, -1, SEEK_END);
3302 if (fgetc(fp_tmp) != '\n')
3304 fseek(fp_tmp, 0, SEEK_END);
3305 fputc('\n', fp_tmp);
3311 mutt_debug(LL_DEBUG1, "%s: write failed\n", tempfile);
3312 mutt_file_fclose(&fp_tmp);
3314 mx_msg_commit(ctx_fcc->mailbox, msg); /* XXX really? */
3315 mx_msg_close(ctx_fcc->mailbox, &msg);
3316 mx_mbox_close(&ctx_fcc);
3320 /* count the number of lines */
3322 char line_buf[1024];
3324 while (fgets(line_buf, sizeof(line_buf), fp_tmp))
3326 fprintf(msg->fp, "Content-Length: " OFF_T_FMT "\n", (LOFF_T) ftello(fp_tmp));
3327 fprintf(msg->fp, "Lines: %d\n\n", lines);
3329 /* copy the body and clean up */
3331 rc = mutt_file_copy_stream(fp_tmp, msg->fp);
3332 if (fclose(fp_tmp) != 0)
3334 /* if there was an error, leave the temp version */
3340 fputc('\n', msg->fp); /* finish off the header */
3341 rc = mutt_write_mime_body(e->content, msg->fp);
3344 if (mx_msg_commit(ctx_fcc->mailbox, msg) != 0)
3347 *finalpath = mutt_str_strdup(msg->committed_path);
3348 mx_msg_close(ctx_fcc->mailbox, &msg);
3349 mx_mbox_close(&ctx_fcc);
3351 if (!post && need_mailbox_cleanup)
3352 mutt_mailbox_cleanup(path, &st);
3355 set_noconv_flags(e->content, false);
3358 m_fcc->append = old_append;
3359 #ifdef RECORD_FOLDER_HOOK
3360 /* We ran a folder hook for the destination mailbox,
3361 * now we run it for the user's current mailbox */
3362 if (Context && Context->mailbox->path)
3363 mutt_folder_hook(Context->mailbox->path, Context->mailbox->desc);