]> diplodocus.org Git - nmh/blob - uip/mhfixmsg.c
Replaced reference to mktemp(3) with mkstemp(3) in
[nmh] / uip / mhfixmsg.c
1 /*
2 * mhfixmsg.c -- rewrite a message with various tranformations
3 *
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/mime.h>
11 #include <h/mhparse.h>
12 #include <h/utils.h>
13 #include <h/signals.h>
14 #include <signal.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #ifdef HAVE_ICONV
18 # include <iconv.h>
19 #endif
20
21 #define MHFIXMSG_SWITCHES \
22 X("decodetext 8bit|7bit", 0, DECODETEXTSW) \
23 X("nodecodetext", 0, NDECODETEXTSW) \
24 X("textcodeset", 0, TEXTCODESETSW) \
25 X("notextcodeset", 0, NTEXTCODESETSW) \
26 X("reformat", 0, REFORMATSW) \
27 X("noreformat", 0, NREFORMATSW) \
28 X("fixboundary", 0, FIXBOUNDARYSW) \
29 X("nofixboundary", 0, NFIXBOUNDARYSW) \
30 X("fixcte", 0, FIXCTESW) \
31 X("nofixcte", 0, NFIXCTESW) \
32 X("file file", 0, FILESW) \
33 X("outfile file", 0, OUTFILESW) \
34 X("rmmproc program", 0, RPROCSW) \
35 X("normmproc", 0, NRPRCSW) \
36 X("verbose", 0, VERBSW) \
37 X("noverbose", 0, NVERBSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
40
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(MHFIXMSG);
43 #undef X
44
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(MHFIXMSG, switches);
47 #undef X
48
49
50 int verbosw;
51 int debugsw; /* Needed by mhparse.c. */
52
53 #define quitser pipeser
54
55 /* mhparse.c */
56 extern char *tmp; /* directory to place tmp files */
57 extern int skip_mp_cte_check; /* flag to InitMultiPart */
58 extern int suppress_bogus_mp_content_warning; /* flag to InitMultiPart */
59 extern int bogus_mp_content; /* flag from InitMultiPart */
60 CT parse_mime (char *);
61 void reverse_parts (CT);
62
63 /* mhoutsbr.c */
64 int output_message (CT, char *);
65
66 /* mhshowsbr.c */
67 int show_content_aux (CT, int, int, char *, char *);
68
69 /* mhmisc.c */
70 void flush_errors (void);
71
72 /* mhfree.c */
73 extern CT *cts;
74 void freects_done (int) NORETURN;
75
76 /*
77 * static prototypes
78 */
79 typedef struct fix_transformations {
80 int fixboundary;
81 int fixcte;
82 int reformat;
83 int decodetext;
84 char *textcodeset;
85 } fix_transformations;
86
87 int mhfixmsgsbr (CT *, const fix_transformations *, char *);
88 static void reverse_alternative_parts (CT);
89 static int fix_boundary (CT *, int *);
90 static int get_multipart_boundary (CT, char **);
91 static int replace_boundary (CT, char *, const char *);
92 static char *update_attr (char *, const char *, const char *e);
93 static int fix_multipart_cte (CT, int *);
94 static int set_ce (CT, int);
95 static int ensure_text_plain (CT *, CT, int *);
96 static CT build_text_plain_part (CT);
97 static CT divide_part (CT);
98 static void copy_ctinfo (CI, CI);
99 static int decode_part (CT);
100 static int reformat_part (CT, char *, char *, char *, int);
101 static int charset_encoding (CT);
102 static CT build_multipart_alt (CT, CT, int, int);
103 static int boundary_in_content (FILE **, char *, const char *);
104 static void transfer_noncontent_headers (CT, CT);
105 static int set_ct_type (CT, int type, int subtype, int encoding);
106 static int decode_text_parts (CT, int, int *);
107 static int content_encoding (CT);
108 static int convert_codesets (CT, char *, int *);
109 static int convert_codeset (CT, char *, int *);
110 static int write_content (CT, char *, char *, int, int);
111 static int remove_file (char *);
112 static void report (char *, char *, char *, ...);
113 static char *upcase (char *);
114 static void pipeser (int);
115
116
117 int
118 main (int argc, char **argv) {
119 int msgnum;
120 char *cp, *file = NULL, *folder = NULL;
121 char *maildir, buf[100], *outfile = NULL;
122 char **argp, **arguments;
123 struct msgs_array msgs = { 0, 0, NULL };
124 struct msgs *mp = NULL;
125 CT *ctp;
126 FILE *fp;
127 int using_stdin = 0;
128 int status = OK;
129 fix_transformations fx;
130 fx.reformat = fx.fixcte = fx.fixboundary = 1;
131 fx.decodetext = CE_8BIT;
132 fx.textcodeset = NULL;
133
134 done = freects_done;
135
136 #ifdef LOCALE
137 setlocale(LC_ALL, "");
138 #endif
139 invo_name = r1bindex (argv[0], '/');
140
141 /* read user profile/context */
142 context_read();
143
144 arguments = getarguments (invo_name, argc, argv, 1);
145 argp = arguments;
146
147 /*
148 * Parse arguments
149 */
150 while ((cp = *argp++)) {
151 if (*cp == '-') {
152 switch (smatch (++cp, switches)) {
153 case AMBIGSW:
154 ambigsw (cp, switches);
155 done (1);
156 case UNKWNSW:
157 adios (NULL, "-%s unknown", cp);
158
159 case HELPSW:
160 snprintf (buf, sizeof buf, "%s [+folder] [msgs] [switches]",
161 invo_name);
162 print_help (buf, switches, 1);
163 done (0);
164 case VERSIONSW:
165 print_version(invo_name);
166 done (0);
167
168 case DECODETEXTSW:
169 if (! (cp = *argp++) || *cp == '-')
170 adios (NULL, "missing argument to %s", argp[-2]);
171 if (! strcasecmp (cp, "8bit")) {
172 fx.decodetext = CE_8BIT;
173 } else if (! strcasecmp (cp, "7bit")) {
174 fx.decodetext = CE_7BIT;
175 } else {
176 adios (NULL, "invalid argument to %s", argp[-2]);
177 }
178 continue;
179 case NDECODETEXTSW:
180 fx.decodetext = 0;
181 continue;
182 case TEXTCODESETSW:
183 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
184 adios (NULL, "missing argument to %s", argp[-2]);
185 fx.textcodeset = cp;
186 continue;
187 case NTEXTCODESETSW:
188 fx.textcodeset = 0;
189 continue;
190 case FIXBOUNDARYSW:
191 fx.fixboundary = 1;
192 continue;
193 case NFIXBOUNDARYSW:
194 fx.fixboundary = 0;
195 continue;
196 case FIXCTESW:
197 fx.fixcte = 1;
198 continue;
199 case NFIXCTESW:
200 fx.fixcte = 0;
201 continue;
202 case REFORMATSW:
203 fx.reformat = 1;
204 continue;
205 case NREFORMATSW:
206 fx.reformat = 0;
207 continue;
208
209 case FILESW:
210 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
211 adios (NULL, "missing argument to %s", argp[-2]);
212 file = *cp == '-' ? add (cp, NULL) : path (cp, TFILE);
213 continue;
214
215 case OUTFILESW:
216 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
217 adios (NULL, "missing argument to %s", argp[-2]);
218 outfile = *cp == '-' ? add (cp, NULL) : path (cp, TFILE);
219 continue;
220
221 case RPROCSW:
222 if (!(rmmproc = *argp++) || *rmmproc == '-')
223 adios (NULL, "missing argument to %s", argp[-2]);
224 continue;
225 case NRPRCSW:
226 rmmproc = NULL;
227 continue;
228
229 case VERBSW:
230 verbosw = 1;
231 continue;
232 case NVERBSW:
233 verbosw = 0;
234 continue;
235 }
236 }
237 if (*cp == '+' || *cp == '@') {
238 if (folder)
239 adios (NULL, "only one folder at a time!");
240 else
241 folder = pluspath (cp);
242 } else
243 app_msgarg(&msgs, cp);
244 }
245
246 SIGNAL (SIGQUIT, quitser);
247 SIGNAL (SIGPIPE, pipeser);
248
249 /*
250 * Read the standard profile setup
251 */
252 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
253 readconfig ((struct node **) 0, fp, cp, 0);
254 fclose (fp);
255 }
256
257 /*
258 * Check for storage directory. If specified,
259 * then store temporary files there. Else we
260 * store them in standard nmh directory.
261 */
262 if ((cp = context_find (nmhstorage)) && *cp)
263 tmp = concat (cp, "/", invo_name, NULL);
264 else
265 tmp = add (m_maildir (invo_name), NULL);
266
267 suppress_bogus_mp_content_warning = skip_mp_cte_check = 1;
268
269 if (! context_find ("path"))
270 free (path ("./", TFOLDER));
271
272 if (file && msgs.size)
273 adios (NULL, "cannot specify msg and file at same time!");
274
275 /*
276 * check if message is coming from file
277 */
278 if (file) {
279 /* If file is stdin, create a tmp file name before parse_mime()
280 has a chance, because it might put in on a different
281 filesystem than the output file. Instead, put it in the
282 user's preferred tmp directory. */
283 CT ct;
284
285 if (! strcmp ("-", file)) {
286 int fd;
287 char *cp;
288
289 using_stdin = 1;
290
291 if ((cp = m_mktemp2 (tmp, invo_name, &fd, NULL)) == NULL) {
292 adios (NULL, "unable to create temporary file");
293 } else {
294 free (file);
295 file = add (cp, NULL);
296 chmod (file, 0600);
297 cpydata (STDIN_FILENO, fd, "-", file);
298 }
299
300 if (close (fd)) {
301 unlink (file);
302 adios (NULL, "failed to write temporary file");
303 }
304 }
305
306 if (! (cts = (CT *) calloc ((size_t) 2, sizeof *cts)))
307 adios (NULL, "out of memory");
308 ctp = cts;
309
310 if ((ct = parse_mime (file)))
311 *ctp++ = ct;
312 } else {
313 /*
314 * message(s) are coming from a folder
315 */
316 CT ct;
317
318 if (! msgs.size)
319 app_msgarg(&msgs, "cur");
320 if (! folder)
321 folder = getfolder (1);
322 maildir = m_maildir (folder);
323
324 if (chdir (maildir) == NOTOK)
325 adios (maildir, "unable to change directory to");
326
327 /* read folder and create message structure */
328 if (! (mp = folder_read (folder)))
329 adios (NULL, "unable to read folder %s", folder);
330
331 /* check for empty folder */
332 if (mp->nummsg == 0)
333 adios (NULL, "no messages in %s", folder);
334
335 /* parse all the message ranges/sequences and set SELECTED */
336 for (msgnum = 0; msgnum < msgs.size; msgnum++)
337 if (! m_convert (mp, msgs.msgs[msgnum]))
338 done (1);
339 seq_setprev (mp); /* set the previous-sequence */
340
341 if (! (cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof *cts)))
342 adios (NULL, "out of memory");
343 ctp = cts;
344
345 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
346 if (is_selected(mp, msgnum)) {
347 char *msgnam;
348
349 msgnam = m_name (msgnum);
350 if ((ct = parse_mime (msgnam)))
351 *ctp++ = ct;
352 }
353 }
354
355 /*
356 * This is a hack. If we are using an external rmmproc,
357 * then save the current folder to the context file,
358 * so the external rmmproc will remove files from the correct
359 * directory. This should be moved to folder_delmsgs().
360 */
361 if (rmmproc) {
362 context_replace (pfolder, folder);/* update current folder */
363 seq_setcur (mp, mp->hghsel); /* update current message */
364 seq_save (mp); /* synchronize sequences */
365 context_save (); /* save the context file */
366 fflush (stdout);
367 }
368 }
369
370 if (*cts) {
371 for (ctp = cts; *ctp; ++ctp) {
372 status += mhfixmsgsbr (ctp, &fx, outfile);
373
374 if (using_stdin) {
375 unlink (file);
376
377 if (! outfile) {
378 /* Just calling m_backup() unlinks the backup file. */
379 (void) m_backup (file);
380 }
381 }
382 }
383 } else {
384 status = 1;
385 }
386
387 free (outfile);
388 free (tmp);
389 free (file);
390
391 /* done is freects_done, which will clean up all of cts. */
392 done (status);
393 return NOTOK;
394 }
395
396
397 int
398 mhfixmsgsbr (CT *ctp, const fix_transformations *fx, char *outfile) {
399 /* Store input filename in case one of the transformations, i.e.,
400 fix_boundary(), rewrites to a tmp file. */
401 char *input_filename = add ((*ctp)->c_file, NULL);
402 int modify_inplace = 0;
403 int message_mods = 0;
404 int status = OK;
405
406 if (outfile == NULL) {
407 modify_inplace = 1;
408
409 if ((*ctp)->c_file) {
410 outfile = add (m_mktemp2 (tmp, invo_name, NULL, NULL), NULL);
411 } else {
412 adios (NULL, "missing both input and output filenames\n");
413 }
414 }
415
416 reverse_alternative_parts (*ctp);
417 if (status == OK && fx->fixboundary) {
418 status = fix_boundary (ctp, &message_mods);
419 }
420 if (status == OK && fx->fixcte) {
421 status = fix_multipart_cte (*ctp, &message_mods);
422 }
423 if (status == OK && fx->reformat) {
424 status = ensure_text_plain (ctp, NULL, &message_mods);
425 }
426 if (status == OK && fx->decodetext) {
427 status = decode_text_parts (*ctp, fx->decodetext, &message_mods);
428 }
429 if (status == OK && fx->textcodeset != NULL) {
430 status = convert_codesets (*ctp, fx->textcodeset, &message_mods);
431 }
432
433 if (! (*ctp)->c_umask) {
434 /* Set the umask for the contents file. This currently
435 isn't used but just in case it is in the future. */
436 struct stat st;
437
438 if (stat ((*ctp)->c_file, &st) != NOTOK) {
439 (*ctp)->c_umask = ~(st.st_mode & 0777);
440 } else {
441 (*ctp)->c_umask = ~m_gmprot();
442 }
443 }
444
445 /*
446 * Write the content to a file
447 */
448 if (status == OK) {
449 status = write_content (*ctp, input_filename, outfile, modify_inplace,
450 message_mods);
451 } else if (! modify_inplace) {
452 /* Something went wrong. Output might be expected, such
453 as if this were run as a filter. Just copy the input
454 to the output. */
455 int in = open (input_filename, O_RDONLY);
456 int out = strcmp (outfile, "-")
457 ? open (outfile, O_WRONLY | O_CREAT, m_gmprot ())
458 : STDOUT_FILENO;
459
460 if (in != -1 && out != -1) {
461 cpydata (in, out, input_filename, outfile);
462 } else {
463 status = NOTOK;
464 }
465
466 close (out);
467 close (in);
468 }
469
470 if (modify_inplace) {
471 free (outfile);
472 outfile = NULL;
473 }
474
475 free (input_filename);
476
477 return status;
478 }
479
480
481 /* parse_mime() arranges alternates in reverse (priority) order, so
482 reverse them back. This will put a text/plain part at the front of
483 a multipart/alternative part, for example, where it belongs. */
484 static void
485 reverse_alternative_parts (CT ct) {
486 if (ct->c_type == CT_MULTIPART) {
487 struct multipart *m = (struct multipart *) ct->c_ctparams;
488 struct part *part;
489
490 if (ct->c_subtype == MULTI_ALTERNATE) {
491 reverse_parts (ct);
492 }
493
494 /* And call recursively on each part of a multipart. */
495 for (part = m->mp_parts; part; part = part->mp_next) {
496 reverse_alternative_parts (part->mp_part);
497 }
498 }
499 }
500
501
502 static int
503 fix_boundary (CT *ct, int *message_mods) {
504 struct multipart *mp;
505 int status = OK;
506
507 if (bogus_mp_content) {
508 mp = (struct multipart *) (*ct)->c_ctparams;
509
510 /*
511 * 1) Get boundary at end of part.
512 * 2) Get boundary at beginning of part and compare to the end-of-part
513 * boundary.
514 * 3) Write out contents of ct to tmp file, replacing boundary in
515 * header with boundary from part. Set c_unlink to 1.
516 * 4) Free ct.
517 * 5) Call parse_mime() on the tmp file, replacing ct.
518 */
519
520 if (mp && mp->mp_start) {
521 char *part_boundary;
522
523 if (get_multipart_boundary (*ct, &part_boundary) == OK) {
524 char *fixed;
525
526 if ((fixed = m_mktemp2 (tmp, invo_name, NULL, &(*ct)->c_fp))) {
527 if (replace_boundary (*ct, fixed, part_boundary) == OK) {
528 char *filename = add ((*ct)->c_file, NULL);
529
530 free_content (*ct);
531 *ct = parse_mime (fixed);
532 (*ct)->c_unlink = 1;
533
534 ++*message_mods;
535 if (verbosw) {
536 report (NULL, filename, "fix multipart boundary");
537 }
538 free (filename);
539 } else {
540 advise (NULL, "unable to replace broken boundary");
541 status = NOTOK;
542 }
543 } else {
544 advise (NULL, "unable to create temporary file");
545 status = NOTOK;
546 }
547
548 free (part_boundary);
549 }
550 }
551 }
552
553 return status;
554 }
555
556
557 static int
558 get_multipart_boundary (CT ct, char **part_boundary) {
559 char buffer[BUFSIZ];
560 char *end_boundary = NULL;
561 off_t begin = (off_t) ct->c_end > (off_t) (ct->c_begin + sizeof buffer)
562 ? (off_t) (ct->c_end - sizeof buffer)
563 : (off_t) ct->c_begin;
564 size_t bytes_read;
565 int status = OK;
566
567 /* This will fail if the boundary spans fread() calls. BUFSIZ should
568 be big enough, even if it's just 1024, to make that unlikely. */
569
570 /* free_content() will close ct->c_fp. */
571 if (! ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
572 advise (ct->c_file, "unable to open for reading");
573 return NOTOK;
574 }
575
576 /* Get boundary at end of multipart. */
577 while (begin >= (off_t) ct->c_begin) {
578 fseeko (ct->c_fp, begin, SEEK_SET);
579 while ((bytes_read = fread (buffer, 1, sizeof buffer, ct->c_fp)) > 0) {
580 char *end = buffer + bytes_read - 1;
581 char *cp;
582
583 if ((cp = rfind_str (buffer, bytes_read, "--"))) {
584 /* Trim off trailing "--" and anything beyond. */
585 *cp-- = '\0';
586 if ((end = rfind_str (buffer, cp - buffer, "\n"))) {
587 if (strlen (end) > 3 && *end++ == '\n' &&
588 *end++ == '-' && *end++ == '-') {
589 end_boundary = add (end, NULL);
590 break;
591 }
592 }
593 }
594 }
595
596 if (! end_boundary && begin > (off_t) (ct->c_begin + sizeof buffer)) {
597 begin -= sizeof buffer;
598 } else {
599 break;
600 }
601 }
602
603 /* Get boundary at beginning of multipart. */
604 if (end_boundary) {
605 fseeko (ct->c_fp, ct->c_begin, SEEK_SET);
606 while ((bytes_read = fread (buffer, 1, sizeof buffer, ct->c_fp)) > 0) {
607 if (bytes_read >= strlen (end_boundary)) {
608 char *cp = find_str (buffer, bytes_read, end_boundary);
609
610 if (cp && cp - buffer >= 2 && *--cp == '-' &&
611 *--cp == '-' && (cp > buffer && *--cp == '\n')) {
612 break;
613 }
614 /* Else the start and end boundaries didn't match, or
615 the start boundary doesn't begin with "\n--" (or
616 "--" if at the beginning of buffer). Keep trying. */
617 } else {
618 status = NOTOK;
619 }
620 }
621 }
622
623 if (status == OK) {
624 *part_boundary = end_boundary;
625 } else {
626 *part_boundary = NULL;
627 free (end_boundary);
628 }
629
630 return status;
631 }
632
633
634 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
635 static int
636 replace_boundary (CT ct, char *file, const char *boundary) {
637 FILE *fpin, *fpout;
638 int compnum, state;
639 char buf[BUFSIZ], name[NAMESZ];
640 char *np, *vp;
641 m_getfld_state_t gstate = 0;
642 int status = OK;
643
644 if (ct->c_file == NULL) {
645 advise (NULL, "missing input filename");
646 return NOTOK;
647 }
648
649 if ((fpin = fopen (ct->c_file, "r")) == NULL) {
650 advise (ct->c_file, "unable to open for reading");
651 return NOTOK;
652 }
653
654 if ((fpout = fopen (file, "w")) == NULL) {
655 fclose (fpin);
656 advise (file, "unable to open for writing");
657 return NOTOK;
658 }
659
660 for (compnum = 1;;) {
661 int bufsz = (int) sizeof buf;
662
663 switch (state = m_getfld (&gstate, name, buf, &bufsz, fpin)) {
664 case FLD:
665 case FLDPLUS:
666 compnum++;
667
668 /* get copies of the buffers */
669 np = add (name, NULL);
670 vp = add (buf, NULL);
671
672 /* if necessary, get rest of field */
673 while (state == FLDPLUS) {
674 bufsz = sizeof buf;
675 state = m_getfld (&gstate, name, buf, &bufsz, fpin);
676 vp = add (buf, vp); /* add to previous value */
677 }
678
679 if (strcasecmp (TYPE_FIELD, np)) {
680 fprintf (fpout, "%s:%s", np, vp);
681 } else {
682 char *new_boundary = update_attr (vp, "boundary=", boundary);
683
684 fprintf (fpout, "%s:%s\n", np, new_boundary);
685 free (new_boundary);
686 }
687
688 free (vp);
689 free (np);
690
691 continue;
692
693 case BODY:
694 fputs ("\n", fpout);
695 /* buf will have a terminating NULL, skip it. */
696 fwrite (buf, 1, bufsz-1, fpout);
697 continue;
698
699 case FILEEOF:
700 break;
701
702 case LENERR:
703 case FMTERR:
704 advise (NULL, "message format error in component #%d", compnum);
705 status = NOTOK;
706 break;
707
708 default:
709 advise (NULL, "getfld() returned %d", state);
710 status = NOTOK;
711 break;
712 }
713
714 break;
715 }
716
717 m_getfld_state_destroy (&gstate);
718 fclose (fpout);
719 fclose (fpin);
720
721 return status;
722 }
723
724
725 /* Change the value of a name=value pair in a header field body.
726 If the name isn't there, append them. In any case, a new
727 string will be allocated and must be free'd by the caller.
728 Trims any trailing newlines. */
729 static char *
730 update_attr (char *body, const char *name, const char *value) {
731 char *bp = nmh_strcasestr (body, name);
732 char *new_body;
733
734 if (bp) {
735 char *other_attrs = strchr (bp, ';');
736
737 *(bp + strlen (name)) = '\0';
738 new_body = concat (body, "\"", value, "\"", NULL);
739
740 if (other_attrs) {
741 char *cp;
742
743 /* Trim any trailing newlines. */
744 for (cp = &other_attrs[strlen (other_attrs) - 1];
745 cp > other_attrs && *cp == '\n';
746 *cp-- = '\0') continue;
747 new_body = add (other_attrs, new_body);
748 }
749 } else {
750 char *cp;
751
752 /* Append name/value pair, after first removing a final newline
753 and (extraneous) semicolon. */
754 if (*(cp = &body[strlen (body) - 1]) == '\n') *cp = '\0';
755 if (*(cp = &body[strlen (body) - 1]) == ';') *cp = '\0';
756 new_body = concat (body, "; ", name, "\"", value, "\"", NULL);
757 }
758
759 return new_body;
760 }
761
762
763 static int
764 fix_multipart_cte (CT ct, int *message_mods) {
765 int status = OK;
766
767 if (ct->c_type == CT_MULTIPART) {
768 struct multipart *m;
769 struct part *part;
770
771 if (ct->c_encoding != CE_7BIT && ct->c_encoding != CE_8BIT &&
772 ct->c_encoding != CE_BINARY) {
773 HF hf;
774
775 for (hf = ct->c_first_hf; hf; hf = hf->next) {
776 char *name = hf->name;
777 for (; *name && isspace ((unsigned char) *name); ++name) {
778 continue;
779 }
780
781 if (! strncasecmp (name, ENCODING_FIELD,
782 strlen (ENCODING_FIELD))) {
783 char *prefix = "Nmh-REPLACED-INVALID-";
784 HF h = mh_xmalloc (sizeof *h);
785
786 h->name = add (hf->name, NULL);
787 h->hf_encoding = hf->hf_encoding;
788 h->next = hf->next;
789 hf->next = h;
790
791 /* Retain old header but prefix its name. */
792 free (hf->name);
793 hf->name = concat (prefix, h->name, NULL);
794
795 ++*message_mods;
796 if (verbosw) {
797 char *encoding = cpytrim (hf->value);
798 report (ct->c_partno, ct->c_file,
799 "replace Content-Transfer-Encoding of %s "
800 "with 8 bit", encoding);
801 free (encoding);
802 }
803
804 h->value = add (" 8bit\n", NULL);
805
806 /* Don't need to warn for multiple C-T-E header
807 fields, parse_mime() already does that. But
808 if there are any, fix them all as necessary. */
809 hf = h;
810 }
811 }
812
813 set_ce (ct, CE_8BIT);
814 }
815
816 m = (struct multipart *) ct->c_ctparams;
817 for (part = m->mp_parts; part; part = part->mp_next) {
818 if (fix_multipart_cte (part->mp_part, message_mods) != OK) {
819 status = NOTOK;
820 break;
821 }
822 }
823 }
824
825 return status;
826 }
827
828
829 static int
830 set_ce (CT ct, int encoding) {
831 const char *ce = ce_str (encoding);
832 const struct str2init *ctinit = get_ce_method (ce);
833
834 if (ctinit) {
835 char *cte = concat (" ", ce, "\n", NULL);
836 int found_cte = 0;
837 HF hf;
838 /* Decoded contents might be in ct->c_cefile.ce_file, if the
839 caller is decode_text_parts (). Save because we'll
840 overwrite below. */
841 struct cefile decoded_content_info = ct->c_cefile;
842
843 ct->c_encoding = encoding;
844
845 ct->c_ctinitfnx = ctinit->si_init;
846 /* This will assign ct->c_cefile with an all-0 struct, which
847 is what we want. */
848 (*ctinit->si_init) (ct);
849 /* After returning, the caller should set
850 ct->c_cefile.ce_file to the name of the file containing
851 the contents. */
852
853 /* Restore the cefile. */
854 ct->c_cefile = decoded_content_info;
855
856 /* Update/add Content-Transfer-Encoding header field. */
857 for (hf = ct->c_first_hf; hf; hf = hf->next) {
858 if (! strcasecmp (ENCODING_FIELD, hf->name)) {
859 found_cte = 1;
860 free (hf->value);
861 hf->value = cte;
862 }
863 }
864 if (! found_cte) {
865 add_header (ct, add (ENCODING_FIELD, NULL), cte);
866 }
867
868 /* Update c_celine. It's used only by mhlist -debug. */
869 free (ct->c_celine);
870 ct->c_celine = add (cte, NULL);
871
872 return OK;
873 } else {
874 return NOTOK;
875 }
876 }
877
878
879 /* Make sure each text part has a corresponding text/plain part. */
880 static int
881 ensure_text_plain (CT *ct, CT parent, int *message_mods) {
882 int status = OK;
883
884 switch ((*ct)->c_type) {
885 case CT_TEXT: {
886 int has_text_plain = 0;
887
888 /* Nothing to do for text/plain. */
889 if ((*ct)->c_subtype == TEXT_PLAIN) return OK;
890
891 if (parent && parent->c_type == CT_MULTIPART &&
892 parent->c_subtype == MULTI_ALTERNATE) {
893 struct multipart *mp = (struct multipart *) parent->c_ctparams;
894 struct part *part;
895 int new_subpart_number = 1;
896
897 /* See if there is a sibling text/plain. */
898 for (part = mp->mp_parts; part; part = part->mp_next) {
899 ++new_subpart_number;
900 if (part->mp_part->c_type == CT_TEXT &&
901 part->mp_part->c_subtype == TEXT_PLAIN) {
902 has_text_plain = 1;
903 break;
904 }
905 }
906
907 if (! has_text_plain) {
908 /* Parent is a multipart/alternative. Insert a new
909 text/plain subpart. */
910 struct part *new_part = mh_xmalloc (sizeof *new_part);
911
912 if ((new_part->mp_part = build_text_plain_part (*ct))) {
913 char buffer[16];
914 snprintf (buffer, sizeof buffer, "%d", new_subpart_number);
915
916 new_part->mp_next = mp->mp_parts;
917 mp->mp_parts = new_part;
918 new_part->mp_part->c_partno =
919 concat (parent->c_partno ? parent->c_partno : "1", ".",
920 buffer, NULL);
921
922 ++*message_mods;
923 if (verbosw) {
924 report (parent->c_partno, parent->c_file,
925 "insert text/plain part");
926 }
927 } else {
928 free_content (new_part->mp_part);
929 free (new_part);
930 status = NOTOK;
931 }
932 }
933 } else {
934 /* Slip new text/plain part into a new multipart/alternative. */
935 CT tp_part = build_text_plain_part (*ct);
936 CT mp_alt = build_multipart_alt (*ct, tp_part, CT_MULTIPART,
937 MULTI_ALTERNATE);
938 struct multipart *mp = (struct multipart *) mp_alt->c_ctparams;
939
940 if (mp && mp->mp_parts && (mp->mp_parts->mp_part = tp_part)) {
941 /* Make the new multipart/alternative the parent. */
942 *ct = mp_alt;
943
944 ++*message_mods;
945 if (verbosw) {
946 report ((*ct)->c_partno, (*ct)->c_file,
947 "insert text/plain part");
948 }
949 } else {
950 free_content (tp_part);
951
952 /* Undo enough of what build_multipart_alt() did so
953 that free_content() can be called on mp_alt. */
954 mp->mp_parts->mp_part = NULL;
955 mp->mp_parts->mp_next->mp_part = NULL;
956 free_content (mp_alt);
957 status = NOTOK;
958 }
959 }
960 break;
961 }
962
963 case CT_MULTIPART: {
964 struct multipart *mp = (struct multipart *) (*ct)->c_ctparams;
965 struct part *part;
966
967 for (part = mp->mp_parts; status == OK && part; part = part->mp_next) {
968 if ((*ct)->c_type == CT_MULTIPART) {
969 status = ensure_text_plain (&part->mp_part, *ct, message_mods);
970 }
971 }
972 break;
973 }
974
975 case CT_MESSAGE:
976 if ((*ct)->c_subtype == MESSAGE_EXTERNAL) {
977 struct exbody *e;
978
979 e = (struct exbody *) (*ct)->c_ctparams;
980 status = ensure_text_plain (&e->eb_content, *ct, message_mods);
981 }
982 break;
983 }
984
985 return status;
986 }
987
988
989 static CT
990 build_text_plain_part (CT encoded_part) {
991 CT tp_part = divide_part (encoded_part);
992 char *tmp_plain_file = NULL;
993
994 if (decode_part (tp_part) == OK) {
995 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
996 contains the decoded contents. And the decoding function, such
997 as openQuoted, will have set ...->ce_unlink to 1 so that it will
998 be unlinked by free_content (). */
999 tmp_plain_file = add (m_mktemp2 (tmp, invo_name, NULL, NULL), NULL);
1000 if (reformat_part (tp_part, tmp_plain_file,
1001 tp_part->c_ctinfo.ci_type,
1002 tp_part->c_ctinfo.ci_subtype,
1003 tp_part->c_type) == OK) {
1004 return tp_part;
1005 }
1006 }
1007
1008 free_content (tp_part);
1009 unlink (tmp_plain_file);
1010 free (tmp_plain_file);
1011
1012 return NULL;
1013 }
1014
1015
1016 static CT
1017 divide_part (CT ct) {
1018 CT new_part;
1019
1020 if ((new_part = (CT) calloc (1, sizeof *new_part)) == NULL)
1021 adios (NULL, "out of memory");
1022
1023 /* Just copy over what is needed for decoding. c_vrsn and
1024 c_celine aren't necessary. */
1025 new_part->c_file = add (ct->c_file, NULL);
1026 new_part->c_begin = ct->c_begin;
1027 new_part->c_end = ct->c_end;
1028 copy_ctinfo (&new_part->c_ctinfo, &ct->c_ctinfo);
1029 new_part->c_type = ct->c_type;
1030 new_part->c_cefile = ct->c_cefile;
1031 new_part->c_encoding = ct->c_encoding;
1032 new_part->c_ctinitfnx = ct->c_ctinitfnx;
1033 new_part->c_ceopenfnx = ct->c_ceopenfnx;
1034 new_part->c_ceclosefnx = ct->c_ceclosefnx;
1035 new_part->c_cesizefnx = ct->c_cesizefnx;
1036
1037 /* c_ctline is used by reformat__part(), so it can preserve
1038 anything after the type/subtype. */
1039 new_part->c_ctline = add (ct->c_ctline, NULL);
1040
1041 return new_part;
1042 }
1043
1044
1045 static void
1046 copy_ctinfo (CI dest, CI src) {
1047 char **s_ap, **d_ap, **s_vp, **d_vp;
1048
1049 dest->ci_type = src->ci_type ? add (src->ci_type, NULL) : NULL;
1050 dest->ci_subtype = src->ci_subtype ? add (src->ci_subtype, NULL) : NULL;
1051
1052 for (s_ap = src->ci_attrs, d_ap = dest->ci_attrs,
1053 s_vp = src->ci_values, d_vp = dest->ci_values;
1054 *s_ap;
1055 ++s_ap, ++d_ap, ++s_vp, ++d_vp) {
1056 *d_ap = add (*s_ap, NULL);
1057 *d_vp = *s_vp;
1058 }
1059 *d_ap = NULL;
1060
1061 dest->ci_comment = src->ci_comment ? add (src->ci_comment, NULL) : NULL;
1062 dest->ci_magic = src->ci_magic ? add (src->ci_magic, NULL) : NULL;
1063 }
1064
1065
1066 static int
1067 decode_part (CT ct) {
1068 char *tmp_decoded;
1069 int status;
1070
1071 tmp_decoded = add (m_mktemp2 (tmp, invo_name, NULL, NULL), NULL);
1072 /* The following call will load ct->c_cefile.ce_file with the tmp
1073 filename of the decoded content. tmp_decoded will contain the
1074 encoded output, get rid of that. */
1075 status = output_message (ct, tmp_decoded);
1076 unlink (tmp_decoded);
1077 free (tmp_decoded);
1078
1079 return status;
1080 }
1081
1082
1083 /* Some of the arguments aren't really needed now, but maybe will
1084 be in the future for other than text types. */
1085 static int
1086 reformat_part (CT ct, char *file, char *type, char *subtype, int c_type) {
1087 int output_subtype, output_encoding;
1088 char *cp, *cf;
1089 int status;
1090
1091 /* Hacky: this redirects the output from whatever command is used
1092 to show the part to a file. So, the user can't have any output
1093 redirection in that command.
1094 Could show_multi() in mhshowsbr.c avoid this? */
1095
1096 /* Check for invo_name-format-type/subtype. */
1097 cp = concat (invo_name, "-format-", type, "/", subtype, NULL);
1098 if ((cf = context_find (cp)) && *cf != '\0') {
1099 if (strchr (cf, '>')) {
1100 free (cp);
1101 advise (NULL, "'>' prohibited in \"%s\",\nplease fix your "
1102 "%s-format-%s/%s profile entry", cf, invo_name, type,
1103 subtype);
1104 return NOTOK;
1105 }
1106 } else {
1107 free (cp);
1108
1109 /* Check for invo_name-format-type. */
1110 cp = concat (invo_name, "-format-", type, NULL);
1111 if (! (cf = context_find (cp)) || *cf == '\0') {
1112 free (cp);
1113 if (verbosw) {
1114 advise (NULL, "Don't know how to convert %s, there is no "
1115 "%s-format-%s/%s profile entry",
1116 ct->c_file, invo_name, type, subtype);
1117 }
1118 return NOTOK;
1119 }
1120
1121 if (strchr (cf, '>')) {
1122 free (cp);
1123 advise (NULL, "'>' prohibited in \"%s\"", cf);
1124 return NOTOK;
1125 }
1126 }
1127 free (cp);
1128
1129 cp = concat (cf, " >", file, NULL);
1130 status = show_content_aux (ct, 1, 0, cp, NULL);
1131 free (cp);
1132
1133 /* Unlink decoded content tmp file and free its filename to avoid
1134 leaks. The file stream should already have been closed. */
1135 if (ct->c_cefile.ce_unlink) {
1136 unlink (ct->c_cefile.ce_file);
1137 free (ct->c_cefile.ce_file);
1138 ct->c_cefile.ce_file = NULL;
1139 ct->c_cefile.ce_unlink = 0;
1140 }
1141
1142 if (c_type == CT_TEXT) {
1143 output_subtype = TEXT_PLAIN;
1144 } else {
1145 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1146 output_subtype = 0;
1147 }
1148 output_encoding = charset_encoding (ct);
1149
1150 if (set_ct_type (ct, c_type, output_subtype, output_encoding) == OK) {
1151 ct->c_cefile.ce_file = file;
1152 ct->c_cefile.ce_unlink = 1;
1153 } else {
1154 ct->c_cefile.ce_unlink = 0;
1155 status = NOTOK;
1156 }
1157
1158 return status;
1159 }
1160
1161
1162 /* Identifies 7bit or 8bit content based on charset, if specified. */
1163 static int
1164 charset_encoding (CT ct) {
1165 int encoding = CE_8BIT;
1166 CI ctinfo = &ct->c_ctinfo;
1167 char **ap, **vp;
1168
1169 for (ap = ctinfo->ci_attrs, vp = ctinfo->ci_values; *ap; ++ap, ++vp) {
1170 if (! strcasecmp (*ap, "charset")) {
1171 /* norm_charmap() is case sensitive. */
1172 char *ch = upcase (*vp);
1173
1174 if (! strcmp (norm_charmap (ch), "US-ASCII")) encoding = CE_7BIT;
1175 free (ch);
1176 break;
1177 }
1178 }
1179
1180 return encoding;
1181 }
1182
1183
1184 static CT
1185 build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) {
1186 char *boundary_prefix = "----=_nmh-multipart";
1187 char *boundary = concat (boundary_prefix, first_alt->c_partno, NULL);
1188 char *boundary_indicator = "; boundary=";
1189 char *typename, *subtypename, *name;
1190 CT ct;
1191 struct part *p;
1192 struct multipart *m;
1193 char *cp;
1194 const struct str2init *ctinit;
1195
1196 if ((ct = (CT) calloc (1, sizeof *ct)) == NULL)
1197 adios (NULL, "out of memory");
1198
1199 /* Set up the multipart/alternative part. These fields of *ct were
1200 initialized to 0 by calloc():
1201 c_fp, c_unlink, c_begin, c_end,
1202 c_vrsn, c_ctline, c_celine,
1203 c_id, c_descr, c_dispo, c_partno,
1204 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1205 c_cefile, c_encoding,
1206 c_digested, c_digest[16], c_ctexbody,
1207 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1208 c_umask, c_pid, c_rfc934,
1209 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1210 */
1211
1212 ct->c_file = add (first_alt->c_file, NULL);
1213 ct->c_type = type;
1214 ct->c_subtype = subtype;
1215
1216 ctinit = get_ct_init (ct->c_type);
1217
1218 typename = ct_type_str (type);
1219 subtypename = ct_subtype_str (type, subtype);
1220
1221 {
1222 int serial = 0;
1223 int found_boundary = 1;
1224
1225 while (found_boundary && serial < 1000000) {
1226 found_boundary = 0;
1227
1228 /* Ensure that the boundary doesn't appear in the decoded
1229 content. */
1230 if (new_part->c_cefile.ce_file) {
1231 if ((found_boundary =
1232 boundary_in_content (&new_part->c_cefile.ce_fp,
1233 new_part->c_cefile.ce_file,
1234 boundary)) == -1) {
1235 return NULL;
1236 }
1237 }
1238
1239 /* Ensure that the boundary doesn't appear in the encoded
1240 content. */
1241 if (! found_boundary && new_part->c_file) {
1242 if ((found_boundary = boundary_in_content (&new_part->c_fp,
1243 new_part->c_file,
1244 boundary)) == -1) {
1245 return NULL;
1246 }
1247 }
1248
1249 if (found_boundary) {
1250 /* Try a slightly different boundary. */
1251 char buffer2[16];
1252
1253 free (boundary);
1254 ++serial;
1255 snprintf (buffer2, sizeof buffer2, "%d", serial);
1256 boundary =
1257 concat (boundary_prefix,
1258 first_alt->c_partno ? first_alt->c_partno : "",
1259 "-", buffer2, NULL);
1260 }
1261 }
1262
1263 if (found_boundary) {
1264 advise (NULL, "giving up trying to find a unique boundary");
1265 return NULL;
1266 }
1267 }
1268
1269 name = concat (" ", typename, "/", subtypename, boundary_indicator, "\"",
1270 boundary, "\"", NULL);
1271
1272 /* Load c_first_hf and c_last_hf. */
1273 transfer_noncontent_headers (first_alt, ct);
1274 add_header (ct, add (TYPE_FIELD, NULL), concat (name, "\n", NULL));
1275 free (name);
1276
1277 /* Load c_partno. */
1278 if (first_alt->c_partno) {
1279 ct->c_partno = add (first_alt->c_partno, NULL);
1280 free (first_alt->c_partno);
1281 first_alt->c_partno = concat (ct->c_partno, ".1", NULL);
1282 new_part->c_partno = concat (ct->c_partno, ".2", NULL);
1283 } else {
1284 first_alt->c_partno = add ("1", NULL);
1285 new_part->c_partno = add ("2", NULL);
1286 }
1287
1288 if (ctinit) {
1289 ct->c_ctinfo.ci_type = add (typename, NULL);
1290 ct->c_ctinfo.ci_subtype = add (subtypename, NULL);
1291 }
1292
1293 name = concat (" ", typename, "/", subtypename, boundary_indicator,
1294 boundary, NULL);
1295 if ((cp = strstr (name, boundary_indicator))) {
1296 ct->c_ctinfo.ci_attrs[0] = name;
1297 ct->c_ctinfo.ci_attrs[1] = NULL;
1298 /* ci_values don't get free'd, so point into ci_attrs. */
1299 ct->c_ctinfo.ci_values[0] = cp + strlen (boundary_indicator);
1300 }
1301
1302 p = (struct part *) mh_xmalloc (sizeof *p);
1303 p->mp_next = (struct part *) mh_xmalloc (sizeof *p->mp_next);
1304 p->mp_next->mp_next = NULL;
1305 p->mp_next->mp_part = first_alt;
1306
1307 if ((m = (struct multipart *) calloc (1, sizeof (struct multipart))) ==
1308 NULL)
1309 adios (NULL, "out of memory");
1310 m->mp_start = concat (boundary, "\n", NULL);
1311 m->mp_stop = concat (boundary, "--\n", NULL);
1312 m->mp_parts = p;
1313 ct->c_ctparams = (void *) m;
1314
1315 free (boundary);
1316
1317 return ct;
1318 }
1319
1320
1321 /* Check that the boundary does not appear in the content. */
1322 static int
1323 boundary_in_content (FILE **fp, char *file, const char *boundary) {
1324 char buffer[BUFSIZ];
1325 size_t bytes_read;
1326 int found_boundary = 0;
1327
1328 /* free_content() will close *fp if we fopen it here. */
1329 if (! *fp && (*fp = fopen (file, "r")) == NULL) {
1330 advise (file, "unable to open %s for reading", file);
1331 return NOTOK;
1332 }
1333
1334 fseeko (*fp, 0L, SEEK_SET);
1335 while ((bytes_read = fread (buffer, 1, sizeof buffer, *fp)) > 0) {
1336 if (find_str (buffer, bytes_read, boundary)) {
1337 found_boundary = 1;
1338 break;
1339 }
1340 }
1341
1342 return found_boundary;
1343 }
1344
1345
1346 /* Remove all non-Content headers. */
1347 static void
1348 transfer_noncontent_headers (CT old, CT new) {
1349 HF hp, hp_prev;
1350
1351 hp_prev = hp = old->c_first_hf;
1352 while (hp) {
1353 HF next = hp->next;
1354
1355 if (strncasecmp (XXX_FIELD_PRF, hp->name, strlen (XXX_FIELD_PRF))) {
1356 if (hp == old->c_last_hf) {
1357 if (hp == old->c_first_hf) {
1358 old->c_last_hf = old->c_first_hf = NULL;
1359 } else {
1360 hp_prev->next = NULL;
1361 old->c_last_hf = hp_prev;
1362 }
1363 } else {
1364 if (hp == old->c_first_hf) {
1365 old->c_first_hf = next;
1366 } else {
1367 hp_prev->next = next;
1368 }
1369 }
1370
1371 /* Put node hp in the new CT. */
1372 if (new->c_first_hf == NULL) {
1373 new->c_first_hf = hp;
1374 } else {
1375 new->c_last_hf->next = hp;
1376 }
1377 new->c_last_hf = hp;
1378 } else {
1379 /* A Content- header, leave in old. */
1380 hp_prev = hp;
1381 }
1382
1383 hp = next;
1384 }
1385 }
1386
1387
1388 static int
1389 set_ct_type (CT ct, int type, int subtype, int encoding) {
1390 char *typename = ct_type_str (type);
1391 char *subtypename = ct_subtype_str (type, subtype);
1392 /* E.g, " text/plain" */
1393 char *type_subtypename = concat (" ", typename, "/", subtypename, NULL);
1394 /* E.g, " text/plain\n" */
1395 char *name_plus_nl = concat (type_subtypename, "\n", NULL);
1396 int found_content_type = 0;
1397 HF hf;
1398 const char *cp = NULL;
1399 char *ctline;
1400 int status;
1401
1402 /* Update/add Content-Type header field. */
1403 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1404 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1405 found_content_type = 1;
1406 free (hf->value);
1407 hf->value = (cp = strchr (ct->c_ctline, ';'))
1408 ? concat (type_subtypename, cp, "\n", NULL)
1409 : add (name_plus_nl, NULL);
1410 }
1411 }
1412 if (! found_content_type) {
1413 add_header (ct, add (TYPE_FIELD, NULL),
1414 (cp = strchr (ct->c_ctline, ';'))
1415 ? concat (type_subtypename, cp, "\n", NULL)
1416 : add (name_plus_nl, NULL));
1417 }
1418
1419 /* Some of these might not be used, but set them anyway. */
1420 ctline = cp
1421 ? concat (type_subtypename, cp, NULL)
1422 : concat (type_subtypename, NULL);
1423 free (ct->c_ctline);
1424 ct->c_ctline = ctline;
1425 /* Leave other ctinfo members as they were. */
1426 free (ct->c_ctinfo.ci_type);
1427 ct->c_ctinfo.ci_type = add (typename, NULL);
1428 free (ct->c_ctinfo.ci_subtype);
1429 ct->c_ctinfo.ci_subtype = add (subtypename, NULL);
1430 ct->c_type = type;
1431 ct->c_subtype = subtype;
1432
1433 free (name_plus_nl);
1434 free (type_subtypename);
1435
1436 status = set_ce (ct, encoding);
1437
1438 return status;
1439 }
1440
1441
1442 static int
1443 decode_text_parts (CT ct, int encoding, int *message_mods) {
1444 int status = OK;
1445
1446 switch (ct->c_type) {
1447 case CT_TEXT:
1448 switch (ct->c_encoding) {
1449 case CE_BASE64:
1450 case CE_QUOTED: {
1451 int ct_encoding;
1452
1453 if (decode_part (ct) == OK && ct->c_cefile.ce_file) {
1454 if ((ct_encoding = content_encoding (ct)) == CE_BINARY &&
1455 encoding != CE_BINARY) {
1456 if (verbosw) {
1457 report (ct->c_partno, ct->c_file,
1458 "will not decode%s because it is binary",
1459 ct->c_partno ? ""
1460 : ct->c_ctline ? ct->c_ctline
1461 : "");
1462 }
1463 unlink (ct->c_cefile.ce_file);
1464 free (ct->c_cefile.ce_file);
1465 ct->c_cefile.ce_file = NULL;
1466 } else if (ct_encoding == CE_8BIT && encoding == CE_7BIT) {
1467 if (verbosw) {
1468 report (ct->c_partno, ct->c_file,
1469 "will not decode%s because it is 8bit",
1470 ct->c_partno ? ""
1471 : ct->c_ctline ? ct->c_ctline
1472 : "");
1473 }
1474 unlink (ct->c_cefile.ce_file);
1475 free (ct->c_cefile.ce_file);
1476 ct->c_cefile.ce_file = NULL;
1477 } else {
1478 int enc = ct_encoding == CE_BINARY
1479 ? CE_BINARY
1480 : charset_encoding (ct);
1481 if (set_ce (ct, enc) == OK) {
1482 ++*message_mods;
1483 if (verbosw) {
1484 report (ct->c_partno, ct->c_file, "decode%s",
1485 ct->c_ctline ? ct->c_ctline : "");
1486 }
1487 } else {
1488 status = NOTOK;
1489 }
1490 }
1491 } else {
1492 status = NOTOK;
1493 }
1494 break;
1495 }
1496 default:
1497 break;
1498 }
1499 break;
1500
1501 case CT_MULTIPART: {
1502 struct multipart *m = (struct multipart *) ct->c_ctparams;
1503 struct part *part;
1504
1505 /* Should check to see if the body for this part is encoded?
1506 For now, it gets passed along as-is by InitMultiPart(). */
1507 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
1508 status = decode_text_parts (part->mp_part, encoding, message_mods);
1509 }
1510 break;
1511 }
1512
1513 case CT_MESSAGE:
1514 if (ct->c_subtype == MESSAGE_EXTERNAL) {
1515 struct exbody *e;
1516
1517 e = (struct exbody *) ct->c_ctparams;
1518 status = decode_text_parts (e->eb_content, encoding, message_mods);
1519 }
1520 break;
1521
1522 default:
1523 break;
1524 }
1525
1526 return status;
1527 }
1528
1529
1530 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1531 if it has any NUL characters, a CR not followed by a LF, or lines
1532 greater than 998 characters in length. */
1533 static int
1534 content_encoding (CT ct) {
1535 CE ce = &ct->c_cefile;
1536 int encoding = CE_7BIT;
1537
1538 if (ce->ce_file) {
1539 char buffer[BUFSIZ];
1540 size_t inbytes;
1541
1542 if (! ce->ce_fp && (ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
1543 advise (ce->ce_file, "unable to open for reading");
1544 return CE_UNKNOWN;
1545 }
1546
1547 fseeko (ce->ce_fp, 0L, SEEK_SET);
1548 while (encoding != CE_BINARY &&
1549 (inbytes = fread (buffer, 1, sizeof buffer, ce->ce_fp)) > 0) {
1550 char *cp;
1551 size_t i;
1552 size_t line_len = 0;
1553 int last_char_was_cr = 0;
1554
1555 for (i = 0, cp = buffer; i < inbytes; ++i, ++cp) {
1556 if (*cp == '\0' || ++line_len > 998 ||
1557 (*cp != '\n' && last_char_was_cr)) {
1558 encoding = CE_BINARY;
1559 break;
1560 } else if (*cp == '\n') {
1561 line_len = 0;
1562 } else if (! isascii ((unsigned char) *cp)) {
1563 encoding = CE_8BIT;
1564 }
1565
1566 last_char_was_cr = *cp == '\r' ? 1 : 0;
1567 }
1568 }
1569
1570 fclose (ce->ce_fp);
1571 ce->ce_fp = NULL;
1572 } /* else should never happen */
1573
1574 return encoding;
1575 }
1576
1577
1578 static int
1579 convert_codesets (CT ct, char *dest_codeset, int *message_mods) {
1580 int status = OK;
1581
1582 switch (ct->c_type) {
1583 case CT_TEXT:
1584 if (ct->c_subtype == TEXT_PLAIN) {
1585 status = convert_codeset (ct, dest_codeset, message_mods);
1586 }
1587 break;
1588
1589 case CT_MULTIPART: {
1590 struct multipart *m = (struct multipart *) ct->c_ctparams;
1591 struct part *part;
1592
1593 /* Should check to see if the body for this part is encoded?
1594 For now, it gets passed along as-is by InitMultiPart(). */
1595 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
1596 status =
1597 convert_codesets (part->mp_part, dest_codeset, message_mods);
1598 }
1599 break;
1600 }
1601
1602 case CT_MESSAGE:
1603 if (ct->c_subtype == MESSAGE_EXTERNAL) {
1604 struct exbody *e;
1605
1606 e = (struct exbody *) ct->c_ctparams;
1607 status =
1608 convert_codesets (e->eb_content, dest_codeset, message_mods);
1609 }
1610 break;
1611
1612 default:
1613 break;
1614 }
1615
1616 return status;
1617 }
1618
1619
1620 static int
1621 convert_codeset (CT ct, char *dest_codeset, int *message_mods) {
1622 const char *const charset = "charset";
1623 char **src_codeset = NULL;
1624 char *default_codeset = NULL;
1625 CI ctinfo = &ct->c_ctinfo;
1626 char **ap, **vp;
1627 int status = OK;
1628
1629 for (ap = ctinfo->ci_attrs, vp = ctinfo->ci_values; *ap; ++ap, ++vp) {
1630 if (! strcasecmp (*ap, charset)) {
1631 src_codeset = vp;
1632 break;
1633 }
1634 }
1635 /* RFC 2045, Sec. 5.2: default to us-ascii. */
1636 if (src_codeset == NULL) src_codeset = &default_codeset;
1637 if (*src_codeset == NULL) *src_codeset = "US-ASCII";
1638
1639 if (strcmp (norm_charmap (*src_codeset), norm_charmap (dest_codeset))) {
1640 #ifdef HAVE_ICONV
1641 iconv_t conv_desc = NULL;
1642 char *dest;
1643 int fd = -1;
1644 char **file = NULL;
1645 FILE **fp = NULL;
1646 long begin;
1647 long end;
1648 int opened_input_file = 0;
1649 char src_buffer[BUFSIZ];
1650 HF hf;
1651
1652 if ((conv_desc = iconv_open (dest_codeset, *src_codeset)) ==
1653 (iconv_t) -1) {
1654 advise (NULL, "Can't convert %s to %s", *src_codeset, dest_codeset);
1655 return -1;
1656 }
1657
1658 dest = add (m_mktemp2 (tmp, invo_name, &fd, NULL), NULL);
1659
1660 if (ct->c_cefile.ce_file) {
1661 file = &ct->c_cefile.ce_file;
1662 fp = &ct->c_cefile.ce_fp;
1663 begin = 0;
1664 end = -1;
1665 } else if (ct->c_file) {
1666 file = &ct->c_file;
1667 fp = &ct->c_fp;
1668 begin = ct->c_begin;
1669 end = ct->c_end;
1670 } /* else no input file: shouldn't happen */
1671
1672 if (file && *file && fp) {
1673 if (! *fp) {
1674 if ((*fp = fopen (*file, "r")) == NULL) {
1675 advise (*file, "unable to open for reading");
1676 status = NOTOK;
1677 } else {
1678 opened_input_file = 1;
1679 }
1680 }
1681 }
1682
1683 if (fp && *fp) {
1684 size_t inbytes;
1685 size_t max = end > 0 ? (size_t) (end-begin) : sizeof src_buffer;
1686
1687 fseeko (*fp, begin, SEEK_SET);
1688 while (status == OK && max > 0 &&
1689 (inbytes = fread (src_buffer, 1, max, *fp)) > 0) {
1690 char dest_buffer[BUFSIZ];
1691 char *ib = src_buffer, *ob = dest_buffer;
1692 size_t outbytes = sizeof dest_buffer;
1693 size_t outbytes_before = outbytes;
1694
1695 if (end > 0) max -= inbytes;
1696
1697 if (iconv (conv_desc, &ib, &inbytes, &ob, &outbytes) ==
1698 (size_t) -1) {
1699 status = NOTOK;
1700 break;
1701 } else {
1702 write (fd, dest_buffer, outbytes_before - outbytes);
1703 }
1704 }
1705
1706 if (opened_input_file) {
1707 fclose (*fp);
1708 *fp = NULL;
1709 }
1710 }
1711
1712 iconv_close (conv_desc);
1713 close (fd);
1714
1715 if (status == OK) {
1716 /* Replace the decoded file with the converted one. */
1717 if (ct->c_cefile.ce_file) {
1718 if (ct->c_cefile.ce_unlink) {
1719 unlink (ct->c_cefile.ce_file);
1720 }
1721 free (ct->c_cefile.ce_file);
1722 }
1723 ct->c_cefile.ce_file = dest;
1724 ct->c_cefile.ce_unlink = 1;
1725
1726 ++*message_mods;
1727 if (verbosw) {
1728 report (ct->c_partno, ct->c_file, "convert %s to %s",
1729 *src_codeset, dest_codeset);
1730 }
1731
1732 /* Update ci_attrs. */
1733 *src_codeset = dest_codeset;
1734
1735 /* Update ct->c_ctline. */
1736 if (ct->c_ctline) {
1737 char *ctline =
1738 update_attr (ct->c_ctline, "charset=", dest_codeset);
1739
1740 free (ct->c_ctline);
1741 ct->c_ctline = ctline;
1742 } /* else no CT line, which is odd */
1743
1744 /* Update Content-Type header field. */
1745 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1746 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1747 char *ctline_less_newline =
1748 update_attr (hf->value, "charset=", dest_codeset);
1749 char *ctline = concat (ctline_less_newline, "\n", NULL);
1750 free (ctline_less_newline);
1751
1752 free (hf->value);
1753 hf->value = ctline;
1754 break;
1755 }
1756 }
1757 } else {
1758 unlink (dest);
1759 }
1760 #else /* ! HAVE_ICONV */
1761 NMH_UNUSED (message_mods);
1762
1763 advise (NULL, "Can't convert %s to %s without iconv", *src_codeset,
1764 dest_codeset);
1765 status = NOTOK;
1766 #endif /* ! HAVE_ICONV */
1767 }
1768
1769 return status;
1770 }
1771
1772
1773 static int
1774 write_content (CT ct, char *input_filename, char *outfile, int modify_inplace,
1775 int message_mods) {
1776 int status = OK;
1777
1778 if (modify_inplace) {
1779 if (message_mods > 0) {
1780 if ((status = output_message (ct, outfile)) == OK) {
1781 char *infile = input_filename
1782 ? add (input_filename, NULL)
1783 : add (ct->c_file ? ct->c_file : "-", NULL);
1784
1785 if (remove_file (infile) == OK) {
1786 if (rename (outfile, infile)) {
1787 /* The -file argument processing used path() to
1788 expand filename to absolute path. */
1789 int file = ct->c_file && ct->c_file[0] == '/';
1790
1791 admonish (NULL, "unable to rename %s %s to %s",
1792 file ? "file" : "message", outfile, infile);
1793 unlink (outfile);
1794 status = NOTOK;
1795 }
1796 } else {
1797 admonish (NULL, "unable to remove input file %s, "
1798 "not modifying it", infile);
1799 unlink (outfile);
1800 status = NOTOK;
1801 }
1802
1803 free (infile);
1804 }
1805 } else {
1806 /* No modifications and didn't need the tmp outfile. */
1807 unlink (outfile);
1808 }
1809 } else {
1810 /* Output is going to some file. Produce it whether or not
1811 there were modifications. */
1812 status = output_message (ct, outfile);
1813 }
1814
1815 flush_errors ();
1816 return status;
1817 }
1818
1819
1820 /*
1821 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1822 * use the standard MH backup file.
1823 */
1824 static int
1825 remove_file (char *file) {
1826 if (rmmproc) {
1827 char *rmm_command = concat (rmmproc, " ", file, NULL);
1828 int status = system (rmm_command);
1829
1830 free (rmm_command);
1831 return WIFEXITED (status) ? WEXITSTATUS (status) : NOTOK;
1832 } else {
1833 /* This is OK for a non-message file, it still uses the
1834 BACKUP_PREFIX form. The backup file will be in the same
1835 directory as file. */
1836 return rename (file, m_backup (file));
1837 }
1838 }
1839
1840
1841 static void
1842 report (char *partno, char *filename, char *message, ...) {
1843 va_list args;
1844 char *fmt;
1845
1846 if (verbosw) {
1847 va_start (args, message);
1848 fmt = concat (filename, partno ? " part " : ", ",
1849 partno ? partno : "", partno ? ", " : "", message, NULL);
1850
1851 advertise (NULL, NULL, fmt, args);
1852
1853 free (fmt);
1854 va_end (args);
1855 }
1856 }
1857
1858
1859 static char *
1860 upcase (char *str) {
1861 char *up = cpytrim (str);
1862 char *cp;
1863
1864 for (cp = up; *cp; ++cp) *cp = toupper ((unsigned char) *cp);
1865
1866 return up;
1867 }
1868
1869
1870 static void
1871 pipeser (int i)
1872 {
1873 if (i == SIGQUIT) {
1874 fflush (stdout);
1875 fprintf (stderr, "\n");
1876 fflush (stderr);
1877 }
1878
1879 done (1);
1880 /* NOTREACHED */
1881 }