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_fp (CT
, FILE *, 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
*, FILE **, char *,
92 static int fix_boundary (CT
*, int *);
93 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
94 static int get_multipart_boundary (CT
, char **);
95 static int replace_boundary (CT
, char *, char *);
96 static int fix_types (CT
, svector_t
, int *);
97 static char *replace_substring (char **, const char *, const char *);
98 static char *remove_parameter (char *, const char *);
99 static int fix_composite_cte (CT
, int *);
100 static int set_ce (CT
, int);
101 static int ensure_text_plain (CT
*, CT
, int *, int);
102 static int find_textplain_sibling (CT
, int, int *);
103 static int insert_new_text_plain_part (CT
, int, CT
);
104 static CT
build_text_plain_part (CT
);
105 static int insert_into_new_mp_alt (CT
*, int *);
106 static CT
divide_part (CT
);
107 static void copy_ctinfo (CI
, CI
);
108 static int decode_part (CT
);
109 static int reformat_part (CT
, char *, char *, char *, int);
110 static CT
build_multipart_alt (CT
, CT
, int, int);
111 static int boundary_in_content (FILE **, char *, const char *);
112 static void transfer_noncontent_headers (CT
, CT
);
113 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
114 static int decode_text_parts (CT
, int, const char *, int *);
115 static int should_decode(const char *, const char *, const char *);
116 static int content_encoding (CT
, const char **);
117 static int strip_crs (CT
, int *);
118 static void update_cte (CT
);
119 static int least_restrictive_encoding (CT
);
120 static int less_restrictive (int, int);
121 static int convert_charsets (CT
, char *, int *);
122 static int fix_always (CT
, int *);
123 static int fix_filename_param (char *, char *, PM
*, PM
*);
124 static int fix_filename_encoding (CT
);
125 static int write_content (CT
, const char *, char *, FILE *, int, int);
126 static void set_text_ctparams(CT
, char *, int);
127 static int remove_file (const char *);
128 static void report (char *, char *, char *, char *, ...);
129 static void pipeser (int);
133 main (int argc
, char **argv
) {
135 char *cp
, *file
= NULL
, *folder
= NULL
;
136 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
137 char **argp
, **arguments
;
138 struct msgs_array msgs
= { 0, 0, NULL
};
139 struct msgs
*mp
= NULL
;
141 FILE *fp
, *infp
= NULL
, *outfp
= NULL
;
145 fix_transformations fx
;
146 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
148 fx
.replacetextplain
= 0;
149 fx
.decodetext
= CE_8BIT
;
150 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
151 fx
.lf_line_endings
= 0;
152 fx
.textcharset
= NULL
;
154 if (nmh_init(argv
[0], 2)) { return 1; }
156 arguments
= getarguments (invo_name
, argc
, argv
, 1);
162 while ((cp
= *argp
++)) {
164 switch (smatch (++cp
, switches
)) {
166 ambigsw (cp
, switches
);
169 adios (NULL
, "-%s unknown", cp
);
172 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
174 print_help (buf
, switches
, 1);
177 print_version(invo_name
);
181 if (! (cp
= *argp
++) || *cp
== '-') {
182 adios (NULL
, "missing argument to %s", argp
[-2]);
184 if (! strcasecmp (cp
, "8bit")) {
185 fx
.decodetext
= CE_8BIT
;
186 } else if (! strcasecmp (cp
, "7bit")) {
187 fx
.decodetext
= CE_7BIT
;
188 } else if (! strcasecmp (cp
, "binary")) {
189 fx
.decodetext
= CE_BINARY
;
191 adios (NULL
, "invalid argument to %s", argp
[-2]);
198 if (! (cp
= *argp
++) || *cp
== '-') {
199 adios (NULL
, "missing argument to %s", argp
[-2]);
203 case CRLFLINEBREAKSSW
:
204 fx
.lf_line_endings
= 0;
206 case NCRLFLINEBREAKSSW
:
207 fx
.lf_line_endings
= 1;
210 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
211 adios (NULL
, "missing argument to %s", argp
[-2]);
224 case FIXCOMPOSITECTESW
:
225 fx
.fixcompositecte
= 1;
227 case NFIXCOMPOSITECTESW
:
228 fx
.fixcompositecte
= 0;
231 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
232 adios (NULL
, "missing argument to %s", argp
[-2]);
234 if (! strncasecmp (cp
, "multipart/", 10) ||
235 ! strncasecmp (cp
, "message/", 8)) {
236 adios (NULL
, "-fixtype %s not allowed", cp
);
237 } else if (! strchr (cp
, '/')) {
238 adios (NULL
, "-fixtype requires type/subtype");
240 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
241 svector_push_back (fx
.fixtypes
, cp
);
249 case REPLACETEXTPLAINSW
:
250 fx
.replacetextplain
= 1;
252 case NREPLACETEXTPLAINSW
:
253 fx
.replacetextplain
= 0;
256 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
257 adios (NULL
, "missing argument to %s", argp
[-2]);
259 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
262 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
263 adios (NULL
, "missing argument to %s", argp
[-2]);
265 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
268 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
269 adios (NULL
, "missing argument to %s", argp
[-2]);
289 if (*cp
== '+' || *cp
== '@') {
291 adios (NULL
, "only one folder at a time!");
293 folder
= pluspath (cp
);
297 /* Interpret a full path as a filename, not a message. */
298 file
= mh_xstrdup (cp
);
300 app_msgarg (&msgs
, cp
);
305 SIGNAL (SIGQUIT
, quitser
);
306 SIGNAL (SIGPIPE
, pipeser
);
309 * Read the standard profile setup
311 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
312 readconfig ((struct node
**) 0, fp
, cp
, 0);
316 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
317 suppress_extraneous_trailing_semicolon_warning
= 1;
319 if (! context_find ("path")) {
320 free (path ("./", TFOLDER
));
323 if (file
&& msgs
.size
) {
324 adios (NULL
, "cannot specify msg and file at same time!");
328 /* Open the outfile now, so we don't have to risk opening it
329 after running out of fds. */
330 if (strcmp (outfile
, "-") == 0) {
332 } else if ((outfp
= fopen (outfile
, "w")) == NULL
) {
333 adios (outfile
, "unable to open for writing");
338 * check if message is coming from file
341 /* If file is stdin, create a tmp file name before parse_mime()
342 has a chance, because it might put in on a different
343 filesystem than the output file. Instead, put it in the
344 user's preferred tmp directory. */
347 if (! strcmp ("-", file
)) {
353 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
354 adios (NULL
, "unable to create temporary file in %s",
358 file
= mh_xstrdup (cp
);
359 cpydata (STDIN_FILENO
, fd
, "-", file
);
363 (void) m_unlink (file
);
364 adios (NULL
, "failed to write temporary file");
368 cts
= mh_xcalloc(2, sizeof *cts
);
371 if ((ct
= parse_mime (file
))) {
372 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
375 inform("unable to parse message from file %s", file
);
378 /* If there's an outfile, pass the input message unchanged, so the
379 message won't get dropped from a pipeline. */
381 /* Something went wrong. Output might be expected, such as if
382 this were run as a filter. Just copy the input to the
384 if ((infp
= fopen (file
, "r")) == NULL
) {
385 adios (file
, "unable to open for reading");
388 if (copy_input_to_output (file
, infp
, outfile
, outfp
) != OK
) {
389 inform("unable to copy message to %s, "
390 "it might be lost\n", outfile
);
399 * message(s) are coming from a folder
404 app_msgarg(&msgs
, "cur");
407 folder
= getfolder (1);
409 maildir
= mh_xstrdup(m_maildir (folder
));
411 /* chdir so that error messages, esp. from MIME parser, just
412 refer to the message and not its path. */
413 if (chdir (maildir
) == NOTOK
) {
414 adios (maildir
, "unable to change directory to");
417 /* read folder and create message structure */
418 if (! (mp
= folder_read (folder
, 1))) {
419 adios (NULL
, "unable to read folder %s", folder
);
422 /* check for empty folder */
423 if (mp
->nummsg
== 0) {
424 adios (NULL
, "no messages in %s", folder
);
427 /* parse all the message ranges/sequences and set SELECTED */
428 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
429 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
432 seq_setprev (mp
); /* set the previous-sequence */
434 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
437 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
438 if (is_selected(mp
, msgnum
)) {
439 char *msgnam
= m_name (msgnum
);
441 if ((ct
= parse_mime (msgnam
))) {
442 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
445 inform("unable to parse message %s", msgnam
);
448 /* If there's an outfile, pass the input message
449 unchanged, so the message won't get dropped from a
452 /* Something went wrong. Output might be expected,
453 such as if this were run as a filter. Just copy
454 the input to the output. */
455 /* Can't use path() here because 1) it might have been
456 called before and it caches the pwd, and 2) we call
457 chdir() after that. */
458 char *input_filename
=
459 concat (maildir
, "/", msgnam
, NULL
);
461 if ((infp
= fopen (input_filename
, "r")) == NULL
) {
462 adios (input_filename
,
463 "unable to open for reading");
466 if (copy_input_to_output (input_filename
, infp
,
467 outfile
, outfp
) != OK
) {
468 inform("unable to copy message to %s, "
469 "it might be lost\n", outfile
);
474 free (input_filename
);
481 seq_setcur (mp
, mp
->hghsel
); /* update current message */
483 seq_save (mp
); /* synchronize sequences */
484 context_replace (pfolder
, folder
);/* update current folder */
485 context_save (); /* save the context file */
489 for (ctp
= cts
; *ctp
; ++ctp
) {
490 status
+= mhfixmsgsbr (ctp
, maildir
, &fx
, &infp
, outfile
, &outfp
);
494 (void) m_unlink (file
);
497 /* Just calling m_backup() unlinks the backup file. */
498 (void) m_backup (file
);
509 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
510 if (infp
) { fclose (infp
); } /* even if stdin */
511 if (outfp
) { fclose (outfp
); } /* even if stdout */
523 * Apply transformations to one message.
526 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
527 FILE **infp
, char *outfile
, FILE **outfp
) {
528 /* Store input filename in case one of the transformations, i.e.,
529 fix_boundary(), rewrites to a tmp file. */
530 char *input_filename
= maildir
531 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
532 : mh_xstrdup ((*ctp
)->c_file
);
533 int modify_inplace
= 0;
534 int message_mods
= 0;
537 /* Though the input file won't need to be opened if everything goes
538 well, do it here just in case there's a failure, and that failure is
539 running out of file descriptors. */
540 if ((*infp
= fopen (input_filename
, "r")) == NULL
) {
541 adios (input_filename
, "unable to open for reading");
544 if (outfile
== NULL
) {
547 if ((*ctp
)->c_file
) {
549 /* outfp will be closed by the caller */
550 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, outfp
)) ==
552 adios (NULL
, "unable to create temporary file in %s",
555 outfile
= mh_xstrdup (tempfile
);
557 adios (NULL
, "missing both input and output filenames\n");
559 } /* else *outfp was defined by caller */
561 reverse_alternative_parts (*ctp
);
562 status
= fix_always (*ctp
, &message_mods
);
563 if (status
== OK
&& fx
->fixboundary
) {
564 status
= fix_boundary (ctp
, &message_mods
);
566 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
567 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
569 if (status
== OK
&& fx
->fixcompositecte
) {
570 status
= fix_composite_cte (*ctp
, &message_mods
);
572 if (status
== OK
&& fx
->reformat
) {
574 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
576 if (status
== OK
&& fx
->decodetext
) {
577 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
581 if (status
== OK
&& fx
->textcharset
!= NULL
) {
582 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
585 if (status
== OK
&& ! (*ctp
)->c_umask
) {
586 /* Set the umask for the contents file. This currently
587 isn't used but just in case it is in the future. */
590 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
591 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
593 (*ctp
)->c_umask
= ~m_gmprot();
598 * Write the content to a file
601 status
= write_content (*ctp
, input_filename
, outfile
, *outfp
,
602 modify_inplace
, message_mods
);
603 } else if (! modify_inplace
) {
604 /* Something went wrong. Output might be expected, such
605 as if this were run as a filter. Just copy the input
607 if (copy_input_to_output (input_filename
, *infp
, outfile
,
609 inform("unable to copy message to %s, it might be lost\n",
614 if (modify_inplace
) {
615 if (status
!= OK
) { (void) m_unlink (outfile
); }
622 free (input_filename
);
629 * Copy input message to output. Assumes not modifying in place, so this
630 * might be running as part of a pipeline.
633 copy_input_to_output (const char *input_filename
, FILE *infp
,
634 const char *output_filename
, FILE *outfp
) {
635 int in
= fileno (infp
);
636 int out
= fileno (outfp
);
639 if (in
!= -1 && out
!= -1) {
640 cpydata (in
, out
, input_filename
, output_filename
);
650 * Fix mismatched outer level boundary.
653 fix_boundary (CT
*ct
, int *message_mods
) {
654 struct multipart
*mp
;
657 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
658 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
661 * 1) Get boundary at end of part.
662 * 2) Get boundary at beginning of part and compare to the end-of-part
664 * 3) Write out contents of ct to tmp file, replacing boundary in
665 * header with boundary from part. Set c_unlink to 1.
667 * 5) Call parse_mime() on the tmp file, replacing ct.
670 if (mp
&& mp
->mp_start
) {
673 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
676 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
677 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
678 char *filename
= mh_xstrdup ((*ct
)->c_file
);
682 if ((fixed_ct
= parse_mime (fixed
))) {
688 report (NULL
, NULL
, filename
,
689 "fix multipart boundary");
693 inform("unable to parse fixed part");
698 inform("unable to replace broken boundary");
702 inform("unable to create temporary file in %s",
707 free (part_boundary
);
709 /* Couldn't fix the boundary. Report failure so that mhfixmsg
710 doesn't modify the message. */
714 /* No multipart struct, even though the content type is
715 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
726 * Find boundary at end of multipart.
729 get_multipart_boundary (CT ct
, char **part_boundary
) {
730 char buffer
[NMH_BUFSIZ
];
731 char *end_boundary
= NULL
;
732 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
733 ? (off_t
) (ct
->c_end
- sizeof buffer
)
734 : (off_t
) ct
->c_begin
;
738 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
739 be big enough, even if it's just 1024, to make that unlikely. */
741 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
742 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
743 advise (ct
->c_file
, "unable to open for reading");
747 /* Get boundary at end of multipart. */
748 while (begin
>= (off_t
) ct
->c_begin
) {
749 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
750 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
751 char *cp
= rfind_str (buffer
, bytes_read
, "--");
756 /* Trim off trailing "--" and anything beyond. */
758 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
759 if (strlen (end
) > 3 && *end
++ == '\n' &&
760 *end
++ == '-' && *end
++ == '-') {
761 end_boundary
= mh_xstrdup (end
);
768 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
769 begin
-= sizeof buffer
;
775 /* Get boundary at beginning of multipart. */
777 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
778 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
779 if (bytes_read
>= strlen (end_boundary
)) {
780 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
782 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
783 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
788 /* The start and end boundaries didn't match, or the
789 start boundary doesn't begin with "\n--" (or "--"
790 if at the beginning of buffer). Keep trying. */
804 *part_boundary
= end_boundary
;
806 *part_boundary
= NULL
;
815 * Open and copy ct->c_file to file, replacing the multipart boundary.
818 replace_boundary (CT ct
, char *file
, char *boundary
) {
821 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
823 m_getfld_state_t gstate
= 0;
826 if (ct
->c_file
== NULL
) {
827 inform("missing input filename");
831 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
832 advise (ct
->c_file
, "unable to open for reading");
836 if ((fpout
= fopen (file
, "w")) == NULL
) {
838 advise (file
, "unable to open for writing");
842 for (compnum
= 1;;) {
843 int bufsz
= (int) sizeof buf
;
845 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
850 /* get copies of the buffers */
851 np
= mh_xstrdup (name
);
852 vp
= mh_xstrdup (buf
);
854 /* if necessary, get rest of field */
855 while (state
== FLDPLUS
) {
857 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
858 vp
= add (buf
, vp
); /* add to previous value */
861 if (strcasecmp (TYPE_FIELD
, np
)) {
862 fprintf (fpout
, "%s:%s", np
, vp
);
864 char *new_ctline
, *new_params
;
866 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
867 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
870 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
871 ct
->c_ctinfo
.ci_subtype
, NULL
);
872 new_params
= output_params(strlen(TYPE_FIELD
) +
873 strlen(new_ctline
) + 1,
874 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
875 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
876 new_params
? new_params
: "");
878 mh_xfree(new_params
);
888 /* buf will have a terminating NULL, skip it. */
889 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
890 advise (file
, "fwrite");
899 inform("message format error in component #%d", compnum
);
904 inform("getfld() returned %d", state
);
912 m_getfld_state_destroy (&gstate
);
921 * Fix Content-Type header to reflect the content of its part.
924 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
927 switch (ct
->c_type
) {
929 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
932 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
933 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
939 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
940 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
942 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
949 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
950 for (typep
= svector_strs (fixtypes
);
951 typep
&& (type
= *typep
);
954 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
957 if (! strcasecmp (type
, type_subtype
) &&
958 decode_part (ct
) == OK
&&
959 ct
->c_cefile
.ce_file
!= NULL
) {
960 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
963 if ((cp
= strchr (ct_type_subtype
, ';'))) {
964 /* Truncate to remove any parameter list from
965 mime_type () result. */
969 if (strcasecmp (type
, ct_type_subtype
)) {
970 char *ct_type
, *ct_subtype
;
973 /* The Content-Type header does not match the
974 content, so update these struct Content
977 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
980 /* Extract type and subtype from type/subtype. */
981 ct_type
= mh_xstrdup(ct_type_subtype
);
982 if ((cp
= strchr (ct_type
, '/'))) {
984 ct_subtype
= mh_xstrdup(++cp
);
986 inform("missing / in MIME type of %s %s",
987 ct
->c_file
, ct
->c_partno
);
992 ct
->c_type
= ct_str_type (ct_type
);
993 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
995 free (ct
->c_ctinfo
.ci_type
);
996 ct
->c_ctinfo
.ci_type
= ct_type
;
997 free (ct
->c_ctinfo
.ci_subtype
);
998 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
999 if (! replace_substring (&ct
->c_ctline
, type
,
1001 inform("did not find %s in %s",
1002 type
, ct
->c_ctline
);
1005 /* Update Content-Type header field. */
1006 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1007 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1008 if (replace_substring (&hf
->value
, type
,
1012 report (NULL
, ct
->c_partno
, ct
->c_file
,
1013 "change Content-Type in header "
1015 type
, ct_type_subtype
);
1019 inform("did not find %s in %s",
1025 free (ct_type_subtype
);
1027 free (type_subtype
);
1037 * Replace a substring, allocating space to hold the new one.
1040 replace_substring (char **str
, const char *old
, const char *new) {
1043 if ((cp
= strstr (*str
, old
))) {
1044 char *remainder
= cp
+ strlen (old
);
1045 char *prefix
, *new_str
;
1048 prefix
= mh_xstrdup(*str
);
1049 *(prefix
+ (cp
- *str
)) = '\0';
1050 new_str
= concat (prefix
, new, remainder
, NULL
);
1053 new_str
= concat (new, remainder
, NULL
);
1058 return *str
= new_str
;
1066 * Remove a name=value parameter, given just its name, from a header value.
1069 remove_parameter (char *str
, const char *name
) {
1070 /* It looks to me, based on the BNF in RFC 2045, than there can't
1071 be whitespace betwwen the parameter name and the "=", or
1072 between the "=" and the parameter value. */
1073 char *param_name
= concat (name
, "=", NULL
);
1076 if ((cp
= strstr (str
, param_name
))) {
1080 /* Remove any leading spaces, before the parameter name. */
1082 start
> str
&& isspace ((unsigned char) *(start
-1));
1086 /* Remove a leading semicolon. */
1087 if (start
> str
&& *(start
-1) == ';') { --start
; }
1089 end
= cp
+ strlen (name
) + 1;
1091 /* Skip past the quoted value, and then the final quote. */
1092 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1095 /* Skip past the value. */
1096 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1099 /* Count how many characters need to be moved. Include
1100 trailing null, which is accounted for by the
1101 initialization of count to 1. */
1102 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1103 (void) memmove (start
, end
, count
);
1113 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1114 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1118 fix_composite_cte (CT ct
, int *message_mods
) {
1121 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1122 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1123 ct
->c_encoding
!= CE_BINARY
) {
1126 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1127 char *name
= hf
->name
;
1128 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
1132 if (! strncasecmp (name
, ENCODING_FIELD
,
1133 strlen (ENCODING_FIELD
))) {
1134 char *prefix
= "Nmh-REPLACED-INVALID-";
1138 h
->name
= mh_xstrdup (hf
->name
);
1139 h
->hf_encoding
= hf
->hf_encoding
;
1143 /* Retain old header but prefix its name. */
1145 hf
->name
= concat (prefix
, h
->name
, NULL
);
1149 char *encoding
= cpytrim (hf
->value
);
1150 report (NULL
, ct
->c_partno
, ct
->c_file
,
1151 "replace Content-Transfer-Encoding of %s "
1152 "with 8 bit", encoding
);
1156 h
->value
= mh_xstrdup (" 8bit\n");
1158 /* Don't need to warn for multiple C-T-E header
1159 fields, parse_mime() already does that. But
1160 if there are any, fix them all as necessary. */
1165 set_ce (ct
, CE_8BIT
);
1168 if (ct
->c_type
== CT_MULTIPART
) {
1169 struct multipart
*m
;
1172 m
= (struct multipart
*) ct
->c_ctparams
;
1173 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1174 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1187 * Set content encoding.
1190 set_ce (CT ct
, int encoding
) {
1191 const char *ce
= ce_str (encoding
);
1192 const struct str2init
*ctinit
= get_ce_method (ce
);
1195 char *cte
= concat (" ", ce
, "\n", NULL
);
1198 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1199 caller is decode_text_parts (). Save because we'll
1201 struct cefile decoded_content_info
= ct
->c_cefile
;
1203 ct
->c_encoding
= encoding
;
1205 ct
->c_ctinitfnx
= ctinit
->si_init
;
1206 /* This will assign ct->c_cefile with an all-0 struct, which
1208 (*ctinit
->si_init
) (ct
);
1209 /* After returning, the caller should set
1210 ct->c_cefile.ce_file to the name of the file containing
1213 if (ct
->c_ceclosefnx
) {
1214 (*ct
->c_ceclosefnx
) (ct
);
1217 /* Restore the cefile. */
1218 ct
->c_cefile
= decoded_content_info
;
1220 /* Update/add Content-Transfer-Encoding header field. */
1221 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1222 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1229 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1232 /* Update c_celine. It's used only by mhlist -debug. */
1233 free (ct
->c_celine
);
1234 ct
->c_celine
= mh_xstrdup (cte
);
1244 * Make sure each text part has a corresponding text/plain part.
1247 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1250 switch ((*ct
)->c_type
) {
1252 /* Nothing to do for text/plain. */
1253 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1255 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1256 parent
->c_subtype
== MULTI_ALTERNATE
) {
1257 int new_subpart_number
= 1;
1258 int has_text_plain
=
1259 find_textplain_sibling (parent
, replacetextplain
,
1260 &new_subpart_number
);
1262 if (! has_text_plain
) {
1263 /* Parent is a multipart/alternative. Insert a new
1264 text/plain subpart. */
1265 const int inserted
=
1266 insert_new_text_plain_part (*ct
, new_subpart_number
,
1271 report (NULL
, parent
->c_partno
, parent
->c_file
,
1272 "insert text/plain part");
1278 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1279 parent
->c_subtype
== MULTI_RELATED
) {
1280 char *type_subtype
=
1281 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1282 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1283 const char *parent_type
=
1284 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1285 int new_subpart_number
= 1;
1286 int has_text_plain
= 0;
1288 /* Have to do string comparison on the subtype because we
1289 don't enumerate all of them in c_subtype values.
1290 parent_type will be NULL if the multipart/related part
1291 doesn't have a type parameter. The type parameter must
1292 be specified according to RFC 2387 Sec. 3.1 but not all
1294 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1295 /* The type of this part matches the root type of the
1296 parent multipart/related. Look to see if there's
1297 text/plain sibling. */
1299 find_textplain_sibling (parent
, replacetextplain
,
1300 &new_subpart_number
);
1303 free (type_subtype
);
1305 if (! has_text_plain
) {
1306 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1310 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1311 if (*ct
!= part
->mp_part
) {
1317 /* Parent is a multipart/related. Insert a new
1318 text/plain subpart in a new multipart/alternative. */
1319 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1320 /* Not an error if text/plain couldn't be added. */
1323 /* There are no siblings, so insert a new text/plain
1324 subpart, and change the parent type from
1325 multipart/related to multipart/alternative. */
1326 const int inserted
=
1327 insert_new_text_plain_part (*ct
, new_subpart_number
,
1333 parent
->c_subtype
= MULTI_ALTERNATE
;
1334 free (parent
->c_ctinfo
.ci_subtype
);
1335 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1336 if (! replace_substring (&parent
->c_ctline
, "/related",
1338 inform("did not find multipart/related in %s",
1342 /* Update Content-Type header field. */
1343 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1344 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1345 if (replace_substring (&hf
->value
, "/related",
1349 report (NULL
, parent
->c_partno
,
1351 "insert text/plain part");
1354 /* Remove, e.g., type="text/html" from
1355 multipart/alternative. */
1356 remove_parameter (hf
->value
, "type");
1359 inform("did not find multipart/"
1360 "related in header %s",
1366 /* Not an error if text/plain couldn't be inserted. */
1371 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1378 case CT_MULTIPART
: {
1379 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1382 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1383 if ((*ct
)->c_type
== CT_MULTIPART
) {
1384 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1392 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1393 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1395 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1406 * See if there is a sibling text/plain, and return its subpart number.
1409 find_textplain_sibling (CT parent
, int replacetextplain
,
1410 int *new_subpart_number
) {
1411 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1412 struct part
*part
, *prev
;
1413 int has_text_plain
= 0;
1415 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1416 ++*new_subpart_number
;
1417 if (part
->mp_part
->c_type
== CT_TEXT
&&
1418 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1419 if (replacetextplain
) {
1420 struct part
*old_part
;
1421 if (part
== mp
->mp_parts
) {
1422 old_part
= mp
->mp_parts
;
1423 mp
->mp_parts
= part
->mp_next
;
1425 old_part
= prev
->mp_next
;
1426 prev
->mp_next
= part
->mp_next
;
1429 report (NULL
, parent
->c_partno
, parent
->c_file
,
1430 "remove text/plain part %s",
1431 old_part
->mp_part
->c_partno
);
1433 free_content (old_part
->mp_part
);
1443 return has_text_plain
;
1448 * Insert a new text/plain part.
1451 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1452 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1453 struct part
*new_part
;
1456 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1458 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1460 new_part
->mp_next
= mp
->mp_parts
;
1461 mp
->mp_parts
= new_part
;
1462 new_part
->mp_part
->c_partno
=
1463 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1469 free_content (new_part
->mp_part
);
1477 * Create a text/plain part to go along with non-plain sibling part.
1480 build_text_plain_part (CT encoded_part
) {
1481 CT tp_part
= divide_part (encoded_part
);
1482 char *tmp_plain_file
= NULL
;
1484 if (decode_part (tp_part
) == OK
) {
1485 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1486 contains the decoded contents. And the decoding function, such
1487 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1488 be unlinked by free_content (). */
1491 /* This m_mktemp2() call closes the temp file. */
1492 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1493 inform("unable to create temporary file in %s",
1496 tmp_plain_file
= mh_xstrdup (tempfile
);
1497 if (reformat_part (tp_part
, tmp_plain_file
,
1498 tp_part
->c_ctinfo
.ci_type
,
1499 tp_part
->c_ctinfo
.ci_subtype
,
1500 tp_part
->c_type
) == OK
) {
1506 free_content (tp_part
);
1507 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1508 free (tmp_plain_file
);
1515 * Slip new text/plain part into a new multipart/alternative.
1518 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1519 CT tp_part
= build_text_plain_part (*ct
);
1523 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1526 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1528 if (mp
&& mp
->mp_parts
) {
1529 mp
->mp_parts
->mp_part
= tp_part
;
1530 /* Make the new multipart/alternative the parent. */
1535 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1536 "insert text/plain part");
1539 free_content (tp_part
);
1540 free_content (mp_alt
);
1547 /* Not an error if text/plain couldn't be built. */
1555 * Clone a MIME part.
1558 divide_part (CT ct
) {
1562 /* Just copy over what is needed for decoding. c_vrsn and
1563 c_celine aren't necessary. */
1564 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1565 new_part
->c_begin
= ct
->c_begin
;
1566 new_part
->c_end
= ct
->c_end
;
1567 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1568 new_part
->c_type
= ct
->c_type
;
1569 new_part
->c_cefile
= ct
->c_cefile
;
1570 new_part
->c_encoding
= ct
->c_encoding
;
1571 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1572 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1573 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1574 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1576 /* c_ctline is used by reformat__part(), so it can preserve
1577 anything after the type/subtype. */
1578 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1585 * Copy the content info from one part to another.
1588 copy_ctinfo (CI dest
, CI src
) {
1591 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1592 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1594 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1595 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1597 if (s_pm
->pm_charset
) {
1598 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1600 if (s_pm
->pm_lang
) {
1601 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1605 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1606 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1614 decode_part (CT ct
) {
1620 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, &file
)) == NULL
) {
1621 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1623 tmp_decoded
= mh_xstrdup (tempfile
);
1624 /* The following call will load ct->c_cefile.ce_file with the tmp
1625 filename of the decoded content. tmp_decoded will contain the
1626 encoded output, get rid of that. */
1627 status
= output_message_fp (ct
, file
, tmp_decoded
);
1628 (void) m_unlink (tmp_decoded
);
1630 if (fclose (file
)) {
1631 inform("unable to close temporary file %s, continuing...", tempfile
);
1639 * Reformat content as plain text.
1640 * Some of the arguments aren't really needed now, but maybe will
1641 * be in the future for other than text types.
1644 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1645 int output_subtype
, output_encoding
;
1646 const char *reason
= NULL
;
1650 /* Hacky: this redirects the output from whatever command is used
1651 to show the part to a file. So, the user can't have any output
1652 redirection in that command.
1653 Could show_multi() in mhshowsbr.c avoid this? */
1655 /* Check for invo_name-format-type/subtype. */
1656 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1658 inform("Don't know how to convert %s, there is no "
1659 "%s-format-%s/%s profile entry",
1660 ct
->c_file
, invo_name
, type
, subtype
);
1664 if (strchr (cf
, '>')) {
1665 inform("'>' prohibited in \"%s\",\nplease fix your "
1666 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1667 subtype
? subtype
: "");
1672 cp
= concat (cf
, " >", file
, NULL
);
1673 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1676 /* Unlink decoded content tmp file and free its filename to avoid
1677 leaks. The file stream should already have been closed. */
1678 if (ct
->c_cefile
.ce_unlink
) {
1679 (void) m_unlink (ct
->c_cefile
.ce_file
);
1680 free (ct
->c_cefile
.ce_file
);
1681 ct
->c_cefile
.ce_file
= NULL
;
1682 ct
->c_cefile
.ce_unlink
= 0;
1685 if (c_type
== CT_TEXT
) {
1686 output_subtype
= TEXT_PLAIN
;
1688 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1692 output_encoding
= content_encoding (ct
, &reason
);
1694 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1695 ct
->c_cefile
.ce_file
= file
;
1696 ct
->c_cefile
.ce_unlink
= 1;
1698 ct
->c_cefile
.ce_unlink
= 0;
1707 * Fill in a multipart/alternative part.
1710 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1711 char *boundary_prefix
= "----=_nmh-multipart";
1712 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1713 char *boundary_indicator
= "; boundary=";
1714 char *typename
, *subtypename
, *name
;
1717 struct multipart
*m
;
1718 const struct str2init
*ctinit
;
1722 /* Set up the multipart/alternative part. These fields of *ct were
1723 initialized to 0 by mh_xcalloc():
1724 c_fp, c_unlink, c_begin, c_end,
1725 c_vrsn, c_ctline, c_celine,
1726 c_id, c_descr, c_dispo, c_partno,
1727 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1728 c_cefile, c_encoding,
1729 c_digested, c_digest[16], c_ctexbody,
1730 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1732 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1735 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1737 ct
->c_subtype
= subtype
;
1739 ctinit
= get_ct_init (ct
->c_type
);
1741 typename
= ct_type_str (type
);
1742 subtypename
= ct_subtype_str (type
, subtype
);
1746 int found_boundary
= 1;
1748 while (found_boundary
&& serial
< 1000000) {
1751 /* Ensure that the boundary doesn't appear in the decoded
1753 if (new_part
->c_cefile
.ce_file
) {
1754 if ((found_boundary
=
1755 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1756 new_part
->c_cefile
.ce_file
,
1757 boundary
)) == NOTOK
) {
1763 /* Ensure that the boundary doesn't appear in the encoded
1765 if (! found_boundary
&& new_part
->c_file
) {
1766 if ((found_boundary
=
1767 boundary_in_content (&new_part
->c_fp
,
1769 boundary
)) == NOTOK
) {
1775 if (found_boundary
) {
1776 /* Try a slightly different boundary. */
1781 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1783 concat (boundary_prefix
,
1784 first_alt
->c_partno
? first_alt
->c_partno
: "",
1785 "-", buffer2
, NULL
);
1789 if (found_boundary
) {
1790 inform("giving up trying to find a unique boundary");
1796 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1797 boundary
, "\"", NULL
);
1799 /* Load c_first_hf and c_last_hf. */
1800 transfer_noncontent_headers (first_alt
, ct
);
1801 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1804 /* Load c_partno. */
1805 if (first_alt
->c_partno
) {
1806 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1807 free (first_alt
->c_partno
);
1808 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1809 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1811 first_alt
->c_partno
= mh_xstrdup ("1");
1812 new_part
->c_partno
= mh_xstrdup ("2");
1816 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1817 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1820 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1821 "boundary", boundary
, 0);
1825 p
->mp_next
->mp_next
= NULL
;
1826 p
->mp_next
->mp_part
= first_alt
;
1829 m
->mp_start
= concat (boundary
, "\n", NULL
);
1830 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1841 * Check that the boundary does not appear in the content.
1844 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1845 char buffer
[NMH_BUFSIZ
];
1847 int found_boundary
= 0;
1849 /* free_content() will close *fp if we fopen it here. */
1850 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1851 advise (file
, "unable to open %s for reading", file
);
1855 fseeko (*fp
, 0L, SEEK_SET
);
1856 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1857 if (find_str (buffer
, bytes_read
, boundary
)) {
1863 return found_boundary
;
1868 * Remove all non-Content headers.
1871 transfer_noncontent_headers (CT old
, CT
new) {
1874 hp_prev
= hp
= old
->c_first_hf
;
1878 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1879 if (hp
== old
->c_last_hf
) {
1880 if (hp
== old
->c_first_hf
) {
1881 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1883 hp_prev
->next
= NULL
;
1884 old
->c_last_hf
= hp_prev
;
1887 if (hp
== old
->c_first_hf
) {
1888 old
->c_first_hf
= next
;
1890 hp_prev
->next
= next
;
1894 /* Put node hp in the new CT. */
1895 if (new->c_first_hf
== NULL
) {
1896 new->c_first_hf
= hp
;
1898 new->c_last_hf
->next
= hp
;
1900 new->c_last_hf
= hp
;
1902 /* A Content- header, leave in old. */
1915 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1916 char *typename
= ct_type_str (type
);
1917 char *subtypename
= ct_subtype_str (type
, subtype
);
1918 /* E.g, " text/plain" */
1919 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1920 /* E.g, " text/plain\n" */
1921 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1922 int found_content_type
= 0;
1924 const char *cp
= NULL
;
1928 /* Update/add Content-Type header field. */
1929 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1930 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1931 found_content_type
= 1;
1933 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1934 ? concat (type_subtypename
, cp
, "\n", NULL
)
1935 : mh_xstrdup (name_plus_nl
);
1938 if (! found_content_type
) {
1939 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
1940 (cp
= strchr (ct
->c_ctline
, ';'))
1941 ? concat (type_subtypename
, cp
, "\n", NULL
)
1942 : mh_xstrdup (name_plus_nl
));
1945 /* Some of these might not be used, but set them anyway. */
1947 ? concat (type_subtypename
, cp
, NULL
)
1948 : concat (type_subtypename
, NULL
);
1949 free (ct
->c_ctline
);
1950 ct
->c_ctline
= ctline
;
1951 /* Leave other ctinfo members as they were. */
1952 free (ct
->c_ctinfo
.ci_type
);
1953 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1954 free (ct
->c_ctinfo
.ci_subtype
);
1955 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1957 ct
->c_subtype
= subtype
;
1959 free (name_plus_nl
);
1960 free (type_subtypename
);
1962 status
= set_ce (ct
, encoding
);
1969 * It's not necessary to update the charset parameter of a Content-Type
1970 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1971 * (content) was originally in the specified charset, "and will be in
1972 * that character set again after decoding."
1975 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
1976 int *message_mods
) {
1978 int lf_line_endings
= 0;
1980 switch (ct
->c_type
) {
1981 case CT_MULTIPART
: {
1982 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1985 /* Should check to see if the body for this part is encoded?
1986 For now, it gets passed along as-is by InitMultiPart(). */
1987 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1988 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
1995 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1996 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1998 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
2004 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2009 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
2011 switch (ct
->c_encoding
) {
2016 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
2017 const char *reason
= NULL
;
2019 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
2020 && encoding
!= CE_BINARY
) {
2021 /* The decoding isn't acceptable so discard it.
2022 Leave status as OK to allow other transformations. */
2024 report (NULL
, ct
->c_partno
, ct
->c_file
,
2025 "will not decode%s because it is binary (%s)",
2027 : ct
->c_ctline
? ct
->c_ctline
2031 (void) m_unlink (ct
->c_cefile
.ce_file
);
2032 free (ct
->c_cefile
.ce_file
);
2033 ct
->c_cefile
.ce_file
= NULL
;
2034 } else if (ct
->c_encoding
== CE_QUOTED
&&
2035 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2036 /* The decoding isn't acceptable so discard it.
2037 Leave status as OK to allow other transformations. */
2039 report (NULL
, ct
->c_partno
, ct
->c_file
,
2040 "will not decode%s because it is 8bit",
2042 : ct
->c_ctline
? ct
->c_ctline
2045 (void) m_unlink (ct
->c_cefile
.ce_file
);
2046 free (ct
->c_cefile
.ce_file
);
2047 ct
->c_cefile
.ce_file
= NULL
;
2051 if (ct_encoding
== CE_BINARY
) {
2053 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2058 if (set_ce (ct
, enc
) == OK
) {
2061 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2062 ct
->c_ctline
? ct
->c_ctline
: "");
2064 if (lf_line_endings
) {
2065 strip_crs (ct
, message_mods
);
2078 if (lf_line_endings
) {
2079 strip_crs (ct
, message_mods
);
2094 * Determine if the part with type[/subtype] should be decoded, according to
2095 * decodetypes (which came from the -decodetypes switch).
2098 should_decode(const char *decodetypes
, const char *type
, const char *subtype
) {
2099 /* Quick search for matching type[/subtype] in decodetypes: bracket
2100 decodetypes with commas, then search for ,type, and ,type/subtype, in
2103 int found_match
= 0;
2104 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2105 char *delimited_type
= concat(",", type
, ",", NULL
);
2107 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2109 } else if (subtype
!= NULL
) {
2110 char *delimited_type_subtype
=
2111 concat(",", type
, "/", subtype
, ",", NULL
);
2113 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2116 free(delimited_type_subtype
);
2119 free(delimited_type
);
2120 free(delimited_decodetypes
);
2127 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2128 * if it has any NUL characters, a CR not followed by a LF, or lines
2129 * greater than 998 characters in length. If binary, reason is set
2130 * to a string explaining why.
2133 content_encoding (CT ct
, const char **reason
) {
2134 CE ce
= &ct
->c_cefile
;
2135 int encoding
= CE_7BIT
;
2138 size_t line_len
= 0;
2139 char buffer
[NMH_BUFSIZ
];
2142 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2143 advise (ce
->ce_file
, "unable to open for reading");
2147 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2148 while (encoding
!= CE_BINARY
&&
2149 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2152 int last_char_was_cr
= 0;
2154 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2155 if (*cp
== '\0' || ++line_len
> 998 ||
2156 (*cp
!= '\n' && last_char_was_cr
)) {
2157 encoding
= CE_BINARY
;
2159 *reason
= "null character";
2160 } else if (line_len
> 998) {
2161 *reason
= "line length > 998";
2162 } else if (*cp
!= '\n' && last_char_was_cr
) {
2163 *reason
= "CR not followed by LF";
2165 /* Should not reach this. */
2169 } else if (*cp
== '\n') {
2171 } else if (! isascii ((unsigned char) *cp
)) {
2175 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2181 } /* else should never happen */
2188 * Strip carriage returns from content.
2191 strip_crs (CT ct
, int *message_mods
) {
2192 char *charset
= content_charset (ct
);
2195 /* Only strip carriage returns if content is ASCII or another
2196 charset that has the same readily recognizable CR followed by a
2197 LF. We can include UTF-8 here because if the high-order bit of
2198 a UTF-8 byte is 0, then it must be a single-byte ASCII
2200 if (! strcasecmp (charset
, "US-ASCII") ||
2201 ! strcasecmp (charset
, "UTF-8") ||
2202 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2203 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2209 int opened_input_file
= 0;
2211 if (ct
->c_cefile
.ce_file
) {
2212 file
= &ct
->c_cefile
.ce_file
;
2213 fp
= &ct
->c_cefile
.ce_fp
;
2215 } else if (ct
->c_file
) {
2218 begin
= (size_t) ct
->c_begin
;
2219 end
= (size_t) ct
->c_end
;
2220 } /* else don't know where the content is */
2222 if (file
&& *file
&& fp
) {
2224 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2225 advise (*file
, "unable to open for reading");
2228 opened_input_file
= 1;
2234 char buffer
[NMH_BUFSIZ
];
2236 size_t bytes_to_read
=
2237 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2239 fseeko (*fp
, begin
, SEEK_SET
);
2240 while ((bytes_read
= fread (buffer
, 1,
2241 min (bytes_to_read
, sizeof buffer
),
2243 /* Look for CR followed by a LF. This is supposed to
2244 be text so there should be LF's. If not, don't
2245 modify the content. */
2248 int last_char_was_cr
= 0;
2250 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2252 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2253 if (*cp
== '\n' && last_char_was_cr
) {
2258 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2264 char *stripped_content_file
;
2265 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2267 if (tempfile
== NULL
) {
2268 adios (NULL
, "unable to create temporary file in %s",
2271 stripped_content_file
= mh_xstrdup (tempfile
);
2273 /* Strip each CR before a LF from the content. */
2274 fseeko (*fp
, begin
, SEEK_SET
);
2275 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2279 int last_char_was_cr
= 0;
2281 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2283 last_char_was_cr
= 1;
2284 } else if (last_char_was_cr
) {
2286 if (write (fd
, "\r", 1) < 0) {
2287 advise (tempfile
, "CR write");
2290 if (write (fd
, cp
, 1) < 0) {
2291 advise (tempfile
, "write");
2293 last_char_was_cr
= 0;
2295 if (write (fd
, cp
, 1) < 0) {
2296 advise (tempfile
, "write");
2298 last_char_was_cr
= 0;
2304 inform("unable to write temporary file %s, continuing...",
2305 stripped_content_file
);
2306 (void) m_unlink (stripped_content_file
);
2309 /* Replace the decoded file with the converted one. */
2310 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2311 (void) m_unlink (ct
->c_cefile
.ce_file
);
2313 mh_xfree(ct
->c_cefile
.ce_file
);
2314 ct
->c_cefile
.ce_file
= stripped_content_file
;
2315 ct
->c_cefile
.ce_unlink
= 1;
2319 report (NULL
, ct
->c_partno
,
2320 begin
== 0 && end
== 0 ? "" : *file
,
2326 if (opened_input_file
) {
2340 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2341 * of the part C-T-E's.
2344 update_cte (CT ct
) {
2345 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2347 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2348 least_restrictive_enc
!= CE_7BIT
) {
2349 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2353 /* Update/add Content-Transfer-Encoding header field. */
2354 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2355 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2362 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2369 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2373 least_restrictive_encoding (CT ct
) {
2374 int encoding
= CE_UNKNOWN
;
2376 switch (ct
->c_type
) {
2377 case CT_MULTIPART
: {
2378 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2381 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2382 const int part_encoding
=
2383 least_restrictive_encoding (part
->mp_part
);
2385 if (less_restrictive (encoding
, part_encoding
)) {
2386 encoding
= part_encoding
;
2393 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2394 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2395 const int part_encoding
=
2396 least_restrictive_encoding (e
->eb_content
);
2398 if (less_restrictive (encoding
, part_encoding
)) {
2399 encoding
= part_encoding
;
2405 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2406 encoding
= ct
->c_encoding
;
2415 * Return whether the second encoding is less restrictive than the first, where
2416 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2417 * CE_BINARY is less restrictive than CE_8BIT and
2418 * CE_8BIT is less restrictive than CE_7BIT.
2421 less_restrictive (int encoding
, int second_encoding
) {
2422 switch (second_encoding
) {
2424 return encoding
!= CE_BINARY
;
2426 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2428 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2429 encoding
!= CE_7BIT
;
2437 * Convert character set of each part.
2440 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2443 switch (ct
->c_type
) {
2445 if (ct
->c_subtype
== TEXT_PLAIN
) {
2446 status
= convert_charset (ct
, dest_charset
, message_mods
);
2449 char *ct_charset
= content_charset (ct
);
2451 report (NULL
, ct
->c_partno
, ct
->c_file
,
2452 "convert %s to %s", ct_charset
, dest_charset
);
2456 char *ct_charset
= content_charset (ct
);
2458 report ("iconv", ct
->c_partno
, ct
->c_file
,
2459 "failed to convert %s to %s", ct_charset
, dest_charset
);
2465 case CT_MULTIPART
: {
2466 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2469 /* Should check to see if the body for this part is encoded?
2470 For now, it gets passed along as-is by InitMultiPart(). */
2471 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2473 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2479 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2480 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2483 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2496 * Fix various problems that aren't handled elsewhere. These
2497 * are fixed unconditionally: there are no switches to disable
2498 * them. Currently, "problems" are these:
2499 * 1) remove extraneous semicolon at the end of a header parameter list
2500 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2501 * filename parameters in Content-Type and Content-Disposition
2502 * headers, respectively.
2505 fix_always (CT ct
, int *message_mods
) {
2508 switch (ct
->c_type
) {
2509 case CT_MULTIPART
: {
2510 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2513 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2514 status
= fix_always (part
->mp_part
, message_mods
);
2520 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2521 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2523 status
= fix_always (e
->eb_content
, message_mods
);
2530 if (ct
->c_first_hf
) {
2531 fix_filename_encoding (ct
);
2534 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2535 size_t len
= strlen (hf
->value
);
2537 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2538 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2539 /* Only do this for Content-Type and
2540 Content-Disposition fields because those are the
2541 only headers that parse_mime() warns about. */
2545 /* whitespace following a trailing ';' will be nuked as well */
2546 if (hf
->value
[len
- 1] == '\n') {
2547 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2548 if (len
-- == 0) { break; }
2552 if (hf
->value
[len
- 2] == ';') {
2553 /* Remove trailing ';' from parameter value. */
2554 hf
->value
[len
- 2] = '\n';
2555 hf
->value
[len
- 1] = '\0';
2557 /* Also, if Content-Type parameter, remove trailing ';'
2558 from ct->c_ctline. This probably isn't necessary
2560 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2561 size_t l
= strlen(ct
->c_ctline
) - 1;
2562 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2563 ct
->c_ctline
[l
] == ';') {
2564 ct
->c_ctline
[l
--] = '\0';
2565 if (l
== 0) { break; }
2571 report (NULL
, ct
->c_partno
, ct
->c_file
,
2572 "remove trailing ; from %s parameter value",
2584 * Factor out common code for loops in fix_filename_encoding().
2587 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
) {
2590 if (has_prefix(value
, "=?") && has_suffix(value
, "?=")) {
2591 /* Looks like an RFC 2047 encoded parameter. */
2592 char decoded
[PATH_MAX
+ 1];
2594 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2595 /* Encode using RFC 2231. */
2596 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2599 inform("failed to decode %s parameter %s", name
, value
);
2608 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2609 * filename parameters in Content-Type and Content-Disposition
2610 * headers, respectively.
2613 fix_filename_encoding (CT ct
) {
2618 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2619 if (pm
->pm_name
&& pm
->pm_value
&&
2620 strcasecmp (pm
->pm_name
, "name") == 0) {
2621 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2622 &ct
->c_ctinfo
.ci_first_pm
,
2623 &ct
->c_ctinfo
.ci_last_pm
);
2627 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2628 if (pm
->pm_name
&& pm
->pm_value
&&
2629 strcasecmp (pm
->pm_name
, "filename") == 0) {
2630 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2636 /* Fix hf values to correspond. */
2637 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2638 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2640 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2641 field
= TYPE_HEADER
;
2642 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2643 field
= DISPO_HEADER
;
2646 if (field
!= OTHER
) {
2647 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2649 if (semicolon_loc
) {
2651 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2652 const char *const params
=
2654 field
== TYPE_HEADER
2655 ? ct
->c_ctinfo
.ci_first_pm
2656 : ct
->c_dispo_first
,
2658 const char *const new_params
= concat (params
, "\n", NULL
);
2660 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2661 free((void *)new_params
); /* Cast away const. Sigh. */
2662 free((void *)params
);
2664 inform("did not find semicolon in %s:%s\n",
2665 hf
->name
, hf
->value
);
2675 * Output content in input file to output file.
2678 write_content (CT ct
, const char *input_filename
, char *outfile
, FILE *outfp
,
2679 int modify_inplace
, int message_mods
) {
2682 if (modify_inplace
) {
2683 if (message_mods
> 0) {
2684 if ((status
= output_message_fp (ct
, outfp
, outfile
)) == OK
) {
2685 char *infile
= input_filename
2686 ? mh_xstrdup (input_filename
)
2687 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2689 if (remove_file (infile
) == OK
) {
2690 if (rename (outfile
, infile
)) {
2691 /* Rename didn't work, possibly because of an
2692 attempt to rename across filesystems. Try
2693 brute force copy. */
2694 int old
= open (outfile
, O_RDONLY
);
2696 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2699 if (old
!= -1 && new != -1) {
2700 char buffer
[NMH_BUFSIZ
];
2702 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2704 if (write (new, buffer
, i
) != i
) {
2710 if (new != -1) { close (new); }
2711 if (old
!= -1) { close (old
); }
2712 (void) m_unlink (outfile
);
2715 /* The -file argument processing used path() to
2716 expand filename to absolute path. */
2717 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2719 inform("unable to rename %s %s to %s, continuing...",
2720 file
? "file" : "message", outfile
,
2726 inform("unable to remove input file %s, "
2727 "not modifying it, continuing...", infile
);
2728 (void) m_unlink (outfile
);
2737 /* No modifications and didn't need the tmp outfile. */
2738 (void) m_unlink (outfile
);
2741 /* Output is going to some file. Produce it whether or not
2742 there were modifications. */
2743 status
= output_message_fp (ct
, outfp
, outfile
);
2752 * parse_mime() does not set lf_line_endings in struct text, so use this
2753 * function to do it. It touches the parts the decodetypes identifies.
2756 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
) {
2757 switch (ct
->c_type
) {
2758 case CT_MULTIPART
: {
2759 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2762 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2763 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2769 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2770 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2772 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2777 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2778 if (ct
->c_ctparams
== NULL
) {
2779 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2781 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2788 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2789 * use the standard MH backup file.
2792 remove_file (const char *file
) {
2794 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2795 int status
= system (rmm_command
);
2798 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2800 /* This is OK for a non-message file, it still uses the
2801 BACKUP_PREFIX form. The backup file will be in the same
2802 directory as file. */
2803 return rename (file
, m_backup (file
));
2808 * Output formatted message to user.
2811 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2816 va_start (args
, message
);
2817 fmt
= concat (filename
, partno
? " part " : ", ",
2818 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
2820 advertise (what
, NULL
, fmt
, args
);
2833 fprintf (stderr
, "\n");