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|binary", 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, FIXCOMPOSITECTESW) \
31 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
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_composite_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 CT
build_multipart_alt (CT
, CT
, int, int);
112 static int boundary_in_content (FILE **, char *, const char *);
113 static void transfer_noncontent_headers (CT
, CT
);
114 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
115 static int decode_text_parts (CT
, int, const char *, int *);
116 static int should_decode(const char *, const char *, const char *);
117 static int content_encoding (CT
, const char **);
118 static int strip_crs (CT
, int *);
119 static void update_cte (CT
);
120 static int least_restrictive_encoding (CT
);
121 static int less_restrictive (int, int);
122 static int convert_charsets (CT
, char *, int *);
123 static int fix_always (CT
, int *);
124 static int fix_filename_param (char *, char *, PM
*, PM
*);
125 static int fix_filename_encoding (CT
);
126 static int write_content (CT
, const char *, char *, int, int);
127 static void set_text_ctparams(CT
, char *, int);
128 static int remove_file (const char *);
129 static void report (char *, char *, char *, char *, ...);
130 static void pipeser (int);
134 main (int argc
, char **argv
) {
136 char *cp
, *file
= NULL
, *folder
= NULL
;
137 char *maildir
, buf
[100], *outfile
= NULL
;
138 char **argp
, **arguments
;
139 struct msgs_array msgs
= { 0, 0, NULL
};
140 struct msgs
*mp
= NULL
;
146 fix_transformations fx
;
147 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
149 fx
.replacetextplain
= 0;
150 fx
.decodetext
= CE_8BIT
;
151 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
152 fx
.lf_line_endings
= 0;
153 fx
.textcharset
= NULL
;
155 if (nmh_init(argv
[0], 2)) { return 1; }
159 arguments
= getarguments (invo_name
, argc
, argv
, 1);
165 while ((cp
= *argp
++)) {
167 switch (smatch (++cp
, switches
)) {
169 ambigsw (cp
, switches
);
172 adios (NULL
, "-%s unknown", cp
);
175 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
177 print_help (buf
, switches
, 1);
180 print_version(invo_name
);
184 if (! (cp
= *argp
++) || *cp
== '-') {
185 adios (NULL
, "missing argument to %s", argp
[-2]);
187 if (! strcasecmp (cp
, "8bit")) {
188 fx
.decodetext
= CE_8BIT
;
189 } else if (! strcasecmp (cp
, "7bit")) {
190 fx
.decodetext
= CE_7BIT
;
191 } else if (! strcasecmp (cp
, "binary")) {
192 fx
.decodetext
= CE_BINARY
;
194 adios (NULL
, "invalid argument to %s", argp
[-2]);
201 if (! (cp
= *argp
++) || *cp
== '-') {
202 adios (NULL
, "missing argument to %s", argp
[-2]);
206 case CRLFLINEBREAKSSW
:
207 fx
.lf_line_endings
= 0;
209 case NCRLFLINEBREAKSSW
:
210 fx
.lf_line_endings
= 1;
213 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
214 adios (NULL
, "missing argument to %s", argp
[-2]);
227 case FIXCOMPOSITECTESW
:
228 fx
.fixcompositecte
= 1;
230 case NFIXCOMPOSITECTESW
:
231 fx
.fixcompositecte
= 0;
234 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
235 adios (NULL
, "missing argument to %s", argp
[-2]);
237 if (! strncasecmp (cp
, "multipart/", 10) ||
238 ! strncasecmp (cp
, "message/", 8)) {
239 adios (NULL
, "-fixtype %s not allowed", cp
);
240 } else if (! strchr (cp
, '/')) {
241 adios (NULL
, "-fixtype requires type/subtype");
243 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
244 svector_push_back (fx
.fixtypes
, cp
);
252 case REPLACETEXTPLAINSW
:
253 fx
.replacetextplain
= 1;
255 case NREPLACETEXTPLAINSW
:
256 fx
.replacetextplain
= 0;
259 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
260 adios (NULL
, "missing argument to %s", argp
[-2]);
262 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
265 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
266 adios (NULL
, "missing argument to %s", argp
[-2]);
268 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
271 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
272 adios (NULL
, "missing argument to %s", argp
[-2]);
292 if (*cp
== '+' || *cp
== '@') {
294 adios (NULL
, "only one folder at a time!");
296 folder
= pluspath (cp
);
300 /* Interpret a full path as a filename, not a message. */
301 file
= add (cp
, NULL
);
303 app_msgarg (&msgs
, cp
);
308 SIGNAL (SIGQUIT
, quitser
);
309 SIGNAL (SIGPIPE
, pipeser
);
312 * Read the standard profile setup
314 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
315 readconfig ((struct node
**) 0, fp
, cp
, 0);
319 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
320 suppress_extraneous_trailing_semicolon_warning
= 1;
322 if (! context_find ("path")) {
323 free (path ("./", TFOLDER
));
326 if (file
&& msgs
.size
) {
327 adios (NULL
, "cannot specify msg and file at same time!");
331 * check if message is coming from file
334 /* If file is stdin, create a tmp file name before parse_mime()
335 has a chance, because it might put in on a different
336 filesystem than the output file. Instead, put it in the
337 user's preferred tmp directory. */
340 if (! strcmp ("-", file
)) {
346 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
347 adios (NULL
, "unable to create temporary file in %s",
351 file
= add (cp
, NULL
);
352 cpydata (STDIN_FILENO
, fd
, "-", file
);
356 (void) m_unlink (file
);
357 adios (NULL
, "failed to write temporary file");
361 cts
= mh_xcalloc(2, sizeof *cts
);
364 if ((ct
= parse_mime (file
))) {
365 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
368 advise (NULL
, "unable to parse message from file %s", file
);
371 /* If there's an outfile, pass the input message unchanged, so the message won't
372 get dropped from a pipeline. */
374 /* Something went wrong. Output might be expected, such as if this were run
375 as a filter. Just copy the input to the output. */
376 if (copy_input_to_output (file
, outfile
) != OK
) {
377 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
383 * message(s) are coming from a folder
388 app_msgarg(&msgs
, "cur");
391 folder
= getfolder (1);
393 maildir
= m_maildir (folder
);
395 if (chdir (maildir
) == NOTOK
) {
396 adios (maildir
, "unable to change directory to");
399 /* read folder and create message structure */
400 if (! (mp
= folder_read (folder
, 1))) {
401 adios (NULL
, "unable to read folder %s", folder
);
404 /* check for empty folder */
405 if (mp
->nummsg
== 0) {
406 adios (NULL
, "no messages in %s", folder
);
409 /* parse all the message ranges/sequences and set SELECTED */
410 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
411 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
414 seq_setprev (mp
); /* set the previous-sequence */
416 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
419 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
420 if (is_selected(mp
, msgnum
)) {
423 msgnam
= m_name (msgnum
);
424 if ((ct
= parse_mime (msgnam
))) {
425 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
428 advise (NULL
, "unable to parse message %s", msgnam
);
431 /* If there's an outfile, pass the input message unchanged, so the message won't
432 get dropped from a pipeline. */
434 /* Something went wrong. Output might be expected, such as if this were run
435 as a filter. Just copy the input to the output. */
436 const char *input_filename
= path (msgnam
, TFILE
);
438 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
439 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
447 seq_setcur (mp
, mp
->hghsel
); /* update current message */
449 seq_save (mp
); /* synchronize sequences */
450 context_replace (pfolder
, folder
);/* update current folder */
451 context_save (); /* save the context file */
455 for (ctp
= cts
; *ctp
; ++ctp
) {
456 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
459 (void) m_unlink (file
);
462 /* Just calling m_backup() unlinks the backup file. */
463 (void) m_backup (file
);
471 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
477 /* done is freects_done, which will clean up all of cts. */
484 * Apply transformations to one message.
487 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
488 /* Store input filename in case one of the transformations, i.e.,
489 fix_boundary(), rewrites to a tmp file. */
490 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
491 int modify_inplace
= 0;
492 int message_mods
= 0;
495 if (outfile
== NULL
) {
498 if ((*ctp
)->c_file
) {
500 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
501 adios (NULL
, "unable to create temporary file in %s",
504 outfile
= add (tempfile
, NULL
);
506 adios (NULL
, "missing both input and output filenames\n");
510 reverse_alternative_parts (*ctp
);
511 status
= fix_always (*ctp
, &message_mods
);
512 if (status
== OK
&& fx
->fixboundary
) {
513 status
= fix_boundary (ctp
, &message_mods
);
515 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
516 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
518 if (status
== OK
&& fx
->fixcompositecte
) {
519 status
= fix_composite_cte (*ctp
, &message_mods
);
521 if (status
== OK
&& fx
->reformat
) {
523 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
525 if (status
== OK
&& fx
->decodetext
) {
526 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
530 if (status
== OK
&& fx
->textcharset
!= NULL
) {
531 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
534 if (status
== OK
&& ! (*ctp
)->c_umask
) {
535 /* Set the umask for the contents file. This currently
536 isn't used but just in case it is in the future. */
539 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
540 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
542 (*ctp
)->c_umask
= ~m_gmprot();
547 * Write the content to a file
550 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
552 } else if (! modify_inplace
) {
553 /* Something went wrong. Output might be expected, such
554 as if this were run as a filter. Just copy the input
556 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
557 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
561 if (modify_inplace
) {
562 if (status
!= OK
) { (void) m_unlink (outfile
); }
567 free (input_filename
);
574 * Copy input message to output. Assumes not modifying in place, so this
575 * might be running as part of a pipeline.
578 copy_input_to_output (const char *input_filename
, const char *output_filename
) {
579 int in
= open (input_filename
, O_RDONLY
);
580 int out
= strcmp (output_filename
, "-")
581 ? open (output_filename
, O_WRONLY
| O_CREAT
, m_gmprot ())
585 if (in
!= -1 && out
!= -1) {
586 cpydata (in
, out
, input_filename
, output_filename
);
599 * Fix mismatched outer level boundary.
602 fix_boundary (CT
*ct
, int *message_mods
) {
603 struct multipart
*mp
;
606 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
607 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
610 * 1) Get boundary at end of part.
611 * 2) Get boundary at beginning of part and compare to the end-of-part
613 * 3) Write out contents of ct to tmp file, replacing boundary in
614 * header with boundary from part. Set c_unlink to 1.
616 * 5) Call parse_mime() on the tmp file, replacing ct.
619 if (mp
&& mp
->mp_start
) {
622 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
625 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
626 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
627 char *filename
= add ((*ct
)->c_file
, NULL
);
631 if ((fixed_ct
= parse_mime (fixed
))) {
637 report (NULL
, NULL
, filename
,
638 "fix multipart boundary");
642 advise (NULL
, "unable to parse fixed part");
647 advise (NULL
, "unable to replace broken boundary");
651 advise (NULL
, "unable to create temporary file in %s",
656 free (part_boundary
);
658 /* Couldn't fix the boundary. Report failure so that mhfixmsg
659 doesn't modify the message. */
663 /* No multipart struct, even though the content type is
664 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
675 * Find boundary at end of multipart.
678 get_multipart_boundary (CT ct
, char **part_boundary
) {
680 char *end_boundary
= NULL
;
681 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
682 ? (off_t
) (ct
->c_end
- sizeof buffer
)
683 : (off_t
) ct
->c_begin
;
687 /* This will fail if the boundary spans fread() calls. BUFSIZ should
688 be big enough, even if it's just 1024, to make that unlikely. */
690 /* free_content() will close ct->c_fp. */
691 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
692 advise (ct
->c_file
, "unable to open for reading");
696 /* Get boundary at end of multipart. */
697 while (begin
>= (off_t
) ct
->c_begin
) {
698 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
699 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
700 char *cp
= rfind_str (buffer
, bytes_read
, "--");
705 /* Trim off trailing "--" and anything beyond. */
707 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
708 if (strlen (end
) > 3 && *end
++ == '\n' &&
709 *end
++ == '-' && *end
++ == '-') {
710 end_boundary
= add (end
, NULL
);
717 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
718 begin
-= sizeof buffer
;
724 /* Get boundary at beginning of multipart. */
726 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
727 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
728 if (bytes_read
>= strlen (end_boundary
)) {
729 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
731 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
732 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
737 /* The start and end boundaries didn't match, or the
738 start boundary doesn't begin with "\n--" (or "--"
739 if at the beginning of buffer). Keep trying. */
748 *part_boundary
= end_boundary
;
750 *part_boundary
= NULL
;
759 * Open and copy ct->c_file to file, replacing the multipart boundary.
762 replace_boundary (CT ct
, char *file
, char *boundary
) {
765 char buf
[BUFSIZ
], name
[NAMESZ
];
767 m_getfld_state_t gstate
= 0;
770 if (ct
->c_file
== NULL
) {
771 advise (NULL
, "missing input filename");
775 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
776 advise (ct
->c_file
, "unable to open for reading");
780 if ((fpout
= fopen (file
, "w")) == NULL
) {
782 advise (file
, "unable to open for writing");
786 for (compnum
= 1;;) {
787 int bufsz
= (int) sizeof buf
;
789 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
794 /* get copies of the buffers */
795 np
= add (name
, NULL
);
796 vp
= add (buf
, NULL
);
798 /* if necessary, get rest of field */
799 while (state
== FLDPLUS
) {
801 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
802 vp
= add (buf
, vp
); /* add to previous value */
805 if (strcasecmp (TYPE_FIELD
, np
)) {
806 fprintf (fpout
, "%s:%s", np
, vp
);
808 char *new_ctline
, *new_params
;
810 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
811 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
814 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
815 ct
->c_ctinfo
.ci_subtype
, NULL
);
816 new_params
= output_params(strlen(TYPE_FIELD
) +
817 strlen(new_ctline
) + 1,
818 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
819 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
820 new_params
? new_params
: "");
822 mh_xfree(new_params
);
832 /* buf will have a terminating NULL, skip it. */
833 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
834 advise (file
, "fwrite");
843 advise (NULL
, "message format error in component #%d", compnum
);
848 advise (NULL
, "getfld() returned %d", state
);
856 m_getfld_state_destroy (&gstate
);
865 * Fix Content-Type header to reflect the content of its part.
868 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
871 switch (ct
->c_type
) {
873 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
876 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
877 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
883 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
884 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
886 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
893 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
894 for (typep
= svector_strs (fixtypes
);
895 typep
&& (type
= *typep
);
898 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
901 if (! strcasecmp (type
, type_subtype
) &&
902 decode_part (ct
) == OK
&&
903 ct
->c_cefile
.ce_file
!= NULL
) {
904 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
907 if ((cp
= strchr (ct_type_subtype
, ';'))) {
908 /* Truncate to remove any parameter list from
909 mime_type () result. */
913 if (strcasecmp (type
, ct_type_subtype
)) {
914 char *ct_type
, *ct_subtype
;
917 /* The Content-Type header does not match the
918 content, so update these struct Content
921 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
924 /* Extract type and subtype from type/subtype. */
925 ct_type
= mh_xstrdup(ct_type_subtype
);
926 if ((cp
= strchr (ct_type
, '/'))) {
928 ct_subtype
= mh_xstrdup(++cp
);
930 advise (NULL
, "missing / in MIME type of %s %s",
931 ct
->c_file
, ct
->c_partno
);
936 ct
->c_type
= ct_str_type (ct_type
);
937 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
939 free (ct
->c_ctinfo
.ci_type
);
940 ct
->c_ctinfo
.ci_type
= ct_type
;
941 free (ct
->c_ctinfo
.ci_subtype
);
942 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
943 if (! replace_substring (&ct
->c_ctline
, type
,
945 advise (NULL
, "did not find %s in %s",
949 /* Update Content-Type header field. */
950 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
951 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
952 if (replace_substring (&hf
->value
, type
,
956 report (NULL
, ct
->c_partno
, ct
->c_file
,
957 "change Content-Type in header "
959 type
, ct_type_subtype
);
963 advise (NULL
, "did not find %s in %s",
969 free (ct_type_subtype
);
981 * Replace a substring, allocating space to hold the new one.
984 replace_substring (char **str
, const char *old
, const char *new) {
987 if ((cp
= strstr (*str
, old
))) {
988 char *remainder
= cp
+ strlen (old
);
989 char *prefix
, *new_str
;
992 prefix
= mh_xstrdup(*str
);
993 *(prefix
+ (cp
- *str
)) = '\0';
994 new_str
= concat (prefix
, new, remainder
, NULL
);
997 new_str
= concat (new, remainder
, NULL
);
1002 return *str
= new_str
;
1010 * Remove a name=value parameter, given just its name, from a header value.
1013 remove_parameter (char *str
, const char *name
) {
1014 /* It looks to me, based on the BNF in RFC 2045, than there can't
1015 be whitespace betwwen the parameter name and the "=", or
1016 between the "=" and the parameter value. */
1017 char *param_name
= concat (name
, "=", NULL
);
1020 if ((cp
= strstr (str
, param_name
))) {
1024 /* Remove any leading spaces, before the parameter name. */
1026 start
> str
&& isspace ((unsigned char) *(start
-1));
1030 /* Remove a leading semicolon. */
1031 if (start
> str
&& *(start
-1) == ';') { --start
; }
1033 end
= cp
+ strlen (name
) + 1;
1035 /* Skip past the quoted value, and then the final quote. */
1036 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1039 /* Skip past the value. */
1040 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1043 /* Count how many characters need to be moved. Include
1044 trailing null, which is accounted for by the
1045 initialization of count to 1. */
1046 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1047 (void) memmove (start
, end
, count
);
1057 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1058 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1062 fix_composite_cte (CT ct
, int *message_mods
) {
1065 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1066 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1067 ct
->c_encoding
!= CE_BINARY
) {
1070 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1071 char *name
= hf
->name
;
1072 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
1076 if (! strncasecmp (name
, ENCODING_FIELD
,
1077 strlen (ENCODING_FIELD
))) {
1078 char *prefix
= "Nmh-REPLACED-INVALID-";
1082 h
->name
= add (hf
->name
, NULL
);
1083 h
->hf_encoding
= hf
->hf_encoding
;
1087 /* Retain old header but prefix its name. */
1089 hf
->name
= concat (prefix
, h
->name
, NULL
);
1093 char *encoding
= cpytrim (hf
->value
);
1094 report (NULL
, ct
->c_partno
, ct
->c_file
,
1095 "replace Content-Transfer-Encoding of %s "
1096 "with 8 bit", encoding
);
1100 h
->value
= add (" 8bit\n", NULL
);
1102 /* Don't need to warn for multiple C-T-E header
1103 fields, parse_mime() already does that. But
1104 if there are any, fix them all as necessary. */
1109 set_ce (ct
, CE_8BIT
);
1112 if (ct
->c_type
== CT_MULTIPART
) {
1113 struct multipart
*m
;
1116 m
= (struct multipart
*) ct
->c_ctparams
;
1117 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1118 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1131 * Set content encoding.
1134 set_ce (CT ct
, int encoding
) {
1135 const char *ce
= ce_str (encoding
);
1136 const struct str2init
*ctinit
= get_ce_method (ce
);
1139 char *cte
= concat (" ", ce
, "\n", NULL
);
1142 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1143 caller is decode_text_parts (). Save because we'll
1145 struct cefile decoded_content_info
= ct
->c_cefile
;
1147 ct
->c_encoding
= encoding
;
1149 ct
->c_ctinitfnx
= ctinit
->si_init
;
1150 /* This will assign ct->c_cefile with an all-0 struct, which
1152 (*ctinit
->si_init
) (ct
);
1153 /* After returning, the caller should set
1154 ct->c_cefile.ce_file to the name of the file containing
1157 if (ct
->c_ceclosefnx
) {
1158 (*ct
->c_ceclosefnx
) (ct
);
1161 /* Restore the cefile. */
1162 ct
->c_cefile
= decoded_content_info
;
1164 /* Update/add Content-Transfer-Encoding header field. */
1165 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1166 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1173 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
1176 /* Update c_celine. It's used only by mhlist -debug. */
1177 free (ct
->c_celine
);
1178 ct
->c_celine
= add (cte
, NULL
);
1188 * Make sure each text part has a corresponding text/plain part.
1191 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1194 switch ((*ct
)->c_type
) {
1196 /* Nothing to do for text/plain. */
1197 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1199 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1200 parent
->c_subtype
== MULTI_ALTERNATE
) {
1201 int new_subpart_number
= 1;
1202 int has_text_plain
=
1203 find_textplain_sibling (parent
, replacetextplain
,
1204 &new_subpart_number
);
1206 if (! has_text_plain
) {
1207 /* Parent is a multipart/alternative. Insert a new
1208 text/plain subpart. */
1209 const int inserted
=
1210 insert_new_text_plain_part (*ct
, new_subpart_number
,
1215 report (NULL
, parent
->c_partno
, parent
->c_file
,
1216 "insert text/plain part");
1222 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1223 parent
->c_subtype
== MULTI_RELATED
) {
1224 char *type_subtype
=
1225 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1226 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1227 const char *parent_type
=
1228 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1229 int new_subpart_number
= 1;
1230 int has_text_plain
= 0;
1232 /* Have to do string comparison on the subtype because we
1233 don't enumerate all of them in c_subtype values.
1234 parent_type will be NULL if the multipart/related part
1235 doesn't have a type parameter. The type parameter must
1236 be specified according to RFC 2387 Sec. 3.1 but not all
1238 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1239 /* The type of this part matches the root type of the
1240 parent multipart/related. Look to see if there's
1241 text/plain sibling. */
1243 find_textplain_sibling (parent
, replacetextplain
,
1244 &new_subpart_number
);
1247 free (type_subtype
);
1249 if (! has_text_plain
) {
1250 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1254 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1255 if (*ct
!= part
->mp_part
) {
1261 /* Parent is a multipart/related. Insert a new
1262 text/plain subpart in a new multipart/alternative. */
1263 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1264 /* Not an error if text/plain couldn't be added. */
1267 /* There are no siblings, so insert a new text/plain
1268 subpart, and change the parent type from
1269 multipart/related to multipart/alternative. */
1270 const int inserted
=
1271 insert_new_text_plain_part (*ct
, new_subpart_number
,
1277 parent
->c_subtype
= MULTI_ALTERNATE
;
1278 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1279 if (! replace_substring (&parent
->c_ctline
, "/related",
1282 "did not find multipart/related in %s",
1286 /* Update Content-Type header field. */
1287 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1288 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1289 if (replace_substring (&hf
->value
, "/related",
1293 report (NULL
, parent
->c_partno
,
1295 "insert text/plain part");
1298 /* Remove, e.g., type="text/html" from
1299 multipart/alternative. */
1300 remove_parameter (hf
->value
, "type");
1303 advise (NULL
, "did not find multipart/"
1304 "related in header %s",
1310 /* Not an error if text/plain couldn't be inserted. */
1315 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1322 case CT_MULTIPART
: {
1323 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1326 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1327 if ((*ct
)->c_type
== CT_MULTIPART
) {
1328 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1336 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1337 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1339 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1350 * See if there is a sibling text/plain, and return its subpart number.
1353 find_textplain_sibling (CT parent
, int replacetextplain
,
1354 int *new_subpart_number
) {
1355 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1356 struct part
*part
, *prev
;
1357 int has_text_plain
= 0;
1359 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1360 ++*new_subpart_number
;
1361 if (part
->mp_part
->c_type
== CT_TEXT
&&
1362 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1363 if (replacetextplain
) {
1364 struct part
*old_part
;
1365 if (part
== mp
->mp_parts
) {
1366 old_part
= mp
->mp_parts
;
1367 mp
->mp_parts
= part
->mp_next
;
1369 old_part
= prev
->mp_next
;
1370 prev
->mp_next
= part
->mp_next
;
1373 report (NULL
, parent
->c_partno
, parent
->c_file
,
1374 "remove text/plain part %s",
1375 old_part
->mp_part
->c_partno
);
1377 free_content (old_part
->mp_part
);
1387 return has_text_plain
;
1392 * Insert a new text/plain part.
1395 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1396 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1397 struct part
*new_part
;
1400 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1402 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1404 new_part
->mp_next
= mp
->mp_parts
;
1405 mp
->mp_parts
= new_part
;
1406 new_part
->mp_part
->c_partno
=
1407 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1413 free_content (new_part
->mp_part
);
1421 * Create a text/plain part to go along with non-plain sibling part.
1424 build_text_plain_part (CT encoded_part
) {
1425 CT tp_part
= divide_part (encoded_part
);
1426 char *tmp_plain_file
= NULL
;
1428 if (decode_part (tp_part
) == OK
) {
1429 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1430 contains the decoded contents. And the decoding function, such
1431 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1432 be unlinked by free_content (). */
1435 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1436 advise (NULL
, "unable to create temporary file in %s",
1439 tmp_plain_file
= add (tempfile
, NULL
);
1440 if (reformat_part (tp_part
, tmp_plain_file
,
1441 tp_part
->c_ctinfo
.ci_type
,
1442 tp_part
->c_ctinfo
.ci_subtype
,
1443 tp_part
->c_type
) == OK
) {
1449 free_content (tp_part
);
1450 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1451 free (tmp_plain_file
);
1458 * Slip new text/plain part into a new multipart/alternative.
1461 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1462 CT tp_part
= build_text_plain_part (*ct
);
1466 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1469 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1471 if (mp
&& mp
->mp_parts
) {
1472 mp
->mp_parts
->mp_part
= tp_part
;
1473 /* Make the new multipart/alternative the parent. */
1478 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1479 "insert text/plain part");
1482 free_content (tp_part
);
1483 free_content (mp_alt
);
1490 /* Not an error if text/plain couldn't be built. */
1498 * Clone a MIME part.
1501 divide_part (CT ct
) {
1505 /* Just copy over what is needed for decoding. c_vrsn and
1506 c_celine aren't necessary. */
1507 new_part
->c_file
= add (ct
->c_file
, NULL
);
1508 new_part
->c_begin
= ct
->c_begin
;
1509 new_part
->c_end
= ct
->c_end
;
1510 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1511 new_part
->c_type
= ct
->c_type
;
1512 new_part
->c_cefile
= ct
->c_cefile
;
1513 new_part
->c_encoding
= ct
->c_encoding
;
1514 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1515 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1516 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1517 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1519 /* c_ctline is used by reformat__part(), so it can preserve
1520 anything after the type/subtype. */
1521 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1528 * Copy the content info from one part to another.
1531 copy_ctinfo (CI dest
, CI src
) {
1534 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1535 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1537 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1538 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1540 if (s_pm
->pm_charset
) {
1541 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1543 if (s_pm
->pm_lang
) {
1544 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1548 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1549 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1557 decode_part (CT ct
) {
1562 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1563 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1565 tmp_decoded
= add (tempfile
, NULL
);
1566 /* The following call will load ct->c_cefile.ce_file with the tmp
1567 filename of the decoded content. tmp_decoded will contain the
1568 encoded output, get rid of that. */
1569 status
= output_message (ct
, tmp_decoded
);
1570 (void) m_unlink (tmp_decoded
);
1578 * Reformat content as plain text.
1579 * Some of the arguments aren't really needed now, but maybe will
1580 * be in the future for other than text types.
1583 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1584 int output_subtype
, output_encoding
;
1585 const char *reason
= NULL
;
1589 /* Hacky: this redirects the output from whatever command is used
1590 to show the part to a file. So, the user can't have any output
1591 redirection in that command.
1592 Could show_multi() in mhshowsbr.c avoid this? */
1594 /* Check for invo_name-format-type/subtype. */
1595 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1597 advise (NULL
, "Don't know how to convert %s, there is no "
1598 "%s-format-%s/%s profile entry",
1599 ct
->c_file
, invo_name
, type
, subtype
);
1603 if (strchr (cf
, '>')) {
1604 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1605 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1606 subtype
? subtype
: "");
1611 cp
= concat (cf
, " >", file
, NULL
);
1612 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1615 /* Unlink decoded content tmp file and free its filename to avoid
1616 leaks. The file stream should already have been closed. */
1617 if (ct
->c_cefile
.ce_unlink
) {
1618 (void) m_unlink (ct
->c_cefile
.ce_file
);
1619 free (ct
->c_cefile
.ce_file
);
1620 ct
->c_cefile
.ce_file
= NULL
;
1621 ct
->c_cefile
.ce_unlink
= 0;
1624 if (c_type
== CT_TEXT
) {
1625 output_subtype
= TEXT_PLAIN
;
1627 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1631 output_encoding
= content_encoding (ct
, &reason
);
1632 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1633 ct
->c_cefile
.ce_file
= file
;
1634 ct
->c_cefile
.ce_unlink
= 1;
1636 ct
->c_cefile
.ce_unlink
= 0;
1645 * Fill in a multipart/alternative part.
1648 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1649 char *boundary_prefix
= "----=_nmh-multipart";
1650 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1651 char *boundary_indicator
= "; boundary=";
1652 char *typename
, *subtypename
, *name
;
1655 struct multipart
*m
;
1656 const struct str2init
*ctinit
;
1660 /* Set up the multipart/alternative part. These fields of *ct were
1661 initialized to 0 by mh_xcalloc():
1662 c_fp, c_unlink, c_begin, c_end,
1663 c_vrsn, c_ctline, c_celine,
1664 c_id, c_descr, c_dispo, c_partno,
1665 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1666 c_cefile, c_encoding,
1667 c_digested, c_digest[16], c_ctexbody,
1668 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1670 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1673 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1675 ct
->c_subtype
= subtype
;
1677 ctinit
= get_ct_init (ct
->c_type
);
1679 typename
= ct_type_str (type
);
1680 subtypename
= ct_subtype_str (type
, subtype
);
1684 int found_boundary
= 1;
1686 while (found_boundary
&& serial
< 1000000) {
1689 /* Ensure that the boundary doesn't appear in the decoded
1691 if (new_part
->c_cefile
.ce_file
) {
1692 if ((found_boundary
=
1693 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1694 new_part
->c_cefile
.ce_file
,
1701 /* Ensure that the boundary doesn't appear in the encoded
1703 if (! found_boundary
&& new_part
->c_file
) {
1704 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1712 if (found_boundary
) {
1713 /* Try a slightly different boundary. */
1718 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1720 concat (boundary_prefix
,
1721 first_alt
->c_partno
? first_alt
->c_partno
: "",
1722 "-", buffer2
, NULL
);
1726 if (found_boundary
) {
1727 advise (NULL
, "giving up trying to find a unique boundary");
1733 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1734 boundary
, "\"", NULL
);
1736 /* Load c_first_hf and c_last_hf. */
1737 transfer_noncontent_headers (first_alt
, ct
);
1738 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1741 /* Load c_partno. */
1742 if (first_alt
->c_partno
) {
1743 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1744 free (first_alt
->c_partno
);
1745 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1746 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1748 first_alt
->c_partno
= add ("1", NULL
);
1749 new_part
->c_partno
= add ("2", NULL
);
1753 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1754 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1757 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1758 "boundary", boundary
, 0);
1762 p
->mp_next
->mp_next
= NULL
;
1763 p
->mp_next
->mp_part
= first_alt
;
1766 m
->mp_start
= concat (boundary
, "\n", NULL
);
1767 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1778 * Check that the boundary does not appear in the content.
1781 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1782 char buffer
[BUFSIZ
];
1784 int found_boundary
= 0;
1786 /* free_content() will close *fp if we fopen it here. */
1787 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1788 advise (file
, "unable to open %s for reading", file
);
1792 fseeko (*fp
, 0L, SEEK_SET
);
1793 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1794 if (find_str (buffer
, bytes_read
, boundary
)) {
1800 return found_boundary
;
1805 * Remove all non-Content headers.
1808 transfer_noncontent_headers (CT old
, CT
new) {
1811 hp_prev
= hp
= old
->c_first_hf
;
1815 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1816 if (hp
== old
->c_last_hf
) {
1817 if (hp
== old
->c_first_hf
) {
1818 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1820 hp_prev
->next
= NULL
;
1821 old
->c_last_hf
= hp_prev
;
1824 if (hp
== old
->c_first_hf
) {
1825 old
->c_first_hf
= next
;
1827 hp_prev
->next
= next
;
1831 /* Put node hp in the new CT. */
1832 if (new->c_first_hf
== NULL
) {
1833 new->c_first_hf
= hp
;
1835 new->c_last_hf
->next
= hp
;
1837 new->c_last_hf
= hp
;
1839 /* A Content- header, leave in old. */
1852 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1853 char *typename
= ct_type_str (type
);
1854 char *subtypename
= ct_subtype_str (type
, subtype
);
1855 /* E.g, " text/plain" */
1856 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1857 /* E.g, " text/plain\n" */
1858 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1859 int found_content_type
= 0;
1861 const char *cp
= NULL
;
1865 /* Update/add Content-Type header field. */
1866 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1867 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1868 found_content_type
= 1;
1870 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1871 ? concat (type_subtypename
, cp
, "\n", NULL
)
1872 : add (name_plus_nl
, NULL
);
1875 if (! found_content_type
) {
1876 add_header (ct
, add (TYPE_FIELD
, NULL
),
1877 (cp
= strchr (ct
->c_ctline
, ';'))
1878 ? concat (type_subtypename
, cp
, "\n", NULL
)
1879 : add (name_plus_nl
, NULL
));
1882 /* Some of these might not be used, but set them anyway. */
1884 ? concat (type_subtypename
, cp
, NULL
)
1885 : concat (type_subtypename
, NULL
);
1886 free (ct
->c_ctline
);
1887 ct
->c_ctline
= ctline
;
1888 /* Leave other ctinfo members as they were. */
1889 free (ct
->c_ctinfo
.ci_type
);
1890 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1891 free (ct
->c_ctinfo
.ci_subtype
);
1892 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1894 ct
->c_subtype
= subtype
;
1896 free (name_plus_nl
);
1897 free (type_subtypename
);
1899 status
= set_ce (ct
, encoding
);
1906 * It's not necessary to update the charset parameter of a Content-Type
1907 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1908 * (content) was originally in the specified charset, "and will be in
1909 * that character set again after decoding."
1912 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
1913 int *message_mods
) {
1915 int lf_line_endings
= 0;
1917 switch (ct
->c_type
) {
1918 case CT_MULTIPART
: {
1919 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1922 /* Should check to see if the body for this part is encoded?
1923 For now, it gets passed along as-is by InitMultiPart(). */
1924 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1925 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
1932 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1933 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1935 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
1941 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
1946 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
1948 switch (ct
->c_encoding
) {
1953 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1954 const char *reason
= NULL
;
1956 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1957 && encoding
!= CE_BINARY
) {
1958 /* The decoding isn't acceptable so discard it.
1959 Leave status as OK to allow other transformations. */
1961 report (NULL
, ct
->c_partno
, ct
->c_file
,
1962 "will not decode%s because it is binary (%s)",
1964 : ct
->c_ctline
? ct
->c_ctline
1968 (void) m_unlink (ct
->c_cefile
.ce_file
);
1969 free (ct
->c_cefile
.ce_file
);
1970 ct
->c_cefile
.ce_file
= NULL
;
1971 } else if (ct
->c_encoding
== CE_QUOTED
&&
1972 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1973 /* The decoding isn't acceptable so discard it.
1974 Leave status as OK to allow other transformations. */
1976 report (NULL
, ct
->c_partno
, ct
->c_file
,
1977 "will not decode%s because it is 8bit",
1979 : ct
->c_ctline
? ct
->c_ctline
1982 (void) m_unlink (ct
->c_cefile
.ce_file
);
1983 free (ct
->c_cefile
.ce_file
);
1984 ct
->c_cefile
.ce_file
= NULL
;
1988 if (ct_encoding
== CE_BINARY
) {
1990 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1995 if (set_ce (ct
, enc
) == OK
) {
1998 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
1999 ct
->c_ctline
? ct
->c_ctline
: "");
2001 if (lf_line_endings
) {
2002 strip_crs (ct
, message_mods
);
2015 if (lf_line_endings
) {
2016 strip_crs (ct
, message_mods
);
2031 * Determine if the part with type[/subtype] should be decoded, according to
2032 * decodetypes (which came from the -decodetypes switch).
2035 should_decode(const char *decodetypes
, const char *type
, const char *subtype
) {
2036 /* Quick search for matching type[/subtype] in decodetypes: bracket
2037 decodetypes with commas, then search for ,type, and ,type/subtype, in
2040 int found_match
= 0;
2041 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2042 char *delimited_type
= concat(",", type
, ",", NULL
);
2044 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2046 } else if (subtype
!= NULL
) {
2047 char *delimited_type_subtype
=
2048 concat(",", type
, "/", subtype
, ",", NULL
);
2050 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2053 free(delimited_type_subtype
);
2056 free(delimited_type
);
2057 free(delimited_decodetypes
);
2064 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2065 * if it has any NUL characters, a CR not followed by a LF, or lines
2066 * greater than 998 characters in length. If binary, reason is set
2067 * to a string explaining why.
2070 content_encoding (CT ct
, const char **reason
) {
2071 CE ce
= &ct
->c_cefile
;
2072 int encoding
= CE_7BIT
;
2075 size_t line_len
= 0;
2076 char buffer
[BUFSIZ
];
2079 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2080 advise (ce
->ce_file
, "unable to open for reading");
2084 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2085 while (encoding
!= CE_BINARY
&&
2086 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2089 int last_char_was_cr
= 0;
2091 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2092 if (*cp
== '\0' || ++line_len
> 998 ||
2093 (*cp
!= '\n' && last_char_was_cr
)) {
2094 encoding
= CE_BINARY
;
2096 *reason
= "null character";
2097 } else if (line_len
> 998) {
2098 *reason
= "line length > 998";
2099 } else if (*cp
!= '\n' && last_char_was_cr
) {
2100 *reason
= "CR not followed by LF";
2102 /* Should not reach this. */
2106 } else if (*cp
== '\n') {
2108 } else if (! isascii ((unsigned char) *cp
)) {
2112 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2118 } /* else should never happen */
2125 * Strip carriage returns from content.
2128 strip_crs (CT ct
, int *message_mods
) {
2129 char *charset
= content_charset (ct
);
2132 /* Only strip carriage returns if content is ASCII or another
2133 charset that has the same readily recognizable CR followed by a
2134 LF. We can include UTF-8 here because if the high-order bit of
2135 a UTF-8 byte is 0, then it must be a single-byte ASCII
2137 if (! strcasecmp (charset
, "US-ASCII") ||
2138 ! strcasecmp (charset
, "UTF-8") ||
2139 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2140 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2146 int opened_input_file
= 0;
2148 if (ct
->c_cefile
.ce_file
) {
2149 file
= &ct
->c_cefile
.ce_file
;
2150 fp
= &ct
->c_cefile
.ce_fp
;
2152 } else if (ct
->c_file
) {
2155 begin
= (size_t) ct
->c_begin
;
2156 end
= (size_t) ct
->c_end
;
2157 } /* else don't know where the content is */
2159 if (file
&& *file
&& fp
) {
2161 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2162 advise (*file
, "unable to open for reading");
2165 opened_input_file
= 1;
2171 char buffer
[BUFSIZ
];
2173 size_t bytes_to_read
=
2174 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2176 fseeko (*fp
, begin
, SEEK_SET
);
2177 while ((bytes_read
= fread (buffer
, 1,
2178 min (bytes_to_read
, sizeof buffer
),
2180 /* Look for CR followed by a LF. This is supposed to
2181 be text so there should be LF's. If not, don't
2182 modify the content. */
2185 int last_char_was_cr
= 0;
2187 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2189 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2190 if (*cp
== '\n' && last_char_was_cr
) {
2195 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2201 char *stripped_content_file
;
2202 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2204 if (tempfile
== NULL
) {
2205 adios (NULL
, "unable to create temporary file in %s",
2208 stripped_content_file
= add (tempfile
, NULL
);
2210 /* Strip each CR before a LF from the content. */
2211 fseeko (*fp
, begin
, SEEK_SET
);
2212 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2216 int last_char_was_cr
= 0;
2218 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2220 last_char_was_cr
= 1;
2221 } else if (last_char_was_cr
) {
2223 if (write (fd
, "\r", 1) < 0) {
2224 advise (tempfile
, "CR write");
2227 if (write (fd
, cp
, 1) < 0) {
2228 advise (tempfile
, "write");
2230 last_char_was_cr
= 0;
2232 if (write (fd
, cp
, 1) < 0) {
2233 advise (tempfile
, "write");
2235 last_char_was_cr
= 0;
2241 admonish (NULL
, "unable to write temporary file %s",
2242 stripped_content_file
);
2243 (void) m_unlink (stripped_content_file
);
2246 /* Replace the decoded file with the converted one. */
2247 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2248 (void) m_unlink (ct
->c_cefile
.ce_file
);
2250 mh_xfree(ct
->c_cefile
.ce_file
);
2251 ct
->c_cefile
.ce_file
= stripped_content_file
;
2252 ct
->c_cefile
.ce_unlink
= 1;
2256 report (NULL
, ct
->c_partno
,
2257 begin
== 0 && end
== 0 ? "" : *file
,
2263 if (opened_input_file
) {
2277 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2278 * of the part C-T-E's.
2281 update_cte (CT ct
) {
2282 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2284 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2285 least_restrictive_enc
!= CE_7BIT
) {
2286 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2290 /* Update/add Content-Transfer-Encoding header field. */
2291 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2292 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2299 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
2306 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2310 least_restrictive_encoding (CT ct
) {
2311 int encoding
= CE_UNKNOWN
;
2313 switch (ct
->c_type
) {
2314 case CT_MULTIPART
: {
2315 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2318 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2319 const int part_encoding
=
2320 least_restrictive_encoding (part
->mp_part
);
2322 if (less_restrictive (encoding
, part_encoding
)) {
2323 encoding
= part_encoding
;
2330 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2331 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2332 const int part_encoding
=
2333 least_restrictive_encoding (e
->eb_content
);
2335 if (less_restrictive (encoding
, part_encoding
)) {
2336 encoding
= part_encoding
;
2342 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2343 encoding
= ct
->c_encoding
;
2352 * Return whether the second encoding is less restrictive than the first, where
2353 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2354 * CE_BINARY is less restrictive than CE_8BIT and
2355 * CE_8BIT is less restrictive than CE_7BIT.
2358 less_restrictive (int encoding
, int second_encoding
) {
2359 switch (second_encoding
) {
2361 return encoding
!= CE_BINARY
;
2363 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2365 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2366 encoding
!= CE_7BIT
;
2374 * Convert character set of each part.
2377 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2380 switch (ct
->c_type
) {
2382 if (ct
->c_subtype
== TEXT_PLAIN
) {
2383 status
= convert_charset (ct
, dest_charset
, message_mods
);
2386 char *ct_charset
= content_charset (ct
);
2388 report (NULL
, ct
->c_partno
, ct
->c_file
,
2389 "convert %s to %s", ct_charset
, dest_charset
);
2393 char *ct_charset
= content_charset (ct
);
2395 report ("iconv", ct
->c_partno
, ct
->c_file
,
2396 "failed to convert %s to %s", ct_charset
, dest_charset
);
2402 case CT_MULTIPART
: {
2403 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2406 /* Should check to see if the body for this part is encoded?
2407 For now, it gets passed along as-is by InitMultiPart(). */
2408 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2410 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2416 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2417 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2420 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2433 * Fix various problems that aren't handled elsewhere. These
2434 * are fixed unconditionally: there are no switches to disable
2435 * them. Currently, "problems" are these:
2436 * 1) remove extraneous semicolon at the end of a header parameter list
2437 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2438 * filename parameters in Content-Type and Content-Disposition
2439 * headers, respectively.
2442 fix_always (CT ct
, int *message_mods
) {
2445 switch (ct
->c_type
) {
2446 case CT_MULTIPART
: {
2447 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2450 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2451 status
= fix_always (part
->mp_part
, message_mods
);
2457 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2458 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2460 status
= fix_always (e
->eb_content
, message_mods
);
2467 if (ct
->c_first_hf
) {
2468 fix_filename_encoding (ct
);
2471 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2472 size_t len
= strlen (hf
->value
);
2474 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2475 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2476 /* Only do this for Content-Type and
2477 Content-Disposition fields because those are the
2478 only headers that parse_mime() warns about. */
2482 /* whitespace following a trailing ';' will be nuked as well */
2483 if (hf
->value
[len
- 1] == '\n') {
2484 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2485 if (len
-- == 0) { break; }
2489 if (hf
->value
[len
- 2] == ';') {
2490 /* Remove trailing ';' from parameter value. */
2491 hf
->value
[len
- 2] = '\n';
2492 hf
->value
[len
- 1] = '\0';
2494 /* Also, if Content-Type parameter, remove trailing ';'
2495 from ct->c_ctline. This probably isn't necessary
2497 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2498 size_t l
= strlen(ct
->c_ctline
) - 1;
2499 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2500 ct
->c_ctline
[l
] == ';') {
2501 ct
->c_ctline
[l
--] = '\0';
2502 if (l
== 0) { break; }
2508 report (NULL
, ct
->c_partno
, ct
->c_file
,
2509 "remove trailing ; from %s parameter value",
2521 * Factor out common code for loops in fix_filename_encoding().
2524 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
) {
2528 if (((value_len
= strlen (value
)) > 0) &&
2529 strncmp (value
, "=?", 2) == 0 &&
2530 strncmp (&value
[value_len
- 2], "?=", 2) == 0) {
2531 /* Looks like an RFC 2047 encoded parameter. */
2532 char decoded
[PATH_MAX
+ 1];
2534 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2535 /* Encode using RFC 2231. */
2536 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2539 advise (NULL
, "failed to decode %s parameter %s", name
, value
);
2548 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2549 * filename parameters in Content-Type and Content-Disposition
2550 * headers, respectively.
2553 fix_filename_encoding (CT ct
) {
2558 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2559 if (pm
->pm_name
&& pm
->pm_value
&&
2560 strcasecmp (pm
->pm_name
, "name") == 0) {
2561 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2562 &ct
->c_ctinfo
.ci_first_pm
,
2563 &ct
->c_ctinfo
.ci_last_pm
);
2567 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2568 if (pm
->pm_name
&& pm
->pm_value
&&
2569 strcasecmp (pm
->pm_name
, "filename") == 0) {
2570 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2576 /* Fix hf values to correspond. */
2577 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2578 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2580 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2581 field
= TYPE_HEADER
;
2582 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2583 field
= DISPO_HEADER
;
2586 if (field
!= OTHER
) {
2587 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2589 if (semicolon_loc
) {
2591 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2592 const char *const params
=
2594 field
== TYPE_HEADER
2595 ? ct
->c_ctinfo
.ci_first_pm
2596 : ct
->c_dispo_first
,
2598 const char *const new_params
= concat (params
, "\n", NULL
);
2600 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2601 free((void *)new_params
); /* Cast away const. Sigh. */
2602 free((void *)params
);
2604 advise (NULL
, "did not find semicolon in %s:%s\n",
2605 hf
->name
, hf
->value
);
2615 * Output content in input file to output file.
2618 write_content (CT ct
, const char *input_filename
, char *outfile
, int modify_inplace
,
2622 if (modify_inplace
) {
2623 if (message_mods
> 0) {
2624 if ((status
= output_message (ct
, outfile
)) == OK
) {
2625 char *infile
= input_filename
2626 ? add (input_filename
, NULL
)
2627 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
2629 if (remove_file (infile
) == OK
) {
2630 if (rename (outfile
, infile
)) {
2631 /* Rename didn't work, possibly because of an
2632 attempt to rename across filesystems. Try
2633 brute force copy. */
2634 int old
= open (outfile
, O_RDONLY
);
2636 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2639 if (old
!= -1 && new != -1) {
2640 char buffer
[BUFSIZ
];
2642 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2644 if (write (new, buffer
, i
) != i
) {
2650 if (new != -1) { close (new); }
2651 if (old
!= -1) { close (old
); }
2652 (void) m_unlink (outfile
);
2655 /* The -file argument processing used path() to
2656 expand filename to absolute path. */
2657 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2659 admonish (NULL
, "unable to rename %s %s to %s",
2660 file
? "file" : "message", outfile
,
2666 admonish (NULL
, "unable to remove input file %s, "
2667 "not modifying it", infile
);
2668 (void) m_unlink (outfile
);
2677 /* No modifications and didn't need the tmp outfile. */
2678 (void) m_unlink (outfile
);
2681 /* Output is going to some file. Produce it whether or not
2682 there were modifications. */
2683 status
= output_message (ct
, outfile
);
2692 * parse_mime() does not set lf_line_endings in struct text, so use this
2693 * function to do it. It touches the parts the decodetypes identifies.
2696 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
) {
2697 switch (ct
->c_type
) {
2698 case CT_MULTIPART
: {
2699 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2702 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2703 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2709 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2710 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2712 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2717 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2718 if (ct
->c_ctparams
== NULL
) {
2719 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2721 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2728 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2729 * use the standard MH backup file.
2732 remove_file (const char *file
) {
2734 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2735 int status
= system (rmm_command
);
2738 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2740 /* This is OK for a non-message file, it still uses the
2741 BACKUP_PREFIX form. The backup file will be in the same
2742 directory as file. */
2743 return rename (file
, m_backup (file
));
2748 * Output formatted message to user.
2751 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2756 va_start (args
, message
);
2757 fmt
= concat (filename
, partno
? " part " : ", ",
2758 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
2760 advertise (what
, NULL
, fmt
, args
);
2773 fprintf (stderr
, "\n");