2 * mhfixmsg.c -- rewrite a message with various transformations
4 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
5 * See the COPYRIGHT file in the root directory of the nmh
6 * distribution for complete copyright information.
11 #include <h/mhparse.h>
13 #include <h/signals.h>
16 #define MHFIXMSG_SWITCHES \
17 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
18 X("nodecodetext", 0, NDECODETEXTSW) \
19 X("decodetypes", 0, DECODETYPESW) \
20 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
21 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
22 X("textcharset", 0, TEXTCHARSETSW) \
23 X("notextcharset", 0, NTEXTCHARSETSW) \
24 X("reformat", 0, REFORMATSW) \
25 X("noreformat", 0, NREFORMATSW) \
26 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
27 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
28 X("fixboundary", 0, FIXBOUNDARYSW) \
29 X("nofixboundary", 0, NFIXBOUNDARYSW) \
30 X("fixcte", 0, FIXCOMPOSITECTESW) \
31 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
32 X("fixtype mimetype", 0, FIXTYPESW) \
33 X("file file", 0, FILESW) \
34 X("outfile file", 0, OUTFILESW) \
35 X("rmmproc program", 0, RPROCSW) \
36 X("normmproc", 0, NRPRCSW) \
37 X("changecur", 0, CHGSW) \
38 X("nochangecur", 0, NCHGSW) \
39 X("verbose", 0, VERBSW) \
40 X("noverbose", 0, NVERBSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(MHFIXMSG
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
54 int debugsw
; /* Needed by mhparse.c. */
56 #define quitser pipeser
59 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
60 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
61 extern int bogus_mp_content
; /* flag from InitMultiPart */
62 /* flags to/from parse_header_attrs */
63 extern int suppress_extraneous_trailing_semicolon_warning
;
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_composite_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 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 fix_filename_param (char *, char *, PM
*, PM
*);
122 static int fix_filename_encoding (CT
);
123 static int write_content (CT
, const char *, char *, int, int);
124 static void set_text_ctparams(CT
, char *, int);
125 static int remove_file (const char *);
126 static void report (char *, char *, char *, char *, ...);
127 static void pipeser (int);
131 main (int argc
, char **argv
) {
133 char *cp
, *file
= NULL
, *folder
= NULL
;
134 char *maildir
, buf
[100], *outfile
= NULL
;
135 char **argp
, **arguments
;
136 struct msgs_array msgs
= { 0, 0, NULL
};
137 struct msgs
*mp
= NULL
;
143 fix_transformations fx
;
144 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
146 fx
.replacetextplain
= 0;
147 fx
.decodetext
= CE_8BIT
;
148 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
149 fx
.lf_line_endings
= 0;
150 fx
.textcharset
= NULL
;
152 if (nmh_init(argv
[0], 1)) { return 1; }
156 arguments
= getarguments (invo_name
, argc
, argv
, 1);
162 while ((cp
= *argp
++)) {
164 switch (smatch (++cp
, switches
)) {
166 ambigsw (cp
, switches
);
169 adios (NULL
, "-%s unknown", cp
);
172 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
174 print_help (buf
, switches
, 1);
177 print_version(invo_name
);
181 if (! (cp
= *argp
++) || *cp
== '-') {
182 adios (NULL
, "missing argument to %s", argp
[-2]);
184 if (! strcasecmp (cp
, "8bit")) {
185 fx
.decodetext
= CE_8BIT
;
186 } else if (! strcasecmp (cp
, "7bit")) {
187 fx
.decodetext
= CE_7BIT
;
188 } else if (! strcasecmp (cp
, "binary")) {
189 fx
.decodetext
= CE_BINARY
;
191 adios (NULL
, "invalid argument to %s", argp
[-2]);
198 if (! (cp
= *argp
++) || *cp
== '-') {
199 adios (NULL
, "missing argument to %s", argp
[-2]);
203 case CRLFLINEBREAKSSW
:
204 fx
.lf_line_endings
= 0;
206 case NCRLFLINEBREAKSSW
:
207 fx
.lf_line_endings
= 1;
210 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
211 adios (NULL
, "missing argument to %s", argp
[-2]);
224 case FIXCOMPOSITECTESW
:
225 fx
.fixcompositecte
= 1;
227 case NFIXCOMPOSITECTESW
:
228 fx
.fixcompositecte
= 0;
231 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
232 adios (NULL
, "missing argument to %s", argp
[-2]);
234 if (! strncasecmp (cp
, "multipart/", 10) ||
235 ! strncasecmp (cp
, "message/", 8)) {
236 adios (NULL
, "-fixtype %s not allowed", cp
);
237 } else if (! strchr (cp
, '/')) {
238 adios (NULL
, "-fixtype requires type/subtype");
240 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
241 svector_push_back (fx
.fixtypes
, cp
);
249 case REPLACETEXTPLAINSW
:
250 fx
.replacetextplain
= 1;
252 case NREPLACETEXTPLAINSW
:
253 fx
.replacetextplain
= 0;
256 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
257 adios (NULL
, "missing argument to %s", argp
[-2]);
259 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
262 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
263 adios (NULL
, "missing argument to %s", argp
[-2]);
265 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
268 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
269 adios (NULL
, "missing argument to %s", argp
[-2]);
289 if (*cp
== '+' || *cp
== '@') {
291 adios (NULL
, "only one folder at a time!");
293 folder
= pluspath (cp
);
297 /* Interpret a full path as a filename, not a message. */
298 file
= add (cp
, NULL
);
300 app_msgarg (&msgs
, cp
);
305 SIGNAL (SIGQUIT
, quitser
);
306 SIGNAL (SIGPIPE
, pipeser
);
309 * Read the standard profile setup
311 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
312 readconfig ((struct node
**) 0, fp
, cp
, 0);
316 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
317 suppress_extraneous_trailing_semicolon_warning
= 1;
319 if (! context_find ("path")) {
320 free (path ("./", TFOLDER
));
323 if (file
&& msgs
.size
) {
324 adios (NULL
, "cannot specify msg and file at same time!");
328 * check if message is coming from file
331 /* If file is stdin, create a tmp file name before parse_mime()
332 has a chance, because it might put in on a different
333 filesystem than the output file. Instead, put it in the
334 user's preferred tmp directory. */
337 if (! strcmp ("-", file
)) {
343 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
344 adios (NULL
, "unable to create temporary file in %s",
348 file
= add (cp
, NULL
);
349 cpydata (STDIN_FILENO
, fd
, "-", file
);
353 (void) m_unlink (file
);
354 adios (NULL
, "failed to write temporary file");
358 if (! (cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof *cts
))) {
359 adios (NULL
, "out of memory");
363 if ((ct
= parse_mime (file
))) {
364 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
367 advise (NULL
, "unable to parse message from file %s", file
);
370 /* If there's an outfile, pass the input message unchanged, so the message won't
371 get dropped from a pipeline. */
373 /* Something went wrong. Output might be expected, such as if this were run
374 as a filter. Just copy the input to the output. */
375 if (copy_input_to_output (file
, outfile
) != OK
) {
376 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
382 * message(s) are coming from a folder
387 app_msgarg(&msgs
, "cur");
390 folder
= getfolder (1);
392 maildir
= m_maildir (folder
);
394 if (chdir (maildir
) == NOTOK
) {
395 adios (maildir
, "unable to change directory to");
398 /* read folder and create message structure */
399 if (! (mp
= folder_read (folder
, 1))) {
400 adios (NULL
, "unable to read folder %s", folder
);
403 /* check for empty folder */
404 if (mp
->nummsg
== 0) {
405 adios (NULL
, "no messages in %s", folder
);
408 /* parse all the message ranges/sequences and set SELECTED */
409 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
410 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
413 seq_setprev (mp
); /* set the previous-sequence */
416 (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
))) {
417 adios (NULL
, "out of memory");
421 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
422 if (is_selected(mp
, msgnum
)) {
425 msgnam
= m_name (msgnum
);
426 if ((ct
= parse_mime (msgnam
))) {
427 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
430 advise (NULL
, "unable to parse message %s", msgnam
);
433 /* If there's an outfile, pass the input message unchanged, so the message won't
434 get dropped from a pipeline. */
436 /* Something went wrong. Output might be expected, such as if this were run
437 as a filter. Just copy the input to the output. */
438 const char *input_filename
= path (msgnam
, TFILE
);
440 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
441 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
449 seq_setcur (mp
, mp
->hghsel
); /* update current message */
451 seq_save (mp
); /* synchronize sequences */
452 context_replace (pfolder
, folder
);/* update current folder */
453 context_save (); /* save the context file */
457 for (ctp
= cts
; *ctp
; ++ctp
) {
458 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
461 (void) m_unlink (file
);
464 /* Just calling m_backup() unlinks the backup file. */
465 (void) m_backup (file
);
473 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
479 /* done is freects_done, which will clean up all of cts. */
486 * Apply transformations to one message.
489 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
490 /* Store input filename in case one of the transformations, i.e.,
491 fix_boundary(), rewrites to a tmp file. */
492 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
493 int modify_inplace
= 0;
494 int message_mods
= 0;
497 if (outfile
== NULL
) {
500 if ((*ctp
)->c_file
) {
502 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
503 adios (NULL
, "unable to create temporary file in %s",
506 outfile
= add (tempfile
, NULL
);
508 adios (NULL
, "missing both input and output filenames\n");
512 reverse_alternative_parts (*ctp
);
513 status
= fix_always (*ctp
, &message_mods
);
514 if (status
== OK
&& fx
->fixboundary
) {
515 status
= fix_boundary (ctp
, &message_mods
);
517 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
518 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
520 if (status
== OK
&& fx
->fixcompositecte
) {
521 status
= fix_composite_cte (*ctp
, &message_mods
);
523 if (status
== OK
&& fx
->reformat
) {
525 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
527 if (status
== OK
&& fx
->decodetext
) {
528 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
531 if (status
== OK
&& fx
->textcharset
!= NULL
) {
532 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
535 if (status
== OK
&& ! (*ctp
)->c_umask
) {
536 /* Set the umask for the contents file. This currently
537 isn't used but just in case it is in the future. */
540 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
541 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
543 (*ctp
)->c_umask
= ~m_gmprot();
548 * Write the content to a file
551 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
553 } else if (! modify_inplace
) {
554 /* Something went wrong. Output might be expected, such
555 as if this were run as a filter. Just copy the input
557 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
558 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
562 if (modify_inplace
) {
563 if (status
!= OK
) { (void) m_unlink (outfile
); }
568 free (input_filename
);
575 * Copy input message to output. Assumes not modifying in place, so this
576 * might be running as part of a pipeline.
579 copy_input_to_output (const char *input_filename
, const char *output_filename
) {
580 int in
= open (input_filename
, O_RDONLY
);
581 int out
= strcmp (output_filename
, "-")
582 ? open (output_filename
, O_WRONLY
| O_CREAT
, m_gmprot ())
586 if (in
!= -1 && out
!= -1) {
587 cpydata (in
, out
, input_filename
, output_filename
);
600 * Fix mismatched outer level boundary.
603 fix_boundary (CT
*ct
, int *message_mods
) {
604 struct multipart
*mp
;
607 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
608 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
611 * 1) Get boundary at end of part.
612 * 2) Get boundary at beginning of part and compare to the end-of-part
614 * 3) Write out contents of ct to tmp file, replacing boundary in
615 * header with boundary from part. Set c_unlink to 1.
617 * 5) Call parse_mime() on the tmp file, replacing ct.
620 if (mp
&& mp
->mp_start
) {
623 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
626 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
627 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
628 char *filename
= add ((*ct
)->c_file
, NULL
);
632 if ((fixed_ct
= parse_mime (fixed
))) {
638 report (NULL
, NULL
, filename
,
639 "fix multipart boundary");
643 advise (NULL
, "unable to parse fixed part");
648 advise (NULL
, "unable to replace broken boundary");
652 advise (NULL
, "unable to create temporary file in %s",
657 free (part_boundary
);
659 /* Couldn't fix the boundary. Report failure so that mhfixmsg
660 doesn't modify the message. */
664 /* No multipart struct, even though the content type is
665 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
676 * Find boundary at end of multipart.
679 get_multipart_boundary (CT ct
, char **part_boundary
) {
681 char *end_boundary
= NULL
;
682 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
683 ? (off_t
) (ct
->c_end
- sizeof buffer
)
684 : (off_t
) ct
->c_begin
;
688 /* This will fail if the boundary spans fread() calls. BUFSIZ should
689 be big enough, even if it's just 1024, to make that unlikely. */
691 /* free_content() will close ct->c_fp. */
692 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
693 advise (ct
->c_file
, "unable to open for reading");
697 /* Get boundary at end of multipart. */
698 while (begin
>= (off_t
) ct
->c_begin
) {
699 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
700 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
701 char *cp
= rfind_str (buffer
, bytes_read
, "--");
706 /* Trim off trailing "--" and anything beyond. */
708 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
709 if (strlen (end
) > 3 && *end
++ == '\n' &&
710 *end
++ == '-' && *end
++ == '-') {
711 end_boundary
= add (end
, NULL
);
718 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
719 begin
-= sizeof buffer
;
725 /* Get boundary at beginning of multipart. */
727 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
728 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
729 if (bytes_read
>= strlen (end_boundary
)) {
730 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
732 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
733 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
738 /* The start and end boundaries didn't match, or the
739 start boundary doesn't begin with "\n--" (or "--"
740 if at the beginning of buffer). Keep trying. */
749 *part_boundary
= end_boundary
;
751 *part_boundary
= NULL
;
760 * Open and copy ct->c_file to file, replacing the multipart boundary.
763 replace_boundary (CT ct
, char *file
, char *boundary
) {
766 char buf
[BUFSIZ
], name
[NAMESZ
];
768 m_getfld_state_t gstate
= 0;
771 if (ct
->c_file
== NULL
) {
772 advise (NULL
, "missing input filename");
776 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
777 advise (ct
->c_file
, "unable to open for reading");
781 if ((fpout
= fopen (file
, "w")) == NULL
) {
783 advise (file
, "unable to open for writing");
787 for (compnum
= 1;;) {
788 int bufsz
= (int) sizeof buf
;
790 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
795 /* get copies of the buffers */
796 np
= add (name
, NULL
);
797 vp
= add (buf
, NULL
);
799 /* if necessary, get rest of field */
800 while (state
== FLDPLUS
) {
802 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
803 vp
= add (buf
, vp
); /* add to previous value */
806 if (strcasecmp (TYPE_FIELD
, np
)) {
807 fprintf (fpout
, "%s:%s", np
, vp
);
809 char *new_ctline
, *new_params
;
811 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
812 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
815 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
816 ct
->c_ctinfo
.ci_subtype
, NULL
);
817 new_params
= output_params(strlen(TYPE_FIELD
) +
818 strlen(new_ctline
) + 1,
819 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
820 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
821 new_params
? new_params
: "");
835 /* buf will have a terminating NULL, skip it. */
836 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
837 advise (file
, "fwrite");
846 advise (NULL
, "message format error in component #%d", compnum
);
851 advise (NULL
, "getfld() returned %d", state
);
859 m_getfld_state_destroy (&gstate
);
868 * Fix Content-Type header to reflect the content of its part.
871 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
874 switch (ct
->c_type
) {
876 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
879 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
880 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
886 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
887 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
889 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
896 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
897 for (typep
= svector_strs (fixtypes
);
898 typep
&& (type
= *typep
);
901 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
904 if (! strcasecmp (type
, type_subtype
) &&
905 decode_part (ct
) == OK
&&
906 ct
->c_cefile
.ce_file
!= NULL
) {
907 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
910 if ((cp
= strchr (ct_type_subtype
, ';'))) {
911 /* Truncate to remove any parameter list from
912 mime_type () result. */
916 if (strcasecmp (type
, ct_type_subtype
)) {
917 char *ct_type
, *ct_subtype
;
920 /* The Content-Type header does not match the
921 content, so update these struct Content
924 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
927 /* Extract type and subtype from type/subtype. */
928 ct_type
= getcpy (ct_type_subtype
);
929 if ((cp
= strchr (ct_type
, '/'))) {
931 ct_subtype
= getcpy (++cp
);
933 advise (NULL
, "missing / in MIME type of %s %s",
934 ct
->c_file
, ct
->c_partno
);
939 ct
->c_type
= ct_str_type (ct_type
);
940 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
942 free (ct
->c_ctinfo
.ci_type
);
943 ct
->c_ctinfo
.ci_type
= ct_type
;
944 free (ct
->c_ctinfo
.ci_subtype
);
945 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
946 if (! replace_substring (&ct
->c_ctline
, type
,
948 advise (NULL
, "did not find %s in %s",
952 /* Update Content-Type header field. */
953 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
954 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
955 if (replace_substring (&hf
->value
, type
,
959 report (NULL
, ct
->c_partno
, ct
->c_file
,
960 "change Content-Type in header "
962 type
, ct_type_subtype
);
966 advise (NULL
, "did not find %s in %s",
972 free (ct_type_subtype
);
984 * Replace a substring, allocating space to hold the new one.
987 replace_substring (char **str
, const char *old
, const char *new) {
990 if ((cp
= strstr (*str
, old
))) {
991 char *remainder
= cp
+ strlen (old
);
992 char *prefix
, *new_str
;
995 prefix
= getcpy (*str
);
996 *(prefix
+ (cp
- *str
)) = '\0';
997 new_str
= concat (prefix
, new, remainder
, NULL
);
1000 new_str
= concat (new, remainder
, NULL
);
1005 return *str
= new_str
;
1013 * Remove a name=value parameter, given just its name, from a header value.
1016 remove_parameter (char *str
, const char *name
) {
1017 /* It looks to me, based on the BNF in RFC 2045, than there can't
1018 be whitespace betwwen the parameter name and the "=", or
1019 between the "=" and the parameter value. */
1020 char *param_name
= concat (name
, "=", NULL
);
1023 if ((cp
= strstr (str
, param_name
))) {
1027 /* Remove any leading spaces, before the parameter name. */
1029 start
> str
&& isspace ((unsigned char) *(start
-1));
1033 /* Remove a leading semicolon. */
1034 if (start
> str
&& *(start
-1) == ';') { --start
; }
1036 end
= cp
+ strlen (name
) + 1;
1038 /* Skip past the quoted value, and then the final quote. */
1039 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1042 /* Skip past the value. */
1043 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1046 /* Count how many characters need to be moved. Include
1047 trailing null, which is accounted for by the
1048 initialization of count to 1. */
1049 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1050 (void) memmove (start
, end
, count
);
1060 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1061 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1065 fix_composite_cte (CT ct
, int *message_mods
) {
1068 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1069 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1070 ct
->c_encoding
!= CE_BINARY
) {
1073 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1074 char *name
= hf
->name
;
1075 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
1079 if (! strncasecmp (name
, ENCODING_FIELD
,
1080 strlen (ENCODING_FIELD
))) {
1081 char *prefix
= "Nmh-REPLACED-INVALID-";
1082 HF h
= mh_xmalloc (sizeof *h
);
1084 h
->name
= add (hf
->name
, NULL
);
1085 h
->hf_encoding
= hf
->hf_encoding
;
1089 /* Retain old header but prefix its name. */
1091 hf
->name
= concat (prefix
, h
->name
, NULL
);
1095 char *encoding
= cpytrim (hf
->value
);
1096 report (NULL
, ct
->c_partno
, ct
->c_file
,
1097 "replace Content-Transfer-Encoding of %s "
1098 "with 8 bit", encoding
);
1102 h
->value
= add (" 8bit\n", NULL
);
1104 /* Don't need to warn for multiple C-T-E header
1105 fields, parse_mime() already does that. But
1106 if there are any, fix them all as necessary. */
1111 set_ce (ct
, CE_8BIT
);
1114 if (ct
->c_type
== CT_MULTIPART
) {
1115 struct multipart
*m
;
1118 m
= (struct multipart
*) ct
->c_ctparams
;
1119 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1120 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1133 * Set content encoding.
1136 set_ce (CT ct
, int encoding
) {
1137 const char *ce
= ce_str (encoding
);
1138 const struct str2init
*ctinit
= get_ce_method (ce
);
1141 char *cte
= concat (" ", ce
, "\n", NULL
);
1144 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1145 caller is decode_text_parts (). Save because we'll
1147 struct cefile decoded_content_info
= ct
->c_cefile
;
1149 ct
->c_encoding
= encoding
;
1151 ct
->c_ctinitfnx
= ctinit
->si_init
;
1152 /* This will assign ct->c_cefile with an all-0 struct, which
1154 (*ctinit
->si_init
) (ct
);
1155 /* After returning, the caller should set
1156 ct->c_cefile.ce_file to the name of the file containing
1159 /* Restore the cefile. */
1160 ct
->c_cefile
= decoded_content_info
;
1162 /* Update/add Content-Transfer-Encoding header field. */
1163 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1164 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1171 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
1174 /* Update c_celine. It's used only by mhlist -debug. */
1175 free (ct
->c_celine
);
1176 ct
->c_celine
= add (cte
, NULL
);
1186 * Make sure each text part has a corresponding text/plain part.
1189 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1192 switch ((*ct
)->c_type
) {
1194 /* Nothing to do for text/plain. */
1195 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1197 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1198 parent
->c_subtype
== MULTI_ALTERNATE
) {
1199 int new_subpart_number
= 1;
1200 int has_text_plain
=
1201 find_textplain_sibling (parent
, replacetextplain
,
1202 &new_subpart_number
);
1204 if (! has_text_plain
) {
1205 /* Parent is a multipart/alternative. Insert a new
1206 text/plain subpart. */
1207 const int inserted
=
1208 insert_new_text_plain_part (*ct
, new_subpart_number
,
1213 report (NULL
, parent
->c_partno
, parent
->c_file
,
1214 "insert text/plain part");
1220 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1221 parent
->c_subtype
== MULTI_RELATED
) {
1222 char *type_subtype
=
1223 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1224 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1225 const char *parent_type
=
1226 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1227 int new_subpart_number
= 1;
1228 int has_text_plain
= 0;
1230 /* Have to do string comparison on the subtype because we
1231 don't enumerate all of them in c_subtype values.
1232 parent_type will be NULL if the multipart/related part
1233 doesn't have a type parameter. The type parameter must
1234 be specified according to RFC 2387 Sec. 3.1 but not all
1236 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1237 /* The type of this part matches the root type of the
1238 parent multipart/related. Look to see if there's
1239 text/plain sibling. */
1241 find_textplain_sibling (parent
, replacetextplain
,
1242 &new_subpart_number
);
1245 free (type_subtype
);
1247 if (! has_text_plain
) {
1248 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1252 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1253 if (*ct
!= part
->mp_part
) {
1259 /* Parent is a multipart/related. Insert a new
1260 text/plain subpart in a new multipart/alternative. */
1261 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1262 /* Not an error if text/plain couldn't be added. */
1265 /* There are no siblings, so insert a new text/plain
1266 subpart, and change the parent type from
1267 multipart/related to multipart/alternative. */
1268 const int inserted
=
1269 insert_new_text_plain_part (*ct
, new_subpart_number
,
1275 parent
->c_subtype
= MULTI_ALTERNATE
;
1276 parent
->c_ctinfo
.ci_subtype
= getcpy ("alternative");
1277 if (! replace_substring (&parent
->c_ctline
, "/related",
1280 "did not find multipart/related in %s",
1284 /* Update Content-Type header field. */
1285 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1286 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1287 if (replace_substring (&hf
->value
, "/related",
1291 report (NULL
, parent
->c_partno
,
1293 "insert text/plain part");
1296 /* Remove, e.g., type="text/html" from
1297 multipart/alternative. */
1298 remove_parameter (hf
->value
, "type");
1301 advise (NULL
, "did not find multipart/"
1302 "related in header %s",
1308 /* Not an error if text/plain couldn't be inserted. */
1313 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1320 case CT_MULTIPART
: {
1321 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1324 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1325 if ((*ct
)->c_type
== CT_MULTIPART
) {
1326 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1334 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1335 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1337 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1348 * See if there is a sibling text/plain, and return its subpart number.
1351 find_textplain_sibling (CT parent
, int replacetextplain
,
1352 int *new_subpart_number
) {
1353 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1354 struct part
*part
, *prev
;
1355 int has_text_plain
= 0;
1357 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1358 ++*new_subpart_number
;
1359 if (part
->mp_part
->c_type
== CT_TEXT
&&
1360 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1361 if (replacetextplain
) {
1362 struct part
*old_part
;
1363 if (part
== mp
->mp_parts
) {
1364 old_part
= mp
->mp_parts
;
1365 mp
->mp_parts
= part
->mp_next
;
1367 old_part
= prev
->mp_next
;
1368 prev
->mp_next
= part
->mp_next
;
1371 report (NULL
, parent
->c_partno
, parent
->c_file
,
1372 "remove text/plain part %s",
1373 old_part
->mp_part
->c_partno
);
1375 free_content (old_part
->mp_part
);
1385 return has_text_plain
;
1390 * Insert a new text/plain part.
1393 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1394 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1395 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
1397 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1399 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1401 new_part
->mp_next
= mp
->mp_parts
;
1402 mp
->mp_parts
= new_part
;
1403 new_part
->mp_part
->c_partno
=
1404 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1409 free_content (new_part
->mp_part
);
1418 * Create a text/plain part to go along with non-plain sibling part.
1421 build_text_plain_part (CT encoded_part
) {
1422 CT tp_part
= divide_part (encoded_part
);
1423 char *tmp_plain_file
= NULL
;
1425 if (decode_part (tp_part
) == OK
) {
1426 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1427 contains the decoded contents. And the decoding function, such
1428 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1429 be unlinked by free_content (). */
1432 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1433 advise (NULL
, "unable to create temporary file in %s",
1436 tmp_plain_file
= add (tempfile
, NULL
);
1437 if (reformat_part (tp_part
, tmp_plain_file
,
1438 tp_part
->c_ctinfo
.ci_type
,
1439 tp_part
->c_ctinfo
.ci_subtype
,
1440 tp_part
->c_type
) == OK
) {
1446 free_content (tp_part
);
1447 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1448 free (tmp_plain_file
);
1455 * Slip new text/plain part into a new multipart/alternative.
1458 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1459 CT tp_part
= build_text_plain_part (*ct
);
1463 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1466 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1468 if (mp
&& mp
->mp_parts
) {
1469 mp
->mp_parts
->mp_part
= tp_part
;
1470 /* Make the new multipart/alternative the parent. */
1475 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1476 "insert text/plain part");
1479 free_content (tp_part
);
1480 free_content (mp_alt
);
1487 /* Not an error if text/plain couldn't be built. */
1495 * Clone a MIME part.
1498 divide_part (CT ct
) {
1501 if ((new_part
= (CT
) mh_xcalloc (1, sizeof *new_part
)) == NULL
)
1502 adios (NULL
, "out of memory");
1504 /* Just copy over what is needed for decoding. c_vrsn and
1505 c_celine aren't necessary. */
1506 new_part
->c_file
= add (ct
->c_file
, NULL
);
1507 new_part
->c_begin
= ct
->c_begin
;
1508 new_part
->c_end
= ct
->c_end
;
1509 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1510 new_part
->c_type
= ct
->c_type
;
1511 new_part
->c_cefile
= ct
->c_cefile
;
1512 new_part
->c_encoding
= ct
->c_encoding
;
1513 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1514 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1515 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1516 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1518 /* c_ctline is used by reformat__part(), so it can preserve
1519 anything after the type/subtype. */
1520 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1527 * Copy the content info from one part to another.
1530 copy_ctinfo (CI dest
, CI src
) {
1533 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1534 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1536 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1537 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1539 if (s_pm
->pm_charset
) {
1540 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1542 if (s_pm
->pm_lang
) {
1543 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1547 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1548 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1556 decode_part (CT ct
) {
1561 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1562 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1564 tmp_decoded
= add (tempfile
, NULL
);
1565 /* The following call will load ct->c_cefile.ce_file with the tmp
1566 filename of the decoded content. tmp_decoded will contain the
1567 encoded output, get rid of that. */
1568 status
= output_message (ct
, tmp_decoded
);
1569 (void) m_unlink (tmp_decoded
);
1577 * Reformat content as plain text.
1578 * Some of the arguments aren't really needed now, but maybe will
1579 * be in the future for other than text types.
1582 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1583 int output_subtype
, output_encoding
;
1584 const char *reason
= NULL
;
1588 /* Hacky: this redirects the output from whatever command is used
1589 to show the part to a file. So, the user can't have any output
1590 redirection in that command.
1591 Could show_multi() in mhshowsbr.c avoid this? */
1593 /* Check for invo_name-format-type/subtype. */
1594 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1596 advise (NULL
, "Don't know how to convert %s, there is no "
1597 "%s-format-%s/%s profile entry",
1598 ct
->c_file
, invo_name
, type
, subtype
);
1602 if (strchr (cf
, '>')) {
1603 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1604 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1605 subtype
? subtype
: "");
1611 cp
= concat (cf
, " >", file
, NULL
);
1612 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1615 /* Unlink decoded content tmp file and free its filename to avoid
1616 leaks. The file stream should already have been closed. */
1617 if (ct
->c_cefile
.ce_unlink
) {
1618 (void) m_unlink (ct
->c_cefile
.ce_file
);
1619 free (ct
->c_cefile
.ce_file
);
1620 ct
->c_cefile
.ce_file
= NULL
;
1621 ct
->c_cefile
.ce_unlink
= 0;
1624 if (c_type
== CT_TEXT
) {
1625 output_subtype
= TEXT_PLAIN
;
1627 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1631 output_encoding
= content_encoding (ct
, &reason
);
1632 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1633 ct
->c_cefile
.ce_file
= file
;
1634 ct
->c_cefile
.ce_unlink
= 1;
1636 ct
->c_cefile
.ce_unlink
= 0;
1645 * Fill in a multipart/alternative part.
1648 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1649 char *boundary_prefix
= "----=_nmh-multipart";
1650 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1651 char *boundary_indicator
= "; boundary=";
1652 char *typename
, *subtypename
, *name
;
1655 struct multipart
*m
;
1656 const struct str2init
*ctinit
;
1658 if ((ct
= (CT
) mh_xcalloc (1, sizeof *ct
)) == NULL
)
1659 adios (NULL
, "out of memory");
1661 /* Set up the multipart/alternative part. These fields of *ct were
1662 initialized to 0 by mh_xcalloc():
1663 c_fp, c_unlink, c_begin, c_end,
1664 c_vrsn, c_ctline, c_celine,
1665 c_id, c_descr, c_dispo, c_partno,
1666 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1667 c_cefile, c_encoding,
1668 c_digested, c_digest[16], c_ctexbody,
1669 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1671 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1674 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1676 ct
->c_subtype
= subtype
;
1678 ctinit
= get_ct_init (ct
->c_type
);
1680 typename
= ct_type_str (type
);
1681 subtypename
= ct_subtype_str (type
, subtype
);
1685 int found_boundary
= 1;
1687 while (found_boundary
&& serial
< 1000000) {
1690 /* Ensure that the boundary doesn't appear in the decoded
1692 if (new_part
->c_cefile
.ce_file
) {
1693 if ((found_boundary
=
1694 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1695 new_part
->c_cefile
.ce_file
,
1702 /* Ensure that the boundary doesn't appear in the encoded
1704 if (! found_boundary
&& new_part
->c_file
) {
1705 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1713 if (found_boundary
) {
1714 /* Try a slightly different boundary. */
1719 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1721 concat (boundary_prefix
,
1722 first_alt
->c_partno
? first_alt
->c_partno
: "",
1723 "-", buffer2
, NULL
);
1727 if (found_boundary
) {
1728 advise (NULL
, "giving up trying to find a unique boundary");
1734 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1735 boundary
, "\"", NULL
);
1737 /* Load c_first_hf and c_last_hf. */
1738 transfer_noncontent_headers (first_alt
, ct
);
1739 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1742 /* Load c_partno. */
1743 if (first_alt
->c_partno
) {
1744 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1745 free (first_alt
->c_partno
);
1746 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1747 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1749 first_alt
->c_partno
= add ("1", NULL
);
1750 new_part
->c_partno
= add ("2", NULL
);
1754 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1755 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1758 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1759 "boundary", boundary
, 0);
1761 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1762 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1763 p
->mp_next
->mp_next
= NULL
;
1764 p
->mp_next
->mp_part
= first_alt
;
1766 if ((m
= (struct multipart
*) mh_xcalloc (1, sizeof (struct multipart
))) ==
1768 adios (NULL
, "out of memory");
1769 m
->mp_start
= concat (boundary
, "\n", NULL
);
1770 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1781 * Check that the boundary does not appear in the content.
1784 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1785 char buffer
[BUFSIZ
];
1787 int found_boundary
= 0;
1789 /* free_content() will close *fp if we fopen it here. */
1790 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1791 advise (file
, "unable to open %s for reading", file
);
1795 fseeko (*fp
, 0L, SEEK_SET
);
1796 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1797 if (find_str (buffer
, bytes_read
, boundary
)) {
1803 return found_boundary
;
1808 * Remove all non-Content headers.
1811 transfer_noncontent_headers (CT old
, CT
new) {
1814 hp_prev
= hp
= old
->c_first_hf
;
1818 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1819 if (hp
== old
->c_last_hf
) {
1820 if (hp
== old
->c_first_hf
) {
1821 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1823 hp_prev
->next
= NULL
;
1824 old
->c_last_hf
= hp_prev
;
1827 if (hp
== old
->c_first_hf
) {
1828 old
->c_first_hf
= next
;
1830 hp_prev
->next
= next
;
1834 /* Put node hp in the new CT. */
1835 if (new->c_first_hf
== NULL
) {
1836 new->c_first_hf
= hp
;
1838 new->c_last_hf
->next
= hp
;
1840 new->c_last_hf
= hp
;
1842 /* A Content- header, leave in old. */
1855 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1856 char *typename
= ct_type_str (type
);
1857 char *subtypename
= ct_subtype_str (type
, subtype
);
1858 /* E.g, " text/plain" */
1859 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1860 /* E.g, " text/plain\n" */
1861 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1862 int found_content_type
= 0;
1864 const char *cp
= NULL
;
1868 /* Update/add Content-Type header field. */
1869 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1870 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1871 found_content_type
= 1;
1873 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1874 ? concat (type_subtypename
, cp
, "\n", NULL
)
1875 : add (name_plus_nl
, NULL
);
1878 if (! found_content_type
) {
1879 add_header (ct
, add (TYPE_FIELD
, NULL
),
1880 (cp
= strchr (ct
->c_ctline
, ';'))
1881 ? concat (type_subtypename
, cp
, "\n", NULL
)
1882 : add (name_plus_nl
, NULL
));
1885 /* Some of these might not be used, but set them anyway. */
1887 ? concat (type_subtypename
, cp
, NULL
)
1888 : concat (type_subtypename
, NULL
);
1889 free (ct
->c_ctline
);
1890 ct
->c_ctline
= ctline
;
1891 /* Leave other ctinfo members as they were. */
1892 free (ct
->c_ctinfo
.ci_type
);
1893 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1894 free (ct
->c_ctinfo
.ci_subtype
);
1895 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1897 ct
->c_subtype
= subtype
;
1899 free (name_plus_nl
);
1900 free (type_subtypename
);
1902 status
= set_ce (ct
, encoding
);
1909 * It's not necessary to update the charset parameter of a Content-Type
1910 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1911 * (content) was originally in the specified charset, "and will be in
1912 * that character set again after decoding."
1915 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
1916 int *message_mods
) {
1918 int lf_line_endings
= 0;
1920 switch (ct
->c_type
) {
1921 case CT_MULTIPART
: {
1922 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1925 /* Should check to see if the body for this part is encoded?
1926 For now, it gets passed along as-is by InitMultiPart(). */
1927 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1928 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
1935 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1936 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1938 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
1944 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
1949 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
1951 switch (ct
->c_encoding
) {
1956 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1957 const char *reason
= NULL
;
1959 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1960 && encoding
!= CE_BINARY
) {
1961 /* The decoding isn't acceptable so discard it.
1962 Leave status as OK to allow other transformations. */
1964 report (NULL
, ct
->c_partno
, ct
->c_file
,
1965 "will not decode%s because it is binary (%s)",
1967 : ct
->c_ctline
? ct
->c_ctline
1971 (void) m_unlink (ct
->c_cefile
.ce_file
);
1972 free (ct
->c_cefile
.ce_file
);
1973 ct
->c_cefile
.ce_file
= NULL
;
1974 } else if (ct
->c_encoding
== CE_QUOTED
&&
1975 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1976 /* The decoding isn't acceptable so discard it.
1977 Leave status as OK to allow other transformations. */
1979 report (NULL
, ct
->c_partno
, ct
->c_file
,
1980 "will not decode%s because it is 8bit",
1982 : ct
->c_ctline
? ct
->c_ctline
1985 (void) m_unlink (ct
->c_cefile
.ce_file
);
1986 free (ct
->c_cefile
.ce_file
);
1987 ct
->c_cefile
.ce_file
= NULL
;
1991 if (ct_encoding
== CE_BINARY
) {
1993 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1998 if (set_ce (ct
, enc
) == OK
) {
2001 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2002 ct
->c_ctline
? ct
->c_ctline
: "");
2004 if (lf_line_endings
) {
2005 strip_crs (ct
, message_mods
);
2018 if (lf_line_endings
) {
2019 strip_crs (ct
, message_mods
);
2034 * Determine if the part with type[/subtype] should be decoded, according to
2035 * decodetypes (which came from the -decodetypes switch).
2038 should_decode(const char *decodetypes
, const char *type
, const char *subtype
) {
2039 /* Quick search for matching type[/subtype] in decodetypes: bracket
2040 decodetypes with commas, then search for ,type, and ,type/subtype, in
2043 int found_match
= 0;
2044 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2045 char *delimited_type
= concat(",", type
, ",", NULL
);
2047 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2049 } else if (subtype
!= NULL
) {
2050 char *delimited_type_subtype
=
2051 concat(",", type
, "/", subtype
, ",", NULL
);
2053 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2056 free(delimited_type_subtype
);
2059 free(delimited_type
);
2060 free(delimited_decodetypes
);
2067 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2068 * if it has any NUL characters, a CR not followed by a LF, or lines
2069 * greater than 998 characters in length. If binary, reason is set
2070 * to a string explaining why.
2073 content_encoding (CT ct
, const char **reason
) {
2074 CE ce
= &ct
->c_cefile
;
2075 int encoding
= CE_7BIT
;
2078 size_t line_len
= 0;
2079 char buffer
[BUFSIZ
];
2082 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2083 advise (ce
->ce_file
, "unable to open for reading");
2087 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2088 while (encoding
!= CE_BINARY
&&
2089 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2092 int last_char_was_cr
= 0;
2094 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2095 if (*cp
== '\0' || ++line_len
> 998 ||
2096 (*cp
!= '\n' && last_char_was_cr
)) {
2097 encoding
= CE_BINARY
;
2099 *reason
= "null character";
2100 } else if (line_len
> 998) {
2101 *reason
= "line length > 998";
2102 } else if (*cp
!= '\n' && last_char_was_cr
) {
2103 *reason
= "CR not followed by LF";
2105 /* Should not reach this. */
2109 } else if (*cp
== '\n') {
2111 } else if (! isascii ((unsigned char) *cp
)) {
2115 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2121 } /* else should never happen */
2128 * Strip carriage returns from content.
2131 strip_crs (CT ct
, int *message_mods
) {
2132 char *charset
= content_charset (ct
);
2135 /* Only strip carriage returns if content is ASCII or another
2136 charset that has the same readily recognizable CR followed by a
2137 LF. We can include UTF-8 here because if the high-order bit of
2138 a UTF-8 byte is 0, then it must be a single-byte ASCII
2140 if (! strcasecmp (charset
, "US-ASCII") ||
2141 ! strcasecmp (charset
, "UTF-8") ||
2142 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2143 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2149 int opened_input_file
= 0;
2151 if (ct
->c_cefile
.ce_file
) {
2152 file
= &ct
->c_cefile
.ce_file
;
2153 fp
= &ct
->c_cefile
.ce_fp
;
2155 } else if (ct
->c_file
) {
2158 begin
= (size_t) ct
->c_begin
;
2159 end
= (size_t) ct
->c_end
;
2160 } /* else don't know where the content is */
2162 if (file
&& *file
&& fp
) {
2164 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2165 advise (*file
, "unable to open for reading");
2168 opened_input_file
= 1;
2174 char buffer
[BUFSIZ
];
2176 size_t bytes_to_read
=
2177 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2179 fseeko (*fp
, begin
, SEEK_SET
);
2180 while ((bytes_read
= fread (buffer
, 1,
2181 min (bytes_to_read
, sizeof buffer
),
2183 /* Look for CR followed by a LF. This is supposed to
2184 be text so there should be LF's. If not, don't
2185 modify the content. */
2188 int last_char_was_cr
= 0;
2190 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2192 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2193 if (*cp
== '\n' && last_char_was_cr
) {
2198 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2204 char *stripped_content_file
;
2205 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2207 if (tempfile
== NULL
) {
2208 adios (NULL
, "unable to create temporary file in %s",
2211 stripped_content_file
= add (tempfile
, NULL
);
2213 /* Strip each CR before a LF from the content. */
2214 fseeko (*fp
, begin
, SEEK_SET
);
2215 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2219 int last_char_was_cr
= 0;
2221 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2223 last_char_was_cr
= 1;
2224 } else if (last_char_was_cr
) {
2226 if (write (fd
, "\r", 1) < 0) {
2227 advise (tempfile
, "CR write");
2230 if (write (fd
, cp
, 1) < 0) {
2231 advise (tempfile
, "write");
2233 last_char_was_cr
= 0;
2235 if (write (fd
, cp
, 1) < 0) {
2236 advise (tempfile
, "write");
2238 last_char_was_cr
= 0;
2244 admonish (NULL
, "unable to write temporary file %s",
2245 stripped_content_file
);
2246 (void) m_unlink (stripped_content_file
);
2249 /* Replace the decoded file with the converted one. */
2250 if (ct
->c_cefile
.ce_file
) {
2251 if (ct
->c_cefile
.ce_unlink
) {
2252 (void) m_unlink (ct
->c_cefile
.ce_file
);
2254 free (ct
->c_cefile
.ce_file
);
2256 ct
->c_cefile
.ce_file
= stripped_content_file
;
2257 ct
->c_cefile
.ce_unlink
= 1;
2261 report (NULL
, ct
->c_partno
,
2262 begin
== 0 && end
== 0 ? "" : *file
,
2268 if (opened_input_file
) {
2282 * Convert character set of each part.
2285 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2288 switch (ct
->c_type
) {
2290 if (ct
->c_subtype
== TEXT_PLAIN
) {
2291 status
= convert_charset (ct
, dest_charset
, message_mods
);
2294 char *ct_charset
= content_charset (ct
);
2296 report (NULL
, ct
->c_partno
, ct
->c_file
,
2297 "convert %s to %s", ct_charset
, dest_charset
);
2301 char *ct_charset
= content_charset (ct
);
2303 report ("iconv", ct
->c_partno
, ct
->c_file
,
2304 "failed to convert %s to %s", ct_charset
, dest_charset
);
2310 case CT_MULTIPART
: {
2311 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2314 /* Should check to see if the body for this part is encoded?
2315 For now, it gets passed along as-is by InitMultiPart(). */
2316 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2318 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2324 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2325 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2328 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2341 * Fix various problems that aren't handled elsewhere. These
2342 * are fixed unconditionally: there are no switches to disable
2343 * them. Currently, "problems" are these:
2344 * 1) remove extraneous semicolon at the end of a header parameter list
2345 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2346 * filename parameters in Content-Type and Content-Disposition
2347 * headers, respectively.
2350 fix_always (CT ct
, int *message_mods
) {
2353 switch (ct
->c_type
) {
2354 case CT_MULTIPART
: {
2355 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2358 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2359 status
= fix_always (part
->mp_part
, message_mods
);
2365 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2366 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2368 status
= fix_always (e
->eb_content
, message_mods
);
2375 if (ct
->c_first_hf
) {
2376 fix_filename_encoding (ct
);
2379 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2380 size_t len
= strlen (hf
->value
);
2382 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2383 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2384 /* Only do this for Content-Type and
2385 Content-Disposition fields because those are the
2386 only headers that parse_mime() warns about. */
2390 /* whitespace following a trailing ';' will be nuked as well */
2391 if (hf
->value
[len
- 1] == '\n') {
2392 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2393 if (len
-- == 0) { break; }
2397 if (hf
->value
[len
- 2] == ';') {
2398 /* Remove trailing ';' from parameter value. */
2399 hf
->value
[len
- 2] = '\n';
2400 hf
->value
[len
- 1] = '\0';
2402 /* Also, if Content-Type parameter, remove trailing ';'
2403 from ct->c_ctline. This probably isn't necessary
2405 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2406 size_t l
= strlen(ct
->c_ctline
) - 1;
2407 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2408 ct
->c_ctline
[l
] == ';') {
2409 ct
->c_ctline
[l
--] = '\0';
2410 if (l
== 0) { break; }
2416 report (NULL
, ct
->c_partno
, ct
->c_file
,
2417 "remove trailing ; from %s parameter value",
2429 * Factor out common code for loops in fix_filename_encoding().
2432 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
) {
2436 if (((value_len
= strlen (value
)) > 0) &&
2437 strncmp (value
, "=?", 2) == 0 &&
2438 strncmp (&value
[value_len
- 2], "?=", 2) == 0) {
2439 /* Looks like an RFC 2047 encoded parameter. */
2440 char decoded
[PATH_MAX
+ 1];
2442 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2443 /* Encode using RFC 2231. */
2444 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2447 advise (NULL
, "failed to decode %s parameter %s", name
, value
);
2456 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2457 * filename parameters in Content-Type and Content-Disposition
2458 * headers, respectively.
2461 fix_filename_encoding (CT ct
) {
2466 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2467 if (pm
->pm_name
&& pm
->pm_value
&&
2468 strcasecmp (pm
->pm_name
, "name") == 0) {
2469 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2470 &ct
->c_ctinfo
.ci_first_pm
,
2471 &ct
->c_ctinfo
.ci_last_pm
);
2475 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2476 if (pm
->pm_name
&& pm
->pm_value
&&
2477 strcasecmp (pm
->pm_name
, "filename") == 0) {
2478 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2484 /* Fix hf values to correspond. */
2485 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2486 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2488 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2489 field
= TYPE_HEADER
;
2490 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2491 field
= DISPO_HEADER
;
2494 if (field
!= OTHER
) {
2495 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2497 if (semicolon_loc
) {
2499 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2500 const char *const params
=
2502 field
== TYPE_HEADER
2503 ? ct
->c_ctinfo
.ci_first_pm
2504 : ct
->c_dispo_first
,
2506 const char *const new_params
= concat (params
, "\n", NULL
);
2508 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2509 free ((char *) new_params
);
2510 free ((char *) params
);
2512 advise (NULL
, "did not find semicolon in %s:%s\n",
2513 hf
->name
, hf
->value
);
2523 * Output content in input file to output file.
2526 write_content (CT ct
, const char *input_filename
, char *outfile
, int modify_inplace
,
2530 if (modify_inplace
) {
2531 if (message_mods
> 0) {
2532 if ((status
= output_message (ct
, outfile
)) == OK
) {
2533 char *infile
= input_filename
2534 ? add (input_filename
, NULL
)
2535 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
2537 if (remove_file (infile
) == OK
) {
2538 if (rename (outfile
, infile
)) {
2539 /* Rename didn't work, possibly because of an
2540 attempt to rename across filesystems. Try
2541 brute force copy. */
2542 int old
= open (outfile
, O_RDONLY
);
2544 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2547 if (old
!= -1 && new != -1) {
2548 char buffer
[BUFSIZ
];
2550 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2552 if (write (new, buffer
, i
) != i
) {
2558 if (new != -1) { close (new); }
2559 if (old
!= -1) { close (old
); }
2560 (void) m_unlink (outfile
);
2563 /* The -file argument processing used path() to
2564 expand filename to absolute path. */
2565 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2567 admonish (NULL
, "unable to rename %s %s to %s",
2568 file
? "file" : "message", outfile
,
2574 admonish (NULL
, "unable to remove input file %s, "
2575 "not modifying it", infile
);
2576 (void) m_unlink (outfile
);
2585 /* No modifications and didn't need the tmp outfile. */
2586 (void) m_unlink (outfile
);
2589 /* Output is going to some file. Produce it whether or not
2590 there were modifications. */
2591 status
= output_message (ct
, outfile
);
2600 * parse_mime() does not set lf_line_endings in struct text, so use this
2601 * function to do it. It touches the parts the decodetypes identifies.
2604 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
) {
2605 switch (ct
->c_type
) {
2606 case CT_MULTIPART
: {
2607 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2610 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2611 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2617 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2618 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2620 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2625 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2626 if (ct
->c_ctparams
== NULL
) {
2627 if ((ct
->c_ctparams
= (struct text
*) mh_xcalloc (1, sizeof (struct text
))) == NULL
) {
2628 adios (NULL
, "out of memory");
2631 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2638 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2639 * use the standard MH backup file.
2642 remove_file (const char *file
) {
2644 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2645 int status
= system (rmm_command
);
2648 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2650 /* This is OK for a non-message file, it still uses the
2651 BACKUP_PREFIX form. The backup file will be in the same
2652 directory as file. */
2653 return rename (file
, m_backup (file
));
2659 * Output formatted message to user.
2662 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2667 va_start (args
, message
);
2668 fmt
= concat (filename
, partno
? " part " : ", ",
2669 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
2671 advertise (what
, NULL
, fmt
, args
);
2684 fprintf (stderr
, "\n");