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/getarguments.h"
10 #include "sbr/concat.h"
11 #include "sbr/seq_setprev.h"
12 #include "sbr/seq_setcur.h"
13 #include "sbr/seq_save.h"
14 #include "sbr/smatch.h"
15 #include "sbr/fmt_rfc2047.h"
16 #include "sbr/cpydata.h"
17 #include "sbr/trimcpy.h"
18 #include "sbr/m_convert.h"
19 #include "sbr/m_backup.h"
20 #include "sbr/getfolder.h"
21 #include "sbr/folder_read.h"
22 #include "sbr/context_save.h"
23 #include "sbr/context_replace.h"
24 #include "sbr/context_find.h"
25 #include "sbr/readconfig.h"
26 #include "sbr/ambigsw.h"
28 #include "sbr/print_version.h"
29 #include "sbr/print_help.h"
30 #include "sbr/error.h"
31 #include "h/fmt_scan.h"
33 #include "h/mhparse.h"
36 #include "h/signals.h"
37 #include "sbr/m_maildir.h"
38 #include "sbr/m_mktemp.h"
39 #include "sbr/mime_type.h"
43 #include "mhshowsbr.h"
46 #define MHFIXMSG_SWITCHES \
47 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
48 X("nodecodetext", 0, NDECODETEXTSW) \
49 X("decodetypes", 0, DECODETYPESW) \
50 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
51 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
52 X("textcharset", 0, TEXTCHARSETSW) \
53 X("notextcharset", 0, NTEXTCHARSETSW) \
54 X("reformat", 0, REFORMATSW) \
55 X("noreformat", 0, NREFORMATSW) \
56 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
57 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
58 X("fixboundary", 0, FIXBOUNDARYSW) \
59 X("nofixboundary", 0, NFIXBOUNDARYSW) \
60 X("fixcte", 0, FIXCOMPOSITECTESW) \
61 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
62 X("fixtype mimetype", 0, FIXTYPESW) \
63 X("file file", 0, FILESW) \
64 X("outfile file", 0, OUTFILESW) \
65 X("rmmproc program", 0, RPROCSW) \
66 X("normmproc", 0, NRPRCSW) \
67 X("changecur", 0, CHGSW) \
68 X("nochangecur", 0, NCHGSW) \
69 X("verbose", 0, VERBSW) \
70 X("noverbose", 0, NVERBSW) \
71 X("version", 0, VERSIONSW) \
72 X("help", 0, HELPSW) \
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(MHFIXMSG
);
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
84 int debugsw
; /* Needed by mhparse.c. */
86 #define quitser pipeser
91 typedef struct fix_transformations
{
99 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
102 } fix_transformations
;
104 static int mhfixmsgsbr (CT
*, char *, const fix_transformations
*,
105 FILE **, char *, FILE **);
106 static int fix_boundary (CT
*, int *);
107 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
108 static int get_multipart_boundary (CT
, char **);
109 static int replace_boundary (CT
, char *, char *);
110 static int fix_types (CT
, svector_t
, int *);
111 static char *replace_substring (char **, const char *, const char *);
112 static char *remove_parameter (char *, const char *);
113 static int fix_composite_cte (CT
, int *);
114 static int set_ce (CT
, int);
115 static int ensure_text_plain (CT
*, CT
, int *, int);
116 static int find_textplain_sibling (CT
, int, int *);
117 static int insert_new_text_plain_part (CT
, int, CT
);
118 static CT
build_text_plain_part (CT
);
119 static int insert_into_new_mp_alt (CT
*, int *);
120 static CT
divide_part (CT
);
121 static void copy_ctinfo (CI
, CI
);
122 static int decode_part (CT
);
123 static int reformat_part (CT
, char *, char *, char *, int);
124 static CT
build_multipart_alt (CT
, CT
, int, int);
125 static int boundary_in_content (FILE **, char *, const char *);
126 static void transfer_noncontent_headers (CT
, CT
);
127 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
128 static int decode_text_parts (CT
, int, const char *, int *);
129 static int should_decode(const char *, const char *, const char *);
130 static int content_encoding (CT
, const char **);
131 static int strip_crs (CT
, int *);
132 static void update_cte (CT
);
133 static int least_restrictive_encoding (CT
) PURE
;
134 static int less_restrictive (int, int);
135 static int convert_charsets (CT
, char *, int *);
136 static int fix_always (CT
, int *);
137 static int fix_filename_param (char *, char *, PM
*, PM
*);
138 static int fix_filename_encoding (CT
);
139 static int write_content (CT
, const char *, char *, FILE *, int, int);
140 static void set_text_ctparams(CT
, char *, int);
141 static int remove_file (const char *);
142 static void report (char *, char *, char *, char *, ...)
144 static void pipeser (int);
148 main (int argc
, char **argv
)
151 char *cp
, *file
= NULL
, *folder
= NULL
;
152 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
153 char **argp
, **arguments
;
154 struct msgs_array msgs
= { 0, 0, NULL
};
155 struct msgs
*mp
= NULL
;
157 FILE *fp
, *infp
= NULL
, *outfp
= NULL
;
158 bool using_stdin
= false;
161 fix_transformations fx
;
162 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
164 fx
.replacetextplain
= 0;
165 fx
.decodetext
= CE_8BIT
;
166 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
167 fx
.lf_line_endings
= 0;
168 fx
.textcharset
= NULL
;
170 if (nmh_init(argv
[0], true, false)) { return 1; }
172 arguments
= getarguments (invo_name
, argc
, argv
, 1);
178 while ((cp
= *argp
++)) {
180 switch (smatch (++cp
, switches
)) {
182 ambigsw (cp
, switches
);
185 die("-%s unknown", cp
);
188 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
190 print_help (buf
, switches
, 1);
193 print_version(invo_name
);
197 if (! (cp
= *argp
++) || *cp
== '-') {
198 die("missing argument to %s", argp
[-2]);
200 if (! strcasecmp (cp
, "8bit")) {
201 fx
.decodetext
= CE_8BIT
;
202 } else if (! strcasecmp (cp
, "7bit")) {
203 fx
.decodetext
= CE_7BIT
;
204 } else if (! strcasecmp (cp
, "binary")) {
205 fx
.decodetext
= CE_BINARY
;
207 die("invalid argument to %s", argp
[-2]);
214 if (! (cp
= *argp
++) || *cp
== '-') {
215 die("missing argument to %s", argp
[-2]);
219 case CRLFLINEBREAKSSW
:
220 fx
.lf_line_endings
= 0;
222 case NCRLFLINEBREAKSSW
:
223 fx
.lf_line_endings
= 1;
226 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
227 die("missing argument to %s", argp
[-2]);
240 case FIXCOMPOSITECTESW
:
241 fx
.fixcompositecte
= 1;
243 case NFIXCOMPOSITECTESW
:
244 fx
.fixcompositecte
= 0;
247 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
248 die("missing argument to %s", argp
[-2]);
250 if (! strncasecmp (cp
, "multipart/", 10) ||
251 ! strncasecmp (cp
, "message/", 8))
252 die("-fixtype %s not allowed", cp
);
253 if (! strchr (cp
, '/'))
254 die("-fixtype requires type/subtype");
255 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
256 svector_push_back (fx
.fixtypes
, cp
);
264 case REPLACETEXTPLAINSW
:
265 fx
.replacetextplain
= 1;
267 case NREPLACETEXTPLAINSW
:
268 fx
.replacetextplain
= 0;
271 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
272 die("missing argument to %s", argp
[-2]);
274 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
277 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
278 die("missing argument to %s", argp
[-2]);
280 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
283 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
284 die("missing argument to %s", argp
[-2]);
304 if (*cp
== '+' || *cp
== '@') {
306 die("only one folder at a time!");
307 folder
= pluspath (cp
);
310 /* Interpret a full path as a filename, not a message. */
311 file
= mh_xstrdup (cp
);
313 app_msgarg (&msgs
, cp
);
318 SIGNAL (SIGQUIT
, quitser
);
319 SIGNAL (SIGPIPE
, pipeser
);
322 * Read the standard profile setup
324 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
325 readconfig(NULL
, fp
, cp
, 0);
329 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= true;
330 suppress_extraneous_trailing_semicolon_warning
= true;
332 if (! context_find ("path")) {
333 free (path ("./", TFOLDER
));
336 if (file
&& msgs
.size
) {
337 die("cannot specify msg and file at same time!");
341 /* Open the outfile now, so we don't have to risk opening it
342 after running out of fds. */
343 if (strcmp (outfile
, "-") == 0) {
345 } else if ((outfp
= fopen (outfile
, "w")) == NULL
) {
346 adios (outfile
, "unable to open for writing");
351 * check if message is coming from file
354 /* If file is stdin, create a tmp file name before parse_mime()
355 has a chance, because it might put in on a different
356 filesystem than the output file. Instead, put it in the
357 user's preferred tmp directory. */
360 if (! strcmp ("-", file
)) {
366 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
367 die("unable to create temporary file in %s",
371 file
= mh_xstrdup (cp
);
372 cpydata (STDIN_FILENO
, fd
, "-", file
);
376 (void) m_unlink (file
);
377 die("failed to write temporary file");
381 cts
= mh_xcalloc(2, sizeof *cts
);
384 if ((ct
= parse_mime (file
))) {
385 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
388 inform("unable to parse message from file %s", file
);
391 /* If there's an outfile, pass the input message unchanged, so the
392 message won't get dropped from a pipeline. */
394 /* Something went wrong. Output might be expected, such as if
395 this were run as a filter. Just copy the input to the
397 if ((infp
= fopen (file
, "r")) == NULL
) {
398 adios (file
, "unable to open for reading");
401 if (copy_input_to_output (file
, infp
, outfile
, outfp
) != OK
) {
402 inform("unable to copy message to %s, "
403 "it might be lost\n", outfile
);
412 * message(s) are coming from a folder
417 app_msgarg(&msgs
, "cur");
420 folder
= getfolder (1);
422 maildir
= mh_xstrdup(m_maildir (folder
));
424 /* chdir so that error messages, esp. from MIME parser, just
425 refer to the message and not its path. */
426 if (chdir (maildir
) == NOTOK
) {
427 adios (maildir
, "unable to change directory to");
430 /* read folder and create message structure */
431 if (! (mp
= folder_read (folder
, 1))) {
432 die("unable to read folder %s", folder
);
435 /* check for empty folder */
436 if (mp
->nummsg
== 0) {
437 die("no messages in %s", folder
);
440 /* parse all the message ranges/sequences and set SELECTED */
441 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
442 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
445 seq_setprev (mp
); /* set the previous-sequence */
447 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
450 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
451 if (is_selected(mp
, msgnum
)) {
452 char *msgnam
= m_name (msgnum
);
454 if ((ct
= parse_mime (msgnam
))) {
455 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
458 inform("unable to parse message %s", msgnam
);
461 /* If there's an outfile, pass the input message
462 unchanged, so the message won't get dropped from a
465 /* Something went wrong. Output might be expected,
466 such as if this were run as a filter. Just copy
467 the input to the output. */
468 /* Can't use path() here because 1) it might have been
469 called before and it caches the pwd, and 2) we call
470 chdir() after that. */
471 char *input_filename
=
472 concat (maildir
, "/", msgnam
, NULL
);
474 if ((infp
= fopen (input_filename
, "r")) == NULL
) {
475 adios (input_filename
,
476 "unable to open for reading");
479 if (copy_input_to_output (input_filename
, infp
,
480 outfile
, outfp
) != OK
) {
481 inform("unable to copy message to %s, "
482 "it might be lost\n", outfile
);
487 free (input_filename
);
494 seq_setcur (mp
, mp
->hghsel
); /* update current message */
496 seq_save (mp
); /* synchronize sequences */
497 context_replace (pfolder
, folder
);/* update current folder */
498 context_save (); /* save the context file */
502 for (ctp
= cts
; *ctp
; ++ctp
) {
504 mhfixmsgsbr (ctp
, maildir
, &fx
, &infp
, outfile
, &outfp
) == OK
510 (void) m_unlink (file
);
513 /* Just calling m_backup() unlinks the backup file. */
514 (void) m_backup (file
);
525 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
526 if (infp
) { fclose (infp
); } /* even if stdin */
527 if (outfp
) { fclose (outfp
); } /* even if stdout */
533 done (status
== OK
? 0 : 1);
539 * Apply transformations to one message.
542 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
543 FILE **infp
, char *outfile
, FILE **outfp
)
545 /* Store input filename in case one of the transformations, i.e.,
546 fix_boundary(), rewrites to a tmp file. */
547 char *input_filename
= maildir
548 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
549 : mh_xstrdup ((*ctp
)->c_file
);
550 bool modify_inplace
= false;
551 int message_mods
= 0;
554 /* Though the input file won't need to be opened if everything goes
555 well, do it here just in case there's a failure, and that failure is
556 running out of file descriptors. */
557 if ((*infp
= fopen (input_filename
, "r")) == NULL
) {
558 adios (input_filename
, "unable to open for reading");
561 if (outfile
== NULL
) {
562 modify_inplace
= true;
564 if ((*ctp
)->c_file
) {
566 /* outfp will be closed by the caller */
567 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, outfp
)) ==
569 die("unable to create temporary file in %s",
572 outfile
= mh_xstrdup (tempfile
);
574 die("missing both input and output filenames\n");
576 } /* else *outfp was defined by caller */
578 reverse_alternative_parts (*ctp
);
579 status
= fix_always (*ctp
, &message_mods
);
580 if (status
== OK
&& fx
->fixboundary
) {
581 status
= fix_boundary (ctp
, &message_mods
);
583 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
584 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
586 if (status
== OK
&& fx
->fixcompositecte
) {
587 status
= fix_composite_cte (*ctp
, &message_mods
);
589 if (status
== OK
&& fx
->reformat
) {
591 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
593 if (status
== OK
&& fx
->decodetext
) {
594 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
598 if (status
== OK
&& fx
->textcharset
!= NULL
) {
599 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
602 if (status
== OK
&& ! (*ctp
)->c_umask
) {
603 /* Set the umask for the contents file. This currently
604 isn't used but just in case it is in the future. */
607 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
608 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
610 (*ctp
)->c_umask
= ~m_gmprot();
615 * Write the content to a file
618 status
= write_content (*ctp
, input_filename
, outfile
, *outfp
,
619 modify_inplace
, message_mods
);
620 } else if (! modify_inplace
) {
621 /* Something went wrong. Output might be expected, such
622 as if this were run as a filter. Just copy the input
624 if (copy_input_to_output (input_filename
, *infp
, outfile
,
626 inform("unable to copy message to %s, it might be lost\n",
631 if (modify_inplace
) {
632 if (status
!= OK
) { (void) m_unlink (outfile
); }
639 free (input_filename
);
646 * Copy input message to output. Assumes not modifying in place, so this
647 * might be running as part of a pipeline.
650 copy_input_to_output (const char *input_filename
, FILE *infp
,
651 const char *output_filename
, FILE *outfp
)
653 int in
= fileno (infp
);
654 int out
= fileno (outfp
);
657 if (in
!= -1 && out
!= -1) {
658 cpydata (in
, out
, input_filename
, output_filename
);
668 * Fix mismatched outer level boundary.
671 fix_boundary (CT
*ct
, int *message_mods
)
673 struct multipart
*mp
;
676 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
677 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
680 * 1) Get boundary at end of part.
681 * 2) Get boundary at beginning of part and compare to the end-of-part
683 * 3) Write out contents of ct to tmp file, replacing boundary in
684 * header with boundary from part. Set c_unlink to 1.
686 * 5) Call parse_mime() on the tmp file, replacing ct.
689 if (mp
&& mp
->mp_start
) {
692 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
695 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
696 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
697 char *filename
= mh_xstrdup ((*ct
)->c_file
);
701 if ((fixed_ct
= parse_mime (fixed
))) {
707 report (NULL
, NULL
, filename
,
708 "fix multipart boundary");
712 inform("unable to parse fixed part");
717 inform("unable to replace broken boundary");
721 inform("unable to create temporary file in %s",
726 free (part_boundary
);
728 /* Couldn't fix the boundary. Report failure so that mhfixmsg
729 doesn't modify the message. */
733 /* No multipart struct, even though the content type is
734 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
745 * Find boundary at end of multipart.
748 get_multipart_boundary (CT ct
, char **part_boundary
)
750 char buffer
[NMH_BUFSIZ
];
751 char *end_boundary
= NULL
;
752 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
753 ? (off_t
) (ct
->c_end
- sizeof buffer
)
754 : (off_t
) ct
->c_begin
;
758 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
759 be big enough, even if it's just 1024, to make that unlikely. */
761 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
762 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
763 advise (ct
->c_file
, "unable to open for reading");
767 /* Get boundary at end of multipart. */
768 while (begin
>= (off_t
) ct
->c_begin
) {
769 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
770 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
771 char *cp
= rfind_str (buffer
, bytes_read
, "--");
776 /* Trim off trailing "--" and anything beyond. */
778 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
779 if (strlen (end
) > 3 && *end
++ == '\n' &&
780 *end
++ == '-' && *end
++ == '-') {
781 end_boundary
= mh_xstrdup (end
);
788 if (end_boundary
|| begin
<= (off_t
) (ct
->c_begin
+ sizeof buffer
))
790 begin
-= sizeof buffer
;
793 /* Get boundary at beginning of multipart. */
795 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
796 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
797 if (bytes_read
>= strlen (end_boundary
)) {
798 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
800 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
801 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
806 /* The start and end boundaries didn't match, or the
807 start boundary doesn't begin with "\n--" (or "--"
808 if at the beginning of buffer). Keep trying. */
822 *part_boundary
= end_boundary
;
824 *part_boundary
= NULL
;
833 * Open and copy ct->c_file to file, replacing the multipart boundary.
836 replace_boundary (CT ct
, char *file
, char *boundary
)
840 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
842 m_getfld_state_t gstate
;
845 if (ct
->c_file
== NULL
) {
846 inform("missing input filename");
850 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
851 advise (ct
->c_file
, "unable to open for reading");
855 if ((fpout
= fopen (file
, "w")) == NULL
) {
857 advise (file
, "unable to open for writing");
861 gstate
= m_getfld_state_init(fpin
);
862 for (compnum
= 1;;) {
863 int bufsz
= (int) sizeof buf
;
865 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
870 /* get copies of the buffers */
871 np
= mh_xstrdup (name
);
872 vp
= mh_xstrdup (buf
);
874 /* if necessary, get rest of field */
875 while (state
== FLDPLUS
) {
877 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
878 vp
= add (buf
, vp
); /* add to previous value */
881 if (strcasecmp (TYPE_FIELD
, np
)) {
882 fprintf (fpout
, "%s:%s", np
, vp
);
884 char *new_ctline
, *new_params
;
886 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
887 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
890 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
891 ct
->c_ctinfo
.ci_subtype
, NULL
);
892 new_params
= output_params(LEN(TYPE_FIELD
) +
893 strlen(new_ctline
) + 1,
894 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
895 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
896 FENDNULL(new_params
));
908 /* buf will have a terminating NULL, skip it. */
909 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
910 advise (file
, "fwrite");
919 inform("message format error in component #%d", compnum
);
924 inform("getfld() returned %d", state
);
932 m_getfld_state_destroy (&gstate
);
941 * Fix Content-Type header to reflect the content of its part.
944 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
)
948 switch (ct
->c_type
) {
950 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
953 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
954 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
960 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
961 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
963 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
970 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
971 for (typep
= svector_strs (fixtypes
);
972 typep
&& (type
= *typep
);
975 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
978 if (! strcasecmp (type
, type_subtype
) &&
979 decode_part (ct
) == OK
&&
980 ct
->c_cefile
.ce_file
!= NULL
) {
981 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
984 if ((cp
= strchr (ct_type_subtype
, ';'))) {
985 /* Truncate to remove any parameter list from
986 mime_type () result. */
990 if (strcasecmp (type
, ct_type_subtype
)) {
991 char *ct_type
, *ct_subtype
;
994 /* The Content-Type header does not match the
995 content, so update these struct Content
998 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
1001 /* Extract type and subtype from type/subtype. */
1002 ct_type
= mh_xstrdup(ct_type_subtype
);
1003 if ((cp
= strchr (ct_type
, '/'))) {
1005 ct_subtype
= mh_xstrdup(++cp
);
1007 inform("missing / in MIME type of %s %s",
1008 ct
->c_file
, ct
->c_partno
);
1013 ct
->c_type
= ct_str_type (ct_type
);
1014 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
1016 free (ct
->c_ctinfo
.ci_type
);
1017 ct
->c_ctinfo
.ci_type
= ct_type
;
1018 free (ct
->c_ctinfo
.ci_subtype
);
1019 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
1020 if (! replace_substring (&ct
->c_ctline
, type
,
1022 inform("did not find %s in %s",
1023 type
, ct
->c_ctline
);
1026 /* Update Content-Type header field. */
1027 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1028 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1029 if (replace_substring (&hf
->value
, type
,
1033 report (NULL
, ct
->c_partno
, ct
->c_file
,
1034 "change Content-Type in header "
1036 type
, ct_type_subtype
);
1040 inform("did not find %s in %s", type
, hf
->value
);
1044 free (ct_type_subtype
);
1046 free (type_subtype
);
1056 * Replace a substring, allocating space to hold the new one.
1059 replace_substring (char **str
, const char *old
, const char *new)
1063 if ((cp
= strstr (*str
, old
))) {
1064 char *remainder
= cp
+ strlen (old
);
1065 char *prefix
, *new_str
;
1068 prefix
= mh_xstrdup(*str
);
1069 *(prefix
+ (cp
- *str
)) = '\0';
1070 new_str
= concat (prefix
, new, remainder
, NULL
);
1073 new_str
= concat (new, remainder
, NULL
);
1078 return *str
= new_str
;
1086 * Remove a name=value parameter, given just its name, from a header value.
1089 remove_parameter (char *str
, const char *name
)
1091 /* It looks to me, based on the BNF in RFC 2045, than there can't
1092 be whitespace between the parameter name and the "=", or
1093 between the "=" and the parameter value. */
1094 char *param_name
= concat (name
, "=", NULL
);
1097 if ((cp
= strstr (str
, param_name
))) {
1101 /* Remove any leading spaces, before the parameter name. */
1103 start
> str
&& isspace ((unsigned char) *(start
-1));
1107 /* Remove a leading semicolon. */
1108 if (start
> str
&& *(start
-1) == ';') { --start
; }
1110 end
= cp
+ strlen (name
) + 1;
1112 /* Skip past the quoted value, and then the final quote. */
1113 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1116 /* Skip past the value. */
1117 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1120 /* Count how many characters need to be moved. Include
1121 trailing null, which is accounted for by the
1122 initialization of count to 1. */
1123 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1124 (void) memmove (start
, end
, count
);
1134 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1135 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1139 fix_composite_cte (CT ct
, int *message_mods
)
1143 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1144 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1145 ct
->c_encoding
!= CE_BINARY
) {
1148 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1149 char *name
= hf
->name
;
1150 for (; isspace((unsigned char)*name
); ++name
) {
1154 if (! strncasecmp (name
, ENCODING_FIELD
,
1155 LEN(ENCODING_FIELD
))) {
1156 char *prefix
= "Nmh-REPLACED-INVALID-";
1160 h
->name
= mh_xstrdup (hf
->name
);
1161 h
->hf_encoding
= hf
->hf_encoding
;
1165 /* Retain old header but prefix its name. */
1167 hf
->name
= concat (prefix
, h
->name
, NULL
);
1171 char *encoding
= cpytrim (hf
->value
);
1172 report (NULL
, ct
->c_partno
, ct
->c_file
,
1173 "replace Content-Transfer-Encoding of %s "
1174 "with 8 bit", encoding
);
1178 h
->value
= mh_xstrdup (" 8bit\n");
1180 /* Don't need to warn for multiple C-T-E header
1181 fields, parse_mime() already does that. But
1182 if there are any, fix them all as necessary. */
1187 set_ce (ct
, CE_8BIT
);
1190 if (ct
->c_type
== CT_MULTIPART
) {
1191 struct multipart
*m
;
1194 m
= (struct multipart
*) ct
->c_ctparams
;
1195 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1196 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1209 * Set content encoding.
1212 set_ce (CT ct
, int encoding
)
1214 const char *ce
= ce_str (encoding
);
1215 const struct str2init
*ctinit
= get_ce_method (ce
);
1218 char *cte
= concat (" ", ce
, "\n", NULL
);
1219 bool found_cte
= false;
1221 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1222 caller is decode_text_parts (). Save because we'll
1224 struct cefile decoded_content_info
= ct
->c_cefile
;
1226 ct
->c_encoding
= encoding
;
1228 ct
->c_ctinitfnx
= ctinit
->si_init
;
1229 /* This will assign ct->c_cefile with an all-0 struct, which
1231 (*ctinit
->si_init
) (ct
);
1232 /* After returning, the caller should set
1233 ct->c_cefile.ce_file to the name of the file containing
1236 if (ct
->c_ceclosefnx
) {
1237 (*ct
->c_ceclosefnx
) (ct
);
1240 /* Restore the cefile. */
1241 ct
->c_cefile
= decoded_content_info
;
1243 /* Update/add Content-Transfer-Encoding header field. */
1244 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1245 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1252 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1255 /* Update c_celine. It's used only by mhlist -debug. */
1256 free (ct
->c_celine
);
1257 ct
->c_celine
= mh_xstrdup (cte
);
1267 * Make sure each text part has a corresponding text/plain part.
1270 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
)
1274 switch ((*ct
)->c_type
) {
1276 /* Nothing to do for text/plain. */
1277 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1279 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1280 parent
->c_subtype
== MULTI_ALTERNATE
) {
1281 int new_subpart_number
= 1;
1282 int has_text_plain
=
1283 find_textplain_sibling (parent
, replacetextplain
,
1284 &new_subpart_number
);
1286 if (! has_text_plain
) {
1287 /* Parent is a multipart/alternative. Insert a new
1288 text/plain subpart. */
1289 const int inserted
=
1290 insert_new_text_plain_part (*ct
, new_subpart_number
,
1295 report (NULL
, parent
->c_partno
, parent
->c_file
,
1296 "insert text/plain part");
1302 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1303 parent
->c_subtype
== MULTI_RELATED
) {
1304 char *type_subtype
=
1305 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1306 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1307 const char *parent_type
=
1308 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1309 int new_subpart_number
= 1;
1310 int has_text_plain
= 0;
1312 /* Have to do string comparison on the subtype because we
1313 don't enumerate all of them in c_subtype values.
1314 parent_type will be NULL if the multipart/related part
1315 doesn't have a type parameter. The type parameter must
1316 be specified according to RFC 2387 Sec. 3.1 but not all
1318 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1319 /* The type of this part matches the root type of the
1320 parent multipart/related. Look to see if there's
1321 text/plain sibling. */
1323 find_textplain_sibling (parent
, replacetextplain
,
1324 &new_subpart_number
);
1327 free (type_subtype
);
1329 if (! has_text_plain
) {
1330 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1334 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1335 if (*ct
!= part
->mp_part
) {
1341 /* Parent is a multipart/related. Insert a new
1342 text/plain subpart in a new multipart/alternative. */
1343 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1344 /* Not an error if text/plain couldn't be added. */
1347 /* There are no siblings, so insert a new text/plain
1348 subpart, and change the parent type from
1349 multipart/related to multipart/alternative. */
1350 const int inserted
=
1351 insert_new_text_plain_part (*ct
, new_subpart_number
,
1357 parent
->c_subtype
= MULTI_ALTERNATE
;
1358 free (parent
->c_ctinfo
.ci_subtype
);
1359 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1360 if (! replace_substring (&parent
->c_ctline
, "/related",
1362 inform("did not find multipart/related in %s",
1366 /* Update Content-Type header field. */
1367 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1368 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1369 if (replace_substring (&hf
->value
, "/related",
1373 report (NULL
, parent
->c_partno
,
1375 "insert text/plain part");
1378 /* Remove, e.g., type="text/html" from
1379 multipart/alternative. */
1380 remove_parameter (hf
->value
, "type");
1383 inform("did not find multipart/"
1384 "related in header %s", hf
->value
);
1388 /* Not an error if text/plain couldn't be inserted. */
1393 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1400 case CT_MULTIPART
: {
1401 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1404 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1405 if ((*ct
)->c_type
== CT_MULTIPART
) {
1406 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1414 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1415 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1417 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1428 * See if there is a sibling text/plain, and return its subpart number.
1431 find_textplain_sibling (CT parent
, int replacetextplain
,
1432 int *new_subpart_number
)
1434 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1435 struct part
*part
, *prev
;
1436 bool has_text_plain
= false;
1438 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1439 ++*new_subpart_number
;
1440 if (part
->mp_part
->c_type
== CT_TEXT
&&
1441 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1442 if (replacetextplain
) {
1443 struct part
*old_part
;
1444 if (part
== mp
->mp_parts
) {
1445 old_part
= mp
->mp_parts
;
1446 mp
->mp_parts
= part
->mp_next
;
1448 old_part
= prev
->mp_next
;
1449 prev
->mp_next
= part
->mp_next
;
1452 report (NULL
, parent
->c_partno
, parent
->c_file
,
1453 "remove text/plain part %s",
1454 old_part
->mp_part
->c_partno
);
1456 free_content (old_part
->mp_part
);
1459 has_text_plain
= true;
1466 return has_text_plain
;
1471 * Insert a new text/plain part.
1474 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
)
1476 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1477 struct part
*new_part
;
1480 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1482 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1484 new_part
->mp_next
= mp
->mp_parts
;
1485 mp
->mp_parts
= new_part
;
1486 new_part
->mp_part
->c_partno
=
1487 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1493 free_content (new_part
->mp_part
);
1501 * Create a text/plain part to go along with non-plain sibling part.
1504 build_text_plain_part (CT encoded_part
)
1506 CT tp_part
= divide_part (encoded_part
);
1507 char *tmp_plain_file
= NULL
;
1509 if (decode_part (tp_part
) == OK
) {
1510 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1511 contains the decoded contents. And the decoding function, such
1512 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1513 be unlinked by free_content (). */
1516 /* This m_mktemp2() call closes the temp file. */
1517 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1518 inform("unable to create temporary file in %s",
1521 tmp_plain_file
= mh_xstrdup (tempfile
);
1522 if (reformat_part (tp_part
, tmp_plain_file
,
1523 tp_part
->c_ctinfo
.ci_type
,
1524 tp_part
->c_ctinfo
.ci_subtype
,
1525 tp_part
->c_type
) == OK
) {
1531 free_content (tp_part
);
1532 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1533 free (tmp_plain_file
);
1540 * Slip new text/plain part into a new multipart/alternative.
1543 insert_into_new_mp_alt (CT
*ct
, int *message_mods
)
1545 CT tp_part
= build_text_plain_part (*ct
);
1549 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1552 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1554 if (mp
&& mp
->mp_parts
) {
1555 mp
->mp_parts
->mp_part
= tp_part
;
1556 /* Make the new multipart/alternative the parent. */
1561 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1562 "insert text/plain part");
1565 free_content (tp_part
);
1566 free_content (mp_alt
);
1573 /* Not an error if text/plain couldn't be built. */
1581 * Clone a MIME part.
1589 /* Just copy over what is needed for decoding. c_vrsn and
1590 c_celine aren't necessary. */
1591 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1592 new_part
->c_begin
= ct
->c_begin
;
1593 new_part
->c_end
= ct
->c_end
;
1594 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1595 new_part
->c_type
= ct
->c_type
;
1596 new_part
->c_cefile
= ct
->c_cefile
;
1597 new_part
->c_encoding
= ct
->c_encoding
;
1598 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1599 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1600 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1601 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1603 /* c_ctline is used by reformat__part(), so it can preserve
1604 anything after the type/subtype. */
1605 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1612 * Copy the content info from one part to another.
1615 copy_ctinfo (CI dest
, CI src
)
1619 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1620 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1622 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1623 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1625 if (s_pm
->pm_charset
) {
1626 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1628 if (s_pm
->pm_lang
) {
1629 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1633 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1634 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1649 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, &file
)) == NULL
) {
1650 die("unable to create temporary file in %s", get_temp_dir());
1652 tmp_decoded
= mh_xstrdup (tempfile
);
1653 /* The following call will load ct->c_cefile.ce_file with the tmp
1654 filename of the decoded content. tmp_decoded will contain the
1655 encoded output, get rid of that. */
1656 status
= output_message_fp (ct
, file
, tmp_decoded
);
1657 (void) m_unlink (tmp_decoded
);
1659 if (fclose (file
)) {
1660 inform("unable to close temporary file %s, continuing...", tempfile
);
1668 * Reformat content as plain text.
1669 * Some of the arguments aren't really needed now, but maybe will
1670 * be in the future for other than text types.
1673 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
)
1675 int output_subtype
, output_encoding
;
1676 const char *reason
= NULL
;
1680 /* Hacky: this redirects the output from whatever command is used
1681 to show the part to a file. So, the user can't have any output
1682 redirection in that command.
1683 Could show_multi() in mhshowsbr.c avoid this? */
1685 /* Check for invo_name-format-type/subtype. */
1686 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1688 inform("Don't know how to convert %s, there is no "
1689 "%s-format-%s/%s profile entry",
1690 ct
->c_file
, invo_name
, type
, subtype
);
1694 if (strchr (cf
, '>')) {
1695 inform("'>' prohibited in \"%s\",\nplease fix your "
1696 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1702 cp
= concat (cf
, " >", file
, NULL
);
1703 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1706 /* Unlink decoded content tmp file and free its filename to avoid
1707 leaks. The file stream should already have been closed. */
1708 if (ct
->c_cefile
.ce_unlink
) {
1709 (void) m_unlink (ct
->c_cefile
.ce_file
);
1710 free (ct
->c_cefile
.ce_file
);
1711 ct
->c_cefile
.ce_file
= NULL
;
1712 ct
->c_cefile
.ce_unlink
= 0;
1715 if (c_type
== CT_TEXT
) {
1716 output_subtype
= TEXT_PLAIN
;
1718 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1722 output_encoding
= content_encoding (ct
, &reason
);
1724 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1725 ct
->c_cefile
.ce_file
= file
;
1726 ct
->c_cefile
.ce_unlink
= 1;
1728 ct
->c_cefile
.ce_unlink
= 0;
1737 * Fill in a multipart/alternative part.
1740 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
)
1742 char *boundary_prefix
= "----=_nmh-multipart";
1743 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1744 char *boundary_indicator
= "; boundary=";
1745 char *typename
, *subtypename
, *name
;
1748 struct multipart
*m
;
1749 const struct str2init
*ctinit
;
1753 /* Set up the multipart/alternative part. These fields of *ct were
1754 initialized to 0 by mh_xcalloc():
1755 c_fp, c_unlink, c_begin, c_end,
1756 c_vrsn, c_ctline, c_celine,
1757 c_id, c_descr, c_dispo, c_partno,
1758 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1759 c_cefile, c_encoding,
1760 c_digested, c_digest[16], c_ctexbody,
1761 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1763 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1766 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1768 ct
->c_subtype
= subtype
;
1770 ctinit
= get_ct_init (ct
->c_type
);
1772 typename
= ct_type_str (type
);
1773 subtypename
= ct_subtype_str (type
, subtype
);
1777 int found_boundary
= 1;
1779 while (found_boundary
&& serial
< 1000000) {
1782 /* Ensure that the boundary doesn't appear in the decoded
1784 if (new_part
->c_cefile
.ce_file
) {
1785 if ((found_boundary
=
1786 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1787 new_part
->c_cefile
.ce_file
,
1788 boundary
)) == NOTOK
) {
1793 /* Ensure that the boundary doesn't appear in the encoded
1795 if (! found_boundary
&& new_part
->c_file
) {
1796 if ((found_boundary
=
1797 boundary_in_content (&new_part
->c_fp
,
1799 boundary
)) == NOTOK
) {
1804 if (found_boundary
) {
1805 /* Try a slightly different boundary. */
1810 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1812 concat (boundary_prefix
,
1813 FENDNULL(first_alt
->c_partno
),
1814 "-", buffer2
, NULL
);
1818 if (found_boundary
) {
1819 inform("giving up trying to find a unique boundary");
1824 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1825 boundary
, "\"", NULL
);
1827 /* Load c_first_hf and c_last_hf. */
1828 transfer_noncontent_headers (first_alt
, ct
);
1829 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1832 /* Load c_partno. */
1833 if (first_alt
->c_partno
) {
1834 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1835 free (first_alt
->c_partno
);
1836 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1837 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1839 first_alt
->c_partno
= mh_xstrdup ("1");
1840 new_part
->c_partno
= mh_xstrdup ("2");
1844 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1845 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1848 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1849 "boundary", boundary
, 0);
1853 p
->mp_next
->mp_next
= NULL
;
1854 p
->mp_next
->mp_part
= first_alt
;
1857 m
->mp_start
= concat (boundary
, "\n", NULL
);
1858 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1874 * Check that the boundary does not appear in the content.
1877 boundary_in_content (FILE **fp
, char *file
, const char *boundary
)
1879 char buffer
[NMH_BUFSIZ
];
1881 bool found_boundary
= false;
1883 /* free_content() will close *fp if we fopen it here. */
1884 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1885 advise (file
, "unable to open %s for reading", file
);
1889 fseeko (*fp
, 0L, SEEK_SET
);
1890 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1891 if (find_str (buffer
, bytes_read
, boundary
)) {
1892 found_boundary
= true;
1897 return found_boundary
;
1902 * Remove all non-Content headers.
1905 transfer_noncontent_headers (CT old
, CT
new)
1909 hp_prev
= hp
= old
->c_first_hf
;
1913 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, LEN(XXX_FIELD_PRF
))) {
1914 if (hp
== old
->c_last_hf
) {
1915 if (hp
== old
->c_first_hf
) {
1916 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1918 hp_prev
->next
= NULL
;
1919 old
->c_last_hf
= hp_prev
;
1922 if (hp
== old
->c_first_hf
) {
1923 old
->c_first_hf
= next
;
1925 hp_prev
->next
= next
;
1929 /* Put node hp in the new CT. */
1930 if (new->c_first_hf
== NULL
) {
1931 new->c_first_hf
= hp
;
1933 new->c_last_hf
->next
= hp
;
1935 new->c_last_hf
= hp
;
1937 /* A Content- header, leave in old. */
1950 set_ct_type (CT ct
, int type
, int subtype
, int encoding
)
1952 char *typename
= ct_type_str (type
);
1953 char *subtypename
= ct_subtype_str (type
, subtype
);
1954 /* E.g, " text/plain" */
1955 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1956 /* E.g, " text/plain\n" */
1957 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1958 bool found_content_type
= false;
1960 const char *cp
= NULL
;
1964 /* Update/add Content-Type header field. */
1965 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1966 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1967 found_content_type
= true;
1969 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1970 ? concat (type_subtypename
, cp
, "\n", NULL
)
1971 : mh_xstrdup (name_plus_nl
);
1974 if (! found_content_type
) {
1975 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
1976 (cp
= strchr (ct
->c_ctline
, ';'))
1977 ? concat (type_subtypename
, cp
, "\n", NULL
)
1978 : mh_xstrdup (name_plus_nl
));
1981 /* Some of these might not be used, but set them anyway. */
1983 ? concat (type_subtypename
, cp
, NULL
)
1984 : concat (type_subtypename
, NULL
);
1985 free (ct
->c_ctline
);
1986 ct
->c_ctline
= ctline
;
1987 /* Leave other ctinfo members as they were. */
1988 free (ct
->c_ctinfo
.ci_type
);
1989 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1990 free (ct
->c_ctinfo
.ci_subtype
);
1991 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1993 ct
->c_subtype
= subtype
;
1995 free (name_plus_nl
);
1996 free (type_subtypename
);
1998 status
= set_ce (ct
, encoding
);
2005 * It's not necessary to update the charset parameter of a Content-Type
2006 * header for a text part. According to RFC 2045 Sec. 6.4, the body
2007 * (content) was originally in the specified charset, "and will be in
2008 * that character set again after decoding."
2011 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
2015 int lf_line_endings
= 0;
2017 switch (ct
->c_type
) {
2018 case CT_MULTIPART
: {
2019 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2022 /* Should check to see if the body for this part is encoded?
2023 For now, it gets passed along as-is by InitMultiPart(). */
2024 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2025 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
2032 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2033 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2035 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
2041 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2046 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
2048 switch (ct
->c_encoding
) {
2053 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
2054 const char *reason
= NULL
;
2056 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
2057 && encoding
!= CE_BINARY
) {
2058 /* The decoding isn't acceptable so discard it.
2059 Leave status as OK to allow other transformations. */
2061 report (NULL
, ct
->c_partno
, ct
->c_file
,
2062 "will not decode%s because it is binary (%s)",
2064 : (FENDNULL(ct
->c_ctline
)),
2067 (void) m_unlink (ct
->c_cefile
.ce_file
);
2068 free (ct
->c_cefile
.ce_file
);
2069 ct
->c_cefile
.ce_file
= NULL
;
2070 } else if (ct
->c_encoding
== CE_QUOTED
&&
2071 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2072 /* The decoding isn't acceptable so discard it.
2073 Leave status as OK to allow other transformations. */
2075 report (NULL
, ct
->c_partno
, ct
->c_file
,
2076 "will not decode%s because it is 8bit",
2078 : (FENDNULL(ct
->c_ctline
)));
2080 (void) m_unlink (ct
->c_cefile
.ce_file
);
2081 free (ct
->c_cefile
.ce_file
);
2082 ct
->c_cefile
.ce_file
= NULL
;
2086 if (ct_encoding
== CE_BINARY
) {
2088 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2093 if (set_ce (ct
, enc
) == OK
) {
2096 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2097 FENDNULL(ct
->c_ctline
));
2099 if (lf_line_endings
) {
2100 strip_crs (ct
, message_mods
);
2113 if (lf_line_endings
) {
2114 strip_crs (ct
, message_mods
);
2129 * Determine if the part with type[/subtype] should be decoded, according to
2130 * decodetypes (which came from the -decodetypes switch).
2133 should_decode(const char *decodetypes
, const char *type
, const char *subtype
)
2135 /* Quick search for matching type[/subtype] in decodetypes: bracket
2136 decodetypes with commas, then search for ,type, and ,type/subtype, in
2139 bool found_match
= false;
2140 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2141 char *delimited_type
= concat(",", type
, ",", NULL
);
2143 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2145 } else if (subtype
!= NULL
) {
2146 char *delimited_type_subtype
=
2147 concat(",", type
, "/", subtype
, ",", NULL
);
2149 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2152 free(delimited_type_subtype
);
2155 free(delimited_type
);
2156 free(delimited_decodetypes
);
2163 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2164 * if it has any NUL characters, a CR not followed by a LF, or lines
2165 * greater than 998 characters in length. If binary, reason is set
2166 * to a string explaining why.
2169 content_encoding (CT ct
, const char **reason
)
2171 CE ce
= &ct
->c_cefile
;
2172 int encoding
= CE_7BIT
;
2175 size_t line_len
= 0;
2176 char buffer
[NMH_BUFSIZ
];
2179 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2180 advise (ce
->ce_file
, "unable to open for reading");
2184 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2185 while (encoding
!= CE_BINARY
&&
2186 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2189 int last_char_was_cr
= 0;
2191 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2192 if (*cp
== '\0' || ++line_len
> 998 ||
2193 (*cp
!= '\n' && last_char_was_cr
)) {
2194 encoding
= CE_BINARY
;
2196 *reason
= "null character";
2197 } else if (line_len
> 998) {
2198 *reason
= "line length > 998";
2199 } else if (*cp
!= '\n' && last_char_was_cr
) {
2200 *reason
= "CR not followed by LF";
2202 /* Should not reach this. */
2209 } else if (! isascii ((unsigned char) *cp
)) {
2213 last_char_was_cr
= *cp
== '\r';
2219 } /* else should never happen */
2226 * Strip carriage returns from content.
2229 strip_crs (CT ct
, int *message_mods
)
2231 char *charset
= content_charset (ct
);
2234 /* Only strip carriage returns if content is ASCII or another
2235 charset that has the same readily recognizable CR followed by a
2236 LF. We can include UTF-8 here because if the high-order bit of
2237 a UTF-8 byte is 0, then it must be a single-byte ASCII
2239 if (! strcasecmp (charset
, "US-ASCII") ||
2240 ! strcasecmp (charset
, "UTF-8") ||
2241 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2242 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2247 bool has_crs
= false;
2248 bool opened_input_file
= false;
2250 if (ct
->c_cefile
.ce_file
) {
2251 file
= &ct
->c_cefile
.ce_file
;
2252 fp
= &ct
->c_cefile
.ce_fp
;
2254 } else if (ct
->c_file
) {
2257 begin
= (size_t) ct
->c_begin
;
2258 end
= (size_t) ct
->c_end
;
2259 } /* else don't know where the content is */
2261 if (file
&& *file
&& fp
) {
2263 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2264 advise (*file
, "unable to open for reading");
2267 opened_input_file
= true;
2273 char buffer
[NMH_BUFSIZ
];
2275 size_t bytes_to_read
=
2276 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2278 fseeko (*fp
, begin
, SEEK_SET
);
2279 while ((bytes_read
= fread (buffer
, 1,
2280 min (bytes_to_read
, sizeof buffer
),
2282 /* Look for CR followed by a LF. This is supposed to
2283 be text so there should be LF's. If not, don't
2284 modify the content. */
2287 bool last_char_was_cr
= false;
2289 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2291 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2292 if (*cp
== '\n' && last_char_was_cr
) {
2297 last_char_was_cr
= *cp
== '\r';
2303 char *stripped_content_file
;
2304 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2306 if (tempfile
== NULL
) {
2307 die("unable to create temporary file in %s",
2310 stripped_content_file
= mh_xstrdup (tempfile
);
2312 /* Strip each CR before a LF from the content. */
2313 fseeko (*fp
, begin
, SEEK_SET
);
2314 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2318 bool last_char_was_cr
= false;
2320 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2322 last_char_was_cr
= true;
2323 } else if (last_char_was_cr
) {
2325 if (write (fd
, "\r", 1) < 0) {
2326 advise (tempfile
, "CR write");
2329 if (write (fd
, cp
, 1) < 0) {
2330 advise (tempfile
, "write");
2332 last_char_was_cr
= false;
2334 if (write (fd
, cp
, 1) < 0) {
2335 advise (tempfile
, "write");
2337 last_char_was_cr
= false;
2343 inform("unable to write temporary file %s, continuing...",
2344 stripped_content_file
);
2345 (void) m_unlink (stripped_content_file
);
2346 free(stripped_content_file
);
2349 /* Replace the decoded file with the converted one. */
2350 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2351 (void) m_unlink (ct
->c_cefile
.ce_file
);
2353 free(ct
->c_cefile
.ce_file
);
2354 ct
->c_cefile
.ce_file
= stripped_content_file
;
2355 ct
->c_cefile
.ce_unlink
= 1;
2359 report (NULL
, ct
->c_partno
,
2360 begin
== 0 && end
== 0 ? "" : *file
,
2366 if (opened_input_file
) {
2380 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2381 * of the part C-T-E's.
2386 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2388 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2389 least_restrictive_enc
!= CE_7BIT
) {
2390 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2392 bool found_cte
= false;
2394 /* Update/add Content-Transfer-Encoding header field. */
2395 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2396 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2403 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2410 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2414 least_restrictive_encoding (CT ct
)
2416 int encoding
= CE_UNKNOWN
;
2418 switch (ct
->c_type
) {
2419 case CT_MULTIPART
: {
2420 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2423 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2424 const int part_encoding
=
2425 least_restrictive_encoding (part
->mp_part
);
2427 if (less_restrictive (encoding
, part_encoding
)) {
2428 encoding
= part_encoding
;
2435 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2436 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2437 const int part_encoding
=
2438 least_restrictive_encoding (e
->eb_content
);
2440 if (less_restrictive (encoding
, part_encoding
)) {
2441 encoding
= part_encoding
;
2447 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2448 encoding
= ct
->c_encoding
;
2457 * Return whether the second encoding is less restrictive than the first, where
2458 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2459 * CE_BINARY is less restrictive than CE_8BIT and
2460 * CE_8BIT is less restrictive than CE_7BIT.
2463 less_restrictive (int encoding
, int second_encoding
)
2465 switch (second_encoding
) {
2467 return encoding
!= CE_BINARY
;
2469 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2471 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2472 encoding
!= CE_7BIT
;
2480 * Convert character set of each part.
2483 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
)
2487 switch (ct
->c_type
) {
2489 if (ct
->c_subtype
== TEXT_PLAIN
) {
2490 status
= convert_charset (ct
, dest_charset
, message_mods
);
2493 char *ct_charset
= content_charset (ct
);
2495 report (NULL
, ct
->c_partno
, ct
->c_file
,
2496 "convert %s to %s", ct_charset
, dest_charset
);
2500 char *ct_charset
= content_charset (ct
);
2502 report ("iconv", ct
->c_partno
, ct
->c_file
,
2503 "failed to convert %s to %s", ct_charset
, dest_charset
);
2509 case CT_MULTIPART
: {
2510 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2513 /* Should check to see if the body for this part is encoded?
2514 For now, it gets passed along as-is by InitMultiPart(). */
2515 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2517 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2523 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2524 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2527 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2540 * Fix various problems that aren't handled elsewhere. These
2541 * are fixed unconditionally: there are no switches to disable
2542 * them. Currently, "problems" are these:
2543 * 1) remove extraneous semicolon at the end of a header parameter list
2544 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2545 * filename parameters in Content-Type and Content-Disposition
2546 * headers, respectively.
2549 fix_always (CT ct
, int *message_mods
)
2553 switch (ct
->c_type
) {
2554 case CT_MULTIPART
: {
2555 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2558 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2559 status
= fix_always (part
->mp_part
, message_mods
);
2565 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2566 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2568 status
= fix_always (e
->eb_content
, message_mods
);
2575 if (ct
->c_first_hf
) {
2576 fix_filename_encoding (ct
);
2579 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2580 size_t len
= strlen (hf
->value
);
2582 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2583 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2584 /* Only do this for Content-Type and
2585 Content-Disposition fields because those are the
2586 only headers that parse_mime() warns about. */
2590 /* whitespace following a trailing ';' will be nuked as well */
2591 if (hf
->value
[len
- 1] == '\n') {
2592 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2593 if (len
-- == 0) { break; }
2597 if (hf
->value
[len
- 2] == ';') {
2598 /* Remove trailing ';' from parameter value. */
2599 hf
->value
[len
- 2] = '\n';
2600 hf
->value
[len
- 1] = '\0';
2602 /* Also, if Content-Type parameter, remove trailing ';'
2603 from ct->c_ctline. This probably isn't necessary
2605 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2606 size_t l
= strlen(ct
->c_ctline
) - 1;
2607 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2608 ct
->c_ctline
[l
] == ';') {
2609 ct
->c_ctline
[l
--] = '\0';
2610 if (l
== 0) { break; }
2616 report (NULL
, ct
->c_partno
, ct
->c_file
,
2617 "remove trailing ; from %s parameter value",
2629 * Factor out common code for loops in fix_filename_encoding().
2632 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
)
2636 if (has_prefix(value
, "=?") && has_suffix(value
, "?=")) {
2637 /* Looks like an RFC 2047 encoded parameter. */
2638 char decoded
[PATH_MAX
+ 1];
2640 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2641 /* Encode using RFC 2231. */
2642 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2645 inform("failed to decode %s parameter %s", name
, value
);
2654 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2655 * filename parameters in Content-Type and Content-Disposition
2656 * headers, respectively.
2659 fix_filename_encoding (CT ct
)
2665 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2666 if (pm
->pm_name
&& pm
->pm_value
&&
2667 strcasecmp (pm
->pm_name
, "name") == 0) {
2668 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2669 &ct
->c_ctinfo
.ci_first_pm
,
2670 &ct
->c_ctinfo
.ci_last_pm
);
2674 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2675 if (pm
->pm_name
&& pm
->pm_value
&&
2676 strcasecmp (pm
->pm_name
, "filename") == 0) {
2677 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2683 /* Fix hf values to correspond. */
2684 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2685 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2687 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2688 field
= TYPE_HEADER
;
2689 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2690 field
= DISPO_HEADER
;
2693 if (field
!= OTHER
) {
2694 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2696 if (semicolon_loc
) {
2698 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2699 const char *const params
=
2701 field
== TYPE_HEADER
2702 ? ct
->c_ctinfo
.ci_first_pm
2703 : ct
->c_dispo_first
,
2705 const char *const new_params
= concat (params
, "\n", NULL
);
2707 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2708 free((void *)new_params
); /* Cast away const. Sigh. */
2709 free((void *)params
);
2711 inform("did not find semicolon in %s:%s\n",
2712 hf
->name
, hf
->value
);
2722 * Output content in input file to output file.
2725 write_content (CT ct
, const char *input_filename
, char *outfile
, FILE *outfp
,
2726 int modify_inplace
, int message_mods
)
2730 if (modify_inplace
) {
2731 if (message_mods
> 0) {
2732 if ((status
= output_message_fp (ct
, outfp
, outfile
)) == OK
) {
2733 char *infile
= input_filename
2734 ? mh_xstrdup (input_filename
)
2735 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2737 if (remove_file (infile
) == OK
) {
2738 if (rename (outfile
, infile
)) {
2739 /* Rename didn't work, possibly because of an
2740 attempt to rename across filesystems. Try
2741 brute force copy. */
2742 int old
= open (outfile
, O_RDONLY
);
2744 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2747 if (old
!= -1 && new != -1) {
2748 char buffer
[NMH_BUFSIZ
];
2750 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2752 if (write (new, buffer
, i
) != i
) {
2758 if (new != -1) { close (new); }
2759 if (old
!= -1) { close (old
); }
2760 (void) m_unlink (outfile
);
2763 /* The -file argument processing used path() to
2764 expand filename to absolute path. */
2765 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2767 inform("unable to rename %s %s to %s, continuing...",
2768 file
? "file" : "message", outfile
,
2774 inform("unable to remove input file %s, "
2775 "not modifying it, continuing...", infile
);
2776 (void) m_unlink (outfile
);
2785 /* No modifications and didn't need the tmp outfile. */
2786 (void) m_unlink (outfile
);
2789 /* Output is going to some file. Produce it whether or not
2790 there were modifications. */
2791 status
= output_message_fp (ct
, outfp
, outfile
);
2800 * parse_mime() does not set lf_line_endings in struct text, so use this
2801 * function to do it. It touches the parts the decodetypes identifies.
2804 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
)
2806 switch (ct
->c_type
) {
2807 case CT_MULTIPART
: {
2808 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2811 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2812 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2818 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2819 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2821 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2826 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2827 if (ct
->c_ctparams
== NULL
) {
2828 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2830 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2837 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2838 * use the standard MH backup file.
2841 remove_file (const char *file
)
2844 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2845 int status
= system (rmm_command
);
2848 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2850 /* This is OK for a non-message file, it still uses the
2851 BACKUP_PREFIX form. The backup file will be in the same
2852 directory as file. */
2853 return rename (file
, m_backup (file
));
2858 * Output formatted message to user.
2861 report (char *what
, char *partno
, char *filename
, char *message
, ...)
2867 va_start (args
, message
);
2868 fmt
= concat (filename
, partno
? " part " : ", ",
2869 FENDNULL(partno
), partno
? ", " : "", message
, NULL
);
2871 advertise (what
, NULL
, fmt
, args
);
2884 fprintf (stderr
, "\n");