1 /* mhfixmsg.c -- rewrite a message with various transformations
3 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
4 * See the COPYRIGHT file in the root directory of the nmh
5 * distribution for complete copyright information.
9 #include <h/fmt_scan.h>
11 #include <h/mhparse.h>
13 #include <h/signals.h>
14 #include "sbr/m_maildir.h"
15 #include "sbr/m_mktemp.h"
16 #include "sbr/mime_type.h"
20 #include "mhshowsbr.h"
23 #define MHFIXMSG_SWITCHES \
24 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
25 X("nodecodetext", 0, NDECODETEXTSW) \
26 X("decodetypes", 0, DECODETYPESW) \
27 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
28 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
29 X("textcharset", 0, TEXTCHARSETSW) \
30 X("notextcharset", 0, NTEXTCHARSETSW) \
31 X("reformat", 0, REFORMATSW) \
32 X("noreformat", 0, NREFORMATSW) \
33 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
34 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
35 X("fixboundary", 0, FIXBOUNDARYSW) \
36 X("nofixboundary", 0, NFIXBOUNDARYSW) \
37 X("fixcte", 0, FIXCOMPOSITECTESW) \
38 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
39 X("fixtype mimetype", 0, FIXTYPESW) \
40 X("file file", 0, FILESW) \
41 X("outfile file", 0, OUTFILESW) \
42 X("rmmproc program", 0, RPROCSW) \
43 X("normmproc", 0, NRPRCSW) \
44 X("changecur", 0, CHGSW) \
45 X("nochangecur", 0, NCHGSW) \
46 X("verbose", 0, VERBSW) \
47 X("noverbose", 0, NVERBSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(MHFIXMSG
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
61 int debugsw
; /* Needed by mhparse.c. */
63 #define quitser pipeser
68 typedef struct fix_transformations
{
76 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
79 } fix_transformations
;
81 static int mhfixmsgsbr (CT
*, char *, const fix_transformations
*,
82 FILE **, char *, FILE **);
83 static int fix_boundary (CT
*, int *);
84 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
85 static int get_multipart_boundary (CT
, char **);
86 static int replace_boundary (CT
, char *, char *);
87 static int fix_types (CT
, svector_t
, int *);
88 static char *replace_substring (char **, const char *, const char *);
89 static char *remove_parameter (char *, const char *);
90 static int fix_composite_cte (CT
, int *);
91 static int set_ce (CT
, int);
92 static int ensure_text_plain (CT
*, CT
, int *, int);
93 static int find_textplain_sibling (CT
, int, int *);
94 static int insert_new_text_plain_part (CT
, int, CT
);
95 static CT
build_text_plain_part (CT
);
96 static int insert_into_new_mp_alt (CT
*, int *);
97 static CT
divide_part (CT
);
98 static void copy_ctinfo (CI
, CI
);
99 static int decode_part (CT
);
100 static int reformat_part (CT
, char *, char *, char *, int);
101 static CT
build_multipart_alt (CT
, CT
, int, int);
102 static int boundary_in_content (FILE **, char *, const char *);
103 static void transfer_noncontent_headers (CT
, CT
);
104 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
105 static int decode_text_parts (CT
, int, const char *, int *);
106 static int should_decode(const char *, const char *, const char *);
107 static int content_encoding (CT
, const char **);
108 static int strip_crs (CT
, int *);
109 static void update_cte (CT
);
110 static int least_restrictive_encoding (CT
) PURE
;
111 static int less_restrictive (int, int);
112 static int convert_charsets (CT
, char *, int *);
113 static int fix_always (CT
, int *);
114 static int fix_filename_param (char *, char *, PM
*, PM
*);
115 static int fix_filename_encoding (CT
);
116 static int write_content (CT
, const char *, char *, FILE *, int, int);
117 static void set_text_ctparams(CT
, char *, int);
118 static int remove_file (const char *);
119 static void report (char *, char *, char *, char *, ...)
121 static void pipeser (int);
125 main (int argc
, char **argv
) {
127 char *cp
, *file
= NULL
, *folder
= NULL
;
128 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
129 char **argp
, **arguments
;
130 struct msgs_array msgs
= { 0, 0, NULL
};
131 struct msgs
*mp
= NULL
;
133 FILE *fp
, *infp
= NULL
, *outfp
= NULL
;
137 fix_transformations fx
;
138 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
140 fx
.replacetextplain
= 0;
141 fx
.decodetext
= CE_8BIT
;
142 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
143 fx
.lf_line_endings
= 0;
144 fx
.textcharset
= NULL
;
146 if (nmh_init(argv
[0], 2)) { return 1; }
148 arguments
= getarguments (invo_name
, argc
, argv
, 1);
154 while ((cp
= *argp
++)) {
156 switch (smatch (++cp
, switches
)) {
158 ambigsw (cp
, switches
);
161 adios (NULL
, "-%s unknown", cp
);
164 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
166 print_help (buf
, switches
, 1);
169 print_version(invo_name
);
173 if (! (cp
= *argp
++) || *cp
== '-') {
174 adios (NULL
, "missing argument to %s", argp
[-2]);
176 if (! strcasecmp (cp
, "8bit")) {
177 fx
.decodetext
= CE_8BIT
;
178 } else if (! strcasecmp (cp
, "7bit")) {
179 fx
.decodetext
= CE_7BIT
;
180 } else if (! strcasecmp (cp
, "binary")) {
181 fx
.decodetext
= CE_BINARY
;
183 adios (NULL
, "invalid argument to %s", argp
[-2]);
190 if (! (cp
= *argp
++) || *cp
== '-') {
191 adios (NULL
, "missing argument to %s", argp
[-2]);
195 case CRLFLINEBREAKSSW
:
196 fx
.lf_line_endings
= 0;
198 case NCRLFLINEBREAKSSW
:
199 fx
.lf_line_endings
= 1;
202 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
203 adios (NULL
, "missing argument to %s", argp
[-2]);
216 case FIXCOMPOSITECTESW
:
217 fx
.fixcompositecte
= 1;
219 case NFIXCOMPOSITECTESW
:
220 fx
.fixcompositecte
= 0;
223 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
224 adios (NULL
, "missing argument to %s", argp
[-2]);
226 if (! strncasecmp (cp
, "multipart/", 10) ||
227 ! strncasecmp (cp
, "message/", 8))
228 adios (NULL
, "-fixtype %s not allowed", cp
);
229 if (! strchr (cp
, '/'))
230 adios (NULL
, "-fixtype requires type/subtype");
231 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
232 svector_push_back (fx
.fixtypes
, cp
);
240 case REPLACETEXTPLAINSW
:
241 fx
.replacetextplain
= 1;
243 case NREPLACETEXTPLAINSW
:
244 fx
.replacetextplain
= 0;
247 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
248 adios (NULL
, "missing argument to %s", argp
[-2]);
250 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
253 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
254 adios (NULL
, "missing argument to %s", argp
[-2]);
256 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
259 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
260 adios (NULL
, "missing argument to %s", argp
[-2]);
280 if (*cp
== '+' || *cp
== '@') {
282 adios (NULL
, "only one folder at a time!");
283 folder
= pluspath (cp
);
286 /* Interpret a full path as a filename, not a message. */
287 file
= mh_xstrdup (cp
);
289 app_msgarg (&msgs
, cp
);
294 SIGNAL (SIGQUIT
, quitser
);
295 SIGNAL (SIGPIPE
, pipeser
);
298 * Read the standard profile setup
300 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
301 readconfig(NULL
, fp
, cp
, 0);
305 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= true;
306 suppress_extraneous_trailing_semicolon_warning
= true;
308 if (! context_find ("path")) {
309 free (path ("./", TFOLDER
));
312 if (file
&& msgs
.size
) {
313 adios (NULL
, "cannot specify msg and file at same time!");
317 /* Open the outfile now, so we don't have to risk opening it
318 after running out of fds. */
319 if (strcmp (outfile
, "-") == 0) {
321 } else if ((outfp
= fopen (outfile
, "w")) == NULL
) {
322 adios (outfile
, "unable to open for writing");
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 inform("unable to parse message from file %s", file
);
367 /* If there's an outfile, pass the input message unchanged, so the
368 message won't get dropped from a pipeline. */
370 /* Something went wrong. Output might be expected, such as if
371 this were run as a filter. Just copy the input to the
373 if ((infp
= fopen (file
, "r")) == NULL
) {
374 adios (file
, "unable to open for reading");
377 if (copy_input_to_output (file
, infp
, outfile
, outfp
) != OK
) {
378 inform("unable to copy message to %s, "
379 "it might be lost\n", outfile
);
388 * message(s) are coming from a folder
393 app_msgarg(&msgs
, "cur");
396 folder
= getfolder (1);
398 maildir
= mh_xstrdup(m_maildir (folder
));
400 /* chdir so that error messages, esp. from MIME parser, just
401 refer to the message and not its path. */
402 if (chdir (maildir
) == NOTOK
) {
403 adios (maildir
, "unable to change directory to");
406 /* read folder and create message structure */
407 if (! (mp
= folder_read (folder
, 1))) {
408 adios (NULL
, "unable to read folder %s", folder
);
411 /* check for empty folder */
412 if (mp
->nummsg
== 0) {
413 adios (NULL
, "no messages in %s", folder
);
416 /* parse all the message ranges/sequences and set SELECTED */
417 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
418 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
421 seq_setprev (mp
); /* set the previous-sequence */
423 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
426 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
427 if (is_selected(mp
, msgnum
)) {
428 char *msgnam
= m_name (msgnum
);
430 if ((ct
= parse_mime (msgnam
))) {
431 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
434 inform("unable to parse message %s", msgnam
);
437 /* If there's an outfile, pass the input message
438 unchanged, so the message won't get dropped from a
441 /* Something went wrong. Output might be expected,
442 such as if this were run as a filter. Just copy
443 the input to the output. */
444 /* Can't use path() here because 1) it might have been
445 called before and it caches the pwd, and 2) we call
446 chdir() after that. */
447 char *input_filename
=
448 concat (maildir
, "/", msgnam
, NULL
);
450 if ((infp
= fopen (input_filename
, "r")) == NULL
) {
451 adios (input_filename
,
452 "unable to open for reading");
455 if (copy_input_to_output (input_filename
, infp
,
456 outfile
, outfp
) != OK
) {
457 inform("unable to copy message to %s, "
458 "it might be lost\n", outfile
);
463 free (input_filename
);
470 seq_setcur (mp
, mp
->hghsel
); /* update current message */
472 seq_save (mp
); /* synchronize sequences */
473 context_replace (pfolder
, folder
);/* update current folder */
474 context_save (); /* save the context file */
478 for (ctp
= cts
; *ctp
; ++ctp
) {
480 mhfixmsgsbr (ctp
, maildir
, &fx
, &infp
, outfile
, &outfp
) == OK
486 (void) m_unlink (file
);
489 /* Just calling m_backup() unlinks the backup file. */
490 (void) m_backup (file
);
501 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
502 if (infp
) { fclose (infp
); } /* even if stdin */
503 if (outfp
) { fclose (outfp
); } /* even if stdout */
509 done (status
== OK
? 0 : 1);
515 * Apply transformations to one message.
518 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
519 FILE **infp
, char *outfile
, FILE **outfp
) {
520 /* Store input filename in case one of the transformations, i.e.,
521 fix_boundary(), rewrites to a tmp file. */
522 char *input_filename
= maildir
523 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
524 : mh_xstrdup ((*ctp
)->c_file
);
525 int modify_inplace
= 0;
526 int message_mods
= 0;
529 /* Though the input file won't need to be opened if everything goes
530 well, do it here just in case there's a failure, and that failure is
531 running out of file descriptors. */
532 if ((*infp
= fopen (input_filename
, "r")) == NULL
) {
533 adios (input_filename
, "unable to open for reading");
536 if (outfile
== NULL
) {
539 if ((*ctp
)->c_file
) {
541 /* outfp will be closed by the caller */
542 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, outfp
)) ==
544 adios (NULL
, "unable to create temporary file in %s",
547 outfile
= mh_xstrdup (tempfile
);
549 adios (NULL
, "missing both input and output filenames\n");
551 } /* else *outfp was defined by caller */
553 reverse_alternative_parts (*ctp
);
554 status
= fix_always (*ctp
, &message_mods
);
555 if (status
== OK
&& fx
->fixboundary
) {
556 status
= fix_boundary (ctp
, &message_mods
);
558 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
559 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
561 if (status
== OK
&& fx
->fixcompositecte
) {
562 status
= fix_composite_cte (*ctp
, &message_mods
);
564 if (status
== OK
&& fx
->reformat
) {
566 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
568 if (status
== OK
&& fx
->decodetext
) {
569 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
573 if (status
== OK
&& fx
->textcharset
!= NULL
) {
574 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
577 if (status
== OK
&& ! (*ctp
)->c_umask
) {
578 /* Set the umask for the contents file. This currently
579 isn't used but just in case it is in the future. */
582 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
583 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
585 (*ctp
)->c_umask
= ~m_gmprot();
590 * Write the content to a file
593 status
= write_content (*ctp
, input_filename
, outfile
, *outfp
,
594 modify_inplace
, message_mods
);
595 } else if (! modify_inplace
) {
596 /* Something went wrong. Output might be expected, such
597 as if this were run as a filter. Just copy the input
599 if (copy_input_to_output (input_filename
, *infp
, outfile
,
601 inform("unable to copy message to %s, it might be lost\n",
606 if (modify_inplace
) {
607 if (status
!= OK
) { (void) m_unlink (outfile
); }
614 free (input_filename
);
621 * Copy input message to output. Assumes not modifying in place, so this
622 * might be running as part of a pipeline.
625 copy_input_to_output (const char *input_filename
, FILE *infp
,
626 const char *output_filename
, FILE *outfp
) {
627 int in
= fileno (infp
);
628 int out
= fileno (outfp
);
631 if (in
!= -1 && out
!= -1) {
632 cpydata (in
, out
, input_filename
, output_filename
);
642 * Fix mismatched outer level boundary.
645 fix_boundary (CT
*ct
, int *message_mods
) {
646 struct multipart
*mp
;
649 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
650 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
653 * 1) Get boundary at end of part.
654 * 2) Get boundary at beginning of part and compare to the end-of-part
656 * 3) Write out contents of ct to tmp file, replacing boundary in
657 * header with boundary from part. Set c_unlink to 1.
659 * 5) Call parse_mime() on the tmp file, replacing ct.
662 if (mp
&& mp
->mp_start
) {
665 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
668 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
669 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
670 char *filename
= mh_xstrdup ((*ct
)->c_file
);
674 if ((fixed_ct
= parse_mime (fixed
))) {
680 report (NULL
, NULL
, filename
,
681 "fix multipart boundary");
685 inform("unable to parse fixed part");
690 inform("unable to replace broken boundary");
694 inform("unable to create temporary file in %s",
699 free (part_boundary
);
701 /* Couldn't fix the boundary. Report failure so that mhfixmsg
702 doesn't modify the message. */
706 /* No multipart struct, even though the content type is
707 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
718 * Find boundary at end of multipart.
721 get_multipart_boundary (CT ct
, char **part_boundary
) {
722 char buffer
[NMH_BUFSIZ
];
723 char *end_boundary
= NULL
;
724 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
725 ? (off_t
) (ct
->c_end
- sizeof buffer
)
726 : (off_t
) ct
->c_begin
;
730 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
731 be big enough, even if it's just 1024, to make that unlikely. */
733 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
734 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
735 advise (ct
->c_file
, "unable to open for reading");
739 /* Get boundary at end of multipart. */
740 while (begin
>= (off_t
) ct
->c_begin
) {
741 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
742 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
743 char *cp
= rfind_str (buffer
, bytes_read
, "--");
748 /* Trim off trailing "--" and anything beyond. */
750 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
751 if (strlen (end
) > 3 && *end
++ == '\n' &&
752 *end
++ == '-' && *end
++ == '-') {
753 end_boundary
= mh_xstrdup (end
);
760 if (end_boundary
|| begin
<= (off_t
) (ct
->c_begin
+ sizeof buffer
))
762 begin
-= sizeof buffer
;
765 /* Get boundary at beginning of multipart. */
767 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
768 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
769 if (bytes_read
>= strlen (end_boundary
)) {
770 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
772 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
773 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
778 /* The start and end boundaries didn't match, or the
779 start boundary doesn't begin with "\n--" (or "--"
780 if at the beginning of buffer). Keep trying. */
794 *part_boundary
= end_boundary
;
796 *part_boundary
= NULL
;
805 * Open and copy ct->c_file to file, replacing the multipart boundary.
808 replace_boundary (CT ct
, char *file
, char *boundary
) {
811 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
813 m_getfld_state_t gstate
;
816 if (ct
->c_file
== NULL
) {
817 inform("missing input filename");
821 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
822 advise (ct
->c_file
, "unable to open for reading");
826 if ((fpout
= fopen (file
, "w")) == NULL
) {
828 advise (file
, "unable to open for writing");
832 gstate
= m_getfld_state_init(fpin
);
833 for (compnum
= 1;;) {
834 int bufsz
= (int) sizeof buf
;
836 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
841 /* get copies of the buffers */
842 np
= mh_xstrdup (name
);
843 vp
= mh_xstrdup (buf
);
845 /* if necessary, get rest of field */
846 while (state
== FLDPLUS
) {
848 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
849 vp
= add (buf
, vp
); /* add to previous value */
852 if (strcasecmp (TYPE_FIELD
, np
)) {
853 fprintf (fpout
, "%s:%s", np
, vp
);
855 char *new_ctline
, *new_params
;
857 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
858 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
861 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
862 ct
->c_ctinfo
.ci_subtype
, NULL
);
863 new_params
= output_params(LEN(TYPE_FIELD
) +
864 strlen(new_ctline
) + 1,
865 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
866 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
867 FENDNULL(new_params
));
879 /* buf will have a terminating NULL, skip it. */
880 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
881 advise (file
, "fwrite");
890 inform("message format error in component #%d", compnum
);
895 inform("getfld() returned %d", state
);
903 m_getfld_state_destroy (&gstate
);
912 * Fix Content-Type header to reflect the content of its part.
915 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
918 switch (ct
->c_type
) {
920 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
923 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
924 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
930 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
931 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
933 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
940 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
941 for (typep
= svector_strs (fixtypes
);
942 typep
&& (type
= *typep
);
945 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
948 if (! strcasecmp (type
, type_subtype
) &&
949 decode_part (ct
) == OK
&&
950 ct
->c_cefile
.ce_file
!= NULL
) {
951 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
954 if ((cp
= strchr (ct_type_subtype
, ';'))) {
955 /* Truncate to remove any parameter list from
956 mime_type () result. */
960 if (strcasecmp (type
, ct_type_subtype
)) {
961 char *ct_type
, *ct_subtype
;
964 /* The Content-Type header does not match the
965 content, so update these struct Content
968 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
971 /* Extract type and subtype from type/subtype. */
972 ct_type
= mh_xstrdup(ct_type_subtype
);
973 if ((cp
= strchr (ct_type
, '/'))) {
975 ct_subtype
= mh_xstrdup(++cp
);
977 inform("missing / in MIME type of %s %s",
978 ct
->c_file
, ct
->c_partno
);
983 ct
->c_type
= ct_str_type (ct_type
);
984 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
986 free (ct
->c_ctinfo
.ci_type
);
987 ct
->c_ctinfo
.ci_type
= ct_type
;
988 free (ct
->c_ctinfo
.ci_subtype
);
989 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
990 if (! replace_substring (&ct
->c_ctline
, type
,
992 inform("did not find %s in %s",
996 /* Update Content-Type header field. */
997 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
998 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
999 if (replace_substring (&hf
->value
, type
,
1003 report (NULL
, ct
->c_partno
, ct
->c_file
,
1004 "change Content-Type in header "
1006 type
, ct_type_subtype
);
1010 inform("did not find %s in %s", type
, hf
->value
);
1014 free (ct_type_subtype
);
1016 free (type_subtype
);
1026 * Replace a substring, allocating space to hold the new one.
1029 replace_substring (char **str
, const char *old
, const char *new) {
1032 if ((cp
= strstr (*str
, old
))) {
1033 char *remainder
= cp
+ strlen (old
);
1034 char *prefix
, *new_str
;
1037 prefix
= mh_xstrdup(*str
);
1038 *(prefix
+ (cp
- *str
)) = '\0';
1039 new_str
= concat (prefix
, new, remainder
, NULL
);
1042 new_str
= concat (new, remainder
, NULL
);
1047 return *str
= new_str
;
1055 * Remove a name=value parameter, given just its name, from a header value.
1058 remove_parameter (char *str
, const char *name
) {
1059 /* It looks to me, based on the BNF in RFC 2045, than there can't
1060 be whitespace between the parameter name and the "=", or
1061 between the "=" and the parameter value. */
1062 char *param_name
= concat (name
, "=", NULL
);
1065 if ((cp
= strstr (str
, param_name
))) {
1069 /* Remove any leading spaces, before the parameter name. */
1071 start
> str
&& isspace ((unsigned char) *(start
-1));
1075 /* Remove a leading semicolon. */
1076 if (start
> str
&& *(start
-1) == ';') { --start
; }
1078 end
= cp
+ strlen (name
) + 1;
1080 /* Skip past the quoted value, and then the final quote. */
1081 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1084 /* Skip past the value. */
1085 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1088 /* Count how many characters need to be moved. Include
1089 trailing null, which is accounted for by the
1090 initialization of count to 1. */
1091 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1092 (void) memmove (start
, end
, count
);
1102 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1103 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1107 fix_composite_cte (CT ct
, int *message_mods
) {
1110 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1111 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1112 ct
->c_encoding
!= CE_BINARY
) {
1115 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1116 char *name
= hf
->name
;
1117 for (; isspace((unsigned char)*name
); ++name
) {
1121 if (! strncasecmp (name
, ENCODING_FIELD
,
1122 LEN(ENCODING_FIELD
))) {
1123 char *prefix
= "Nmh-REPLACED-INVALID-";
1127 h
->name
= mh_xstrdup (hf
->name
);
1128 h
->hf_encoding
= hf
->hf_encoding
;
1132 /* Retain old header but prefix its name. */
1134 hf
->name
= concat (prefix
, h
->name
, NULL
);
1138 char *encoding
= cpytrim (hf
->value
);
1139 report (NULL
, ct
->c_partno
, ct
->c_file
,
1140 "replace Content-Transfer-Encoding of %s "
1141 "with 8 bit", encoding
);
1145 h
->value
= mh_xstrdup (" 8bit\n");
1147 /* Don't need to warn for multiple C-T-E header
1148 fields, parse_mime() already does that. But
1149 if there are any, fix them all as necessary. */
1154 set_ce (ct
, CE_8BIT
);
1157 if (ct
->c_type
== CT_MULTIPART
) {
1158 struct multipart
*m
;
1161 m
= (struct multipart
*) ct
->c_ctparams
;
1162 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1163 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1176 * Set content encoding.
1179 set_ce (CT ct
, int encoding
) {
1180 const char *ce
= ce_str (encoding
);
1181 const struct str2init
*ctinit
= get_ce_method (ce
);
1184 char *cte
= concat (" ", ce
, "\n", NULL
);
1187 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1188 caller is decode_text_parts (). Save because we'll
1190 struct cefile decoded_content_info
= ct
->c_cefile
;
1192 ct
->c_encoding
= encoding
;
1194 ct
->c_ctinitfnx
= ctinit
->si_init
;
1195 /* This will assign ct->c_cefile with an all-0 struct, which
1197 (*ctinit
->si_init
) (ct
);
1198 /* After returning, the caller should set
1199 ct->c_cefile.ce_file to the name of the file containing
1202 if (ct
->c_ceclosefnx
) {
1203 (*ct
->c_ceclosefnx
) (ct
);
1206 /* Restore the cefile. */
1207 ct
->c_cefile
= decoded_content_info
;
1209 /* Update/add Content-Transfer-Encoding header field. */
1210 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1211 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1218 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1221 /* Update c_celine. It's used only by mhlist -debug. */
1222 free (ct
->c_celine
);
1223 ct
->c_celine
= mh_xstrdup (cte
);
1233 * Make sure each text part has a corresponding text/plain part.
1236 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1239 switch ((*ct
)->c_type
) {
1241 /* Nothing to do for text/plain. */
1242 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1244 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1245 parent
->c_subtype
== MULTI_ALTERNATE
) {
1246 int new_subpart_number
= 1;
1247 int has_text_plain
=
1248 find_textplain_sibling (parent
, replacetextplain
,
1249 &new_subpart_number
);
1251 if (! has_text_plain
) {
1252 /* Parent is a multipart/alternative. Insert a new
1253 text/plain subpart. */
1254 const int inserted
=
1255 insert_new_text_plain_part (*ct
, new_subpart_number
,
1260 report (NULL
, parent
->c_partno
, parent
->c_file
,
1261 "insert text/plain part");
1267 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1268 parent
->c_subtype
== MULTI_RELATED
) {
1269 char *type_subtype
=
1270 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1271 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1272 const char *parent_type
=
1273 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1274 int new_subpart_number
= 1;
1275 int has_text_plain
= 0;
1277 /* Have to do string comparison on the subtype because we
1278 don't enumerate all of them in c_subtype values.
1279 parent_type will be NULL if the multipart/related part
1280 doesn't have a type parameter. The type parameter must
1281 be specified according to RFC 2387 Sec. 3.1 but not all
1283 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1284 /* The type of this part matches the root type of the
1285 parent multipart/related. Look to see if there's
1286 text/plain sibling. */
1288 find_textplain_sibling (parent
, replacetextplain
,
1289 &new_subpart_number
);
1292 free (type_subtype
);
1294 if (! has_text_plain
) {
1295 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1299 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1300 if (*ct
!= part
->mp_part
) {
1306 /* Parent is a multipart/related. Insert a new
1307 text/plain subpart in a new multipart/alternative. */
1308 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1309 /* Not an error if text/plain couldn't be added. */
1312 /* There are no siblings, so insert a new text/plain
1313 subpart, and change the parent type from
1314 multipart/related to multipart/alternative. */
1315 const int inserted
=
1316 insert_new_text_plain_part (*ct
, new_subpart_number
,
1322 parent
->c_subtype
= MULTI_ALTERNATE
;
1323 free (parent
->c_ctinfo
.ci_subtype
);
1324 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1325 if (! replace_substring (&parent
->c_ctline
, "/related",
1327 inform("did not find multipart/related in %s",
1331 /* Update Content-Type header field. */
1332 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1333 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1334 if (replace_substring (&hf
->value
, "/related",
1338 report (NULL
, parent
->c_partno
,
1340 "insert text/plain part");
1343 /* Remove, e.g., type="text/html" from
1344 multipart/alternative. */
1345 remove_parameter (hf
->value
, "type");
1348 inform("did not find multipart/"
1349 "related in header %s", hf
->value
);
1353 /* Not an error if text/plain couldn't be inserted. */
1358 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1365 case CT_MULTIPART
: {
1366 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1369 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1370 if ((*ct
)->c_type
== CT_MULTIPART
) {
1371 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1379 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1380 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1382 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1393 * See if there is a sibling text/plain, and return its subpart number.
1396 find_textplain_sibling (CT parent
, int replacetextplain
,
1397 int *new_subpart_number
) {
1398 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1399 struct part
*part
, *prev
;
1400 int has_text_plain
= 0;
1402 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1403 ++*new_subpart_number
;
1404 if (part
->mp_part
->c_type
== CT_TEXT
&&
1405 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1406 if (replacetextplain
) {
1407 struct part
*old_part
;
1408 if (part
== mp
->mp_parts
) {
1409 old_part
= mp
->mp_parts
;
1410 mp
->mp_parts
= part
->mp_next
;
1412 old_part
= prev
->mp_next
;
1413 prev
->mp_next
= part
->mp_next
;
1416 report (NULL
, parent
->c_partno
, parent
->c_file
,
1417 "remove text/plain part %s",
1418 old_part
->mp_part
->c_partno
);
1420 free_content (old_part
->mp_part
);
1430 return has_text_plain
;
1435 * Insert a new text/plain part.
1438 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1439 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1440 struct part
*new_part
;
1443 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1445 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1447 new_part
->mp_next
= mp
->mp_parts
;
1448 mp
->mp_parts
= new_part
;
1449 new_part
->mp_part
->c_partno
=
1450 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1456 free_content (new_part
->mp_part
);
1464 * Create a text/plain part to go along with non-plain sibling part.
1467 build_text_plain_part (CT encoded_part
) {
1468 CT tp_part
= divide_part (encoded_part
);
1469 char *tmp_plain_file
= NULL
;
1471 if (decode_part (tp_part
) == OK
) {
1472 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1473 contains the decoded contents. And the decoding function, such
1474 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1475 be unlinked by free_content (). */
1478 /* This m_mktemp2() call closes the temp file. */
1479 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1480 inform("unable to create temporary file in %s",
1483 tmp_plain_file
= mh_xstrdup (tempfile
);
1484 if (reformat_part (tp_part
, tmp_plain_file
,
1485 tp_part
->c_ctinfo
.ci_type
,
1486 tp_part
->c_ctinfo
.ci_subtype
,
1487 tp_part
->c_type
) == OK
) {
1493 free_content (tp_part
);
1494 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1495 free (tmp_plain_file
);
1502 * Slip new text/plain part into a new multipart/alternative.
1505 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1506 CT tp_part
= build_text_plain_part (*ct
);
1510 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1513 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1515 if (mp
&& mp
->mp_parts
) {
1516 mp
->mp_parts
->mp_part
= tp_part
;
1517 /* Make the new multipart/alternative the parent. */
1522 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1523 "insert text/plain part");
1526 free_content (tp_part
);
1527 free_content (mp_alt
);
1534 /* Not an error if text/plain couldn't be built. */
1542 * Clone a MIME part.
1545 divide_part (CT ct
) {
1549 /* Just copy over what is needed for decoding. c_vrsn and
1550 c_celine aren't necessary. */
1551 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1552 new_part
->c_begin
= ct
->c_begin
;
1553 new_part
->c_end
= ct
->c_end
;
1554 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1555 new_part
->c_type
= ct
->c_type
;
1556 new_part
->c_cefile
= ct
->c_cefile
;
1557 new_part
->c_encoding
= ct
->c_encoding
;
1558 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1559 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1560 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1561 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1563 /* c_ctline is used by reformat__part(), so it can preserve
1564 anything after the type/subtype. */
1565 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1572 * Copy the content info from one part to another.
1575 copy_ctinfo (CI dest
, CI src
) {
1578 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1579 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1581 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1582 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1584 if (s_pm
->pm_charset
) {
1585 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1587 if (s_pm
->pm_lang
) {
1588 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1592 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1593 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1601 decode_part (CT ct
) {
1607 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, &file
)) == NULL
) {
1608 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1610 tmp_decoded
= mh_xstrdup (tempfile
);
1611 /* The following call will load ct->c_cefile.ce_file with the tmp
1612 filename of the decoded content. tmp_decoded will contain the
1613 encoded output, get rid of that. */
1614 status
= output_message_fp (ct
, file
, tmp_decoded
);
1615 (void) m_unlink (tmp_decoded
);
1617 if (fclose (file
)) {
1618 inform("unable to close temporary file %s, continuing...", tempfile
);
1626 * Reformat content as plain text.
1627 * Some of the arguments aren't really needed now, but maybe will
1628 * be in the future for other than text types.
1631 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1632 int output_subtype
, output_encoding
;
1633 const char *reason
= NULL
;
1637 /* Hacky: this redirects the output from whatever command is used
1638 to show the part to a file. So, the user can't have any output
1639 redirection in that command.
1640 Could show_multi() in mhshowsbr.c avoid this? */
1642 /* Check for invo_name-format-type/subtype. */
1643 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1645 inform("Don't know how to convert %s, there is no "
1646 "%s-format-%s/%s profile entry",
1647 ct
->c_file
, invo_name
, type
, subtype
);
1651 if (strchr (cf
, '>')) {
1652 inform("'>' prohibited in \"%s\",\nplease fix your "
1653 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1659 cp
= concat (cf
, " >", file
, NULL
);
1660 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1663 /* Unlink decoded content tmp file and free its filename to avoid
1664 leaks. The file stream should already have been closed. */
1665 if (ct
->c_cefile
.ce_unlink
) {
1666 (void) m_unlink (ct
->c_cefile
.ce_file
);
1667 free (ct
->c_cefile
.ce_file
);
1668 ct
->c_cefile
.ce_file
= NULL
;
1669 ct
->c_cefile
.ce_unlink
= 0;
1672 if (c_type
== CT_TEXT
) {
1673 output_subtype
= TEXT_PLAIN
;
1675 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1679 output_encoding
= content_encoding (ct
, &reason
);
1681 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1682 ct
->c_cefile
.ce_file
= file
;
1683 ct
->c_cefile
.ce_unlink
= 1;
1685 ct
->c_cefile
.ce_unlink
= 0;
1694 * Fill in a multipart/alternative part.
1697 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1698 char *boundary_prefix
= "----=_nmh-multipart";
1699 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1700 char *boundary_indicator
= "; boundary=";
1701 char *typename
, *subtypename
, *name
;
1704 struct multipart
*m
;
1705 const struct str2init
*ctinit
;
1709 /* Set up the multipart/alternative part. These fields of *ct were
1710 initialized to 0 by mh_xcalloc():
1711 c_fp, c_unlink, c_begin, c_end,
1712 c_vrsn, c_ctline, c_celine,
1713 c_id, c_descr, c_dispo, c_partno,
1714 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1715 c_cefile, c_encoding,
1716 c_digested, c_digest[16], c_ctexbody,
1717 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1719 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1722 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1724 ct
->c_subtype
= subtype
;
1726 ctinit
= get_ct_init (ct
->c_type
);
1728 typename
= ct_type_str (type
);
1729 subtypename
= ct_subtype_str (type
, subtype
);
1733 int found_boundary
= 1;
1735 while (found_boundary
&& serial
< 1000000) {
1738 /* Ensure that the boundary doesn't appear in the decoded
1740 if (new_part
->c_cefile
.ce_file
) {
1741 if ((found_boundary
=
1742 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1743 new_part
->c_cefile
.ce_file
,
1744 boundary
)) == NOTOK
) {
1749 /* Ensure that the boundary doesn't appear in the encoded
1751 if (! found_boundary
&& new_part
->c_file
) {
1752 if ((found_boundary
=
1753 boundary_in_content (&new_part
->c_fp
,
1755 boundary
)) == NOTOK
) {
1760 if (found_boundary
) {
1761 /* Try a slightly different boundary. */
1766 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1768 concat (boundary_prefix
,
1769 FENDNULL(first_alt
->c_partno
),
1770 "-", buffer2
, NULL
);
1774 if (found_boundary
) {
1775 inform("giving up trying to find a unique boundary");
1780 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1781 boundary
, "\"", NULL
);
1783 /* Load c_first_hf and c_last_hf. */
1784 transfer_noncontent_headers (first_alt
, ct
);
1785 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1788 /* Load c_partno. */
1789 if (first_alt
->c_partno
) {
1790 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1791 free (first_alt
->c_partno
);
1792 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1793 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1795 first_alt
->c_partno
= mh_xstrdup ("1");
1796 new_part
->c_partno
= mh_xstrdup ("2");
1800 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1801 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1804 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1805 "boundary", boundary
, 0);
1809 p
->mp_next
->mp_next
= NULL
;
1810 p
->mp_next
->mp_part
= first_alt
;
1813 m
->mp_start
= concat (boundary
, "\n", NULL
);
1814 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1830 * Check that the boundary does not appear in the content.
1833 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1834 char buffer
[NMH_BUFSIZ
];
1836 int found_boundary
= 0;
1838 /* free_content() will close *fp if we fopen it here. */
1839 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1840 advise (file
, "unable to open %s for reading", file
);
1844 fseeko (*fp
, 0L, SEEK_SET
);
1845 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1846 if (find_str (buffer
, bytes_read
, boundary
)) {
1852 return found_boundary
;
1857 * Remove all non-Content headers.
1860 transfer_noncontent_headers (CT old
, CT
new) {
1863 hp_prev
= hp
= old
->c_first_hf
;
1867 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, LEN(XXX_FIELD_PRF
))) {
1868 if (hp
== old
->c_last_hf
) {
1869 if (hp
== old
->c_first_hf
) {
1870 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1872 hp_prev
->next
= NULL
;
1873 old
->c_last_hf
= hp_prev
;
1876 if (hp
== old
->c_first_hf
) {
1877 old
->c_first_hf
= next
;
1879 hp_prev
->next
= next
;
1883 /* Put node hp in the new CT. */
1884 if (new->c_first_hf
== NULL
) {
1885 new->c_first_hf
= hp
;
1887 new->c_last_hf
->next
= hp
;
1889 new->c_last_hf
= hp
;
1891 /* A Content- header, leave in old. */
1904 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1905 char *typename
= ct_type_str (type
);
1906 char *subtypename
= ct_subtype_str (type
, subtype
);
1907 /* E.g, " text/plain" */
1908 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1909 /* E.g, " text/plain\n" */
1910 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1911 int found_content_type
= 0;
1913 const char *cp
= NULL
;
1917 /* Update/add Content-Type header field. */
1918 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1919 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1920 found_content_type
= 1;
1922 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1923 ? concat (type_subtypename
, cp
, "\n", NULL
)
1924 : mh_xstrdup (name_plus_nl
);
1927 if (! found_content_type
) {
1928 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
1929 (cp
= strchr (ct
->c_ctline
, ';'))
1930 ? concat (type_subtypename
, cp
, "\n", NULL
)
1931 : mh_xstrdup (name_plus_nl
));
1934 /* Some of these might not be used, but set them anyway. */
1936 ? concat (type_subtypename
, cp
, NULL
)
1937 : concat (type_subtypename
, NULL
);
1938 free (ct
->c_ctline
);
1939 ct
->c_ctline
= ctline
;
1940 /* Leave other ctinfo members as they were. */
1941 free (ct
->c_ctinfo
.ci_type
);
1942 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1943 free (ct
->c_ctinfo
.ci_subtype
);
1944 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1946 ct
->c_subtype
= subtype
;
1948 free (name_plus_nl
);
1949 free (type_subtypename
);
1951 status
= set_ce (ct
, encoding
);
1958 * It's not necessary to update the charset parameter of a Content-Type
1959 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1960 * (content) was originally in the specified charset, "and will be in
1961 * that character set again after decoding."
1964 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
1965 int *message_mods
) {
1967 int lf_line_endings
= 0;
1969 switch (ct
->c_type
) {
1970 case CT_MULTIPART
: {
1971 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1974 /* Should check to see if the body for this part is encoded?
1975 For now, it gets passed along as-is by InitMultiPart(). */
1976 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1977 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
1984 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1985 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1987 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
1993 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
1998 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
2000 switch (ct
->c_encoding
) {
2005 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
2006 const char *reason
= NULL
;
2008 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
2009 && encoding
!= CE_BINARY
) {
2010 /* The decoding isn't acceptable so discard it.
2011 Leave status as OK to allow other transformations. */
2013 report (NULL
, ct
->c_partno
, ct
->c_file
,
2014 "will not decode%s because it is binary (%s)",
2016 : (FENDNULL(ct
->c_ctline
)),
2019 (void) m_unlink (ct
->c_cefile
.ce_file
);
2020 free (ct
->c_cefile
.ce_file
);
2021 ct
->c_cefile
.ce_file
= NULL
;
2022 } else if (ct
->c_encoding
== CE_QUOTED
&&
2023 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2024 /* The decoding isn't acceptable so discard it.
2025 Leave status as OK to allow other transformations. */
2027 report (NULL
, ct
->c_partno
, ct
->c_file
,
2028 "will not decode%s because it is 8bit",
2030 : (FENDNULL(ct
->c_ctline
)));
2032 (void) m_unlink (ct
->c_cefile
.ce_file
);
2033 free (ct
->c_cefile
.ce_file
);
2034 ct
->c_cefile
.ce_file
= NULL
;
2038 if (ct_encoding
== CE_BINARY
) {
2040 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2045 if (set_ce (ct
, enc
) == OK
) {
2048 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2049 FENDNULL(ct
->c_ctline
));
2051 if (lf_line_endings
) {
2052 strip_crs (ct
, message_mods
);
2065 if (lf_line_endings
) {
2066 strip_crs (ct
, message_mods
);
2081 * Determine if the part with type[/subtype] should be decoded, according to
2082 * decodetypes (which came from the -decodetypes switch).
2085 should_decode(const char *decodetypes
, const char *type
, const char *subtype
) {
2086 /* Quick search for matching type[/subtype] in decodetypes: bracket
2087 decodetypes with commas, then search for ,type, and ,type/subtype, in
2090 int found_match
= 0;
2091 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2092 char *delimited_type
= concat(",", type
, ",", NULL
);
2094 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2096 } else if (subtype
!= NULL
) {
2097 char *delimited_type_subtype
=
2098 concat(",", type
, "/", subtype
, ",", NULL
);
2100 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2103 free(delimited_type_subtype
);
2106 free(delimited_type
);
2107 free(delimited_decodetypes
);
2114 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2115 * if it has any NUL characters, a CR not followed by a LF, or lines
2116 * greater than 998 characters in length. If binary, reason is set
2117 * to a string explaining why.
2120 content_encoding (CT ct
, const char **reason
) {
2121 CE ce
= &ct
->c_cefile
;
2122 int encoding
= CE_7BIT
;
2125 size_t line_len
= 0;
2126 char buffer
[NMH_BUFSIZ
];
2129 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2130 advise (ce
->ce_file
, "unable to open for reading");
2134 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2135 while (encoding
!= CE_BINARY
&&
2136 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2139 int last_char_was_cr
= 0;
2141 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2142 if (*cp
== '\0' || ++line_len
> 998 ||
2143 (*cp
!= '\n' && last_char_was_cr
)) {
2144 encoding
= CE_BINARY
;
2146 *reason
= "null character";
2147 } else if (line_len
> 998) {
2148 *reason
= "line length > 998";
2149 } else if (*cp
!= '\n' && last_char_was_cr
) {
2150 *reason
= "CR not followed by LF";
2152 /* Should not reach this. */
2159 } else if (! isascii ((unsigned char) *cp
)) {
2163 last_char_was_cr
= *cp
== '\r';
2169 } /* else should never happen */
2176 * Strip carriage returns from content.
2179 strip_crs (CT ct
, int *message_mods
) {
2180 char *charset
= content_charset (ct
);
2183 /* Only strip carriage returns if content is ASCII or another
2184 charset that has the same readily recognizable CR followed by a
2185 LF. We can include UTF-8 here because if the high-order bit of
2186 a UTF-8 byte is 0, then it must be a single-byte ASCII
2188 if (! strcasecmp (charset
, "US-ASCII") ||
2189 ! strcasecmp (charset
, "UTF-8") ||
2190 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2191 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2197 int opened_input_file
= 0;
2199 if (ct
->c_cefile
.ce_file
) {
2200 file
= &ct
->c_cefile
.ce_file
;
2201 fp
= &ct
->c_cefile
.ce_fp
;
2203 } else if (ct
->c_file
) {
2206 begin
= (size_t) ct
->c_begin
;
2207 end
= (size_t) ct
->c_end
;
2208 } /* else don't know where the content is */
2210 if (file
&& *file
&& fp
) {
2212 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2213 advise (*file
, "unable to open for reading");
2216 opened_input_file
= 1;
2222 char buffer
[NMH_BUFSIZ
];
2224 size_t bytes_to_read
=
2225 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2227 fseeko (*fp
, begin
, SEEK_SET
);
2228 while ((bytes_read
= fread (buffer
, 1,
2229 min (bytes_to_read
, sizeof buffer
),
2231 /* Look for CR followed by a LF. This is supposed to
2232 be text so there should be LF's. If not, don't
2233 modify the content. */
2236 int last_char_was_cr
= 0;
2238 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2240 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2241 if (*cp
== '\n' && last_char_was_cr
) {
2246 last_char_was_cr
= *cp
== '\r';
2252 char *stripped_content_file
;
2253 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2255 if (tempfile
== NULL
) {
2256 adios (NULL
, "unable to create temporary file in %s",
2259 stripped_content_file
= mh_xstrdup (tempfile
);
2261 /* Strip each CR before a LF from the content. */
2262 fseeko (*fp
, begin
, SEEK_SET
);
2263 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2267 int last_char_was_cr
= 0;
2269 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2271 last_char_was_cr
= 1;
2272 } else if (last_char_was_cr
) {
2274 if (write (fd
, "\r", 1) < 0) {
2275 advise (tempfile
, "CR write");
2278 if (write (fd
, cp
, 1) < 0) {
2279 advise (tempfile
, "write");
2281 last_char_was_cr
= 0;
2283 if (write (fd
, cp
, 1) < 0) {
2284 advise (tempfile
, "write");
2286 last_char_was_cr
= 0;
2292 inform("unable to write temporary file %s, continuing...",
2293 stripped_content_file
);
2294 (void) m_unlink (stripped_content_file
);
2295 free(stripped_content_file
);
2298 /* Replace the decoded file with the converted one. */
2299 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2300 (void) m_unlink (ct
->c_cefile
.ce_file
);
2302 free(ct
->c_cefile
.ce_file
);
2303 ct
->c_cefile
.ce_file
= stripped_content_file
;
2304 ct
->c_cefile
.ce_unlink
= 1;
2308 report (NULL
, ct
->c_partno
,
2309 begin
== 0 && end
== 0 ? "" : *file
,
2315 if (opened_input_file
) {
2329 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2330 * of the part C-T-E's.
2333 update_cte (CT ct
) {
2334 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2336 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2337 least_restrictive_enc
!= CE_7BIT
) {
2338 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2342 /* Update/add Content-Transfer-Encoding header field. */
2343 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2344 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2351 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2358 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2362 least_restrictive_encoding (CT ct
) {
2363 int encoding
= CE_UNKNOWN
;
2365 switch (ct
->c_type
) {
2366 case CT_MULTIPART
: {
2367 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2370 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2371 const int part_encoding
=
2372 least_restrictive_encoding (part
->mp_part
);
2374 if (less_restrictive (encoding
, part_encoding
)) {
2375 encoding
= part_encoding
;
2382 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2383 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2384 const int part_encoding
=
2385 least_restrictive_encoding (e
->eb_content
);
2387 if (less_restrictive (encoding
, part_encoding
)) {
2388 encoding
= part_encoding
;
2394 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2395 encoding
= ct
->c_encoding
;
2404 * Return whether the second encoding is less restrictive than the first, where
2405 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2406 * CE_BINARY is less restrictive than CE_8BIT and
2407 * CE_8BIT is less restrictive than CE_7BIT.
2410 less_restrictive (int encoding
, int second_encoding
) {
2411 switch (second_encoding
) {
2413 return encoding
!= CE_BINARY
;
2415 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2417 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2418 encoding
!= CE_7BIT
;
2426 * Convert character set of each part.
2429 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2432 switch (ct
->c_type
) {
2434 if (ct
->c_subtype
== TEXT_PLAIN
) {
2435 status
= convert_charset (ct
, dest_charset
, message_mods
);
2438 char *ct_charset
= content_charset (ct
);
2440 report (NULL
, ct
->c_partno
, ct
->c_file
,
2441 "convert %s to %s", ct_charset
, dest_charset
);
2445 char *ct_charset
= content_charset (ct
);
2447 report ("iconv", ct
->c_partno
, ct
->c_file
,
2448 "failed to convert %s to %s", ct_charset
, dest_charset
);
2454 case CT_MULTIPART
: {
2455 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2458 /* Should check to see if the body for this part is encoded?
2459 For now, it gets passed along as-is by InitMultiPart(). */
2460 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2462 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2468 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2469 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2472 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2485 * Fix various problems that aren't handled elsewhere. These
2486 * are fixed unconditionally: there are no switches to disable
2487 * them. Currently, "problems" are these:
2488 * 1) remove extraneous semicolon at the end of a header parameter list
2489 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2490 * filename parameters in Content-Type and Content-Disposition
2491 * headers, respectively.
2494 fix_always (CT ct
, int *message_mods
) {
2497 switch (ct
->c_type
) {
2498 case CT_MULTIPART
: {
2499 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2502 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2503 status
= fix_always (part
->mp_part
, message_mods
);
2509 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2510 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2512 status
= fix_always (e
->eb_content
, message_mods
);
2519 if (ct
->c_first_hf
) {
2520 fix_filename_encoding (ct
);
2523 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2524 size_t len
= strlen (hf
->value
);
2526 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2527 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2528 /* Only do this for Content-Type and
2529 Content-Disposition fields because those are the
2530 only headers that parse_mime() warns about. */
2534 /* whitespace following a trailing ';' will be nuked as well */
2535 if (hf
->value
[len
- 1] == '\n') {
2536 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2537 if (len
-- == 0) { break; }
2541 if (hf
->value
[len
- 2] == ';') {
2542 /* Remove trailing ';' from parameter value. */
2543 hf
->value
[len
- 2] = '\n';
2544 hf
->value
[len
- 1] = '\0';
2546 /* Also, if Content-Type parameter, remove trailing ';'
2547 from ct->c_ctline. This probably isn't necessary
2549 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2550 size_t l
= strlen(ct
->c_ctline
) - 1;
2551 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2552 ct
->c_ctline
[l
] == ';') {
2553 ct
->c_ctline
[l
--] = '\0';
2554 if (l
== 0) { break; }
2560 report (NULL
, ct
->c_partno
, ct
->c_file
,
2561 "remove trailing ; from %s parameter value",
2573 * Factor out common code for loops in fix_filename_encoding().
2576 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
) {
2579 if (has_prefix(value
, "=?") && has_suffix(value
, "?=")) {
2580 /* Looks like an RFC 2047 encoded parameter. */
2581 char decoded
[PATH_MAX
+ 1];
2583 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2584 /* Encode using RFC 2231. */
2585 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2588 inform("failed to decode %s parameter %s", name
, value
);
2597 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2598 * filename parameters in Content-Type and Content-Disposition
2599 * headers, respectively.
2602 fix_filename_encoding (CT ct
) {
2607 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2608 if (pm
->pm_name
&& pm
->pm_value
&&
2609 strcasecmp (pm
->pm_name
, "name") == 0) {
2610 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2611 &ct
->c_ctinfo
.ci_first_pm
,
2612 &ct
->c_ctinfo
.ci_last_pm
);
2616 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2617 if (pm
->pm_name
&& pm
->pm_value
&&
2618 strcasecmp (pm
->pm_name
, "filename") == 0) {
2619 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2625 /* Fix hf values to correspond. */
2626 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2627 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2629 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2630 field
= TYPE_HEADER
;
2631 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2632 field
= DISPO_HEADER
;
2635 if (field
!= OTHER
) {
2636 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2638 if (semicolon_loc
) {
2640 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2641 const char *const params
=
2643 field
== TYPE_HEADER
2644 ? ct
->c_ctinfo
.ci_first_pm
2645 : ct
->c_dispo_first
,
2647 const char *const new_params
= concat (params
, "\n", NULL
);
2649 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2650 free((void *)new_params
); /* Cast away const. Sigh. */
2651 free((void *)params
);
2653 inform("did not find semicolon in %s:%s\n",
2654 hf
->name
, hf
->value
);
2664 * Output content in input file to output file.
2667 write_content (CT ct
, const char *input_filename
, char *outfile
, FILE *outfp
,
2668 int modify_inplace
, int message_mods
) {
2671 if (modify_inplace
) {
2672 if (message_mods
> 0) {
2673 if ((status
= output_message_fp (ct
, outfp
, outfile
)) == OK
) {
2674 char *infile
= input_filename
2675 ? mh_xstrdup (input_filename
)
2676 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2678 if (remove_file (infile
) == OK
) {
2679 if (rename (outfile
, infile
)) {
2680 /* Rename didn't work, possibly because of an
2681 attempt to rename across filesystems. Try
2682 brute force copy. */
2683 int old
= open (outfile
, O_RDONLY
);
2685 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2688 if (old
!= -1 && new != -1) {
2689 char buffer
[NMH_BUFSIZ
];
2691 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2693 if (write (new, buffer
, i
) != i
) {
2699 if (new != -1) { close (new); }
2700 if (old
!= -1) { close (old
); }
2701 (void) m_unlink (outfile
);
2704 /* The -file argument processing used path() to
2705 expand filename to absolute path. */
2706 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2708 inform("unable to rename %s %s to %s, continuing...",
2709 file
? "file" : "message", outfile
,
2715 inform("unable to remove input file %s, "
2716 "not modifying it, continuing...", infile
);
2717 (void) m_unlink (outfile
);
2726 /* No modifications and didn't need the tmp outfile. */
2727 (void) m_unlink (outfile
);
2730 /* Output is going to some file. Produce it whether or not
2731 there were modifications. */
2732 status
= output_message_fp (ct
, outfp
, outfile
);
2741 * parse_mime() does not set lf_line_endings in struct text, so use this
2742 * function to do it. It touches the parts the decodetypes identifies.
2745 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
) {
2746 switch (ct
->c_type
) {
2747 case CT_MULTIPART
: {
2748 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2751 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2752 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2758 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2759 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2761 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2766 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2767 if (ct
->c_ctparams
== NULL
) {
2768 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2770 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2777 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2778 * use the standard MH backup file.
2781 remove_file (const char *file
) {
2783 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2784 int status
= system (rmm_command
);
2787 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2789 /* This is OK for a non-message file, it still uses the
2790 BACKUP_PREFIX form. The backup file will be in the same
2791 directory as file. */
2792 return rename (file
, m_backup (file
));
2797 * Output formatted message to user.
2800 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2805 va_start (args
, message
);
2806 fmt
= concat (filename
, partno
? " part " : ", ",
2807 FENDNULL(partno
), partno
? ", " : "", message
, NULL
);
2809 advertise (what
, NULL
, fmt
, args
);
2822 fprintf (stderr
, "\n");