]> diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
sbr/utils.c: Add HasSuffix(s, suffix).
[nmh] / uip / mhshowsbr.c
1
2 /*
3 * mhshowsbr.c -- routines to display the contents of MIME messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/fmt_scan.h>
19 #include <h/utils.h>
20 #ifdef HAVE_ICONV
21 # include <iconv.h>
22 #endif /* ! HAVE_ICONV */
23
24 extern int debugsw;
25 extern int npart;
26 extern int ntype;
27
28 int nolist = 0;
29
30 char *progsw = NULL;
31
32 /* flags for moreproc/header display */
33 int nomore = 0;
34 char *formsw = NULL;
35
36 /* for output markerss and headers */
37 char *folder = NULL;
38 char *markerform;
39 char *headerform;
40 int headersw = -1;
41
42
43 /* mhmisc.c */
44 int part_ok (CT);
45 int part_exact (CT);
46 int type_ok (CT, int);
47 void content_error (char *, CT, char *, ...);
48 void flush_errors (void);
49
50 /*
51 * static prototypes
52 */
53 static void show_single_message (CT, char *, int, int, int, struct format *);
54 static void DisplayMsgHeader (CT, char *, int);
55 static int show_switch (CT, int, int, int, int, struct format *);
56 static int show_content (CT, int, int, int, struct format *);
57 static int show_content_aux2 (CT, int, char *, char *, int, int, int, struct format *);
58 static int show_text (CT, int, int, struct format *);
59 static int show_multi (CT, int, int, int, int, struct format *);
60 static int show_multi_internal (CT, int, int, int, int, struct format *);
61 static int show_multi_aux (CT, int, char *, struct format *);
62 static int show_message_rfc822 (CT, int, struct format *);
63 static int show_partial (CT, int);
64 static int show_external (CT, int, int, int, int, struct format *);
65 static int parse_display_string (CT, char *, int *, int *, char *, char *,
66 size_t, int multipart);
67 static int convert_content_charset (CT, char **);
68 static struct format *compile_header(char *);
69 static struct format *compile_marker(char *);
70 static void output_header (CT, struct format *);
71 static void output_marker (CT, struct format *, int);
72 static void free_markercomps (void);
73 static int pidcheck(int);
74
75 /*
76 * Components (and list of parameters/components) we care about for the
77 * content marker display.
78 */
79
80 static struct comp *folder_comp = NULL;
81 static struct comp *part_comp = NULL;
82 static struct comp *ctype_comp = NULL;
83 static struct comp *description_comp = NULL;
84 static struct comp *dispo_comp = NULL;
85
86 struct param_comp_list {
87 char *param;
88 struct comp *comp;
89 struct param_comp_list *next;
90 };
91
92 static struct param_comp_list *ctype_pc_list = NULL;
93 static struct param_comp_list *dispo_pc_list = NULL;
94
95
96 /*
97 * Top level entry point to show/display a group of messages
98 */
99
100 void
101 show_all_messages (CT *cts, int concatsw, int textonly, int inlineonly)
102 {
103 CT ct, *ctp;
104 struct format *hfmt, *mfmt;
105
106 /*
107 * If form is not specified, then get default form
108 * for showing headers of MIME messages.
109 */
110 if (!formsw)
111 formsw = getcpy (etcpath ("mhl.headers"));
112
113 /*
114 * Compile the content marker and header format lines
115 */
116 mfmt = compile_marker(markerform);
117 hfmt = compile_header(headerform);
118
119 /*
120 * If form is "mhl.null", suppress display of header.
121 */
122 if (!strcmp (formsw, "mhl.null"))
123 formsw = NULL;
124
125 for (ctp = cts; *ctp; ctp++) {
126 ct = *ctp;
127
128 /* if top-level type is ok, then display message */
129 if (type_ok (ct, 1)) {
130 if (headersw) output_header(ct, hfmt);
131
132 show_single_message (ct, formsw, concatsw, textonly, inlineonly,
133 mfmt);
134 }
135 }
136
137 free_markercomps();
138 fmt_free(hfmt, 1);
139 fmt_free(mfmt, 1);
140 }
141
142
143 /*
144 * Entry point to show/display a single message
145 */
146
147 static void
148 show_single_message (CT ct, char *form, int concatsw, int textonly,
149 int inlineonly, struct format *fmt)
150 {
151 sigset_t set, oset;
152
153 int status = OK;
154
155 /* Allow user executable bit so that temporary directories created by
156 * the viewer (e.g., lynx) are going to be accessible */
157 umask (ct->c_umask & ~(0100));
158
159 /*
160 * If you have a format file, then display
161 * the message headers.
162 */
163 if (form)
164 DisplayMsgHeader(ct, form, concatsw);
165
166 /* Show the body of the message */
167 show_switch (ct, 0, concatsw, textonly, inlineonly, fmt);
168
169 if (ct->c_fp) {
170 fclose (ct->c_fp);
171 ct->c_fp = NULL;
172 }
173 if (ct->c_ceclosefnx)
174 (*ct->c_ceclosefnx) (ct);
175
176 /* block a few signals */
177 sigemptyset (&set);
178 sigaddset (&set, SIGHUP);
179 sigaddset (&set, SIGINT);
180 sigaddset (&set, SIGQUIT);
181 sigaddset (&set, SIGTERM);
182 sigprocmask (SIG_BLOCK, &set, &oset);
183
184 while (!concatsw && wait (&status) != NOTOK) {
185 pidcheck (status);
186 continue;
187 }
188
189 /* reset the signal mask */
190 sigprocmask (SIG_SETMASK, &oset, &set);
191
192 flush_errors ();
193 }
194
195
196 /*
197 * Use the mhlproc to show the header fields
198 */
199
200 static void
201 DisplayMsgHeader (CT ct, char *form, int concatsw)
202 {
203 pid_t child_id;
204 int i, vecp;
205 char **vec;
206 char *file;
207
208 vec = argsplit(mhlproc, &file, &vecp);
209 vec[vecp++] = mh_xstrdup("-form");
210 vec[vecp++] = mh_xstrdup(form);
211 vec[vecp++] = mh_xstrdup("-nobody");
212 vec[vecp++] = getcpy(ct->c_file);
213
214 /*
215 * If we've specified -(no)moreproc,
216 * then just pass that along.
217 */
218 if (nomore || concatsw) {
219 vec[vecp++] = mh_xstrdup("-nomoreproc");
220 } else if (progsw) {
221 vec[vecp++] = mh_xstrdup("-moreproc");
222 vec[vecp++] = mh_xstrdup(progsw);
223 }
224 vec[vecp] = NULL;
225
226 fflush (stdout);
227
228 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
229 sleep (5);
230
231 switch (child_id) {
232 case NOTOK:
233 adios ("fork", "unable to");
234 /* NOTREACHED */
235
236 case OK:
237 execvp (file, vec);
238 fprintf (stderr, "unable to exec ");
239 perror (mhlproc);
240 _exit (-1);
241 /* NOTREACHED */
242
243 default:
244 pidcheck(pidwait(child_id, NOTOK));
245 break;
246 }
247
248 arglist_free(file, vec);
249 }
250
251
252 /*
253 * Switching routine. Call the correct routine
254 * based on content type.
255 */
256
257 static int
258 show_switch (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
259 struct format *fmt)
260 {
261 switch (ct->c_type) {
262 case CT_MULTIPART:
263 return show_multi (ct, alternate, concatsw, textonly,
264 inlineonly, fmt);
265
266 case CT_MESSAGE:
267 switch (ct->c_subtype) {
268 case MESSAGE_PARTIAL:
269 return show_partial (ct, alternate);
270
271 case MESSAGE_EXTERNAL:
272 return show_external (ct, alternate, concatsw, textonly,
273 inlineonly, fmt);
274
275 case MESSAGE_RFC822:
276 return show_message_rfc822 (ct, alternate, fmt);
277
278 /*
279 * Treat unknown message types as equivalent to
280 * application/octet-stream for now
281 */
282 default:
283 return show_content (ct, alternate, textonly,
284 inlineonly, fmt);
285 }
286
287 case CT_TEXT:
288 return show_text (ct, alternate, concatsw, fmt);
289
290 case CT_AUDIO:
291 case CT_IMAGE:
292 case CT_VIDEO:
293 case CT_APPLICATION:
294 default:
295 return show_content (ct, alternate, textonly, inlineonly, fmt);
296 }
297
298 return 0; /* NOT REACHED */
299 }
300
301
302 /*
303 * Generic method for displaying content
304 */
305
306 static int
307 show_content (CT ct, int alternate, int textonly, int inlineonly,
308 struct format *fmt)
309 {
310 char *cp;
311 CI ci = &ct->c_ctinfo;
312
313 /*
314 * If we're here, we are not a text type. So we don't need to check
315 * the content-type.
316 */
317
318 if (textonly || (inlineonly && !is_inline(ct))) {
319 output_marker(ct, fmt, 1);
320 return OK;
321 }
322
323 /* Check for invo_name-show-type[/subtype] */
324 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
325 return show_content_aux (ct, alternate, cp, NULL, fmt);
326
327 if ((cp = ct->c_showproc))
328 return show_content_aux (ct, alternate, cp, NULL, fmt);
329
330 /* complain if we are not a part of a multipart/alternative */
331 if (!alternate)
332 content_error (NULL, ct, "don't know how to display content");
333
334 return NOTOK;
335 }
336
337
338 /*
339 * Parse the display string for displaying generic content
340 */
341
342 int
343 show_content_aux (CT ct, int alternate, char *cp, char *cracked, struct format *fmt)
344 {
345 int fd;
346 int xstdin = 0, xlist = 0;
347 char *file = NULL, buffer[BUFSIZ];
348
349 if (!ct->c_ceopenfnx) {
350 if (!alternate)
351 content_error (NULL, ct, "don't know how to decode content");
352
353 return NOTOK;
354 }
355
356 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
357 return NOTOK;
358 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
359 return OK;
360
361 if (! strcmp(invo_name, "mhshow") &&
362 ct->c_type == CT_TEXT && ct->c_subtype == TEXT_PLAIN) {
363 /* This has to be done after calling c_ceopenfnx, so
364 unfortunately the type checks are necessary without
365 some code rearrangement. And to make this really ugly,
366 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
367 if (convert_content_charset (ct, &file) == OK) {
368 (*ct->c_ceclosefnx) (ct);
369 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
370 return NOTOK;
371 } else {
372 char *charset = content_charset (ct);
373 admonish (NULL, "unable to convert character set%s%s from %s",
374 ct->c_partno ? " of part " : "",
375 ct->c_partno ? ct->c_partno : "",
376 charset);
377 free (charset);
378 }
379 }
380
381 if (cracked) {
382 strncpy (buffer, cp, sizeof(buffer));
383 goto got_command;
384 }
385
386 if (parse_display_string (ct, cp, &xstdin, &xlist, file, buffer,
387 sizeof(buffer) - 1, 0)) {
388 admonish (NULL, "Buffer overflow constructing show command!\n");
389 return NOTOK;
390 }
391
392 got_command:
393 return show_content_aux2 (ct, alternate, cracked, buffer,
394 fd, xlist, xstdin, fmt);
395 }
396
397
398 /*
399 * Routine to actually display the content
400 */
401
402 static int
403 show_content_aux2 (CT ct, int alternate, char *cracked, char *buffer,
404 int fd, int xlist, int xstdin, struct format *fmt)
405 {
406 pid_t child_id;
407 int i, vecp;
408 char **vec, *file;
409
410 if (debugsw || cracked) {
411 fflush (stdout);
412
413 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
414 ct->c_file);
415 if (ct->c_partno)
416 fprintf (stderr, " part %s", ct->c_partno);
417 if (cracked)
418 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
419 else
420 fprintf (stderr, " using command %s\n", buffer);
421 }
422
423 if (xlist && fmt) {
424 output_marker(ct, fmt, 0);
425 }
426
427 /*
428 * If the command is a zero-length string, just write the output on
429 * stdout.
430 */
431
432 if (buffer[0] == '\0') {
433 char readbuf[BUFSIZ];
434 ssize_t cc;
435 char lastchar = '\n';
436
437 if (fd == NOTOK) {
438 advise(NULL, "Cannot use NULL command to display content-type "
439 "%s/%s", ct->c_ctinfo.ci_type, ct->c_ctinfo.ci_subtype);
440 return NOTOK;
441 }
442
443 while ((cc = read(fd, readbuf, sizeof(readbuf))) > 0) {
444 if ((ssize_t) fwrite(readbuf, sizeof(char), cc, stdout) < cc) {
445 advise ("putline", "fwrite");
446 }
447 lastchar = readbuf[cc - 1];
448 }
449
450 if (cc < 0) {
451 advise("read", "while reading text content");
452 return NOTOK;
453 }
454
455 /*
456 * The MIME standards allow content to not have a trailing newline.
457 * But because we are (presumably) sending this to stdout, include
458 * a newline for text content if the final character was not a
459 * newline. Only do this for mhshow.
460 */
461
462 if (strcmp(invo_name, "mhshow") == 0 && ct->c_type == CT_TEXT &&
463 ct->c_subtype == TEXT_PLAIN && lastchar != '\n') {
464 putc('\n', stdout);
465 }
466
467 fflush(stdout);
468
469 return OK;
470 }
471
472 vec = argsplit(buffer, &file, &vecp);
473 vec[vecp++] = NULL;
474
475 fflush (stdout);
476
477 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
478 sleep (5);
479 switch (child_id) {
480 case NOTOK:
481 advise ("fork", "unable to");
482 (*ct->c_ceclosefnx) (ct);
483 return NOTOK;
484
485 case OK:
486 if (cracked) {
487 if (chdir (cracked) < 0) {
488 advise (cracked, "chdir");
489 }
490 }
491 if (!xstdin)
492 dup2 (fd, 0);
493 close (fd);
494 execvp (file, vec);
495 fprintf (stderr, "unable to exec ");
496 perror (buffer);
497 _exit (-1);
498 /* NOTREACHED */
499
500 default: {
501 int status;
502
503 arglist_free(file, vec);
504
505 pidcheck ((status = pidXwait (child_id, NULL)));
506
507 if (fd != NOTOK)
508 (*ct->c_ceclosefnx) (ct);
509 return (alternate ? OK : status);
510 }
511 }
512 }
513
514
515 /*
516 * show content of type "text"
517 */
518
519 static int
520 show_text (CT ct, int alternate, int concatsw, struct format *fmt)
521 {
522 char *cp, buffer[BUFSIZ];
523 CI ci = &ct->c_ctinfo;
524
525 /* Check for invo_name-show-type[/subtype] */
526 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
527 return show_content_aux (ct, alternate, cp, NULL, fmt);
528
529 /*
530 * Use default method if content is text/plain, or if
531 * if it is not a text part of a multipart/alternative
532 */
533 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
534 if (concatsw) {
535 if (ct->c_termproc)
536 snprintf(buffer, sizeof(buffer), "%%lcat");
537 else
538 snprintf(buffer, sizeof(buffer), "%%l");
539 } else
540 snprintf (buffer, sizeof(buffer), "%%l%s %%F", progsw ? progsw :
541 moreproc && *moreproc ? moreproc : DEFAULT_PAGER);
542 cp = (ct->c_showproc = add (buffer, NULL));
543 return show_content_aux (ct, alternate, cp, NULL, fmt);
544 }
545
546 return NOTOK;
547 }
548
549
550 /*
551 * show message body of type "multipart"
552 */
553
554 static int
555 show_multi (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
556 struct format *fmt)
557 {
558 char *cp;
559 CI ci = &ct->c_ctinfo;
560
561 /* Check for invo_name-show-type[/subtype] */
562 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
563 return show_multi_aux (ct, alternate, cp, fmt);
564
565 if ((cp = ct->c_showproc)) {
566 return show_multi_aux (ct, alternate, cp, fmt);
567 }
568
569 /*
570 * Use default method to display this multipart content. Even
571 * unknown types are displayable, since they're treated as mixed
572 * per RFC 2046.
573 */
574 return show_multi_internal (ct, alternate, concatsw, textonly,
575 inlineonly, fmt);
576 }
577
578
579 /*
580 * show message body of subtypes of multipart that
581 * we understand directly (mixed, alternate, etc...)
582 */
583
584 static int
585 show_multi_internal (CT ct, int alternate, int concatsw, int textonly,
586 int inlineonly, struct format *fmt)
587 {
588 int alternating, nowalternate, result;
589 struct multipart *m = (struct multipart *) ct->c_ctparams;
590 struct part *part;
591 int request_matched;
592 int display_success;
593 int mult_alt_done;
594 int ret;
595 CT p;
596
597 alternating = 0;
598 nowalternate = alternate;
599
600 if (ct->c_subtype == MULTI_ALTERNATE) {
601 nowalternate = 1;
602 alternating = 1;
603 }
604
605 /*
606 * alternate -> we are a part inside a multipart/alternative
607 * alternating -> we are a multipart/alternative
608 */
609
610 result = NOTOK;
611 request_matched = 0;
612 display_success = 0;
613 mult_alt_done = 0;
614
615 for (part = m->mp_parts; part; part = part->mp_next) {
616 p = part->mp_part;
617
618 /* while looking for the right displayable alternative, we
619 * use a looser search criterion than we do after finding it.
620 * specifically, while still looking, part_ok() will match
621 * "parent" parts (e.g. "-part 2" where 2 is a high-level
622 * multipart). after finding it, we use part_exact() to only
623 * choose a part that was requested explicitly.
624 */
625 if ((part_exact(p) && type_ok(p, 1)) ||
626 (!mult_alt_done && part_ok (p) && type_ok (p, 1))) {
627
628 int inneresult;
629
630 inneresult = show_switch (p, nowalternate, concatsw, textonly,
631 inlineonly, fmt);
632 switch (inneresult) {
633 case NOTOK: /* hard display error */
634 request_matched = 1;
635 if (alternate && !alternating) {
636 result = NOTOK;
637 goto out;
638 }
639 continue;
640
641 case DONE: /* found no match on content type */
642 continue;
643
644 case OK: /* display successful */
645 request_matched = 1;
646 display_success = 1;
647 result = OK;
648
649 /* if we got success on a sub-part of
650 * multipart/alternative, we're done, unless
651 * there's a chance an explicit part should be
652 * matched later in the alternatives. */
653 if (alternating) {
654 mult_alt_done = 1;
655 } else if (alternate) {
656 alternate = nowalternate = 0;
657 }
658 continue;
659 }
660 break;
661 }
662 }
663
664 /* we're supposed to be displaying at least something from a
665 * multipart/alternative. if we've had parts to consider, and
666 * we've had no success, then we should complain. we shouldn't
667 * complain if none of the parts matched any -part or -type option.
668 */
669 if (alternating && request_matched && !display_success) {
670 /* if we're ourselves an alternate. don't complain yet. */
671 if (!alternate)
672 content_error (NULL, ct, "don't know how to display any of the contents");
673 result = NOTOK;
674 }
675
676 out:
677 /* if no parts matched what was requested, there can't have been
678 * any display errors. we report DONE rather than OK. */
679 ret = request_matched ? result : DONE;
680 return ret;
681 }
682
683
684 /*
685 * Parse display string for multipart content
686 * and use external program to display it.
687 */
688
689 static int
690 show_multi_aux (CT ct, int alternate, char *cp, struct format *fmt)
691 {
692 /* xstdin is only used in the call to parse_display_string():
693 its value is ignored in the function. */
694 int xstdin = 0, xlist = 0;
695 char *file = NULL, buffer[BUFSIZ];
696 struct multipart *m = (struct multipart *) ct->c_ctparams;
697 struct part *part;
698 CT p;
699
700 for (part = m->mp_parts; part; part = part->mp_next) {
701 p = part->mp_part;
702
703 if (!p->c_ceopenfnx) {
704 if (!alternate)
705 content_error (NULL, p, "don't know how to decode content");
706 return NOTOK;
707 }
708
709 if (p->c_storage == NULL) {
710 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
711 return NOTOK;
712
713 p->c_storage = add (file, NULL);
714
715 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
716 return OK;
717 (*p->c_ceclosefnx) (p);
718 }
719 }
720
721 if (parse_display_string (ct, cp, &xstdin, &xlist, file,
722 buffer, sizeof(buffer) - 1, 1)) {
723 admonish (NULL, "Buffer overflow constructing show command!\n");
724 return NOTOK;
725 }
726
727 return show_content_aux2 (ct, alternate, NULL, buffer, NOTOK, xlist, 0, fmt);
728 }
729
730
731 /*
732 * show content of type "message/rfc822"
733 */
734
735 static int
736 show_message_rfc822 (CT ct, int alternate, struct format *fmt)
737 {
738 char *cp;
739 CI ci = &ct->c_ctinfo;
740
741 /* Check for invo_name-show-type[/subtype] */
742 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
743 return show_content_aux (ct, alternate, cp, NULL, fmt);
744
745 if ((cp = ct->c_showproc))
746 return show_content_aux (ct, alternate, cp, NULL, fmt);
747
748 /* default method for message/rfc822 */
749 if (ct->c_subtype == MESSAGE_RFC822) {
750 cp = (ct->c_showproc = add ("%pshow -file %F", NULL));
751 return show_content_aux (ct, alternate, cp, NULL, fmt);
752 }
753
754 /* complain if we are not a part of a multipart/alternative */
755 if (!alternate)
756 content_error (NULL, ct, "don't know how to display content");
757
758 return NOTOK;
759 }
760
761
762 /*
763 * Show content of type "message/partial".
764 */
765
766 static int
767 show_partial (CT ct, int alternate)
768 {
769 NMH_UNUSED (alternate);
770
771 content_error (NULL, ct,
772 "in order to display this message, you must reassemble it");
773 return NOTOK;
774 }
775
776
777 /*
778 * Show content of type "message/external".
779 *
780 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
781 */
782
783 static int
784 show_external (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
785 struct format *fmt)
786 {
787 struct exbody *e = (struct exbody *) ct->c_ctparams;
788 CT p = e->eb_content;
789
790 if (!type_ok (p, 0))
791 return OK;
792
793 return show_switch (p, alternate, concatsw, textonly, inlineonly, fmt);
794 }
795
796
797 static int
798 parse_display_string (CT ct, char *cp, int *xstdin, int *xlist,
799 char *file, char *buffer, size_t buflen,
800 int multipart) {
801 int len, quoted = 0;
802 char *bp = buffer, *pp;
803 CI ci = &ct->c_ctinfo;
804
805 bp[0] = bp[buflen] = '\0';
806
807 for ( ; *cp && buflen > 0; cp++) {
808 if (*cp == '%') {
809 pp = bp;
810
811 switch (*++cp) {
812 case 'a':
813 /* insert parameters from Content-Type field */
814 {
815 PM pm;
816 char *s = "";
817
818 for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) {
819 snprintf (bp, buflen, "%s%s=\"%s\"", s, pm->pm_name,
820 get_param_value(pm, '?'));
821 len = strlen (bp);
822 bp += len;
823 buflen -= len;
824 s = " ";
825 }
826 }
827 break;
828
829 case 'd':
830 /* insert content description */
831 if (ct->c_descr) {
832 char *s;
833
834 s = trimcpy (ct->c_descr);
835 strncpy (bp, s, buflen);
836 free (s);
837 }
838 break;
839
840 case 'e':
841 /* no longer implemented */
842 break;
843
844 case 'F':
845 /* %f, and stdin is terminal not content */
846 *xstdin = 1;
847 /* and fall... */
848
849 case 'f':
850 if (multipart) {
851 /* insert filename(s) containing content */
852 struct multipart *m = (struct multipart *) ct->c_ctparams;
853 struct part *part;
854 char *s = "";
855 CT p;
856
857 for (part = m->mp_parts; part; part = part->mp_next) {
858 p = part->mp_part;
859
860 snprintf (bp, buflen, "%s%s", s, p->c_storage);
861 len = strlen (bp);
862 bp += len;
863 buflen -= len;
864 s = " ";
865 }
866 } else {
867 /* insert filename containing content */
868 snprintf (bp, buflen, "%s", file);
869
870 /*
871 * Old comments below are left here for posterity.
872 * This was/is tricky.
873 */
874 /* since we've quoted the file argument, set things up
875 * to look past it, to avoid problems with the quoting
876 * logic below. (I know, I should figure out what's
877 * broken with the quoting logic, but..)
878 */
879 /*
880 * Here's the email that submitted the patch with
881 * the comment above:
882 * https://www.mail-archive.com/nmh-workers@mhost.com/
883 * msg00288.html
884 * I can't tell from that exactly what was broken,
885 * beyond misquoting of the filename. The profile
886 * had appearances of %F both with and without quotes.
887 * The unquoted ones should have been quoted by the
888 * code below.
889 * The fix was to always quote the filename. But
890 * that broke '%F' because it expanded to ''filename''.
891 */
892 /*
893 * Old comments above are left here for posterity.
894 * The quoting below should work properly now.
895 */
896 }
897 break;
898
899 case 'p':
900 /* No longer supported */
901 /* and fall... */
902
903 case 'l':
904 /* display listing prior to displaying content */
905 *xlist = !nolist;
906 break;
907
908 case 's':
909 /* insert subtype of content */
910 strncpy (bp, ci->ci_subtype, buflen);
911 break;
912
913 case '%':
914 /* insert character % */
915 goto raw;
916
917 case '{' : {
918 const char *closing_brace = strchr(cp, '}');
919
920 if (closing_brace) {
921 const size_t param_len = closing_brace - cp - 1;
922 char *param = mh_xmalloc(param_len + 1);
923 char *value;
924
925 (void) strncpy(param, cp + 1, param_len);
926 param[param_len] = '\0';
927 value = get_param(ci->ci_first_pm, param, '?', 0);
928 free(param);
929
930 cp += param_len + 1; /* Skip both braces, too. */
931
932 if (value) {
933 /* %{param} is set in the Content-Type header.
934 After the break below, quote it if necessary. */
935 (void) strncpy(bp, value, buflen);
936 free(value);
937 } else {
938 /* %{param} not found, so skip it completely. cp
939 was advanced above. */
940 continue;
941 }
942 } else {
943 /* This will get confused if there are multiple %{}'s,
944 but its real purpose is to avoid doing bad things
945 above if a closing brace wasn't found. */
946 admonish(NULL,
947 "no closing brace for display string escape %s",
948 cp);
949 }
950 break;
951 }
952
953 default:
954 *bp++ = *--cp;
955 *bp = '\0';
956 buflen--;
957 continue;
958 }
959 len = strlen (bp);
960 bp += len;
961 buflen -= len;
962 *bp = '\0';
963
964 /* Did we actually insert something? */
965 if (bp != pp) {
966 /* Insert single quote if not inside quotes already */
967 if (!quoted && buflen) {
968 len = strlen (pp);
969 memmove (pp + 1, pp, len+1);
970 *pp++ = '\'';
971 buflen--;
972 bp++;
973 quoted = 1;
974 }
975 /* Escape existing quotes */
976 while ((pp = strchr (pp, '\'')) && buflen > 3) {
977 len = strlen (pp++);
978 if (quoted) {
979 /* Quoted. Let this quote close that quoting.
980 Insert an escaped quote to replace it and
981 another quote to reopen quoting, which will be
982 closed below. */
983 memmove (pp + 2, pp, len);
984 *pp++ = '\\';
985 *pp++ = '\'';
986 buflen -= 2;
987 bp += 2;
988 quoted = 0;
989 } else {
990 /* Not quoted. This should not be reached with
991 the current code, but handle the condition
992 in case the code changes. Just escape the
993 quote. */
994 memmove (pp, pp-1, len+1);
995 *(pp++-1) = '\\';
996 buflen -= 1;
997 bp += 1;
998 }
999 }
1000 /* If pp is still set, that means we ran out of space. */
1001 if (pp)
1002 buflen = 0;
1003 /* Close quoting. */
1004 if (quoted && buflen) {
1005 /* See if we need to close the quote by looking
1006 for an odd number of unescaped close quotes in
1007 the remainder of the display string. */
1008 int found_quote = 0, escaped = 0;
1009 char *c;
1010
1011 for (c = cp+1; *c; ++c) {
1012 if (*c == '\\') {
1013 escaped = ! escaped;
1014 } else {
1015 if (escaped) {
1016 escaped = 0;
1017 } else {
1018 if (*c == '\'') {
1019 found_quote = ! found_quote;
1020 }
1021 }
1022 }
1023 }
1024 if (! found_quote) {
1025 *bp++ = '\'';
1026 buflen--;
1027 quoted = 0;
1028 }
1029 }
1030 }
1031 } else {
1032 raw:
1033 *bp++ = *cp;
1034 buflen--;
1035
1036 if (*cp == '\'')
1037 quoted = !quoted;
1038 }
1039
1040 *bp = '\0';
1041 }
1042
1043 if (buflen <= 0 ||
1044 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1045 /* content_error would provide a more useful error message
1046 * here, except that if we got overrun, it probably would
1047 * too.
1048 */
1049 return NOTOK;
1050 }
1051
1052 /* use charset string to modify display method */
1053 if (ct->c_termproc) {
1054 char term[BUFSIZ];
1055
1056 strncpy (term, buffer, sizeof(term));
1057 snprintf (buffer, buflen, ct->c_termproc, term);
1058 }
1059
1060 return OK;
1061 }
1062
1063
1064 int
1065 convert_charset (CT ct, char *dest_charset, int *message_mods) {
1066 char *src_charset = content_charset (ct);
1067 int status = OK;
1068
1069 if (strcasecmp (src_charset, dest_charset)) {
1070 #ifdef HAVE_ICONV
1071 iconv_t conv_desc = NULL;
1072 char *dest;
1073 int fd = -1;
1074 char **file = NULL;
1075 FILE **fp = NULL;
1076 size_t begin;
1077 size_t end;
1078 int opened_input_file = 0;
1079 char src_buffer[BUFSIZ];
1080 size_t dest_buffer_size = BUFSIZ;
1081 char *dest_buffer = mh_xmalloc(dest_buffer_size);
1082 HF hf;
1083 char *tempfile;
1084 int fromutf8 = !strcasecmp(src_charset, "UTF-8");
1085
1086 if ((conv_desc = iconv_open (dest_charset, src_charset)) ==
1087 (iconv_t) -1) {
1088 advise (NULL, "Can't convert %s to %s", src_charset, dest_charset);
1089 free (src_charset);
1090 return NOTOK;
1091 }
1092
1093 if ((tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
1094 adios (NULL, "unable to create temporary file in %s",
1095 get_temp_dir());
1096 }
1097 dest = add (tempfile, NULL);
1098
1099 if (ct->c_cefile.ce_file) {
1100 file = &ct->c_cefile.ce_file;
1101 fp = &ct->c_cefile.ce_fp;
1102 begin = end = 0;
1103 } else if (ct->c_file) {
1104 file = &ct->c_file;
1105 fp = &ct->c_fp;
1106 begin = (size_t) ct->c_begin;
1107 end = (size_t) ct->c_end;
1108 } /* else no input file: shouldn't happen */
1109
1110 if (file && *file && fp) {
1111 if (! *fp) {
1112 if ((*fp = fopen (*file, "r")) == NULL) {
1113 advise (*file, "unable to open for reading");
1114 status = NOTOK;
1115 } else {
1116 opened_input_file = 1;
1117 }
1118 }
1119 }
1120
1121 if (fp && *fp) {
1122 size_t inbytes;
1123 size_t bytes_to_read =
1124 end > 0 && end > begin ? end - begin : sizeof src_buffer;
1125
1126 fseeko (*fp, begin, SEEK_SET);
1127 while ((inbytes = fread (src_buffer, 1,
1128 min (bytes_to_read, sizeof src_buffer),
1129 *fp)) > 0) {
1130 ICONV_CONST char *ib = src_buffer;
1131 char *ob = dest_buffer;
1132 size_t outbytes = dest_buffer_size;
1133 size_t outbytes_before = outbytes;
1134
1135 if (end > 0) bytes_to_read -= inbytes;
1136
1137 iconv_start:
1138 if (iconv (conv_desc, &ib, &inbytes, &ob, &outbytes) ==
1139 (size_t) -1) {
1140 if (errno == E2BIG) {
1141 /*
1142 * Bump up the buffer by at least a factor of 2
1143 * over what we need.
1144 */
1145 size_t bumpup = inbytes * 2, ob_off = ob - dest_buffer;
1146 dest_buffer_size += bumpup;
1147 dest_buffer = mh_xrealloc(dest_buffer,
1148 dest_buffer_size);
1149 ob = dest_buffer + ob_off;
1150 outbytes += bumpup;
1151 outbytes_before += bumpup;
1152 goto iconv_start;
1153 }
1154 if (errno == EINVAL) {
1155 /* middle of multi-byte sequence */
1156 if (write (fd, dest_buffer, outbytes_before - outbytes) < 0) {
1157 advise (dest, "write");
1158 }
1159 fseeko (*fp, -inbytes, SEEK_CUR);
1160 if (end > 0) bytes_to_read += inbytes;
1161 /* advise(NULL, "convert_charset: EINVAL"); */
1162 continue;
1163 }
1164 if (errno == EILSEQ) {
1165 /* invalid multi-byte sequence */
1166 if (fromutf8) {
1167 for (++ib, --inbytes;
1168 inbytes > 0 &&
1169 (((unsigned char) *ib) & 0xc0) == 0x80;
1170 ++ib, --inbytes)
1171 continue;
1172 } else {
1173 ib++; inbytes--; /* skip it */
1174 }
1175 (*ob++) = '?'; outbytes --;
1176 /* advise(NULL, "convert_charset: EILSEQ"); */
1177 goto iconv_start;
1178 }
1179 advise (NULL, "convert_charset: errno = %d", errno);
1180 status = NOTOK;
1181 break;
1182 } else {
1183 if (write (fd, dest_buffer, outbytes_before - outbytes)
1184 < 0) {
1185 advise (dest, "write");
1186 }
1187 }
1188 }
1189
1190 if (opened_input_file) {
1191 fclose (*fp);
1192 *fp = NULL;
1193 }
1194 }
1195
1196 iconv_close (conv_desc);
1197 close (fd);
1198
1199 if (status == OK) {
1200 /* Replace the decoded file with the converted one. */
1201 if (ct->c_cefile.ce_file) {
1202 if (ct->c_cefile.ce_unlink) {
1203 (void) m_unlink (ct->c_cefile.ce_file);
1204 }
1205 free (ct->c_cefile.ce_file);
1206 }
1207 ct->c_cefile.ce_file = dest;
1208 ct->c_cefile.ce_unlink = 1;
1209
1210 ++*message_mods;
1211
1212 /* Update ct->c_ctline. */
1213 if (ct->c_ctline) {
1214 char *ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
1215 ct->c_ctinfo.ci_subtype, NULL);
1216 char *outline;
1217
1218 replace_param(&ct->c_ctinfo.ci_first_pm,
1219 &ct->c_ctinfo.ci_last_pm, "charset",
1220 dest_charset, 0);
1221 outline = output_params(strlen(TYPE_FIELD) + 1 + strlen(ctline),
1222 ct->c_ctinfo.ci_first_pm, NULL, 0);
1223 if (outline) {
1224 ctline = add(outline, ctline);
1225 free(outline);
1226 }
1227
1228 free (ct->c_ctline);
1229 ct->c_ctline = ctline;
1230 } /* else no CT line, which is odd */
1231
1232 /* Update Content-Type header field. */
1233 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1234 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1235 char *ctline = concat (ct->c_ctline, "\n", NULL);
1236
1237 free (hf->value);
1238 hf->value = ctline;
1239 break;
1240 }
1241 }
1242 } else {
1243 (void) m_unlink (dest);
1244 }
1245 free(dest_buffer);
1246 #else /* ! HAVE_ICONV */
1247 NMH_UNUSED (message_mods);
1248
1249 advise (NULL, "Can't convert %s to %s without iconv", src_charset,
1250 dest_charset);
1251 errno = ENOSYS;
1252 status = NOTOK;
1253 #endif /* ! HAVE_ICONV */
1254 }
1255
1256 free (src_charset);
1257 return status;
1258 }
1259
1260
1261 static int
1262 convert_content_charset (CT ct, char **file) {
1263 int status = OK;
1264
1265 #ifdef HAVE_ICONV
1266 /* Using current locale, see if the content needs to be converted. */
1267
1268 /* content_charset() cannot return NULL. */
1269 char *src_charset = content_charset (ct);
1270
1271 if (! check_charset (src_charset, strlen (src_charset))) {
1272 int unused = 0;
1273
1274 char *dest_charset = getcpy (get_charset ());
1275
1276 if (convert_charset (ct, dest_charset, &unused) == 0) {
1277 *file = ct->c_cefile.ce_file;
1278 } else {
1279 status = NOTOK;
1280 }
1281
1282 free (dest_charset);
1283 }
1284 free (src_charset);
1285 #else /* ! HAVE_ICONV */
1286 NMH_UNUSED (ct);
1287 NMH_UNUSED (file);
1288 #endif /* ! HAVE_ICONV */
1289
1290 return status;
1291 }
1292
1293 /*
1294 * Compile our format string and save any parameters we care about.
1295 */
1296
1297 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1298 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1299 "%<{description}%{description}" \
1300 "%?{cdispo-filename}%{cdispo-filename}" \
1301 "%|%{ctype-name}%> " \
1302 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1303
1304 static struct format *
1305 compile_header(char *form)
1306 {
1307 struct format *fmt;
1308 char *fmtstring;
1309 struct comp *comp = NULL;
1310 unsigned int bucket;
1311
1312 fmtstring = new_fs(form, NULL, DEFAULT_HEADER);
1313
1314 (void) fmt_compile(fmtstring, &fmt, 1);
1315 free_fs();
1316
1317 while ((comp = fmt_nextcomp(comp, &bucket)) != NULL) {
1318 if (strcasecmp(comp->c_name, "folder") == 0) {
1319 folder_comp = comp;
1320 }
1321 }
1322
1323 return fmt;
1324 }
1325
1326 static struct format *
1327 compile_marker(char *form)
1328 {
1329 struct format *fmt;
1330 char *fmtstring;
1331 struct comp *comp = NULL;
1332 unsigned int bucket;
1333 struct param_comp_list *pc_entry;
1334
1335 fmtstring = new_fs(form, NULL, DEFAULT_MARKER);
1336
1337 (void) fmt_compile(fmtstring, &fmt, 1);
1338 free_fs();
1339
1340 /*
1341 * Things we care about:
1342 *
1343 * part - Part name (e.g., 1.1)
1344 * content-type - Content-Type
1345 * description - Content-Description
1346 * disposition - Content-Disposition (inline, attachment)
1347 * ctype-<param> - Content-Type parameter
1348 * cdispo-<param> - Content-Disposition parameter
1349 */
1350
1351 while ((comp = fmt_nextcomp(comp, &bucket)) != NULL) {
1352 if (strcasecmp(comp->c_name, "part") == 0) {
1353 part_comp = comp;
1354 } else if (strcasecmp(comp->c_name, "content-type") == 0) {
1355 ctype_comp = comp;
1356 } else if (strcasecmp(comp->c_name, "description") == 0) {
1357 description_comp = comp;
1358 } else if (strcasecmp(comp->c_name, "disposition") == 0) {
1359 dispo_comp = comp;
1360 } else if (strncasecmp(comp->c_name, "ctype-", 6) == 0 &&
1361 strlen(comp->c_name) > 6) {
1362 NEW(pc_entry);
1363 pc_entry->param = mh_xstrdup(comp->c_name + 6);
1364 pc_entry->comp = comp;
1365 pc_entry->next = ctype_pc_list;
1366 ctype_pc_list = pc_entry;
1367 } else if (strncasecmp(comp->c_name, "cdispo-", 7) == 0 &&
1368 strlen(comp->c_name) > 7) {
1369 NEW(pc_entry);
1370 pc_entry->param = mh_xstrdup(comp->c_name + 7);
1371 pc_entry->comp = comp;
1372 pc_entry->next = dispo_pc_list;
1373 dispo_pc_list = pc_entry;
1374 }
1375 }
1376
1377 return fmt;
1378 }
1379
1380 /*
1381 * Output on stdout an appropriate marker for this content, using mh-format
1382 */
1383
1384 static void
1385 output_header(CT ct, struct format *fmt)
1386 {
1387 charstring_t outbuf = charstring_create (BUFSIZ);
1388 int dat[5] = { 0 };
1389 char *endp;
1390 int message = 0;
1391
1392 if (folder_comp)
1393 folder_comp->c_text = getcpy(folder);
1394
1395 if (ct->c_file && *ct->c_file) {
1396 message = strtol(ct->c_file, &endp, 10);
1397 if (*endp) message = 0;
1398 dat[0] = message;
1399 }
1400
1401 /* it would be nice to populate dat[2], for %(size) here,
1402 * but it's not available. it might also be nice to know
1403 * if the message originally had any mime parts or not -- but
1404 * there's also no record of that. (except for MIME-version:)
1405 */
1406
1407 fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
1408
1409 fputs(charstring_buffer (outbuf), stdout);
1410 charstring_free (outbuf);
1411
1412 fmt_freecomptext();
1413 }
1414
1415 static void
1416 output_marker(CT ct, struct format *fmt, int hidden)
1417 {
1418 charstring_t outbuf = charstring_create (BUFSIZ);
1419 struct param_comp_list *pcentry;
1420 int partsize;
1421 int message = 0;
1422 char *endp;
1423 int dat[5] = { 0 };
1424
1425 /*
1426 * Grab any items we care about.
1427 */
1428
1429 if (ctype_comp && ct->c_ctinfo.ci_type) {
1430 ctype_comp->c_text = concat(ct->c_ctinfo.ci_type, "/",
1431 ct->c_ctinfo.ci_subtype, NULL);
1432 }
1433
1434 if (part_comp && ct->c_partno) {
1435 part_comp->c_text = mh_xstrdup(ct->c_partno);
1436 }
1437
1438 if (description_comp && ct->c_descr) {
1439 description_comp->c_text = mh_xstrdup(ct->c_descr);
1440 }
1441
1442 if (dispo_comp && ct->c_dispo_type) {
1443 dispo_comp->c_text = mh_xstrdup(ct->c_dispo_type);
1444 }
1445
1446 for (pcentry = ctype_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1447 pcentry->comp->c_text = get_param(ct->c_ctinfo.ci_first_pm,
1448 pcentry->param, '?', 0);
1449 }
1450
1451 for (pcentry = dispo_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1452 pcentry->comp->c_text = get_param(ct->c_dispo_first,
1453 pcentry->param, '?', 0);
1454 }
1455
1456 if (ct->c_cesizefnx)
1457 partsize = (*ct->c_cesizefnx) (ct);
1458 else
1459 partsize = ct->c_end - ct->c_begin;
1460
1461 if (ct->c_file && *ct->c_file) {
1462 message = strtol(ct->c_file, &endp, 10);
1463 if (*endp) message = 0;
1464 dat[0] = message;
1465 }
1466 dat[2] = partsize;
1467
1468 /* make the part's hidden aspect available by overloading the
1469 * %(unseen) function. make the part's size available via %(size).
1470 * see comments in h/fmt_scan.h.
1471 */
1472 dat[4] = hidden;
1473
1474 fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
1475
1476 fputs(charstring_buffer (outbuf), stdout);
1477 charstring_free (outbuf);
1478
1479 fmt_freecomptext();
1480 }
1481
1482 /*
1483 * Reset (and free) any of the saved marker text
1484 */
1485
1486 static void
1487 free_markercomps(void)
1488 {
1489 struct param_comp_list *pc_entry, *pc2;
1490
1491 folder_comp = NULL;
1492 part_comp = NULL;
1493 ctype_comp = NULL;
1494 description_comp = NULL;
1495 dispo_comp = NULL;
1496
1497 for (pc_entry = ctype_pc_list; pc_entry != NULL; ) {
1498 free(pc_entry->param);
1499 pc2 = pc_entry->next;
1500 free(pc_entry);
1501 pc_entry = pc2;
1502 }
1503
1504 for (pc_entry = dispo_pc_list; pc_entry != NULL; ) {
1505 free(pc_entry->param);
1506 pc2 = pc_entry->next;
1507 free(pc_entry);
1508 pc_entry = pc2;
1509 }
1510 }
1511
1512 /*
1513 * Exit if the display process returned with a nonzero exit code, or terminated
1514 * with a SIGQUIT signal.
1515 */
1516
1517 static int
1518 pidcheck (int status)
1519 {
1520 if ((status & 0xff00) == 0xff00 || (status & 0x007f) != SIGQUIT)
1521 return status;
1522
1523 fflush (stdout);
1524 fflush (stderr);
1525 done (1);
1526 return 1;
1527 }