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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
13 #include "h/fmt_scan.h"
15 #include "h/mhparse.h"
18 #include "h/signals.h"
19 #include "sbr/m_maildir.h"
20 #include "sbr/m_mktemp.h"
21 #include "sbr/mime_type.h"
25 #include "mhshowsbr.h"
28 #define MHFIXMSG_SWITCHES \
29 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
30 X("nodecodetext", 0, NDECODETEXTSW) \
31 X("decodetypes", 0, DECODETYPESW) \
32 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
33 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
34 X("textcharset", 0, TEXTCHARSETSW) \
35 X("notextcharset", 0, NTEXTCHARSETSW) \
36 X("reformat", 0, REFORMATSW) \
37 X("noreformat", 0, NREFORMATSW) \
38 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
39 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
40 X("fixboundary", 0, FIXBOUNDARYSW) \
41 X("nofixboundary", 0, NFIXBOUNDARYSW) \
42 X("fixcte", 0, FIXCOMPOSITECTESW) \
43 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
44 X("fixtype mimetype", 0, FIXTYPESW) \
45 X("file file", 0, FILESW) \
46 X("outfile file", 0, OUTFILESW) \
47 X("rmmproc program", 0, RPROCSW) \
48 X("normmproc", 0, NRPRCSW) \
49 X("changecur", 0, CHGSW) \
50 X("nochangecur", 0, NCHGSW) \
51 X("verbose", 0, VERBSW) \
52 X("noverbose", 0, NVERBSW) \
53 X("version", 0, VERSIONSW) \
54 X("help", 0, HELPSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(MHFIXMSG
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
66 int debugsw
; /* Needed by mhparse.c. */
68 #define quitser pipeser
73 typedef struct fix_transformations
{
81 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
84 } fix_transformations
;
86 static int mhfixmsgsbr (CT
*, char *, const fix_transformations
*,
87 FILE **, char *, FILE **);
88 static int fix_boundary (CT
*, int *);
89 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
90 static int get_multipart_boundary (CT
, char **);
91 static int replace_boundary (CT
, char *, char *);
92 static int fix_types (CT
, svector_t
, int *);
93 static char *replace_substring (char **, const char *, const char *);
94 static char *remove_parameter (char *, const char *);
95 static int fix_composite_cte (CT
, int *);
96 static int set_ce (CT
, int);
97 static int ensure_text_plain (CT
*, CT
, int *, int);
98 static int find_textplain_sibling (CT
, int, int *);
99 static int insert_new_text_plain_part (CT
, int, CT
);
100 static CT
build_text_plain_part (CT
);
101 static int insert_into_new_mp_alt (CT
*, int *);
102 static CT
divide_part (CT
);
103 static void copy_ctinfo (CI
, CI
);
104 static int decode_part (CT
);
105 static int reformat_part (CT
, char *, char *, char *, int);
106 static CT
build_multipart_alt (CT
, CT
, int, int);
107 static int boundary_in_content (FILE **, char *, const char *);
108 static void transfer_noncontent_headers (CT
, CT
);
109 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
110 static int decode_text_parts (CT
, int, const char *, int *);
111 static int should_decode(const char *, const char *, const char *);
112 static int content_encoding (CT
, const char **);
113 static int strip_crs (CT
, int *);
114 static void update_cte (CT
);
115 static int least_restrictive_encoding (CT
) PURE
;
116 static int less_restrictive (int, int);
117 static int convert_charsets (CT
, char *, int *);
118 static int fix_always (CT
, int *);
119 static int fix_filename_param (char *, char *, PM
*, PM
*);
120 static int fix_filename_encoding (CT
);
121 static int write_content (CT
, const char *, char *, FILE *, int, int);
122 static void set_text_ctparams(CT
, char *, int);
123 static int remove_file (const char *);
124 static void report (char *, char *, char *, char *, ...)
126 static void pipeser (int);
130 main (int argc
, char **argv
)
133 char *cp
, *file
= NULL
, *folder
= NULL
;
134 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
135 char **argp
, **arguments
;
136 struct msgs_array msgs
= { 0, 0, NULL
};
137 struct msgs
*mp
= NULL
;
139 FILE *fp
, *infp
= NULL
, *outfp
= NULL
;
140 bool using_stdin
= false;
143 fix_transformations fx
;
144 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
146 fx
.replacetextplain
= 0;
147 fx
.decodetext
= CE_8BIT
;
148 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
149 fx
.lf_line_endings
= 0;
150 fx
.textcharset
= NULL
;
152 if (nmh_init(argv
[0], true, false)) { return 1; }
154 arguments
= getarguments (invo_name
, argc
, argv
, 1);
160 while ((cp
= *argp
++)) {
162 switch (smatch (++cp
, switches
)) {
164 ambigsw (cp
, switches
);
167 die("-%s unknown", cp
);
170 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
172 print_help (buf
, switches
, 1);
175 print_version(invo_name
);
179 if (! (cp
= *argp
++) || *cp
== '-') {
180 die("missing argument to %s", argp
[-2]);
182 if (! strcasecmp (cp
, "8bit")) {
183 fx
.decodetext
= CE_8BIT
;
184 } else if (! strcasecmp (cp
, "7bit")) {
185 fx
.decodetext
= CE_7BIT
;
186 } else if (! strcasecmp (cp
, "binary")) {
187 fx
.decodetext
= CE_BINARY
;
189 die("invalid argument to %s", argp
[-2]);
196 if (! (cp
= *argp
++) || *cp
== '-') {
197 die("missing argument to %s", argp
[-2]);
201 case CRLFLINEBREAKSSW
:
202 fx
.lf_line_endings
= 0;
204 case NCRLFLINEBREAKSSW
:
205 fx
.lf_line_endings
= 1;
208 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
209 die("missing argument to %s", argp
[-2]);
222 case FIXCOMPOSITECTESW
:
223 fx
.fixcompositecte
= 1;
225 case NFIXCOMPOSITECTESW
:
226 fx
.fixcompositecte
= 0;
229 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
230 die("missing argument to %s", argp
[-2]);
232 if (! strncasecmp (cp
, "multipart/", 10) ||
233 ! strncasecmp (cp
, "message/", 8))
234 die("-fixtype %s not allowed", cp
);
235 if (! strchr (cp
, '/'))
236 die("-fixtype requires type/subtype");
237 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
238 svector_push_back (fx
.fixtypes
, cp
);
246 case REPLACETEXTPLAINSW
:
247 fx
.replacetextplain
= 1;
249 case NREPLACETEXTPLAINSW
:
250 fx
.replacetextplain
= 0;
253 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
254 die("missing argument to %s", argp
[-2]);
256 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
259 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
260 die("missing argument to %s", argp
[-2]);
262 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
265 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
266 die("missing argument to %s", argp
[-2]);
286 if (*cp
== '+' || *cp
== '@') {
288 die("only one folder at a time!");
289 folder
= pluspath (cp
);
292 /* Interpret a full path as a filename, not a message. */
293 file
= mh_xstrdup (cp
);
295 app_msgarg (&msgs
, cp
);
300 SIGNAL (SIGQUIT
, quitser
);
301 SIGNAL (SIGPIPE
, pipeser
);
304 * Read the standard profile setup
306 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
307 readconfig(NULL
, fp
, cp
, 0);
311 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= true;
312 suppress_extraneous_trailing_semicolon_warning
= true;
314 if (! context_find ("path")) {
315 free (path ("./", TFOLDER
));
318 if (file
&& msgs
.size
) {
319 die("cannot specify msg and file at same time!");
323 /* Open the outfile now, so we don't have to risk opening it
324 after running out of fds. */
325 if (strcmp (outfile
, "-") == 0) {
327 } else if ((outfp
= fopen (outfile
, "w")) == NULL
) {
328 adios (outfile
, "unable to open for writing");
333 * check if message is coming from file
336 /* If file is stdin, create a tmp file name before parse_mime()
337 has a chance, because it might put in on a different
338 filesystem than the output file. Instead, put it in the
339 user's preferred tmp directory. */
342 if (! strcmp ("-", file
)) {
348 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
349 die("unable to create temporary file in %s",
353 file
= mh_xstrdup (cp
);
354 cpydata (STDIN_FILENO
, fd
, "-", file
);
358 (void) m_unlink (file
);
359 die("failed to write temporary file");
363 cts
= mh_xcalloc(2, sizeof *cts
);
366 if ((ct
= parse_mime (file
))) {
367 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
370 inform("unable to parse message from file %s", file
);
373 /* If there's an outfile, pass the input message unchanged, so the
374 message won't get dropped from a pipeline. */
376 /* Something went wrong. Output might be expected, such as if
377 this were run as a filter. Just copy the input to the
379 if ((infp
= fopen (file
, "r")) == NULL
) {
380 adios (file
, "unable to open for reading");
383 if (copy_input_to_output (file
, infp
, outfile
, outfp
) != OK
) {
384 inform("unable to copy message to %s, "
385 "it might be lost\n", outfile
);
394 * message(s) are coming from a folder
399 app_msgarg(&msgs
, "cur");
402 folder
= getfolder (1);
404 maildir
= mh_xstrdup(m_maildir (folder
));
406 /* chdir so that error messages, esp. from MIME parser, just
407 refer to the message and not its path. */
408 if (chdir (maildir
) == NOTOK
) {
409 adios (maildir
, "unable to change directory to");
412 /* read folder and create message structure */
413 if (! (mp
= folder_read (folder
, 1))) {
414 die("unable to read folder %s", folder
);
417 /* check for empty folder */
418 if (mp
->nummsg
== 0) {
419 die("no messages in %s", folder
);
422 /* parse all the message ranges/sequences and set SELECTED */
423 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
424 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
427 seq_setprev (mp
); /* set the previous-sequence */
429 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
432 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
433 if (is_selected(mp
, msgnum
)) {
434 char *msgnam
= m_name (msgnum
);
436 if ((ct
= parse_mime (msgnam
))) {
437 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
440 inform("unable to parse message %s", msgnam
);
443 /* If there's an outfile, pass the input message
444 unchanged, so the message won't get dropped from a
447 /* Something went wrong. Output might be expected,
448 such as if this were run as a filter. Just copy
449 the input to the output. */
450 /* Can't use path() here because 1) it might have been
451 called before and it caches the pwd, and 2) we call
452 chdir() after that. */
453 char *input_filename
=
454 concat (maildir
, "/", msgnam
, NULL
);
456 if ((infp
= fopen (input_filename
, "r")) == NULL
) {
457 adios (input_filename
,
458 "unable to open for reading");
461 if (copy_input_to_output (input_filename
, infp
,
462 outfile
, outfp
) != OK
) {
463 inform("unable to copy message to %s, "
464 "it might be lost\n", outfile
);
469 free (input_filename
);
476 seq_setcur (mp
, mp
->hghsel
); /* update current message */
478 seq_save (mp
); /* synchronize sequences */
479 context_replace (pfolder
, folder
);/* update current folder */
480 context_save (); /* save the context file */
484 for (ctp
= cts
; *ctp
; ++ctp
) {
486 mhfixmsgsbr (ctp
, maildir
, &fx
, &infp
, outfile
, &outfp
) == OK
492 (void) m_unlink (file
);
495 /* Just calling m_backup() unlinks the backup file. */
496 (void) m_backup (file
);
507 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
508 if (infp
) { fclose (infp
); } /* even if stdin */
509 if (outfp
) { fclose (outfp
); } /* even if stdout */
515 done (status
== OK
? 0 : 1);
521 * Apply transformations to one message.
524 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
525 FILE **infp
, char *outfile
, FILE **outfp
)
527 /* Store input filename in case one of the transformations, i.e.,
528 fix_boundary(), rewrites to a tmp file. */
529 char *input_filename
= maildir
530 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
531 : mh_xstrdup ((*ctp
)->c_file
);
532 bool modify_inplace
= false;
533 int message_mods
= 0;
536 /* Though the input file won't need to be opened if everything goes
537 well, do it here just in case there's a failure, and that failure is
538 running out of file descriptors. */
539 if ((*infp
= fopen (input_filename
, "r")) == NULL
) {
540 adios (input_filename
, "unable to open for reading");
543 if (outfile
== NULL
) {
544 modify_inplace
= true;
546 if ((*ctp
)->c_file
) {
548 /* outfp will be closed by the caller */
549 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, outfp
)) ==
551 die("unable to create temporary file in %s",
554 outfile
= mh_xstrdup (tempfile
);
556 die("missing both input and output filenames\n");
558 } /* else *outfp was defined by caller */
560 reverse_alternative_parts (*ctp
);
561 status
= fix_always (*ctp
, &message_mods
);
562 if (status
== OK
&& fx
->fixboundary
) {
563 status
= fix_boundary (ctp
, &message_mods
);
565 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
566 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
568 if (status
== OK
&& fx
->fixcompositecte
) {
569 status
= fix_composite_cte (*ctp
, &message_mods
);
571 if (status
== OK
&& fx
->reformat
) {
573 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
575 if (status
== OK
&& fx
->decodetext
) {
576 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
580 if (status
== OK
&& fx
->textcharset
!= NULL
) {
581 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
584 if (status
== OK
&& ! (*ctp
)->c_umask
) {
585 /* Set the umask for the contents file. This currently
586 isn't used but just in case it is in the future. */
589 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
590 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
592 (*ctp
)->c_umask
= ~m_gmprot();
597 * Write the content to a file
600 status
= write_content (*ctp
, input_filename
, outfile
, *outfp
,
601 modify_inplace
, message_mods
);
602 } else if (! modify_inplace
) {
603 /* Something went wrong. Output might be expected, such
604 as if this were run as a filter. Just copy the input
606 if (copy_input_to_output (input_filename
, *infp
, outfile
,
608 inform("unable to copy message to %s, it might be lost\n",
613 if (modify_inplace
) {
614 if (status
!= OK
) { (void) m_unlink (outfile
); }
621 free (input_filename
);
628 * Copy input message to output. Assumes not modifying in place, so this
629 * might be running as part of a pipeline.
632 copy_input_to_output (const char *input_filename
, FILE *infp
,
633 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
)
655 struct multipart
*mp
;
658 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
659 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
662 * 1) Get boundary at end of part.
663 * 2) Get boundary at beginning of part and compare to the end-of-part
665 * 3) Write out contents of ct to tmp file, replacing boundary in
666 * header with boundary from part. Set c_unlink to 1.
668 * 5) Call parse_mime() on the tmp file, replacing ct.
671 if (mp
&& mp
->mp_start
) {
674 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
677 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
678 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
679 char *filename
= mh_xstrdup ((*ct
)->c_file
);
683 if ((fixed_ct
= parse_mime (fixed
))) {
689 report (NULL
, NULL
, filename
,
690 "fix multipart boundary");
694 inform("unable to parse fixed part");
699 inform("unable to replace broken boundary");
703 inform("unable to create temporary file in %s",
708 free (part_boundary
);
710 /* Couldn't fix the boundary. Report failure so that mhfixmsg
711 doesn't modify the message. */
715 /* No multipart struct, even though the content type is
716 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
727 * Find boundary at end of multipart.
730 get_multipart_boundary (CT ct
, char **part_boundary
)
732 char buffer
[NMH_BUFSIZ
];
733 char *end_boundary
= NULL
;
734 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
735 ? (off_t
) (ct
->c_end
- sizeof buffer
)
736 : (off_t
) ct
->c_begin
;
740 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
741 be big enough, even if it's just 1024, to make that unlikely. */
743 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
744 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
745 advise (ct
->c_file
, "unable to open for reading");
749 /* Get boundary at end of multipart. */
750 while (begin
>= (off_t
) ct
->c_begin
) {
751 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
752 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
753 char *cp
= rfind_str (buffer
, bytes_read
, "--");
758 /* Trim off trailing "--" and anything beyond. */
760 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
761 if (strlen (end
) > 3 && *end
++ == '\n' &&
762 *end
++ == '-' && *end
++ == '-') {
763 end_boundary
= mh_xstrdup (end
);
770 if (end_boundary
|| begin
<= (off_t
) (ct
->c_begin
+ sizeof buffer
))
772 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
)
822 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
824 m_getfld_state_t gstate
;
827 if (ct
->c_file
== NULL
) {
828 inform("missing input filename");
832 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
833 advise (ct
->c_file
, "unable to open for reading");
837 if ((fpout
= fopen (file
, "w")) == NULL
) {
839 advise (file
, "unable to open for writing");
843 gstate
= m_getfld_state_init(fpin
);
844 for (compnum
= 1;;) {
845 int bufsz
= (int) sizeof buf
;
847 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
852 /* get copies of the buffers */
853 np
= mh_xstrdup (name
);
854 vp
= mh_xstrdup (buf
);
856 /* if necessary, get rest of field */
857 while (state
== FLDPLUS
) {
859 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
860 vp
= add (buf
, vp
); /* add to previous value */
863 if (strcasecmp (TYPE_FIELD
, np
)) {
864 fprintf (fpout
, "%s:%s", np
, vp
);
866 char *new_ctline
, *new_params
;
868 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
869 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
872 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
873 ct
->c_ctinfo
.ci_subtype
, NULL
);
874 new_params
= output_params(LEN(TYPE_FIELD
) +
875 strlen(new_ctline
) + 1,
876 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
877 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
878 FENDNULL(new_params
));
890 /* buf will have a terminating NULL, skip it. */
891 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
892 advise (file
, "fwrite");
901 inform("message format error in component #%d", compnum
);
906 inform("getfld() returned %d", state
);
914 m_getfld_state_destroy (&gstate
);
923 * Fix Content-Type header to reflect the content of its part.
926 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
)
930 switch (ct
->c_type
) {
932 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
935 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
936 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
942 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
943 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
945 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
952 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
953 for (typep
= svector_strs (fixtypes
);
954 typep
&& (type
= *typep
);
957 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
960 if (! strcasecmp (type
, type_subtype
) &&
961 decode_part (ct
) == OK
&&
962 ct
->c_cefile
.ce_file
!= NULL
) {
963 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
966 if ((cp
= strchr (ct_type_subtype
, ';'))) {
967 /* Truncate to remove any parameter list from
968 mime_type () result. */
972 if (strcasecmp (type
, ct_type_subtype
)) {
973 char *ct_type
, *ct_subtype
;
976 /* The Content-Type header does not match the
977 content, so update these struct Content
980 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
983 /* Extract type and subtype from type/subtype. */
984 ct_type
= mh_xstrdup(ct_type_subtype
);
985 if ((cp
= strchr (ct_type
, '/'))) {
987 ct_subtype
= mh_xstrdup(++cp
);
989 inform("missing / in MIME type of %s %s",
990 ct
->c_file
, ct
->c_partno
);
995 ct
->c_type
= ct_str_type (ct_type
);
996 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
998 free (ct
->c_ctinfo
.ci_type
);
999 ct
->c_ctinfo
.ci_type
= ct_type
;
1000 free (ct
->c_ctinfo
.ci_subtype
);
1001 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
1002 if (! replace_substring (&ct
->c_ctline
, type
,
1004 inform("did not find %s in %s",
1005 type
, ct
->c_ctline
);
1008 /* Update Content-Type header field. */
1009 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1010 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1011 if (replace_substring (&hf
->value
, type
,
1015 report (NULL
, ct
->c_partno
, ct
->c_file
,
1016 "change Content-Type in header "
1018 type
, ct_type_subtype
);
1022 inform("did not find %s in %s", type
, hf
->value
);
1026 free (ct_type_subtype
);
1028 free (type_subtype
);
1038 * Replace a substring, allocating space to hold the new one.
1041 replace_substring (char **str
, const char *old
, const char *new)
1045 if ((cp
= strstr (*str
, old
))) {
1046 char *remainder
= cp
+ strlen (old
);
1047 char *prefix
, *new_str
;
1050 prefix
= mh_xstrdup(*str
);
1051 *(prefix
+ (cp
- *str
)) = '\0';
1052 new_str
= concat (prefix
, new, remainder
, NULL
);
1055 new_str
= concat (new, remainder
, NULL
);
1060 return *str
= new_str
;
1068 * Remove a name=value parameter, given just its name, from a header value.
1071 remove_parameter (char *str
, const char *name
)
1073 /* It looks to me, based on the BNF in RFC 2045, than there can't
1074 be whitespace between the parameter name and the "=", or
1075 between the "=" and the parameter value. */
1076 char *param_name
= concat (name
, "=", NULL
);
1079 if ((cp
= strstr (str
, param_name
))) {
1083 /* Remove any leading spaces, before the parameter name. */
1085 start
> str
&& isspace ((unsigned char) *(start
-1));
1089 /* Remove a leading semicolon. */
1090 if (start
> str
&& *(start
-1) == ';') { --start
; }
1092 end
= cp
+ strlen (name
) + 1;
1094 /* Skip past the quoted value, and then the final quote. */
1095 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1098 /* Skip past the value. */
1099 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1102 /* Count how many characters need to be moved. Include
1103 trailing null, which is accounted for by the
1104 initialization of count to 1. */
1105 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1106 (void) memmove (start
, end
, count
);
1116 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1117 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1121 fix_composite_cte (CT ct
, int *message_mods
)
1125 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1126 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1127 ct
->c_encoding
!= CE_BINARY
) {
1130 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1131 char *name
= hf
->name
;
1132 for (; isspace((unsigned char)*name
); ++name
) {
1136 if (! strncasecmp (name
, ENCODING_FIELD
,
1137 LEN(ENCODING_FIELD
))) {
1138 char *prefix
= "Nmh-REPLACED-INVALID-";
1142 h
->name
= mh_xstrdup (hf
->name
);
1143 h
->hf_encoding
= hf
->hf_encoding
;
1147 /* Retain old header but prefix its name. */
1149 hf
->name
= concat (prefix
, h
->name
, NULL
);
1153 char *encoding
= cpytrim (hf
->value
);
1154 report (NULL
, ct
->c_partno
, ct
->c_file
,
1155 "replace Content-Transfer-Encoding of %s "
1156 "with 8 bit", encoding
);
1160 h
->value
= mh_xstrdup (" 8bit\n");
1162 /* Don't need to warn for multiple C-T-E header
1163 fields, parse_mime() already does that. But
1164 if there are any, fix them all as necessary. */
1169 set_ce (ct
, CE_8BIT
);
1172 if (ct
->c_type
== CT_MULTIPART
) {
1173 struct multipart
*m
;
1176 m
= (struct multipart
*) ct
->c_ctparams
;
1177 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1178 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1191 * Set content encoding.
1194 set_ce (CT ct
, int encoding
)
1196 const char *ce
= ce_str (encoding
);
1197 const struct str2init
*ctinit
= get_ce_method (ce
);
1200 char *cte
= concat (" ", ce
, "\n", NULL
);
1201 bool found_cte
= false;
1203 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1204 caller is decode_text_parts (). Save because we'll
1206 struct cefile decoded_content_info
= ct
->c_cefile
;
1208 ct
->c_encoding
= encoding
;
1210 ct
->c_ctinitfnx
= ctinit
->si_init
;
1211 /* This will assign ct->c_cefile with an all-0 struct, which
1213 (*ctinit
->si_init
) (ct
);
1214 /* After returning, the caller should set
1215 ct->c_cefile.ce_file to the name of the file containing
1218 if (ct
->c_ceclosefnx
) {
1219 (*ct
->c_ceclosefnx
) (ct
);
1222 /* Restore the cefile. */
1223 ct
->c_cefile
= decoded_content_info
;
1225 /* Update/add Content-Transfer-Encoding header field. */
1226 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1227 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1234 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1237 /* Update c_celine. It's used only by mhlist -debug. */
1238 free (ct
->c_celine
);
1239 ct
->c_celine
= mh_xstrdup (cte
);
1249 * Make sure each text part has a corresponding text/plain part.
1252 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
)
1256 switch ((*ct
)->c_type
) {
1258 /* Nothing to do for text/plain. */
1259 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1261 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1262 parent
->c_subtype
== MULTI_ALTERNATE
) {
1263 int new_subpart_number
= 1;
1264 int has_text_plain
=
1265 find_textplain_sibling (parent
, replacetextplain
,
1266 &new_subpart_number
);
1268 if (! has_text_plain
) {
1269 /* Parent is a multipart/alternative. Insert a new
1270 text/plain subpart. */
1271 const int inserted
=
1272 insert_new_text_plain_part (*ct
, new_subpart_number
,
1277 report (NULL
, parent
->c_partno
, parent
->c_file
,
1278 "insert text/plain part");
1284 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1285 parent
->c_subtype
== MULTI_RELATED
) {
1286 char *type_subtype
=
1287 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1288 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1289 const char *parent_type
=
1290 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1291 int new_subpart_number
= 1;
1292 int has_text_plain
= 0;
1294 /* Have to do string comparison on the subtype because we
1295 don't enumerate all of them in c_subtype values.
1296 parent_type will be NULL if the multipart/related part
1297 doesn't have a type parameter. The type parameter must
1298 be specified according to RFC 2387 Sec. 3.1 but not all
1300 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1301 /* The type of this part matches the root type of the
1302 parent multipart/related. Look to see if there's
1303 text/plain sibling. */
1305 find_textplain_sibling (parent
, replacetextplain
,
1306 &new_subpart_number
);
1309 free (type_subtype
);
1311 if (! has_text_plain
) {
1312 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1316 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1317 if (*ct
!= part
->mp_part
) {
1323 /* Parent is a multipart/related. Insert a new
1324 text/plain subpart in a new multipart/alternative. */
1325 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1326 /* Not an error if text/plain couldn't be added. */
1329 /* There are no siblings, so insert a new text/plain
1330 subpart, and change the parent type from
1331 multipart/related to multipart/alternative. */
1332 const int inserted
=
1333 insert_new_text_plain_part (*ct
, new_subpart_number
,
1339 parent
->c_subtype
= MULTI_ALTERNATE
;
1340 free (parent
->c_ctinfo
.ci_subtype
);
1341 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1342 if (! replace_substring (&parent
->c_ctline
, "/related",
1344 inform("did not find multipart/related in %s",
1348 /* Update Content-Type header field. */
1349 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1350 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1351 if (replace_substring (&hf
->value
, "/related",
1355 report (NULL
, parent
->c_partno
,
1357 "insert text/plain part");
1360 /* Remove, e.g., type="text/html" from
1361 multipart/alternative. */
1362 remove_parameter (hf
->value
, "type");
1365 inform("did not find multipart/"
1366 "related in header %s", hf
->value
);
1370 /* Not an error if text/plain couldn't be inserted. */
1375 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1382 case CT_MULTIPART
: {
1383 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1386 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1387 if ((*ct
)->c_type
== CT_MULTIPART
) {
1388 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1396 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1397 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1399 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1410 * See if there is a sibling text/plain, and return its subpart number.
1413 find_textplain_sibling (CT parent
, int replacetextplain
,
1414 int *new_subpart_number
)
1416 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1417 struct part
*part
, *prev
;
1418 bool has_text_plain
= false;
1420 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1421 ++*new_subpart_number
;
1422 if (part
->mp_part
->c_type
== CT_TEXT
&&
1423 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1424 if (replacetextplain
) {
1425 struct part
*old_part
;
1426 if (part
== mp
->mp_parts
) {
1427 old_part
= mp
->mp_parts
;
1428 mp
->mp_parts
= part
->mp_next
;
1430 old_part
= prev
->mp_next
;
1431 prev
->mp_next
= part
->mp_next
;
1434 report (NULL
, parent
->c_partno
, parent
->c_file
,
1435 "remove text/plain part %s",
1436 old_part
->mp_part
->c_partno
);
1438 free_content (old_part
->mp_part
);
1441 has_text_plain
= true;
1448 return has_text_plain
;
1453 * Insert a new text/plain part.
1456 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
)
1458 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1459 struct part
*new_part
;
1462 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1464 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1466 new_part
->mp_next
= mp
->mp_parts
;
1467 mp
->mp_parts
= new_part
;
1468 new_part
->mp_part
->c_partno
=
1469 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1475 free_content (new_part
->mp_part
);
1483 * Create a text/plain part to go along with non-plain sibling part.
1486 build_text_plain_part (CT encoded_part
)
1488 CT tp_part
= divide_part (encoded_part
);
1489 char *tmp_plain_file
= NULL
;
1491 if (decode_part (tp_part
) == OK
) {
1492 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1493 contains the decoded contents. And the decoding function, such
1494 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1495 be unlinked by free_content (). */
1498 /* This m_mktemp2() call closes the temp file. */
1499 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1500 inform("unable to create temporary file in %s",
1503 tmp_plain_file
= mh_xstrdup (tempfile
);
1504 if (reformat_part (tp_part
, tmp_plain_file
,
1505 tp_part
->c_ctinfo
.ci_type
,
1506 tp_part
->c_ctinfo
.ci_subtype
,
1507 tp_part
->c_type
) == OK
) {
1513 free_content (tp_part
);
1514 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1515 free (tmp_plain_file
);
1522 * Slip new text/plain part into a new multipart/alternative.
1525 insert_into_new_mp_alt (CT
*ct
, int *message_mods
)
1527 CT tp_part
= build_text_plain_part (*ct
);
1531 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1534 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1536 if (mp
&& mp
->mp_parts
) {
1537 mp
->mp_parts
->mp_part
= tp_part
;
1538 /* Make the new multipart/alternative the parent. */
1543 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1544 "insert text/plain part");
1547 free_content (tp_part
);
1548 free_content (mp_alt
);
1555 /* Not an error if text/plain couldn't be built. */
1563 * Clone a MIME part.
1571 /* Just copy over what is needed for decoding. c_vrsn and
1572 c_celine aren't necessary. */
1573 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1574 new_part
->c_begin
= ct
->c_begin
;
1575 new_part
->c_end
= ct
->c_end
;
1576 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1577 new_part
->c_type
= ct
->c_type
;
1578 new_part
->c_cefile
= ct
->c_cefile
;
1579 new_part
->c_encoding
= ct
->c_encoding
;
1580 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1581 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1582 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1583 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1585 /* c_ctline is used by reformat__part(), so it can preserve
1586 anything after the type/subtype. */
1587 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1594 * Copy the content info from one part to another.
1597 copy_ctinfo (CI dest
, CI src
)
1601 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1602 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1604 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1605 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1607 if (s_pm
->pm_charset
) {
1608 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1610 if (s_pm
->pm_lang
) {
1611 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1615 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1616 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1631 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, &file
)) == NULL
) {
1632 die("unable to create temporary file in %s", get_temp_dir());
1634 tmp_decoded
= mh_xstrdup (tempfile
);
1635 /* The following call will load ct->c_cefile.ce_file with the tmp
1636 filename of the decoded content. tmp_decoded will contain the
1637 encoded output, get rid of that. */
1638 status
= output_message_fp (ct
, file
, tmp_decoded
);
1639 (void) m_unlink (tmp_decoded
);
1641 if (fclose (file
)) {
1642 inform("unable to close temporary file %s, continuing...", tempfile
);
1650 * Reformat content as plain text.
1651 * Some of the arguments aren't really needed now, but maybe will
1652 * be in the future for other than text types.
1655 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
)
1657 int output_subtype
, output_encoding
;
1658 const char *reason
= NULL
;
1662 /* Hacky: this redirects the output from whatever command is used
1663 to show the part to a file. So, the user can't have any output
1664 redirection in that command.
1665 Could show_multi() in mhshowsbr.c avoid this? */
1667 /* Check for invo_name-format-type/subtype. */
1668 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1670 inform("Don't know how to convert %s, there is no "
1671 "%s-format-%s/%s profile entry",
1672 ct
->c_file
, invo_name
, type
, subtype
);
1676 if (strchr (cf
, '>')) {
1677 inform("'>' prohibited in \"%s\",\nplease fix your "
1678 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1684 cp
= concat (cf
, " >", file
, NULL
);
1685 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1688 /* Unlink decoded content tmp file and free its filename to avoid
1689 leaks. The file stream should already have been closed. */
1690 if (ct
->c_cefile
.ce_unlink
) {
1691 (void) m_unlink (ct
->c_cefile
.ce_file
);
1692 free (ct
->c_cefile
.ce_file
);
1693 ct
->c_cefile
.ce_file
= NULL
;
1694 ct
->c_cefile
.ce_unlink
= 0;
1697 if (c_type
== CT_TEXT
) {
1698 output_subtype
= TEXT_PLAIN
;
1700 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1704 output_encoding
= content_encoding (ct
, &reason
);
1706 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1707 ct
->c_cefile
.ce_file
= file
;
1708 ct
->c_cefile
.ce_unlink
= 1;
1710 ct
->c_cefile
.ce_unlink
= 0;
1719 * Fill in a multipart/alternative part.
1722 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
)
1724 char *boundary_prefix
= "----=_nmh-multipart";
1725 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1726 char *boundary_indicator
= "; boundary=";
1727 char *typename
, *subtypename
, *name
;
1730 struct multipart
*m
;
1731 const struct str2init
*ctinit
;
1735 /* Set up the multipart/alternative part. These fields of *ct were
1736 initialized to 0 by mh_xcalloc():
1737 c_fp, c_unlink, c_begin, c_end,
1738 c_vrsn, c_ctline, c_celine,
1739 c_id, c_descr, c_dispo, c_partno,
1740 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1741 c_cefile, c_encoding,
1742 c_digested, c_digest[16], c_ctexbody,
1743 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1745 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1748 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1750 ct
->c_subtype
= subtype
;
1752 ctinit
= get_ct_init (ct
->c_type
);
1754 typename
= ct_type_str (type
);
1755 subtypename
= ct_subtype_str (type
, subtype
);
1759 int found_boundary
= 1;
1761 while (found_boundary
&& serial
< 1000000) {
1764 /* Ensure that the boundary doesn't appear in the decoded
1766 if (new_part
->c_cefile
.ce_file
) {
1767 if ((found_boundary
=
1768 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1769 new_part
->c_cefile
.ce_file
,
1770 boundary
)) == NOTOK
) {
1775 /* Ensure that the boundary doesn't appear in the encoded
1777 if (! found_boundary
&& new_part
->c_file
) {
1778 if ((found_boundary
=
1779 boundary_in_content (&new_part
->c_fp
,
1781 boundary
)) == NOTOK
) {
1786 if (found_boundary
) {
1787 /* Try a slightly different boundary. */
1792 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1794 concat (boundary_prefix
,
1795 FENDNULL(first_alt
->c_partno
),
1796 "-", buffer2
, NULL
);
1800 if (found_boundary
) {
1801 inform("giving up trying to find a unique boundary");
1806 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1807 boundary
, "\"", NULL
);
1809 /* Load c_first_hf and c_last_hf. */
1810 transfer_noncontent_headers (first_alt
, ct
);
1811 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1814 /* Load c_partno. */
1815 if (first_alt
->c_partno
) {
1816 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1817 free (first_alt
->c_partno
);
1818 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1819 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1821 first_alt
->c_partno
= mh_xstrdup ("1");
1822 new_part
->c_partno
= mh_xstrdup ("2");
1826 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1827 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1830 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1831 "boundary", boundary
, 0);
1835 p
->mp_next
->mp_next
= NULL
;
1836 p
->mp_next
->mp_part
= first_alt
;
1839 m
->mp_start
= concat (boundary
, "\n", NULL
);
1840 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1856 * Check that the boundary does not appear in the content.
1859 boundary_in_content (FILE **fp
, char *file
, const char *boundary
)
1861 char buffer
[NMH_BUFSIZ
];
1863 bool found_boundary
= false;
1865 /* free_content() will close *fp if we fopen it here. */
1866 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1867 advise (file
, "unable to open %s for reading", file
);
1871 fseeko (*fp
, 0L, SEEK_SET
);
1872 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1873 if (find_str (buffer
, bytes_read
, boundary
)) {
1874 found_boundary
= true;
1879 return found_boundary
;
1884 * Remove all non-Content headers.
1887 transfer_noncontent_headers (CT old
, CT
new)
1891 hp_prev
= hp
= old
->c_first_hf
;
1895 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, LEN(XXX_FIELD_PRF
))) {
1896 if (hp
== old
->c_last_hf
) {
1897 if (hp
== old
->c_first_hf
) {
1898 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1900 hp_prev
->next
= NULL
;
1901 old
->c_last_hf
= hp_prev
;
1904 if (hp
== old
->c_first_hf
) {
1905 old
->c_first_hf
= next
;
1907 hp_prev
->next
= next
;
1911 /* Put node hp in the new CT. */
1912 if (new->c_first_hf
== NULL
) {
1913 new->c_first_hf
= hp
;
1915 new->c_last_hf
->next
= hp
;
1917 new->c_last_hf
= hp
;
1919 /* A Content- header, leave in old. */
1932 set_ct_type (CT ct
, int type
, int subtype
, int encoding
)
1934 char *typename
= ct_type_str (type
);
1935 char *subtypename
= ct_subtype_str (type
, subtype
);
1936 /* E.g, " text/plain" */
1937 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1938 /* E.g, " text/plain\n" */
1939 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1940 bool found_content_type
= false;
1942 const char *cp
= NULL
;
1946 /* Update/add Content-Type header field. */
1947 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1948 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1949 found_content_type
= true;
1951 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1952 ? concat (type_subtypename
, cp
, "\n", NULL
)
1953 : mh_xstrdup (name_plus_nl
);
1956 if (! found_content_type
) {
1957 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
1958 (cp
= strchr (ct
->c_ctline
, ';'))
1959 ? concat (type_subtypename
, cp
, "\n", NULL
)
1960 : mh_xstrdup (name_plus_nl
));
1963 /* Some of these might not be used, but set them anyway. */
1965 ? concat (type_subtypename
, cp
, NULL
)
1966 : concat (type_subtypename
, NULL
);
1967 free (ct
->c_ctline
);
1968 ct
->c_ctline
= ctline
;
1969 /* Leave other ctinfo members as they were. */
1970 free (ct
->c_ctinfo
.ci_type
);
1971 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1972 free (ct
->c_ctinfo
.ci_subtype
);
1973 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1975 ct
->c_subtype
= subtype
;
1977 free (name_plus_nl
);
1978 free (type_subtypename
);
1980 status
= set_ce (ct
, encoding
);
1987 * It's not necessary to update the charset parameter of a Content-Type
1988 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1989 * (content) was originally in the specified charset, "and will be in
1990 * that character set again after decoding."
1993 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
1997 int lf_line_endings
= 0;
1999 switch (ct
->c_type
) {
2000 case CT_MULTIPART
: {
2001 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2004 /* Should check to see if the body for this part is encoded?
2005 For now, it gets passed along as-is by InitMultiPart(). */
2006 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2007 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
2014 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2015 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2017 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
2023 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2028 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
2030 switch (ct
->c_encoding
) {
2035 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
2036 const char *reason
= NULL
;
2038 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
2039 && encoding
!= CE_BINARY
) {
2040 /* The decoding isn't acceptable so discard it.
2041 Leave status as OK to allow other transformations. */
2043 report (NULL
, ct
->c_partno
, ct
->c_file
,
2044 "will not decode%s because it is binary (%s)",
2046 : (FENDNULL(ct
->c_ctline
)),
2049 (void) m_unlink (ct
->c_cefile
.ce_file
);
2050 free (ct
->c_cefile
.ce_file
);
2051 ct
->c_cefile
.ce_file
= NULL
;
2052 } else if (ct
->c_encoding
== CE_QUOTED
&&
2053 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2054 /* The decoding isn't acceptable so discard it.
2055 Leave status as OK to allow other transformations. */
2057 report (NULL
, ct
->c_partno
, ct
->c_file
,
2058 "will not decode%s because it is 8bit",
2060 : (FENDNULL(ct
->c_ctline
)));
2062 (void) m_unlink (ct
->c_cefile
.ce_file
);
2063 free (ct
->c_cefile
.ce_file
);
2064 ct
->c_cefile
.ce_file
= NULL
;
2068 if (ct_encoding
== CE_BINARY
) {
2070 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2075 if (set_ce (ct
, enc
) == OK
) {
2078 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2079 FENDNULL(ct
->c_ctline
));
2081 if (lf_line_endings
) {
2082 strip_crs (ct
, message_mods
);
2095 if (lf_line_endings
) {
2096 strip_crs (ct
, message_mods
);
2111 * Determine if the part with type[/subtype] should be decoded, according to
2112 * decodetypes (which came from the -decodetypes switch).
2115 should_decode(const char *decodetypes
, const char *type
, const char *subtype
)
2117 /* Quick search for matching type[/subtype] in decodetypes: bracket
2118 decodetypes with commas, then search for ,type, and ,type/subtype, in
2121 bool found_match
= false;
2122 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2123 char *delimited_type
= concat(",", type
, ",", NULL
);
2125 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2127 } else if (subtype
!= NULL
) {
2128 char *delimited_type_subtype
=
2129 concat(",", type
, "/", subtype
, ",", NULL
);
2131 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2134 free(delimited_type_subtype
);
2137 free(delimited_type
);
2138 free(delimited_decodetypes
);
2145 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2146 * if it has any NUL characters, a CR not followed by a LF, or lines
2147 * greater than 998 characters in length. If binary, reason is set
2148 * to a string explaining why.
2151 content_encoding (CT ct
, const char **reason
)
2153 CE ce
= &ct
->c_cefile
;
2154 int encoding
= CE_7BIT
;
2157 size_t line_len
= 0;
2158 char buffer
[NMH_BUFSIZ
];
2161 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2162 advise (ce
->ce_file
, "unable to open for reading");
2166 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2167 while (encoding
!= CE_BINARY
&&
2168 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2171 int last_char_was_cr
= 0;
2173 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2174 if (*cp
== '\0' || ++line_len
> 998 ||
2175 (*cp
!= '\n' && last_char_was_cr
)) {
2176 encoding
= CE_BINARY
;
2178 *reason
= "null character";
2179 } else if (line_len
> 998) {
2180 *reason
= "line length > 998";
2181 } else if (*cp
!= '\n' && last_char_was_cr
) {
2182 *reason
= "CR not followed by LF";
2184 /* Should not reach this. */
2191 } else if (! isascii ((unsigned char) *cp
)) {
2195 last_char_was_cr
= *cp
== '\r';
2201 } /* else should never happen */
2208 * Strip carriage returns from content.
2211 strip_crs (CT ct
, int *message_mods
)
2213 char *charset
= content_charset (ct
);
2216 /* Only strip carriage returns if content is ASCII or another
2217 charset that has the same readily recognizable CR followed by a
2218 LF. We can include UTF-8 here because if the high-order bit of
2219 a UTF-8 byte is 0, then it must be a single-byte ASCII
2221 if (! strcasecmp (charset
, "US-ASCII") ||
2222 ! strcasecmp (charset
, "UTF-8") ||
2223 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2224 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2229 bool has_crs
= false;
2230 bool opened_input_file
= false;
2232 if (ct
->c_cefile
.ce_file
) {
2233 file
= &ct
->c_cefile
.ce_file
;
2234 fp
= &ct
->c_cefile
.ce_fp
;
2236 } else if (ct
->c_file
) {
2239 begin
= (size_t) ct
->c_begin
;
2240 end
= (size_t) ct
->c_end
;
2241 } /* else don't know where the content is */
2243 if (file
&& *file
&& fp
) {
2245 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2246 advise (*file
, "unable to open for reading");
2249 opened_input_file
= true;
2255 char buffer
[NMH_BUFSIZ
];
2257 size_t bytes_to_read
=
2258 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2260 fseeko (*fp
, begin
, SEEK_SET
);
2261 while ((bytes_read
= fread (buffer
, 1,
2262 min (bytes_to_read
, sizeof buffer
),
2264 /* Look for CR followed by a LF. This is supposed to
2265 be text so there should be LF's. If not, don't
2266 modify the content. */
2269 bool last_char_was_cr
= false;
2271 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2273 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2274 if (*cp
== '\n' && last_char_was_cr
) {
2279 last_char_was_cr
= *cp
== '\r';
2285 char *stripped_content_file
;
2286 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2288 if (tempfile
== NULL
) {
2289 die("unable to create temporary file in %s",
2292 stripped_content_file
= mh_xstrdup (tempfile
);
2294 /* Strip each CR before a LF from the content. */
2295 fseeko (*fp
, begin
, SEEK_SET
);
2296 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2300 bool last_char_was_cr
= false;
2302 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2304 last_char_was_cr
= true;
2305 } else if (last_char_was_cr
) {
2307 if (write (fd
, "\r", 1) < 0) {
2308 advise (tempfile
, "CR write");
2311 if (write (fd
, cp
, 1) < 0) {
2312 advise (tempfile
, "write");
2314 last_char_was_cr
= false;
2316 if (write (fd
, cp
, 1) < 0) {
2317 advise (tempfile
, "write");
2319 last_char_was_cr
= false;
2325 inform("unable to write temporary file %s, continuing...",
2326 stripped_content_file
);
2327 (void) m_unlink (stripped_content_file
);
2328 free(stripped_content_file
);
2331 /* Replace the decoded file with the converted one. */
2332 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2333 (void) m_unlink (ct
->c_cefile
.ce_file
);
2335 free(ct
->c_cefile
.ce_file
);
2336 ct
->c_cefile
.ce_file
= stripped_content_file
;
2337 ct
->c_cefile
.ce_unlink
= 1;
2341 report (NULL
, ct
->c_partno
,
2342 begin
== 0 && end
== 0 ? "" : *file
,
2348 if (opened_input_file
) {
2362 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2363 * of the part C-T-E's.
2368 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2370 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2371 least_restrictive_enc
!= CE_7BIT
) {
2372 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2374 bool found_cte
= false;
2376 /* Update/add Content-Transfer-Encoding header field. */
2377 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2378 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2385 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2392 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2396 least_restrictive_encoding (CT ct
)
2398 int encoding
= CE_UNKNOWN
;
2400 switch (ct
->c_type
) {
2401 case CT_MULTIPART
: {
2402 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2405 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2406 const int part_encoding
=
2407 least_restrictive_encoding (part
->mp_part
);
2409 if (less_restrictive (encoding
, part_encoding
)) {
2410 encoding
= part_encoding
;
2417 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2418 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2419 const int part_encoding
=
2420 least_restrictive_encoding (e
->eb_content
);
2422 if (less_restrictive (encoding
, part_encoding
)) {
2423 encoding
= part_encoding
;
2429 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2430 encoding
= ct
->c_encoding
;
2439 * Return whether the second encoding is less restrictive than the first, where
2440 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2441 * CE_BINARY is less restrictive than CE_8BIT and
2442 * CE_8BIT is less restrictive than CE_7BIT.
2445 less_restrictive (int encoding
, int second_encoding
)
2447 switch (second_encoding
) {
2449 return encoding
!= CE_BINARY
;
2451 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2453 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2454 encoding
!= CE_7BIT
;
2462 * Convert character set of each part.
2465 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
)
2469 switch (ct
->c_type
) {
2471 if (ct
->c_subtype
== TEXT_PLAIN
) {
2472 status
= convert_charset (ct
, dest_charset
, message_mods
);
2475 char *ct_charset
= content_charset (ct
);
2477 report (NULL
, ct
->c_partno
, ct
->c_file
,
2478 "convert %s to %s", ct_charset
, dest_charset
);
2482 char *ct_charset
= content_charset (ct
);
2484 report ("iconv", ct
->c_partno
, ct
->c_file
,
2485 "failed to convert %s to %s", ct_charset
, dest_charset
);
2491 case CT_MULTIPART
: {
2492 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2495 /* Should check to see if the body for this part is encoded?
2496 For now, it gets passed along as-is by InitMultiPart(). */
2497 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2499 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2505 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2506 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2509 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2522 * Fix various problems that aren't handled elsewhere. These
2523 * are fixed unconditionally: there are no switches to disable
2524 * them. Currently, "problems" are these:
2525 * 1) remove extraneous semicolon at the end of a header parameter list
2526 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2527 * filename parameters in Content-Type and Content-Disposition
2528 * headers, respectively.
2531 fix_always (CT ct
, int *message_mods
)
2535 switch (ct
->c_type
) {
2536 case CT_MULTIPART
: {
2537 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2540 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2541 status
= fix_always (part
->mp_part
, message_mods
);
2547 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2548 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2550 status
= fix_always (e
->eb_content
, message_mods
);
2557 if (ct
->c_first_hf
) {
2558 fix_filename_encoding (ct
);
2561 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2562 size_t len
= strlen (hf
->value
);
2564 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2565 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2566 /* Only do this for Content-Type and
2567 Content-Disposition fields because those are the
2568 only headers that parse_mime() warns about. */
2572 /* whitespace following a trailing ';' will be nuked as well */
2573 if (hf
->value
[len
- 1] == '\n') {
2574 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2575 if (len
-- == 0) { break; }
2579 if (hf
->value
[len
- 2] == ';') {
2580 /* Remove trailing ';' from parameter value. */
2581 hf
->value
[len
- 2] = '\n';
2582 hf
->value
[len
- 1] = '\0';
2584 /* Also, if Content-Type parameter, remove trailing ';'
2585 from ct->c_ctline. This probably isn't necessary
2587 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2588 size_t l
= strlen(ct
->c_ctline
) - 1;
2589 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2590 ct
->c_ctline
[l
] == ';') {
2591 ct
->c_ctline
[l
--] = '\0';
2592 if (l
== 0) { break; }
2598 report (NULL
, ct
->c_partno
, ct
->c_file
,
2599 "remove trailing ; from %s parameter value",
2611 * Factor out common code for loops in fix_filename_encoding().
2614 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
)
2618 if (has_prefix(value
, "=?") && has_suffix(value
, "?=")) {
2619 /* Looks like an RFC 2047 encoded parameter. */
2620 char decoded
[PATH_MAX
+ 1];
2622 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2623 /* Encode using RFC 2231. */
2624 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2627 inform("failed to decode %s parameter %s", name
, value
);
2636 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2637 * filename parameters in Content-Type and Content-Disposition
2638 * headers, respectively.
2641 fix_filename_encoding (CT ct
)
2647 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2648 if (pm
->pm_name
&& pm
->pm_value
&&
2649 strcasecmp (pm
->pm_name
, "name") == 0) {
2650 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2651 &ct
->c_ctinfo
.ci_first_pm
,
2652 &ct
->c_ctinfo
.ci_last_pm
);
2656 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2657 if (pm
->pm_name
&& pm
->pm_value
&&
2658 strcasecmp (pm
->pm_name
, "filename") == 0) {
2659 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2665 /* Fix hf values to correspond. */
2666 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2667 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2669 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2670 field
= TYPE_HEADER
;
2671 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2672 field
= DISPO_HEADER
;
2675 if (field
!= OTHER
) {
2676 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2678 if (semicolon_loc
) {
2680 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2681 const char *const params
=
2683 field
== TYPE_HEADER
2684 ? ct
->c_ctinfo
.ci_first_pm
2685 : ct
->c_dispo_first
,
2687 const char *const new_params
= concat (params
, "\n", NULL
);
2689 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2690 free((void *)new_params
); /* Cast away const. Sigh. */
2691 free((void *)params
);
2693 inform("did not find semicolon in %s:%s\n",
2694 hf
->name
, hf
->value
);
2704 * Output content in input file to output file.
2707 write_content (CT ct
, const char *input_filename
, char *outfile
, FILE *outfp
,
2708 int modify_inplace
, int message_mods
)
2712 if (modify_inplace
) {
2713 if (message_mods
> 0) {
2714 if ((status
= output_message_fp (ct
, outfp
, outfile
)) == OK
) {
2715 char *infile
= input_filename
2716 ? mh_xstrdup (input_filename
)
2717 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2719 if (remove_file (infile
) == OK
) {
2720 if (rename (outfile
, infile
)) {
2721 /* Rename didn't work, possibly because of an
2722 attempt to rename across filesystems. Try
2723 brute force copy. */
2724 int old
= open (outfile
, O_RDONLY
);
2726 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2729 if (old
!= -1 && new != -1) {
2730 char buffer
[NMH_BUFSIZ
];
2732 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2734 if (write (new, buffer
, i
) != i
) {
2740 if (new != -1) { close (new); }
2741 if (old
!= -1) { close (old
); }
2742 (void) m_unlink (outfile
);
2745 /* The -file argument processing used path() to
2746 expand filename to absolute path. */
2747 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2749 inform("unable to rename %s %s to %s, continuing...",
2750 file
? "file" : "message", outfile
,
2756 inform("unable to remove input file %s, "
2757 "not modifying it, continuing...", infile
);
2758 (void) m_unlink (outfile
);
2767 /* No modifications and didn't need the tmp outfile. */
2768 (void) m_unlink (outfile
);
2771 /* Output is going to some file. Produce it whether or not
2772 there were modifications. */
2773 status
= output_message_fp (ct
, outfp
, outfile
);
2782 * parse_mime() does not set lf_line_endings in struct text, so use this
2783 * function to do it. It touches the parts the decodetypes identifies.
2786 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
)
2788 switch (ct
->c_type
) {
2789 case CT_MULTIPART
: {
2790 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2793 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2794 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2800 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2801 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2803 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2808 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2809 if (ct
->c_ctparams
== NULL
) {
2810 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2812 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2819 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2820 * use the standard MH backup file.
2823 remove_file (const char *file
)
2826 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2827 int status
= system (rmm_command
);
2830 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2832 /* This is OK for a non-message file, it still uses the
2833 BACKUP_PREFIX form. The backup file will be in the same
2834 directory as file. */
2835 return rename (file
, m_backup (file
));
2840 * Output formatted message to user.
2843 report (char *what
, char *partno
, char *filename
, char *message
, ...)
2849 va_start (args
, message
);
2850 fmt
= concat (filename
, partno
? " part " : ", ",
2851 FENDNULL(partno
), partno
? ", " : "", message
, NULL
);
2853 advertise (what
, NULL
, fmt
, args
);
2866 fprintf (stderr
, "\n");