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", 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, FIXCTESW) \
31 X("nofixcte", 0, NFIXCTESW) \
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
;
64 extern int extraneous_trailing_semicolon
;
67 int output_message (CT
, char *);
70 void flush_errors (void);
74 void freects_done (int) NORETURN
;
79 typedef struct fix_transformations
{
87 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
90 } fix_transformations
;
92 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
93 static int fix_boundary (CT
*, int *);
94 static int copy_input_to_output (const char *, const char *);
95 static int get_multipart_boundary (CT
, char **);
96 static int replace_boundary (CT
, char *, char *);
97 static int fix_types (CT
, svector_t
, int *);
98 static char *replace_substring (char **, const char *, const char *);
99 static char *remove_parameter (char *, const char *);
100 static int fix_multipart_cte (CT
, int *);
101 static int set_ce (CT
, int);
102 static int ensure_text_plain (CT
*, CT
, int *, int);
103 static int find_textplain_sibling (CT
, int, int *);
104 static int insert_new_text_plain_part (CT
, int, CT
);
105 static CT
build_text_plain_part (CT
);
106 static int insert_into_new_mp_alt (CT
*, int *);
107 static CT
divide_part (CT
);
108 static void copy_ctinfo (CI
, CI
);
109 static int decode_part (CT
);
110 static int reformat_part (CT
, char *, char *, char *, int);
111 static int charset_encoding (CT
);
112 static CT
build_multipart_alt (CT
, CT
, int, int);
113 static int boundary_in_content (FILE **, char *, const char *);
114 static void transfer_noncontent_headers (CT
, CT
);
115 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
116 static int decode_text_parts (CT
, int, const char *, int *);
117 static int should_decode(const char *, const char *, const char *);
118 static int content_encoding (CT
, const char **);
119 static int strip_crs (CT
, int *);
120 static int convert_charsets (CT
, char *, int *);
121 static int fix_always (CT
, int *);
122 static int write_content (CT
, const char *, char *, int, int);
123 static void set_text_ctparams(CT
, char *, int);
124 static int remove_file (const char *);
125 static void report (char *, char *, char *, char *, ...);
126 static void pipeser (int);
130 main (int argc
, char **argv
) {
132 char *cp
, *file
= NULL
, *folder
= NULL
;
133 char *maildir
, buf
[100], *outfile
= NULL
;
134 char **argp
, **arguments
;
135 struct msgs_array msgs
= { 0, 0, NULL
};
136 struct msgs
*mp
= NULL
;
142 fix_transformations fx
;
143 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
145 fx
.replacetextplain
= 0;
146 fx
.decodetext
= CE_8BIT
;
147 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
148 fx
.lf_line_endings
= 0;
149 fx
.textcharset
= NULL
;
151 if (nmh_init(argv
[0], 1)) { 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
;
188 adios (NULL
, "invalid argument to %s", argp
[-2]);
195 if (! (cp
= *argp
++) || *cp
== '-') {
196 adios (NULL
, "missing argument to %s", argp
[-2]);
200 case CRLFLINEBREAKSSW
:
201 fx
.lf_line_endings
= 0;
203 case NCRLFLINEBREAKSSW
:
204 fx
.lf_line_endings
= 1;
207 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
208 adios (NULL
, "missing argument to %s", argp
[-2]);
228 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
229 adios (NULL
, "missing argument to %s", argp
[-2]);
231 if (! strncasecmp (cp
, "multipart/", 10) ||
232 ! strncasecmp (cp
, "message/", 8)) {
233 adios (NULL
, "-fixtype %s not allowed", cp
);
234 } else if (! strchr (cp
, '/')) {
235 adios (NULL
, "-fixtype requires type/subtype");
237 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
238 svector_push_back (fx
.fixtypes
, cp
);
246 case REPLACETEXTPLAINSW
:
247 fx
.replacetextplain
= 1;
249 case NREPLACETEXTPLAINSW
:
250 fx
.replacetextplain
= 0;
253 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
254 adios (NULL
, "missing argument to %s", argp
[-2]);
256 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
259 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
260 adios (NULL
, "missing argument to %s", argp
[-2]);
262 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
265 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
266 adios (NULL
, "missing argument to %s", argp
[-2]);
286 if (*cp
== '+' || *cp
== '@') {
288 adios (NULL
, "only one folder at a time!");
290 folder
= pluspath (cp
);
294 /* Interpret a full path as a filename, not a message. */
295 file
= add (cp
, NULL
);
297 app_msgarg (&msgs
, cp
);
302 SIGNAL (SIGQUIT
, quitser
);
303 SIGNAL (SIGPIPE
, pipeser
);
306 * Read the standard profile setup
308 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
309 readconfig ((struct node
**) 0, fp
, cp
, 0);
313 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
314 suppress_extraneous_trailing_semicolon_warning
= 1;
316 if (! context_find ("path")) {
317 free (path ("./", TFOLDER
));
320 if (file
&& msgs
.size
) {
321 adios (NULL
, "cannot specify msg and file at same time!");
325 * check if message is coming from file
328 /* If file is stdin, create a tmp file name before parse_mime()
329 has a chance, because it might put in on a different
330 filesystem than the output file. Instead, put it in the
331 user's preferred tmp directory. */
334 if (! strcmp ("-", file
)) {
340 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
341 adios (NULL
, "unable to create temporary file in %s",
345 file
= add (cp
, NULL
);
346 cpydata (STDIN_FILENO
, fd
, "-", file
);
350 (void) m_unlink (file
);
351 adios (NULL
, "failed to write temporary file");
355 if (! (cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof *cts
))) {
356 adios (NULL
, "out of memory");
360 if ((ct
= parse_mime (file
))) {
361 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
364 advise (NULL
, "unable to parse message from file %s", file
);
367 /* If there's an outfile, pass the input message unchanged, so the message won't
368 get dropped from a pipeline. */
370 /* Something went wrong. Output might be expected, such as if this were run
371 as a filter. Just copy the input to the output. */
372 if (copy_input_to_output (file
, outfile
) != OK
) {
373 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
379 * message(s) are coming from a folder
384 app_msgarg(&msgs
, "cur");
387 folder
= getfolder (1);
389 maildir
= m_maildir (folder
);
391 if (chdir (maildir
) == NOTOK
) {
392 adios (maildir
, "unable to change directory to");
395 /* read folder and create message structure */
396 if (! (mp
= folder_read (folder
, 1))) {
397 adios (NULL
, "unable to read folder %s", folder
);
400 /* check for empty folder */
401 if (mp
->nummsg
== 0) {
402 adios (NULL
, "no messages in %s", folder
);
405 /* parse all the message ranges/sequences and set SELECTED */
406 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
407 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
410 seq_setprev (mp
); /* set the previous-sequence */
413 (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
))) {
414 adios (NULL
, "out of memory");
418 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
419 if (is_selected(mp
, msgnum
)) {
422 msgnam
= m_name (msgnum
);
423 if ((ct
= parse_mime (msgnam
))) {
424 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
427 advise (NULL
, "unable to parse message %s", msgnam
);
430 /* If there's an outfile, pass the input message unchanged, so the message won't
431 get dropped from a pipeline. */
433 /* Something went wrong. Output might be expected, such as if this were run
434 as a filter. Just copy the input to the output. */
435 const char *input_filename
= path (msgnam
, TFILE
);
437 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
438 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
446 seq_setcur (mp
, mp
->hghsel
); /* update current message */
448 seq_save (mp
); /* synchronize sequences */
449 context_replace (pfolder
, folder
);/* update current folder */
450 context_save (); /* save the context file */
454 for (ctp
= cts
; *ctp
; ++ctp
) {
455 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
458 (void) m_unlink (file
);
461 /* Just calling m_backup() unlinks the backup file. */
462 (void) m_backup (file
);
470 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
476 /* done is freects_done, which will clean up all of cts. */
483 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
484 /* Store input filename in case one of the transformations, i.e.,
485 fix_boundary(), rewrites to a tmp file. */
486 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
487 int modify_inplace
= 0;
488 int message_mods
= 0;
491 if (outfile
== NULL
) {
494 if ((*ctp
)->c_file
) {
496 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
497 adios (NULL
, "unable to create temporary file in %s",
500 outfile
= add (tempfile
, NULL
);
502 adios (NULL
, "missing both input and output filenames\n");
506 reverse_alternative_parts (*ctp
);
507 status
= fix_always (*ctp
, &message_mods
);
508 if (status
== OK
&& fx
->fixboundary
) {
509 status
= fix_boundary (ctp
, &message_mods
);
511 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
512 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
514 if (status
== OK
&& fx
->fixcte
) {
515 status
= fix_multipart_cte (*ctp
, &message_mods
);
517 if (status
== OK
&& fx
->reformat
) {
519 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
521 if (status
== OK
&& fx
->decodetext
) {
522 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
, &message_mods
);
524 if (status
== OK
&& fx
->textcharset
!= NULL
) {
525 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
528 if (status
== OK
&& ! (*ctp
)->c_umask
) {
529 /* Set the umask for the contents file. This currently
530 isn't used but just in case it is in the future. */
533 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
534 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
536 (*ctp
)->c_umask
= ~m_gmprot();
541 * Write the content to a file
544 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
546 } else if (! modify_inplace
) {
547 /* Something went wrong. Output might be expected, such
548 as if this were run as a filter. Just copy the input
550 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
551 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
555 if (modify_inplace
) {
556 if (status
!= OK
) { (void) m_unlink (outfile
); }
561 free (input_filename
);
567 /* Copy input message to output. Assumes not modifying in place, so this
568 might be running as part of a pipeline. */
570 copy_input_to_output (const char *input_filename
, const char *output_filename
) {
571 int in
= open (input_filename
, O_RDONLY
);
572 int out
= strcmp (output_filename
, "-")
573 ? open (output_filename
, O_WRONLY
| O_CREAT
, m_gmprot ())
577 if (in
!= -1 && out
!= -1) {
578 cpydata (in
, out
, input_filename
, output_filename
);
591 fix_boundary (CT
*ct
, int *message_mods
) {
592 struct multipart
*mp
;
595 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
596 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
599 * 1) Get boundary at end of part.
600 * 2) Get boundary at beginning of part and compare to the end-of-part
602 * 3) Write out contents of ct to tmp file, replacing boundary in
603 * header with boundary from part. Set c_unlink to 1.
605 * 5) Call parse_mime() on the tmp file, replacing ct.
608 if (mp
&& mp
->mp_start
) {
611 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
614 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
615 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
616 char *filename
= add ((*ct
)->c_file
, NULL
);
620 if ((fixed_ct
= parse_mime (fixed
))) {
626 report (NULL
, NULL
, filename
,
627 "fix multipart boundary");
631 advise (NULL
, "unable to parse fixed part");
636 advise (NULL
, "unable to replace broken boundary");
640 advise (NULL
, "unable to create temporary file in %s",
645 free (part_boundary
);
647 /* Couldn't fix the boundary. Report failure so that mhfixmsg
648 doesn't modify the message. */
652 /* No multipart struct, even though the content type is
653 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
664 get_multipart_boundary (CT ct
, char **part_boundary
) {
666 char *end_boundary
= NULL
;
667 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
668 ? (off_t
) (ct
->c_end
- sizeof buffer
)
669 : (off_t
) ct
->c_begin
;
673 /* This will fail if the boundary spans fread() calls. BUFSIZ should
674 be big enough, even if it's just 1024, to make that unlikely. */
676 /* free_content() will close ct->c_fp. */
677 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
678 advise (ct
->c_file
, "unable to open for reading");
682 /* Get boundary at end of multipart. */
683 while (begin
>= (off_t
) ct
->c_begin
) {
684 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
685 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
686 char *cp
= rfind_str (buffer
, bytes_read
, "--");
691 /* Trim off trailing "--" and anything beyond. */
693 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
694 if (strlen (end
) > 3 && *end
++ == '\n' &&
695 *end
++ == '-' && *end
++ == '-') {
696 end_boundary
= add (end
, NULL
);
703 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
704 begin
-= sizeof buffer
;
710 /* Get boundary at beginning of multipart. */
712 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
713 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
714 if (bytes_read
>= strlen (end_boundary
)) {
715 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
717 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
718 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
723 /* The start and end boundaries didn't match, or the
724 start boundary doesn't begin with "\n--" (or "--"
725 if at the beginning of buffer). Keep trying. */
734 *part_boundary
= end_boundary
;
736 *part_boundary
= NULL
;
744 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
746 replace_boundary (CT ct
, char *file
, char *boundary
) {
749 char buf
[BUFSIZ
], name
[NAMESZ
];
751 m_getfld_state_t gstate
= 0;
754 if (ct
->c_file
== NULL
) {
755 advise (NULL
, "missing input filename");
759 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
760 advise (ct
->c_file
, "unable to open for reading");
764 if ((fpout
= fopen (file
, "w")) == NULL
) {
766 advise (file
, "unable to open for writing");
770 for (compnum
= 1;;) {
771 int bufsz
= (int) sizeof buf
;
773 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
778 /* get copies of the buffers */
779 np
= add (name
, NULL
);
780 vp
= add (buf
, NULL
);
782 /* if necessary, get rest of field */
783 while (state
== FLDPLUS
) {
785 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
786 vp
= add (buf
, vp
); /* add to previous value */
789 if (strcasecmp (TYPE_FIELD
, np
)) {
790 fprintf (fpout
, "%s:%s", np
, vp
);
792 char *new_ctline
, *new_params
;
794 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
795 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
798 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
799 ct
->c_ctinfo
.ci_subtype
, NULL
);
800 new_params
= output_params(strlen(TYPE_FIELD
) +
801 strlen(new_ctline
) + 1,
802 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
803 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
804 new_params
? new_params
: "");
818 /* buf will have a terminating NULL, skip it. */
819 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
820 advise (file
, "fwrite");
829 advise (NULL
, "message format error in component #%d", compnum
);
834 advise (NULL
, "getfld() returned %d", state
);
842 m_getfld_state_destroy (&gstate
);
851 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
854 switch (ct
->c_type
) {
856 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
859 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
860 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
866 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
867 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
869 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
876 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
877 for (typep
= svector_strs (fixtypes
);
878 typep
&& (type
= *typep
);
881 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
884 if (! strcasecmp (type
, type_subtype
) &&
885 decode_part (ct
) == OK
&&
886 ct
->c_cefile
.ce_file
!= NULL
) {
887 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
890 if ((cp
= strchr (ct_type_subtype
, ';'))) {
891 /* Truncate to remove any parameter list from
892 mime_type () result. */
896 if (strcasecmp (type
, ct_type_subtype
)) {
897 char *ct_type
, *ct_subtype
;
900 /* The Content-Type header does not match the
901 content, so update these struct Content
904 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
907 /* Extract type and subtype from type/subtype. */
908 ct_type
= getcpy (ct_type_subtype
);
909 if ((cp
= strchr (ct_type
, '/'))) {
911 ct_subtype
= getcpy (++cp
);
913 advise (NULL
, "missing / in MIME type of %s %s",
914 ct
->c_file
, ct
->c_partno
);
919 ct
->c_type
= ct_str_type (ct_type
);
920 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
922 free (ct
->c_ctinfo
.ci_type
);
923 ct
->c_ctinfo
.ci_type
= ct_type
;
924 free (ct
->c_ctinfo
.ci_subtype
);
925 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
926 if (! replace_substring (&ct
->c_ctline
, type
,
928 advise (NULL
, "did not find %s in %s",
932 /* Update Content-Type header field. */
933 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
934 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
935 if (replace_substring (&hf
->value
, type
,
939 report (NULL
, ct
->c_partno
, ct
->c_file
,
940 "change Content-Type in header "
942 type
, ct_type_subtype
);
946 advise (NULL
, "did not find %s in %s",
952 free (ct_type_subtype
);
963 replace_substring (char **str
, const char *old
, const char *new) {
966 if ((cp
= strstr (*str
, old
))) {
967 char *remainder
= cp
+ strlen (old
);
968 char *prefix
, *new_str
;
971 prefix
= getcpy (*str
);
972 *(prefix
+ (cp
- *str
)) = '\0';
973 new_str
= concat (prefix
, new, remainder
, NULL
);
976 new_str
= concat (new, remainder
, NULL
);
981 return *str
= new_str
;
988 * Remove a name=value parameter, given just its name, from a header value.
991 remove_parameter (char *str
, const char *name
) {
992 /* It looks to me, based on the BNF in RFC 2045, than there can't
993 be whitespace betwwen the parameter name and the "=", or
994 between the "=" and the parameter value. */
995 char *param_name
= concat (name
, "=", NULL
);
998 if ((cp
= strstr (str
, param_name
))) {
1002 /* Remove any leading spaces, before the parameter name. */
1004 start
> str
&& isspace ((unsigned char) *(start
-1));
1008 /* Remove a leading semicolon. */
1009 if (start
> str
&& *(start
-1) == ';') { --start
; }
1011 end
= cp
+ strlen (name
) + 1;
1013 /* Skip past the quoted value, and then the final quote. */
1014 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1017 /* Skip past the value. */
1018 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1021 /* Count how many characters need to be moved. Include
1022 trailing null, which is accounted for by the
1023 initialization of count to 1. */
1024 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1025 (void) memmove (start
, end
, count
);
1034 fix_multipart_cte (CT ct
, int *message_mods
) {
1037 if (ct
->c_type
== CT_MULTIPART
) {
1038 struct multipart
*m
;
1041 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1042 ct
->c_encoding
!= CE_BINARY
) {
1045 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1046 char *name
= hf
->name
;
1047 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
1051 if (! strncasecmp (name
, ENCODING_FIELD
,
1052 strlen (ENCODING_FIELD
))) {
1053 char *prefix
= "Nmh-REPLACED-INVALID-";
1054 HF h
= mh_xmalloc (sizeof *h
);
1056 h
->name
= add (hf
->name
, NULL
);
1057 h
->hf_encoding
= hf
->hf_encoding
;
1061 /* Retain old header but prefix its name. */
1063 hf
->name
= concat (prefix
, h
->name
, NULL
);
1067 char *encoding
= cpytrim (hf
->value
);
1068 report (NULL
, ct
->c_partno
, ct
->c_file
,
1069 "replace Content-Transfer-Encoding of %s "
1070 "with 8 bit", encoding
);
1074 h
->value
= add (" 8bit\n", NULL
);
1076 /* Don't need to warn for multiple C-T-E header
1077 fields, parse_mime() already does that. But
1078 if there are any, fix them all as necessary. */
1083 set_ce (ct
, CE_8BIT
);
1086 m
= (struct multipart
*) ct
->c_ctparams
;
1087 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1088 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
1100 set_ce (CT ct
, int encoding
) {
1101 const char *ce
= ce_str (encoding
);
1102 const struct str2init
*ctinit
= get_ce_method (ce
);
1105 char *cte
= concat (" ", ce
, "\n", NULL
);
1108 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1109 caller is decode_text_parts (). Save because we'll
1111 struct cefile decoded_content_info
= ct
->c_cefile
;
1113 ct
->c_encoding
= encoding
;
1115 ct
->c_ctinitfnx
= ctinit
->si_init
;
1116 /* This will assign ct->c_cefile with an all-0 struct, which
1118 (*ctinit
->si_init
) (ct
);
1119 /* After returning, the caller should set
1120 ct->c_cefile.ce_file to the name of the file containing
1123 /* Restore the cefile. */
1124 ct
->c_cefile
= decoded_content_info
;
1126 /* Update/add Content-Transfer-Encoding header field. */
1127 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1128 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1135 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
1138 /* Update c_celine. It's used only by mhlist -debug. */
1139 free (ct
->c_celine
);
1140 ct
->c_celine
= add (cte
, NULL
);
1149 /* Make sure each text part has a corresponding text/plain part. */
1151 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1154 switch ((*ct
)->c_type
) {
1156 /* Nothing to do for text/plain. */
1157 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1159 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1160 parent
->c_subtype
== MULTI_ALTERNATE
) {
1161 int new_subpart_number
= 1;
1162 int has_text_plain
=
1163 find_textplain_sibling (parent
, replacetextplain
,
1164 &new_subpart_number
);
1166 if (! has_text_plain
) {
1167 /* Parent is a multipart/alternative. Insert a new
1168 text/plain subpart. */
1169 const int inserted
=
1170 insert_new_text_plain_part (*ct
, new_subpart_number
,
1175 report (NULL
, parent
->c_partno
, parent
->c_file
,
1176 "insert text/plain part");
1182 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1183 parent
->c_subtype
== MULTI_RELATED
) {
1184 char *type_subtype
=
1185 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1186 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1187 const char *parent_type
=
1188 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1189 int new_subpart_number
= 1;
1190 int has_text_plain
= 0;
1192 /* Have to do string comparison on the subtype because we
1193 don't enumerate all of them in c_subtype values.
1194 parent_type will be NULL if the multipart/related part
1195 doesn't have a type parameter. The type parameter must
1196 be specified according to RFC 2387 Sec. 3.1 but not all
1198 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1199 /* The type of this part matches the root type of the
1200 parent multipart/related. Look to see if there's
1201 text/plain sibling. */
1203 find_textplain_sibling (parent
, replacetextplain
,
1204 &new_subpart_number
);
1207 free (type_subtype
);
1209 if (! has_text_plain
) {
1210 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1214 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1215 if (*ct
!= part
->mp_part
) {
1221 /* Parent is a multipart/related. Insert a new
1222 text/plain subpart in a new multipart/alternative. */
1223 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1224 /* Not an error if text/plain couldn't be added. */
1227 /* There are no siblings, so insert a new text/plain
1228 subpart, and change the parent type from
1229 multipart/related to multipart/alternative. */
1230 const int inserted
=
1231 insert_new_text_plain_part (*ct
, new_subpart_number
,
1237 parent
->c_subtype
= MULTI_ALTERNATE
;
1238 parent
->c_ctinfo
.ci_subtype
= getcpy ("alternative");
1239 if (! replace_substring (&parent
->c_ctline
, "/related",
1242 "did not find multipart/related in %s",
1246 /* Update Content-Type header field. */
1247 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1248 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1249 if (replace_substring (&hf
->value
, "/related",
1253 report (NULL
, parent
->c_partno
,
1255 "insert text/plain part");
1258 /* Remove, e.g., type="text/html" from
1259 multipart/alternative. */
1260 remove_parameter (hf
->value
, "type");
1263 advise (NULL
, "did not find multipart/"
1264 "related in header %s",
1270 /* Not an error if text/plain couldn't be inserted. */
1275 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1282 case CT_MULTIPART
: {
1283 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1286 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1287 if ((*ct
)->c_type
== CT_MULTIPART
) {
1288 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1296 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1297 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1299 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1309 /* See if there is a sibling text/plain. */
1311 find_textplain_sibling (CT parent
, int replacetextplain
,
1312 int *new_subpart_number
) {
1313 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1314 struct part
*part
, *prev
;
1315 int has_text_plain
= 0;
1317 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1318 ++*new_subpart_number
;
1319 if (part
->mp_part
->c_type
== CT_TEXT
&&
1320 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1321 if (replacetextplain
) {
1322 struct part
*old_part
;
1323 if (part
== mp
->mp_parts
) {
1324 old_part
= mp
->mp_parts
;
1325 mp
->mp_parts
= part
->mp_next
;
1327 old_part
= prev
->mp_next
;
1328 prev
->mp_next
= part
->mp_next
;
1331 report (NULL
, parent
->c_partno
, parent
->c_file
,
1332 "remove text/plain part %s",
1333 old_part
->mp_part
->c_partno
);
1335 free_content (old_part
->mp_part
);
1345 return has_text_plain
;
1350 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1351 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1352 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
1354 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1356 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1358 new_part
->mp_next
= mp
->mp_parts
;
1359 mp
->mp_parts
= new_part
;
1360 new_part
->mp_part
->c_partno
=
1361 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1366 free_content (new_part
->mp_part
);
1375 build_text_plain_part (CT encoded_part
) {
1376 CT tp_part
= divide_part (encoded_part
);
1377 char *tmp_plain_file
= NULL
;
1379 if (decode_part (tp_part
) == OK
) {
1380 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1381 contains the decoded contents. And the decoding function, such
1382 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1383 be unlinked by free_content (). */
1386 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1387 advise (NULL
, "unable to create temporary file in %s",
1390 tmp_plain_file
= add (tempfile
, NULL
);
1391 if (reformat_part (tp_part
, tmp_plain_file
,
1392 tp_part
->c_ctinfo
.ci_type
,
1393 tp_part
->c_ctinfo
.ci_subtype
,
1394 tp_part
->c_type
) == OK
) {
1400 free_content (tp_part
);
1401 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1402 free (tmp_plain_file
);
1408 /* Slip new text/plain part into a new multipart/alternative. */
1410 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1411 CT tp_part
= build_text_plain_part (*ct
);
1415 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1418 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1420 if (mp
&& mp
->mp_parts
) {
1421 mp
->mp_parts
->mp_part
= tp_part
;
1422 /* Make the new multipart/alternative the parent. */
1427 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1428 "insert text/plain part");
1431 free_content (tp_part
);
1432 free_content (mp_alt
);
1439 /* Not an error if text/plain couldn't be built. */
1446 divide_part (CT ct
) {
1449 if ((new_part
= (CT
) mh_xcalloc (1, sizeof *new_part
)) == NULL
)
1450 adios (NULL
, "out of memory");
1452 /* Just copy over what is needed for decoding. c_vrsn and
1453 c_celine aren't necessary. */
1454 new_part
->c_file
= add (ct
->c_file
, NULL
);
1455 new_part
->c_begin
= ct
->c_begin
;
1456 new_part
->c_end
= ct
->c_end
;
1457 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1458 new_part
->c_type
= ct
->c_type
;
1459 new_part
->c_cefile
= ct
->c_cefile
;
1460 new_part
->c_encoding
= ct
->c_encoding
;
1461 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1462 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1463 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1464 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1466 /* c_ctline is used by reformat__part(), so it can preserve
1467 anything after the type/subtype. */
1468 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1475 copy_ctinfo (CI dest
, CI src
) {
1478 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1479 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1481 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1482 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1484 if (s_pm
->pm_charset
)
1485 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1487 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1490 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1491 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1496 decode_part (CT ct
) {
1501 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1502 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1504 tmp_decoded
= add (tempfile
, NULL
);
1505 /* The following call will load ct->c_cefile.ce_file with the tmp
1506 filename of the decoded content. tmp_decoded will contain the
1507 encoded output, get rid of that. */
1508 status
= output_message (ct
, tmp_decoded
);
1509 (void) m_unlink (tmp_decoded
);
1516 /* Some of the arguments aren't really needed now, but maybe will
1517 be in the future for other than text types. */
1519 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1520 int output_subtype
, output_encoding
;
1524 /* Hacky: this redirects the output from whatever command is used
1525 to show the part to a file. So, the user can't have any output
1526 redirection in that command.
1527 Could show_multi() in mhshowsbr.c avoid this? */
1529 /* Check for invo_name-format-type/subtype. */
1530 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1532 advise (NULL
, "Don't know how to convert %s, there is no "
1533 "%s-format-%s/%s profile entry",
1534 ct
->c_file
, invo_name
, type
, subtype
);
1538 if (strchr (cf
, '>')) {
1539 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1540 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1541 subtype
? subtype
: "");
1547 cp
= concat (cf
, " >", file
, NULL
);
1548 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1551 /* Unlink decoded content tmp file and free its filename to avoid
1552 leaks. The file stream should already have been closed. */
1553 if (ct
->c_cefile
.ce_unlink
) {
1554 (void) m_unlink (ct
->c_cefile
.ce_file
);
1555 free (ct
->c_cefile
.ce_file
);
1556 ct
->c_cefile
.ce_file
= NULL
;
1557 ct
->c_cefile
.ce_unlink
= 0;
1560 if (c_type
== CT_TEXT
) {
1561 output_subtype
= TEXT_PLAIN
;
1563 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1566 output_encoding
= charset_encoding (ct
);
1568 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1569 ct
->c_cefile
.ce_file
= file
;
1570 ct
->c_cefile
.ce_unlink
= 1;
1572 ct
->c_cefile
.ce_unlink
= 0;
1580 /* Identifies 7bit or 8bit content based on charset. */
1582 charset_encoding (CT ct
) {
1583 char *ct_charset
= content_charset (ct
);
1584 int encoding
= strcasecmp (ct_charset
, "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1593 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1594 char *boundary_prefix
= "----=_nmh-multipart";
1595 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1596 char *boundary_indicator
= "; boundary=";
1597 char *typename
, *subtypename
, *name
;
1600 struct multipart
*m
;
1601 const struct str2init
*ctinit
;
1603 if ((ct
= (CT
) mh_xcalloc (1, sizeof *ct
)) == NULL
)
1604 adios (NULL
, "out of memory");
1606 /* Set up the multipart/alternative part. These fields of *ct were
1607 initialized to 0 by mh_xcalloc():
1608 c_fp, c_unlink, c_begin, c_end,
1609 c_vrsn, c_ctline, c_celine,
1610 c_id, c_descr, c_dispo, c_partno,
1611 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1612 c_cefile, c_encoding,
1613 c_digested, c_digest[16], c_ctexbody,
1614 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1616 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1619 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1621 ct
->c_subtype
= subtype
;
1623 ctinit
= get_ct_init (ct
->c_type
);
1625 typename
= ct_type_str (type
);
1626 subtypename
= ct_subtype_str (type
, subtype
);
1630 int found_boundary
= 1;
1632 while (found_boundary
&& serial
< 1000000) {
1635 /* Ensure that the boundary doesn't appear in the decoded
1637 if (new_part
->c_cefile
.ce_file
) {
1638 if ((found_boundary
=
1639 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1640 new_part
->c_cefile
.ce_file
,
1647 /* Ensure that the boundary doesn't appear in the encoded
1649 if (! found_boundary
&& new_part
->c_file
) {
1650 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1658 if (found_boundary
) {
1659 /* Try a slightly different boundary. */
1664 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1666 concat (boundary_prefix
,
1667 first_alt
->c_partno
? first_alt
->c_partno
: "",
1668 "-", buffer2
, NULL
);
1672 if (found_boundary
) {
1673 advise (NULL
, "giving up trying to find a unique boundary");
1679 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1680 boundary
, "\"", NULL
);
1682 /* Load c_first_hf and c_last_hf. */
1683 transfer_noncontent_headers (first_alt
, ct
);
1684 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1687 /* Load c_partno. */
1688 if (first_alt
->c_partno
) {
1689 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1690 free (first_alt
->c_partno
);
1691 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1692 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1694 first_alt
->c_partno
= add ("1", NULL
);
1695 new_part
->c_partno
= add ("2", NULL
);
1699 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1700 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1703 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1704 "boundary", boundary
, 0);
1706 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1707 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1708 p
->mp_next
->mp_next
= NULL
;
1709 p
->mp_next
->mp_part
= first_alt
;
1711 if ((m
= (struct multipart
*) mh_xcalloc (1, sizeof (struct multipart
))) ==
1713 adios (NULL
, "out of memory");
1714 m
->mp_start
= concat (boundary
, "\n", NULL
);
1715 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1725 /* Check that the boundary does not appear in the content. */
1727 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1728 char buffer
[BUFSIZ
];
1730 int found_boundary
= 0;
1732 /* free_content() will close *fp if we fopen it here. */
1733 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1734 advise (file
, "unable to open %s for reading", file
);
1738 fseeko (*fp
, 0L, SEEK_SET
);
1739 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1740 if (find_str (buffer
, bytes_read
, boundary
)) {
1746 return found_boundary
;
1750 /* Remove all non-Content headers. */
1752 transfer_noncontent_headers (CT old
, CT
new) {
1755 hp_prev
= hp
= old
->c_first_hf
;
1759 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1760 if (hp
== old
->c_last_hf
) {
1761 if (hp
== old
->c_first_hf
) {
1762 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1764 hp_prev
->next
= NULL
;
1765 old
->c_last_hf
= hp_prev
;
1768 if (hp
== old
->c_first_hf
) {
1769 old
->c_first_hf
= next
;
1771 hp_prev
->next
= next
;
1775 /* Put node hp in the new CT. */
1776 if (new->c_first_hf
== NULL
) {
1777 new->c_first_hf
= hp
;
1779 new->c_last_hf
->next
= hp
;
1781 new->c_last_hf
= hp
;
1783 /* A Content- header, leave in old. */
1793 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1794 char *typename
= ct_type_str (type
);
1795 char *subtypename
= ct_subtype_str (type
, subtype
);
1796 /* E.g, " text/plain" */
1797 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1798 /* E.g, " text/plain\n" */
1799 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1800 int found_content_type
= 0;
1802 const char *cp
= NULL
;
1806 /* Update/add Content-Type header field. */
1807 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1808 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1809 found_content_type
= 1;
1811 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1812 ? concat (type_subtypename
, cp
, "\n", NULL
)
1813 : add (name_plus_nl
, NULL
);
1816 if (! found_content_type
) {
1817 add_header (ct
, add (TYPE_FIELD
, NULL
),
1818 (cp
= strchr (ct
->c_ctline
, ';'))
1819 ? concat (type_subtypename
, cp
, "\n", NULL
)
1820 : add (name_plus_nl
, NULL
));
1823 /* Some of these might not be used, but set them anyway. */
1825 ? concat (type_subtypename
, cp
, NULL
)
1826 : concat (type_subtypename
, NULL
);
1827 free (ct
->c_ctline
);
1828 ct
->c_ctline
= ctline
;
1829 /* Leave other ctinfo members as they were. */
1830 free (ct
->c_ctinfo
.ci_type
);
1831 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1832 free (ct
->c_ctinfo
.ci_subtype
);
1833 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1835 ct
->c_subtype
= subtype
;
1837 free (name_plus_nl
);
1838 free (type_subtypename
);
1840 status
= set_ce (ct
, encoding
);
1847 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
, int *message_mods
) {
1849 int lf_line_endings
= 0;
1851 switch (ct
->c_type
) {
1852 case CT_MULTIPART
: {
1853 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1856 /* Should check to see if the body for this part is encoded?
1857 For now, it gets passed along as-is by InitMultiPart(). */
1858 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1859 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
, message_mods
);
1865 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1866 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1868 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
, message_mods
);
1873 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
1878 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
1880 switch (ct
->c_encoding
) {
1885 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1886 const char *reason
= NULL
;
1888 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1889 && encoding
!= CE_BINARY
) {
1890 /* The decoding isn't acceptable so discard it.
1891 Leave status as OK to allow other transformations. */
1893 report (NULL
, ct
->c_partno
, ct
->c_file
,
1894 "will not decode%s because it is binary (%s)",
1896 : ct
->c_ctline
? ct
->c_ctline
1900 (void) m_unlink (ct
->c_cefile
.ce_file
);
1901 free (ct
->c_cefile
.ce_file
);
1902 ct
->c_cefile
.ce_file
= NULL
;
1903 } else if (ct
->c_encoding
== CE_QUOTED
&&
1904 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1905 /* The decoding isn't acceptable so discard it.
1906 Leave status as OK to allow other transformations. */
1908 report (NULL
, ct
->c_partno
, ct
->c_file
,
1909 "will not decode%s because it is 8bit",
1911 : ct
->c_ctline
? ct
->c_ctline
1914 (void) m_unlink (ct
->c_cefile
.ce_file
);
1915 free (ct
->c_cefile
.ce_file
);
1916 ct
->c_cefile
.ce_file
= NULL
;
1919 if (ct_encoding
== CE_BINARY
) {
1921 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1924 enc
= charset_encoding (ct
);
1926 if (set_ce (ct
, enc
) == OK
) {
1929 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
1930 ct
->c_ctline
? ct
->c_ctline
: "");
1932 if (lf_line_endings
) {
1933 strip_crs (ct
, message_mods
);
1946 if (lf_line_endings
) {
1947 strip_crs (ct
, message_mods
);
1961 /* Determine if the part with type[/subtype] should be decoded, according to
1962 decodetypes (which came from the -decodetypes switch). */
1964 should_decode(const char *decodetypes
, const char *type
, const char *subtype
) {
1965 /* Quick search for matching type[/subtype] in decodetypes: bracket
1966 decodetypes with commas, then search for ,type, and ,type/subtype, in
1969 int found_match
= 0;
1970 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
1971 char *delimited_type
= concat(",", type
, ",", NULL
);
1973 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
1975 } else if (subtype
!= NULL
) {
1976 char *delimited_type_subtype
=
1977 concat(",", type
, "/", subtype
, ",", NULL
);
1979 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
1982 free(delimited_type_subtype
);
1985 free(delimited_type
);
1986 free(delimited_decodetypes
);
1992 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1993 if it has any NUL characters, a CR not followed by a LF, or lines
1994 greater than 998 characters in length. If binary, reason is set
1995 to a string explaining why. */
1997 content_encoding (CT ct
, const char **reason
) {
1998 CE ce
= &ct
->c_cefile
;
1999 int encoding
= CE_7BIT
;
2002 size_t line_len
= 0;
2003 char buffer
[BUFSIZ
];
2006 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2007 advise (ce
->ce_file
, "unable to open for reading");
2011 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2012 while (encoding
!= CE_BINARY
&&
2013 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2016 int last_char_was_cr
= 0;
2018 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2019 if (*cp
== '\0' || ++line_len
> 998 ||
2020 (*cp
!= '\n' && last_char_was_cr
)) {
2021 encoding
= CE_BINARY
;
2023 *reason
= "null character";
2024 } else if (line_len
> 998) {
2025 *reason
= "line length > 998";
2026 } else if (*cp
!= '\n' && last_char_was_cr
) {
2027 *reason
= "CR not followed by LF";
2029 /* Should not reach this. */
2033 } else if (*cp
== '\n') {
2035 } else if (! isascii ((unsigned char) *cp
)) {
2039 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2045 } /* else should never happen */
2052 strip_crs (CT ct
, int *message_mods
) {
2053 char *charset
= content_charset (ct
);
2056 /* Only strip carriage returns if content is ASCII or another
2057 charset that has the same readily recognizable CR followed by a
2058 LF. We can include UTF-8 here because if the high-order bit of
2059 a UTF-8 byte is 0, then it must be a single-byte ASCII
2061 if (! strcasecmp (charset
, "US-ASCII") ||
2062 ! strcasecmp (charset
, "UTF-8") ||
2063 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2064 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2070 int opened_input_file
= 0;
2072 if (ct
->c_cefile
.ce_file
) {
2073 file
= &ct
->c_cefile
.ce_file
;
2074 fp
= &ct
->c_cefile
.ce_fp
;
2076 } else if (ct
->c_file
) {
2079 begin
= (size_t) ct
->c_begin
;
2080 end
= (size_t) ct
->c_end
;
2081 } /* else don't know where the content is */
2083 if (file
&& *file
&& fp
) {
2085 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2086 advise (*file
, "unable to open for reading");
2089 opened_input_file
= 1;
2095 char buffer
[BUFSIZ
];
2097 size_t bytes_to_read
=
2098 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2100 fseeko (*fp
, begin
, SEEK_SET
);
2101 while ((bytes_read
= fread (buffer
, 1,
2102 min (bytes_to_read
, sizeof buffer
),
2104 /* Look for CR followed by a LF. This is supposed to
2105 be text so there should be LF's. If not, don't
2106 modify the content. */
2109 int last_char_was_cr
= 0;
2111 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2113 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2114 if (*cp
== '\n' && last_char_was_cr
) {
2119 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2125 char *stripped_content_file
;
2126 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2128 if (tempfile
== NULL
) {
2129 adios (NULL
, "unable to create temporary file in %s",
2132 stripped_content_file
= add (tempfile
, NULL
);
2134 /* Strip each CR before a LF from the content. */
2135 fseeko (*fp
, begin
, SEEK_SET
);
2136 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2140 int last_char_was_cr
= 0;
2142 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2144 last_char_was_cr
= 1;
2145 } else if (last_char_was_cr
) {
2147 if (write (fd
, "\r", 1) < 0) {
2148 advise (tempfile
, "CR write");
2151 if (write (fd
, cp
, 1) < 0) {
2152 advise (tempfile
, "write");
2154 last_char_was_cr
= 0;
2156 if (write (fd
, cp
, 1) < 0) {
2157 advise (tempfile
, "write");
2159 last_char_was_cr
= 0;
2165 admonish (NULL
, "unable to write temporary file %s",
2166 stripped_content_file
);
2167 (void) m_unlink (stripped_content_file
);
2170 /* Replace the decoded file with the converted one. */
2171 if (ct
->c_cefile
.ce_file
) {
2172 if (ct
->c_cefile
.ce_unlink
) {
2173 (void) m_unlink (ct
->c_cefile
.ce_file
);
2175 free (ct
->c_cefile
.ce_file
);
2177 ct
->c_cefile
.ce_file
= stripped_content_file
;
2178 ct
->c_cefile
.ce_unlink
= 1;
2182 report (NULL
, ct
->c_partno
,
2183 begin
== 0 && end
== 0 ? "" : *file
,
2189 if (opened_input_file
) {
2203 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2206 switch (ct
->c_type
) {
2208 if (ct
->c_subtype
== TEXT_PLAIN
) {
2209 status
= convert_charset (ct
, dest_charset
, message_mods
);
2212 char *ct_charset
= content_charset (ct
);
2214 report (NULL
, ct
->c_partno
, ct
->c_file
,
2215 "convert %s to %s", ct_charset
, dest_charset
);
2219 char *ct_charset
= content_charset (ct
);
2221 report ("iconv", ct
->c_partno
, ct
->c_file
,
2222 "failed to convert %s to %s", ct_charset
, dest_charset
);
2228 case CT_MULTIPART
: {
2229 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2232 /* Should check to see if the body for this part is encoded?
2233 For now, it gets passed along as-is by InitMultiPart(). */
2234 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2236 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2242 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2243 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2246 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2259 * Fix various problems that aren't handled elsewhere. These
2260 * are fixed unconditionally: there are no switches to disable
2261 * them. (Currently, "problems" is just one: an extraneous
2262 * semicolon at the end of a header parameter list.)
2265 fix_always (CT ct
, int *message_mods
) {
2268 switch (ct
->c_type
) {
2269 case CT_MULTIPART
: {
2270 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2273 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2274 status
= fix_always (part
->mp_part
, message_mods
);
2280 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2281 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2283 status
= fix_always (e
->eb_content
, message_mods
);
2290 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2291 size_t len
= strlen (hf
->value
);
2293 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2294 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2295 /* Only do this for Content-Type and
2296 Content-Disposition fields because those are the
2297 only headers that parse_mime() warns about. */
2301 /* whitespace following a trailing ';' will be nuked as well */
2302 if (hf
->value
[len
- 1] == '\n') {
2303 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2304 if (len
-- == 0) { break; }
2308 if (hf
->value
[len
- 2] == ';') {
2309 /* Remove trailing ';' from parameter value. */
2310 hf
->value
[len
- 2] = '\n';
2311 hf
->value
[len
- 1] = '\0';
2313 /* Also, if Content-Type parameter, remove trailing ';'
2314 from ct->c_ctline. This probably isn't necessary
2316 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2317 size_t l
= strlen(ct
->c_ctline
) - 1;
2318 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2319 ct
->c_ctline
[l
] == ';') {
2320 ct
->c_ctline
[l
--] = '\0';
2321 if (l
== 0) { break; }
2327 report (NULL
, ct
->c_partno
, ct
->c_file
,
2328 "remove trailing ; from %s parameter value",
2340 write_content (CT ct
, const char *input_filename
, char *outfile
, int modify_inplace
,
2344 if (modify_inplace
) {
2345 if (message_mods
> 0) {
2346 if ((status
= output_message (ct
, outfile
)) == OK
) {
2347 char *infile
= input_filename
2348 ? add (input_filename
, NULL
)
2349 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
2351 if (remove_file (infile
) == OK
) {
2352 if (rename (outfile
, infile
)) {
2353 /* Rename didn't work, possibly because of an
2354 attempt to rename across filesystems. Try
2355 brute force copy. */
2356 int old
= open (outfile
, O_RDONLY
);
2358 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2361 if (old
!= -1 && new != -1) {
2362 char buffer
[BUFSIZ
];
2364 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2366 if (write (new, buffer
, i
) != i
) {
2372 if (new != -1) { close (new); }
2373 if (old
!= -1) { close (old
); }
2374 (void) m_unlink (outfile
);
2377 /* The -file argument processing used path() to
2378 expand filename to absolute path. */
2379 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2381 admonish (NULL
, "unable to rename %s %s to %s",
2382 file
? "file" : "message", outfile
,
2388 admonish (NULL
, "unable to remove input file %s, "
2389 "not modifying it", infile
);
2390 (void) m_unlink (outfile
);
2399 /* No modifications and didn't need the tmp outfile. */
2400 (void) m_unlink (outfile
);
2403 /* Output is going to some file. Produce it whether or not
2404 there were modifications. */
2405 status
= output_message (ct
, outfile
);
2414 * parse_mime() does not set lf_line_endings in struct text, so use this function to do it.
2415 * It touches the parts the decodetypes identifies.
2418 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
) {
2419 switch (ct
->c_type
) {
2420 case CT_MULTIPART
: {
2421 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2424 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2425 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2431 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2432 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2434 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2439 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2440 if (ct
->c_ctparams
== NULL
) {
2441 if ((ct
->c_ctparams
= (struct text
*) mh_xcalloc (1, sizeof (struct text
))) == NULL
) {
2442 adios (NULL
, "out of memory");
2445 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2452 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2453 * use the standard MH backup file.
2456 remove_file (const char *file
) {
2458 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2459 int status
= system (rmm_command
);
2462 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2464 /* This is OK for a non-message file, it still uses the
2465 BACKUP_PREFIX form. The backup file will be in the same
2466 directory as file. */
2467 return rename (file
, m_backup (file
));
2473 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2478 va_start (args
, message
);
2479 fmt
= concat (filename
, partno
? " part " : ", ",
2480 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
2482 advertise (what
, NULL
, fmt
, args
);
2495 fprintf (stderr
, "\n");