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