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
;
66 int output_message (CT
, char *);
69 void flush_errors (void);
77 typedef struct fix_transformations
{
85 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
88 } fix_transformations
;
90 int mhfixmsgsbr (CT
*, char *, const fix_transformations
*, char *);
91 static int fix_boundary (CT
*, int *);
92 static int copy_input_to_output (const char *, const char *);
93 static int get_multipart_boundary (CT
, char **);
94 static int replace_boundary (CT
, char *, char *);
95 static int fix_types (CT
, svector_t
, int *);
96 static char *replace_substring (char **, const char *, const char *);
97 static char *remove_parameter (char *, const char *);
98 static int fix_composite_cte (CT
, int *);
99 static int set_ce (CT
, int);
100 static int ensure_text_plain (CT
*, CT
, int *, int);
101 static int find_textplain_sibling (CT
, int, int *);
102 static int insert_new_text_plain_part (CT
, int, CT
);
103 static CT
build_text_plain_part (CT
);
104 static int insert_into_new_mp_alt (CT
*, int *);
105 static CT
divide_part (CT
);
106 static void copy_ctinfo (CI
, CI
);
107 static int decode_part (CT
);
108 static int reformat_part (CT
, char *, char *, char *, int);
109 static CT
build_multipart_alt (CT
, CT
, int, int);
110 static int boundary_in_content (FILE **, char *, const char *);
111 static void transfer_noncontent_headers (CT
, CT
);
112 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
113 static int decode_text_parts (CT
, int, const char *, int *);
114 static int should_decode(const char *, const char *, const char *);
115 static int content_encoding (CT
, const char **);
116 static int strip_crs (CT
, int *);
117 static void update_cte (CT
);
118 static int least_restrictive_encoding (CT
);
119 static int less_restrictive (int, int);
120 static int convert_charsets (CT
, char *, int *);
121 static int fix_always (CT
, int *);
122 static int fix_filename_param (char *, char *, PM
*, PM
*);
123 static int fix_filename_encoding (CT
);
124 static int write_content (CT
, const char *, char *, int, int);
125 static void set_text_ctparams(CT
, char *, int);
126 static int remove_file (const char *);
127 static void report (char *, char *, char *, char *, ...);
128 static void pipeser (int);
132 main (int argc
, char **argv
) {
134 char *cp
, *file
= NULL
, *folder
= NULL
;
135 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
136 char **argp
, **arguments
;
137 struct msgs_array msgs
= { 0, 0, NULL
};
138 struct msgs
*mp
= NULL
;
144 fix_transformations fx
;
145 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
147 fx
.replacetextplain
= 0;
148 fx
.decodetext
= CE_8BIT
;
149 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
150 fx
.lf_line_endings
= 0;
151 fx
.textcharset
= NULL
;
153 if (nmh_init(argv
[0], 2)) { 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]);
183 if (! strcasecmp (cp
, "8bit")) {
184 fx
.decodetext
= CE_8BIT
;
185 } else if (! strcasecmp (cp
, "7bit")) {
186 fx
.decodetext
= CE_7BIT
;
187 } else if (! strcasecmp (cp
, "binary")) {
188 fx
.decodetext
= CE_BINARY
;
190 adios (NULL
, "invalid argument to %s", argp
[-2]);
197 if (! (cp
= *argp
++) || *cp
== '-') {
198 adios (NULL
, "missing argument to %s", argp
[-2]);
202 case CRLFLINEBREAKSSW
:
203 fx
.lf_line_endings
= 0;
205 case NCRLFLINEBREAKSSW
:
206 fx
.lf_line_endings
= 1;
209 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
210 adios (NULL
, "missing argument to %s", argp
[-2]);
223 case FIXCOMPOSITECTESW
:
224 fx
.fixcompositecte
= 1;
226 case NFIXCOMPOSITECTESW
:
227 fx
.fixcompositecte
= 0;
230 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
231 adios (NULL
, "missing argument to %s", argp
[-2]);
233 if (! strncasecmp (cp
, "multipart/", 10) ||
234 ! strncasecmp (cp
, "message/", 8)) {
235 adios (NULL
, "-fixtype %s not allowed", cp
);
236 } else if (! strchr (cp
, '/')) {
237 adios (NULL
, "-fixtype requires type/subtype");
239 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
240 svector_push_back (fx
.fixtypes
, cp
);
248 case REPLACETEXTPLAINSW
:
249 fx
.replacetextplain
= 1;
251 case NREPLACETEXTPLAINSW
:
252 fx
.replacetextplain
= 0;
255 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
256 adios (NULL
, "missing argument to %s", argp
[-2]);
258 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
261 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
262 adios (NULL
, "missing argument to %s", argp
[-2]);
264 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
267 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
268 adios (NULL
, "missing argument to %s", argp
[-2]);
288 if (*cp
== '+' || *cp
== '@') {
290 adios (NULL
, "only one folder at a time!");
292 folder
= pluspath (cp
);
296 /* Interpret a full path as a filename, not a message. */
297 file
= mh_xstrdup (cp
);
299 app_msgarg (&msgs
, cp
);
304 SIGNAL (SIGQUIT
, quitser
);
305 SIGNAL (SIGPIPE
, pipeser
);
308 * Read the standard profile setup
310 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
311 readconfig ((struct node
**) 0, fp
, cp
, 0);
315 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
316 suppress_extraneous_trailing_semicolon_warning
= 1;
318 if (! context_find ("path")) {
319 free (path ("./", TFOLDER
));
322 if (file
&& msgs
.size
) {
323 adios (NULL
, "cannot specify msg and file at same time!");
327 * check if message is coming from file
330 /* If file is stdin, create a tmp file name before parse_mime()
331 has a chance, because it might put in on a different
332 filesystem than the output file. Instead, put it in the
333 user's preferred tmp directory. */
336 if (! strcmp ("-", file
)) {
342 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
343 adios (NULL
, "unable to create temporary file in %s",
347 file
= mh_xstrdup (cp
);
348 cpydata (STDIN_FILENO
, fd
, "-", file
);
352 (void) m_unlink (file
);
353 adios (NULL
, "failed to write temporary file");
357 cts
= mh_xcalloc(2, sizeof *cts
);
360 if ((ct
= parse_mime (file
))) {
361 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
364 advise (NULL
, "unable to parse message from file %s", file
);
367 /* If there's an outfile, pass the input message unchanged, so the message won't
368 get dropped from a pipeline. */
370 /* Something went wrong. Output might be expected, such as if this were run
371 as a filter. Just copy the input to the output. */
372 if (copy_input_to_output (file
, outfile
) != OK
) {
373 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
379 * message(s) are coming from a folder
384 app_msgarg(&msgs
, "cur");
387 folder
= getfolder (1);
389 maildir
= m_maildir (folder
);
391 /* chdir so that error messages, esp. from MIME parser, just
392 refer to the message and not its path. */
393 if (chdir (maildir
) == NOTOK
) {
394 adios (maildir
, "unable to change directory to");
397 /* read folder and create message structure */
398 if (! (mp
= folder_read (folder
, 1))) {
399 adios (NULL
, "unable to read folder %s", folder
);
402 /* check for empty folder */
403 if (mp
->nummsg
== 0) {
404 adios (NULL
, "no messages in %s", folder
);
407 /* parse all the message ranges/sequences and set SELECTED */
408 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
409 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
412 seq_setprev (mp
); /* set the previous-sequence */
414 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
417 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
418 if (is_selected(mp
, msgnum
)) {
419 char *msgnam
= m_name (msgnum
);
421 if ((ct
= parse_mime (msgnam
))) {
422 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
425 advise (NULL
, "unable to parse message %s", msgnam
);
428 /* If there's an outfile, pass the input message
429 unchanged, so the message won't get dropped from a
432 /* Something went wrong. Output might be expected,
433 such as if this were run as a filter. Just copy
434 the input to the output. */
435 /* Can't use path() here because 1) it might have been
436 called before and it caches the pwd, and 2) we call
437 chdir() after that. */
438 char *input_filename
=
439 concat (maildir
, "/", msgnam
, NULL
);
441 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
443 "unable to copy message to %s, it might be lost\n",
446 free (input_filename
);
453 seq_setcur (mp
, mp
->hghsel
); /* update current message */
455 seq_save (mp
); /* synchronize sequences */
456 context_replace (pfolder
, folder
);/* update current folder */
457 context_save (); /* save the context file */
461 for (ctp
= cts
; *ctp
; ++ctp
) {
462 status
+= mhfixmsgsbr (ctp
, maildir
, &fx
, outfile
);
466 (void) m_unlink (file
);
469 /* Just calling m_backup() unlinks the backup file. */
470 (void) m_backup (file
);
480 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
492 * Apply transformations to one message.
495 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
497 /* Store input filename in case one of the transformations, i.e.,
498 fix_boundary(), rewrites to a tmp file. */
499 char *input_filename
= maildir
500 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
501 : mh_xstrdup ((*ctp
)->c_file
);
502 int modify_inplace
= 0;
503 int message_mods
= 0;
506 if (outfile
== NULL
) {
509 if ((*ctp
)->c_file
) {
511 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
512 adios (NULL
, "unable to create temporary file in %s",
515 outfile
= mh_xstrdup (tempfile
);
517 adios (NULL
, "missing both input and output filenames\n");
521 reverse_alternative_parts (*ctp
);
522 status
= fix_always (*ctp
, &message_mods
);
523 if (status
== OK
&& fx
->fixboundary
) {
524 status
= fix_boundary (ctp
, &message_mods
);
526 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
527 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
529 if (status
== OK
&& fx
->fixcompositecte
) {
530 status
= fix_composite_cte (*ctp
, &message_mods
);
532 if (status
== OK
&& fx
->reformat
) {
534 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
536 if (status
== OK
&& fx
->decodetext
) {
537 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
541 if (status
== OK
&& fx
->textcharset
!= NULL
) {
542 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
545 if (status
== OK
&& ! (*ctp
)->c_umask
) {
546 /* Set the umask for the contents file. This currently
547 isn't used but just in case it is in the future. */
550 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
551 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
553 (*ctp
)->c_umask
= ~m_gmprot();
558 * Write the content to a file
561 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
563 } else if (! modify_inplace
) {
564 /* Something went wrong. Output might be expected, such
565 as if this were run as a filter. Just copy the input
567 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
568 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
572 if (modify_inplace
) {
573 if (status
!= OK
) { (void) m_unlink (outfile
); }
578 free (input_filename
);
585 * Copy input message to output. Assumes not modifying in place, so this
586 * might be running as part of a pipeline.
589 copy_input_to_output (const char *input_filename
, const char *output_filename
) {
590 int in
= open (input_filename
, O_RDONLY
);
591 int out
= strcmp (output_filename
, "-")
592 ? open (output_filename
, O_WRONLY
| O_CREAT
, m_gmprot ())
596 if (in
!= -1 && out
!= -1) {
597 cpydata (in
, out
, input_filename
, output_filename
);
610 * Fix mismatched outer level boundary.
613 fix_boundary (CT
*ct
, int *message_mods
) {
614 struct multipart
*mp
;
617 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
618 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
621 * 1) Get boundary at end of part.
622 * 2) Get boundary at beginning of part and compare to the end-of-part
624 * 3) Write out contents of ct to tmp file, replacing boundary in
625 * header with boundary from part. Set c_unlink to 1.
627 * 5) Call parse_mime() on the tmp file, replacing ct.
630 if (mp
&& mp
->mp_start
) {
633 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
636 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
637 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
638 char *filename
= mh_xstrdup ((*ct
)->c_file
);
642 if ((fixed_ct
= parse_mime (fixed
))) {
648 report (NULL
, NULL
, filename
,
649 "fix multipart boundary");
653 advise (NULL
, "unable to parse fixed part");
658 advise (NULL
, "unable to replace broken boundary");
662 advise (NULL
, "unable to create temporary file in %s",
667 free (part_boundary
);
669 /* Couldn't fix the boundary. Report failure so that mhfixmsg
670 doesn't modify the message. */
674 /* No multipart struct, even though the content type is
675 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
686 * Find boundary at end of multipart.
689 get_multipart_boundary (CT ct
, char **part_boundary
) {
690 char buffer
[NMH_BUFSIZ
];
691 char *end_boundary
= NULL
;
692 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
693 ? (off_t
) (ct
->c_end
- sizeof buffer
)
694 : (off_t
) ct
->c_begin
;
698 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
699 be big enough, even if it's just 1024, to make that unlikely. */
701 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
702 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
703 advise (ct
->c_file
, "unable to open for reading");
707 /* Get boundary at end of multipart. */
708 while (begin
>= (off_t
) ct
->c_begin
) {
709 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
710 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
711 char *cp
= rfind_str (buffer
, bytes_read
, "--");
716 /* Trim off trailing "--" and anything beyond. */
718 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
719 if (strlen (end
) > 3 && *end
++ == '\n' &&
720 *end
++ == '-' && *end
++ == '-') {
721 end_boundary
= mh_xstrdup (end
);
728 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
729 begin
-= sizeof buffer
;
735 /* Get boundary at beginning of multipart. */
737 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
738 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
739 if (bytes_read
>= strlen (end_boundary
)) {
740 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
742 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
743 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
748 /* The start and end boundaries didn't match, or the
749 start boundary doesn't begin with "\n--" (or "--"
750 if at the beginning of buffer). Keep trying. */
764 *part_boundary
= end_boundary
;
766 *part_boundary
= NULL
;
775 * Open and copy ct->c_file to file, replacing the multipart boundary.
778 replace_boundary (CT ct
, char *file
, char *boundary
) {
781 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
783 m_getfld_state_t gstate
= 0;
786 if (ct
->c_file
== NULL
) {
787 advise (NULL
, "missing input filename");
791 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
792 advise (ct
->c_file
, "unable to open for reading");
796 if ((fpout
= fopen (file
, "w")) == NULL
) {
798 advise (file
, "unable to open for writing");
802 for (compnum
= 1;;) {
803 int bufsz
= (int) sizeof buf
;
805 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
810 /* get copies of the buffers */
811 np
= mh_xstrdup (name
);
812 vp
= mh_xstrdup (buf
);
814 /* if necessary, get rest of field */
815 while (state
== FLDPLUS
) {
817 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
818 vp
= add (buf
, vp
); /* add to previous value */
821 if (strcasecmp (TYPE_FIELD
, np
)) {
822 fprintf (fpout
, "%s:%s", np
, vp
);
824 char *new_ctline
, *new_params
;
826 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
827 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
830 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
831 ct
->c_ctinfo
.ci_subtype
, NULL
);
832 new_params
= output_params(strlen(TYPE_FIELD
) +
833 strlen(new_ctline
) + 1,
834 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
835 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
836 new_params
? new_params
: "");
838 mh_xfree(new_params
);
848 /* buf will have a terminating NULL, skip it. */
849 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
850 advise (file
, "fwrite");
859 advise (NULL
, "message format error in component #%d", compnum
);
864 advise (NULL
, "getfld() returned %d", state
);
872 m_getfld_state_destroy (&gstate
);
881 * Fix Content-Type header to reflect the content of its part.
884 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
887 switch (ct
->c_type
) {
889 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
892 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
893 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
899 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
900 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
902 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
909 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
910 for (typep
= svector_strs (fixtypes
);
911 typep
&& (type
= *typep
);
914 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
917 if (! strcasecmp (type
, type_subtype
) &&
918 decode_part (ct
) == OK
&&
919 ct
->c_cefile
.ce_file
!= NULL
) {
920 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
923 if ((cp
= strchr (ct_type_subtype
, ';'))) {
924 /* Truncate to remove any parameter list from
925 mime_type () result. */
929 if (strcasecmp (type
, ct_type_subtype
)) {
930 char *ct_type
, *ct_subtype
;
933 /* The Content-Type header does not match the
934 content, so update these struct Content
937 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
940 /* Extract type and subtype from type/subtype. */
941 ct_type
= mh_xstrdup(ct_type_subtype
);
942 if ((cp
= strchr (ct_type
, '/'))) {
944 ct_subtype
= mh_xstrdup(++cp
);
946 advise (NULL
, "missing / in MIME type of %s %s",
947 ct
->c_file
, ct
->c_partno
);
952 ct
->c_type
= ct_str_type (ct_type
);
953 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
955 free (ct
->c_ctinfo
.ci_type
);
956 ct
->c_ctinfo
.ci_type
= ct_type
;
957 free (ct
->c_ctinfo
.ci_subtype
);
958 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
959 if (! replace_substring (&ct
->c_ctline
, type
,
961 advise (NULL
, "did not find %s in %s",
965 /* Update Content-Type header field. */
966 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
967 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
968 if (replace_substring (&hf
->value
, type
,
972 report (NULL
, ct
->c_partno
, ct
->c_file
,
973 "change Content-Type in header "
975 type
, ct_type_subtype
);
979 advise (NULL
, "did not find %s in %s",
985 free (ct_type_subtype
);
997 * Replace a substring, allocating space to hold the new one.
1000 replace_substring (char **str
, const char *old
, const char *new) {
1003 if ((cp
= strstr (*str
, old
))) {
1004 char *remainder
= cp
+ strlen (old
);
1005 char *prefix
, *new_str
;
1008 prefix
= mh_xstrdup(*str
);
1009 *(prefix
+ (cp
- *str
)) = '\0';
1010 new_str
= concat (prefix
, new, remainder
, NULL
);
1013 new_str
= concat (new, remainder
, NULL
);
1018 return *str
= new_str
;
1026 * Remove a name=value parameter, given just its name, from a header value.
1029 remove_parameter (char *str
, const char *name
) {
1030 /* It looks to me, based on the BNF in RFC 2045, than there can't
1031 be whitespace betwwen the parameter name and the "=", or
1032 between the "=" and the parameter value. */
1033 char *param_name
= concat (name
, "=", NULL
);
1036 if ((cp
= strstr (str
, param_name
))) {
1040 /* Remove any leading spaces, before the parameter name. */
1042 start
> str
&& isspace ((unsigned char) *(start
-1));
1046 /* Remove a leading semicolon. */
1047 if (start
> str
&& *(start
-1) == ';') { --start
; }
1049 end
= cp
+ strlen (name
) + 1;
1051 /* Skip past the quoted value, and then the final quote. */
1052 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1055 /* Skip past the value. */
1056 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1059 /* Count how many characters need to be moved. Include
1060 trailing null, which is accounted for by the
1061 initialization of count to 1. */
1062 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1063 (void) memmove (start
, end
, count
);
1073 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1074 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1078 fix_composite_cte (CT ct
, int *message_mods
) {
1081 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1082 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1083 ct
->c_encoding
!= CE_BINARY
) {
1086 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1087 char *name
= hf
->name
;
1088 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
1092 if (! strncasecmp (name
, ENCODING_FIELD
,
1093 strlen (ENCODING_FIELD
))) {
1094 char *prefix
= "Nmh-REPLACED-INVALID-";
1098 h
->name
= mh_xstrdup (hf
->name
);
1099 h
->hf_encoding
= hf
->hf_encoding
;
1103 /* Retain old header but prefix its name. */
1105 hf
->name
= concat (prefix
, h
->name
, NULL
);
1109 char *encoding
= cpytrim (hf
->value
);
1110 report (NULL
, ct
->c_partno
, ct
->c_file
,
1111 "replace Content-Transfer-Encoding of %s "
1112 "with 8 bit", encoding
);
1116 h
->value
= mh_xstrdup (" 8bit\n");
1118 /* Don't need to warn for multiple C-T-E header
1119 fields, parse_mime() already does that. But
1120 if there are any, fix them all as necessary. */
1125 set_ce (ct
, CE_8BIT
);
1128 if (ct
->c_type
== CT_MULTIPART
) {
1129 struct multipart
*m
;
1132 m
= (struct multipart
*) ct
->c_ctparams
;
1133 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1134 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1147 * Set content encoding.
1150 set_ce (CT ct
, int encoding
) {
1151 const char *ce
= ce_str (encoding
);
1152 const struct str2init
*ctinit
= get_ce_method (ce
);
1155 char *cte
= concat (" ", ce
, "\n", NULL
);
1158 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1159 caller is decode_text_parts (). Save because we'll
1161 struct cefile decoded_content_info
= ct
->c_cefile
;
1163 ct
->c_encoding
= encoding
;
1165 ct
->c_ctinitfnx
= ctinit
->si_init
;
1166 /* This will assign ct->c_cefile with an all-0 struct, which
1168 (*ctinit
->si_init
) (ct
);
1169 /* After returning, the caller should set
1170 ct->c_cefile.ce_file to the name of the file containing
1173 if (ct
->c_ceclosefnx
) {
1174 (*ct
->c_ceclosefnx
) (ct
);
1177 /* Restore the cefile. */
1178 ct
->c_cefile
= decoded_content_info
;
1180 /* Update/add Content-Transfer-Encoding header field. */
1181 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1182 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1189 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1192 /* Update c_celine. It's used only by mhlist -debug. */
1193 free (ct
->c_celine
);
1194 ct
->c_celine
= mh_xstrdup (cte
);
1204 * Make sure each text part has a corresponding text/plain part.
1207 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1210 switch ((*ct
)->c_type
) {
1212 /* Nothing to do for text/plain. */
1213 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1215 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1216 parent
->c_subtype
== MULTI_ALTERNATE
) {
1217 int new_subpart_number
= 1;
1218 int has_text_plain
=
1219 find_textplain_sibling (parent
, replacetextplain
,
1220 &new_subpart_number
);
1222 if (! has_text_plain
) {
1223 /* Parent is a multipart/alternative. Insert a new
1224 text/plain subpart. */
1225 const int inserted
=
1226 insert_new_text_plain_part (*ct
, new_subpart_number
,
1231 report (NULL
, parent
->c_partno
, parent
->c_file
,
1232 "insert text/plain part");
1238 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1239 parent
->c_subtype
== MULTI_RELATED
) {
1240 char *type_subtype
=
1241 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1242 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1243 const char *parent_type
=
1244 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1245 int new_subpart_number
= 1;
1246 int has_text_plain
= 0;
1248 /* Have to do string comparison on the subtype because we
1249 don't enumerate all of them in c_subtype values.
1250 parent_type will be NULL if the multipart/related part
1251 doesn't have a type parameter. The type parameter must
1252 be specified according to RFC 2387 Sec. 3.1 but not all
1254 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1255 /* The type of this part matches the root type of the
1256 parent multipart/related. Look to see if there's
1257 text/plain sibling. */
1259 find_textplain_sibling (parent
, replacetextplain
,
1260 &new_subpart_number
);
1263 free (type_subtype
);
1265 if (! has_text_plain
) {
1266 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1270 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1271 if (*ct
!= part
->mp_part
) {
1277 /* Parent is a multipart/related. Insert a new
1278 text/plain subpart in a new multipart/alternative. */
1279 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1280 /* Not an error if text/plain couldn't be added. */
1283 /* There are no siblings, so insert a new text/plain
1284 subpart, and change the parent type from
1285 multipart/related to multipart/alternative. */
1286 const int inserted
=
1287 insert_new_text_plain_part (*ct
, new_subpart_number
,
1293 parent
->c_subtype
= MULTI_ALTERNATE
;
1294 free (parent
->c_ctinfo
.ci_subtype
);
1295 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1296 if (! replace_substring (&parent
->c_ctline
, "/related",
1299 "did not find multipart/related in %s",
1303 /* Update Content-Type header field. */
1304 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1305 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1306 if (replace_substring (&hf
->value
, "/related",
1310 report (NULL
, parent
->c_partno
,
1312 "insert text/plain part");
1315 /* Remove, e.g., type="text/html" from
1316 multipart/alternative. */
1317 remove_parameter (hf
->value
, "type");
1320 advise (NULL
, "did not find multipart/"
1321 "related in header %s",
1327 /* Not an error if text/plain couldn't be inserted. */
1332 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1339 case CT_MULTIPART
: {
1340 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1343 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1344 if ((*ct
)->c_type
== CT_MULTIPART
) {
1345 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1353 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1354 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1356 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1367 * See if there is a sibling text/plain, and return its subpart number.
1370 find_textplain_sibling (CT parent
, int replacetextplain
,
1371 int *new_subpart_number
) {
1372 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1373 struct part
*part
, *prev
;
1374 int has_text_plain
= 0;
1376 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1377 ++*new_subpart_number
;
1378 if (part
->mp_part
->c_type
== CT_TEXT
&&
1379 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1380 if (replacetextplain
) {
1381 struct part
*old_part
;
1382 if (part
== mp
->mp_parts
) {
1383 old_part
= mp
->mp_parts
;
1384 mp
->mp_parts
= part
->mp_next
;
1386 old_part
= prev
->mp_next
;
1387 prev
->mp_next
= part
->mp_next
;
1390 report (NULL
, parent
->c_partno
, parent
->c_file
,
1391 "remove text/plain part %s",
1392 old_part
->mp_part
->c_partno
);
1394 free_content (old_part
->mp_part
);
1404 return has_text_plain
;
1409 * Insert a new text/plain part.
1412 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1413 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1414 struct part
*new_part
;
1417 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1419 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1421 new_part
->mp_next
= mp
->mp_parts
;
1422 mp
->mp_parts
= new_part
;
1423 new_part
->mp_part
->c_partno
=
1424 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1430 free_content (new_part
->mp_part
);
1438 * Create a text/plain part to go along with non-plain sibling part.
1441 build_text_plain_part (CT encoded_part
) {
1442 CT tp_part
= divide_part (encoded_part
);
1443 char *tmp_plain_file
= NULL
;
1445 if (decode_part (tp_part
) == OK
) {
1446 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1447 contains the decoded contents. And the decoding function, such
1448 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1449 be unlinked by free_content (). */
1452 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1453 advise (NULL
, "unable to create temporary file in %s",
1456 tmp_plain_file
= mh_xstrdup (tempfile
);
1457 if (reformat_part (tp_part
, tmp_plain_file
,
1458 tp_part
->c_ctinfo
.ci_type
,
1459 tp_part
->c_ctinfo
.ci_subtype
,
1460 tp_part
->c_type
) == OK
) {
1466 free_content (tp_part
);
1467 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1468 free (tmp_plain_file
);
1475 * Slip new text/plain part into a new multipart/alternative.
1478 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1479 CT tp_part
= build_text_plain_part (*ct
);
1483 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1486 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1488 if (mp
&& mp
->mp_parts
) {
1489 mp
->mp_parts
->mp_part
= tp_part
;
1490 /* Make the new multipart/alternative the parent. */
1495 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1496 "insert text/plain part");
1499 free_content (tp_part
);
1500 free_content (mp_alt
);
1507 /* Not an error if text/plain couldn't be built. */
1515 * Clone a MIME part.
1518 divide_part (CT ct
) {
1522 /* Just copy over what is needed for decoding. c_vrsn and
1523 c_celine aren't necessary. */
1524 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1525 new_part
->c_begin
= ct
->c_begin
;
1526 new_part
->c_end
= ct
->c_end
;
1527 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1528 new_part
->c_type
= ct
->c_type
;
1529 new_part
->c_cefile
= ct
->c_cefile
;
1530 new_part
->c_encoding
= ct
->c_encoding
;
1531 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1532 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1533 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1534 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1536 /* c_ctline is used by reformat__part(), so it can preserve
1537 anything after the type/subtype. */
1538 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1545 * Copy the content info from one part to another.
1548 copy_ctinfo (CI dest
, CI src
) {
1551 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1552 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1554 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1555 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1557 if (s_pm
->pm_charset
) {
1558 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1560 if (s_pm
->pm_lang
) {
1561 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1565 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1566 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1574 decode_part (CT ct
) {
1579 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1580 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1582 tmp_decoded
= mh_xstrdup (tempfile
);
1583 /* The following call will load ct->c_cefile.ce_file with the tmp
1584 filename of the decoded content. tmp_decoded will contain the
1585 encoded output, get rid of that. */
1586 status
= output_message (ct
, tmp_decoded
);
1587 (void) m_unlink (tmp_decoded
);
1595 * Reformat content as plain text.
1596 * Some of the arguments aren't really needed now, but maybe will
1597 * be in the future for other than text types.
1600 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1601 int output_subtype
, output_encoding
;
1602 const char *reason
= NULL
;
1606 /* Hacky: this redirects the output from whatever command is used
1607 to show the part to a file. So, the user can't have any output
1608 redirection in that command.
1609 Could show_multi() in mhshowsbr.c avoid this? */
1611 /* Check for invo_name-format-type/subtype. */
1612 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1614 advise (NULL
, "Don't know how to convert %s, there is no "
1615 "%s-format-%s/%s profile entry",
1616 ct
->c_file
, invo_name
, type
, subtype
);
1620 if (strchr (cf
, '>')) {
1621 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1622 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1623 subtype
? subtype
: "");
1628 cp
= concat (cf
, " >", file
, NULL
);
1629 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1632 /* Unlink decoded content tmp file and free its filename to avoid
1633 leaks. The file stream should already have been closed. */
1634 if (ct
->c_cefile
.ce_unlink
) {
1635 (void) m_unlink (ct
->c_cefile
.ce_file
);
1636 free (ct
->c_cefile
.ce_file
);
1637 ct
->c_cefile
.ce_file
= NULL
;
1638 ct
->c_cefile
.ce_unlink
= 0;
1641 if (c_type
== CT_TEXT
) {
1642 output_subtype
= TEXT_PLAIN
;
1644 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1648 output_encoding
= content_encoding (ct
, &reason
);
1650 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1651 ct
->c_cefile
.ce_file
= file
;
1652 ct
->c_cefile
.ce_unlink
= 1;
1654 ct
->c_cefile
.ce_unlink
= 0;
1663 * Fill in a multipart/alternative part.
1666 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1667 char *boundary_prefix
= "----=_nmh-multipart";
1668 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1669 char *boundary_indicator
= "; boundary=";
1670 char *typename
, *subtypename
, *name
;
1673 struct multipart
*m
;
1674 const struct str2init
*ctinit
;
1678 /* Set up the multipart/alternative part. These fields of *ct were
1679 initialized to 0 by mh_xcalloc():
1680 c_fp, c_unlink, c_begin, c_end,
1681 c_vrsn, c_ctline, c_celine,
1682 c_id, c_descr, c_dispo, c_partno,
1683 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1684 c_cefile, c_encoding,
1685 c_digested, c_digest[16], c_ctexbody,
1686 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1688 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1691 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1693 ct
->c_subtype
= subtype
;
1695 ctinit
= get_ct_init (ct
->c_type
);
1697 typename
= ct_type_str (type
);
1698 subtypename
= ct_subtype_str (type
, subtype
);
1702 int found_boundary
= 1;
1704 while (found_boundary
&& serial
< 1000000) {
1707 /* Ensure that the boundary doesn't appear in the decoded
1709 if (new_part
->c_cefile
.ce_file
) {
1710 if ((found_boundary
=
1711 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1712 new_part
->c_cefile
.ce_file
,
1713 boundary
)) == NOTOK
) {
1719 /* Ensure that the boundary doesn't appear in the encoded
1721 if (! found_boundary
&& new_part
->c_file
) {
1722 if ((found_boundary
=
1723 boundary_in_content (&new_part
->c_fp
,
1725 boundary
)) == NOTOK
) {
1731 if (found_boundary
) {
1732 /* Try a slightly different boundary. */
1737 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1739 concat (boundary_prefix
,
1740 first_alt
->c_partno
? first_alt
->c_partno
: "",
1741 "-", buffer2
, NULL
);
1745 if (found_boundary
) {
1746 advise (NULL
, "giving up trying to find a unique boundary");
1752 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1753 boundary
, "\"", NULL
);
1755 /* Load c_first_hf and c_last_hf. */
1756 transfer_noncontent_headers (first_alt
, ct
);
1757 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1760 /* Load c_partno. */
1761 if (first_alt
->c_partno
) {
1762 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1763 free (first_alt
->c_partno
);
1764 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1765 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1767 first_alt
->c_partno
= mh_xstrdup ("1");
1768 new_part
->c_partno
= mh_xstrdup ("2");
1772 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1773 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1776 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1777 "boundary", boundary
, 0);
1781 p
->mp_next
->mp_next
= NULL
;
1782 p
->mp_next
->mp_part
= first_alt
;
1785 m
->mp_start
= concat (boundary
, "\n", NULL
);
1786 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1797 * Check that the boundary does not appear in the content.
1800 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1801 char buffer
[NMH_BUFSIZ
];
1803 int found_boundary
= 0;
1805 /* free_content() will close *fp if we fopen it here. */
1806 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1807 advise (file
, "unable to open %s for reading", file
);
1811 fseeko (*fp
, 0L, SEEK_SET
);
1812 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1813 if (find_str (buffer
, bytes_read
, boundary
)) {
1819 return found_boundary
;
1824 * Remove all non-Content headers.
1827 transfer_noncontent_headers (CT old
, CT
new) {
1830 hp_prev
= hp
= old
->c_first_hf
;
1834 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1835 if (hp
== old
->c_last_hf
) {
1836 if (hp
== old
->c_first_hf
) {
1837 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1839 hp_prev
->next
= NULL
;
1840 old
->c_last_hf
= hp_prev
;
1843 if (hp
== old
->c_first_hf
) {
1844 old
->c_first_hf
= next
;
1846 hp_prev
->next
= next
;
1850 /* Put node hp in the new CT. */
1851 if (new->c_first_hf
== NULL
) {
1852 new->c_first_hf
= hp
;
1854 new->c_last_hf
->next
= hp
;
1856 new->c_last_hf
= hp
;
1858 /* A Content- header, leave in old. */
1871 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1872 char *typename
= ct_type_str (type
);
1873 char *subtypename
= ct_subtype_str (type
, subtype
);
1874 /* E.g, " text/plain" */
1875 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1876 /* E.g, " text/plain\n" */
1877 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1878 int found_content_type
= 0;
1880 const char *cp
= NULL
;
1884 /* Update/add Content-Type header field. */
1885 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1886 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1887 found_content_type
= 1;
1889 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1890 ? concat (type_subtypename
, cp
, "\n", NULL
)
1891 : mh_xstrdup (name_plus_nl
);
1894 if (! found_content_type
) {
1895 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
1896 (cp
= strchr (ct
->c_ctline
, ';'))
1897 ? concat (type_subtypename
, cp
, "\n", NULL
)
1898 : mh_xstrdup (name_plus_nl
));
1901 /* Some of these might not be used, but set them anyway. */
1903 ? concat (type_subtypename
, cp
, NULL
)
1904 : concat (type_subtypename
, NULL
);
1905 free (ct
->c_ctline
);
1906 ct
->c_ctline
= ctline
;
1907 /* Leave other ctinfo members as they were. */
1908 free (ct
->c_ctinfo
.ci_type
);
1909 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1910 free (ct
->c_ctinfo
.ci_subtype
);
1911 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1913 ct
->c_subtype
= subtype
;
1915 free (name_plus_nl
);
1916 free (type_subtypename
);
1918 status
= set_ce (ct
, encoding
);
1925 * It's not necessary to update the charset parameter of a Content-Type
1926 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1927 * (content) was originally in the specified charset, "and will be in
1928 * that character set again after decoding."
1931 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
1932 int *message_mods
) {
1934 int lf_line_endings
= 0;
1936 switch (ct
->c_type
) {
1937 case CT_MULTIPART
: {
1938 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1941 /* Should check to see if the body for this part is encoded?
1942 For now, it gets passed along as-is by InitMultiPart(). */
1943 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1944 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
1951 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1952 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1954 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
1960 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
1965 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
1967 switch (ct
->c_encoding
) {
1972 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1973 const char *reason
= NULL
;
1975 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1976 && encoding
!= CE_BINARY
) {
1977 /* The decoding isn't acceptable so discard it.
1978 Leave status as OK to allow other transformations. */
1980 report (NULL
, ct
->c_partno
, ct
->c_file
,
1981 "will not decode%s because it is binary (%s)",
1983 : ct
->c_ctline
? ct
->c_ctline
1987 (void) m_unlink (ct
->c_cefile
.ce_file
);
1988 free (ct
->c_cefile
.ce_file
);
1989 ct
->c_cefile
.ce_file
= NULL
;
1990 } else if (ct
->c_encoding
== CE_QUOTED
&&
1991 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1992 /* The decoding isn't acceptable so discard it.
1993 Leave status as OK to allow other transformations. */
1995 report (NULL
, ct
->c_partno
, ct
->c_file
,
1996 "will not decode%s because it is 8bit",
1998 : ct
->c_ctline
? ct
->c_ctline
2001 (void) m_unlink (ct
->c_cefile
.ce_file
);
2002 free (ct
->c_cefile
.ce_file
);
2003 ct
->c_cefile
.ce_file
= NULL
;
2007 if (ct_encoding
== CE_BINARY
) {
2009 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2014 if (set_ce (ct
, enc
) == OK
) {
2017 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2018 ct
->c_ctline
? ct
->c_ctline
: "");
2020 if (lf_line_endings
) {
2021 strip_crs (ct
, message_mods
);
2034 if (lf_line_endings
) {
2035 strip_crs (ct
, message_mods
);
2050 * Determine if the part with type[/subtype] should be decoded, according to
2051 * decodetypes (which came from the -decodetypes switch).
2054 should_decode(const char *decodetypes
, const char *type
, const char *subtype
) {
2055 /* Quick search for matching type[/subtype] in decodetypes: bracket
2056 decodetypes with commas, then search for ,type, and ,type/subtype, in
2059 int found_match
= 0;
2060 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2061 char *delimited_type
= concat(",", type
, ",", NULL
);
2063 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2065 } else if (subtype
!= NULL
) {
2066 char *delimited_type_subtype
=
2067 concat(",", type
, "/", subtype
, ",", NULL
);
2069 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2072 free(delimited_type_subtype
);
2075 free(delimited_type
);
2076 free(delimited_decodetypes
);
2083 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2084 * if it has any NUL characters, a CR not followed by a LF, or lines
2085 * greater than 998 characters in length. If binary, reason is set
2086 * to a string explaining why.
2089 content_encoding (CT ct
, const char **reason
) {
2090 CE ce
= &ct
->c_cefile
;
2091 int encoding
= CE_7BIT
;
2094 size_t line_len
= 0;
2095 char buffer
[NMH_BUFSIZ
];
2098 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2099 advise (ce
->ce_file
, "unable to open for reading");
2103 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2104 while (encoding
!= CE_BINARY
&&
2105 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2108 int last_char_was_cr
= 0;
2110 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2111 if (*cp
== '\0' || ++line_len
> 998 ||
2112 (*cp
!= '\n' && last_char_was_cr
)) {
2113 encoding
= CE_BINARY
;
2115 *reason
= "null character";
2116 } else if (line_len
> 998) {
2117 *reason
= "line length > 998";
2118 } else if (*cp
!= '\n' && last_char_was_cr
) {
2119 *reason
= "CR not followed by LF";
2121 /* Should not reach this. */
2125 } else if (*cp
== '\n') {
2127 } else if (! isascii ((unsigned char) *cp
)) {
2131 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2137 } /* else should never happen */
2144 * Strip carriage returns from content.
2147 strip_crs (CT ct
, int *message_mods
) {
2148 char *charset
= content_charset (ct
);
2151 /* Only strip carriage returns if content is ASCII or another
2152 charset that has the same readily recognizable CR followed by a
2153 LF. We can include UTF-8 here because if the high-order bit of
2154 a UTF-8 byte is 0, then it must be a single-byte ASCII
2156 if (! strcasecmp (charset
, "US-ASCII") ||
2157 ! strcasecmp (charset
, "UTF-8") ||
2158 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2159 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2165 int opened_input_file
= 0;
2167 if (ct
->c_cefile
.ce_file
) {
2168 file
= &ct
->c_cefile
.ce_file
;
2169 fp
= &ct
->c_cefile
.ce_fp
;
2171 } else if (ct
->c_file
) {
2174 begin
= (size_t) ct
->c_begin
;
2175 end
= (size_t) ct
->c_end
;
2176 } /* else don't know where the content is */
2178 if (file
&& *file
&& fp
) {
2180 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2181 advise (*file
, "unable to open for reading");
2184 opened_input_file
= 1;
2190 char buffer
[NMH_BUFSIZ
];
2192 size_t bytes_to_read
=
2193 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2195 fseeko (*fp
, begin
, SEEK_SET
);
2196 while ((bytes_read
= fread (buffer
, 1,
2197 min (bytes_to_read
, sizeof buffer
),
2199 /* Look for CR followed by a LF. This is supposed to
2200 be text so there should be LF's. If not, don't
2201 modify the content. */
2204 int last_char_was_cr
= 0;
2206 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2208 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2209 if (*cp
== '\n' && last_char_was_cr
) {
2214 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2220 char *stripped_content_file
;
2221 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2223 if (tempfile
== NULL
) {
2224 adios (NULL
, "unable to create temporary file in %s",
2227 stripped_content_file
= mh_xstrdup (tempfile
);
2229 /* Strip each CR before a LF from the content. */
2230 fseeko (*fp
, begin
, SEEK_SET
);
2231 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2235 int last_char_was_cr
= 0;
2237 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2239 last_char_was_cr
= 1;
2240 } else if (last_char_was_cr
) {
2242 if (write (fd
, "\r", 1) < 0) {
2243 advise (tempfile
, "CR write");
2246 if (write (fd
, cp
, 1) < 0) {
2247 advise (tempfile
, "write");
2249 last_char_was_cr
= 0;
2251 if (write (fd
, cp
, 1) < 0) {
2252 advise (tempfile
, "write");
2254 last_char_was_cr
= 0;
2260 admonish (NULL
, "unable to write temporary file %s",
2261 stripped_content_file
);
2262 (void) m_unlink (stripped_content_file
);
2265 /* Replace the decoded file with the converted one. */
2266 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2267 (void) m_unlink (ct
->c_cefile
.ce_file
);
2269 mh_xfree(ct
->c_cefile
.ce_file
);
2270 ct
->c_cefile
.ce_file
= stripped_content_file
;
2271 ct
->c_cefile
.ce_unlink
= 1;
2275 report (NULL
, ct
->c_partno
,
2276 begin
== 0 && end
== 0 ? "" : *file
,
2282 if (opened_input_file
) {
2296 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2297 * of the part C-T-E's.
2300 update_cte (CT ct
) {
2301 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2303 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2304 least_restrictive_enc
!= CE_7BIT
) {
2305 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2309 /* Update/add Content-Transfer-Encoding header field. */
2310 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2311 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2318 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2325 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2329 least_restrictive_encoding (CT ct
) {
2330 int encoding
= CE_UNKNOWN
;
2332 switch (ct
->c_type
) {
2333 case CT_MULTIPART
: {
2334 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2337 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2338 const int part_encoding
=
2339 least_restrictive_encoding (part
->mp_part
);
2341 if (less_restrictive (encoding
, part_encoding
)) {
2342 encoding
= part_encoding
;
2349 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2350 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2351 const int part_encoding
=
2352 least_restrictive_encoding (e
->eb_content
);
2354 if (less_restrictive (encoding
, part_encoding
)) {
2355 encoding
= part_encoding
;
2361 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2362 encoding
= ct
->c_encoding
;
2371 * Return whether the second encoding is less restrictive than the first, where
2372 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2373 * CE_BINARY is less restrictive than CE_8BIT and
2374 * CE_8BIT is less restrictive than CE_7BIT.
2377 less_restrictive (int encoding
, int second_encoding
) {
2378 switch (second_encoding
) {
2380 return encoding
!= CE_BINARY
;
2382 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2384 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2385 encoding
!= CE_7BIT
;
2393 * Convert character set of each part.
2396 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2399 switch (ct
->c_type
) {
2401 if (ct
->c_subtype
== TEXT_PLAIN
) {
2402 status
= convert_charset (ct
, dest_charset
, message_mods
);
2405 char *ct_charset
= content_charset (ct
);
2407 report (NULL
, ct
->c_partno
, ct
->c_file
,
2408 "convert %s to %s", ct_charset
, dest_charset
);
2412 char *ct_charset
= content_charset (ct
);
2414 report ("iconv", ct
->c_partno
, ct
->c_file
,
2415 "failed to convert %s to %s", ct_charset
, dest_charset
);
2421 case CT_MULTIPART
: {
2422 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2425 /* Should check to see if the body for this part is encoded?
2426 For now, it gets passed along as-is by InitMultiPart(). */
2427 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2429 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2435 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2436 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2439 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2452 * Fix various problems that aren't handled elsewhere. These
2453 * are fixed unconditionally: there are no switches to disable
2454 * them. Currently, "problems" are these:
2455 * 1) remove extraneous semicolon at the end of a header parameter list
2456 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2457 * filename parameters in Content-Type and Content-Disposition
2458 * headers, respectively.
2461 fix_always (CT ct
, int *message_mods
) {
2464 switch (ct
->c_type
) {
2465 case CT_MULTIPART
: {
2466 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2469 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2470 status
= fix_always (part
->mp_part
, message_mods
);
2476 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2477 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2479 status
= fix_always (e
->eb_content
, message_mods
);
2486 if (ct
->c_first_hf
) {
2487 fix_filename_encoding (ct
);
2490 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2491 size_t len
= strlen (hf
->value
);
2493 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2494 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2495 /* Only do this for Content-Type and
2496 Content-Disposition fields because those are the
2497 only headers that parse_mime() warns about. */
2501 /* whitespace following a trailing ';' will be nuked as well */
2502 if (hf
->value
[len
- 1] == '\n') {
2503 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2504 if (len
-- == 0) { break; }
2508 if (hf
->value
[len
- 2] == ';') {
2509 /* Remove trailing ';' from parameter value. */
2510 hf
->value
[len
- 2] = '\n';
2511 hf
->value
[len
- 1] = '\0';
2513 /* Also, if Content-Type parameter, remove trailing ';'
2514 from ct->c_ctline. This probably isn't necessary
2516 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2517 size_t l
= strlen(ct
->c_ctline
) - 1;
2518 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2519 ct
->c_ctline
[l
] == ';') {
2520 ct
->c_ctline
[l
--] = '\0';
2521 if (l
== 0) { break; }
2527 report (NULL
, ct
->c_partno
, ct
->c_file
,
2528 "remove trailing ; from %s parameter value",
2540 * Factor out common code for loops in fix_filename_encoding().
2543 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
) {
2546 if (HasPrefix(value
, "=?") && HasSuffix(value
, "?=")) {
2547 /* Looks like an RFC 2047 encoded parameter. */
2548 char decoded
[PATH_MAX
+ 1];
2550 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2551 /* Encode using RFC 2231. */
2552 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2555 advise (NULL
, "failed to decode %s parameter %s", name
, value
);
2564 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2565 * filename parameters in Content-Type and Content-Disposition
2566 * headers, respectively.
2569 fix_filename_encoding (CT ct
) {
2574 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2575 if (pm
->pm_name
&& pm
->pm_value
&&
2576 strcasecmp (pm
->pm_name
, "name") == 0) {
2577 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2578 &ct
->c_ctinfo
.ci_first_pm
,
2579 &ct
->c_ctinfo
.ci_last_pm
);
2583 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2584 if (pm
->pm_name
&& pm
->pm_value
&&
2585 strcasecmp (pm
->pm_name
, "filename") == 0) {
2586 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2592 /* Fix hf values to correspond. */
2593 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2594 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2596 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2597 field
= TYPE_HEADER
;
2598 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2599 field
= DISPO_HEADER
;
2602 if (field
!= OTHER
) {
2603 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2605 if (semicolon_loc
) {
2607 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2608 const char *const params
=
2610 field
== TYPE_HEADER
2611 ? ct
->c_ctinfo
.ci_first_pm
2612 : ct
->c_dispo_first
,
2614 const char *const new_params
= concat (params
, "\n", NULL
);
2616 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2617 free((void *)new_params
); /* Cast away const. Sigh. */
2618 free((void *)params
);
2620 advise (NULL
, "did not find semicolon in %s:%s\n",
2621 hf
->name
, hf
->value
);
2631 * Output content in input file to output file.
2634 write_content (CT ct
, const char *input_filename
, char *outfile
, int modify_inplace
,
2638 if (modify_inplace
) {
2639 if (message_mods
> 0) {
2640 if ((status
= output_message (ct
, outfile
)) == OK
) {
2641 char *infile
= input_filename
2642 ? mh_xstrdup (input_filename
)
2643 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2645 if (remove_file (infile
) == OK
) {
2646 if (rename (outfile
, infile
)) {
2647 /* Rename didn't work, possibly because of an
2648 attempt to rename across filesystems. Try
2649 brute force copy. */
2650 int old
= open (outfile
, O_RDONLY
);
2652 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2655 if (old
!= -1 && new != -1) {
2656 char buffer
[NMH_BUFSIZ
];
2658 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2660 if (write (new, buffer
, i
) != i
) {
2666 if (new != -1) { close (new); }
2667 if (old
!= -1) { close (old
); }
2668 (void) m_unlink (outfile
);
2671 /* The -file argument processing used path() to
2672 expand filename to absolute path. */
2673 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2675 admonish (NULL
, "unable to rename %s %s to %s",
2676 file
? "file" : "message", outfile
,
2682 admonish (NULL
, "unable to remove input file %s, "
2683 "not modifying it", infile
);
2684 (void) m_unlink (outfile
);
2693 /* No modifications and didn't need the tmp outfile. */
2694 (void) m_unlink (outfile
);
2697 /* Output is going to some file. Produce it whether or not
2698 there were modifications. */
2699 status
= output_message (ct
, outfile
);
2708 * parse_mime() does not set lf_line_endings in struct text, so use this
2709 * function to do it. It touches the parts the decodetypes identifies.
2712 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
) {
2713 switch (ct
->c_type
) {
2714 case CT_MULTIPART
: {
2715 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2718 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2719 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2725 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2726 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2728 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2733 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2734 if (ct
->c_ctparams
== NULL
) {
2735 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2737 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2744 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2745 * use the standard MH backup file.
2748 remove_file (const char *file
) {
2750 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2751 int status
= system (rmm_command
);
2754 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2756 /* This is OK for a non-message file, it still uses the
2757 BACKUP_PREFIX form. The backup file will be in the same
2758 directory as file. */
2759 return rename (file
, m_backup (file
));
2764 * Output formatted message to user.
2767 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2772 va_start (args
, message
);
2773 fmt
= concat (filename
, partno
? " part " : ", ",
2774 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
2776 advertise (what
, NULL
, fmt
, args
);
2789 fprintf (stderr
, "\n");