+ 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 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 = getcpy (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. */
+ reply_ct = (CT) mh_xcalloc (1, sizeof *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;
+ }
+
+ /* 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) {
+ /* Assume that 8bit is sufficient (for text). */
+ reply_ct->c_reqencoding =
+ strcasecmp (charset, "US-ASCII") ? 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). */
+ (*text_plain_ct)->c_reqencoding =
+ strcasecmp (charset, "US-ASCII") ? 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 (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 mulitpart/mixed, "m". */
+ part = (struct part *) mh_xcalloc (1, sizeof *part);
+ part->mp_part = reply_ct;
+ if (m->mp_parts) {
+ struct part *p;