X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/6b073f7b67a0afd64012ff7d00818ec1bd07b5f2..41a82a7ecba04be5b27b3ffc813e2f34d2786a38:/uip/mhbuildsbr.c diff --git a/uip/mhbuildsbr.c b/uip/mhbuildsbr.c index dde5027c..3ef5022a 100644 --- a/uip/mhbuildsbr.c +++ b/uip/mhbuildsbr.c @@ -17,7 +17,6 @@ #include #include -#include #include #include #include @@ -361,15 +360,17 @@ finish_field: } m_getfld_state_destroy (&gstate); - /* - * Iterate through the list of headers and call the function to MIME-ify - * them if required. - */ + if (header_encoding != CE_8BIT) { + /* + * Iterate through the list of headers and call the function to MIME-ify + * them if required. + */ - for (hp = ct->c_first_hf; hp != NULL; hp = hp->next) { - if (encode_rfc2047(hp->name, &hp->value, header_encoding, NULL)) { - adios(NULL, "Unable to encode header \"%s\"", hp->name); - } + for (hp = ct->c_first_hf; hp != NULL; hp = hp->next) { + if (encode_rfc2047(hp->name, &hp->value, header_encoding, NULL)) { + adios(NULL, "Unable to encode header \"%s\"", hp->name); + } + } } /* @@ -1452,15 +1453,17 @@ scan_content (CT ct, size_t maxunencoded) * Decide what to check while scanning this content. Note that * for text content we always check for 8bit characters if the * charset is unspecified, because that controls whether or not the - * character set is us-ascii or retrieved from the locale. + * character set is us-ascii or retrieved from the locale. And + * we check even if the charset is specified, to allow setting + * the proper Content-Transfer-Encoding. */ if (ct->c_type == CT_TEXT) { t = (struct text *) ct->c_ctparams; if (t->tx_charset == CHARSET_UNSPECIFIED) { - check8bit = 1; checknul = 1; } + check8bit = 1; } switch (ct->c_reqencoding) { @@ -1749,8 +1752,11 @@ build_headers (CT ct, int header_encoding) if (ct->c_descr) { np = add (DESCR_FIELD, NULL); vp = concat (" ", ct->c_descr, NULL); - if (encode_rfc2047(DESCR_FIELD, &vp, header_encoding, NULL)) - adios(NULL, "Unable to encode %s header", DESCR_FIELD); + if (header_encoding != CE_8BIT) { + if (encode_rfc2047(DESCR_FIELD, &vp, header_encoding, NULL)) { + adios(NULL, "Unable to encode %s header", DESCR_FIELD); + } + } add_header (ct, np, vp); } @@ -2160,7 +2166,7 @@ expand_pseudoheaders (CT ct, struct multipart *m, const char *infile, part->mp_part->c_ctinfo.ci_subtype, NULL); if (part->mp_part->c_type == CT_MULTIPART) { - expand_pseudoheaders (part->mp_part, mp, infile, + expand_pseudoheaders (part->mp_part, m, infile, convert_head); } else { const convert_list *c; @@ -2221,6 +2227,8 @@ expand_pseudoheader (CT ct, CT *text_plain_ct, struct multipart *m, struct str2init *s2i; CT reply_ct; struct part *part; + int eightbit = 0; + int status; type_p = getcpy (type); if ((subtype_p = strchr (type_p, '/'))) { @@ -2247,7 +2255,9 @@ expand_pseudoheader (CT ct, CT *text_plain_ct, struct multipart *m, /* Convert here . . . */ ct->c_storeproc = getcpy (convert_command); ct->c_umask = ~m_gmprot (); - if (show_content_aux (ct, 0, convert_command, NULL, NULL) == NOTOK) { + + if ((status = show_content_aux (ct, 0, convert_command, NULL, NULL)) != + OK) { admonish (NULL, "store of %s content failed", type); } free (convert_command); @@ -2264,6 +2274,20 @@ expand_pseudoheader (CT ct, CT *text_plain_ct, struct multipart *m, return; } + /* For text content only, see if it is 8-bit text. */ + if (reply_ct->c_type == CT_TEXT) { + int fd; + + if ((fd = open (reply_file, O_RDONLY)) == NOTOK || + scan_input (fd, &eightbit) == NOTOK) { + free (reply_file); + admonish (NULL, "failed to read %s", reply_file); + return; + } else { + (void) close (fd); + } + } + /* This sets reply_ct->c_ctparams, and reply_ct->c_termproc if the charset can't be handled natively. */ for (s2i = str2cts; s2i->si_key; s2i++) { @@ -2283,10 +2307,15 @@ expand_pseudoheader (CT ct, CT *text_plain_ct, struct multipart *m, } else { set_charset (reply_ct, -1); charset = get_param (reply_ct->c_ctinfo.ci_first_pm, "charset", '?', 1); - if (reply_ct->c_reqencoding == CE_UNKNOWN) { - /* Assume that 8bit is sufficient (for text). */ - reply_ct->c_reqencoding = - strcasecmp (charset, "US-ASCII") ? CE_8BIT : CE_7BIT; + if (reply_ct->c_reqencoding == CE_UNKNOWN && + reply_ct->c_type == CT_TEXT) { + /* Assume that 8bit is sufficient (for text). In other words, + don't allow it to be encoded as quoted printable if lines + are too long. This also sidesteps the check for whether + it needs to be encoded as binary; instead, it relies on + the applicable mhbuild-convert-text directive to ensure + that the resultant text is not binary. */ + reply_ct->c_reqencoding = eightbit ? CE_8BIT : CE_7BIT; } } @@ -2294,15 +2323,21 @@ expand_pseudoheader (CT ct, CT *text_plain_ct, struct multipart *m, if (reply_ct->c_type == CT_TEXT && reply_ct->c_subtype == TEXT_PLAIN) { if (! *text_plain_ct && m->mp_parts && m->mp_parts->mp_part && - m->mp_parts->mp_part->c_type == CT_TEXT) { + m->mp_parts->mp_part->c_type == CT_TEXT && + m->mp_parts->mp_part->c_subtype == TEXT_PLAIN) { *text_plain_ct = m->mp_parts->mp_part; /* Make sure that the charset is set in the text/plain part. */ set_charset (*text_plain_ct, -1); if ((*text_plain_ct)->c_reqencoding == CE_UNKNOWN) { - /* Assume that 8bit is sufficient (for text). */ + /* Assume that 8bit is sufficient (for text). In other words, + don't allow it to be encoded as quoted printable if lines + are too long. This also sidesteps the check for whether + it needs to be encoded as binary; instead, it relies on + the applicable mhbuild-convert-text directive to ensure + that the resultant text is not binary. */ (*text_plain_ct)->c_reqencoding = - strcasecmp (charset, "US-ASCII") ? CE_8BIT : CE_7BIT; + eightbit ? CE_8BIT : CE_7BIT; } } @@ -2332,6 +2367,13 @@ expand_pseudoheader (CT ct, CT *text_plain_ct, struct multipart *m, reply_file); if (close (text_plain_reply) == OK && close (addl_reply) == OK) { + /* If appended text needed 8-bit but first text didn't, + propagate the 8-bit indication. */ + if ((*text_plain_ct)->c_reqencoding == CE_7BIT && + reply_ct->c_reqencoding == CE_8BIT) { + (*text_plain_ct)->c_reqencoding = CE_8BIT; + } + if (reply_fp) { fclose (reply_fp); } free (reply_file); free_content (reply_ct); @@ -2349,7 +2391,7 @@ expand_pseudoheader (CT ct, CT *text_plain_ct, struct multipart *m, reply_ct->c_cefile.ce_fp = reply_fp; reply_ct->c_cefile.ce_unlink = 1; - /* Attach the new part to the parent mulitpart/mixed, "m". */ + /* Attach the new part to the parent multipart/mixed, "m". */ part = (struct part *) mh_xcalloc (1, sizeof *part); part->mp_part = reply_ct; if (m->mp_parts) { @@ -2387,8 +2429,8 @@ extract_headers (CT ct, char *reply_file, FILE **reply_fp) { buffer[statbuf.st_size] = '\0'; /* Look for a header in the convert reply. */ - if (strncasecmp (buffer, TYPE_FIELD, sizeof TYPE_FIELD) == 0 && - buffer[sizeof TYPE_FIELD] == ':') { + if (strncasecmp (buffer, TYPE_FIELD, strlen (TYPE_FIELD)) == 0 && + buffer[strlen (TYPE_FIELD)] == ':') { if ((end_of_header = strstr (buffer, "\r\n\r\n"))) { end_of_header += 2; found_header = 1;