+ 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;
+
+ for (p = m->mp_parts; p && p->mp_next; p = p->mp_next) { continue; }
+ p->mp_next = part;
+ } else {
+ m->mp_parts = part;
+ }
+}
+
+
+/* Extract any Content-Type header from beginning of convert output. */
+int
+extract_headers (CT ct, char *reply_file, FILE **reply_fp) {
+ char *buffer = NULL, *cp, *end_of_header;
+ int found_header = 0;
+ struct stat statbuf;
+
+ /* Read the convert reply from the file to memory. */
+ if (stat (reply_file, &statbuf) == NOTOK) {
+ admonish (reply_file, "failed to stat");
+ goto failed_to_extract_ct;
+ }
+
+ buffer = mh_xmalloc (statbuf.st_size + 1);
+
+ if ((*reply_fp = fopen (reply_file, "r+")) == NULL ||
+ fread (buffer, 1, (size_t) statbuf.st_size, *reply_fp) <
+ (size_t) statbuf.st_size) {
+ admonish (reply_file, "failed to read");
+ goto failed_to_extract_ct;
+ }
+ buffer[statbuf.st_size] = '\0';
+
+ /* Look for a header in the convert reply. */
+ 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;
+ } else if ((end_of_header = strstr (buffer, "\n\n"))) {
+ ++end_of_header;
+ found_header = 1;
+ }
+ }
+
+ if (found_header) {
+ CT tmp_ct;
+ char *tmp_file;
+ FILE *tmp_f;
+ size_t n;
+
+ /* Truncate buffer to just the C-T. */
+ *end_of_header = '\0';
+ n = strlen (buffer);
+
+ if (get_ctinfo (buffer + 14, ct, 0) != OK) {
+ admonish (NULL, "unable to get content info for reply");
+ goto failed_to_extract_ct;
+ }
+
+ /* Hack. Use parse_mime() to detect the type/subtype of the
+ reply, which we'll use below. */
+ tmp_file = getcpy (m_mktemp2 (NULL, invo_name, NULL, NULL));
+ if ((tmp_f = fopen (tmp_file, "w")) &&
+ fwrite (buffer, 1, n, tmp_f) == n) {
+ fclose (tmp_f);
+ } else {
+ goto failed_to_extract_ct;
+ }
+ tmp_ct = parse_mime (tmp_file);
+
+ if (tmp_ct) {
+ /* The type and subtype were detected from the reply
+ using parse_mime() above. */
+ ct->c_type = tmp_ct->c_type;
+ ct->c_subtype = tmp_ct->c_subtype;
+ free_content (tmp_ct);
+ }
+
+ free (tmp_file);
+
+ /* Rewrite the content without the header. */
+ cp = end_of_header + 1;
+ rewind (*reply_fp);
+
+ if (fwrite (cp, 1, statbuf.st_size - (cp - buffer), *reply_fp) <
+ (size_t) (statbuf.st_size - (cp - buffer))) {
+ admonish (reply_file, "failed to write");
+ goto failed_to_extract_ct;
+ }
+
+ if (ftruncate (fileno (*reply_fp), statbuf.st_size - (cp - buffer)) !=
+ 0) {
+ advise (reply_file, "ftruncate");
+ goto failed_to_extract_ct;
+ }
+ } else {
+ /* No header section, assume the reply is text/plain. */
+ ct->c_type = CT_TEXT;
+ ct->c_subtype = TEXT_PLAIN;
+ if (get_ctinfo ("text/plain", ct, 0) == NOTOK) {
+ /* This never should fail, but just in case. */
+ adios (NULL, "unable to get content info for reply");
+ }
+ }
+
+ /* free_encoding() will close reply_fp, which is passed through
+ ct->c_cefile.ce_fp. */
+ free (buffer);
+ return OK;