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