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