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_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/concat.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_setcur.h"
16 #include "sbr/seq_save.h"
17 #include "sbr/smatch.h"
18 #include "sbr/fmt_rfc2047.h"
19 #include "sbr/cpydata.h"
20 #include "sbr/trimcpy.h"
21 #include "sbr/m_convert.h"
22 #include "sbr/m_backup.h"
23 #include "sbr/getfolder.h"
24 #include "sbr/folder_read.h"
25 #include "sbr/context_save.h"
26 #include "sbr/context_replace.h"
27 #include "sbr/context_find.h"
28 #include "sbr/readconfig.h"
29 #include "sbr/ambigsw.h"
31 #include "sbr/print_version.h"
32 #include "sbr/print_help.h"
33 #include "sbr/error.h"
34 #include "h/fmt_scan.h"
36 #include "h/mhparse.h"
39 #include "h/signals.h"
40 #include "sbr/m_maildir.h"
41 #include "sbr/m_mktemp.h"
42 #include "sbr/mime_type.h"
46 #include "mhshowsbr.h"
49 #define MHFIXMSG_SWITCHES \
50 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
51 X("nodecodetext", 0, NDECODETEXTSW) \
52 X("decodetypes", 0, DECODETYPESW) \
53 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
54 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
55 X("textcharset", 0, TEXTCHARSETSW) \
56 X("notextcharset", 0, NTEXTCHARSETSW) \
57 X("reformat", 0, REFORMATSW) \
58 X("noreformat", 0, NREFORMATSW) \
59 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
60 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
61 X("fixboundary", 0, FIXBOUNDARYSW) \
62 X("nofixboundary", 0, NFIXBOUNDARYSW) \
63 X("fixcte", 0, FIXCOMPOSITECTESW) \
64 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
65 X("fixtype mimetype", 0, FIXTYPESW) \
66 X("file file", 0, FILESW) \
67 X("outfile file", 0, OUTFILESW) \
68 X("rmmproc program", 0, RPROCSW) \
69 X("normmproc", 0, NRPRCSW) \
70 X("changecur", 0, CHGSW) \
71 X("nochangecur", 0, NCHGSW) \
72 X("verbose", 0, VERBSW) \
73 X("noverbose", 0, NVERBSW) \
74 X("version", 0, VERSIONSW) \
75 X("help", 0, HELPSW) \
77 #define X(sw, minchars, id) id,
78 DEFINE_SWITCH_ENUM(MHFIXMSG
);
81 #define X(sw, minchars, id) { sw, minchars, id },
82 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
87 int debugsw
; /* Needed by mhparse.c. */
89 #define quitser pipeser
94 typedef struct fix_transformations
{
102 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
105 } fix_transformations
;
107 static int mhfixmsgsbr (CT
*, char *, const fix_transformations
*,
108 FILE **, char *, FILE **);
109 static int fix_boundary (CT
*, int *);
110 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
111 static int get_multipart_boundary (CT
, char **);
112 static int replace_boundary (CT
, char *, char *);
113 static int fix_types (CT
, svector_t
, int *);
114 static char *replace_substring (char **, const char *, const char *);
115 static char *remove_parameter (char *, const char *);
116 static int fix_composite_cte (CT
, int *);
117 static int set_ce (CT
, int);
118 static int ensure_text_plain (CT
*, CT
, int *, int);
119 static int find_textplain_sibling (CT
, int, int *);
120 static int insert_new_text_plain_part (CT
, int, CT
);
121 static CT
build_text_plain_part (CT
);
122 static int insert_into_new_mp_alt (CT
*, int *);
123 static CT
divide_part (CT
);
124 static void copy_ctinfo (CI
, CI
);
125 static int decode_part (CT
);
126 static int reformat_part (CT
, char *, char *, char *, int);
127 static CT
build_multipart_alt (CT
, CT
, int, int);
128 static int boundary_in_content (FILE **, char *, const char *);
129 static void transfer_noncontent_headers (CT
, CT
);
130 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
131 static int decode_text_parts (CT
, int, const char *, int *);
132 static int should_decode(const char *, const char *, const char *);
133 static int content_encoding (CT
, const char **);
134 static int strip_crs (CT
, int *);
135 static void update_cte (CT
);
136 static int least_restrictive_encoding (CT
) PURE
;
137 static int less_restrictive (int, int);
138 static int convert_charsets (CT
, char *, int *);
139 static int fix_always (CT
, int *);
140 static int fix_filename_param (char *, char *, PM
*, PM
*);
141 static int fix_filename_encoding (CT
);
142 static int write_content (CT
, const char *, char *, FILE *, int, int);
143 static void set_text_ctparams(CT
, char *, int);
144 static int remove_file (const char *);
145 static void report (char *, char *, char *, char *, ...)
147 static void pipeser (int);
151 main (int argc
, char **argv
)
154 char *cp
, *file
= NULL
, *folder
= NULL
;
155 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
156 char **argp
, **arguments
;
157 struct msgs_array msgs
= { 0, 0, NULL
};
158 struct msgs
*mp
= NULL
;
160 FILE *fp
, *infp
= NULL
, *outfp
= NULL
;
161 bool using_stdin
= false;
164 fix_transformations fx
;
165 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
167 fx
.replacetextplain
= 0;
168 fx
.decodetext
= CE_8BIT
;
169 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
170 fx
.lf_line_endings
= 0;
171 fx
.textcharset
= NULL
;
173 if (nmh_init(argv
[0], true, false)) { return 1; }
175 arguments
= getarguments (invo_name
, argc
, argv
, 1);
181 while ((cp
= *argp
++)) {
183 switch (smatch (++cp
, switches
)) {
185 ambigsw (cp
, switches
);
188 die("-%s unknown", cp
);
191 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
193 print_help (buf
, switches
, 1);
196 print_version(invo_name
);
200 if (! (cp
= *argp
++) || *cp
== '-') {
201 die("missing argument to %s", argp
[-2]);
203 if (! strcasecmp (cp
, "8bit")) {
204 fx
.decodetext
= CE_8BIT
;
205 } else if (! strcasecmp (cp
, "7bit")) {
206 fx
.decodetext
= CE_7BIT
;
207 } else if (! strcasecmp (cp
, "binary")) {
208 fx
.decodetext
= CE_BINARY
;
210 die("invalid argument to %s", argp
[-2]);
217 if (! (cp
= *argp
++) || *cp
== '-') {
218 die("missing argument to %s", argp
[-2]);
222 case CRLFLINEBREAKSSW
:
223 fx
.lf_line_endings
= 0;
225 case NCRLFLINEBREAKSSW
:
226 fx
.lf_line_endings
= 1;
229 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
230 die("missing argument to %s", argp
[-2]);
243 case FIXCOMPOSITECTESW
:
244 fx
.fixcompositecte
= 1;
246 case NFIXCOMPOSITECTESW
:
247 fx
.fixcompositecte
= 0;
250 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
251 die("missing argument to %s", argp
[-2]);
253 if (! strncasecmp (cp
, "multipart/", 10) ||
254 ! strncasecmp (cp
, "message/", 8))
255 die("-fixtype %s not allowed", cp
);
256 if (! strchr (cp
, '/'))
257 die("-fixtype requires type/subtype");
258 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
259 svector_push_back (fx
.fixtypes
, cp
);
267 case REPLACETEXTPLAINSW
:
268 fx
.replacetextplain
= 1;
270 case NREPLACETEXTPLAINSW
:
271 fx
.replacetextplain
= 0;
274 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
275 die("missing argument to %s", argp
[-2]);
277 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
280 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
281 die("missing argument to %s", argp
[-2]);
283 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
286 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
287 die("missing argument to %s", argp
[-2]);
307 if (*cp
== '+' || *cp
== '@') {
309 die("only one folder at a time!");
310 folder
= pluspath (cp
);
313 /* Interpret a full path as a filename, not a message. */
314 file
= mh_xstrdup (cp
);
316 app_msgarg (&msgs
, cp
);
321 SIGNAL (SIGQUIT
, quitser
);
322 SIGNAL (SIGPIPE
, pipeser
);
325 * Read the standard profile setup
327 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
328 readconfig(NULL
, fp
, cp
, 0);
332 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= true;
333 suppress_extraneous_trailing_semicolon_warning
= true;
335 if (! context_find ("path")) {
336 free (path ("./", TFOLDER
));
339 if (file
&& msgs
.size
) {
340 die("cannot specify msg and file at same time!");
344 /* Open the outfile now, so we don't have to risk opening it
345 after running out of fds. */
346 if (strcmp (outfile
, "-") == 0) {
348 } else if ((outfp
= fopen (outfile
, "w")) == NULL
) {
349 adios (outfile
, "unable to open for writing");
354 * check if message is coming from file
357 /* If file is stdin, create a tmp file name before parse_mime()
358 has a chance, because it might put in on a different
359 filesystem than the output file. Instead, put it in the
360 user's preferred tmp directory. */
363 if (! strcmp ("-", file
)) {
369 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
370 die("unable to create temporary file in %s",
374 file
= mh_xstrdup (cp
);
375 cpydata (STDIN_FILENO
, fd
, "-", file
);
379 (void) m_unlink (file
);
380 die("failed to write temporary file");
384 cts
= mh_xcalloc(2, sizeof *cts
);
387 if ((ct
= parse_mime (file
))) {
388 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
391 inform("unable to parse message from file %s", file
);
394 /* If there's an outfile, pass the input message unchanged, so the
395 message won't get dropped from a pipeline. */
397 /* Something went wrong. Output might be expected, such as if
398 this were run as a filter. Just copy the input to the
400 if ((infp
= fopen (file
, "r")) == NULL
) {
401 adios (file
, "unable to open for reading");
404 if (copy_input_to_output (file
, infp
, outfile
, outfp
) != OK
) {
405 inform("unable to copy message to %s, "
406 "it might be lost\n", outfile
);
415 * message(s) are coming from a folder
420 app_msgarg(&msgs
, "cur");
423 folder
= getfolder (1);
425 maildir
= mh_xstrdup(m_maildir (folder
));
427 /* chdir so that error messages, esp. from MIME parser, just
428 refer to the message and not its path. */
429 if (chdir (maildir
) == NOTOK
) {
430 adios (maildir
, "unable to change directory to");
433 /* read folder and create message structure */
434 if (! (mp
= folder_read (folder
, 1))) {
435 die("unable to read folder %s", folder
);
438 /* check for empty folder */
439 if (mp
->nummsg
== 0) {
440 die("no messages in %s", folder
);
443 /* parse all the message ranges/sequences and set SELECTED */
444 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
445 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
448 seq_setprev (mp
); /* set the previous-sequence */
450 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
453 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
454 if (is_selected(mp
, msgnum
)) {
455 char *msgnam
= m_name (msgnum
);
457 if ((ct
= parse_mime (msgnam
))) {
458 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
461 inform("unable to parse message %s", msgnam
);
464 /* If there's an outfile, pass the input message
465 unchanged, so the message won't get dropped from a
468 /* Something went wrong. Output might be expected,
469 such as if this were run as a filter. Just copy
470 the input to the output. */
471 /* Can't use path() here because 1) it might have been
472 called before and it caches the pwd, and 2) we call
473 chdir() after that. */
474 char *input_filename
=
475 concat (maildir
, "/", msgnam
, NULL
);
477 if ((infp
= fopen (input_filename
, "r")) == NULL
) {
478 adios (input_filename
,
479 "unable to open for reading");
482 if (copy_input_to_output (input_filename
, infp
,
483 outfile
, outfp
) != OK
) {
484 inform("unable to copy message to %s, "
485 "it might be lost\n", outfile
);
490 free (input_filename
);
497 seq_setcur (mp
, mp
->hghsel
); /* update current message */
499 seq_save (mp
); /* synchronize sequences */
500 context_replace (pfolder
, folder
);/* update current folder */
501 context_save (); /* save the context file */
505 for (ctp
= cts
; *ctp
; ++ctp
) {
507 mhfixmsgsbr (ctp
, maildir
, &fx
, &infp
, outfile
, &outfp
) == OK
513 (void) m_unlink (file
);
516 /* Just calling m_backup() unlinks the backup file. */
517 (void) m_backup (file
);
528 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
529 if (infp
) { fclose (infp
); } /* even if stdin */
530 if (outfp
) { fclose (outfp
); } /* even if stdout */
536 done (status
== OK
? 0 : 1);
542 * Apply transformations to one message.
545 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
546 FILE **infp
, char *outfile
, FILE **outfp
)
548 /* Store input filename in case one of the transformations, i.e.,
549 fix_boundary(), rewrites to a tmp file. */
550 char *input_filename
= maildir
551 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
552 : mh_xstrdup ((*ctp
)->c_file
);
553 bool modify_inplace
= false;
554 int message_mods
= 0;
557 /* Though the input file won't need to be opened if everything goes
558 well, do it here just in case there's a failure, and that failure is
559 running out of file descriptors. */
560 if ((*infp
= fopen (input_filename
, "r")) == NULL
) {
561 adios (input_filename
, "unable to open for reading");
564 if (outfile
== NULL
) {
565 modify_inplace
= true;
567 if ((*ctp
)->c_file
) {
569 /* outfp will be closed by the caller */
570 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, outfp
)) ==
572 die("unable to create temporary file in %s",
575 outfile
= mh_xstrdup (tempfile
);
577 die("missing both input and output filenames\n");
579 } /* else *outfp was defined by caller */
581 reverse_alternative_parts (*ctp
);
582 status
= fix_always (*ctp
, &message_mods
);
583 if (status
== OK
&& fx
->fixboundary
) {
584 status
= fix_boundary (ctp
, &message_mods
);
586 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
587 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
589 if (status
== OK
&& fx
->fixcompositecte
) {
590 status
= fix_composite_cte (*ctp
, &message_mods
);
592 if (status
== OK
&& fx
->reformat
) {
594 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
596 if (status
== OK
&& fx
->decodetext
) {
597 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
601 if (status
== OK
&& fx
->textcharset
!= NULL
) {
602 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
605 if (status
== OK
&& ! (*ctp
)->c_umask
) {
606 /* Set the umask for the contents file. This currently
607 isn't used but just in case it is in the future. */
610 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
611 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
613 (*ctp
)->c_umask
= ~m_gmprot();
618 * Write the content to a file
621 status
= write_content (*ctp
, input_filename
, outfile
, *outfp
,
622 modify_inplace
, message_mods
);
623 } else if (! modify_inplace
) {
624 /* Something went wrong. Output might be expected, such
625 as if this were run as a filter. Just copy the input
627 if (copy_input_to_output (input_filename
, *infp
, outfile
,
629 inform("unable to copy message to %s, it might be lost\n",
634 if (modify_inplace
) {
635 if (status
!= OK
) { (void) m_unlink (outfile
); }
642 free (input_filename
);
649 * Copy input message to output. Assumes not modifying in place, so this
650 * might be running as part of a pipeline.
653 copy_input_to_output (const char *input_filename
, FILE *infp
,
654 const char *output_filename
, FILE *outfp
)
656 int in
= fileno (infp
);
657 int out
= fileno (outfp
);
660 if (in
!= -1 && out
!= -1) {
661 cpydata (in
, out
, input_filename
, output_filename
);
671 * Fix mismatched outer level boundary.
674 fix_boundary (CT
*ct
, int *message_mods
)
676 struct multipart
*mp
;
679 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
680 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
683 * 1) Get boundary at end of part.
684 * 2) Get boundary at beginning of part and compare to the end-of-part
686 * 3) Write out contents of ct to tmp file, replacing boundary in
687 * header with boundary from part. Set c_unlink to 1.
689 * 5) Call parse_mime() on the tmp file, replacing ct.
692 if (mp
&& mp
->mp_start
) {
695 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
698 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
699 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
700 char *filename
= mh_xstrdup ((*ct
)->c_file
);
704 if ((fixed_ct
= parse_mime (fixed
))) {
710 report (NULL
, NULL
, filename
,
711 "fix multipart boundary");
715 inform("unable to parse fixed part");
720 inform("unable to replace broken boundary");
724 inform("unable to create temporary file in %s",
729 free (part_boundary
);
731 /* Couldn't fix the boundary. Report failure so that mhfixmsg
732 doesn't modify the message. */
736 /* No multipart struct, even though the content type is
737 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
748 * Find boundary at end of multipart.
751 get_multipart_boundary (CT ct
, char **part_boundary
)
753 char buffer
[NMH_BUFSIZ
];
754 char *end_boundary
= NULL
;
755 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
756 ? (off_t
) (ct
->c_end
- sizeof buffer
)
757 : (off_t
) ct
->c_begin
;
761 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
762 be big enough, even if it's just 1024, to make that unlikely. */
764 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
765 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
766 advise (ct
->c_file
, "unable to open for reading");
770 /* Get boundary at end of multipart. */
771 while (begin
>= (off_t
) ct
->c_begin
) {
772 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
773 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
774 char *cp
= rfind_str (buffer
, bytes_read
, "--");
779 /* Trim off trailing "--" and anything beyond. */
781 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
782 if (strlen (end
) > 3 && *end
++ == '\n' &&
783 *end
++ == '-' && *end
++ == '-') {
784 end_boundary
= mh_xstrdup (end
);
791 if (end_boundary
|| begin
<= (off_t
) (ct
->c_begin
+ sizeof buffer
))
793 begin
-= sizeof buffer
;
796 /* Get boundary at beginning of multipart. */
798 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
799 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
800 if (bytes_read
>= strlen (end_boundary
)) {
801 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
803 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
804 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
809 /* The start and end boundaries didn't match, or the
810 start boundary doesn't begin with "\n--" (or "--"
811 if at the beginning of buffer). Keep trying. */
825 *part_boundary
= end_boundary
;
827 *part_boundary
= NULL
;
836 * Open and copy ct->c_file to file, replacing the multipart boundary.
839 replace_boundary (CT ct
, char *file
, char *boundary
)
843 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
845 m_getfld_state_t gstate
;
848 if (ct
->c_file
== NULL
) {
849 inform("missing input filename");
853 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
854 advise (ct
->c_file
, "unable to open for reading");
858 if ((fpout
= fopen (file
, "w")) == NULL
) {
860 advise (file
, "unable to open for writing");
864 gstate
= m_getfld_state_init(fpin
);
865 for (compnum
= 1;;) {
866 int bufsz
= (int) sizeof buf
;
868 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
873 /* get copies of the buffers */
874 np
= mh_xstrdup (name
);
875 vp
= mh_xstrdup (buf
);
877 /* if necessary, get rest of field */
878 while (state
== FLDPLUS
) {
880 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
881 vp
= add (buf
, vp
); /* add to previous value */
884 if (strcasecmp (TYPE_FIELD
, np
)) {
885 fprintf (fpout
, "%s:%s", np
, vp
);
887 char *new_ctline
, *new_params
;
889 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
890 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
893 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
894 ct
->c_ctinfo
.ci_subtype
, NULL
);
895 new_params
= output_params(LEN(TYPE_FIELD
) +
896 strlen(new_ctline
) + 1,
897 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
898 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
899 FENDNULL(new_params
));
911 /* buf will have a terminating NULL, skip it. */
912 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
913 advise (file
, "fwrite");
922 inform("message format error in component #%d", compnum
);
927 inform("getfld() returned %d", state
);
935 m_getfld_state_destroy (&gstate
);
944 * Fix Content-Type header to reflect the content of its part.
947 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
)
951 switch (ct
->c_type
) {
953 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
956 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
957 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
963 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
964 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
966 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
973 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
974 for (typep
= svector_strs (fixtypes
);
975 typep
&& (type
= *typep
);
978 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
981 if (! strcasecmp (type
, type_subtype
) &&
982 decode_part (ct
) == OK
&&
983 ct
->c_cefile
.ce_file
!= NULL
) {
984 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
987 if ((cp
= strchr (ct_type_subtype
, ';'))) {
988 /* Truncate to remove any parameter list from
989 mime_type () result. */
993 if (strcasecmp (type
, ct_type_subtype
)) {
994 char *ct_type
, *ct_subtype
;
997 /* The Content-Type header does not match the
998 content, so update these struct Content
1001 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
1004 /* Extract type and subtype from type/subtype. */
1005 ct_type
= mh_xstrdup(ct_type_subtype
);
1006 if ((cp
= strchr (ct_type
, '/'))) {
1008 ct_subtype
= mh_xstrdup(++cp
);
1010 inform("missing / in MIME type of %s %s",
1011 ct
->c_file
, ct
->c_partno
);
1016 ct
->c_type
= ct_str_type (ct_type
);
1017 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
1019 free (ct
->c_ctinfo
.ci_type
);
1020 ct
->c_ctinfo
.ci_type
= ct_type
;
1021 free (ct
->c_ctinfo
.ci_subtype
);
1022 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
1023 if (! replace_substring (&ct
->c_ctline
, type
,
1025 inform("did not find %s in %s",
1026 type
, ct
->c_ctline
);
1029 /* Update Content-Type header field. */
1030 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1031 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1032 if (replace_substring (&hf
->value
, type
,
1036 report (NULL
, ct
->c_partno
, ct
->c_file
,
1037 "change Content-Type in header "
1039 type
, ct_type_subtype
);
1043 inform("did not find %s in %s", type
, hf
->value
);
1047 free (ct_type_subtype
);
1049 free (type_subtype
);
1059 * Replace a substring, allocating space to hold the new one.
1062 replace_substring (char **str
, const char *old
, const char *new)
1066 if ((cp
= strstr (*str
, old
))) {
1067 char *remainder
= cp
+ strlen (old
);
1068 char *prefix
, *new_str
;
1071 prefix
= mh_xstrdup(*str
);
1072 *(prefix
+ (cp
- *str
)) = '\0';
1073 new_str
= concat (prefix
, new, remainder
, NULL
);
1076 new_str
= concat (new, remainder
, NULL
);
1081 return *str
= new_str
;
1089 * Remove a name=value parameter, given just its name, from a header value.
1092 remove_parameter (char *str
, const char *name
)
1094 /* It looks to me, based on the BNF in RFC 2045, than there can't
1095 be whitespace between the parameter name and the "=", or
1096 between the "=" and the parameter value. */
1097 char *param_name
= concat (name
, "=", NULL
);
1100 if ((cp
= strstr (str
, param_name
))) {
1104 /* Remove any leading spaces, before the parameter name. */
1106 start
> str
&& isspace ((unsigned char) *(start
-1));
1110 /* Remove a leading semicolon. */
1111 if (start
> str
&& *(start
-1) == ';') { --start
; }
1113 end
= cp
+ strlen (name
) + 1;
1115 /* Skip past the quoted value, and then the final quote. */
1116 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1119 /* Skip past the value. */
1120 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1123 /* Count how many characters need to be moved. Include
1124 trailing null, which is accounted for by the
1125 initialization of count to 1. */
1126 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1127 (void) memmove (start
, end
, count
);
1137 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1138 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1142 fix_composite_cte (CT ct
, int *message_mods
)
1146 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1147 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1148 ct
->c_encoding
!= CE_BINARY
) {
1151 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1152 char *name
= hf
->name
;
1153 for (; isspace((unsigned char)*name
); ++name
) {
1157 if (! strncasecmp (name
, ENCODING_FIELD
,
1158 LEN(ENCODING_FIELD
))) {
1159 char *prefix
= "Nmh-REPLACED-INVALID-";
1163 h
->name
= mh_xstrdup (hf
->name
);
1164 h
->hf_encoding
= hf
->hf_encoding
;
1168 /* Retain old header but prefix its name. */
1170 hf
->name
= concat (prefix
, h
->name
, NULL
);
1174 char *encoding
= cpytrim (hf
->value
);
1175 report (NULL
, ct
->c_partno
, ct
->c_file
,
1176 "replace Content-Transfer-Encoding of %s "
1177 "with 8 bit", encoding
);
1181 h
->value
= mh_xstrdup (" 8bit\n");
1183 /* Don't need to warn for multiple C-T-E header
1184 fields, parse_mime() already does that. But
1185 if there are any, fix them all as necessary. */
1190 set_ce (ct
, CE_8BIT
);
1193 if (ct
->c_type
== CT_MULTIPART
) {
1194 struct multipart
*m
;
1197 m
= (struct multipart
*) ct
->c_ctparams
;
1198 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1199 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1212 * Set content encoding.
1215 set_ce (CT ct
, int encoding
)
1217 const char *ce
= ce_str (encoding
);
1218 const struct str2init
*ctinit
= get_ce_method (ce
);
1221 char *cte
= concat (" ", ce
, "\n", NULL
);
1222 bool found_cte
= false;
1224 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1225 caller is decode_text_parts (). Save because we'll
1227 struct cefile decoded_content_info
= ct
->c_cefile
;
1229 ct
->c_encoding
= encoding
;
1231 ct
->c_ctinitfnx
= ctinit
->si_init
;
1232 /* This will assign ct->c_cefile with an all-0 struct, which
1234 (*ctinit
->si_init
) (ct
);
1235 /* After returning, the caller should set
1236 ct->c_cefile.ce_file to the name of the file containing
1239 if (ct
->c_ceclosefnx
) {
1240 (*ct
->c_ceclosefnx
) (ct
);
1243 /* Restore the cefile. */
1244 ct
->c_cefile
= decoded_content_info
;
1246 /* Update/add Content-Transfer-Encoding header field. */
1247 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1248 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1255 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1258 /* Update c_celine. It's used only by mhlist -debug. */
1259 free (ct
->c_celine
);
1260 ct
->c_celine
= mh_xstrdup (cte
);
1270 * Make sure each text part has a corresponding text/plain part.
1273 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
)
1277 switch ((*ct
)->c_type
) {
1279 /* Nothing to do for text/plain. */
1280 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1282 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1283 parent
->c_subtype
== MULTI_ALTERNATE
) {
1284 int new_subpart_number
= 1;
1285 int has_text_plain
=
1286 find_textplain_sibling (parent
, replacetextplain
,
1287 &new_subpart_number
);
1289 if (! has_text_plain
) {
1290 /* Parent is a multipart/alternative. Insert a new
1291 text/plain subpart. */
1292 const int inserted
=
1293 insert_new_text_plain_part (*ct
, new_subpart_number
,
1298 report (NULL
, parent
->c_partno
, parent
->c_file
,
1299 "insert text/plain part");
1305 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1306 parent
->c_subtype
== MULTI_RELATED
) {
1307 char *type_subtype
=
1308 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1309 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1310 const char *parent_type
=
1311 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1312 int new_subpart_number
= 1;
1313 int has_text_plain
= 0;
1315 /* Have to do string comparison on the subtype because we
1316 don't enumerate all of them in c_subtype values.
1317 parent_type will be NULL if the multipart/related part
1318 doesn't have a type parameter. The type parameter must
1319 be specified according to RFC 2387 Sec. 3.1 but not all
1321 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1322 /* The type of this part matches the root type of the
1323 parent multipart/related. Look to see if there's
1324 text/plain sibling. */
1326 find_textplain_sibling (parent
, replacetextplain
,
1327 &new_subpart_number
);
1330 free (type_subtype
);
1332 if (! has_text_plain
) {
1333 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1337 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1338 if (*ct
!= part
->mp_part
) {
1344 /* Parent is a multipart/related. Insert a new
1345 text/plain subpart in a new multipart/alternative. */
1346 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1347 /* Not an error if text/plain couldn't be added. */
1350 /* There are no siblings, so insert a new text/plain
1351 subpart, and change the parent type from
1352 multipart/related to multipart/alternative. */
1353 const int inserted
=
1354 insert_new_text_plain_part (*ct
, new_subpart_number
,
1360 parent
->c_subtype
= MULTI_ALTERNATE
;
1361 free (parent
->c_ctinfo
.ci_subtype
);
1362 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1363 if (! replace_substring (&parent
->c_ctline
, "/related",
1365 inform("did not find multipart/related in %s",
1369 /* Update Content-Type header field. */
1370 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1371 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1372 if (replace_substring (&hf
->value
, "/related",
1376 report (NULL
, parent
->c_partno
,
1378 "insert text/plain part");
1381 /* Remove, e.g., type="text/html" from
1382 multipart/alternative. */
1383 remove_parameter (hf
->value
, "type");
1386 inform("did not find multipart/"
1387 "related in header %s", hf
->value
);
1391 /* Not an error if text/plain couldn't be inserted. */
1396 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1403 case CT_MULTIPART
: {
1404 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1407 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1408 if ((*ct
)->c_type
== CT_MULTIPART
) {
1409 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1417 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1418 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1420 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1431 * See if there is a sibling text/plain, and return its subpart number.
1434 find_textplain_sibling (CT parent
, int replacetextplain
,
1435 int *new_subpart_number
)
1437 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1438 struct part
*part
, *prev
;
1439 bool has_text_plain
= false;
1441 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1442 ++*new_subpart_number
;
1443 if (part
->mp_part
->c_type
== CT_TEXT
&&
1444 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1445 if (replacetextplain
) {
1446 struct part
*old_part
;
1447 if (part
== mp
->mp_parts
) {
1448 old_part
= mp
->mp_parts
;
1449 mp
->mp_parts
= part
->mp_next
;
1451 old_part
= prev
->mp_next
;
1452 prev
->mp_next
= part
->mp_next
;
1455 report (NULL
, parent
->c_partno
, parent
->c_file
,
1456 "remove text/plain part %s",
1457 old_part
->mp_part
->c_partno
);
1459 free_content (old_part
->mp_part
);
1462 has_text_plain
= true;
1469 return has_text_plain
;
1474 * Insert a new text/plain part.
1477 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
)
1479 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1480 struct part
*new_part
;
1483 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1485 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1487 new_part
->mp_next
= mp
->mp_parts
;
1488 mp
->mp_parts
= new_part
;
1489 new_part
->mp_part
->c_partno
=
1490 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1496 free_content (new_part
->mp_part
);
1504 * Create a text/plain part to go along with non-plain sibling part.
1507 build_text_plain_part (CT encoded_part
)
1509 CT tp_part
= divide_part (encoded_part
);
1510 char *tmp_plain_file
= NULL
;
1512 if (decode_part (tp_part
) == OK
) {
1513 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1514 contains the decoded contents. And the decoding function, such
1515 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1516 be unlinked by free_content (). */
1519 /* This m_mktemp2() call closes the temp file. */
1520 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1521 inform("unable to create temporary file in %s",
1524 tmp_plain_file
= mh_xstrdup (tempfile
);
1525 if (reformat_part (tp_part
, tmp_plain_file
,
1526 tp_part
->c_ctinfo
.ci_type
,
1527 tp_part
->c_ctinfo
.ci_subtype
,
1528 tp_part
->c_type
) == OK
) {
1534 free_content (tp_part
);
1535 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1536 free (tmp_plain_file
);
1543 * Slip new text/plain part into a new multipart/alternative.
1546 insert_into_new_mp_alt (CT
*ct
, int *message_mods
)
1548 CT tp_part
= build_text_plain_part (*ct
);
1552 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1555 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1557 if (mp
&& mp
->mp_parts
) {
1558 mp
->mp_parts
->mp_part
= tp_part
;
1559 /* Make the new multipart/alternative the parent. */
1564 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1565 "insert text/plain part");
1568 free_content (tp_part
);
1569 free_content (mp_alt
);
1576 /* Not an error if text/plain couldn't be built. */
1584 * Clone a MIME part.
1592 /* Just copy over what is needed for decoding. c_vrsn and
1593 c_celine aren't necessary. */
1594 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1595 new_part
->c_begin
= ct
->c_begin
;
1596 new_part
->c_end
= ct
->c_end
;
1597 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1598 new_part
->c_type
= ct
->c_type
;
1599 new_part
->c_cefile
= ct
->c_cefile
;
1600 new_part
->c_encoding
= ct
->c_encoding
;
1601 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1602 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1603 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1604 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1606 /* c_ctline is used by reformat__part(), so it can preserve
1607 anything after the type/subtype. */
1608 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1615 * Copy the content info from one part to another.
1618 copy_ctinfo (CI dest
, CI src
)
1622 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1623 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1625 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1626 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1628 if (s_pm
->pm_charset
) {
1629 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1631 if (s_pm
->pm_lang
) {
1632 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1636 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1637 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1652 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, &file
)) == NULL
) {
1653 die("unable to create temporary file in %s", get_temp_dir());
1655 tmp_decoded
= mh_xstrdup (tempfile
);
1656 /* The following call will load ct->c_cefile.ce_file with the tmp
1657 filename of the decoded content. tmp_decoded will contain the
1658 encoded output, get rid of that. */
1659 status
= output_message_fp (ct
, file
, tmp_decoded
);
1660 (void) m_unlink (tmp_decoded
);
1662 if (fclose (file
)) {
1663 inform("unable to close temporary file %s, continuing...", tempfile
);
1671 * Reformat content as plain text.
1672 * Some of the arguments aren't really needed now, but maybe will
1673 * be in the future for other than text types.
1676 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
)
1678 int output_subtype
, output_encoding
;
1679 const char *reason
= NULL
;
1683 /* Hacky: this redirects the output from whatever command is used
1684 to show the part to a file. So, the user can't have any output
1685 redirection in that command.
1686 Could show_multi() in mhshowsbr.c avoid this? */
1688 /* Check for invo_name-format-type/subtype. */
1689 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1691 inform("Don't know how to convert %s, there is no "
1692 "%s-format-%s/%s profile entry",
1693 ct
->c_file
, invo_name
, type
, subtype
);
1697 if (strchr (cf
, '>')) {
1698 inform("'>' prohibited in \"%s\",\nplease fix your "
1699 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1705 cp
= concat (cf
, " >", file
, NULL
);
1706 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1709 /* Unlink decoded content tmp file and free its filename to avoid
1710 leaks. The file stream should already have been closed. */
1711 if (ct
->c_cefile
.ce_unlink
) {
1712 (void) m_unlink (ct
->c_cefile
.ce_file
);
1713 free (ct
->c_cefile
.ce_file
);
1714 ct
->c_cefile
.ce_file
= NULL
;
1715 ct
->c_cefile
.ce_unlink
= 0;
1718 if (c_type
== CT_TEXT
) {
1719 output_subtype
= TEXT_PLAIN
;
1721 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1725 output_encoding
= content_encoding (ct
, &reason
);
1727 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1728 ct
->c_cefile
.ce_file
= file
;
1729 ct
->c_cefile
.ce_unlink
= 1;
1731 ct
->c_cefile
.ce_unlink
= 0;
1740 * Fill in a multipart/alternative part.
1743 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
)
1745 char *boundary_prefix
= "----=_nmh-multipart";
1746 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1747 char *boundary_indicator
= "; boundary=";
1748 char *typename
, *subtypename
, *name
;
1751 struct multipart
*m
;
1752 const struct str2init
*ctinit
;
1756 /* Set up the multipart/alternative part. These fields of *ct were
1757 initialized to 0 by mh_xcalloc():
1758 c_fp, c_unlink, c_begin, c_end,
1759 c_vrsn, c_ctline, c_celine,
1760 c_id, c_descr, c_dispo, c_partno,
1761 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1762 c_cefile, c_encoding,
1763 c_digested, c_digest[16], c_ctexbody,
1764 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1766 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1769 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1771 ct
->c_subtype
= subtype
;
1773 ctinit
= get_ct_init (ct
->c_type
);
1775 typename
= ct_type_str (type
);
1776 subtypename
= ct_subtype_str (type
, subtype
);
1780 int found_boundary
= 1;
1782 while (found_boundary
&& serial
< 1000000) {
1785 /* Ensure that the boundary doesn't appear in the decoded
1787 if (new_part
->c_cefile
.ce_file
) {
1788 if ((found_boundary
=
1789 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1790 new_part
->c_cefile
.ce_file
,
1791 boundary
)) == NOTOK
) {
1796 /* Ensure that the boundary doesn't appear in the encoded
1798 if (! found_boundary
&& new_part
->c_file
) {
1799 if ((found_boundary
=
1800 boundary_in_content (&new_part
->c_fp
,
1802 boundary
)) == NOTOK
) {
1807 if (found_boundary
) {
1808 /* Try a slightly different boundary. */
1813 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1815 concat (boundary_prefix
,
1816 FENDNULL(first_alt
->c_partno
),
1817 "-", buffer2
, NULL
);
1821 if (found_boundary
) {
1822 inform("giving up trying to find a unique boundary");
1827 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1828 boundary
, "\"", NULL
);
1830 /* Load c_first_hf and c_last_hf. */
1831 transfer_noncontent_headers (first_alt
, ct
);
1832 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1835 /* Load c_partno. */
1836 if (first_alt
->c_partno
) {
1837 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1838 free (first_alt
->c_partno
);
1839 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1840 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1842 first_alt
->c_partno
= mh_xstrdup ("1");
1843 new_part
->c_partno
= mh_xstrdup ("2");
1847 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1848 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1851 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1852 "boundary", boundary
, 0);
1856 p
->mp_next
->mp_next
= NULL
;
1857 p
->mp_next
->mp_part
= first_alt
;
1860 m
->mp_start
= concat (boundary
, "\n", NULL
);
1861 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1877 * Check that the boundary does not appear in the content.
1880 boundary_in_content (FILE **fp
, char *file
, const char *boundary
)
1882 char buffer
[NMH_BUFSIZ
];
1884 bool found_boundary
= false;
1886 /* free_content() will close *fp if we fopen it here. */
1887 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1888 advise (file
, "unable to open %s for reading", file
);
1892 fseeko (*fp
, 0L, SEEK_SET
);
1893 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1894 if (find_str (buffer
, bytes_read
, boundary
)) {
1895 found_boundary
= true;
1900 return found_boundary
;
1905 * Remove all non-Content headers.
1908 transfer_noncontent_headers (CT old
, CT
new)
1912 hp_prev
= hp
= old
->c_first_hf
;
1916 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, LEN(XXX_FIELD_PRF
))) {
1917 if (hp
== old
->c_last_hf
) {
1918 if (hp
== old
->c_first_hf
) {
1919 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1921 hp_prev
->next
= NULL
;
1922 old
->c_last_hf
= hp_prev
;
1925 if (hp
== old
->c_first_hf
) {
1926 old
->c_first_hf
= next
;
1928 hp_prev
->next
= next
;
1932 /* Put node hp in the new CT. */
1933 if (new->c_first_hf
== NULL
) {
1934 new->c_first_hf
= hp
;
1936 new->c_last_hf
->next
= hp
;
1938 new->c_last_hf
= hp
;
1940 /* A Content- header, leave in old. */
1953 set_ct_type (CT ct
, int type
, int subtype
, int encoding
)
1955 char *typename
= ct_type_str (type
);
1956 char *subtypename
= ct_subtype_str (type
, subtype
);
1957 /* E.g, " text/plain" */
1958 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1959 /* E.g, " text/plain\n" */
1960 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1961 bool found_content_type
= false;
1963 const char *cp
= NULL
;
1967 /* Update/add Content-Type header field. */
1968 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1969 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1970 found_content_type
= true;
1972 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1973 ? concat (type_subtypename
, cp
, "\n", NULL
)
1974 : mh_xstrdup (name_plus_nl
);
1977 if (! found_content_type
) {
1978 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
1979 (cp
= strchr (ct
->c_ctline
, ';'))
1980 ? concat (type_subtypename
, cp
, "\n", NULL
)
1981 : mh_xstrdup (name_plus_nl
));
1984 /* Some of these might not be used, but set them anyway. */
1986 ? concat (type_subtypename
, cp
, NULL
)
1987 : concat (type_subtypename
, NULL
);
1988 free (ct
->c_ctline
);
1989 ct
->c_ctline
= ctline
;
1990 /* Leave other ctinfo members as they were. */
1991 free (ct
->c_ctinfo
.ci_type
);
1992 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1993 free (ct
->c_ctinfo
.ci_subtype
);
1994 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1996 ct
->c_subtype
= subtype
;
1998 free (name_plus_nl
);
1999 free (type_subtypename
);
2001 status
= set_ce (ct
, encoding
);
2008 * It's not necessary to update the charset parameter of a Content-Type
2009 * header for a text part. According to RFC 2045 Sec. 6.4, the body
2010 * (content) was originally in the specified charset, "and will be in
2011 * that character set again after decoding."
2014 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
2018 int lf_line_endings
= 0;
2020 switch (ct
->c_type
) {
2021 case CT_MULTIPART
: {
2022 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2025 /* Should check to see if the body for this part is encoded?
2026 For now, it gets passed along as-is by InitMultiPart(). */
2027 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2028 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
2035 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2036 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2038 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
2044 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2049 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
2051 switch (ct
->c_encoding
) {
2056 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
2057 const char *reason
= NULL
;
2059 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
2060 && encoding
!= CE_BINARY
) {
2061 /* The decoding isn't acceptable so discard it.
2062 Leave status as OK to allow other transformations. */
2064 report (NULL
, ct
->c_partno
, ct
->c_file
,
2065 "will not decode%s because it is binary (%s)",
2067 : (FENDNULL(ct
->c_ctline
)),
2070 (void) m_unlink (ct
->c_cefile
.ce_file
);
2071 free (ct
->c_cefile
.ce_file
);
2072 ct
->c_cefile
.ce_file
= NULL
;
2073 } else if (ct
->c_encoding
== CE_QUOTED
&&
2074 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2075 /* The decoding isn't acceptable so discard it.
2076 Leave status as OK to allow other transformations. */
2078 report (NULL
, ct
->c_partno
, ct
->c_file
,
2079 "will not decode%s because it is 8bit",
2081 : (FENDNULL(ct
->c_ctline
)));
2083 (void) m_unlink (ct
->c_cefile
.ce_file
);
2084 free (ct
->c_cefile
.ce_file
);
2085 ct
->c_cefile
.ce_file
= NULL
;
2089 if (ct_encoding
== CE_BINARY
) {
2091 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2096 if (set_ce (ct
, enc
) == OK
) {
2099 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2100 FENDNULL(ct
->c_ctline
));
2102 if (lf_line_endings
) {
2103 strip_crs (ct
, message_mods
);
2116 if (lf_line_endings
) {
2117 strip_crs (ct
, message_mods
);
2132 * Determine if the part with type[/subtype] should be decoded, according to
2133 * decodetypes (which came from the -decodetypes switch).
2136 should_decode(const char *decodetypes
, const char *type
, const char *subtype
)
2138 /* Quick search for matching type[/subtype] in decodetypes: bracket
2139 decodetypes with commas, then search for ,type, and ,type/subtype, in
2142 bool found_match
= false;
2143 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2144 char *delimited_type
= concat(",", type
, ",", NULL
);
2146 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2148 } else if (subtype
!= NULL
) {
2149 char *delimited_type_subtype
=
2150 concat(",", type
, "/", subtype
, ",", NULL
);
2152 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2155 free(delimited_type_subtype
);
2158 free(delimited_type
);
2159 free(delimited_decodetypes
);
2166 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2167 * if it has any NUL characters, a CR not followed by a LF, or lines
2168 * greater than 998 characters in length. If binary, reason is set
2169 * to a string explaining why.
2172 content_encoding (CT ct
, const char **reason
)
2174 CE ce
= &ct
->c_cefile
;
2175 int encoding
= CE_7BIT
;
2178 size_t line_len
= 0;
2179 char buffer
[NMH_BUFSIZ
];
2182 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2183 advise (ce
->ce_file
, "unable to open for reading");
2187 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2188 while (encoding
!= CE_BINARY
&&
2189 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2192 int last_char_was_cr
= 0;
2194 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2195 if (*cp
== '\0' || ++line_len
> 998 ||
2196 (*cp
!= '\n' && last_char_was_cr
)) {
2197 encoding
= CE_BINARY
;
2199 *reason
= "null character";
2200 } else if (line_len
> 998) {
2201 *reason
= "line length > 998";
2202 } else if (*cp
!= '\n' && last_char_was_cr
) {
2203 *reason
= "CR not followed by LF";
2205 /* Should not reach this. */
2212 } else if (! isascii ((unsigned char) *cp
)) {
2216 last_char_was_cr
= *cp
== '\r';
2222 } /* else should never happen */
2229 * Strip carriage returns from content.
2232 strip_crs (CT ct
, int *message_mods
)
2234 char *charset
= content_charset (ct
);
2237 /* Only strip carriage returns if content is ASCII or another
2238 charset that has the same readily recognizable CR followed by a
2239 LF. We can include UTF-8 here because if the high-order bit of
2240 a UTF-8 byte is 0, then it must be a single-byte ASCII
2242 if (! strcasecmp (charset
, "US-ASCII") ||
2243 ! strcasecmp (charset
, "UTF-8") ||
2244 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2245 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2250 bool has_crs
= false;
2251 bool opened_input_file
= false;
2253 if (ct
->c_cefile
.ce_file
) {
2254 file
= &ct
->c_cefile
.ce_file
;
2255 fp
= &ct
->c_cefile
.ce_fp
;
2257 } else if (ct
->c_file
) {
2260 begin
= (size_t) ct
->c_begin
;
2261 end
= (size_t) ct
->c_end
;
2262 } /* else don't know where the content is */
2264 if (file
&& *file
&& fp
) {
2266 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2267 advise (*file
, "unable to open for reading");
2270 opened_input_file
= true;
2276 char buffer
[NMH_BUFSIZ
];
2278 size_t bytes_to_read
=
2279 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2281 fseeko (*fp
, begin
, SEEK_SET
);
2282 while ((bytes_read
= fread (buffer
, 1,
2283 min (bytes_to_read
, sizeof buffer
),
2285 /* Look for CR followed by a LF. This is supposed to
2286 be text so there should be LF's. If not, don't
2287 modify the content. */
2290 bool last_char_was_cr
= false;
2292 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2294 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2295 if (*cp
== '\n' && last_char_was_cr
) {
2300 last_char_was_cr
= *cp
== '\r';
2306 char *stripped_content_file
;
2307 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2309 if (tempfile
== NULL
) {
2310 die("unable to create temporary file in %s",
2313 stripped_content_file
= mh_xstrdup (tempfile
);
2315 /* Strip each CR before a LF from the content. */
2316 fseeko (*fp
, begin
, SEEK_SET
);
2317 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2321 bool last_char_was_cr
= false;
2323 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2325 last_char_was_cr
= true;
2326 } else if (last_char_was_cr
) {
2328 if (write (fd
, "\r", 1) < 0) {
2329 advise (tempfile
, "CR write");
2332 if (write (fd
, cp
, 1) < 0) {
2333 advise (tempfile
, "write");
2335 last_char_was_cr
= false;
2337 if (write (fd
, cp
, 1) < 0) {
2338 advise (tempfile
, "write");
2340 last_char_was_cr
= false;
2346 inform("unable to write temporary file %s, continuing...",
2347 stripped_content_file
);
2348 (void) m_unlink (stripped_content_file
);
2349 free(stripped_content_file
);
2352 /* Replace the decoded file with the converted one. */
2353 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2354 (void) m_unlink (ct
->c_cefile
.ce_file
);
2356 free(ct
->c_cefile
.ce_file
);
2357 ct
->c_cefile
.ce_file
= stripped_content_file
;
2358 ct
->c_cefile
.ce_unlink
= 1;
2362 report (NULL
, ct
->c_partno
,
2363 begin
== 0 && end
== 0 ? "" : *file
,
2369 if (opened_input_file
) {
2383 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2384 * of the part C-T-E's.
2389 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2391 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2392 least_restrictive_enc
!= CE_7BIT
) {
2393 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2395 bool found_cte
= false;
2397 /* Update/add Content-Transfer-Encoding header field. */
2398 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2399 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2406 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2413 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2417 least_restrictive_encoding (CT ct
)
2419 int encoding
= CE_UNKNOWN
;
2421 switch (ct
->c_type
) {
2422 case CT_MULTIPART
: {
2423 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2426 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2427 const int part_encoding
=
2428 least_restrictive_encoding (part
->mp_part
);
2430 if (less_restrictive (encoding
, part_encoding
)) {
2431 encoding
= part_encoding
;
2438 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2439 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2440 const int part_encoding
=
2441 least_restrictive_encoding (e
->eb_content
);
2443 if (less_restrictive (encoding
, part_encoding
)) {
2444 encoding
= part_encoding
;
2450 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2451 encoding
= ct
->c_encoding
;
2460 * Return whether the second encoding is less restrictive than the first, where
2461 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2462 * CE_BINARY is less restrictive than CE_8BIT and
2463 * CE_8BIT is less restrictive than CE_7BIT.
2466 less_restrictive (int encoding
, int second_encoding
)
2468 switch (second_encoding
) {
2470 return encoding
!= CE_BINARY
;
2472 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2474 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2475 encoding
!= CE_7BIT
;
2483 * Convert character set of each part.
2486 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
)
2490 switch (ct
->c_type
) {
2492 if (ct
->c_subtype
== TEXT_PLAIN
) {
2493 status
= convert_charset (ct
, dest_charset
, message_mods
);
2496 char *ct_charset
= content_charset (ct
);
2498 report (NULL
, ct
->c_partno
, ct
->c_file
,
2499 "convert %s to %s", ct_charset
, dest_charset
);
2503 char *ct_charset
= content_charset (ct
);
2505 report ("iconv", ct
->c_partno
, ct
->c_file
,
2506 "failed to convert %s to %s", ct_charset
, dest_charset
);
2512 case CT_MULTIPART
: {
2513 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2516 /* Should check to see if the body for this part is encoded?
2517 For now, it gets passed along as-is by InitMultiPart(). */
2518 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2520 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2526 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2527 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2530 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2543 * Fix various problems that aren't handled elsewhere. These
2544 * are fixed unconditionally: there are no switches to disable
2545 * them. Currently, "problems" are these:
2546 * 1) remove extraneous semicolon at the end of a header parameter list
2547 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2548 * filename parameters in Content-Type and Content-Disposition
2549 * headers, respectively.
2552 fix_always (CT ct
, int *message_mods
)
2556 switch (ct
->c_type
) {
2557 case CT_MULTIPART
: {
2558 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2561 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2562 status
= fix_always (part
->mp_part
, message_mods
);
2568 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2569 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2571 status
= fix_always (e
->eb_content
, message_mods
);
2578 if (ct
->c_first_hf
) {
2579 fix_filename_encoding (ct
);
2582 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2583 size_t len
= strlen (hf
->value
);
2585 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2586 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2587 /* Only do this for Content-Type and
2588 Content-Disposition fields because those are the
2589 only headers that parse_mime() warns about. */
2593 /* whitespace following a trailing ';' will be nuked as well */
2594 if (hf
->value
[len
- 1] == '\n') {
2595 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2596 if (len
-- == 0) { break; }
2600 if (hf
->value
[len
- 2] == ';') {
2601 /* Remove trailing ';' from parameter value. */
2602 hf
->value
[len
- 2] = '\n';
2603 hf
->value
[len
- 1] = '\0';
2605 /* Also, if Content-Type parameter, remove trailing ';'
2606 from ct->c_ctline. This probably isn't necessary
2608 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2609 size_t l
= strlen(ct
->c_ctline
) - 1;
2610 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2611 ct
->c_ctline
[l
] == ';') {
2612 ct
->c_ctline
[l
--] = '\0';
2613 if (l
== 0) { break; }
2619 report (NULL
, ct
->c_partno
, ct
->c_file
,
2620 "remove trailing ; from %s parameter value",
2632 * Factor out common code for loops in fix_filename_encoding().
2635 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
)
2639 if (has_prefix(value
, "=?") && has_suffix(value
, "?=")) {
2640 /* Looks like an RFC 2047 encoded parameter. */
2641 char decoded
[PATH_MAX
+ 1];
2643 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2644 /* Encode using RFC 2231. */
2645 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2648 inform("failed to decode %s parameter %s", name
, value
);
2657 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2658 * filename parameters in Content-Type and Content-Disposition
2659 * headers, respectively.
2662 fix_filename_encoding (CT ct
)
2668 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2669 if (pm
->pm_name
&& pm
->pm_value
&&
2670 strcasecmp (pm
->pm_name
, "name") == 0) {
2671 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2672 &ct
->c_ctinfo
.ci_first_pm
,
2673 &ct
->c_ctinfo
.ci_last_pm
);
2677 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2678 if (pm
->pm_name
&& pm
->pm_value
&&
2679 strcasecmp (pm
->pm_name
, "filename") == 0) {
2680 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2686 /* Fix hf values to correspond. */
2687 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2688 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2690 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2691 field
= TYPE_HEADER
;
2692 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2693 field
= DISPO_HEADER
;
2696 if (field
!= OTHER
) {
2697 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2699 if (semicolon_loc
) {
2701 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2702 const char *const params
=
2704 field
== TYPE_HEADER
2705 ? ct
->c_ctinfo
.ci_first_pm
2706 : ct
->c_dispo_first
,
2708 const char *const new_params
= concat (params
, "\n", NULL
);
2710 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2711 free((void *)new_params
); /* Cast away const. Sigh. */
2712 free((void *)params
);
2714 inform("did not find semicolon in %s:%s\n",
2715 hf
->name
, hf
->value
);
2725 * Output content in input file to output file.
2728 write_content (CT ct
, const char *input_filename
, char *outfile
, FILE *outfp
,
2729 int modify_inplace
, int message_mods
)
2733 if (modify_inplace
) {
2734 if (message_mods
> 0) {
2735 if ((status
= output_message_fp (ct
, outfp
, outfile
)) == OK
) {
2736 char *infile
= input_filename
2737 ? mh_xstrdup (input_filename
)
2738 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2740 if (remove_file (infile
) == OK
) {
2741 if (rename (outfile
, infile
)) {
2742 /* Rename didn't work, possibly because of an
2743 attempt to rename across filesystems. Try
2744 brute force copy. */
2745 int old
= open (outfile
, O_RDONLY
);
2747 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2750 if (old
!= -1 && new != -1) {
2751 char buffer
[NMH_BUFSIZ
];
2753 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2755 if (write (new, buffer
, i
) != i
) {
2761 if (new != -1) { close (new); }
2762 if (old
!= -1) { close (old
); }
2763 (void) m_unlink (outfile
);
2766 /* The -file argument processing used path() to
2767 expand filename to absolute path. */
2768 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2770 inform("unable to rename %s %s to %s, continuing...",
2771 file
? "file" : "message", outfile
,
2777 inform("unable to remove input file %s, "
2778 "not modifying it, continuing...", infile
);
2779 (void) m_unlink (outfile
);
2788 /* No modifications and didn't need the tmp outfile. */
2789 (void) m_unlink (outfile
);
2792 /* Output is going to some file. Produce it whether or not
2793 there were modifications. */
2794 status
= output_message_fp (ct
, outfp
, outfile
);
2803 * parse_mime() does not set lf_line_endings in struct text, so use this
2804 * function to do it. It touches the parts the decodetypes identifies.
2807 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
)
2809 switch (ct
->c_type
) {
2810 case CT_MULTIPART
: {
2811 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2814 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2815 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2821 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2822 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2824 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2829 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2830 if (ct
->c_ctparams
== NULL
) {
2831 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2833 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2840 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2841 * use the standard MH backup file.
2844 remove_file (const char *file
)
2847 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2848 int status
= system (rmm_command
);
2851 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2853 /* This is OK for a non-message file, it still uses the
2854 BACKUP_PREFIX form. The backup file will be in the same
2855 directory as file. */
2856 return rename (file
, m_backup (file
));
2861 * Output formatted message to user.
2864 report (char *what
, char *partno
, char *filename
, char *message
, ...)
2870 va_start (args
, message
);
2871 fmt
= concat (filename
, partno
? " part " : ", ",
2872 FENDNULL(partno
), partno
? ", " : "", message
, NULL
);
2874 advertise (what
, NULL
, fmt
, args
);
2887 fprintf (stderr
, "\n");