2 * mhfixmsg.c -- rewrite a message with various transformations
4 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
5 * See the COPYRIGHT file in the root directory of the nmh
6 * distribution for complete copyright information.
11 #include <h/mhparse.h>
13 #include <h/signals.h>
16 #define MHFIXMSG_SWITCHES \
17 X("decodetext 8bit|7bit", 0, DECODETEXTSW) \
18 X("nodecodetext", 0, NDECODETEXTSW) \
19 X("decodetypes", 0, DECODETYPESW) \
20 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
21 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
22 X("textcharset", 0, TEXTCHARSETSW) \
23 X("notextcharset", 0, NTEXTCHARSETSW) \
24 X("reformat", 0, REFORMATSW) \
25 X("noreformat", 0, NREFORMATSW) \
26 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
27 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
28 X("fixboundary", 0, FIXBOUNDARYSW) \
29 X("nofixboundary", 0, NFIXBOUNDARYSW) \
30 X("fixcte", 0, FIXCTESW) \
31 X("nofixcte", 0, NFIXCTESW) \
32 X("fixtype mimetype", 0, FIXTYPESW) \
33 X("file file", 0, FILESW) \
34 X("outfile file", 0, OUTFILESW) \
35 X("rmmproc program", 0, RPROCSW) \
36 X("normmproc", 0, NRPRCSW) \
37 X("changecur", 0, CHGSW) \
38 X("nochangecur", 0, NCHGSW) \
39 X("verbose", 0, VERBSW) \
40 X("noverbose", 0, NVERBSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(MHFIXMSG
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
54 int debugsw
; /* Needed by mhparse.c. */
56 #define quitser pipeser
59 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
60 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
61 extern int bogus_mp_content
; /* flag from InitMultiPart */
62 /* flags to/from parse_header_attrs */
63 extern int suppress_extraneous_trailing_semicolon_warning
;
64 extern int extraneous_trailing_semicolon
;
67 int output_message (CT
, char *);
70 void flush_errors (void);
74 void freects_done (int) NORETURN
;
79 typedef struct fix_transformations
{
87 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
90 } fix_transformations
;
92 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
93 static int fix_boundary (CT
*, int *);
94 static int copy_input_to_output (const char *, const char *);
95 static int get_multipart_boundary (CT
, char **);
96 static int replace_boundary (CT
, char *, char *);
97 static int fix_types (CT
, svector_t
, int *);
98 static char *replace_substring (char **, const char *, const char *);
99 static char *remove_parameter (char *, const char *);
100 static int fix_multipart_cte (CT
, int *);
101 static int set_ce (CT
, int);
102 static int ensure_text_plain (CT
*, CT
, int *, int);
103 static int find_textplain_sibling (CT
, int, int *);
104 static int insert_new_text_plain_part (CT
, int, CT
);
105 static CT
build_text_plain_part (CT
);
106 static int insert_into_new_mp_alt (CT
*, int *);
107 static CT
divide_part (CT
);
108 static void copy_ctinfo (CI
, CI
);
109 static int decode_part (CT
);
110 static int reformat_part (CT
, char *, char *, char *, int);
111 static int charset_encoding (CT
);
112 static CT
build_multipart_alt (CT
, CT
, int, int);
113 static int boundary_in_content (FILE **, char *, const char *);
114 static void transfer_noncontent_headers (CT
, CT
);
115 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
116 static int decode_text_parts (CT
, int, const char *, int *);
117 static int should_decode(const char *, const char *, const char *);
118 static int content_encoding (CT
, const char **);
119 static int strip_crs (CT
, int *);
120 static int convert_charsets (CT
, char *, int *);
121 static int fix_always (CT
, int *);
122 static int write_content (CT
, const char *, char *, int, int);
123 static void set_text_ctparams(CT
, char *, int);
124 static int remove_file (const char *);
125 static void report (char *, char *, char *, char *, ...);
126 static void pipeser (int);
130 main (int argc
, char **argv
) {
132 char *cp
, *file
= NULL
, *folder
= NULL
;
133 char *maildir
, buf
[100], *outfile
= NULL
;
134 char **argp
, **arguments
;
135 struct msgs_array msgs
= { 0, 0, NULL
};
136 struct msgs
*mp
= NULL
;
142 fix_transformations fx
;
143 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
145 fx
.replacetextplain
= 0;
146 fx
.decodetext
= CE_8BIT
;
147 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
148 fx
.lf_line_endings
= 0;
149 fx
.textcharset
= NULL
;
151 if (nmh_init(argv
[0], 1)) { return 1; }
155 arguments
= getarguments (invo_name
, argc
, argv
, 1);
161 while ((cp
= *argp
++)) {
163 switch (smatch (++cp
, switches
)) {
165 ambigsw (cp
, switches
);
168 adios (NULL
, "-%s unknown", cp
);
171 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
173 print_help (buf
, switches
, 1);
176 print_version(invo_name
);
180 if (! (cp
= *argp
++) || *cp
== '-')
181 adios (NULL
, "missing argument to %s", argp
[-2]);
182 if (! strcasecmp (cp
, "8bit")) {
183 fx
.decodetext
= CE_8BIT
;
184 } else if (! strcasecmp (cp
, "7bit")) {
185 fx
.decodetext
= CE_7BIT
;
187 adios (NULL
, "invalid argument to %s", argp
[-2]);
194 if (! (cp
= *argp
++) || *cp
== '-')
195 adios (NULL
, "missing argument to %s", argp
[-2]);
198 case CRLFLINEBREAKSSW
:
199 fx
.lf_line_endings
= 0;
201 case NCRLFLINEBREAKSSW
:
202 fx
.lf_line_endings
= 1;
205 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
206 adios (NULL
, "missing argument to %s", argp
[-2]);
225 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
226 adios (NULL
, "missing argument to %s", argp
[-2]);
227 if (! strncasecmp (cp
, "multipart/", 10) ||
228 ! strncasecmp (cp
, "message/", 8)) {
229 adios (NULL
, "-fixtype %s not allowed", cp
);
230 } else if (! strchr (cp
, '/')) {
231 adios (NULL
, "-fixtype requires type/subtype");
233 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
234 svector_push_back (fx
.fixtypes
, cp
);
242 case REPLACETEXTPLAINSW
:
243 fx
.replacetextplain
= 1;
245 case NREPLACETEXTPLAINSW
:
246 fx
.replacetextplain
= 0;
249 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
250 adios (NULL
, "missing argument to %s", argp
[-2]);
251 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
254 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
255 adios (NULL
, "missing argument to %s", argp
[-2]);
256 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
259 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
260 adios (NULL
, "missing argument to %s", argp
[-2]);
279 if (*cp
== '+' || *cp
== '@') {
281 adios (NULL
, "only one folder at a time!");
283 folder
= pluspath (cp
);
286 /* Interpret a full path as a filename, not a message. */
287 file
= add (cp
, NULL
);
289 app_msgarg (&msgs
, cp
);
294 SIGNAL (SIGQUIT
, quitser
);
295 SIGNAL (SIGPIPE
, pipeser
);
298 * Read the standard profile setup
300 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
301 readconfig ((struct node
**) 0, fp
, cp
, 0);
305 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
306 suppress_extraneous_trailing_semicolon_warning
= 1;
308 if (! context_find ("path"))
309 free (path ("./", TFOLDER
));
311 if (file
&& msgs
.size
)
312 adios (NULL
, "cannot specify msg and file at same time!");
315 * check if message is coming from file
318 /* If file is stdin, create a tmp file name before parse_mime()
319 has a chance, because it might put in on a different
320 filesystem than the output file. Instead, put it in the
321 user's preferred tmp directory. */
324 if (! strcmp ("-", file
)) {
330 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
331 adios (NULL
, "unable to create temporary file in %s",
335 file
= add (cp
, NULL
);
336 cpydata (STDIN_FILENO
, fd
, "-", file
);
340 (void) m_unlink (file
);
341 adios (NULL
, "failed to write temporary file");
345 if (! (cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof *cts
))) {
346 adios (NULL
, "out of memory");
350 if ((ct
= parse_mime (file
))) {
351 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
354 advise (NULL
, "unable to parse message from file %s", file
);
357 /* If there's an outfile, pass the input message unchanged, so the message won't
358 get dropped from a pipeline. */
360 /* Something went wrong. Output might be expected, such as if this were run
361 as a filter. Just copy the input to the output. */
362 if (copy_input_to_output (file
, outfile
) != OK
) {
363 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
369 * message(s) are coming from a folder
374 app_msgarg(&msgs
, "cur");
376 folder
= getfolder (1);
377 maildir
= m_maildir (folder
);
379 if (chdir (maildir
) == NOTOK
)
380 adios (maildir
, "unable to change directory to");
382 /* read folder and create message structure */
383 if (! (mp
= folder_read (folder
, 1)))
384 adios (NULL
, "unable to read folder %s", folder
);
386 /* check for empty folder */
388 adios (NULL
, "no messages in %s", folder
);
390 /* parse all the message ranges/sequences and set SELECTED */
391 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
392 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
394 seq_setprev (mp
); /* set the previous-sequence */
397 (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
))) {
398 adios (NULL
, "out of memory");
402 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
403 if (is_selected(mp
, msgnum
)) {
406 msgnam
= m_name (msgnum
);
407 if ((ct
= parse_mime (msgnam
))) {
408 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
411 advise (NULL
, "unable to parse message %s", msgnam
);
414 /* If there's an outfile, pass the input message unchanged, so the message won't
415 get dropped from a pipeline. */
417 /* Something went wrong. Output might be expected, such as if this were run
418 as a filter. Just copy the input to the output. */
419 const char *input_filename
= path (msgnam
, TFILE
);
421 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
422 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
430 seq_setcur (mp
, mp
->hghsel
); /* update current message */
432 seq_save (mp
); /* synchronize sequences */
433 context_replace (pfolder
, folder
);/* update current folder */
434 context_save (); /* save the context file */
438 for (ctp
= cts
; *ctp
; ++ctp
) {
439 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
442 (void) m_unlink (file
);
445 /* Just calling m_backup() unlinks the backup file. */
446 (void) m_backup (file
);
454 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
458 /* done is freects_done, which will clean up all of cts. */
465 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
466 /* Store input filename in case one of the transformations, i.e.,
467 fix_boundary(), rewrites to a tmp file. */
468 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
469 int modify_inplace
= 0;
470 int message_mods
= 0;
473 if (outfile
== NULL
) {
476 if ((*ctp
)->c_file
) {
478 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
479 adios (NULL
, "unable to create temporary file in %s",
482 outfile
= add (tempfile
, NULL
);
484 adios (NULL
, "missing both input and output filenames\n");
488 reverse_alternative_parts (*ctp
);
489 status
= fix_always (*ctp
, &message_mods
);
490 if (status
== OK
&& fx
->fixboundary
) {
491 status
= fix_boundary (ctp
, &message_mods
);
493 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
494 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
496 if (status
== OK
&& fx
->fixcte
) {
497 status
= fix_multipart_cte (*ctp
, &message_mods
);
499 if (status
== OK
&& fx
->reformat
) {
501 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
503 if (status
== OK
&& fx
->decodetext
) {
504 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
, &message_mods
);
506 if (status
== OK
&& fx
->textcharset
!= NULL
) {
507 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
510 if (status
== OK
&& ! (*ctp
)->c_umask
) {
511 /* Set the umask for the contents file. This currently
512 isn't used but just in case it is in the future. */
515 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
516 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
518 (*ctp
)->c_umask
= ~m_gmprot();
523 * Write the content to a file
526 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
528 } else if (! modify_inplace
) {
529 /* Something went wrong. Output might be expected, such
530 as if this were run as a filter. Just copy the input
532 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
533 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
537 if (modify_inplace
) {
538 if (status
!= OK
) { (void) m_unlink (outfile
); }
543 free (input_filename
);
549 /* Copy input message to output. Assumes not modifying in place, so this
550 might be running as part of a pipeline. */
552 copy_input_to_output (const char *input_filename
, const char *output_filename
) {
553 int in
= open (input_filename
, O_RDONLY
);
554 int out
= strcmp (output_filename
, "-")
555 ? open (output_filename
, O_WRONLY
| O_CREAT
, m_gmprot ())
559 if (in
!= -1 && out
!= -1) {
560 cpydata (in
, out
, input_filename
, output_filename
);
573 fix_boundary (CT
*ct
, int *message_mods
) {
574 struct multipart
*mp
;
577 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
578 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
581 * 1) Get boundary at end of part.
582 * 2) Get boundary at beginning of part and compare to the end-of-part
584 * 3) Write out contents of ct to tmp file, replacing boundary in
585 * header with boundary from part. Set c_unlink to 1.
587 * 5) Call parse_mime() on the tmp file, replacing ct.
590 if (mp
&& mp
->mp_start
) {
593 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
596 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
597 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
598 char *filename
= add ((*ct
)->c_file
, NULL
);
602 if ((fixed_ct
= parse_mime (fixed
))) {
608 report (NULL
, NULL
, filename
,
609 "fix multipart boundary");
613 advise (NULL
, "unable to parse fixed part");
618 advise (NULL
, "unable to replace broken boundary");
622 advise (NULL
, "unable to create temporary file in %s",
627 free (part_boundary
);
629 /* Couldn't fix the boundary. Report failure so that mhfixmsg
630 doesn't modify the message. */
634 /* No multipart struct, even though the content type is
635 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
646 get_multipart_boundary (CT ct
, char **part_boundary
) {
648 char *end_boundary
= NULL
;
649 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
650 ? (off_t
) (ct
->c_end
- sizeof buffer
)
651 : (off_t
) ct
->c_begin
;
655 /* This will fail if the boundary spans fread() calls. BUFSIZ should
656 be big enough, even if it's just 1024, to make that unlikely. */
658 /* free_content() will close ct->c_fp. */
659 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
660 advise (ct
->c_file
, "unable to open for reading");
664 /* Get boundary at end of multipart. */
665 while (begin
>= (off_t
) ct
->c_begin
) {
666 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
667 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
668 char *cp
= rfind_str (buffer
, bytes_read
, "--");
673 /* Trim off trailing "--" and anything beyond. */
675 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
676 if (strlen (end
) > 3 && *end
++ == '\n' &&
677 *end
++ == '-' && *end
++ == '-') {
678 end_boundary
= add (end
, NULL
);
685 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
686 begin
-= sizeof buffer
;
692 /* Get boundary at beginning of multipart. */
694 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
695 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
696 if (bytes_read
>= strlen (end_boundary
)) {
697 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
699 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
700 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
705 /* The start and end boundaries didn't match, or the
706 start boundary doesn't begin with "\n--" (or "--"
707 if at the beginning of buffer). Keep trying. */
716 *part_boundary
= end_boundary
;
718 *part_boundary
= NULL
;
726 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
728 replace_boundary (CT ct
, char *file
, char *boundary
) {
731 char buf
[BUFSIZ
], name
[NAMESZ
];
733 m_getfld_state_t gstate
= 0;
736 if (ct
->c_file
== NULL
) {
737 advise (NULL
, "missing input filename");
741 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
742 advise (ct
->c_file
, "unable to open for reading");
746 if ((fpout
= fopen (file
, "w")) == NULL
) {
748 advise (file
, "unable to open for writing");
752 for (compnum
= 1;;) {
753 int bufsz
= (int) sizeof buf
;
755 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
760 /* get copies of the buffers */
761 np
= add (name
, NULL
);
762 vp
= add (buf
, NULL
);
764 /* if necessary, get rest of field */
765 while (state
== FLDPLUS
) {
767 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
768 vp
= add (buf
, vp
); /* add to previous value */
771 if (strcasecmp (TYPE_FIELD
, np
)) {
772 fprintf (fpout
, "%s:%s", np
, vp
);
774 char *new_ctline
, *new_params
;
776 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
777 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
780 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
781 ct
->c_ctinfo
.ci_subtype
, NULL
);
782 new_params
= output_params(strlen(TYPE_FIELD
) +
783 strlen(new_ctline
) + 1,
784 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
785 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
786 new_params
? new_params
: "");
799 /* buf will have a terminating NULL, skip it. */
800 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
801 advise (file
, "fwrite");
810 advise (NULL
, "message format error in component #%d", compnum
);
815 advise (NULL
, "getfld() returned %d", state
);
823 m_getfld_state_destroy (&gstate
);
832 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
835 switch (ct
->c_type
) {
837 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
840 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
841 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
847 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
848 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
850 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
857 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
858 for (typep
= svector_strs (fixtypes
);
859 typep
&& (type
= *typep
);
862 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
865 if (! strcasecmp (type
, type_subtype
) &&
866 decode_part (ct
) == OK
&&
867 ct
->c_cefile
.ce_file
!= NULL
) {
868 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
871 if ((cp
= strchr (ct_type_subtype
, ';'))) {
872 /* Truncate to remove any parameter list from
873 mime_type () result. */
877 if (strcasecmp (type
, ct_type_subtype
)) {
878 char *ct_type
, *ct_subtype
;
881 /* The Content-Type header does not match the
882 content, so update these struct Content
885 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
888 /* Extract type and subtype from type/subtype. */
889 ct_type
= getcpy (ct_type_subtype
);
890 if ((cp
= strchr (ct_type
, '/'))) {
892 ct_subtype
= getcpy (++cp
);
894 advise (NULL
, "missing / in MIME type of %s %s",
895 ct
->c_file
, ct
->c_partno
);
900 ct
->c_type
= ct_str_type (ct_type
);
901 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
903 free (ct
->c_ctinfo
.ci_type
);
904 ct
->c_ctinfo
.ci_type
= ct_type
;
905 free (ct
->c_ctinfo
.ci_subtype
);
906 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
907 if (! replace_substring (&ct
->c_ctline
, type
,
909 advise (NULL
, "did not find %s in %s",
913 /* Update Content-Type header field. */
914 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
915 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
916 if (replace_substring (&hf
->value
, type
,
920 report (NULL
, ct
->c_partno
, ct
->c_file
,
921 "change Content-Type in header "
923 type
, ct_type_subtype
);
927 advise (NULL
, "did not find %s in %s",
933 free (ct_type_subtype
);
944 replace_substring (char **str
, const char *old
, const char *new) {
947 if ((cp
= strstr (*str
, old
))) {
948 char *remainder
= cp
+ strlen (old
);
949 char *prefix
, *new_str
;
952 prefix
= getcpy (*str
);
953 *(prefix
+ (cp
- *str
)) = '\0';
954 new_str
= concat (prefix
, new, remainder
, NULL
);
957 new_str
= concat (new, remainder
, NULL
);
962 return *str
= new_str
;
969 * Remove a name=value parameter, given just its name, from a header value.
972 remove_parameter (char *str
, const char *name
) {
973 /* It looks to me, based on the BNF in RFC 2045, than there can't
974 be whitespace betwwen the parameter name and the "=", or
975 between the "=" and the parameter value. */
976 char *param_name
= concat (name
, "=", NULL
);
979 if ((cp
= strstr (str
, param_name
))) {
983 /* Remove any leading spaces, before the parameter name. */
985 start
> str
&& isspace ((unsigned char) *(start
-1));
989 /* Remove a leading semicolon. */
990 if (start
> str
&& *(start
-1) == ';') { --start
; }
992 end
= cp
+ strlen (name
) + 1;
994 /* Skip past the quoted value, and then the final quote. */
995 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
998 /* Skip past the value. */
999 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1002 /* Count how many characters need to be moved. Include
1003 trailing null, which is accounted for by the
1004 initialization of count to 1. */
1005 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1006 (void) memmove (start
, end
, count
);
1015 fix_multipart_cte (CT ct
, int *message_mods
) {
1018 if (ct
->c_type
== CT_MULTIPART
) {
1019 struct multipart
*m
;
1022 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1023 ct
->c_encoding
!= CE_BINARY
) {
1026 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1027 char *name
= hf
->name
;
1028 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
1032 if (! strncasecmp (name
, ENCODING_FIELD
,
1033 strlen (ENCODING_FIELD
))) {
1034 char *prefix
= "Nmh-REPLACED-INVALID-";
1035 HF h
= mh_xmalloc (sizeof *h
);
1037 h
->name
= add (hf
->name
, NULL
);
1038 h
->hf_encoding
= hf
->hf_encoding
;
1042 /* Retain old header but prefix its name. */
1044 hf
->name
= concat (prefix
, h
->name
, NULL
);
1048 char *encoding
= cpytrim (hf
->value
);
1049 report (NULL
, ct
->c_partno
, ct
->c_file
,
1050 "replace Content-Transfer-Encoding of %s "
1051 "with 8 bit", encoding
);
1055 h
->value
= add (" 8bit\n", NULL
);
1057 /* Don't need to warn for multiple C-T-E header
1058 fields, parse_mime() already does that. But
1059 if there are any, fix them all as necessary. */
1064 set_ce (ct
, CE_8BIT
);
1067 m
= (struct multipart
*) ct
->c_ctparams
;
1068 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1069 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
1081 set_ce (CT ct
, int encoding
) {
1082 const char *ce
= ce_str (encoding
);
1083 const struct str2init
*ctinit
= get_ce_method (ce
);
1086 char *cte
= concat (" ", ce
, "\n", NULL
);
1089 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1090 caller is decode_text_parts (). Save because we'll
1092 struct cefile decoded_content_info
= ct
->c_cefile
;
1094 ct
->c_encoding
= encoding
;
1096 ct
->c_ctinitfnx
= ctinit
->si_init
;
1097 /* This will assign ct->c_cefile with an all-0 struct, which
1099 (*ctinit
->si_init
) (ct
);
1100 /* After returning, the caller should set
1101 ct->c_cefile.ce_file to the name of the file containing
1104 /* Restore the cefile. */
1105 ct
->c_cefile
= decoded_content_info
;
1107 /* Update/add Content-Transfer-Encoding header field. */
1108 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1109 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1116 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
1119 /* Update c_celine. It's used only by mhlist -debug. */
1120 free (ct
->c_celine
);
1121 ct
->c_celine
= add (cte
, NULL
);
1130 /* Make sure each text part has a corresponding text/plain part. */
1132 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1135 switch ((*ct
)->c_type
) {
1137 /* Nothing to do for text/plain. */
1138 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1140 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1141 parent
->c_subtype
== MULTI_ALTERNATE
) {
1142 int new_subpart_number
= 1;
1143 int has_text_plain
=
1144 find_textplain_sibling (parent
, replacetextplain
,
1145 &new_subpart_number
);
1147 if (! has_text_plain
) {
1148 /* Parent is a multipart/alternative. Insert a new
1149 text/plain subpart. */
1150 const int inserted
=
1151 insert_new_text_plain_part (*ct
, new_subpart_number
,
1156 report (NULL
, parent
->c_partno
, parent
->c_file
,
1157 "insert text/plain part");
1163 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1164 parent
->c_subtype
== MULTI_RELATED
) {
1165 char *type_subtype
=
1166 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1167 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1168 const char *parent_type
=
1169 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1170 int new_subpart_number
= 1;
1171 int has_text_plain
= 0;
1173 /* Have to do string comparison on the subtype because we
1174 don't enumerate all of them in c_subtype values.
1175 parent_type will be NULL if the multipart/related part
1176 doesn't have a type parameter. The type parameter must
1177 be specified according to RFC 2387 Sec. 3.1 but not all
1179 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1180 /* The type of this part matches the root type of the
1181 parent multipart/related. Look to see if there's
1182 text/plain sibling. */
1184 find_textplain_sibling (parent
, replacetextplain
,
1185 &new_subpart_number
);
1188 free (type_subtype
);
1190 if (! has_text_plain
) {
1191 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1195 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1196 if (*ct
!= part
->mp_part
) {
1202 /* Parent is a multipart/related. Insert a new
1203 text/plain subpart in a new multipart/alternative. */
1204 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1205 /* Not an error if text/plain couldn't be added. */
1208 /* There are no siblings, so insert a new text/plain
1209 subpart, and change the parent type from
1210 multipart/related to multipart/alternative. */
1211 const int inserted
=
1212 insert_new_text_plain_part (*ct
, new_subpart_number
,
1218 parent
->c_subtype
= MULTI_ALTERNATE
;
1219 parent
->c_ctinfo
.ci_subtype
= getcpy ("alternative");
1220 if (! replace_substring (&parent
->c_ctline
, "/related",
1223 "did not find multipart/related in %s",
1227 /* Update Content-Type header field. */
1228 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1229 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1230 if (replace_substring (&hf
->value
, "/related",
1234 report (NULL
, parent
->c_partno
,
1236 "insert text/plain part");
1239 /* Remove, e.g., type="text/html" from
1240 multipart/alternative. */
1241 remove_parameter (hf
->value
, "type");
1244 advise (NULL
, "did not find multipart/"
1245 "related in header %s",
1251 /* Not an error if text/plain couldn't be inserted. */
1256 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1263 case CT_MULTIPART
: {
1264 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1267 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1268 if ((*ct
)->c_type
== CT_MULTIPART
) {
1269 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1277 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1278 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1280 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1290 /* See if there is a sibling text/plain. */
1292 find_textplain_sibling (CT parent
, int replacetextplain
,
1293 int *new_subpart_number
) {
1294 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1295 struct part
*part
, *prev
;
1296 int has_text_plain
= 0;
1298 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1299 ++*new_subpart_number
;
1300 if (part
->mp_part
->c_type
== CT_TEXT
&&
1301 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1302 if (replacetextplain
) {
1303 struct part
*old_part
;
1304 if (part
== mp
->mp_parts
) {
1305 old_part
= mp
->mp_parts
;
1306 mp
->mp_parts
= part
->mp_next
;
1308 old_part
= prev
->mp_next
;
1309 prev
->mp_next
= part
->mp_next
;
1312 report (NULL
, parent
->c_partno
, parent
->c_file
,
1313 "remove text/plain part %s",
1314 old_part
->mp_part
->c_partno
);
1316 free_content (old_part
->mp_part
);
1326 return has_text_plain
;
1331 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1332 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1333 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
1335 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1337 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1339 new_part
->mp_next
= mp
->mp_parts
;
1340 mp
->mp_parts
= new_part
;
1341 new_part
->mp_part
->c_partno
=
1342 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1347 free_content (new_part
->mp_part
);
1356 build_text_plain_part (CT encoded_part
) {
1357 CT tp_part
= divide_part (encoded_part
);
1358 char *tmp_plain_file
= NULL
;
1360 if (decode_part (tp_part
) == OK
) {
1361 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1362 contains the decoded contents. And the decoding function, such
1363 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1364 be unlinked by free_content (). */
1367 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1368 advise (NULL
, "unable to create temporary file in %s",
1371 tmp_plain_file
= add (tempfile
, NULL
);
1372 if (reformat_part (tp_part
, tmp_plain_file
,
1373 tp_part
->c_ctinfo
.ci_type
,
1374 tp_part
->c_ctinfo
.ci_subtype
,
1375 tp_part
->c_type
) == OK
) {
1381 free_content (tp_part
);
1382 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1383 free (tmp_plain_file
);
1389 /* Slip new text/plain part into a new multipart/alternative. */
1391 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1392 CT tp_part
= build_text_plain_part (*ct
);
1396 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1399 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1401 if (mp
&& mp
->mp_parts
) {
1402 mp
->mp_parts
->mp_part
= tp_part
;
1403 /* Make the new multipart/alternative the parent. */
1408 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1409 "insert text/plain part");
1412 free_content (tp_part
);
1413 free_content (mp_alt
);
1420 /* Not an error if text/plain couldn't be built. */
1427 divide_part (CT ct
) {
1430 if ((new_part
= (CT
) mh_xcalloc (1, sizeof *new_part
)) == NULL
)
1431 adios (NULL
, "out of memory");
1433 /* Just copy over what is needed for decoding. c_vrsn and
1434 c_celine aren't necessary. */
1435 new_part
->c_file
= add (ct
->c_file
, NULL
);
1436 new_part
->c_begin
= ct
->c_begin
;
1437 new_part
->c_end
= ct
->c_end
;
1438 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1439 new_part
->c_type
= ct
->c_type
;
1440 new_part
->c_cefile
= ct
->c_cefile
;
1441 new_part
->c_encoding
= ct
->c_encoding
;
1442 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1443 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1444 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1445 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1447 /* c_ctline is used by reformat__part(), so it can preserve
1448 anything after the type/subtype. */
1449 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1456 copy_ctinfo (CI dest
, CI src
) {
1459 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1460 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1462 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1463 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1465 if (s_pm
->pm_charset
)
1466 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1468 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1471 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1472 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1477 decode_part (CT ct
) {
1482 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1483 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1485 tmp_decoded
= add (tempfile
, NULL
);
1486 /* The following call will load ct->c_cefile.ce_file with the tmp
1487 filename of the decoded content. tmp_decoded will contain the
1488 encoded output, get rid of that. */
1489 status
= output_message (ct
, tmp_decoded
);
1490 (void) m_unlink (tmp_decoded
);
1497 /* Some of the arguments aren't really needed now, but maybe will
1498 be in the future for other than text types. */
1500 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1501 int output_subtype
, output_encoding
;
1505 /* Hacky: this redirects the output from whatever command is used
1506 to show the part to a file. So, the user can't have any output
1507 redirection in that command.
1508 Could show_multi() in mhshowsbr.c avoid this? */
1510 /* Check for invo_name-format-type/subtype. */
1511 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1513 advise (NULL
, "Don't know how to convert %s, there is no "
1514 "%s-format-%s/%s profile entry",
1515 ct
->c_file
, invo_name
, type
, subtype
);
1519 if (strchr (cf
, '>')) {
1520 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1521 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1522 subtype
? subtype
: "");
1528 cp
= concat (cf
, " >", file
, NULL
);
1529 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1532 /* Unlink decoded content tmp file and free its filename to avoid
1533 leaks. The file stream should already have been closed. */
1534 if (ct
->c_cefile
.ce_unlink
) {
1535 (void) m_unlink (ct
->c_cefile
.ce_file
);
1536 free (ct
->c_cefile
.ce_file
);
1537 ct
->c_cefile
.ce_file
= NULL
;
1538 ct
->c_cefile
.ce_unlink
= 0;
1541 if (c_type
== CT_TEXT
) {
1542 output_subtype
= TEXT_PLAIN
;
1544 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1547 output_encoding
= charset_encoding (ct
);
1549 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1550 ct
->c_cefile
.ce_file
= file
;
1551 ct
->c_cefile
.ce_unlink
= 1;
1553 ct
->c_cefile
.ce_unlink
= 0;
1561 /* Identifies 7bit or 8bit content based on charset. */
1563 charset_encoding (CT ct
) {
1564 char *ct_charset
= content_charset (ct
);
1565 int encoding
= strcasecmp (ct_charset
, "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1574 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1575 char *boundary_prefix
= "----=_nmh-multipart";
1576 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1577 char *boundary_indicator
= "; boundary=";
1578 char *typename
, *subtypename
, *name
;
1581 struct multipart
*m
;
1582 const struct str2init
*ctinit
;
1584 if ((ct
= (CT
) mh_xcalloc (1, sizeof *ct
)) == NULL
)
1585 adios (NULL
, "out of memory");
1587 /* Set up the multipart/alternative part. These fields of *ct were
1588 initialized to 0 by mh_xcalloc():
1589 c_fp, c_unlink, c_begin, c_end,
1590 c_vrsn, c_ctline, c_celine,
1591 c_id, c_descr, c_dispo, c_partno,
1592 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1593 c_cefile, c_encoding,
1594 c_digested, c_digest[16], c_ctexbody,
1595 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1597 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1600 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1602 ct
->c_subtype
= subtype
;
1604 ctinit
= get_ct_init (ct
->c_type
);
1606 typename
= ct_type_str (type
);
1607 subtypename
= ct_subtype_str (type
, subtype
);
1611 int found_boundary
= 1;
1613 while (found_boundary
&& serial
< 1000000) {
1616 /* Ensure that the boundary doesn't appear in the decoded
1618 if (new_part
->c_cefile
.ce_file
) {
1619 if ((found_boundary
=
1620 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1621 new_part
->c_cefile
.ce_file
,
1628 /* Ensure that the boundary doesn't appear in the encoded
1630 if (! found_boundary
&& new_part
->c_file
) {
1631 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1639 if (found_boundary
) {
1640 /* Try a slightly different boundary. */
1645 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1647 concat (boundary_prefix
,
1648 first_alt
->c_partno
? first_alt
->c_partno
: "",
1649 "-", buffer2
, NULL
);
1653 if (found_boundary
) {
1654 advise (NULL
, "giving up trying to find a unique boundary");
1660 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1661 boundary
, "\"", NULL
);
1663 /* Load c_first_hf and c_last_hf. */
1664 transfer_noncontent_headers (first_alt
, ct
);
1665 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1668 /* Load c_partno. */
1669 if (first_alt
->c_partno
) {
1670 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1671 free (first_alt
->c_partno
);
1672 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1673 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1675 first_alt
->c_partno
= add ("1", NULL
);
1676 new_part
->c_partno
= add ("2", NULL
);
1680 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1681 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1684 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1685 "boundary", boundary
, 0);
1687 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1688 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1689 p
->mp_next
->mp_next
= NULL
;
1690 p
->mp_next
->mp_part
= first_alt
;
1692 if ((m
= (struct multipart
*) mh_xcalloc (1, sizeof (struct multipart
))) ==
1694 adios (NULL
, "out of memory");
1695 m
->mp_start
= concat (boundary
, "\n", NULL
);
1696 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1706 /* Check that the boundary does not appear in the content. */
1708 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1709 char buffer
[BUFSIZ
];
1711 int found_boundary
= 0;
1713 /* free_content() will close *fp if we fopen it here. */
1714 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1715 advise (file
, "unable to open %s for reading", file
);
1719 fseeko (*fp
, 0L, SEEK_SET
);
1720 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1721 if (find_str (buffer
, bytes_read
, boundary
)) {
1727 return found_boundary
;
1731 /* Remove all non-Content headers. */
1733 transfer_noncontent_headers (CT old
, CT
new) {
1736 hp_prev
= hp
= old
->c_first_hf
;
1740 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1741 if (hp
== old
->c_last_hf
) {
1742 if (hp
== old
->c_first_hf
) {
1743 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1745 hp_prev
->next
= NULL
;
1746 old
->c_last_hf
= hp_prev
;
1749 if (hp
== old
->c_first_hf
) {
1750 old
->c_first_hf
= next
;
1752 hp_prev
->next
= next
;
1756 /* Put node hp in the new CT. */
1757 if (new->c_first_hf
== NULL
) {
1758 new->c_first_hf
= hp
;
1760 new->c_last_hf
->next
= hp
;
1762 new->c_last_hf
= hp
;
1764 /* A Content- header, leave in old. */
1774 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1775 char *typename
= ct_type_str (type
);
1776 char *subtypename
= ct_subtype_str (type
, subtype
);
1777 /* E.g, " text/plain" */
1778 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1779 /* E.g, " text/plain\n" */
1780 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1781 int found_content_type
= 0;
1783 const char *cp
= NULL
;
1787 /* Update/add Content-Type header field. */
1788 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1789 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1790 found_content_type
= 1;
1792 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1793 ? concat (type_subtypename
, cp
, "\n", NULL
)
1794 : add (name_plus_nl
, NULL
);
1797 if (! found_content_type
) {
1798 add_header (ct
, add (TYPE_FIELD
, NULL
),
1799 (cp
= strchr (ct
->c_ctline
, ';'))
1800 ? concat (type_subtypename
, cp
, "\n", NULL
)
1801 : add (name_plus_nl
, NULL
));
1804 /* Some of these might not be used, but set them anyway. */
1806 ? concat (type_subtypename
, cp
, NULL
)
1807 : concat (type_subtypename
, NULL
);
1808 free (ct
->c_ctline
);
1809 ct
->c_ctline
= ctline
;
1810 /* Leave other ctinfo members as they were. */
1811 free (ct
->c_ctinfo
.ci_type
);
1812 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1813 free (ct
->c_ctinfo
.ci_subtype
);
1814 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1816 ct
->c_subtype
= subtype
;
1818 free (name_plus_nl
);
1819 free (type_subtypename
);
1821 status
= set_ce (ct
, encoding
);
1828 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
, int *message_mods
) {
1830 int lf_line_endings
= 0;
1832 switch (ct
->c_type
) {
1833 case CT_MULTIPART
: {
1834 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1837 /* Should check to see if the body for this part is encoded?
1838 For now, it gets passed along as-is by InitMultiPart(). */
1839 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1840 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
, message_mods
);
1846 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1847 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1849 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
, message_mods
);
1854 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
1859 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
1861 switch (ct
->c_encoding
) {
1866 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1867 const char *reason
= NULL
;
1869 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1870 && encoding
!= CE_BINARY
) {
1871 /* The decoding isn't acceptable so discard it.
1872 Leave status as OK to allow other transformations. */
1874 report (NULL
, ct
->c_partno
, ct
->c_file
,
1875 "will not decode%s because it is binary (%s)",
1877 : ct
->c_ctline
? ct
->c_ctline
1881 (void) m_unlink (ct
->c_cefile
.ce_file
);
1882 free (ct
->c_cefile
.ce_file
);
1883 ct
->c_cefile
.ce_file
= NULL
;
1884 } else if (ct
->c_encoding
== CE_QUOTED
&&
1885 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1886 /* The decoding isn't acceptable so discard it.
1887 Leave status as OK to allow other transformations. */
1889 report (NULL
, ct
->c_partno
, ct
->c_file
,
1890 "will not decode%s because it is 8bit",
1892 : ct
->c_ctline
? ct
->c_ctline
1895 (void) m_unlink (ct
->c_cefile
.ce_file
);
1896 free (ct
->c_cefile
.ce_file
);
1897 ct
->c_cefile
.ce_file
= NULL
;
1900 if (ct_encoding
== CE_BINARY
)
1902 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1905 enc
= charset_encoding (ct
);
1906 if (set_ce (ct
, enc
) == OK
) {
1909 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
1910 ct
->c_ctline
? ct
->c_ctline
: "");
1912 if (lf_line_endings
) {
1913 strip_crs (ct
, message_mods
);
1926 if (lf_line_endings
) {
1927 strip_crs (ct
, message_mods
);
1941 /* Determine if the part with type[/subtype] should be decoded, according to
1942 decodetypes (which came from the -decodetypes switch). */
1944 should_decode(const char *decodetypes
, const char *type
, const char *subtype
) {
1945 /* Quick search for matching type[/subtype] in decodetypes: bracket
1946 decodetypes with commas, then search for ,type, and ,type/subtype, in
1949 int found_match
= 0;
1950 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
1951 char *delimited_type
= concat(",", type
, ",", NULL
);
1953 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
1955 } else if (subtype
!= NULL
) {
1956 char *delimited_type_subtype
=
1957 concat(",", type
, "/", subtype
, ",", NULL
);
1959 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
1962 free(delimited_type_subtype
);
1965 free(delimited_type
);
1966 free(delimited_decodetypes
);
1972 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1973 if it has any NUL characters, a CR not followed by a LF, or lines
1974 greater than 998 characters in length. If binary, reason is set
1975 to a string explaining why. */
1977 content_encoding (CT ct
, const char **reason
) {
1978 CE ce
= &ct
->c_cefile
;
1979 int encoding
= CE_7BIT
;
1982 size_t line_len
= 0;
1983 char buffer
[BUFSIZ
];
1986 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1987 advise (ce
->ce_file
, "unable to open for reading");
1991 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1992 while (encoding
!= CE_BINARY
&&
1993 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1996 int last_char_was_cr
= 0;
1998 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1999 if (*cp
== '\0' || ++line_len
> 998 ||
2000 (*cp
!= '\n' && last_char_was_cr
)) {
2001 encoding
= CE_BINARY
;
2003 *reason
= "null character";
2004 } else if (line_len
> 998) {
2005 *reason
= "line length > 998";
2006 } else if (*cp
!= '\n' && last_char_was_cr
) {
2007 *reason
= "CR not followed by LF";
2009 /* Should not reach this. */
2013 } else if (*cp
== '\n') {
2015 } else if (! isascii ((unsigned char) *cp
)) {
2019 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2025 } /* else should never happen */
2032 strip_crs (CT ct
, int *message_mods
) {
2033 char *charset
= content_charset (ct
);
2036 /* Only strip carriage returns if content is ASCII or another
2037 charset that has the same readily recognizable CR followed by a
2038 LF. We can include UTF-8 here because if the high-order bit of
2039 a UTF-8 byte is 0, then it must be a single-byte ASCII
2041 if (! strcasecmp (charset
, "US-ASCII") ||
2042 ! strcasecmp (charset
, "UTF-8") ||
2043 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2044 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2050 int opened_input_file
= 0;
2052 if (ct
->c_cefile
.ce_file
) {
2053 file
= &ct
->c_cefile
.ce_file
;
2054 fp
= &ct
->c_cefile
.ce_fp
;
2056 } else if (ct
->c_file
) {
2059 begin
= (size_t) ct
->c_begin
;
2060 end
= (size_t) ct
->c_end
;
2061 } /* else don't know where the content is */
2063 if (file
&& *file
&& fp
) {
2065 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2066 advise (*file
, "unable to open for reading");
2069 opened_input_file
= 1;
2075 char buffer
[BUFSIZ
];
2077 size_t bytes_to_read
=
2078 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2080 fseeko (*fp
, begin
, SEEK_SET
);
2081 while ((bytes_read
= fread (buffer
, 1,
2082 min (bytes_to_read
, sizeof buffer
),
2084 /* Look for CR followed by a LF. This is supposed to
2085 be text so there should be LF's. If not, don't
2086 modify the content. */
2089 int last_char_was_cr
= 0;
2091 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2093 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2094 if (*cp
== '\n' && last_char_was_cr
) {
2099 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2105 char *stripped_content_file
;
2106 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2108 if (tempfile
== NULL
) {
2109 adios (NULL
, "unable to create temporary file in %s",
2112 stripped_content_file
= add (tempfile
, NULL
);
2114 /* Strip each CR before a LF from the content. */
2115 fseeko (*fp
, begin
, SEEK_SET
);
2116 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2120 int last_char_was_cr
= 0;
2122 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2124 last_char_was_cr
= 1;
2125 } else if (last_char_was_cr
) {
2127 if (write (fd
, "\r", 1) < 0) {
2128 advise (tempfile
, "CR write");
2131 if (write (fd
, cp
, 1) < 0) {
2132 advise (tempfile
, "write");
2134 last_char_was_cr
= 0;
2136 if (write (fd
, cp
, 1) < 0) {
2137 advise (tempfile
, "write");
2139 last_char_was_cr
= 0;
2145 admonish (NULL
, "unable to write temporary file %s",
2146 stripped_content_file
);
2147 (void) m_unlink (stripped_content_file
);
2150 /* Replace the decoded file with the converted one. */
2151 if (ct
->c_cefile
.ce_file
) {
2152 if (ct
->c_cefile
.ce_unlink
) {
2153 (void) m_unlink (ct
->c_cefile
.ce_file
);
2155 free (ct
->c_cefile
.ce_file
);
2157 ct
->c_cefile
.ce_file
= stripped_content_file
;
2158 ct
->c_cefile
.ce_unlink
= 1;
2162 report (NULL
, ct
->c_partno
,
2163 begin
== 0 && end
== 0 ? "" : *file
,
2169 if (opened_input_file
) {
2183 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2186 switch (ct
->c_type
) {
2188 if (ct
->c_subtype
== TEXT_PLAIN
) {
2189 status
= convert_charset (ct
, dest_charset
, message_mods
);
2192 char *ct_charset
= content_charset (ct
);
2194 report (NULL
, ct
->c_partno
, ct
->c_file
,
2195 "convert %s to %s", ct_charset
, dest_charset
);
2199 char *ct_charset
= content_charset (ct
);
2201 report ("iconv", ct
->c_partno
, ct
->c_file
,
2202 "failed to convert %s to %s", ct_charset
, dest_charset
);
2208 case CT_MULTIPART
: {
2209 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2212 /* Should check to see if the body for this part is encoded?
2213 For now, it gets passed along as-is by InitMultiPart(). */
2214 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2216 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2222 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2223 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2226 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2239 * Fix various problems that aren't handled elsewhere. These
2240 * are fixed unconditionally: there are no switches to disable
2241 * them. (Currently, "problems" is just one: an extraneous
2242 * semicolon at the end of a header parameter list.)
2245 fix_always (CT ct
, int *message_mods
) {
2248 switch (ct
->c_type
) {
2249 case CT_MULTIPART
: {
2250 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2253 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2254 status
= fix_always (part
->mp_part
, message_mods
);
2260 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2261 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2263 status
= fix_always (e
->eb_content
, message_mods
);
2270 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2271 size_t len
= strlen (hf
->value
);
2273 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2274 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2275 /* Only do this for Content-Type and
2276 Content-Disposition fields because those are the
2277 only headers that parse_mime() warns about. */
2281 /* whitespace following a trailing ';' will be nuked as well */
2282 if (hf
->value
[len
- 1] == '\n') {
2283 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2284 if (len
-- == 0) { break; }
2288 if (hf
->value
[len
- 2] == ';') {
2289 /* Remove trailing ';' from parameter value. */
2290 hf
->value
[len
- 2] = '\n';
2291 hf
->value
[len
- 1] = '\0';
2293 /* Also, if Content-Type parameter, remove trailing ';'
2294 from ct->c_ctline. This probably isn't necessary
2296 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2297 size_t l
= strlen(ct
->c_ctline
) - 1;
2298 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2299 ct
->c_ctline
[l
] == ';') {
2300 ct
->c_ctline
[l
--] = '\0';
2301 if (l
== 0) { break; }
2307 report (NULL
, ct
->c_partno
, ct
->c_file
,
2308 "remove trailing ; from %s parameter value",
2320 write_content (CT ct
, const char *input_filename
, char *outfile
, int modify_inplace
,
2324 if (modify_inplace
) {
2325 if (message_mods
> 0) {
2326 if ((status
= output_message (ct
, outfile
)) == OK
) {
2327 char *infile
= input_filename
2328 ? add (input_filename
, NULL
)
2329 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
2331 if (remove_file (infile
) == OK
) {
2332 if (rename (outfile
, infile
)) {
2333 /* Rename didn't work, possibly because of an
2334 attempt to rename across filesystems. Try
2335 brute force copy. */
2336 int old
= open (outfile
, O_RDONLY
);
2338 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2341 if (old
!= -1 && new != -1) {
2342 char buffer
[BUFSIZ
];
2344 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2346 if (write (new, buffer
, i
) != i
) {
2352 if (new != -1) { close (new); }
2353 if (old
!= -1) { close (old
); }
2354 (void) m_unlink (outfile
);
2357 /* The -file argument processing used path() to
2358 expand filename to absolute path. */
2359 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2361 admonish (NULL
, "unable to rename %s %s to %s",
2362 file
? "file" : "message", outfile
,
2368 admonish (NULL
, "unable to remove input file %s, "
2369 "not modifying it", infile
);
2370 (void) m_unlink (outfile
);
2379 /* No modifications and didn't need the tmp outfile. */
2380 (void) m_unlink (outfile
);
2383 /* Output is going to some file. Produce it whether or not
2384 there were modifications. */
2385 status
= output_message (ct
, outfile
);
2394 * parse_mime() does not set lf_line_endings in struct text, so use this function to do it.
2395 * It touches the parts the decodetypes identifies.
2398 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
) {
2399 switch (ct
->c_type
) {
2400 case CT_MULTIPART
: {
2401 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2404 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2405 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2411 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2412 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2414 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2419 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2420 if (ct
->c_ctparams
== NULL
) {
2421 if ((ct
->c_ctparams
= (struct text
*) mh_xcalloc (1, sizeof (struct text
))) == NULL
) {
2422 adios (NULL
, "out of memory");
2425 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2432 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2433 * use the standard MH backup file.
2436 remove_file (const char *file
) {
2438 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2439 int status
= system (rmm_command
);
2442 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2444 /* This is OK for a non-message file, it still uses the
2445 BACKUP_PREFIX form. The backup file will be in the same
2446 directory as file. */
2447 return rename (file
, m_backup (file
));
2453 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2458 va_start (args
, message
);
2459 fmt
= concat (filename
, partno
? " part " : ", ",
2460 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
2462 advertise (what
, NULL
, fmt
, args
);
2475 fprintf (stderr
, "\n");