2 * mhfixmsg.c -- rewrite a message with various tranformations
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 get_multipart_boundary (CT
, char **);
95 static int replace_boundary (CT
, char *, char *);
96 static int fix_types (CT
, svector_t
, int *);
97 static char *replace_substring (char **, const char *, const char *);
98 static char *remove_parameter (char *, const char *);
99 static int fix_multipart_cte (CT
, int *);
100 static int set_ce (CT
, int);
101 static int ensure_text_plain (CT
*, CT
, int *, int);
102 static int find_textplain_sibling (CT
, int, int *);
103 static int insert_new_text_plain_part (CT
, int, CT
);
104 static CT
build_text_plain_part (CT
);
105 static int insert_into_new_mp_alt (CT
*, int *);
106 static CT
divide_part (CT
);
107 static void copy_ctinfo (CI
, CI
);
108 static int decode_part (CT
);
109 static int reformat_part (CT
, char *, char *, char *, int);
110 static int charset_encoding (CT
);
111 static CT
build_multipart_alt (CT
, CT
, int, int);
112 static int boundary_in_content (FILE **, char *, const char *);
113 static void transfer_noncontent_headers (CT
, CT
);
114 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
115 static int decode_text_parts (CT
, int, const char *, int *);
116 static int should_decode(const char *, const char *, const char *);
117 static int content_encoding (CT
, const char **);
118 static int strip_crs (CT
, int *);
119 static int convert_charsets (CT
, char *, int *);
120 static int fix_always (CT
, int *);
121 static int write_content (CT
, char *, char *, int, int);
122 static void set_text_ctparams(CT
, char *, int);
123 static int remove_file (char *);
124 static void report (char *, char *, char *, char *, ...);
125 static void pipeser (int);
129 main (int argc
, char **argv
) {
131 char *cp
, *file
= NULL
, *folder
= NULL
;
132 char *maildir
, buf
[100], *outfile
= NULL
;
133 char **argp
, **arguments
;
134 struct msgs_array msgs
= { 0, 0, NULL
};
135 struct msgs
*mp
= NULL
;
141 fix_transformations fx
;
142 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
144 fx
.replacetextplain
= 0;
145 fx
.decodetext
= CE_8BIT
;
146 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
147 fx
.lf_line_endings
= 0;
148 fx
.textcharset
= NULL
;
150 if (nmh_init(argv
[0], 1)) { return 1; }
154 arguments
= getarguments (invo_name
, argc
, argv
, 1);
160 while ((cp
= *argp
++)) {
162 switch (smatch (++cp
, switches
)) {
164 ambigsw (cp
, switches
);
167 adios (NULL
, "-%s unknown", cp
);
170 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
172 print_help (buf
, switches
, 1);
175 print_version(invo_name
);
179 if (! (cp
= *argp
++) || *cp
== '-')
180 adios (NULL
, "missing argument to %s", argp
[-2]);
181 if (! strcasecmp (cp
, "8bit")) {
182 fx
.decodetext
= CE_8BIT
;
183 } else if (! strcasecmp (cp
, "7bit")) {
184 fx
.decodetext
= CE_7BIT
;
186 adios (NULL
, "invalid argument to %s", argp
[-2]);
193 if (! (cp
= *argp
++) || *cp
== '-')
194 adios (NULL
, "missing argument to %s", argp
[-2]);
197 case CRLFLINEBREAKSSW
:
198 fx
.lf_line_endings
= 0;
200 case NCRLFLINEBREAKSSW
:
201 fx
.lf_line_endings
= 1;
204 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
205 adios (NULL
, "missing argument to %s", argp
[-2]);
224 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
225 adios (NULL
, "missing argument to %s", argp
[-2]);
226 if (! strncasecmp (cp
, "multipart/", 10) ||
227 ! strncasecmp (cp
, "message/", 8)) {
228 adios (NULL
, "-fixtype %s not allowed", cp
);
229 } else if (! strchr (cp
, '/')) {
230 adios (NULL
, "-fixtype requires type/subtype");
232 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
233 svector_push_back (fx
.fixtypes
, cp
);
241 case REPLACETEXTPLAINSW
:
242 fx
.replacetextplain
= 1;
244 case NREPLACETEXTPLAINSW
:
245 fx
.replacetextplain
= 0;
248 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
249 adios (NULL
, "missing argument to %s", argp
[-2]);
250 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
253 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
254 adios (NULL
, "missing argument to %s", argp
[-2]);
255 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
258 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
259 adios (NULL
, "missing argument to %s", argp
[-2]);
278 if (*cp
== '+' || *cp
== '@') {
280 adios (NULL
, "only one folder at a time!");
282 folder
= pluspath (cp
);
285 /* Interpret a full path as a filename, not a message. */
286 file
= add (cp
, NULL
);
288 app_msgarg (&msgs
, cp
);
293 SIGNAL (SIGQUIT
, quitser
);
294 SIGNAL (SIGPIPE
, pipeser
);
297 * Read the standard profile setup
299 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
300 readconfig ((struct node
**) 0, fp
, cp
, 0);
304 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
305 suppress_extraneous_trailing_semicolon_warning
= 1;
307 if (! context_find ("path"))
308 free (path ("./", TFOLDER
));
310 if (file
&& msgs
.size
)
311 adios (NULL
, "cannot specify msg and file at same time!");
314 * check if message is coming from file
317 /* If file is stdin, create a tmp file name before parse_mime()
318 has a chance, because it might put in on a different
319 filesystem than the output file. Instead, put it in the
320 user's preferred tmp directory. */
323 if (! strcmp ("-", file
)) {
329 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
330 adios (NULL
, "unable to create temporary file in %s",
334 file
= add (cp
, NULL
);
335 cpydata (STDIN_FILENO
, fd
, "-", file
);
339 (void) m_unlink (file
);
340 adios (NULL
, "failed to write temporary file");
344 if (! (cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof *cts
))) {
345 adios (NULL
, "out of memory");
349 if ((ct
= parse_mime (file
))) {
350 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
355 * message(s) are coming from a folder
360 app_msgarg(&msgs
, "cur");
362 folder
= getfolder (1);
363 maildir
= m_maildir (folder
);
365 if (chdir (maildir
) == NOTOK
)
366 adios (maildir
, "unable to change directory to");
368 /* read folder and create message structure */
369 if (! (mp
= folder_read (folder
, 1)))
370 adios (NULL
, "unable to read folder %s", folder
);
372 /* check for empty folder */
374 adios (NULL
, "no messages in %s", folder
);
376 /* parse all the message ranges/sequences and set SELECTED */
377 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
378 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
380 seq_setprev (mp
); /* set the previous-sequence */
383 (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
))) {
384 adios (NULL
, "out of memory");
388 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
389 if (is_selected(mp
, msgnum
)) {
392 msgnam
= m_name (msgnum
);
393 if ((ct
= parse_mime (msgnam
))) {
394 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
401 seq_setcur (mp
, mp
->hghsel
); /* update current message */
403 seq_save (mp
); /* synchronize sequences */
404 context_replace (pfolder
, folder
);/* update current folder */
405 context_save (); /* save the context file */
409 for (ctp
= cts
; *ctp
; ++ctp
) {
410 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
413 (void) m_unlink (file
);
416 /* Just calling m_backup() unlinks the backup file. */
417 (void) m_backup (file
);
425 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
429 /* done is freects_done, which will clean up all of cts. */
436 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
437 /* Store input filename in case one of the transformations, i.e.,
438 fix_boundary(), rewrites to a tmp file. */
439 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
440 int modify_inplace
= 0;
441 int message_mods
= 0;
444 if (outfile
== NULL
) {
447 if ((*ctp
)->c_file
) {
449 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
450 adios (NULL
, "unable to create temporary file in %s",
453 outfile
= add (tempfile
, NULL
);
455 adios (NULL
, "missing both input and output filenames\n");
459 reverse_alternative_parts (*ctp
);
460 status
= fix_always (*ctp
, &message_mods
);
461 if (status
== OK
&& fx
->fixboundary
) {
462 status
= fix_boundary (ctp
, &message_mods
);
464 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
465 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
467 if (status
== OK
&& fx
->fixcte
) {
468 status
= fix_multipart_cte (*ctp
, &message_mods
);
470 if (status
== OK
&& fx
->reformat
) {
472 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
474 if (status
== OK
&& fx
->decodetext
) {
475 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
, &message_mods
);
477 if (status
== OK
&& fx
->textcharset
!= NULL
) {
478 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
481 if (status
== OK
&& ! (*ctp
)->c_umask
) {
482 /* Set the umask for the contents file. This currently
483 isn't used but just in case it is in the future. */
486 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
487 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
489 (*ctp
)->c_umask
= ~m_gmprot();
494 * Write the content to a file
497 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
499 } else if (! modify_inplace
) {
500 /* Something went wrong. Output might be expected, such
501 as if this were run as a filter. Just copy the input
503 int in
= open (input_filename
, O_RDONLY
);
504 int out
= strcmp (outfile
, "-")
505 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
508 if (in
!= -1 && out
!= -1) {
509 cpydata (in
, out
, input_filename
, outfile
);
518 if (modify_inplace
) {
519 if (status
!= OK
) { (void) m_unlink (outfile
); }
524 free (input_filename
);
531 fix_boundary (CT
*ct
, int *message_mods
) {
532 struct multipart
*mp
;
535 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
536 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
539 * 1) Get boundary at end of part.
540 * 2) Get boundary at beginning of part and compare to the end-of-part
542 * 3) Write out contents of ct to tmp file, replacing boundary in
543 * header with boundary from part. Set c_unlink to 1.
545 * 5) Call parse_mime() on the tmp file, replacing ct.
548 if (mp
&& mp
->mp_start
) {
551 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
554 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
555 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
556 char *filename
= add ((*ct
)->c_file
, NULL
);
560 if ((fixed_ct
= parse_mime (fixed
))) {
566 report (NULL
, NULL
, filename
,
567 "fix multipart boundary");
571 advise (NULL
, "unable to parse fixed part");
576 advise (NULL
, "unable to replace broken boundary");
580 advise (NULL
, "unable to create temporary file in %s",
585 free (part_boundary
);
587 /* Couldn't fix the boundary. Report failure so that mhfixmsg
588 doesn't modify the message. */
592 /* No multipart struct, even though the content type is
593 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
604 get_multipart_boundary (CT ct
, char **part_boundary
) {
606 char *end_boundary
= NULL
;
607 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
608 ? (off_t
) (ct
->c_end
- sizeof buffer
)
609 : (off_t
) ct
->c_begin
;
613 /* This will fail if the boundary spans fread() calls. BUFSIZ should
614 be big enough, even if it's just 1024, to make that unlikely. */
616 /* free_content() will close ct->c_fp. */
617 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
618 advise (ct
->c_file
, "unable to open for reading");
622 /* Get boundary at end of multipart. */
623 while (begin
>= (off_t
) ct
->c_begin
) {
624 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
625 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
626 char *cp
= rfind_str (buffer
, bytes_read
, "--");
631 /* Trim off trailing "--" and anything beyond. */
633 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
634 if (strlen (end
) > 3 && *end
++ == '\n' &&
635 *end
++ == '-' && *end
++ == '-') {
636 end_boundary
= add (end
, NULL
);
643 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
644 begin
-= sizeof buffer
;
650 /* Get boundary at beginning of multipart. */
652 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
653 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
654 if (bytes_read
>= strlen (end_boundary
)) {
655 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
657 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
658 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
663 /* The start and end boundaries didn't match, or the
664 start boundary doesn't begin with "\n--" (or "--"
665 if at the beginning of buffer). Keep trying. */
674 *part_boundary
= end_boundary
;
676 *part_boundary
= NULL
;
684 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
686 replace_boundary (CT ct
, char *file
, char *boundary
) {
689 char buf
[BUFSIZ
], name
[NAMESZ
];
691 m_getfld_state_t gstate
= 0;
694 if (ct
->c_file
== NULL
) {
695 advise (NULL
, "missing input filename");
699 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
700 advise (ct
->c_file
, "unable to open for reading");
704 if ((fpout
= fopen (file
, "w")) == NULL
) {
706 advise (file
, "unable to open for writing");
710 for (compnum
= 1;;) {
711 int bufsz
= (int) sizeof buf
;
713 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
718 /* get copies of the buffers */
719 np
= add (name
, NULL
);
720 vp
= add (buf
, NULL
);
722 /* if necessary, get rest of field */
723 while (state
== FLDPLUS
) {
725 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
726 vp
= add (buf
, vp
); /* add to previous value */
729 if (strcasecmp (TYPE_FIELD
, np
)) {
730 fprintf (fpout
, "%s:%s", np
, vp
);
732 char *new_ctline
, *new_params
;
734 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
735 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
738 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
739 ct
->c_ctinfo
.ci_subtype
, NULL
);
740 new_params
= output_params(strlen(TYPE_FIELD
) +
741 strlen(new_ctline
) + 1,
742 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
743 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
744 new_params
? new_params
: "");
757 /* buf will have a terminating NULL, skip it. */
758 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
759 advise (file
, "fwrite");
768 advise (NULL
, "message format error in component #%d", compnum
);
773 advise (NULL
, "getfld() returned %d", state
);
781 m_getfld_state_destroy (&gstate
);
790 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
793 switch (ct
->c_type
) {
795 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
798 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
799 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
805 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
806 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
808 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
815 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
816 for (typep
= svector_strs (fixtypes
);
817 typep
&& (type
= *typep
);
820 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
823 if (! strcasecmp (type
, type_subtype
) &&
824 decode_part (ct
) == OK
&&
825 ct
->c_cefile
.ce_file
!= NULL
) {
826 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
829 if ((cp
= strchr (ct_type_subtype
, ';'))) {
830 /* Truncate to remove any parameter list from
831 mime_type () result. */
835 if (strcasecmp (type
, ct_type_subtype
)) {
836 char *ct_type
, *ct_subtype
;
839 /* The Content-Type header does not match the
840 content, so update these struct Content
843 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
846 /* Extract type and subtype from type/subtype. */
847 ct_type
= getcpy (ct_type_subtype
);
848 if ((cp
= strchr (ct_type
, '/'))) {
850 ct_subtype
= getcpy (++cp
);
852 advise (NULL
, "missing / in MIME type of %s %s",
853 ct
->c_file
, ct
->c_partno
);
858 ct
->c_type
= ct_str_type (ct_type
);
859 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
861 free (ct
->c_ctinfo
.ci_type
);
862 ct
->c_ctinfo
.ci_type
= ct_type
;
863 free (ct
->c_ctinfo
.ci_subtype
);
864 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
865 if (! replace_substring (&ct
->c_ctline
, type
,
867 advise (NULL
, "did not find %s in %s",
871 /* Update Content-Type header field. */
872 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
873 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
874 if (replace_substring (&hf
->value
, type
,
878 report (NULL
, ct
->c_partno
, ct
->c_file
,
879 "change Content-Type in header "
881 type
, ct_type_subtype
);
885 advise (NULL
, "did not find %s in %s",
891 free (ct_type_subtype
);
902 replace_substring (char **str
, const char *old
, const char *new) {
905 if ((cp
= strstr (*str
, old
))) {
906 char *remainder
= cp
+ strlen (old
);
907 char *prefix
, *new_str
;
910 prefix
= getcpy (*str
);
911 *(prefix
+ (cp
- *str
)) = '\0';
912 new_str
= concat (prefix
, new, remainder
, NULL
);
915 new_str
= concat (new, remainder
, NULL
);
920 return *str
= new_str
;
927 * Remove a name=value parameter, given just its name, from a header value.
930 remove_parameter (char *str
, const char *name
) {
931 /* It looks to me, based on the BNF in RFC 2045, than there can't
932 be whitespace betwwen the parameter name and the "=", or
933 between the "=" and the parameter value. */
934 char *param_name
= concat (name
, "=", NULL
);
937 if ((cp
= strstr (str
, param_name
))) {
941 /* Remove any leading spaces, before the parameter name. */
943 start
> str
&& isspace ((unsigned char) *(start
-1));
947 /* Remove a leading semicolon. */
948 if (start
> str
&& *(start
-1) == ';') { --start
; }
950 end
= cp
+ strlen (name
) + 1;
952 /* Skip past the quoted value, and then the final quote. */
953 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
956 /* Skip past the value. */
957 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
960 /* Count how many characters need to be moved. Include
961 trailing null, which is accounted for by the
962 initialization of count to 1. */
963 for (cp
= end
; *cp
; ++cp
) { ++count
; }
964 (void) memmove (start
, end
, count
);
973 fix_multipart_cte (CT ct
, int *message_mods
) {
976 if (ct
->c_type
== CT_MULTIPART
) {
980 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
981 ct
->c_encoding
!= CE_BINARY
) {
984 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
985 char *name
= hf
->name
;
986 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
990 if (! strncasecmp (name
, ENCODING_FIELD
,
991 strlen (ENCODING_FIELD
))) {
992 char *prefix
= "Nmh-REPLACED-INVALID-";
993 HF h
= mh_xmalloc (sizeof *h
);
995 h
->name
= add (hf
->name
, NULL
);
996 h
->hf_encoding
= hf
->hf_encoding
;
1000 /* Retain old header but prefix its name. */
1002 hf
->name
= concat (prefix
, h
->name
, NULL
);
1006 char *encoding
= cpytrim (hf
->value
);
1007 report (NULL
, ct
->c_partno
, ct
->c_file
,
1008 "replace Content-Transfer-Encoding of %s "
1009 "with 8 bit", encoding
);
1013 h
->value
= add (" 8bit\n", NULL
);
1015 /* Don't need to warn for multiple C-T-E header
1016 fields, parse_mime() already does that. But
1017 if there are any, fix them all as necessary. */
1022 set_ce (ct
, CE_8BIT
);
1025 m
= (struct multipart
*) ct
->c_ctparams
;
1026 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1027 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
1039 set_ce (CT ct
, int encoding
) {
1040 const char *ce
= ce_str (encoding
);
1041 const struct str2init
*ctinit
= get_ce_method (ce
);
1044 char *cte
= concat (" ", ce
, "\n", NULL
);
1047 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1048 caller is decode_text_parts (). Save because we'll
1050 struct cefile decoded_content_info
= ct
->c_cefile
;
1052 ct
->c_encoding
= encoding
;
1054 ct
->c_ctinitfnx
= ctinit
->si_init
;
1055 /* This will assign ct->c_cefile with an all-0 struct, which
1057 (*ctinit
->si_init
) (ct
);
1058 /* After returning, the caller should set
1059 ct->c_cefile.ce_file to the name of the file containing
1062 /* Restore the cefile. */
1063 ct
->c_cefile
= decoded_content_info
;
1065 /* Update/add Content-Transfer-Encoding header field. */
1066 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1067 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1074 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
1077 /* Update c_celine. It's used only by mhlist -debug. */
1078 free (ct
->c_celine
);
1079 ct
->c_celine
= add (cte
, NULL
);
1088 /* Make sure each text part has a corresponding text/plain part. */
1090 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1093 switch ((*ct
)->c_type
) {
1095 /* Nothing to do for text/plain. */
1096 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1098 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1099 parent
->c_subtype
== MULTI_ALTERNATE
) {
1100 int new_subpart_number
= 1;
1101 int has_text_plain
=
1102 find_textplain_sibling (parent
, replacetextplain
,
1103 &new_subpart_number
);
1105 if (! has_text_plain
) {
1106 /* Parent is a multipart/alternative. Insert a new
1107 text/plain subpart. */
1108 const int inserted
=
1109 insert_new_text_plain_part (*ct
, new_subpart_number
,
1114 report (NULL
, parent
->c_partno
, parent
->c_file
,
1115 "insert text/plain part");
1121 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1122 parent
->c_subtype
== MULTI_RELATED
) {
1123 char *type_subtype
=
1124 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1125 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1126 const char *parent_type
=
1127 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1128 int new_subpart_number
= 1;
1129 int has_text_plain
= 0;
1131 /* Have to do string comparison on the subtype because we
1132 don't enumerate all of them in c_subtype values.
1133 parent_type will be NULL if the multipart/related part
1134 doesn't have a type parameter. The type parameter must
1135 be specified according to RFC 2387 Sec. 3.1 but not all
1137 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1138 /* The type of this part matches the root type of the
1139 parent multipart/related. Look to see if there's
1140 text/plain sibling. */
1142 find_textplain_sibling (parent
, replacetextplain
,
1143 &new_subpart_number
);
1146 free (type_subtype
);
1148 if (! has_text_plain
) {
1149 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1153 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1154 if (*ct
!= part
->mp_part
) {
1160 /* Parent is a multipart/related. Insert a new
1161 text/plain subpart in a new multipart/alternative. */
1162 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1163 /* Not an error if text/plain couldn't be added. */
1166 /* There are no siblings, so insert a new text/plain
1167 subpart, and change the parent type from
1168 multipart/related to multipart/alternative. */
1169 const int inserted
=
1170 insert_new_text_plain_part (*ct
, new_subpart_number
,
1176 parent
->c_subtype
= MULTI_ALTERNATE
;
1177 parent
->c_ctinfo
.ci_subtype
= getcpy ("alternative");
1178 if (! replace_substring (&parent
->c_ctline
, "/related",
1181 "did not find multipart/related in %s",
1185 /* Update Content-Type header field. */
1186 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1187 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1188 if (replace_substring (&hf
->value
, "/related",
1192 report (NULL
, parent
->c_partno
,
1194 "insert text/plain part");
1197 /* Remove, e.g., type="text/html" from
1198 multipart/alternative. */
1199 remove_parameter (hf
->value
, "type");
1202 advise (NULL
, "did not find multipart/"
1203 "related in header %s",
1209 /* Not an error if text/plain couldn't be inserted. */
1214 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1221 case CT_MULTIPART
: {
1222 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1225 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1226 if ((*ct
)->c_type
== CT_MULTIPART
) {
1227 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1235 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1236 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1238 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1248 /* See if there is a sibling text/plain. */
1250 find_textplain_sibling (CT parent
, int replacetextplain
,
1251 int *new_subpart_number
) {
1252 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1253 struct part
*part
, *prev
;
1254 int has_text_plain
= 0;
1256 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1257 ++*new_subpart_number
;
1258 if (part
->mp_part
->c_type
== CT_TEXT
&&
1259 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1260 if (replacetextplain
) {
1261 struct part
*old_part
;
1262 if (part
== mp
->mp_parts
) {
1263 old_part
= mp
->mp_parts
;
1264 mp
->mp_parts
= part
->mp_next
;
1266 old_part
= prev
->mp_next
;
1267 prev
->mp_next
= part
->mp_next
;
1270 report (NULL
, parent
->c_partno
, parent
->c_file
,
1271 "remove text/plain part %s",
1272 old_part
->mp_part
->c_partno
);
1274 free_content (old_part
->mp_part
);
1284 return has_text_plain
;
1289 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1290 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1291 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
1293 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1295 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1297 new_part
->mp_next
= mp
->mp_parts
;
1298 mp
->mp_parts
= new_part
;
1299 new_part
->mp_part
->c_partno
=
1300 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1305 free_content (new_part
->mp_part
);
1314 build_text_plain_part (CT encoded_part
) {
1315 CT tp_part
= divide_part (encoded_part
);
1316 char *tmp_plain_file
= NULL
;
1318 if (decode_part (tp_part
) == OK
) {
1319 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1320 contains the decoded contents. And the decoding function, such
1321 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1322 be unlinked by free_content (). */
1325 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1326 advise (NULL
, "unable to create temporary file in %s",
1329 tmp_plain_file
= add (tempfile
, NULL
);
1330 if (reformat_part (tp_part
, tmp_plain_file
,
1331 tp_part
->c_ctinfo
.ci_type
,
1332 tp_part
->c_ctinfo
.ci_subtype
,
1333 tp_part
->c_type
) == OK
) {
1339 free_content (tp_part
);
1340 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1341 free (tmp_plain_file
);
1347 /* Slip new text/plain part into a new multipart/alternative. */
1349 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1350 CT tp_part
= build_text_plain_part (*ct
);
1354 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1357 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1359 if (mp
&& mp
->mp_parts
) {
1360 mp
->mp_parts
->mp_part
= tp_part
;
1361 /* Make the new multipart/alternative the parent. */
1366 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1367 "insert text/plain part");
1370 free_content (tp_part
);
1371 free_content (mp_alt
);
1378 /* Not an error if text/plain couldn't be built. */
1385 divide_part (CT ct
) {
1388 if ((new_part
= (CT
) mh_xcalloc (1, sizeof *new_part
)) == NULL
)
1389 adios (NULL
, "out of memory");
1391 /* Just copy over what is needed for decoding. c_vrsn and
1392 c_celine aren't necessary. */
1393 new_part
->c_file
= add (ct
->c_file
, NULL
);
1394 new_part
->c_begin
= ct
->c_begin
;
1395 new_part
->c_end
= ct
->c_end
;
1396 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1397 new_part
->c_type
= ct
->c_type
;
1398 new_part
->c_cefile
= ct
->c_cefile
;
1399 new_part
->c_encoding
= ct
->c_encoding
;
1400 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1401 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1402 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1403 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1405 /* c_ctline is used by reformat__part(), so it can preserve
1406 anything after the type/subtype. */
1407 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1414 copy_ctinfo (CI dest
, CI src
) {
1417 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1418 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1420 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1421 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1423 if (s_pm
->pm_charset
)
1424 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1426 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1429 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1430 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1435 decode_part (CT ct
) {
1440 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1441 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1443 tmp_decoded
= add (tempfile
, NULL
);
1444 /* The following call will load ct->c_cefile.ce_file with the tmp
1445 filename of the decoded content. tmp_decoded will contain the
1446 encoded output, get rid of that. */
1447 status
= output_message (ct
, tmp_decoded
);
1448 (void) m_unlink (tmp_decoded
);
1455 /* Some of the arguments aren't really needed now, but maybe will
1456 be in the future for other than text types. */
1458 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1459 int output_subtype
, output_encoding
;
1463 /* Hacky: this redirects the output from whatever command is used
1464 to show the part to a file. So, the user can't have any output
1465 redirection in that command.
1466 Could show_multi() in mhshowsbr.c avoid this? */
1468 /* Check for invo_name-format-type/subtype. */
1469 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1471 advise (NULL
, "Don't know how to convert %s, there is no "
1472 "%s-format-%s/%s profile entry",
1473 ct
->c_file
, invo_name
, type
, subtype
);
1477 if (strchr (cf
, '>')) {
1478 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1479 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1480 subtype
? subtype
: "");
1486 cp
= concat (cf
, " >", file
, NULL
);
1487 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1490 /* Unlink decoded content tmp file and free its filename to avoid
1491 leaks. The file stream should already have been closed. */
1492 if (ct
->c_cefile
.ce_unlink
) {
1493 (void) m_unlink (ct
->c_cefile
.ce_file
);
1494 free (ct
->c_cefile
.ce_file
);
1495 ct
->c_cefile
.ce_file
= NULL
;
1496 ct
->c_cefile
.ce_unlink
= 0;
1499 if (c_type
== CT_TEXT
) {
1500 output_subtype
= TEXT_PLAIN
;
1502 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1505 output_encoding
= charset_encoding (ct
);
1507 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1508 ct
->c_cefile
.ce_file
= file
;
1509 ct
->c_cefile
.ce_unlink
= 1;
1511 ct
->c_cefile
.ce_unlink
= 0;
1519 /* Identifies 7bit or 8bit content based on charset. */
1521 charset_encoding (CT ct
) {
1522 char *ct_charset
= content_charset (ct
);
1523 int encoding
= strcasecmp (ct_charset
, "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1532 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1533 char *boundary_prefix
= "----=_nmh-multipart";
1534 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1535 char *boundary_indicator
= "; boundary=";
1536 char *typename
, *subtypename
, *name
;
1539 struct multipart
*m
;
1540 const struct str2init
*ctinit
;
1542 if ((ct
= (CT
) mh_xcalloc (1, sizeof *ct
)) == NULL
)
1543 adios (NULL
, "out of memory");
1545 /* Set up the multipart/alternative part. These fields of *ct were
1546 initialized to 0 by mh_xcalloc():
1547 c_fp, c_unlink, c_begin, c_end,
1548 c_vrsn, c_ctline, c_celine,
1549 c_id, c_descr, c_dispo, c_partno,
1550 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1551 c_cefile, c_encoding,
1552 c_digested, c_digest[16], c_ctexbody,
1553 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1555 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1558 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1560 ct
->c_subtype
= subtype
;
1562 ctinit
= get_ct_init (ct
->c_type
);
1564 typename
= ct_type_str (type
);
1565 subtypename
= ct_subtype_str (type
, subtype
);
1569 int found_boundary
= 1;
1571 while (found_boundary
&& serial
< 1000000) {
1574 /* Ensure that the boundary doesn't appear in the decoded
1576 if (new_part
->c_cefile
.ce_file
) {
1577 if ((found_boundary
=
1578 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1579 new_part
->c_cefile
.ce_file
,
1586 /* Ensure that the boundary doesn't appear in the encoded
1588 if (! found_boundary
&& new_part
->c_file
) {
1589 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1597 if (found_boundary
) {
1598 /* Try a slightly different boundary. */
1603 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1605 concat (boundary_prefix
,
1606 first_alt
->c_partno
? first_alt
->c_partno
: "",
1607 "-", buffer2
, NULL
);
1611 if (found_boundary
) {
1612 advise (NULL
, "giving up trying to find a unique boundary");
1618 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1619 boundary
, "\"", NULL
);
1621 /* Load c_first_hf and c_last_hf. */
1622 transfer_noncontent_headers (first_alt
, ct
);
1623 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1626 /* Load c_partno. */
1627 if (first_alt
->c_partno
) {
1628 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1629 free (first_alt
->c_partno
);
1630 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1631 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1633 first_alt
->c_partno
= add ("1", NULL
);
1634 new_part
->c_partno
= add ("2", NULL
);
1638 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1639 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1642 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1643 "boundary", boundary
, 0);
1645 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1646 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1647 p
->mp_next
->mp_next
= NULL
;
1648 p
->mp_next
->mp_part
= first_alt
;
1650 if ((m
= (struct multipart
*) mh_xcalloc (1, sizeof (struct multipart
))) ==
1652 adios (NULL
, "out of memory");
1653 m
->mp_start
= concat (boundary
, "\n", NULL
);
1654 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1664 /* Check that the boundary does not appear in the content. */
1666 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1667 char buffer
[BUFSIZ
];
1669 int found_boundary
= 0;
1671 /* free_content() will close *fp if we fopen it here. */
1672 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1673 advise (file
, "unable to open %s for reading", file
);
1677 fseeko (*fp
, 0L, SEEK_SET
);
1678 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1679 if (find_str (buffer
, bytes_read
, boundary
)) {
1685 return found_boundary
;
1689 /* Remove all non-Content headers. */
1691 transfer_noncontent_headers (CT old
, CT
new) {
1694 hp_prev
= hp
= old
->c_first_hf
;
1698 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1699 if (hp
== old
->c_last_hf
) {
1700 if (hp
== old
->c_first_hf
) {
1701 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1703 hp_prev
->next
= NULL
;
1704 old
->c_last_hf
= hp_prev
;
1707 if (hp
== old
->c_first_hf
) {
1708 old
->c_first_hf
= next
;
1710 hp_prev
->next
= next
;
1714 /* Put node hp in the new CT. */
1715 if (new->c_first_hf
== NULL
) {
1716 new->c_first_hf
= hp
;
1718 new->c_last_hf
->next
= hp
;
1720 new->c_last_hf
= hp
;
1722 /* A Content- header, leave in old. */
1732 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1733 char *typename
= ct_type_str (type
);
1734 char *subtypename
= ct_subtype_str (type
, subtype
);
1735 /* E.g, " text/plain" */
1736 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1737 /* E.g, " text/plain\n" */
1738 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1739 int found_content_type
= 0;
1741 const char *cp
= NULL
;
1745 /* Update/add Content-Type header field. */
1746 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1747 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1748 found_content_type
= 1;
1750 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1751 ? concat (type_subtypename
, cp
, "\n", NULL
)
1752 : add (name_plus_nl
, NULL
);
1755 if (! found_content_type
) {
1756 add_header (ct
, add (TYPE_FIELD
, NULL
),
1757 (cp
= strchr (ct
->c_ctline
, ';'))
1758 ? concat (type_subtypename
, cp
, "\n", NULL
)
1759 : add (name_plus_nl
, NULL
));
1762 /* Some of these might not be used, but set them anyway. */
1764 ? concat (type_subtypename
, cp
, NULL
)
1765 : concat (type_subtypename
, NULL
);
1766 free (ct
->c_ctline
);
1767 ct
->c_ctline
= ctline
;
1768 /* Leave other ctinfo members as they were. */
1769 free (ct
->c_ctinfo
.ci_type
);
1770 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1771 free (ct
->c_ctinfo
.ci_subtype
);
1772 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1774 ct
->c_subtype
= subtype
;
1776 free (name_plus_nl
);
1777 free (type_subtypename
);
1779 status
= set_ce (ct
, encoding
);
1786 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
, int *message_mods
) {
1788 int lf_line_endings
= 0;
1790 switch (ct
->c_type
) {
1791 case CT_MULTIPART
: {
1792 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1795 /* Should check to see if the body for this part is encoded?
1796 For now, it gets passed along as-is by InitMultiPart(). */
1797 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1798 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
, message_mods
);
1804 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1805 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1807 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
, message_mods
);
1812 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
1817 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
1819 switch (ct
->c_encoding
) {
1824 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1825 const char *reason
= NULL
;
1827 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1828 && encoding
!= CE_BINARY
) {
1829 /* The decoding isn't acceptable so discard it.
1830 Leave status as OK to allow other transformations. */
1832 report (NULL
, ct
->c_partno
, ct
->c_file
,
1833 "will not decode%s because it is binary (%s)",
1835 : ct
->c_ctline
? ct
->c_ctline
1839 (void) m_unlink (ct
->c_cefile
.ce_file
);
1840 free (ct
->c_cefile
.ce_file
);
1841 ct
->c_cefile
.ce_file
= NULL
;
1842 } else if (ct
->c_encoding
== CE_QUOTED
&&
1843 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1844 /* The decoding isn't acceptable so discard it.
1845 Leave status as OK to allow other transformations. */
1847 report (NULL
, ct
->c_partno
, ct
->c_file
,
1848 "will not decode%s because it is 8bit",
1850 : ct
->c_ctline
? ct
->c_ctline
1853 (void) m_unlink (ct
->c_cefile
.ce_file
);
1854 free (ct
->c_cefile
.ce_file
);
1855 ct
->c_cefile
.ce_file
= NULL
;
1858 if (ct_encoding
== CE_BINARY
)
1860 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1863 enc
= charset_encoding (ct
);
1864 if (set_ce (ct
, enc
) == OK
) {
1867 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
1868 ct
->c_ctline
? ct
->c_ctline
: "");
1870 if (lf_line_endings
) {
1871 strip_crs (ct
, message_mods
);
1884 if (lf_line_endings
) {
1885 strip_crs (ct
, message_mods
);
1899 /* Determine if the part with type[/subtype] should be decoded, according to
1900 decodetypes (which came from the -decodetypes switch). */
1902 should_decode(const char *decodetypes
, const char *type
, const char *subtype
) {
1903 /* Quick search for matching type[/subtype] in decodetypes: bracket
1904 decodetypes with commas, then search for ,type, and ,type/subtype, in
1907 int found_match
= 0;
1908 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
1909 char *delimited_type
= concat(",", type
, ",", NULL
);
1911 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
1913 } else if (subtype
!= NULL
) {
1914 char *delimited_type_subtype
=
1915 concat(",", type
, "/", subtype
, ",", NULL
);
1917 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
1920 free(delimited_type_subtype
);
1923 free(delimited_type
);
1924 free(delimited_decodetypes
);
1930 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1931 if it has any NUL characters, a CR not followed by a LF, or lines
1932 greater than 998 characters in length. If binary, reason is set
1933 to a string explaining why. */
1935 content_encoding (CT ct
, const char **reason
) {
1936 CE ce
= &ct
->c_cefile
;
1937 int encoding
= CE_7BIT
;
1940 size_t line_len
= 0;
1941 char buffer
[BUFSIZ
];
1944 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1945 advise (ce
->ce_file
, "unable to open for reading");
1949 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1950 while (encoding
!= CE_BINARY
&&
1951 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1954 int last_char_was_cr
= 0;
1956 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1957 if (*cp
== '\0' || ++line_len
> 998 ||
1958 (*cp
!= '\n' && last_char_was_cr
)) {
1959 encoding
= CE_BINARY
;
1961 *reason
= "null character";
1962 } else if (line_len
> 998) {
1963 *reason
= "line length > 998";
1964 } else if (*cp
!= '\n' && last_char_was_cr
) {
1965 *reason
= "CR not followed by LF";
1967 /* Should not reach this. */
1971 } else if (*cp
== '\n') {
1973 } else if (! isascii ((unsigned char) *cp
)) {
1977 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1983 } /* else should never happen */
1990 strip_crs (CT ct
, int *message_mods
) {
1991 char *charset
= content_charset (ct
);
1994 /* Only strip carriage returns if content is ASCII or another
1995 charset that has the same readily recognizable CR followed by a
1996 LF. We can include UTF-8 here because if the high-order bit of
1997 a UTF-8 byte is 0, then it must be a single-byte ASCII
1999 if (! strcasecmp (charset
, "US-ASCII") ||
2000 ! strcasecmp (charset
, "UTF-8") ||
2001 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2002 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2008 int opened_input_file
= 0;
2010 if (ct
->c_cefile
.ce_file
) {
2011 file
= &ct
->c_cefile
.ce_file
;
2012 fp
= &ct
->c_cefile
.ce_fp
;
2014 } else if (ct
->c_file
) {
2017 begin
= (size_t) ct
->c_begin
;
2018 end
= (size_t) ct
->c_end
;
2019 } /* else don't know where the content is */
2021 if (file
&& *file
&& fp
) {
2023 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2024 advise (*file
, "unable to open for reading");
2027 opened_input_file
= 1;
2033 char buffer
[BUFSIZ
];
2035 size_t bytes_to_read
=
2036 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2038 fseeko (*fp
, begin
, SEEK_SET
);
2039 while ((bytes_read
= fread (buffer
, 1,
2040 min (bytes_to_read
, sizeof buffer
),
2042 /* Look for CR followed by a LF. This is supposed to
2043 be text so there should be LF's. If not, don't
2044 modify the content. */
2047 int last_char_was_cr
= 0;
2049 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2051 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2052 if (*cp
== '\n' && last_char_was_cr
) {
2057 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2063 char *stripped_content_file
;
2064 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2066 if (tempfile
== NULL
) {
2067 adios (NULL
, "unable to create temporary file in %s",
2070 stripped_content_file
= add (tempfile
, NULL
);
2072 /* Strip each CR before a LF from the content. */
2073 fseeko (*fp
, begin
, SEEK_SET
);
2074 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2078 int last_char_was_cr
= 0;
2080 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2082 last_char_was_cr
= 1;
2083 } else if (last_char_was_cr
) {
2085 if (write (fd
, "\r", 1) < 0) {
2086 advise (tempfile
, "CR write");
2089 if (write (fd
, cp
, 1) < 0) {
2090 advise (tempfile
, "write");
2092 last_char_was_cr
= 0;
2094 if (write (fd
, cp
, 1) < 0) {
2095 advise (tempfile
, "write");
2097 last_char_was_cr
= 0;
2103 admonish (NULL
, "unable to write temporary file %s",
2104 stripped_content_file
);
2105 (void) m_unlink (stripped_content_file
);
2108 /* Replace the decoded file with the converted one. */
2109 if (ct
->c_cefile
.ce_file
) {
2110 if (ct
->c_cefile
.ce_unlink
) {
2111 (void) m_unlink (ct
->c_cefile
.ce_file
);
2113 free (ct
->c_cefile
.ce_file
);
2115 ct
->c_cefile
.ce_file
= stripped_content_file
;
2116 ct
->c_cefile
.ce_unlink
= 1;
2120 report (NULL
, ct
->c_partno
,
2121 begin
== 0 && end
== 0 ? "" : *file
,
2127 if (opened_input_file
) {
2141 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2144 switch (ct
->c_type
) {
2146 if (ct
->c_subtype
== TEXT_PLAIN
) {
2147 status
= convert_charset (ct
, dest_charset
, message_mods
);
2150 char *ct_charset
= content_charset (ct
);
2152 report (NULL
, ct
->c_partno
, ct
->c_file
,
2153 "convert %s to %s", ct_charset
, dest_charset
);
2157 char *ct_charset
= content_charset (ct
);
2159 report ("iconv", ct
->c_partno
, ct
->c_file
,
2160 "failed to convert %s to %s", ct_charset
, dest_charset
);
2166 case CT_MULTIPART
: {
2167 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2170 /* Should check to see if the body for this part is encoded?
2171 For now, it gets passed along as-is by InitMultiPart(). */
2172 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2174 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2180 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2181 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2184 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2197 * Fix various problems that aren't handled elsewhere. These
2198 * are fixed unconditionally: there are no switches to disable
2199 * them. (Currently, "problems" is just one: an extraneous
2200 * semicolon at the end of a header parameter list.)
2203 fix_always (CT ct
, int *message_mods
) {
2206 switch (ct
->c_type
) {
2207 case CT_MULTIPART
: {
2208 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2211 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2212 status
= fix_always (part
->mp_part
, message_mods
);
2218 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2219 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2221 status
= fix_always (e
->eb_content
, message_mods
);
2228 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2229 size_t len
= strlen (hf
->value
);
2231 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2232 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2233 /* Only do this for Content-Type and
2234 Content-Disposition fields because those are the
2235 only headers that parse_mime() warns about. */
2239 /* whitespace following a trailing ';' will be nuked as well */
2240 if (hf
->value
[len
- 1] == '\n') {
2241 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2242 if (len
-- == 0) { break; }
2246 if (hf
->value
[len
- 2] == ';') {
2247 /* Remove trailing ';' from parameter value. */
2248 hf
->value
[len
- 2] = '\n';
2249 hf
->value
[len
- 1] = '\0';
2251 /* Also, if Content-Type parameter, remove trailing ';'
2252 from ct->c_ctline. This probably isn't necessary
2254 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2255 size_t l
= strlen(ct
->c_ctline
) - 1;
2256 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2257 ct
->c_ctline
[l
] == ';') {
2258 ct
->c_ctline
[l
--] = '\0';
2259 if (l
== 0) { break; }
2265 report (NULL
, ct
->c_partno
, ct
->c_file
,
2266 "remove trailing ; from %s parameter value",
2278 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
2282 if (modify_inplace
) {
2283 if (message_mods
> 0) {
2284 if ((status
= output_message (ct
, outfile
)) == OK
) {
2285 char *infile
= input_filename
2286 ? add (input_filename
, NULL
)
2287 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
2289 if (remove_file (infile
) == OK
) {
2290 if (rename (outfile
, infile
)) {
2291 /* Rename didn't work, possibly because of an
2292 attempt to rename across filesystems. Try
2293 brute force copy. */
2294 int old
= open (outfile
, O_RDONLY
);
2296 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2299 if (old
!= -1 && new != -1) {
2300 char buffer
[BUFSIZ
];
2302 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2304 if (write (new, buffer
, i
) != i
) {
2310 if (new != -1) { close (new); }
2311 if (old
!= -1) { close (old
); }
2312 (void) m_unlink (outfile
);
2315 /* The -file argument processing used path() to
2316 expand filename to absolute path. */
2317 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2319 admonish (NULL
, "unable to rename %s %s to %s",
2320 file
? "file" : "message", outfile
,
2326 admonish (NULL
, "unable to remove input file %s, "
2327 "not modifying it", infile
);
2328 (void) m_unlink (outfile
);
2337 /* No modifications and didn't need the tmp outfile. */
2338 (void) m_unlink (outfile
);
2341 /* Output is going to some file. Produce it whether or not
2342 there were modifications. */
2343 status
= output_message (ct
, outfile
);
2352 * parse_mime() does not set lf_line_endings in struct text, so use this function to do it.
2353 * It touches the parts the decodetypes identifies.
2356 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
) {
2357 switch (ct
->c_type
) {
2358 case CT_MULTIPART
: {
2359 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2362 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2363 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2369 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2370 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2372 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2377 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2378 if (ct
->c_ctparams
== NULL
) {
2379 if ((ct
->c_ctparams
= (struct text
*) mh_xcalloc (1, sizeof (struct text
))) == NULL
) {
2380 adios (NULL
, "out of memory");
2383 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2390 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2391 * use the standard MH backup file.
2394 remove_file (char *file
) {
2396 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2397 int status
= system (rmm_command
);
2400 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2402 /* This is OK for a non-message file, it still uses the
2403 BACKUP_PREFIX form. The backup file will be in the same
2404 directory as file. */
2405 return rename (file
, m_backup (file
));
2411 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2416 va_start (args
, message
);
2417 fmt
= concat (filename
, partno
? " part " : ", ",
2418 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
2420 advertise (what
, NULL
, fmt
, args
);
2433 fprintf (stderr
, "\n");