1 /* mhfixmsg.c -- rewrite a message with various transformations
3 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
4 * See the COPYRIGHT file in the root directory of the nmh
5 * distribution for complete copyright information.
9 #include "sbr/m_convert.h"
10 #include "sbr/m_backup.h"
11 #include "sbr/getfolder.h"
12 #include "sbr/folder_read.h"
13 #include "sbr/context_save.h"
14 #include "sbr/context_replace.h"
15 #include "sbr/context_find.h"
16 #include "sbr/readconfig.h"
17 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
22 #include "h/fmt_scan.h"
24 #include "h/mhparse.h"
27 #include "h/signals.h"
28 #include "sbr/m_maildir.h"
29 #include "sbr/m_mktemp.h"
30 #include "sbr/mime_type.h"
34 #include "mhshowsbr.h"
37 #define MHFIXMSG_SWITCHES \
38 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
39 X("nodecodetext", 0, NDECODETEXTSW) \
40 X("decodetypes", 0, DECODETYPESW) \
41 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
42 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
43 X("textcharset", 0, TEXTCHARSETSW) \
44 X("notextcharset", 0, NTEXTCHARSETSW) \
45 X("reformat", 0, REFORMATSW) \
46 X("noreformat", 0, NREFORMATSW) \
47 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
48 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
49 X("fixboundary", 0, FIXBOUNDARYSW) \
50 X("nofixboundary", 0, NFIXBOUNDARYSW) \
51 X("fixcte", 0, FIXCOMPOSITECTESW) \
52 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
53 X("fixtype mimetype", 0, FIXTYPESW) \
54 X("file file", 0, FILESW) \
55 X("outfile file", 0, OUTFILESW) \
56 X("rmmproc program", 0, RPROCSW) \
57 X("normmproc", 0, NRPRCSW) \
58 X("changecur", 0, CHGSW) \
59 X("nochangecur", 0, NCHGSW) \
60 X("verbose", 0, VERBSW) \
61 X("noverbose", 0, NVERBSW) \
62 X("version", 0, VERSIONSW) \
63 X("help", 0, HELPSW) \
65 #define X(sw, minchars, id) id,
66 DEFINE_SWITCH_ENUM(MHFIXMSG
);
69 #define X(sw, minchars, id) { sw, minchars, id },
70 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
75 int debugsw
; /* Needed by mhparse.c. */
77 #define quitser pipeser
82 typedef struct fix_transformations
{
90 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
93 } fix_transformations
;
95 static int mhfixmsgsbr (CT
*, char *, const fix_transformations
*,
96 FILE **, char *, FILE **);
97 static int fix_boundary (CT
*, int *);
98 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
99 static int get_multipart_boundary (CT
, char **);
100 static int replace_boundary (CT
, char *, char *);
101 static int fix_types (CT
, svector_t
, int *);
102 static char *replace_substring (char **, const char *, const char *);
103 static char *remove_parameter (char *, const char *);
104 static int fix_composite_cte (CT
, int *);
105 static int set_ce (CT
, int);
106 static int ensure_text_plain (CT
*, CT
, int *, int);
107 static int find_textplain_sibling (CT
, int, int *);
108 static int insert_new_text_plain_part (CT
, int, CT
);
109 static CT
build_text_plain_part (CT
);
110 static int insert_into_new_mp_alt (CT
*, int *);
111 static CT
divide_part (CT
);
112 static void copy_ctinfo (CI
, CI
);
113 static int decode_part (CT
);
114 static int reformat_part (CT
, char *, char *, char *, int);
115 static CT
build_multipart_alt (CT
, CT
, int, int);
116 static int boundary_in_content (FILE **, char *, const char *);
117 static void transfer_noncontent_headers (CT
, CT
);
118 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
119 static int decode_text_parts (CT
, int, const char *, int *);
120 static int should_decode(const char *, const char *, const char *);
121 static int content_encoding (CT
, const char **);
122 static int strip_crs (CT
, int *);
123 static void update_cte (CT
);
124 static int least_restrictive_encoding (CT
) PURE
;
125 static int less_restrictive (int, int);
126 static int convert_charsets (CT
, char *, int *);
127 static int fix_always (CT
, int *);
128 static int fix_filename_param (char *, char *, PM
*, PM
*);
129 static int fix_filename_encoding (CT
);
130 static int write_content (CT
, const char *, char *, FILE *, int, int);
131 static void set_text_ctparams(CT
, char *, int);
132 static int remove_file (const char *);
133 static void report (char *, char *, char *, char *, ...)
135 static void pipeser (int);
139 main (int argc
, char **argv
)
142 char *cp
, *file
= NULL
, *folder
= NULL
;
143 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
144 char **argp
, **arguments
;
145 struct msgs_array msgs
= { 0, 0, NULL
};
146 struct msgs
*mp
= NULL
;
148 FILE *fp
, *infp
= NULL
, *outfp
= NULL
;
149 bool using_stdin
= false;
152 fix_transformations fx
;
153 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
155 fx
.replacetextplain
= 0;
156 fx
.decodetext
= CE_8BIT
;
157 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
158 fx
.lf_line_endings
= 0;
159 fx
.textcharset
= NULL
;
161 if (nmh_init(argv
[0], true, false)) { return 1; }
163 arguments
= getarguments (invo_name
, argc
, argv
, 1);
169 while ((cp
= *argp
++)) {
171 switch (smatch (++cp
, switches
)) {
173 ambigsw (cp
, switches
);
176 die("-%s unknown", cp
);
179 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
181 print_help (buf
, switches
, 1);
184 print_version(invo_name
);
188 if (! (cp
= *argp
++) || *cp
== '-') {
189 die("missing argument to %s", argp
[-2]);
191 if (! strcasecmp (cp
, "8bit")) {
192 fx
.decodetext
= CE_8BIT
;
193 } else if (! strcasecmp (cp
, "7bit")) {
194 fx
.decodetext
= CE_7BIT
;
195 } else if (! strcasecmp (cp
, "binary")) {
196 fx
.decodetext
= CE_BINARY
;
198 die("invalid argument to %s", argp
[-2]);
205 if (! (cp
= *argp
++) || *cp
== '-') {
206 die("missing argument to %s", argp
[-2]);
210 case CRLFLINEBREAKSSW
:
211 fx
.lf_line_endings
= 0;
213 case NCRLFLINEBREAKSSW
:
214 fx
.lf_line_endings
= 1;
217 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
218 die("missing argument to %s", argp
[-2]);
231 case FIXCOMPOSITECTESW
:
232 fx
.fixcompositecte
= 1;
234 case NFIXCOMPOSITECTESW
:
235 fx
.fixcompositecte
= 0;
238 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
239 die("missing argument to %s", argp
[-2]);
241 if (! strncasecmp (cp
, "multipart/", 10) ||
242 ! strncasecmp (cp
, "message/", 8))
243 die("-fixtype %s not allowed", cp
);
244 if (! strchr (cp
, '/'))
245 die("-fixtype requires type/subtype");
246 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
247 svector_push_back (fx
.fixtypes
, cp
);
255 case REPLACETEXTPLAINSW
:
256 fx
.replacetextplain
= 1;
258 case NREPLACETEXTPLAINSW
:
259 fx
.replacetextplain
= 0;
262 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
263 die("missing argument to %s", argp
[-2]);
265 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
268 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
269 die("missing argument to %s", argp
[-2]);
271 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
274 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
275 die("missing argument to %s", argp
[-2]);
295 if (*cp
== '+' || *cp
== '@') {
297 die("only one folder at a time!");
298 folder
= pluspath (cp
);
301 /* Interpret a full path as a filename, not a message. */
302 file
= mh_xstrdup (cp
);
304 app_msgarg (&msgs
, cp
);
309 SIGNAL (SIGQUIT
, quitser
);
310 SIGNAL (SIGPIPE
, pipeser
);
313 * Read the standard profile setup
315 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
316 readconfig(NULL
, fp
, cp
, 0);
320 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= true;
321 suppress_extraneous_trailing_semicolon_warning
= true;
323 if (! context_find ("path")) {
324 free (path ("./", TFOLDER
));
327 if (file
&& msgs
.size
) {
328 die("cannot specify msg and file at same time!");
332 /* Open the outfile now, so we don't have to risk opening it
333 after running out of fds. */
334 if (strcmp (outfile
, "-") == 0) {
336 } else if ((outfp
= fopen (outfile
, "w")) == NULL
) {
337 adios (outfile
, "unable to open for writing");
342 * check if message is coming from file
345 /* If file is stdin, create a tmp file name before parse_mime()
346 has a chance, because it might put in on a different
347 filesystem than the output file. Instead, put it in the
348 user's preferred tmp directory. */
351 if (! strcmp ("-", file
)) {
357 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
358 die("unable to create temporary file in %s",
362 file
= mh_xstrdup (cp
);
363 cpydata (STDIN_FILENO
, fd
, "-", file
);
367 (void) m_unlink (file
);
368 die("failed to write temporary file");
372 cts
= mh_xcalloc(2, sizeof *cts
);
375 if ((ct
= parse_mime (file
))) {
376 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
379 inform("unable to parse message from file %s", file
);
382 /* If there's an outfile, pass the input message unchanged, so the
383 message won't get dropped from a pipeline. */
385 /* Something went wrong. Output might be expected, such as if
386 this were run as a filter. Just copy the input to the
388 if ((infp
= fopen (file
, "r")) == NULL
) {
389 adios (file
, "unable to open for reading");
392 if (copy_input_to_output (file
, infp
, outfile
, outfp
) != OK
) {
393 inform("unable to copy message to %s, "
394 "it might be lost\n", outfile
);
403 * message(s) are coming from a folder
408 app_msgarg(&msgs
, "cur");
411 folder
= getfolder (1);
413 maildir
= mh_xstrdup(m_maildir (folder
));
415 /* chdir so that error messages, esp. from MIME parser, just
416 refer to the message and not its path. */
417 if (chdir (maildir
) == NOTOK
) {
418 adios (maildir
, "unable to change directory to");
421 /* read folder and create message structure */
422 if (! (mp
= folder_read (folder
, 1))) {
423 die("unable to read folder %s", folder
);
426 /* check for empty folder */
427 if (mp
->nummsg
== 0) {
428 die("no messages in %s", folder
);
431 /* parse all the message ranges/sequences and set SELECTED */
432 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
433 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
436 seq_setprev (mp
); /* set the previous-sequence */
438 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
441 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
442 if (is_selected(mp
, msgnum
)) {
443 char *msgnam
= m_name (msgnum
);
445 if ((ct
= parse_mime (msgnam
))) {
446 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
449 inform("unable to parse message %s", msgnam
);
452 /* If there's an outfile, pass the input message
453 unchanged, so the message won't get dropped from a
456 /* Something went wrong. Output might be expected,
457 such as if this were run as a filter. Just copy
458 the input to the output. */
459 /* Can't use path() here because 1) it might have been
460 called before and it caches the pwd, and 2) we call
461 chdir() after that. */
462 char *input_filename
=
463 concat (maildir
, "/", msgnam
, NULL
);
465 if ((infp
= fopen (input_filename
, "r")) == NULL
) {
466 adios (input_filename
,
467 "unable to open for reading");
470 if (copy_input_to_output (input_filename
, infp
,
471 outfile
, outfp
) != OK
) {
472 inform("unable to copy message to %s, "
473 "it might be lost\n", outfile
);
478 free (input_filename
);
485 seq_setcur (mp
, mp
->hghsel
); /* update current message */
487 seq_save (mp
); /* synchronize sequences */
488 context_replace (pfolder
, folder
);/* update current folder */
489 context_save (); /* save the context file */
493 for (ctp
= cts
; *ctp
; ++ctp
) {
495 mhfixmsgsbr (ctp
, maildir
, &fx
, &infp
, outfile
, &outfp
) == OK
501 (void) m_unlink (file
);
504 /* Just calling m_backup() unlinks the backup file. */
505 (void) m_backup (file
);
516 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
517 if (infp
) { fclose (infp
); } /* even if stdin */
518 if (outfp
) { fclose (outfp
); } /* even if stdout */
524 done (status
== OK
? 0 : 1);
530 * Apply transformations to one message.
533 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
534 FILE **infp
, char *outfile
, FILE **outfp
)
536 /* Store input filename in case one of the transformations, i.e.,
537 fix_boundary(), rewrites to a tmp file. */
538 char *input_filename
= maildir
539 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
540 : mh_xstrdup ((*ctp
)->c_file
);
541 bool modify_inplace
= false;
542 int message_mods
= 0;
545 /* Though the input file won't need to be opened if everything goes
546 well, do it here just in case there's a failure, and that failure is
547 running out of file descriptors. */
548 if ((*infp
= fopen (input_filename
, "r")) == NULL
) {
549 adios (input_filename
, "unable to open for reading");
552 if (outfile
== NULL
) {
553 modify_inplace
= true;
555 if ((*ctp
)->c_file
) {
557 /* outfp will be closed by the caller */
558 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, outfp
)) ==
560 die("unable to create temporary file in %s",
563 outfile
= mh_xstrdup (tempfile
);
565 die("missing both input and output filenames\n");
567 } /* else *outfp was defined by caller */
569 reverse_alternative_parts (*ctp
);
570 status
= fix_always (*ctp
, &message_mods
);
571 if (status
== OK
&& fx
->fixboundary
) {
572 status
= fix_boundary (ctp
, &message_mods
);
574 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
575 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
577 if (status
== OK
&& fx
->fixcompositecte
) {
578 status
= fix_composite_cte (*ctp
, &message_mods
);
580 if (status
== OK
&& fx
->reformat
) {
582 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
584 if (status
== OK
&& fx
->decodetext
) {
585 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
589 if (status
== OK
&& fx
->textcharset
!= NULL
) {
590 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
593 if (status
== OK
&& ! (*ctp
)->c_umask
) {
594 /* Set the umask for the contents file. This currently
595 isn't used but just in case it is in the future. */
598 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
599 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
601 (*ctp
)->c_umask
= ~m_gmprot();
606 * Write the content to a file
609 status
= write_content (*ctp
, input_filename
, outfile
, *outfp
,
610 modify_inplace
, message_mods
);
611 } else if (! modify_inplace
) {
612 /* Something went wrong. Output might be expected, such
613 as if this were run as a filter. Just copy the input
615 if (copy_input_to_output (input_filename
, *infp
, outfile
,
617 inform("unable to copy message to %s, it might be lost\n",
622 if (modify_inplace
) {
623 if (status
!= OK
) { (void) m_unlink (outfile
); }
630 free (input_filename
);
637 * Copy input message to output. Assumes not modifying in place, so this
638 * might be running as part of a pipeline.
641 copy_input_to_output (const char *input_filename
, FILE *infp
,
642 const char *output_filename
, FILE *outfp
)
644 int in
= fileno (infp
);
645 int out
= fileno (outfp
);
648 if (in
!= -1 && out
!= -1) {
649 cpydata (in
, out
, input_filename
, output_filename
);
659 * Fix mismatched outer level boundary.
662 fix_boundary (CT
*ct
, int *message_mods
)
664 struct multipart
*mp
;
667 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
668 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
671 * 1) Get boundary at end of part.
672 * 2) Get boundary at beginning of part and compare to the end-of-part
674 * 3) Write out contents of ct to tmp file, replacing boundary in
675 * header with boundary from part. Set c_unlink to 1.
677 * 5) Call parse_mime() on the tmp file, replacing ct.
680 if (mp
&& mp
->mp_start
) {
683 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
686 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
687 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
688 char *filename
= mh_xstrdup ((*ct
)->c_file
);
692 if ((fixed_ct
= parse_mime (fixed
))) {
698 report (NULL
, NULL
, filename
,
699 "fix multipart boundary");
703 inform("unable to parse fixed part");
708 inform("unable to replace broken boundary");
712 inform("unable to create temporary file in %s",
717 free (part_boundary
);
719 /* Couldn't fix the boundary. Report failure so that mhfixmsg
720 doesn't modify the message. */
724 /* No multipart struct, even though the content type is
725 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
736 * Find boundary at end of multipart.
739 get_multipart_boundary (CT ct
, char **part_boundary
)
741 char buffer
[NMH_BUFSIZ
];
742 char *end_boundary
= NULL
;
743 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
744 ? (off_t
) (ct
->c_end
- sizeof buffer
)
745 : (off_t
) ct
->c_begin
;
749 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
750 be big enough, even if it's just 1024, to make that unlikely. */
752 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
753 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
754 advise (ct
->c_file
, "unable to open for reading");
758 /* Get boundary at end of multipart. */
759 while (begin
>= (off_t
) ct
->c_begin
) {
760 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
761 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
762 char *cp
= rfind_str (buffer
, bytes_read
, "--");
767 /* Trim off trailing "--" and anything beyond. */
769 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
770 if (strlen (end
) > 3 && *end
++ == '\n' &&
771 *end
++ == '-' && *end
++ == '-') {
772 end_boundary
= mh_xstrdup (end
);
779 if (end_boundary
|| begin
<= (off_t
) (ct
->c_begin
+ sizeof buffer
))
781 begin
-= sizeof buffer
;
784 /* Get boundary at beginning of multipart. */
786 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
787 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
788 if (bytes_read
>= strlen (end_boundary
)) {
789 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
791 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
792 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
797 /* The start and end boundaries didn't match, or the
798 start boundary doesn't begin with "\n--" (or "--"
799 if at the beginning of buffer). Keep trying. */
813 *part_boundary
= end_boundary
;
815 *part_boundary
= NULL
;
824 * Open and copy ct->c_file to file, replacing the multipart boundary.
827 replace_boundary (CT ct
, char *file
, char *boundary
)
831 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
833 m_getfld_state_t gstate
;
836 if (ct
->c_file
== NULL
) {
837 inform("missing input filename");
841 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
842 advise (ct
->c_file
, "unable to open for reading");
846 if ((fpout
= fopen (file
, "w")) == NULL
) {
848 advise (file
, "unable to open for writing");
852 gstate
= m_getfld_state_init(fpin
);
853 for (compnum
= 1;;) {
854 int bufsz
= (int) sizeof buf
;
856 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
861 /* get copies of the buffers */
862 np
= mh_xstrdup (name
);
863 vp
= mh_xstrdup (buf
);
865 /* if necessary, get rest of field */
866 while (state
== FLDPLUS
) {
868 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
869 vp
= add (buf
, vp
); /* add to previous value */
872 if (strcasecmp (TYPE_FIELD
, np
)) {
873 fprintf (fpout
, "%s:%s", np
, vp
);
875 char *new_ctline
, *new_params
;
877 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
878 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
881 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
882 ct
->c_ctinfo
.ci_subtype
, NULL
);
883 new_params
= output_params(LEN(TYPE_FIELD
) +
884 strlen(new_ctline
) + 1,
885 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
886 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
887 FENDNULL(new_params
));
899 /* buf will have a terminating NULL, skip it. */
900 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
901 advise (file
, "fwrite");
910 inform("message format error in component #%d", compnum
);
915 inform("getfld() returned %d", state
);
923 m_getfld_state_destroy (&gstate
);
932 * Fix Content-Type header to reflect the content of its part.
935 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
)
939 switch (ct
->c_type
) {
941 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
944 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
945 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
951 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
952 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
954 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
961 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
962 for (typep
= svector_strs (fixtypes
);
963 typep
&& (type
= *typep
);
966 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
969 if (! strcasecmp (type
, type_subtype
) &&
970 decode_part (ct
) == OK
&&
971 ct
->c_cefile
.ce_file
!= NULL
) {
972 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
975 if ((cp
= strchr (ct_type_subtype
, ';'))) {
976 /* Truncate to remove any parameter list from
977 mime_type () result. */
981 if (strcasecmp (type
, ct_type_subtype
)) {
982 char *ct_type
, *ct_subtype
;
985 /* The Content-Type header does not match the
986 content, so update these struct Content
989 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
992 /* Extract type and subtype from type/subtype. */
993 ct_type
= mh_xstrdup(ct_type_subtype
);
994 if ((cp
= strchr (ct_type
, '/'))) {
996 ct_subtype
= mh_xstrdup(++cp
);
998 inform("missing / in MIME type of %s %s",
999 ct
->c_file
, ct
->c_partno
);
1004 ct
->c_type
= ct_str_type (ct_type
);
1005 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
1007 free (ct
->c_ctinfo
.ci_type
);
1008 ct
->c_ctinfo
.ci_type
= ct_type
;
1009 free (ct
->c_ctinfo
.ci_subtype
);
1010 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
1011 if (! replace_substring (&ct
->c_ctline
, type
,
1013 inform("did not find %s in %s",
1014 type
, ct
->c_ctline
);
1017 /* Update Content-Type header field. */
1018 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1019 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1020 if (replace_substring (&hf
->value
, type
,
1024 report (NULL
, ct
->c_partno
, ct
->c_file
,
1025 "change Content-Type in header "
1027 type
, ct_type_subtype
);
1031 inform("did not find %s in %s", type
, hf
->value
);
1035 free (ct_type_subtype
);
1037 free (type_subtype
);
1047 * Replace a substring, allocating space to hold the new one.
1050 replace_substring (char **str
, const char *old
, const char *new)
1054 if ((cp
= strstr (*str
, old
))) {
1055 char *remainder
= cp
+ strlen (old
);
1056 char *prefix
, *new_str
;
1059 prefix
= mh_xstrdup(*str
);
1060 *(prefix
+ (cp
- *str
)) = '\0';
1061 new_str
= concat (prefix
, new, remainder
, NULL
);
1064 new_str
= concat (new, remainder
, NULL
);
1069 return *str
= new_str
;
1077 * Remove a name=value parameter, given just its name, from a header value.
1080 remove_parameter (char *str
, const char *name
)
1082 /* It looks to me, based on the BNF in RFC 2045, than there can't
1083 be whitespace between the parameter name and the "=", or
1084 between the "=" and the parameter value. */
1085 char *param_name
= concat (name
, "=", NULL
);
1088 if ((cp
= strstr (str
, param_name
))) {
1092 /* Remove any leading spaces, before the parameter name. */
1094 start
> str
&& isspace ((unsigned char) *(start
-1));
1098 /* Remove a leading semicolon. */
1099 if (start
> str
&& *(start
-1) == ';') { --start
; }
1101 end
= cp
+ strlen (name
) + 1;
1103 /* Skip past the quoted value, and then the final quote. */
1104 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1107 /* Skip past the value. */
1108 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1111 /* Count how many characters need to be moved. Include
1112 trailing null, which is accounted for by the
1113 initialization of count to 1. */
1114 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1115 (void) memmove (start
, end
, count
);
1125 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1126 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1130 fix_composite_cte (CT ct
, int *message_mods
)
1134 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1135 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1136 ct
->c_encoding
!= CE_BINARY
) {
1139 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1140 char *name
= hf
->name
;
1141 for (; isspace((unsigned char)*name
); ++name
) {
1145 if (! strncasecmp (name
, ENCODING_FIELD
,
1146 LEN(ENCODING_FIELD
))) {
1147 char *prefix
= "Nmh-REPLACED-INVALID-";
1151 h
->name
= mh_xstrdup (hf
->name
);
1152 h
->hf_encoding
= hf
->hf_encoding
;
1156 /* Retain old header but prefix its name. */
1158 hf
->name
= concat (prefix
, h
->name
, NULL
);
1162 char *encoding
= cpytrim (hf
->value
);
1163 report (NULL
, ct
->c_partno
, ct
->c_file
,
1164 "replace Content-Transfer-Encoding of %s "
1165 "with 8 bit", encoding
);
1169 h
->value
= mh_xstrdup (" 8bit\n");
1171 /* Don't need to warn for multiple C-T-E header
1172 fields, parse_mime() already does that. But
1173 if there are any, fix them all as necessary. */
1178 set_ce (ct
, CE_8BIT
);
1181 if (ct
->c_type
== CT_MULTIPART
) {
1182 struct multipart
*m
;
1185 m
= (struct multipart
*) ct
->c_ctparams
;
1186 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1187 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1200 * Set content encoding.
1203 set_ce (CT ct
, int encoding
)
1205 const char *ce
= ce_str (encoding
);
1206 const struct str2init
*ctinit
= get_ce_method (ce
);
1209 char *cte
= concat (" ", ce
, "\n", NULL
);
1210 bool found_cte
= false;
1212 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1213 caller is decode_text_parts (). Save because we'll
1215 struct cefile decoded_content_info
= ct
->c_cefile
;
1217 ct
->c_encoding
= encoding
;
1219 ct
->c_ctinitfnx
= ctinit
->si_init
;
1220 /* This will assign ct->c_cefile with an all-0 struct, which
1222 (*ctinit
->si_init
) (ct
);
1223 /* After returning, the caller should set
1224 ct->c_cefile.ce_file to the name of the file containing
1227 if (ct
->c_ceclosefnx
) {
1228 (*ct
->c_ceclosefnx
) (ct
);
1231 /* Restore the cefile. */
1232 ct
->c_cefile
= decoded_content_info
;
1234 /* Update/add Content-Transfer-Encoding header field. */
1235 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1236 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1243 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1246 /* Update c_celine. It's used only by mhlist -debug. */
1247 free (ct
->c_celine
);
1248 ct
->c_celine
= mh_xstrdup (cte
);
1258 * Make sure each text part has a corresponding text/plain part.
1261 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
)
1265 switch ((*ct
)->c_type
) {
1267 /* Nothing to do for text/plain. */
1268 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1270 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1271 parent
->c_subtype
== MULTI_ALTERNATE
) {
1272 int new_subpart_number
= 1;
1273 int has_text_plain
=
1274 find_textplain_sibling (parent
, replacetextplain
,
1275 &new_subpart_number
);
1277 if (! has_text_plain
) {
1278 /* Parent is a multipart/alternative. Insert a new
1279 text/plain subpart. */
1280 const int inserted
=
1281 insert_new_text_plain_part (*ct
, new_subpart_number
,
1286 report (NULL
, parent
->c_partno
, parent
->c_file
,
1287 "insert text/plain part");
1293 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1294 parent
->c_subtype
== MULTI_RELATED
) {
1295 char *type_subtype
=
1296 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1297 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1298 const char *parent_type
=
1299 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1300 int new_subpart_number
= 1;
1301 int has_text_plain
= 0;
1303 /* Have to do string comparison on the subtype because we
1304 don't enumerate all of them in c_subtype values.
1305 parent_type will be NULL if the multipart/related part
1306 doesn't have a type parameter. The type parameter must
1307 be specified according to RFC 2387 Sec. 3.1 but not all
1309 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1310 /* The type of this part matches the root type of the
1311 parent multipart/related. Look to see if there's
1312 text/plain sibling. */
1314 find_textplain_sibling (parent
, replacetextplain
,
1315 &new_subpart_number
);
1318 free (type_subtype
);
1320 if (! has_text_plain
) {
1321 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1325 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1326 if (*ct
!= part
->mp_part
) {
1332 /* Parent is a multipart/related. Insert a new
1333 text/plain subpart in a new multipart/alternative. */
1334 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1335 /* Not an error if text/plain couldn't be added. */
1338 /* There are no siblings, so insert a new text/plain
1339 subpart, and change the parent type from
1340 multipart/related to multipart/alternative. */
1341 const int inserted
=
1342 insert_new_text_plain_part (*ct
, new_subpart_number
,
1348 parent
->c_subtype
= MULTI_ALTERNATE
;
1349 free (parent
->c_ctinfo
.ci_subtype
);
1350 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1351 if (! replace_substring (&parent
->c_ctline
, "/related",
1353 inform("did not find multipart/related in %s",
1357 /* Update Content-Type header field. */
1358 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1359 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1360 if (replace_substring (&hf
->value
, "/related",
1364 report (NULL
, parent
->c_partno
,
1366 "insert text/plain part");
1369 /* Remove, e.g., type="text/html" from
1370 multipart/alternative. */
1371 remove_parameter (hf
->value
, "type");
1374 inform("did not find multipart/"
1375 "related in header %s", hf
->value
);
1379 /* Not an error if text/plain couldn't be inserted. */
1384 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1391 case CT_MULTIPART
: {
1392 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1395 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1396 if ((*ct
)->c_type
== CT_MULTIPART
) {
1397 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1405 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1406 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1408 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1419 * See if there is a sibling text/plain, and return its subpart number.
1422 find_textplain_sibling (CT parent
, int replacetextplain
,
1423 int *new_subpart_number
)
1425 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1426 struct part
*part
, *prev
;
1427 bool has_text_plain
= false;
1429 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1430 ++*new_subpart_number
;
1431 if (part
->mp_part
->c_type
== CT_TEXT
&&
1432 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1433 if (replacetextplain
) {
1434 struct part
*old_part
;
1435 if (part
== mp
->mp_parts
) {
1436 old_part
= mp
->mp_parts
;
1437 mp
->mp_parts
= part
->mp_next
;
1439 old_part
= prev
->mp_next
;
1440 prev
->mp_next
= part
->mp_next
;
1443 report (NULL
, parent
->c_partno
, parent
->c_file
,
1444 "remove text/plain part %s",
1445 old_part
->mp_part
->c_partno
);
1447 free_content (old_part
->mp_part
);
1450 has_text_plain
= true;
1457 return has_text_plain
;
1462 * Insert a new text/plain part.
1465 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
)
1467 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1468 struct part
*new_part
;
1471 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1473 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1475 new_part
->mp_next
= mp
->mp_parts
;
1476 mp
->mp_parts
= new_part
;
1477 new_part
->mp_part
->c_partno
=
1478 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1484 free_content (new_part
->mp_part
);
1492 * Create a text/plain part to go along with non-plain sibling part.
1495 build_text_plain_part (CT encoded_part
)
1497 CT tp_part
= divide_part (encoded_part
);
1498 char *tmp_plain_file
= NULL
;
1500 if (decode_part (tp_part
) == OK
) {
1501 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1502 contains the decoded contents. And the decoding function, such
1503 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1504 be unlinked by free_content (). */
1507 /* This m_mktemp2() call closes the temp file. */
1508 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1509 inform("unable to create temporary file in %s",
1512 tmp_plain_file
= mh_xstrdup (tempfile
);
1513 if (reformat_part (tp_part
, tmp_plain_file
,
1514 tp_part
->c_ctinfo
.ci_type
,
1515 tp_part
->c_ctinfo
.ci_subtype
,
1516 tp_part
->c_type
) == OK
) {
1522 free_content (tp_part
);
1523 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1524 free (tmp_plain_file
);
1531 * Slip new text/plain part into a new multipart/alternative.
1534 insert_into_new_mp_alt (CT
*ct
, int *message_mods
)
1536 CT tp_part
= build_text_plain_part (*ct
);
1540 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1543 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1545 if (mp
&& mp
->mp_parts
) {
1546 mp
->mp_parts
->mp_part
= tp_part
;
1547 /* Make the new multipart/alternative the parent. */
1552 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1553 "insert text/plain part");
1556 free_content (tp_part
);
1557 free_content (mp_alt
);
1564 /* Not an error if text/plain couldn't be built. */
1572 * Clone a MIME part.
1580 /* Just copy over what is needed for decoding. c_vrsn and
1581 c_celine aren't necessary. */
1582 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1583 new_part
->c_begin
= ct
->c_begin
;
1584 new_part
->c_end
= ct
->c_end
;
1585 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1586 new_part
->c_type
= ct
->c_type
;
1587 new_part
->c_cefile
= ct
->c_cefile
;
1588 new_part
->c_encoding
= ct
->c_encoding
;
1589 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1590 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1591 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1592 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1594 /* c_ctline is used by reformat__part(), so it can preserve
1595 anything after the type/subtype. */
1596 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1603 * Copy the content info from one part to another.
1606 copy_ctinfo (CI dest
, CI src
)
1610 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1611 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1613 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1614 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1616 if (s_pm
->pm_charset
) {
1617 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1619 if (s_pm
->pm_lang
) {
1620 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1624 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1625 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1640 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, &file
)) == NULL
) {
1641 die("unable to create temporary file in %s", get_temp_dir());
1643 tmp_decoded
= mh_xstrdup (tempfile
);
1644 /* The following call will load ct->c_cefile.ce_file with the tmp
1645 filename of the decoded content. tmp_decoded will contain the
1646 encoded output, get rid of that. */
1647 status
= output_message_fp (ct
, file
, tmp_decoded
);
1648 (void) m_unlink (tmp_decoded
);
1650 if (fclose (file
)) {
1651 inform("unable to close temporary file %s, continuing...", tempfile
);
1659 * Reformat content as plain text.
1660 * Some of the arguments aren't really needed now, but maybe will
1661 * be in the future for other than text types.
1664 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
)
1666 int output_subtype
, output_encoding
;
1667 const char *reason
= NULL
;
1671 /* Hacky: this redirects the output from whatever command is used
1672 to show the part to a file. So, the user can't have any output
1673 redirection in that command.
1674 Could show_multi() in mhshowsbr.c avoid this? */
1676 /* Check for invo_name-format-type/subtype. */
1677 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1679 inform("Don't know how to convert %s, there is no "
1680 "%s-format-%s/%s profile entry",
1681 ct
->c_file
, invo_name
, type
, subtype
);
1685 if (strchr (cf
, '>')) {
1686 inform("'>' prohibited in \"%s\",\nplease fix your "
1687 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1693 cp
= concat (cf
, " >", file
, NULL
);
1694 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1697 /* Unlink decoded content tmp file and free its filename to avoid
1698 leaks. The file stream should already have been closed. */
1699 if (ct
->c_cefile
.ce_unlink
) {
1700 (void) m_unlink (ct
->c_cefile
.ce_file
);
1701 free (ct
->c_cefile
.ce_file
);
1702 ct
->c_cefile
.ce_file
= NULL
;
1703 ct
->c_cefile
.ce_unlink
= 0;
1706 if (c_type
== CT_TEXT
) {
1707 output_subtype
= TEXT_PLAIN
;
1709 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1713 output_encoding
= content_encoding (ct
, &reason
);
1715 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1716 ct
->c_cefile
.ce_file
= file
;
1717 ct
->c_cefile
.ce_unlink
= 1;
1719 ct
->c_cefile
.ce_unlink
= 0;
1728 * Fill in a multipart/alternative part.
1731 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
)
1733 char *boundary_prefix
= "----=_nmh-multipart";
1734 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1735 char *boundary_indicator
= "; boundary=";
1736 char *typename
, *subtypename
, *name
;
1739 struct multipart
*m
;
1740 const struct str2init
*ctinit
;
1744 /* Set up the multipart/alternative part. These fields of *ct were
1745 initialized to 0 by mh_xcalloc():
1746 c_fp, c_unlink, c_begin, c_end,
1747 c_vrsn, c_ctline, c_celine,
1748 c_id, c_descr, c_dispo, c_partno,
1749 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1750 c_cefile, c_encoding,
1751 c_digested, c_digest[16], c_ctexbody,
1752 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1754 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1757 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1759 ct
->c_subtype
= subtype
;
1761 ctinit
= get_ct_init (ct
->c_type
);
1763 typename
= ct_type_str (type
);
1764 subtypename
= ct_subtype_str (type
, subtype
);
1768 int found_boundary
= 1;
1770 while (found_boundary
&& serial
< 1000000) {
1773 /* Ensure that the boundary doesn't appear in the decoded
1775 if (new_part
->c_cefile
.ce_file
) {
1776 if ((found_boundary
=
1777 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1778 new_part
->c_cefile
.ce_file
,
1779 boundary
)) == NOTOK
) {
1784 /* Ensure that the boundary doesn't appear in the encoded
1786 if (! found_boundary
&& new_part
->c_file
) {
1787 if ((found_boundary
=
1788 boundary_in_content (&new_part
->c_fp
,
1790 boundary
)) == NOTOK
) {
1795 if (found_boundary
) {
1796 /* Try a slightly different boundary. */
1801 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1803 concat (boundary_prefix
,
1804 FENDNULL(first_alt
->c_partno
),
1805 "-", buffer2
, NULL
);
1809 if (found_boundary
) {
1810 inform("giving up trying to find a unique boundary");
1815 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1816 boundary
, "\"", NULL
);
1818 /* Load c_first_hf and c_last_hf. */
1819 transfer_noncontent_headers (first_alt
, ct
);
1820 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1823 /* Load c_partno. */
1824 if (first_alt
->c_partno
) {
1825 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1826 free (first_alt
->c_partno
);
1827 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1828 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1830 first_alt
->c_partno
= mh_xstrdup ("1");
1831 new_part
->c_partno
= mh_xstrdup ("2");
1835 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1836 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1839 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1840 "boundary", boundary
, 0);
1844 p
->mp_next
->mp_next
= NULL
;
1845 p
->mp_next
->mp_part
= first_alt
;
1848 m
->mp_start
= concat (boundary
, "\n", NULL
);
1849 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1865 * Check that the boundary does not appear in the content.
1868 boundary_in_content (FILE **fp
, char *file
, const char *boundary
)
1870 char buffer
[NMH_BUFSIZ
];
1872 bool found_boundary
= false;
1874 /* free_content() will close *fp if we fopen it here. */
1875 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1876 advise (file
, "unable to open %s for reading", file
);
1880 fseeko (*fp
, 0L, SEEK_SET
);
1881 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1882 if (find_str (buffer
, bytes_read
, boundary
)) {
1883 found_boundary
= true;
1888 return found_boundary
;
1893 * Remove all non-Content headers.
1896 transfer_noncontent_headers (CT old
, CT
new)
1900 hp_prev
= hp
= old
->c_first_hf
;
1904 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, LEN(XXX_FIELD_PRF
))) {
1905 if (hp
== old
->c_last_hf
) {
1906 if (hp
== old
->c_first_hf
) {
1907 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1909 hp_prev
->next
= NULL
;
1910 old
->c_last_hf
= hp_prev
;
1913 if (hp
== old
->c_first_hf
) {
1914 old
->c_first_hf
= next
;
1916 hp_prev
->next
= next
;
1920 /* Put node hp in the new CT. */
1921 if (new->c_first_hf
== NULL
) {
1922 new->c_first_hf
= hp
;
1924 new->c_last_hf
->next
= hp
;
1926 new->c_last_hf
= hp
;
1928 /* A Content- header, leave in old. */
1941 set_ct_type (CT ct
, int type
, int subtype
, int encoding
)
1943 char *typename
= ct_type_str (type
);
1944 char *subtypename
= ct_subtype_str (type
, subtype
);
1945 /* E.g, " text/plain" */
1946 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1947 /* E.g, " text/plain\n" */
1948 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1949 bool found_content_type
= false;
1951 const char *cp
= NULL
;
1955 /* Update/add Content-Type header field. */
1956 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1957 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1958 found_content_type
= true;
1960 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1961 ? concat (type_subtypename
, cp
, "\n", NULL
)
1962 : mh_xstrdup (name_plus_nl
);
1965 if (! found_content_type
) {
1966 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
1967 (cp
= strchr (ct
->c_ctline
, ';'))
1968 ? concat (type_subtypename
, cp
, "\n", NULL
)
1969 : mh_xstrdup (name_plus_nl
));
1972 /* Some of these might not be used, but set them anyway. */
1974 ? concat (type_subtypename
, cp
, NULL
)
1975 : concat (type_subtypename
, NULL
);
1976 free (ct
->c_ctline
);
1977 ct
->c_ctline
= ctline
;
1978 /* Leave other ctinfo members as they were. */
1979 free (ct
->c_ctinfo
.ci_type
);
1980 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1981 free (ct
->c_ctinfo
.ci_subtype
);
1982 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1984 ct
->c_subtype
= subtype
;
1986 free (name_plus_nl
);
1987 free (type_subtypename
);
1989 status
= set_ce (ct
, encoding
);
1996 * It's not necessary to update the charset parameter of a Content-Type
1997 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1998 * (content) was originally in the specified charset, "and will be in
1999 * that character set again after decoding."
2002 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
2006 int lf_line_endings
= 0;
2008 switch (ct
->c_type
) {
2009 case CT_MULTIPART
: {
2010 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2013 /* Should check to see if the body for this part is encoded?
2014 For now, it gets passed along as-is by InitMultiPart(). */
2015 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2016 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
2023 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2024 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2026 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
2032 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2037 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
2039 switch (ct
->c_encoding
) {
2044 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
2045 const char *reason
= NULL
;
2047 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
2048 && encoding
!= CE_BINARY
) {
2049 /* The decoding isn't acceptable so discard it.
2050 Leave status as OK to allow other transformations. */
2052 report (NULL
, ct
->c_partno
, ct
->c_file
,
2053 "will not decode%s because it is binary (%s)",
2055 : (FENDNULL(ct
->c_ctline
)),
2058 (void) m_unlink (ct
->c_cefile
.ce_file
);
2059 free (ct
->c_cefile
.ce_file
);
2060 ct
->c_cefile
.ce_file
= NULL
;
2061 } else if (ct
->c_encoding
== CE_QUOTED
&&
2062 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2063 /* The decoding isn't acceptable so discard it.
2064 Leave status as OK to allow other transformations. */
2066 report (NULL
, ct
->c_partno
, ct
->c_file
,
2067 "will not decode%s because it is 8bit",
2069 : (FENDNULL(ct
->c_ctline
)));
2071 (void) m_unlink (ct
->c_cefile
.ce_file
);
2072 free (ct
->c_cefile
.ce_file
);
2073 ct
->c_cefile
.ce_file
= NULL
;
2077 if (ct_encoding
== CE_BINARY
) {
2079 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2084 if (set_ce (ct
, enc
) == OK
) {
2087 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2088 FENDNULL(ct
->c_ctline
));
2090 if (lf_line_endings
) {
2091 strip_crs (ct
, message_mods
);
2104 if (lf_line_endings
) {
2105 strip_crs (ct
, message_mods
);
2120 * Determine if the part with type[/subtype] should be decoded, according to
2121 * decodetypes (which came from the -decodetypes switch).
2124 should_decode(const char *decodetypes
, const char *type
, const char *subtype
)
2126 /* Quick search for matching type[/subtype] in decodetypes: bracket
2127 decodetypes with commas, then search for ,type, and ,type/subtype, in
2130 bool found_match
= false;
2131 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2132 char *delimited_type
= concat(",", type
, ",", NULL
);
2134 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2136 } else if (subtype
!= NULL
) {
2137 char *delimited_type_subtype
=
2138 concat(",", type
, "/", subtype
, ",", NULL
);
2140 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2143 free(delimited_type_subtype
);
2146 free(delimited_type
);
2147 free(delimited_decodetypes
);
2154 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2155 * if it has any NUL characters, a CR not followed by a LF, or lines
2156 * greater than 998 characters in length. If binary, reason is set
2157 * to a string explaining why.
2160 content_encoding (CT ct
, const char **reason
)
2162 CE ce
= &ct
->c_cefile
;
2163 int encoding
= CE_7BIT
;
2166 size_t line_len
= 0;
2167 char buffer
[NMH_BUFSIZ
];
2170 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2171 advise (ce
->ce_file
, "unable to open for reading");
2175 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2176 while (encoding
!= CE_BINARY
&&
2177 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2180 int last_char_was_cr
= 0;
2182 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2183 if (*cp
== '\0' || ++line_len
> 998 ||
2184 (*cp
!= '\n' && last_char_was_cr
)) {
2185 encoding
= CE_BINARY
;
2187 *reason
= "null character";
2188 } else if (line_len
> 998) {
2189 *reason
= "line length > 998";
2190 } else if (*cp
!= '\n' && last_char_was_cr
) {
2191 *reason
= "CR not followed by LF";
2193 /* Should not reach this. */
2200 } else if (! isascii ((unsigned char) *cp
)) {
2204 last_char_was_cr
= *cp
== '\r';
2210 } /* else should never happen */
2217 * Strip carriage returns from content.
2220 strip_crs (CT ct
, int *message_mods
)
2222 char *charset
= content_charset (ct
);
2225 /* Only strip carriage returns if content is ASCII or another
2226 charset that has the same readily recognizable CR followed by a
2227 LF. We can include UTF-8 here because if the high-order bit of
2228 a UTF-8 byte is 0, then it must be a single-byte ASCII
2230 if (! strcasecmp (charset
, "US-ASCII") ||
2231 ! strcasecmp (charset
, "UTF-8") ||
2232 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2233 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2238 bool has_crs
= false;
2239 bool opened_input_file
= false;
2241 if (ct
->c_cefile
.ce_file
) {
2242 file
= &ct
->c_cefile
.ce_file
;
2243 fp
= &ct
->c_cefile
.ce_fp
;
2245 } else if (ct
->c_file
) {
2248 begin
= (size_t) ct
->c_begin
;
2249 end
= (size_t) ct
->c_end
;
2250 } /* else don't know where the content is */
2252 if (file
&& *file
&& fp
) {
2254 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2255 advise (*file
, "unable to open for reading");
2258 opened_input_file
= true;
2264 char buffer
[NMH_BUFSIZ
];
2266 size_t bytes_to_read
=
2267 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2269 fseeko (*fp
, begin
, SEEK_SET
);
2270 while ((bytes_read
= fread (buffer
, 1,
2271 min (bytes_to_read
, sizeof buffer
),
2273 /* Look for CR followed by a LF. This is supposed to
2274 be text so there should be LF's. If not, don't
2275 modify the content. */
2278 bool last_char_was_cr
= false;
2280 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2282 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2283 if (*cp
== '\n' && last_char_was_cr
) {
2288 last_char_was_cr
= *cp
== '\r';
2294 char *stripped_content_file
;
2295 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2297 if (tempfile
== NULL
) {
2298 die("unable to create temporary file in %s",
2301 stripped_content_file
= mh_xstrdup (tempfile
);
2303 /* Strip each CR before a LF from the content. */
2304 fseeko (*fp
, begin
, SEEK_SET
);
2305 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2309 bool last_char_was_cr
= false;
2311 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2313 last_char_was_cr
= true;
2314 } else if (last_char_was_cr
) {
2316 if (write (fd
, "\r", 1) < 0) {
2317 advise (tempfile
, "CR write");
2320 if (write (fd
, cp
, 1) < 0) {
2321 advise (tempfile
, "write");
2323 last_char_was_cr
= false;
2325 if (write (fd
, cp
, 1) < 0) {
2326 advise (tempfile
, "write");
2328 last_char_was_cr
= false;
2334 inform("unable to write temporary file %s, continuing...",
2335 stripped_content_file
);
2336 (void) m_unlink (stripped_content_file
);
2337 free(stripped_content_file
);
2340 /* Replace the decoded file with the converted one. */
2341 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2342 (void) m_unlink (ct
->c_cefile
.ce_file
);
2344 free(ct
->c_cefile
.ce_file
);
2345 ct
->c_cefile
.ce_file
= stripped_content_file
;
2346 ct
->c_cefile
.ce_unlink
= 1;
2350 report (NULL
, ct
->c_partno
,
2351 begin
== 0 && end
== 0 ? "" : *file
,
2357 if (opened_input_file
) {
2371 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2372 * of the part C-T-E's.
2377 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2379 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2380 least_restrictive_enc
!= CE_7BIT
) {
2381 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2383 bool found_cte
= false;
2385 /* Update/add Content-Transfer-Encoding header field. */
2386 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2387 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2394 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2401 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2405 least_restrictive_encoding (CT ct
)
2407 int encoding
= CE_UNKNOWN
;
2409 switch (ct
->c_type
) {
2410 case CT_MULTIPART
: {
2411 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2414 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2415 const int part_encoding
=
2416 least_restrictive_encoding (part
->mp_part
);
2418 if (less_restrictive (encoding
, part_encoding
)) {
2419 encoding
= part_encoding
;
2426 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2427 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2428 const int part_encoding
=
2429 least_restrictive_encoding (e
->eb_content
);
2431 if (less_restrictive (encoding
, part_encoding
)) {
2432 encoding
= part_encoding
;
2438 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2439 encoding
= ct
->c_encoding
;
2448 * Return whether the second encoding is less restrictive than the first, where
2449 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2450 * CE_BINARY is less restrictive than CE_8BIT and
2451 * CE_8BIT is less restrictive than CE_7BIT.
2454 less_restrictive (int encoding
, int second_encoding
)
2456 switch (second_encoding
) {
2458 return encoding
!= CE_BINARY
;
2460 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2462 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2463 encoding
!= CE_7BIT
;
2471 * Convert character set of each part.
2474 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
)
2478 switch (ct
->c_type
) {
2480 if (ct
->c_subtype
== TEXT_PLAIN
) {
2481 status
= convert_charset (ct
, dest_charset
, message_mods
);
2484 char *ct_charset
= content_charset (ct
);
2486 report (NULL
, ct
->c_partno
, ct
->c_file
,
2487 "convert %s to %s", ct_charset
, dest_charset
);
2491 char *ct_charset
= content_charset (ct
);
2493 report ("iconv", ct
->c_partno
, ct
->c_file
,
2494 "failed to convert %s to %s", ct_charset
, dest_charset
);
2500 case CT_MULTIPART
: {
2501 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2504 /* Should check to see if the body for this part is encoded?
2505 For now, it gets passed along as-is by InitMultiPart(). */
2506 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2508 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2514 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2515 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2518 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2531 * Fix various problems that aren't handled elsewhere. These
2532 * are fixed unconditionally: there are no switches to disable
2533 * them. Currently, "problems" are these:
2534 * 1) remove extraneous semicolon at the end of a header parameter list
2535 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2536 * filename parameters in Content-Type and Content-Disposition
2537 * headers, respectively.
2540 fix_always (CT ct
, int *message_mods
)
2544 switch (ct
->c_type
) {
2545 case CT_MULTIPART
: {
2546 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2549 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2550 status
= fix_always (part
->mp_part
, message_mods
);
2556 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2557 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2559 status
= fix_always (e
->eb_content
, message_mods
);
2566 if (ct
->c_first_hf
) {
2567 fix_filename_encoding (ct
);
2570 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2571 size_t len
= strlen (hf
->value
);
2573 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2574 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2575 /* Only do this for Content-Type and
2576 Content-Disposition fields because those are the
2577 only headers that parse_mime() warns about. */
2581 /* whitespace following a trailing ';' will be nuked as well */
2582 if (hf
->value
[len
- 1] == '\n') {
2583 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2584 if (len
-- == 0) { break; }
2588 if (hf
->value
[len
- 2] == ';') {
2589 /* Remove trailing ';' from parameter value. */
2590 hf
->value
[len
- 2] = '\n';
2591 hf
->value
[len
- 1] = '\0';
2593 /* Also, if Content-Type parameter, remove trailing ';'
2594 from ct->c_ctline. This probably isn't necessary
2596 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2597 size_t l
= strlen(ct
->c_ctline
) - 1;
2598 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2599 ct
->c_ctline
[l
] == ';') {
2600 ct
->c_ctline
[l
--] = '\0';
2601 if (l
== 0) { break; }
2607 report (NULL
, ct
->c_partno
, ct
->c_file
,
2608 "remove trailing ; from %s parameter value",
2620 * Factor out common code for loops in fix_filename_encoding().
2623 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
)
2627 if (has_prefix(value
, "=?") && has_suffix(value
, "?=")) {
2628 /* Looks like an RFC 2047 encoded parameter. */
2629 char decoded
[PATH_MAX
+ 1];
2631 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2632 /* Encode using RFC 2231. */
2633 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2636 inform("failed to decode %s parameter %s", name
, value
);
2645 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2646 * filename parameters in Content-Type and Content-Disposition
2647 * headers, respectively.
2650 fix_filename_encoding (CT ct
)
2656 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2657 if (pm
->pm_name
&& pm
->pm_value
&&
2658 strcasecmp (pm
->pm_name
, "name") == 0) {
2659 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2660 &ct
->c_ctinfo
.ci_first_pm
,
2661 &ct
->c_ctinfo
.ci_last_pm
);
2665 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2666 if (pm
->pm_name
&& pm
->pm_value
&&
2667 strcasecmp (pm
->pm_name
, "filename") == 0) {
2668 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2674 /* Fix hf values to correspond. */
2675 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2676 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2678 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2679 field
= TYPE_HEADER
;
2680 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2681 field
= DISPO_HEADER
;
2684 if (field
!= OTHER
) {
2685 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2687 if (semicolon_loc
) {
2689 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2690 const char *const params
=
2692 field
== TYPE_HEADER
2693 ? ct
->c_ctinfo
.ci_first_pm
2694 : ct
->c_dispo_first
,
2696 const char *const new_params
= concat (params
, "\n", NULL
);
2698 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2699 free((void *)new_params
); /* Cast away const. Sigh. */
2700 free((void *)params
);
2702 inform("did not find semicolon in %s:%s\n",
2703 hf
->name
, hf
->value
);
2713 * Output content in input file to output file.
2716 write_content (CT ct
, const char *input_filename
, char *outfile
, FILE *outfp
,
2717 int modify_inplace
, int message_mods
)
2721 if (modify_inplace
) {
2722 if (message_mods
> 0) {
2723 if ((status
= output_message_fp (ct
, outfp
, outfile
)) == OK
) {
2724 char *infile
= input_filename
2725 ? mh_xstrdup (input_filename
)
2726 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2728 if (remove_file (infile
) == OK
) {
2729 if (rename (outfile
, infile
)) {
2730 /* Rename didn't work, possibly because of an
2731 attempt to rename across filesystems. Try
2732 brute force copy. */
2733 int old
= open (outfile
, O_RDONLY
);
2735 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2738 if (old
!= -1 && new != -1) {
2739 char buffer
[NMH_BUFSIZ
];
2741 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2743 if (write (new, buffer
, i
) != i
) {
2749 if (new != -1) { close (new); }
2750 if (old
!= -1) { close (old
); }
2751 (void) m_unlink (outfile
);
2754 /* The -file argument processing used path() to
2755 expand filename to absolute path. */
2756 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2758 inform("unable to rename %s %s to %s, continuing...",
2759 file
? "file" : "message", outfile
,
2765 inform("unable to remove input file %s, "
2766 "not modifying it, continuing...", infile
);
2767 (void) m_unlink (outfile
);
2776 /* No modifications and didn't need the tmp outfile. */
2777 (void) m_unlink (outfile
);
2780 /* Output is going to some file. Produce it whether or not
2781 there were modifications. */
2782 status
= output_message_fp (ct
, outfp
, outfile
);
2791 * parse_mime() does not set lf_line_endings in struct text, so use this
2792 * function to do it. It touches the parts the decodetypes identifies.
2795 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
)
2797 switch (ct
->c_type
) {
2798 case CT_MULTIPART
: {
2799 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2802 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2803 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2809 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2810 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2812 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2817 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2818 if (ct
->c_ctparams
== NULL
) {
2819 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2821 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2828 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2829 * use the standard MH backup file.
2832 remove_file (const char *file
)
2835 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2836 int status
= system (rmm_command
);
2839 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2841 /* This is OK for a non-message file, it still uses the
2842 BACKUP_PREFIX form. The backup file will be in the same
2843 directory as file. */
2844 return rename (file
, m_backup (file
));
2849 * Output formatted message to user.
2852 report (char *what
, char *partno
, char *filename
, char *message
, ...)
2858 va_start (args
, message
);
2859 fmt
= concat (filename
, partno
? " part " : ", ",
2860 FENDNULL(partno
), partno
? ", " : "", message
, NULL
);
2862 advertise (what
, NULL
, fmt
, args
);
2875 fprintf (stderr
, "\n");