+ set_disposition (ct);
+
+ add_param(&ct->c_dispo_first, &ct->c_dispo_last, "filename", simplename, 0);
+}
+
+/*
+ * If disposition type hasn't already been set in ct:
+ * Look for mhbuild-disposition-<type>/<subtype> entry
+ * that specifies Content-Disposition type. Only
+ * 'attachment' and 'inline' are allowed. Default to
+ * 'attachment'.
+ */
+void
+set_disposition (CT ct) {
+ if (ct->c_dispo_type == NULL) {
+ char *cp = context_find_by_type ("disposition", ct->c_ctinfo.ci_type,
+ ct->c_ctinfo.ci_subtype);
+
+ if (cp && strcasecmp (cp, "attachment") &&
+ strcasecmp (cp, "inline")) {
+ admonish (NULL, "configuration problem: %s-disposition-%s%s%s "
+ "specifies '%s' but only 'attachment' and 'inline' are "
+ "allowed", invo_name,
+ ct->c_ctinfo.ci_type,
+ ct->c_ctinfo.ci_subtype ? "/" : "",
+ ct->c_ctinfo.ci_subtype ? ct->c_ctinfo.ci_subtype : "",
+ cp);
+ }
+
+ if (!cp)
+ cp = "attachment";
+ ct->c_dispo_type = mh_xstrdup(cp);
+ }
+}
+
+/*
+ * Set text content charset if it was unspecified. contains8bit
+ * selctions:
+ * 0: content does not contain 8-bit characters
+ * 1: content contains 8-bit characters
+ * -1: ignore content and use user's locale to determine charset
+ */
+void
+set_charset (CT ct, int contains8bit) {
+ if (ct->c_type == CT_TEXT) {
+ struct text *t;
+
+ if (ct->c_ctparams == NULL) {
+ NEW0(t);
+ ct->c_ctparams = t;
+ t->tx_charset = CHARSET_UNSPECIFIED;
+ } else {
+ t = (struct text *) ct->c_ctparams;
+ }
+
+ if (t->tx_charset == CHARSET_UNSPECIFIED) {
+ CI ci = &ct->c_ctinfo;
+ char *eightbitcharset = write_charset_8bit();
+ char *charset = contains8bit ? eightbitcharset : "us-ascii";
+
+ if (contains8bit == 1 &&
+ strcasecmp (eightbitcharset, "US-ASCII") == 0) {
+ adios (NULL, "Text content contains 8 bit characters, but "
+ "character set is US-ASCII");
+ }
+
+ add_param (&ci->ci_first_pm, &ci->ci_last_pm, "charset", charset,
+ 0);
+
+ t->tx_charset = CHARSET_SPECIFIED;
+ }
+ }
+}
+
+
+/*
+ * Look at all of the replied-to message parts and expand any that
+ * are matched by a pseudoheader. Except don't descend into
+ * message parts.
+ */
+void
+expand_pseudoheaders (CT ct, struct multipart *m, const char *infile,
+ const convert_list *convert_head) {
+ /* text_plain_ct is used to concatenate all of the text/plain
+ replies into one part, instead of having each one in a separate
+ part. */
+ CT text_plain_ct = NULL;
+
+ switch (ct->c_type) {
+ case CT_MULTIPART: {
+ struct multipart *mp = (struct multipart *) ct->c_ctparams;
+ struct part *part;
+
+ if (ct->c_subtype == MULTI_ALTERNATE) {
+ int matched = 0;
+
+ /* The parts are in descending priority order (defined by
+ RFC 2046 Sec. 5.1.4) because they were reversed by
+ parse_mime (). So, stop looking for matches with
+ immediate subparts after the first match of an
+ alternative. */
+ for (part = mp->mp_parts; ! matched && part; part = part->mp_next) {
+ char *type_subtype =
+ concat (part->mp_part->c_ctinfo.ci_type, "/",
+ part->mp_part->c_ctinfo.ci_subtype, NULL);
+
+ if (part->mp_part->c_type == CT_MULTIPART) {
+ expand_pseudoheaders (part->mp_part, m, infile,
+ convert_head);
+ } else {
+ const convert_list *c;
+
+ for (c = convert_head; c; c = c->next) {
+ if (! strcasecmp (type_subtype, c->type)) {
+ expand_pseudoheader (part->mp_part, &text_plain_ct,
+ m, infile,
+ c->type, c->argstring);
+ matched = 1;
+ break;
+ }
+ }
+ }
+ free (type_subtype);
+ }
+ } else {
+ for (part = mp->mp_parts; part; part = part->mp_next) {
+ expand_pseudoheaders (part->mp_part, m, infile, convert_head);
+ }
+ }
+ break;
+ }
+
+ default: {
+ char *type_subtype =
+ concat (ct->c_ctinfo.ci_type, "/", ct->c_ctinfo.ci_subtype,
+ NULL);
+ const convert_list *c;
+
+ for (c = convert_head; c; c = c->next) {
+ if (! strcasecmp (type_subtype, c->type)) {
+ expand_pseudoheader (ct, &text_plain_ct, m, infile, c->type,
+ c->argstring);
+ break;
+ }
+ }
+ free (type_subtype);
+ break;
+ }
+ }
+}
+
+
+/*
+ * Expand a single pseudoheader. It's for the specified type.
+ */
+void
+expand_pseudoheader (CT ct, CT *text_plain_ct, struct multipart *m,
+ const char *infile, const char *type,
+ const char *argstring) {
+ char *reply_file;
+ FILE *reply_fp = NULL;
+ char *convert, *type_p, *subtype_p;
+ char *convert_command;
+ char *charset = NULL;
+ char *cp;
+ struct str2init *s2i;
+ CT reply_ct;
+ struct part *part;
+ int eightbit = 0;
+ int status;
+
+ type_p = getcpy (type);
+ if ((subtype_p = strchr (type_p, '/'))) {
+ *subtype_p++ = '\0';
+ convert = context_find_by_type ("convert", type_p, subtype_p);
+ } else {
+ free (type_p);
+ type_p = concat ("mhbuild-convert-", type, NULL);
+ convert = context_find (type_p);
+ }
+ free (type_p);
+
+ if (! (convert)) {
+ /* No mhbuild-convert- entry in mhn.defaults or profile
+ for type. */
+ return;
+ }
+ /* reply_file is used to pass the output of the convert. */
+ reply_file = getcpy (m_mktemp2 (NULL, invo_name, NULL, NULL));
+ convert_command =
+ concat (convert, " ", argstring ? argstring : "", " >", reply_file,
+ NULL);
+
+ /* Convert here . . . */
+ ct->c_storeproc = mh_xstrdup(convert_command);
+ ct->c_umask = ~m_gmprot ();
+
+ if ((status = show_content_aux (ct, 0, convert_command, NULL, NULL)) !=
+ OK) {
+ admonish (NULL, "store of %s content failed", type);
+ }
+ free (convert_command);
+
+ /* Fill out the the new ct, reply_ct. */
+ NEW0(reply_ct);
+ init_decoded_content (reply_ct, infile);
+
+ if (extract_headers (reply_ct, reply_file, &reply_fp) == NOTOK) {
+ free (reply_file);
+ admonish (NULL,
+ "failed to extract headers from convert output in %s",
+ reply_file);
+ 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;
+ }
+ (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++) {
+ if (strcasecmp(reply_ct->c_ctinfo.ci_type, s2i->si_key) == 0) {
+ break;
+ }
+ }
+
+ if ((reply_ct->c_ctinitfnx = s2i->si_init)) {
+ (*reply_ct->c_ctinitfnx)(reply_ct);
+ }
+
+ if ((cp =
+ get_param (reply_ct->c_ctinfo.ci_first_pm, "charset", '?', 1))) {
+ /* The reply Content-Type had the charset. */
+ charset = cp;
+ } 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 &&
+ 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;
+ }
+ }
+
+ /* Concatenate text/plain parts. */
+ 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_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). 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 =
+ eightbit ? CE_8BIT : CE_7BIT;
+ }
+ }
+
+ if (*text_plain_ct) {
+ /* Only concatenate if the charsets are identical. */
+ char *text_plain_ct_charset =
+ get_param ((*text_plain_ct)->c_ctinfo.ci_first_pm, "charset",
+ '?', 1);
+
+ if (strcasecmp (text_plain_ct_charset, charset) == 0) {
+ /* Append this text/plain reply to the first one.
+ If there's a problem anywhere along the way,
+ instead attach it is a separate part. */
+ int text_plain_reply =
+ open ((*text_plain_ct)->c_cefile.ce_file,
+ O_WRONLY | O_APPEND);
+ int addl_reply = open (reply_file, O_RDONLY);
+
+ if (text_plain_reply != NOTOK && addl_reply != NOTOK) {
+ /* Insert blank line before each addl part. */
+ /* It would be nice not to do this for the first one. */
+ if (write (text_plain_reply, "\n", 1) == 1) {
+ /* Copy the text from the new reply and
+ then free its Content struct. */
+ cpydata (addl_reply, text_plain_reply,
+ (*text_plain_ct)->c_cefile.ce_file,
+ 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);
+ return;
+ }
+ }
+ }
+ }
+ } else {
+ *text_plain_ct = reply_ct;
+ }
+ }
+
+ reply_ct->c_cefile.ce_file = reply_file;
+ reply_ct->c_cefile.ce_fp = reply_fp;
+ reply_ct->c_cefile.ce_unlink = 1;
+
+ /* Attach the new part to the parent multipart/mixed, "m". */
+ NEW0(part);
+ part->mp_part = reply_ct;
+ if (m->mp_parts) {
+ struct part *p;