From: David Levine Date: Fri, 21 Nov 2014 04:28:28 +0000 (-0600) Subject: Look at type of parent multipart/related when determining whether X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/888c7cccce9b0259d87383df77503b17a4284b86?hp=e7929465963035b8285dfc75519da61d5774388d Look at type of parent multipart/related when determining whether a non-plain text part already has a text/plain sibling. --- diff --git a/test/mhfixmsg/test-mhfixmsg b/test/mhfixmsg/test-mhfixmsg index b3c00090..594793a6 100755 --- a/test/mhfixmsg/test-mhfixmsg +++ b/test/mhfixmsg/test-mhfixmsg @@ -466,8 +466,7 @@ This is additional content before the first subpart of the multipart. ------- =_aaaaaaaaaa0 Content-Type: multipart/related; - type="multipart/alternative"; - boundary="subpart__1.1" + type="text/plain"; boundary="subpart__1.1" --subpart__1.1 Content-Type: text/plain; charset="iso-8859-1" @@ -516,8 +515,7 @@ This is additional content before the first subpart of the multipart. ------- =_aaaaaaaaaa0 Content-Type: multipart/related; - type="multipart/alternative"; - boundary="subpart__1.1" + type="text/plain"; boundary="subpart__1.1" --subpart__1.1 Content-Type: text/plain; charset="iso-8859-1" @@ -794,24 +792,16 @@ run_test 'mhfixmsg last -outfile '"$actual"' -verbose' \ check "$expected" "$actual" -# check that text/plain part is added to lone text/html in multipart/related -prepare_space >"$expected" <`mhpath new` < -------=_nmh-multipart1-- - ------=_Part_90310_101292502.1 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit @@ -835,32 +823,7 @@ Your email client does not support HTML messages ------=_Part_90310_101292502.1-- EOF -cat >`mhpath new` < - - HTML Content - - - This is the real content. - - -------=_Part_90310_101292502.1 -Content-Type: text/plain; charset="us-ascii" -Content-Transfer-Encoding: 7bit - -Your email client does not support HTML messages -------=_Part_90310_101292502.1-- -EOF +cp -p `mhpath last` "$expected" if [ $can_reformat_texthtml -eq 1 ]; then #### lynx inserts multiple blank lines, so squeeze them. diff --git a/uip/mhfixmsg.c b/uip/mhfixmsg.c index 31b3efe2..8739f8b9 100644 --- a/uip/mhfixmsg.c +++ b/uip/mhfixmsg.c @@ -85,6 +85,8 @@ static int replace_boundary (CT, char *, char *); static int fix_multipart_cte (CT, int *); static int set_ce (CT, int); static int ensure_text_plain (CT *, CT, int *, int); +static int find_textplain_sibling (CT, int, int *); +static int insert_new_text_plain_part (CT, int, CT); static CT build_text_plain_part (CT); static CT divide_part (CT); static void copy_ctinfo (CI, CI); @@ -215,12 +217,12 @@ main (int argc, char **argv) { case NRPRCSW: rmmproc = NULL; continue; - case CHGSW: - chgflag = 1; - continue; - case NCHGSW: - chgflag = 0; - continue; + case CHGSW: + chgflag = 1; + continue; + case NCHGSW: + chgflag = 0; + continue; case VERBSW: verbosw = 1; continue; @@ -842,72 +844,69 @@ ensure_text_plain (CT *ct, CT parent, int *message_mods, int replacetextplain) { switch ((*ct)->c_type) { case CT_TEXT: { - int has_text_plain = 0; - /* Nothing to do for text/plain. */ if ((*ct)->c_subtype == TEXT_PLAIN) { return OK; } if (parent && parent->c_type == CT_MULTIPART && parent->c_subtype == MULTI_ALTERNATE) { - struct multipart *mp = (struct multipart *) parent->c_ctparams; - struct part *part, *prev; int new_subpart_number = 1; - - /* See if there is a sibling text/plain. */ - for (prev = part = mp->mp_parts; part; part = part->mp_next) { - ++new_subpart_number; - if (part->mp_part->c_type == CT_TEXT && - part->mp_part->c_subtype == TEXT_PLAIN) { - if (replacetextplain) { - struct part *old_part; - if (part == mp->mp_parts) { - old_part = mp->mp_parts; - mp->mp_parts = part->mp_next; - } else { - old_part = prev->mp_next; - prev->mp_next = part->mp_next; - } - if (verbosw) { - report (NULL, parent->c_partno, parent->c_file, - "remove text/plain part %s", - old_part->mp_part->c_partno); - } - free_content (old_part->mp_part); - free (old_part); - } else { - has_text_plain = 1; - } - break; - } - prev = part; - } + int has_text_plain = + find_textplain_sibling (parent, replacetextplain, + &new_subpart_number); if (! has_text_plain) { /* Parent is a multipart/alternative. Insert a new text/plain subpart. */ - struct part *new_part = mh_xmalloc (sizeof *new_part); - - if ((new_part->mp_part = build_text_plain_part (*ct))) { - char buffer[16]; - snprintf (buffer, sizeof buffer, "%d", new_subpart_number); - - new_part->mp_next = mp->mp_parts; - mp->mp_parts = new_part; - new_part->mp_part->c_partno = - concat (parent->c_partno ? parent->c_partno : "1", ".", - buffer, NULL); - + const int inserted = + insert_new_text_plain_part (*ct, new_subpart_number, + parent); + if (inserted) { ++*message_mods; if (verbosw) { report (NULL, parent->c_partno, parent->c_file, "insert text/plain part"); } } else { - free_content (new_part->mp_part); - free (new_part); status = NOTOK; } } + } else if (parent && parent->c_type == CT_MULTIPART && + parent->c_subtype == MULTI_RELATED) { + char *type_subtype = + concat ((*ct)->c_ctinfo.ci_type, "/", + (*ct)->c_ctinfo.ci_subtype, NULL); + const char *parent_type = + get_param (parent->c_ctinfo.ci_first_pm, "type", '?', 1); + + /* Have to do string comparison, especially on the subtype + because we don't enumerate all of them in c_subtype + values. */ + if (strcasecmp (type_subtype, parent_type) == 0) { + /* The type of this part matches the root type of the + parent multipart/related. Look to see if there's + text/plain sibling. */ + int new_subpart_number = 1; + int has_text_plain = + find_textplain_sibling (parent, replacetextplain, + &new_subpart_number); + + if (! has_text_plain) { + /* Parent is a multipart/alternative. Insert a new + text/plain subpart. */ + const int inserted = + insert_new_text_plain_part (*ct, new_subpart_number, + parent); + if (inserted) { + ++*message_mods; + if (verbosw) { + report (NULL, parent->c_partno, parent->c_file, + "insert text/plain part"); + } + } else { + status = NOTOK; + } + } + } } else { /* Slip new text/plain part into a new multipart/alternative. */ CT tp_part = build_text_plain_part (*ct); @@ -971,6 +970,71 @@ ensure_text_plain (CT *ct, CT parent, int *message_mods, int replacetextplain) { } +/* See if there is a sibling text/plain. */ +static int +find_textplain_sibling (CT parent, int replacetextplain, + int *new_subpart_number) { + struct multipart *mp = (struct multipart *) parent->c_ctparams; + struct part *part, *prev; + int has_text_plain = 0; + + for (prev = part = mp->mp_parts; part; part = part->mp_next) { + ++*new_subpart_number; + if (part->mp_part->c_type == CT_TEXT && + part->mp_part->c_subtype == TEXT_PLAIN) { + if (replacetextplain) { + struct part *old_part; + if (part == mp->mp_parts) { + old_part = mp->mp_parts; + mp->mp_parts = part->mp_next; + } else { + old_part = prev->mp_next; + prev->mp_next = part->mp_next; + } + if (verbosw) { + report (NULL, parent->c_partno, parent->c_file, + "remove text/plain part %s", + old_part->mp_part->c_partno); + } + free_content (old_part->mp_part); + free (old_part); + } else { + has_text_plain = 1; + } + break; + } + prev = part; + } + + return has_text_plain; +} + + +static int +insert_new_text_plain_part (CT ct, int new_subpart_number, CT parent) { + struct multipart *mp = (struct multipart *) parent->c_ctparams; + struct part *new_part = mh_xmalloc (sizeof *new_part); + + if ((new_part->mp_part = build_text_plain_part (ct))) { + char buffer[16]; + snprintf (buffer, sizeof buffer, "%d", new_subpart_number); + + new_part->mp_next = mp->mp_parts; + mp->mp_parts = new_part; + new_part->mp_part->c_partno = + concat (parent->c_partno ? parent->c_partno : "1", ".", + buffer, NULL); + + return 1; + } else { + free_content (new_part->mp_part); + free (new_part); + + return 0; + } +} + + static CT build_text_plain_part (CT encoded_part) { CT tp_part = divide_part (encoded_part); @@ -1263,7 +1327,7 @@ build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) { } add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm, - "boundary", boundary, 0); + "boundary", boundary, 0); p = (struct part *) mh_xmalloc (sizeof *p); p->mp_next = (struct part *) mh_xmalloc (sizeof *p->mp_next);