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