2 * mhfixmsg.c -- rewrite a message with various transformations
4 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
5 * See the COPYRIGHT file in the root directory of the nmh
6 * distribution for complete copyright information.
11 #include <h/mhparse.h>
13 #include <h/signals.h>
16 #define MHFIXMSG_SWITCHES \
17 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
18 X("nodecodetext", 0, NDECODETEXTSW) \
19 X("decodetypes", 0, DECODETYPESW) \
20 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
21 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
22 X("textcharset", 0, TEXTCHARSETSW) \
23 X("notextcharset", 0, NTEXTCHARSETSW) \
24 X("reformat", 0, REFORMATSW) \
25 X("noreformat", 0, NREFORMATSW) \
26 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
27 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
28 X("fixboundary", 0, FIXBOUNDARYSW) \
29 X("nofixboundary", 0, NFIXBOUNDARYSW) \
30 X("fixcte", 0, FIXCOMPOSITECTESW) \
31 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
32 X("fixtype mimetype", 0, FIXTYPESW) \
33 X("file file", 0, FILESW) \
34 X("outfile file", 0, OUTFILESW) \
35 X("rmmproc program", 0, RPROCSW) \
36 X("normmproc", 0, NRPRCSW) \
37 X("changecur", 0, CHGSW) \
38 X("nochangecur", 0, NCHGSW) \
39 X("verbose", 0, VERBSW) \
40 X("noverbose", 0, NVERBSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(MHFIXMSG
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
54 int debugsw
; /* Needed by mhparse.c. */
56 #define quitser pipeser
59 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
60 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
61 extern int bogus_mp_content
; /* flag from InitMultiPart */
62 /* flags to/from parse_header_attrs */
63 extern int suppress_extraneous_trailing_semicolon_warning
;
66 int output_message_fp (CT
, FILE *, char *);
69 void flush_errors (void);
77 typedef struct fix_transformations
{
85 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
88 } fix_transformations
;
90 int mhfixmsgsbr (CT
*, char *, const fix_transformations
*, char *, FILE **);
91 static int fix_boundary (CT
*, int *);
92 static int copy_input_to_output (const char *, const char *, FILE *);
93 static int get_multipart_boundary (CT
, char **);
94 static int replace_boundary (CT
, char *, char *);
95 static int fix_types (CT
, svector_t
, int *);
96 static char *replace_substring (char **, const char *, const char *);
97 static char *remove_parameter (char *, const char *);
98 static int fix_composite_cte (CT
, int *);
99 static int set_ce (CT
, int);
100 static int ensure_text_plain (CT
*, CT
, int *, int);
101 static int find_textplain_sibling (CT
, int, int *);
102 static int insert_new_text_plain_part (CT
, int, CT
);
103 static CT
build_text_plain_part (CT
);
104 static int insert_into_new_mp_alt (CT
*, int *);
105 static CT
divide_part (CT
);
106 static void copy_ctinfo (CI
, CI
);
107 static int decode_part (CT
);
108 static int reformat_part (CT
, char *, char *, char *, int);
109 static CT
build_multipart_alt (CT
, CT
, int, int);
110 static int boundary_in_content (FILE **, char *, const char *);
111 static void transfer_noncontent_headers (CT
, CT
);
112 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
113 static int decode_text_parts (CT
, int, const char *, int *);
114 static int should_decode(const char *, const char *, const char *);
115 static int content_encoding (CT
, const char **);
116 static int strip_crs (CT
, int *);
117 static void update_cte (CT
);
118 static int least_restrictive_encoding (CT
);
119 static int less_restrictive (int, int);
120 static int convert_charsets (CT
, char *, int *);
121 static int fix_always (CT
, int *);
122 static int fix_filename_param (char *, char *, PM
*, PM
*);
123 static int fix_filename_encoding (CT
);
124 static int write_content (CT
, const char *, char *, FILE *, int, int);
125 static void set_text_ctparams(CT
, char *, int);
126 static int remove_file (const char *);
127 static void report (char *, char *, char *, char *, ...);
128 static void pipeser (int);
132 main (int argc
, char **argv
) {
134 char *cp
, *file
= NULL
, *folder
= NULL
;
135 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
136 char **argp
, **arguments
;
137 struct msgs_array msgs
= { 0, 0, NULL
};
138 struct msgs
*mp
= NULL
;
140 FILE *fp
, *outfp
= NULL
;
144 fix_transformations fx
;
145 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
147 fx
.replacetextplain
= 0;
148 fx
.decodetext
= CE_8BIT
;
149 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
150 fx
.lf_line_endings
= 0;
151 fx
.textcharset
= NULL
;
153 if (nmh_init(argv
[0], 2)) { return 1; }
155 arguments
= getarguments (invo_name
, argc
, argv
, 1);
161 while ((cp
= *argp
++)) {
163 switch (smatch (++cp
, switches
)) {
165 ambigsw (cp
, switches
);
168 adios (NULL
, "-%s unknown", cp
);
171 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
173 print_help (buf
, switches
, 1);
176 print_version(invo_name
);
180 if (! (cp
= *argp
++) || *cp
== '-') {
181 adios (NULL
, "missing argument to %s", argp
[-2]);
183 if (! strcasecmp (cp
, "8bit")) {
184 fx
.decodetext
= CE_8BIT
;
185 } else if (! strcasecmp (cp
, "7bit")) {
186 fx
.decodetext
= CE_7BIT
;
187 } else if (! strcasecmp (cp
, "binary")) {
188 fx
.decodetext
= CE_BINARY
;
190 adios (NULL
, "invalid argument to %s", argp
[-2]);
197 if (! (cp
= *argp
++) || *cp
== '-') {
198 adios (NULL
, "missing argument to %s", argp
[-2]);
202 case CRLFLINEBREAKSSW
:
203 fx
.lf_line_endings
= 0;
205 case NCRLFLINEBREAKSSW
:
206 fx
.lf_line_endings
= 1;
209 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
210 adios (NULL
, "missing argument to %s", argp
[-2]);
223 case FIXCOMPOSITECTESW
:
224 fx
.fixcompositecte
= 1;
226 case NFIXCOMPOSITECTESW
:
227 fx
.fixcompositecte
= 0;
230 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
231 adios (NULL
, "missing argument to %s", argp
[-2]);
233 if (! strncasecmp (cp
, "multipart/", 10) ||
234 ! strncasecmp (cp
, "message/", 8)) {
235 adios (NULL
, "-fixtype %s not allowed", cp
);
236 } else if (! strchr (cp
, '/')) {
237 adios (NULL
, "-fixtype requires type/subtype");
239 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
240 svector_push_back (fx
.fixtypes
, cp
);
248 case REPLACETEXTPLAINSW
:
249 fx
.replacetextplain
= 1;
251 case NREPLACETEXTPLAINSW
:
252 fx
.replacetextplain
= 0;
255 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
256 adios (NULL
, "missing argument to %s", argp
[-2]);
258 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
261 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
262 adios (NULL
, "missing argument to %s", argp
[-2]);
264 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
267 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
268 adios (NULL
, "missing argument to %s", argp
[-2]);
288 if (*cp
== '+' || *cp
== '@') {
290 adios (NULL
, "only one folder at a time!");
292 folder
= pluspath (cp
);
296 /* Interpret a full path as a filename, not a message. */
297 file
= mh_xstrdup (cp
);
299 app_msgarg (&msgs
, cp
);
304 SIGNAL (SIGQUIT
, quitser
);
305 SIGNAL (SIGPIPE
, pipeser
);
308 * Read the standard profile setup
310 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
311 readconfig ((struct node
**) 0, fp
, cp
, 0);
315 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
316 suppress_extraneous_trailing_semicolon_warning
= 1;
318 if (! context_find ("path")) {
319 free (path ("./", TFOLDER
));
322 if (file
&& msgs
.size
) {
323 adios (NULL
, "cannot specify msg and file at same time!");
327 /* Open the outfile now, so we don't have to risk opening it
328 after running out of fds. */
329 if (strcmp (outfile
, "-") == 0) {
331 } else if ((outfp
= fopen (outfile
, "w")) == NULL
) {
332 adios (outfile
, "unable to open for writing");
337 * check if message is coming from file
340 /* If file is stdin, create a tmp file name before parse_mime()
341 has a chance, because it might put in on a different
342 filesystem than the output file. Instead, put it in the
343 user's preferred tmp directory. */
346 if (! strcmp ("-", file
)) {
352 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
353 adios (NULL
, "unable to create temporary file in %s",
357 file
= mh_xstrdup (cp
);
358 cpydata (STDIN_FILENO
, fd
, "-", file
);
362 (void) m_unlink (file
);
363 adios (NULL
, "failed to write temporary file");
367 cts
= mh_xcalloc(2, sizeof *cts
);
370 if ((ct
= parse_mime (file
))) {
371 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
374 advise (NULL
, "unable to parse message from file %s", file
);
377 /* If there's an outfile, pass the input message unchanged, so the message won't
378 get dropped from a pipeline. */
380 /* Something went wrong. Output might be expected, such as if this were run
381 as a filter. Just copy the input to the output. */
382 if (copy_input_to_output (file
, outfile
, outfp
) != OK
) {
383 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
389 * message(s) are coming from a folder
394 app_msgarg(&msgs
, "cur");
397 folder
= getfolder (1);
399 maildir
= m_maildir (folder
);
401 /* chdir so that error messages, esp. from MIME parser, just
402 refer to the message and not its path. */
403 if (chdir (maildir
) == NOTOK
) {
404 adios (maildir
, "unable to change directory to");
407 /* read folder and create message structure */
408 if (! (mp
= folder_read (folder
, 1))) {
409 adios (NULL
, "unable to read folder %s", folder
);
412 /* check for empty folder */
413 if (mp
->nummsg
== 0) {
414 adios (NULL
, "no messages in %s", folder
);
417 /* parse all the message ranges/sequences and set SELECTED */
418 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
419 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
422 seq_setprev (mp
); /* set the previous-sequence */
424 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
427 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
428 if (is_selected(mp
, msgnum
)) {
429 char *msgnam
= m_name (msgnum
);
431 if ((ct
= parse_mime (msgnam
))) {
432 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
435 advise (NULL
, "unable to parse message %s", msgnam
);
438 /* If there's an outfile, pass the input message
439 unchanged, so the message won't get dropped from a
442 /* Something went wrong. Output might be expected,
443 such as if this were run as a filter. Just copy
444 the input to the output. */
445 /* Can't use path() here because 1) it might have been
446 called before and it caches the pwd, and 2) we call
447 chdir() after that. */
448 char *input_filename
=
449 concat (maildir
, "/", msgnam
, NULL
);
451 if (copy_input_to_output (input_filename
, outfile
,
454 "unable to copy message to %s, "
455 "it might be lost\n",
458 free (input_filename
);
465 seq_setcur (mp
, mp
->hghsel
); /* update current message */
467 seq_save (mp
); /* synchronize sequences */
468 context_replace (pfolder
, folder
);/* update current folder */
469 context_save (); /* save the context file */
473 for (ctp
= cts
; *ctp
; ++ctp
) {
474 status
+= mhfixmsgsbr (ctp
, maildir
, &fx
, outfile
, &outfp
);
478 (void) m_unlink (file
);
481 /* Just calling m_backup() unlinks the backup file. */
482 (void) m_backup (file
);
492 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
493 if (outfp
) { fclose (outfp
); } /* even if stdout */
505 * Apply transformations to one message.
508 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
509 char *outfile
, FILE **outfp
) {
510 /* Store input filename in case one of the transformations, i.e.,
511 fix_boundary(), rewrites to a tmp file. */
512 char *input_filename
= maildir
513 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
514 : mh_xstrdup ((*ctp
)->c_file
);
515 int modify_inplace
= 0;
516 int message_mods
= 0;
519 if (outfile
== NULL
) {
522 if ((*ctp
)->c_file
) {
524 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, outfp
)) ==
526 adios (NULL
, "unable to create temporary file in %s",
529 outfile
= mh_xstrdup (tempfile
);
531 adios (NULL
, "missing both input and output filenames\n");
533 } /* else *outfp was defined by caller */
535 reverse_alternative_parts (*ctp
);
536 status
= fix_always (*ctp
, &message_mods
);
537 if (status
== OK
&& fx
->fixboundary
) {
538 status
= fix_boundary (ctp
, &message_mods
);
540 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
541 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
543 if (status
== OK
&& fx
->fixcompositecte
) {
544 status
= fix_composite_cte (*ctp
, &message_mods
);
546 if (status
== OK
&& fx
->reformat
) {
548 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
550 if (status
== OK
&& fx
->decodetext
) {
551 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
555 if (status
== OK
&& fx
->textcharset
!= NULL
) {
556 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
559 if (status
== OK
&& ! (*ctp
)->c_umask
) {
560 /* Set the umask for the contents file. This currently
561 isn't used but just in case it is in the future. */
564 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
565 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
567 (*ctp
)->c_umask
= ~m_gmprot();
572 * Write the content to a file
575 status
= write_content (*ctp
, input_filename
, outfile
, *outfp
,
576 modify_inplace
, message_mods
);
577 } else if (! modify_inplace
) {
578 /* Something went wrong. Output might be expected, such
579 as if this were run as a filter. Just copy the input
581 if (copy_input_to_output (input_filename
, outfile
, *outfp
) != OK
) {
582 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
586 if (modify_inplace
) {
587 if (status
!= OK
) { (void) m_unlink (outfile
); }
592 free (input_filename
);
599 * Copy input message to output. Assumes not modifying in place, so this
600 * might be running as part of a pipeline.
603 copy_input_to_output (const char *input_filename
, const char *output_filename
,
605 int in
= open (input_filename
, O_RDONLY
);
606 int out
= fileno (outfp
);
609 if (in
!= -1 && out
!= -1) {
610 cpydata (in
, out
, input_filename
, output_filename
);
622 * Fix mismatched outer level boundary.
625 fix_boundary (CT
*ct
, int *message_mods
) {
626 struct multipart
*mp
;
629 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
630 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
633 * 1) Get boundary at end of part.
634 * 2) Get boundary at beginning of part and compare to the end-of-part
636 * 3) Write out contents of ct to tmp file, replacing boundary in
637 * header with boundary from part. Set c_unlink to 1.
639 * 5) Call parse_mime() on the tmp file, replacing ct.
642 if (mp
&& mp
->mp_start
) {
645 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
648 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
649 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
650 char *filename
= mh_xstrdup ((*ct
)->c_file
);
654 if ((fixed_ct
= parse_mime (fixed
))) {
660 report (NULL
, NULL
, filename
,
661 "fix multipart boundary");
665 advise (NULL
, "unable to parse fixed part");
670 advise (NULL
, "unable to replace broken boundary");
674 advise (NULL
, "unable to create temporary file in %s",
679 free (part_boundary
);
681 /* Couldn't fix the boundary. Report failure so that mhfixmsg
682 doesn't modify the message. */
686 /* No multipart struct, even though the content type is
687 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
698 * Find boundary at end of multipart.
701 get_multipart_boundary (CT ct
, char **part_boundary
) {
702 char buffer
[NMH_BUFSIZ
];
703 char *end_boundary
= NULL
;
704 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
705 ? (off_t
) (ct
->c_end
- sizeof buffer
)
706 : (off_t
) ct
->c_begin
;
710 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
711 be big enough, even if it's just 1024, to make that unlikely. */
713 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
714 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
715 advise (ct
->c_file
, "unable to open for reading");
719 /* Get boundary at end of multipart. */
720 while (begin
>= (off_t
) ct
->c_begin
) {
721 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
722 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
723 char *cp
= rfind_str (buffer
, bytes_read
, "--");
728 /* Trim off trailing "--" and anything beyond. */
730 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
731 if (strlen (end
) > 3 && *end
++ == '\n' &&
732 *end
++ == '-' && *end
++ == '-') {
733 end_boundary
= mh_xstrdup (end
);
740 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
741 begin
-= sizeof buffer
;
747 /* Get boundary at beginning of multipart. */
749 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
750 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
751 if (bytes_read
>= strlen (end_boundary
)) {
752 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
754 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
755 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
760 /* The start and end boundaries didn't match, or the
761 start boundary doesn't begin with "\n--" (or "--"
762 if at the beginning of buffer). Keep trying. */
776 *part_boundary
= end_boundary
;
778 *part_boundary
= NULL
;
787 * Open and copy ct->c_file to file, replacing the multipart boundary.
790 replace_boundary (CT ct
, char *file
, char *boundary
) {
793 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
795 m_getfld_state_t gstate
= 0;
798 if (ct
->c_file
== NULL
) {
799 advise (NULL
, "missing input filename");
803 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
804 advise (ct
->c_file
, "unable to open for reading");
808 if ((fpout
= fopen (file
, "w")) == NULL
) {
810 advise (file
, "unable to open for writing");
814 for (compnum
= 1;;) {
815 int bufsz
= (int) sizeof buf
;
817 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
822 /* get copies of the buffers */
823 np
= mh_xstrdup (name
);
824 vp
= mh_xstrdup (buf
);
826 /* if necessary, get rest of field */
827 while (state
== FLDPLUS
) {
829 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
830 vp
= add (buf
, vp
); /* add to previous value */
833 if (strcasecmp (TYPE_FIELD
, np
)) {
834 fprintf (fpout
, "%s:%s", np
, vp
);
836 char *new_ctline
, *new_params
;
838 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
839 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
842 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
843 ct
->c_ctinfo
.ci_subtype
, NULL
);
844 new_params
= output_params(strlen(TYPE_FIELD
) +
845 strlen(new_ctline
) + 1,
846 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
847 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
848 new_params
? new_params
: "");
850 mh_xfree(new_params
);
860 /* buf will have a terminating NULL, skip it. */
861 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
862 advise (file
, "fwrite");
871 advise (NULL
, "message format error in component #%d", compnum
);
876 advise (NULL
, "getfld() returned %d", state
);
884 m_getfld_state_destroy (&gstate
);
893 * Fix Content-Type header to reflect the content of its part.
896 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
899 switch (ct
->c_type
) {
901 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
904 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
905 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
911 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
912 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
914 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
921 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
922 for (typep
= svector_strs (fixtypes
);
923 typep
&& (type
= *typep
);
926 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
929 if (! strcasecmp (type
, type_subtype
) &&
930 decode_part (ct
) == OK
&&
931 ct
->c_cefile
.ce_file
!= NULL
) {
932 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
935 if ((cp
= strchr (ct_type_subtype
, ';'))) {
936 /* Truncate to remove any parameter list from
937 mime_type () result. */
941 if (strcasecmp (type
, ct_type_subtype
)) {
942 char *ct_type
, *ct_subtype
;
945 /* The Content-Type header does not match the
946 content, so update these struct Content
949 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
952 /* Extract type and subtype from type/subtype. */
953 ct_type
= mh_xstrdup(ct_type_subtype
);
954 if ((cp
= strchr (ct_type
, '/'))) {
956 ct_subtype
= mh_xstrdup(++cp
);
958 advise (NULL
, "missing / in MIME type of %s %s",
959 ct
->c_file
, ct
->c_partno
);
964 ct
->c_type
= ct_str_type (ct_type
);
965 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
967 free (ct
->c_ctinfo
.ci_type
);
968 ct
->c_ctinfo
.ci_type
= ct_type
;
969 free (ct
->c_ctinfo
.ci_subtype
);
970 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
971 if (! replace_substring (&ct
->c_ctline
, type
,
973 advise (NULL
, "did not find %s in %s",
977 /* Update Content-Type header field. */
978 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
979 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
980 if (replace_substring (&hf
->value
, type
,
984 report (NULL
, ct
->c_partno
, ct
->c_file
,
985 "change Content-Type in header "
987 type
, ct_type_subtype
);
991 advise (NULL
, "did not find %s in %s",
997 free (ct_type_subtype
);
1009 * Replace a substring, allocating space to hold the new one.
1012 replace_substring (char **str
, const char *old
, const char *new) {
1015 if ((cp
= strstr (*str
, old
))) {
1016 char *remainder
= cp
+ strlen (old
);
1017 char *prefix
, *new_str
;
1020 prefix
= mh_xstrdup(*str
);
1021 *(prefix
+ (cp
- *str
)) = '\0';
1022 new_str
= concat (prefix
, new, remainder
, NULL
);
1025 new_str
= concat (new, remainder
, NULL
);
1030 return *str
= new_str
;
1038 * Remove a name=value parameter, given just its name, from a header value.
1041 remove_parameter (char *str
, const char *name
) {
1042 /* It looks to me, based on the BNF in RFC 2045, than there can't
1043 be whitespace betwwen the parameter name and the "=", or
1044 between the "=" and the parameter value. */
1045 char *param_name
= concat (name
, "=", NULL
);
1048 if ((cp
= strstr (str
, param_name
))) {
1052 /* Remove any leading spaces, before the parameter name. */
1054 start
> str
&& isspace ((unsigned char) *(start
-1));
1058 /* Remove a leading semicolon. */
1059 if (start
> str
&& *(start
-1) == ';') { --start
; }
1061 end
= cp
+ strlen (name
) + 1;
1063 /* Skip past the quoted value, and then the final quote. */
1064 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1067 /* Skip past the value. */
1068 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1071 /* Count how many characters need to be moved. Include
1072 trailing null, which is accounted for by the
1073 initialization of count to 1. */
1074 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1075 (void) memmove (start
, end
, count
);
1085 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1086 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1090 fix_composite_cte (CT ct
, int *message_mods
) {
1093 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1094 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1095 ct
->c_encoding
!= CE_BINARY
) {
1098 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1099 char *name
= hf
->name
;
1100 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
1104 if (! strncasecmp (name
, ENCODING_FIELD
,
1105 strlen (ENCODING_FIELD
))) {
1106 char *prefix
= "Nmh-REPLACED-INVALID-";
1110 h
->name
= mh_xstrdup (hf
->name
);
1111 h
->hf_encoding
= hf
->hf_encoding
;
1115 /* Retain old header but prefix its name. */
1117 hf
->name
= concat (prefix
, h
->name
, NULL
);
1121 char *encoding
= cpytrim (hf
->value
);
1122 report (NULL
, ct
->c_partno
, ct
->c_file
,
1123 "replace Content-Transfer-Encoding of %s "
1124 "with 8 bit", encoding
);
1128 h
->value
= mh_xstrdup (" 8bit\n");
1130 /* Don't need to warn for multiple C-T-E header
1131 fields, parse_mime() already does that. But
1132 if there are any, fix them all as necessary. */
1137 set_ce (ct
, CE_8BIT
);
1140 if (ct
->c_type
== CT_MULTIPART
) {
1141 struct multipart
*m
;
1144 m
= (struct multipart
*) ct
->c_ctparams
;
1145 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1146 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1159 * Set content encoding.
1162 set_ce (CT ct
, int encoding
) {
1163 const char *ce
= ce_str (encoding
);
1164 const struct str2init
*ctinit
= get_ce_method (ce
);
1167 char *cte
= concat (" ", ce
, "\n", NULL
);
1170 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1171 caller is decode_text_parts (). Save because we'll
1173 struct cefile decoded_content_info
= ct
->c_cefile
;
1175 ct
->c_encoding
= encoding
;
1177 ct
->c_ctinitfnx
= ctinit
->si_init
;
1178 /* This will assign ct->c_cefile with an all-0 struct, which
1180 (*ctinit
->si_init
) (ct
);
1181 /* After returning, the caller should set
1182 ct->c_cefile.ce_file to the name of the file containing
1185 if (ct
->c_ceclosefnx
) {
1186 (*ct
->c_ceclosefnx
) (ct
);
1189 /* Restore the cefile. */
1190 ct
->c_cefile
= decoded_content_info
;
1192 /* Update/add Content-Transfer-Encoding header field. */
1193 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1194 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1201 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1204 /* Update c_celine. It's used only by mhlist -debug. */
1205 free (ct
->c_celine
);
1206 ct
->c_celine
= mh_xstrdup (cte
);
1216 * Make sure each text part has a corresponding text/plain part.
1219 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1222 switch ((*ct
)->c_type
) {
1224 /* Nothing to do for text/plain. */
1225 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1227 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1228 parent
->c_subtype
== MULTI_ALTERNATE
) {
1229 int new_subpart_number
= 1;
1230 int has_text_plain
=
1231 find_textplain_sibling (parent
, replacetextplain
,
1232 &new_subpart_number
);
1234 if (! has_text_plain
) {
1235 /* Parent is a multipart/alternative. Insert a new
1236 text/plain subpart. */
1237 const int inserted
=
1238 insert_new_text_plain_part (*ct
, new_subpart_number
,
1243 report (NULL
, parent
->c_partno
, parent
->c_file
,
1244 "insert text/plain part");
1250 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1251 parent
->c_subtype
== MULTI_RELATED
) {
1252 char *type_subtype
=
1253 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1254 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1255 const char *parent_type
=
1256 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1257 int new_subpart_number
= 1;
1258 int has_text_plain
= 0;
1260 /* Have to do string comparison on the subtype because we
1261 don't enumerate all of them in c_subtype values.
1262 parent_type will be NULL if the multipart/related part
1263 doesn't have a type parameter. The type parameter must
1264 be specified according to RFC 2387 Sec. 3.1 but not all
1266 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1267 /* The type of this part matches the root type of the
1268 parent multipart/related. Look to see if there's
1269 text/plain sibling. */
1271 find_textplain_sibling (parent
, replacetextplain
,
1272 &new_subpart_number
);
1275 free (type_subtype
);
1277 if (! has_text_plain
) {
1278 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1282 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1283 if (*ct
!= part
->mp_part
) {
1289 /* Parent is a multipart/related. Insert a new
1290 text/plain subpart in a new multipart/alternative. */
1291 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1292 /* Not an error if text/plain couldn't be added. */
1295 /* There are no siblings, so insert a new text/plain
1296 subpart, and change the parent type from
1297 multipart/related to multipart/alternative. */
1298 const int inserted
=
1299 insert_new_text_plain_part (*ct
, new_subpart_number
,
1305 parent
->c_subtype
= MULTI_ALTERNATE
;
1306 free (parent
->c_ctinfo
.ci_subtype
);
1307 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1308 if (! replace_substring (&parent
->c_ctline
, "/related",
1311 "did not find multipart/related in %s",
1315 /* Update Content-Type header field. */
1316 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1317 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1318 if (replace_substring (&hf
->value
, "/related",
1322 report (NULL
, parent
->c_partno
,
1324 "insert text/plain part");
1327 /* Remove, e.g., type="text/html" from
1328 multipart/alternative. */
1329 remove_parameter (hf
->value
, "type");
1332 advise (NULL
, "did not find multipart/"
1333 "related in header %s",
1339 /* Not an error if text/plain couldn't be inserted. */
1344 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1351 case CT_MULTIPART
: {
1352 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1355 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1356 if ((*ct
)->c_type
== CT_MULTIPART
) {
1357 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1365 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1366 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1368 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1379 * See if there is a sibling text/plain, and return its subpart number.
1382 find_textplain_sibling (CT parent
, int replacetextplain
,
1383 int *new_subpart_number
) {
1384 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1385 struct part
*part
, *prev
;
1386 int has_text_plain
= 0;
1388 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1389 ++*new_subpart_number
;
1390 if (part
->mp_part
->c_type
== CT_TEXT
&&
1391 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1392 if (replacetextplain
) {
1393 struct part
*old_part
;
1394 if (part
== mp
->mp_parts
) {
1395 old_part
= mp
->mp_parts
;
1396 mp
->mp_parts
= part
->mp_next
;
1398 old_part
= prev
->mp_next
;
1399 prev
->mp_next
= part
->mp_next
;
1402 report (NULL
, parent
->c_partno
, parent
->c_file
,
1403 "remove text/plain part %s",
1404 old_part
->mp_part
->c_partno
);
1406 free_content (old_part
->mp_part
);
1416 return has_text_plain
;
1421 * Insert a new text/plain part.
1424 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1425 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1426 struct part
*new_part
;
1429 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1431 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1433 new_part
->mp_next
= mp
->mp_parts
;
1434 mp
->mp_parts
= new_part
;
1435 new_part
->mp_part
->c_partno
=
1436 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1442 free_content (new_part
->mp_part
);
1450 * Create a text/plain part to go along with non-plain sibling part.
1453 build_text_plain_part (CT encoded_part
) {
1454 CT tp_part
= divide_part (encoded_part
);
1455 char *tmp_plain_file
= NULL
;
1457 if (decode_part (tp_part
) == OK
) {
1458 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1459 contains the decoded contents. And the decoding function, such
1460 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1461 be unlinked by free_content (). */
1464 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1465 advise (NULL
, "unable to create temporary file in %s",
1468 tmp_plain_file
= mh_xstrdup (tempfile
);
1469 if (reformat_part (tp_part
, tmp_plain_file
,
1470 tp_part
->c_ctinfo
.ci_type
,
1471 tp_part
->c_ctinfo
.ci_subtype
,
1472 tp_part
->c_type
) == OK
) {
1478 free_content (tp_part
);
1479 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1480 free (tmp_plain_file
);
1487 * Slip new text/plain part into a new multipart/alternative.
1490 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1491 CT tp_part
= build_text_plain_part (*ct
);
1495 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1498 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1500 if (mp
&& mp
->mp_parts
) {
1501 mp
->mp_parts
->mp_part
= tp_part
;
1502 /* Make the new multipart/alternative the parent. */
1507 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1508 "insert text/plain part");
1511 free_content (tp_part
);
1512 free_content (mp_alt
);
1519 /* Not an error if text/plain couldn't be built. */
1527 * Clone a MIME part.
1530 divide_part (CT ct
) {
1534 /* Just copy over what is needed for decoding. c_vrsn and
1535 c_celine aren't necessary. */
1536 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1537 new_part
->c_begin
= ct
->c_begin
;
1538 new_part
->c_end
= ct
->c_end
;
1539 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1540 new_part
->c_type
= ct
->c_type
;
1541 new_part
->c_cefile
= ct
->c_cefile
;
1542 new_part
->c_encoding
= ct
->c_encoding
;
1543 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1544 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1545 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1546 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1548 /* c_ctline is used by reformat__part(), so it can preserve
1549 anything after the type/subtype. */
1550 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1557 * Copy the content info from one part to another.
1560 copy_ctinfo (CI dest
, CI src
) {
1563 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1564 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1566 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1567 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1569 if (s_pm
->pm_charset
) {
1570 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1572 if (s_pm
->pm_lang
) {
1573 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1577 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1578 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1586 decode_part (CT ct
) {
1592 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, &file
)) == NULL
) {
1593 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1595 tmp_decoded
= mh_xstrdup (tempfile
);
1596 /* The following call will load ct->c_cefile.ce_file with the tmp
1597 filename of the decoded content. tmp_decoded will contain the
1598 encoded output, get rid of that. */
1599 status
= output_message_fp (ct
, file
, tmp_decoded
);
1600 (void) m_unlink (tmp_decoded
);
1608 * Reformat content as plain text.
1609 * Some of the arguments aren't really needed now, but maybe will
1610 * be in the future for other than text types.
1613 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1614 int output_subtype
, output_encoding
;
1615 const char *reason
= NULL
;
1619 /* Hacky: this redirects the output from whatever command is used
1620 to show the part to a file. So, the user can't have any output
1621 redirection in that command.
1622 Could show_multi() in mhshowsbr.c avoid this? */
1624 /* Check for invo_name-format-type/subtype. */
1625 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1627 advise (NULL
, "Don't know how to convert %s, there is no "
1628 "%s-format-%s/%s profile entry",
1629 ct
->c_file
, invo_name
, type
, subtype
);
1633 if (strchr (cf
, '>')) {
1634 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1635 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1636 subtype
? subtype
: "");
1641 cp
= concat (cf
, " >", file
, NULL
);
1642 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1645 /* Unlink decoded content tmp file and free its filename to avoid
1646 leaks. The file stream should already have been closed. */
1647 if (ct
->c_cefile
.ce_unlink
) {
1648 (void) m_unlink (ct
->c_cefile
.ce_file
);
1649 free (ct
->c_cefile
.ce_file
);
1650 ct
->c_cefile
.ce_file
= NULL
;
1651 ct
->c_cefile
.ce_unlink
= 0;
1654 if (c_type
== CT_TEXT
) {
1655 output_subtype
= TEXT_PLAIN
;
1657 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1661 output_encoding
= content_encoding (ct
, &reason
);
1663 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1664 ct
->c_cefile
.ce_file
= file
;
1665 ct
->c_cefile
.ce_unlink
= 1;
1667 ct
->c_cefile
.ce_unlink
= 0;
1676 * Fill in a multipart/alternative part.
1679 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1680 char *boundary_prefix
= "----=_nmh-multipart";
1681 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1682 char *boundary_indicator
= "; boundary=";
1683 char *typename
, *subtypename
, *name
;
1686 struct multipart
*m
;
1687 const struct str2init
*ctinit
;
1691 /* Set up the multipart/alternative part. These fields of *ct were
1692 initialized to 0 by mh_xcalloc():
1693 c_fp, c_unlink, c_begin, c_end,
1694 c_vrsn, c_ctline, c_celine,
1695 c_id, c_descr, c_dispo, c_partno,
1696 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1697 c_cefile, c_encoding,
1698 c_digested, c_digest[16], c_ctexbody,
1699 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1701 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1704 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1706 ct
->c_subtype
= subtype
;
1708 ctinit
= get_ct_init (ct
->c_type
);
1710 typename
= ct_type_str (type
);
1711 subtypename
= ct_subtype_str (type
, subtype
);
1715 int found_boundary
= 1;
1717 while (found_boundary
&& serial
< 1000000) {
1720 /* Ensure that the boundary doesn't appear in the decoded
1722 if (new_part
->c_cefile
.ce_file
) {
1723 if ((found_boundary
=
1724 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1725 new_part
->c_cefile
.ce_file
,
1726 boundary
)) == NOTOK
) {
1732 /* Ensure that the boundary doesn't appear in the encoded
1734 if (! found_boundary
&& new_part
->c_file
) {
1735 if ((found_boundary
=
1736 boundary_in_content (&new_part
->c_fp
,
1738 boundary
)) == NOTOK
) {
1744 if (found_boundary
) {
1745 /* Try a slightly different boundary. */
1750 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1752 concat (boundary_prefix
,
1753 first_alt
->c_partno
? first_alt
->c_partno
: "",
1754 "-", buffer2
, NULL
);
1758 if (found_boundary
) {
1759 advise (NULL
, "giving up trying to find a unique boundary");
1765 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1766 boundary
, "\"", NULL
);
1768 /* Load c_first_hf and c_last_hf. */
1769 transfer_noncontent_headers (first_alt
, ct
);
1770 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1773 /* Load c_partno. */
1774 if (first_alt
->c_partno
) {
1775 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1776 free (first_alt
->c_partno
);
1777 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1778 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1780 first_alt
->c_partno
= mh_xstrdup ("1");
1781 new_part
->c_partno
= mh_xstrdup ("2");
1785 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1786 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1789 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1790 "boundary", boundary
, 0);
1794 p
->mp_next
->mp_next
= NULL
;
1795 p
->mp_next
->mp_part
= first_alt
;
1798 m
->mp_start
= concat (boundary
, "\n", NULL
);
1799 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1810 * Check that the boundary does not appear in the content.
1813 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1814 char buffer
[NMH_BUFSIZ
];
1816 int found_boundary
= 0;
1818 /* free_content() will close *fp if we fopen it here. */
1819 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1820 advise (file
, "unable to open %s for reading", file
);
1824 fseeko (*fp
, 0L, SEEK_SET
);
1825 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1826 if (find_str (buffer
, bytes_read
, boundary
)) {
1832 return found_boundary
;
1837 * Remove all non-Content headers.
1840 transfer_noncontent_headers (CT old
, CT
new) {
1843 hp_prev
= hp
= old
->c_first_hf
;
1847 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1848 if (hp
== old
->c_last_hf
) {
1849 if (hp
== old
->c_first_hf
) {
1850 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1852 hp_prev
->next
= NULL
;
1853 old
->c_last_hf
= hp_prev
;
1856 if (hp
== old
->c_first_hf
) {
1857 old
->c_first_hf
= next
;
1859 hp_prev
->next
= next
;
1863 /* Put node hp in the new CT. */
1864 if (new->c_first_hf
== NULL
) {
1865 new->c_first_hf
= hp
;
1867 new->c_last_hf
->next
= hp
;
1869 new->c_last_hf
= hp
;
1871 /* A Content- header, leave in old. */
1884 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1885 char *typename
= ct_type_str (type
);
1886 char *subtypename
= ct_subtype_str (type
, subtype
);
1887 /* E.g, " text/plain" */
1888 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1889 /* E.g, " text/plain\n" */
1890 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1891 int found_content_type
= 0;
1893 const char *cp
= NULL
;
1897 /* Update/add Content-Type header field. */
1898 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1899 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1900 found_content_type
= 1;
1902 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1903 ? concat (type_subtypename
, cp
, "\n", NULL
)
1904 : mh_xstrdup (name_plus_nl
);
1907 if (! found_content_type
) {
1908 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
1909 (cp
= strchr (ct
->c_ctline
, ';'))
1910 ? concat (type_subtypename
, cp
, "\n", NULL
)
1911 : mh_xstrdup (name_plus_nl
));
1914 /* Some of these might not be used, but set them anyway. */
1916 ? concat (type_subtypename
, cp
, NULL
)
1917 : concat (type_subtypename
, NULL
);
1918 free (ct
->c_ctline
);
1919 ct
->c_ctline
= ctline
;
1920 /* Leave other ctinfo members as they were. */
1921 free (ct
->c_ctinfo
.ci_type
);
1922 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1923 free (ct
->c_ctinfo
.ci_subtype
);
1924 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1926 ct
->c_subtype
= subtype
;
1928 free (name_plus_nl
);
1929 free (type_subtypename
);
1931 status
= set_ce (ct
, encoding
);
1938 * It's not necessary to update the charset parameter of a Content-Type
1939 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1940 * (content) was originally in the specified charset, "and will be in
1941 * that character set again after decoding."
1944 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
1945 int *message_mods
) {
1947 int lf_line_endings
= 0;
1949 switch (ct
->c_type
) {
1950 case CT_MULTIPART
: {
1951 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1954 /* Should check to see if the body for this part is encoded?
1955 For now, it gets passed along as-is by InitMultiPart(). */
1956 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1957 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
1964 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1965 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1967 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
1973 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
1978 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
1980 switch (ct
->c_encoding
) {
1985 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1986 const char *reason
= NULL
;
1988 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1989 && encoding
!= CE_BINARY
) {
1990 /* The decoding isn't acceptable so discard it.
1991 Leave status as OK to allow other transformations. */
1993 report (NULL
, ct
->c_partno
, ct
->c_file
,
1994 "will not decode%s because it is binary (%s)",
1996 : ct
->c_ctline
? ct
->c_ctline
2000 (void) m_unlink (ct
->c_cefile
.ce_file
);
2001 free (ct
->c_cefile
.ce_file
);
2002 ct
->c_cefile
.ce_file
= NULL
;
2003 } else if (ct
->c_encoding
== CE_QUOTED
&&
2004 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2005 /* The decoding isn't acceptable so discard it.
2006 Leave status as OK to allow other transformations. */
2008 report (NULL
, ct
->c_partno
, ct
->c_file
,
2009 "will not decode%s because it is 8bit",
2011 : ct
->c_ctline
? ct
->c_ctline
2014 (void) m_unlink (ct
->c_cefile
.ce_file
);
2015 free (ct
->c_cefile
.ce_file
);
2016 ct
->c_cefile
.ce_file
= NULL
;
2020 if (ct_encoding
== CE_BINARY
) {
2022 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2027 if (set_ce (ct
, enc
) == OK
) {
2030 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2031 ct
->c_ctline
? ct
->c_ctline
: "");
2033 if (lf_line_endings
) {
2034 strip_crs (ct
, message_mods
);
2047 if (lf_line_endings
) {
2048 strip_crs (ct
, message_mods
);
2063 * Determine if the part with type[/subtype] should be decoded, according to
2064 * decodetypes (which came from the -decodetypes switch).
2067 should_decode(const char *decodetypes
, const char *type
, const char *subtype
) {
2068 /* Quick search for matching type[/subtype] in decodetypes: bracket
2069 decodetypes with commas, then search for ,type, and ,type/subtype, in
2072 int found_match
= 0;
2073 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2074 char *delimited_type
= concat(",", type
, ",", NULL
);
2076 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2078 } else if (subtype
!= NULL
) {
2079 char *delimited_type_subtype
=
2080 concat(",", type
, "/", subtype
, ",", NULL
);
2082 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2085 free(delimited_type_subtype
);
2088 free(delimited_type
);
2089 free(delimited_decodetypes
);
2096 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2097 * if it has any NUL characters, a CR not followed by a LF, or lines
2098 * greater than 998 characters in length. If binary, reason is set
2099 * to a string explaining why.
2102 content_encoding (CT ct
, const char **reason
) {
2103 CE ce
= &ct
->c_cefile
;
2104 int encoding
= CE_7BIT
;
2107 size_t line_len
= 0;
2108 char buffer
[NMH_BUFSIZ
];
2111 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2112 advise (ce
->ce_file
, "unable to open for reading");
2116 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2117 while (encoding
!= CE_BINARY
&&
2118 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2121 int last_char_was_cr
= 0;
2123 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2124 if (*cp
== '\0' || ++line_len
> 998 ||
2125 (*cp
!= '\n' && last_char_was_cr
)) {
2126 encoding
= CE_BINARY
;
2128 *reason
= "null character";
2129 } else if (line_len
> 998) {
2130 *reason
= "line length > 998";
2131 } else if (*cp
!= '\n' && last_char_was_cr
) {
2132 *reason
= "CR not followed by LF";
2134 /* Should not reach this. */
2138 } else if (*cp
== '\n') {
2140 } else if (! isascii ((unsigned char) *cp
)) {
2144 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2150 } /* else should never happen */
2157 * Strip carriage returns from content.
2160 strip_crs (CT ct
, int *message_mods
) {
2161 char *charset
= content_charset (ct
);
2164 /* Only strip carriage returns if content is ASCII or another
2165 charset that has the same readily recognizable CR followed by a
2166 LF. We can include UTF-8 here because if the high-order bit of
2167 a UTF-8 byte is 0, then it must be a single-byte ASCII
2169 if (! strcasecmp (charset
, "US-ASCII") ||
2170 ! strcasecmp (charset
, "UTF-8") ||
2171 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2172 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2178 int opened_input_file
= 0;
2180 if (ct
->c_cefile
.ce_file
) {
2181 file
= &ct
->c_cefile
.ce_file
;
2182 fp
= &ct
->c_cefile
.ce_fp
;
2184 } else if (ct
->c_file
) {
2187 begin
= (size_t) ct
->c_begin
;
2188 end
= (size_t) ct
->c_end
;
2189 } /* else don't know where the content is */
2191 if (file
&& *file
&& fp
) {
2193 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2194 advise (*file
, "unable to open for reading");
2197 opened_input_file
= 1;
2203 char buffer
[NMH_BUFSIZ
];
2205 size_t bytes_to_read
=
2206 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2208 fseeko (*fp
, begin
, SEEK_SET
);
2209 while ((bytes_read
= fread (buffer
, 1,
2210 min (bytes_to_read
, sizeof buffer
),
2212 /* Look for CR followed by a LF. This is supposed to
2213 be text so there should be LF's. If not, don't
2214 modify the content. */
2217 int last_char_was_cr
= 0;
2219 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2221 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2222 if (*cp
== '\n' && last_char_was_cr
) {
2227 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2233 char *stripped_content_file
;
2234 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2236 if (tempfile
== NULL
) {
2237 adios (NULL
, "unable to create temporary file in %s",
2240 stripped_content_file
= mh_xstrdup (tempfile
);
2242 /* Strip each CR before a LF from the content. */
2243 fseeko (*fp
, begin
, SEEK_SET
);
2244 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2248 int last_char_was_cr
= 0;
2250 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2252 last_char_was_cr
= 1;
2253 } else if (last_char_was_cr
) {
2255 if (write (fd
, "\r", 1) < 0) {
2256 advise (tempfile
, "CR write");
2259 if (write (fd
, cp
, 1) < 0) {
2260 advise (tempfile
, "write");
2262 last_char_was_cr
= 0;
2264 if (write (fd
, cp
, 1) < 0) {
2265 advise (tempfile
, "write");
2267 last_char_was_cr
= 0;
2273 admonish (NULL
, "unable to write temporary file %s",
2274 stripped_content_file
);
2275 (void) m_unlink (stripped_content_file
);
2278 /* Replace the decoded file with the converted one. */
2279 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2280 (void) m_unlink (ct
->c_cefile
.ce_file
);
2282 mh_xfree(ct
->c_cefile
.ce_file
);
2283 ct
->c_cefile
.ce_file
= stripped_content_file
;
2284 ct
->c_cefile
.ce_unlink
= 1;
2288 report (NULL
, ct
->c_partno
,
2289 begin
== 0 && end
== 0 ? "" : *file
,
2295 if (opened_input_file
) {
2309 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2310 * of the part C-T-E's.
2313 update_cte (CT ct
) {
2314 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2316 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2317 least_restrictive_enc
!= CE_7BIT
) {
2318 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2322 /* Update/add Content-Transfer-Encoding header field. */
2323 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2324 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2331 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2338 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2342 least_restrictive_encoding (CT ct
) {
2343 int encoding
= CE_UNKNOWN
;
2345 switch (ct
->c_type
) {
2346 case CT_MULTIPART
: {
2347 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2350 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2351 const int part_encoding
=
2352 least_restrictive_encoding (part
->mp_part
);
2354 if (less_restrictive (encoding
, part_encoding
)) {
2355 encoding
= part_encoding
;
2362 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2363 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2364 const int part_encoding
=
2365 least_restrictive_encoding (e
->eb_content
);
2367 if (less_restrictive (encoding
, part_encoding
)) {
2368 encoding
= part_encoding
;
2374 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2375 encoding
= ct
->c_encoding
;
2384 * Return whether the second encoding is less restrictive than the first, where
2385 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2386 * CE_BINARY is less restrictive than CE_8BIT and
2387 * CE_8BIT is less restrictive than CE_7BIT.
2390 less_restrictive (int encoding
, int second_encoding
) {
2391 switch (second_encoding
) {
2393 return encoding
!= CE_BINARY
;
2395 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2397 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2398 encoding
!= CE_7BIT
;
2406 * Convert character set of each part.
2409 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2412 switch (ct
->c_type
) {
2414 if (ct
->c_subtype
== TEXT_PLAIN
) {
2415 status
= convert_charset (ct
, dest_charset
, message_mods
);
2418 char *ct_charset
= content_charset (ct
);
2420 report (NULL
, ct
->c_partno
, ct
->c_file
,
2421 "convert %s to %s", ct_charset
, dest_charset
);
2425 char *ct_charset
= content_charset (ct
);
2427 report ("iconv", ct
->c_partno
, ct
->c_file
,
2428 "failed to convert %s to %s", ct_charset
, dest_charset
);
2434 case CT_MULTIPART
: {
2435 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2438 /* Should check to see if the body for this part is encoded?
2439 For now, it gets passed along as-is by InitMultiPart(). */
2440 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2442 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2448 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2449 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2452 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2465 * Fix various problems that aren't handled elsewhere. These
2466 * are fixed unconditionally: there are no switches to disable
2467 * them. Currently, "problems" are these:
2468 * 1) remove extraneous semicolon at the end of a header parameter list
2469 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2470 * filename parameters in Content-Type and Content-Disposition
2471 * headers, respectively.
2474 fix_always (CT ct
, int *message_mods
) {
2477 switch (ct
->c_type
) {
2478 case CT_MULTIPART
: {
2479 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2482 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2483 status
= fix_always (part
->mp_part
, message_mods
);
2489 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2490 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2492 status
= fix_always (e
->eb_content
, message_mods
);
2499 if (ct
->c_first_hf
) {
2500 fix_filename_encoding (ct
);
2503 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2504 size_t len
= strlen (hf
->value
);
2506 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2507 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2508 /* Only do this for Content-Type and
2509 Content-Disposition fields because those are the
2510 only headers that parse_mime() warns about. */
2514 /* whitespace following a trailing ';' will be nuked as well */
2515 if (hf
->value
[len
- 1] == '\n') {
2516 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2517 if (len
-- == 0) { break; }
2521 if (hf
->value
[len
- 2] == ';') {
2522 /* Remove trailing ';' from parameter value. */
2523 hf
->value
[len
- 2] = '\n';
2524 hf
->value
[len
- 1] = '\0';
2526 /* Also, if Content-Type parameter, remove trailing ';'
2527 from ct->c_ctline. This probably isn't necessary
2529 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2530 size_t l
= strlen(ct
->c_ctline
) - 1;
2531 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2532 ct
->c_ctline
[l
] == ';') {
2533 ct
->c_ctline
[l
--] = '\0';
2534 if (l
== 0) { break; }
2540 report (NULL
, ct
->c_partno
, ct
->c_file
,
2541 "remove trailing ; from %s parameter value",
2553 * Factor out common code for loops in fix_filename_encoding().
2556 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
) {
2559 if (HasPrefix(value
, "=?") && HasSuffix(value
, "?=")) {
2560 /* Looks like an RFC 2047 encoded parameter. */
2561 char decoded
[PATH_MAX
+ 1];
2563 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2564 /* Encode using RFC 2231. */
2565 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2568 advise (NULL
, "failed to decode %s parameter %s", name
, value
);
2577 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2578 * filename parameters in Content-Type and Content-Disposition
2579 * headers, respectively.
2582 fix_filename_encoding (CT ct
) {
2587 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2588 if (pm
->pm_name
&& pm
->pm_value
&&
2589 strcasecmp (pm
->pm_name
, "name") == 0) {
2590 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2591 &ct
->c_ctinfo
.ci_first_pm
,
2592 &ct
->c_ctinfo
.ci_last_pm
);
2596 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2597 if (pm
->pm_name
&& pm
->pm_value
&&
2598 strcasecmp (pm
->pm_name
, "filename") == 0) {
2599 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2605 /* Fix hf values to correspond. */
2606 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2607 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2609 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2610 field
= TYPE_HEADER
;
2611 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2612 field
= DISPO_HEADER
;
2615 if (field
!= OTHER
) {
2616 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2618 if (semicolon_loc
) {
2620 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2621 const char *const params
=
2623 field
== TYPE_HEADER
2624 ? ct
->c_ctinfo
.ci_first_pm
2625 : ct
->c_dispo_first
,
2627 const char *const new_params
= concat (params
, "\n", NULL
);
2629 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2630 free((void *)new_params
); /* Cast away const. Sigh. */
2631 free((void *)params
);
2633 advise (NULL
, "did not find semicolon in %s:%s\n",
2634 hf
->name
, hf
->value
);
2644 * Output content in input file to output file.
2647 write_content (CT ct
, const char *input_filename
, char *outfile
, FILE *outfp
,
2648 int modify_inplace
, int message_mods
) {
2651 if (modify_inplace
) {
2652 if (message_mods
> 0) {
2653 if ((status
= output_message_fp (ct
, outfp
, outfile
)) == OK
) {
2654 char *infile
= input_filename
2655 ? mh_xstrdup (input_filename
)
2656 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2658 if (remove_file (infile
) == OK
) {
2659 if (rename (outfile
, infile
)) {
2660 /* Rename didn't work, possibly because of an
2661 attempt to rename across filesystems. Try
2662 brute force copy. */
2663 int old
= open (outfile
, O_RDONLY
);
2665 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2668 if (old
!= -1 && new != -1) {
2669 char buffer
[NMH_BUFSIZ
];
2671 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2673 if (write (new, buffer
, i
) != i
) {
2679 if (new != -1) { close (new); }
2680 if (old
!= -1) { close (old
); }
2681 (void) m_unlink (outfile
);
2684 /* The -file argument processing used path() to
2685 expand filename to absolute path. */
2686 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2688 admonish (NULL
, "unable to rename %s %s to %s",
2689 file
? "file" : "message", outfile
,
2695 admonish (NULL
, "unable to remove input file %s, "
2696 "not modifying it", infile
);
2697 (void) m_unlink (outfile
);
2706 /* No modifications and didn't need the tmp outfile. */
2707 (void) m_unlink (outfile
);
2710 /* Output is going to some file. Produce it whether or not
2711 there were modifications. */
2712 status
= output_message_fp (ct
, outfp
, outfile
);
2721 * parse_mime() does not set lf_line_endings in struct text, so use this
2722 * function to do it. It touches the parts the decodetypes identifies.
2725 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
) {
2726 switch (ct
->c_type
) {
2727 case CT_MULTIPART
: {
2728 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2731 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2732 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2738 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2739 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2741 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2746 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2747 if (ct
->c_ctparams
== NULL
) {
2748 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2750 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2757 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2758 * use the standard MH backup file.
2761 remove_file (const char *file
) {
2763 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2764 int status
= system (rmm_command
);
2767 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2769 /* This is OK for a non-message file, it still uses the
2770 BACKUP_PREFIX form. The backup file will be in the same
2771 directory as file. */
2772 return rename (file
, m_backup (file
));
2777 * Output formatted message to user.
2780 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2785 va_start (args
, message
);
2786 fmt
= concat (filename
, partno
? " part " : ", ",
2787 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
2789 advertise (what
, NULL
, fmt
, args
);
2802 fprintf (stderr
, "\n");