]> diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
b1ec2db572cdd0a3be6370906915371d672f2fbe
[nmh] / uip / mhshowsbr.c
1 /*
2 * mhshowsbr.c -- routines to display the contents of MIME messages
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/signals.h>
12 #include <h/md5.h>
13 #include <h/mts.h>
14 #include <h/tws.h>
15 #include <h/mime.h>
16 #include <h/mhparse.h>
17 #include <h/fmt_scan.h>
18 #include <h/utils.h>
19 #ifdef HAVE_ICONV
20 # include <iconv.h>
21 #endif /* ! HAVE_ICONV */
22
23 extern int debugsw;
24 extern int npart;
25 extern int ntype;
26
27 int nolist = 0;
28
29 char *progsw = NULL;
30
31 /* flags for moreproc/header display */
32 int nomore = 0;
33 char *formsw = NULL;
34
35 /* for output markerss and headers */
36 char *folder = NULL;
37 char *markerform;
38 char *headerform;
39 int headersw = -1;
40
41
42 /* mhmisc.c */
43 int part_ok (CT);
44 int part_exact (CT);
45 int type_ok (CT, int);
46 void content_error (char *, CT, char *, ...);
47 void flush_errors (void);
48
49 /*
50 * static prototypes
51 */
52 static void show_single_message (CT, char *, int, int, int, struct format *);
53 static void DisplayMsgHeader (CT, char *, int);
54 static int show_switch (CT, int, int, int, int, struct format *);
55 static int show_content (CT, int, int, int, struct format *);
56 static int show_content_aux2 (CT, int, char *, char *, int, int, int, struct format *);
57 static int show_text (CT, int, int, struct format *);
58 static int show_multi (CT, int, int, int, int, struct format *);
59 static int show_multi_internal (CT, int, int, int, int, struct format *);
60 static int show_multi_aux (CT, int, char *, struct format *);
61 static int show_message_rfc822 (CT, int, struct format *);
62 static int show_partial (CT, int);
63 static int show_external (CT, int, int, int, int, struct format *);
64 static int parse_display_string (CT, char *, int *, int *, char *, char *,
65 size_t, int multipart);
66 static int convert_content_charset (CT, char **);
67 static struct format *compile_header(char *);
68 static struct format *compile_marker(char *);
69 static void output_header (CT, struct format *);
70 static void output_marker (CT, struct format *, int);
71 static void free_markercomps (void);
72 static int pidcheck(int);
73
74 /*
75 * Components (and list of parameters/components) we care about for the
76 * content marker display.
77 */
78
79 static struct comp *folder_comp = NULL;
80 static struct comp *part_comp = NULL;
81 static struct comp *ctype_comp = NULL;
82 static struct comp *description_comp = NULL;
83 static struct comp *dispo_comp = NULL;
84
85 struct param_comp_list {
86 char *param;
87 struct comp *comp;
88 struct param_comp_list *next;
89 };
90
91 static struct param_comp_list *ctype_pc_list = NULL;
92 static struct param_comp_list *dispo_pc_list = NULL;
93
94
95 /*
96 * Top level entry point to show/display a group of messages
97 */
98
99 void
100 show_all_messages (CT *cts, int concatsw, int textonly, int inlineonly)
101 {
102 CT ct, *ctp;
103 struct format *hfmt, *mfmt;
104
105 /*
106 * If form is not specified, then get default form
107 * for showing headers of MIME messages.
108 */
109 if (!formsw)
110 formsw = getcpy (etcpath ("mhl.headers"));
111
112 /*
113 * Compile the content marker and header format lines
114 */
115 mfmt = compile_marker(markerform);
116 hfmt = compile_header(headerform);
117
118 /*
119 * If form is "mhl.null", suppress display of header.
120 */
121 if (!strcmp (formsw, "mhl.null"))
122 formsw = NULL;
123
124 for (ctp = cts; *ctp; ctp++) {
125 ct = *ctp;
126
127 /* if top-level type is ok, then display message */
128 if (type_ok (ct, 1)) {
129 if (headersw) output_header(ct, hfmt);
130
131 show_single_message (ct, formsw, concatsw, textonly, inlineonly,
132 mfmt);
133 }
134 }
135
136 free_markercomps();
137 fmt_free(hfmt, 1);
138 fmt_free(mfmt, 1);
139 }
140
141
142 /*
143 * Entry point to show/display a single message
144 */
145
146 static void
147 show_single_message (CT ct, char *form, int concatsw, int textonly,
148 int inlineonly, struct format *fmt)
149 {
150 sigset_t set, oset;
151
152 int status = OK;
153
154 /* Allow user executable bit so that temporary directories created by
155 * the viewer (e.g., lynx) are going to be accessible */
156 umask (ct->c_umask & ~(0100));
157
158 /*
159 * If you have a format file, then display
160 * the message headers.
161 */
162 if (form)
163 DisplayMsgHeader(ct, form, concatsw);
164
165 /* Show the body of the message */
166 show_switch (ct, 0, concatsw, textonly, inlineonly, fmt);
167
168 if (ct->c_fp) {
169 fclose (ct->c_fp);
170 ct->c_fp = NULL;
171 }
172 if (ct->c_ceclosefnx)
173 (*ct->c_ceclosefnx) (ct);
174
175 /* block a few signals */
176 sigemptyset (&set);
177 sigaddset (&set, SIGHUP);
178 sigaddset (&set, SIGINT);
179 sigaddset (&set, SIGQUIT);
180 sigaddset (&set, SIGTERM);
181 sigprocmask (SIG_BLOCK, &set, &oset);
182
183 while (!concatsw && wait (&status) != NOTOK) {
184 pidcheck (status);
185 continue;
186 }
187
188 /* reset the signal mask */
189 sigprocmask (SIG_SETMASK, &oset, &set);
190
191 flush_errors ();
192 }
193
194
195 /*
196 * Use the mhlproc to show the header fields
197 */
198
199 static void
200 DisplayMsgHeader (CT ct, char *form, int concatsw)
201 {
202 pid_t child_id;
203 int i, vecp;
204 char **vec;
205 char *file;
206
207 vec = argsplit(mhlproc, &file, &vecp);
208 vec[vecp++] = mh_xstrdup("-form");
209 vec[vecp++] = mh_xstrdup(form);
210 vec[vecp++] = mh_xstrdup("-nobody");
211 vec[vecp++] = getcpy(ct->c_file);
212
213 /*
214 * If we've specified -(no)moreproc,
215 * then just pass that along.
216 */
217 if (nomore || concatsw) {
218 vec[vecp++] = mh_xstrdup("-nomoreproc");
219 } else if (progsw) {
220 vec[vecp++] = mh_xstrdup("-moreproc");
221 vec[vecp++] = mh_xstrdup(progsw);
222 }
223 vec[vecp] = NULL;
224
225 fflush (stdout);
226
227 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
228 sleep (5);
229
230 switch (child_id) {
231 case NOTOK:
232 adios ("fork", "unable to");
233 /* NOTREACHED */
234
235 case OK:
236 execvp (file, vec);
237 fprintf (stderr, "unable to exec ");
238 perror (mhlproc);
239 _exit (-1);
240 /* NOTREACHED */
241
242 default:
243 pidcheck(pidwait(child_id, NOTOK));
244 break;
245 }
246
247 arglist_free(file, vec);
248 }
249
250
251 /*
252 * Switching routine. Call the correct routine
253 * based on content type.
254 */
255
256 static int
257 show_switch (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
258 struct format *fmt)
259 {
260 switch (ct->c_type) {
261 case CT_MULTIPART:
262 return show_multi (ct, alternate, concatsw, textonly,
263 inlineonly, fmt);
264
265 case CT_MESSAGE:
266 switch (ct->c_subtype) {
267 case MESSAGE_PARTIAL:
268 return show_partial (ct, alternate);
269
270 case MESSAGE_EXTERNAL:
271 return show_external (ct, alternate, concatsw, textonly,
272 inlineonly, fmt);
273
274 case MESSAGE_RFC822:
275 return show_message_rfc822 (ct, alternate, fmt);
276
277 /*
278 * Treat unknown message types as equivalent to
279 * application/octet-stream for now
280 */
281 default:
282 return show_content (ct, alternate, textonly,
283 inlineonly, fmt);
284 }
285
286 case CT_TEXT:
287 return show_text (ct, alternate, concatsw, fmt);
288
289 case CT_AUDIO:
290 case CT_IMAGE:
291 case CT_VIDEO:
292 case CT_APPLICATION:
293 default:
294 return show_content (ct, alternate, textonly, inlineonly, fmt);
295 }
296
297 return 0; /* NOT REACHED */
298 }
299
300
301 /*
302 * Generic method for displaying content
303 */
304
305 static int
306 show_content (CT ct, int alternate, int textonly, int inlineonly,
307 struct format *fmt)
308 {
309 char *cp;
310 CI ci = &ct->c_ctinfo;
311
312 /*
313 * If we're here, we are not a text type. So we don't need to check
314 * the content-type.
315 */
316
317 if (textonly || (inlineonly && !is_inline(ct))) {
318 output_marker(ct, fmt, 1);
319 return OK;
320 }
321
322 /* Check for invo_name-show-type[/subtype] */
323 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
324 return show_content_aux (ct, alternate, cp, NULL, fmt);
325
326 if ((cp = ct->c_showproc))
327 return show_content_aux (ct, alternate, cp, NULL, fmt);
328
329 /* complain if we are not a part of a multipart/alternative */
330 if (!alternate)
331 content_error (NULL, ct, "don't know how to display content");
332
333 return NOTOK;
334 }
335
336
337 /*
338 * Parse the display string for displaying generic content
339 */
340
341 int
342 show_content_aux (CT ct, int alternate, char *cp, char *cracked, struct format *fmt)
343 {
344 int fd;
345 int xstdin = 0, xlist = 0;
346 char *file = NULL, buffer[BUFSIZ];
347
348 if (!ct->c_ceopenfnx) {
349 if (!alternate)
350 content_error (NULL, ct, "don't know how to decode content");
351
352 return NOTOK;
353 }
354
355 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
356 return NOTOK;
357 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
358 return OK;
359
360 if (! strcmp(invo_name, "mhshow") &&
361 ct->c_type == CT_TEXT && ct->c_subtype == TEXT_PLAIN) {
362 /* This has to be done after calling c_ceopenfnx, so
363 unfortunately the type checks are necessary without
364 some code rearrangement. And to make this really ugly,
365 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
366 if (convert_content_charset (ct, &file) == OK) {
367 (*ct->c_ceclosefnx) (ct);
368 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
369 return NOTOK;
370 } else {
371 char *charset = content_charset (ct);
372 inform("unable to convert character set%s%s from %s, continuing...",
373 ct->c_partno ? " of part " : "",
374 ct->c_partno ? ct->c_partno : "",
375 charset);
376 free (charset);
377 }
378 }
379
380 if (cracked) {
381 strncpy (buffer, cp, sizeof(buffer));
382 goto got_command;
383 }
384
385 if (parse_display_string (ct, cp, &xstdin, &xlist, file, buffer,
386 sizeof(buffer) - 1, 0)) {
387 inform("Buffer overflow constructing show command, continuing...");
388 return NOTOK;
389 }
390
391 got_command:
392 return show_content_aux2 (ct, alternate, cracked, buffer,
393 fd, xlist, xstdin, fmt);
394 }
395
396
397 /*
398 * Routine to actually display the content
399 */
400
401 static int
402 show_content_aux2 (CT ct, int alternate, char *cracked, char *buffer,
403 int fd, int xlist, int xstdin, struct format *fmt)
404 {
405 pid_t child_id;
406 int i, vecp;
407 char **vec, *file;
408
409 if (debugsw || cracked) {
410 fflush (stdout);
411
412 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
413 ct->c_file);
414 if (ct->c_partno)
415 fprintf (stderr, " part %s", ct->c_partno);
416 if (cracked)
417 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
418 else
419 fprintf (stderr, " using command %s\n", buffer);
420 }
421
422 if (xlist && fmt) {
423 output_marker(ct, fmt, 0);
424 }
425
426 /*
427 * If the command is a zero-length string, just write the output on
428 * stdout.
429 */
430
431 if (buffer[0] == '\0') {
432 char readbuf[BUFSIZ];
433 ssize_t cc;
434 char lastchar = '\n';
435
436 if (fd == NOTOK) {
437 inform("Cannot use NULL command to display content-type "
438 "%s/%s", ct->c_ctinfo.ci_type, ct->c_ctinfo.ci_subtype);
439 return NOTOK;
440 }
441
442 while ((cc = read(fd, readbuf, sizeof(readbuf))) > 0) {
443 if ((ssize_t) fwrite(readbuf, sizeof(char), cc, stdout) < cc) {
444 advise ("putline", "fwrite");
445 }
446 lastchar = readbuf[cc - 1];
447 }
448
449 if (cc < 0) {
450 advise("read", "while reading text content");
451 return NOTOK;
452 }
453
454 /*
455 * The MIME standards allow content to not have a trailing newline.
456 * But because we are (presumably) sending this to stdout, include
457 * a newline for text content if the final character was not a
458 * newline. Only do this for mhshow.
459 */
460
461 if (strcmp(invo_name, "mhshow") == 0 && ct->c_type == CT_TEXT &&
462 ct->c_subtype == TEXT_PLAIN && lastchar != '\n') {
463 putchar('\n');
464 }
465
466 fflush(stdout);
467
468 return OK;
469 }
470
471 vec = argsplit(buffer, &file, &vecp);
472 vec[vecp++] = NULL;
473
474 fflush (stdout);
475
476 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
477 sleep (5);
478 switch (child_id) {
479 case NOTOK:
480 advise ("fork", "unable to");
481 (*ct->c_ceclosefnx) (ct);
482 return NOTOK;
483
484 case OK:
485 if (cracked) {
486 if (chdir (cracked) < 0) {
487 advise (cracked, "chdir");
488 }
489 }
490 if (!xstdin)
491 dup2 (fd, 0);
492 close (fd);
493 execvp (file, vec);
494 fprintf (stderr, "unable to exec ");
495 perror (buffer);
496 _exit (-1);
497 /* NOTREACHED */
498
499 default: {
500 int status;
501
502 arglist_free(file, vec);
503
504 pidcheck ((status = pidXwait (child_id, NULL)));
505
506 if (fd != NOTOK)
507 (*ct->c_ceclosefnx) (ct);
508 return (alternate ? OK : status);
509 }
510 }
511 }
512
513
514 /*
515 * show content of type "text"
516 */
517
518 static int
519 show_text (CT ct, int alternate, int concatsw, struct format *fmt)
520 {
521 char *cp, buffer[BUFSIZ];
522 CI ci = &ct->c_ctinfo;
523
524 /* Check for invo_name-show-type[/subtype] */
525 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
526 return show_content_aux (ct, alternate, cp, NULL, fmt);
527
528 /*
529 * Use default method if content is text/plain, or if
530 * if it is not a text part of a multipart/alternative
531 */
532 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
533 if (concatsw) {
534 if (ct->c_termproc)
535 snprintf(buffer, sizeof(buffer), "%%lcat");
536 else
537 snprintf(buffer, sizeof(buffer), "%%l");
538 } else
539 snprintf (buffer, sizeof(buffer), "%%l%s %%F", progsw ? progsw :
540 moreproc && *moreproc ? moreproc : DEFAULT_PAGER);
541 cp = (ct->c_showproc = mh_xstrdup(buffer));
542 return show_content_aux (ct, alternate, cp, NULL, fmt);
543 }
544
545 return NOTOK;
546 }
547
548
549 /*
550 * show message body of type "multipart"
551 */
552
553 static int
554 show_multi (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
555 struct format *fmt)
556 {
557 char *cp;
558 CI ci = &ct->c_ctinfo;
559
560 /* Check for invo_name-show-type[/subtype] */
561 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
562 return show_multi_aux (ct, alternate, cp, fmt);
563
564 if ((cp = ct->c_showproc)) {
565 return show_multi_aux (ct, alternate, cp, fmt);
566 }
567
568 /*
569 * Use default method to display this multipart content. Even
570 * unknown types are displayable, since they're treated as mixed
571 * per RFC 2046.
572 */
573 return show_multi_internal (ct, alternate, concatsw, textonly,
574 inlineonly, fmt);
575 }
576
577
578 /*
579 * show message body of subtypes of multipart that
580 * we understand directly (mixed, alternate, etc...)
581 */
582
583 static int
584 show_multi_internal (CT ct, int alternate, int concatsw, int textonly,
585 int inlineonly, struct format *fmt)
586 {
587 int alternating, nowalternate, result;
588 struct multipart *m = (struct multipart *) ct->c_ctparams;
589 struct part *part;
590 int request_matched;
591 int display_success;
592 int mult_alt_done;
593 int ret;
594 CT p;
595
596 alternating = 0;
597 nowalternate = alternate;
598
599 if (ct->c_subtype == MULTI_ALTERNATE) {
600 nowalternate = 1;
601 alternating = 1;
602 }
603
604 /*
605 * alternate -> we are a part inside a multipart/alternative
606 * alternating -> we are a multipart/alternative
607 */
608
609 result = NOTOK;
610 request_matched = 0;
611 display_success = 0;
612 mult_alt_done = 0;
613
614 for (part = m->mp_parts; part; part = part->mp_next) {
615 p = part->mp_part;
616
617 /* while looking for the right displayable alternative, we
618 * use a looser search criterion than we do after finding it.
619 * specifically, while still looking, part_ok() will match
620 * "parent" parts (e.g. "-part 2" where 2 is a high-level
621 * multipart). after finding it, we use part_exact() to only
622 * choose a part that was requested explicitly.
623 */
624 if ((part_exact(p) && type_ok(p, 1)) ||
625 (!mult_alt_done && part_ok (p) && type_ok (p, 1))) {
626
627 int inneresult;
628
629 inneresult = show_switch (p, nowalternate, concatsw, textonly,
630 inlineonly, fmt);
631 switch (inneresult) {
632 case NOTOK: /* hard display error */
633 request_matched = 1;
634 if (alternate && !alternating) {
635 result = NOTOK;
636 goto out;
637 }
638 continue;
639
640 case DONE: /* found no match on content type */
641 continue;
642
643 case OK: /* display successful */
644 request_matched = 1;
645 display_success = 1;
646 result = OK;
647
648 /* if we got success on a sub-part of
649 * multipart/alternative, we're done, unless
650 * there's a chance an explicit part should be
651 * matched later in the alternatives. */
652 if (alternating) {
653 mult_alt_done = 1;
654 } else if (alternate) {
655 alternate = nowalternate = 0;
656 }
657 continue;
658 }
659 break;
660 }
661 }
662
663 /* we're supposed to be displaying at least something from a
664 * multipart/alternative. if we've had parts to consider, and
665 * we've had no success, then we should complain. we shouldn't
666 * complain if none of the parts matched any -part or -type option.
667 */
668 if (alternating && request_matched && !display_success) {
669 /* if we're ourselves an alternate. don't complain yet. */
670 if (!alternate)
671 content_error (NULL, ct, "don't know how to display any of the contents");
672 result = NOTOK;
673 }
674
675 out:
676 /* if no parts matched what was requested, there can't have been
677 * any display errors. we report DONE rather than OK. */
678 ret = request_matched ? result : DONE;
679 return ret;
680 }
681
682
683 /*
684 * Parse display string for multipart content
685 * and use external program to display it.
686 */
687
688 static int
689 show_multi_aux (CT ct, int alternate, char *cp, struct format *fmt)
690 {
691 /* xstdin is only used in the call to parse_display_string():
692 its value is ignored in the function. */
693 int xstdin = 0, xlist = 0;
694 char *file = NULL, buffer[BUFSIZ];
695 struct multipart *m = (struct multipart *) ct->c_ctparams;
696 struct part *part;
697 CT p;
698
699 for (part = m->mp_parts; part; part = part->mp_next) {
700 p = part->mp_part;
701
702 if (!p->c_ceopenfnx) {
703 if (!alternate)
704 content_error (NULL, p, "don't know how to decode content");
705 return NOTOK;
706 }
707
708 if (p->c_storage == NULL) {
709 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
710 return NOTOK;
711
712 p->c_storage = add (file, NULL);
713
714 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
715 return OK;
716 (*p->c_ceclosefnx) (p);
717 }
718 }
719
720 if (parse_display_string (ct, cp, &xstdin, &xlist, file,
721 buffer, sizeof(buffer) - 1, 1)) {
722 inform("Buffer overflow constructing show command, continuing...");
723 return NOTOK;
724 }
725
726 return show_content_aux2 (ct, alternate, NULL, buffer, NOTOK, xlist, 0, fmt);
727 }
728
729
730 /*
731 * show content of type "message/rfc822"
732 */
733
734 static int
735 show_message_rfc822 (CT ct, int alternate, struct format *fmt)
736 {
737 char *cp;
738 CI ci = &ct->c_ctinfo;
739
740 /* Check for invo_name-show-type[/subtype] */
741 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
742 return show_content_aux (ct, alternate, cp, NULL, fmt);
743
744 if ((cp = ct->c_showproc))
745 return show_content_aux (ct, alternate, cp, NULL, fmt);
746
747 /* default method for message/rfc822 */
748 if (ct->c_subtype == MESSAGE_RFC822) {
749 cp = (ct->c_showproc = mh_xstrdup("%pshow -file %F"));
750 return show_content_aux (ct, alternate, cp, NULL, fmt);
751 }
752
753 /* complain if we are not a part of a multipart/alternative */
754 if (!alternate)
755 content_error (NULL, ct, "don't know how to display content");
756
757 return NOTOK;
758 }
759
760
761 /*
762 * Show content of type "message/partial".
763 */
764
765 static int
766 show_partial (CT ct, int alternate)
767 {
768 NMH_UNUSED (alternate);
769
770 content_error (NULL, ct,
771 "in order to display this message, you must reassemble it");
772 return NOTOK;
773 }
774
775
776 /*
777 * Show content of type "message/external".
778 *
779 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
780 */
781
782 static int
783 show_external (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
784 struct format *fmt)
785 {
786 struct exbody *e = (struct exbody *) ct->c_ctparams;
787 CT p = e->eb_content;
788
789 if (!type_ok (p, 0))
790 return OK;
791
792 return show_switch (p, alternate, concatsw, textonly, inlineonly, fmt);
793 }
794
795
796 static int
797 parse_display_string (CT ct, char *cp, int *xstdin, int *xlist,
798 char *file, char *buffer, size_t buflen,
799 int multipart) {
800 int len, quoted = 0;
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 = 1;
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 = 0;
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 -= 1;
995 bp += 1;
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 = 0;
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 char *src_charset = content_charset (ct);
1065 int status = OK;
1066
1067 if (strcasecmp (src_charset, dest_charset)) {
1068 #ifdef HAVE_ICONV
1069 iconv_t conv_desc = NULL;
1070 char *dest;
1071 int fd = -1;
1072 char **file = NULL;
1073 FILE **fp = NULL;
1074 size_t begin;
1075 size_t end;
1076 int opened_input_file = 0;
1077 char src_buffer[BUFSIZ];
1078 size_t dest_buffer_size = BUFSIZ;
1079 char *dest_buffer = mh_xmalloc(dest_buffer_size);
1080 HF hf;
1081 char *tempfile;
1082 int fromutf8 = !strcasecmp(src_charset, "UTF-8");
1083
1084 if ((conv_desc = iconv_open (dest_charset, src_charset)) ==
1085 (iconv_t) -1) {
1086 inform("Can't convert %s to %s", src_charset, dest_charset);
1087 free (src_charset);
1088 return NOTOK;
1089 }
1090
1091 if ((tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
1092 adios (NULL, "unable to create temporary file in %s",
1093 get_temp_dir());
1094 }
1095 dest = mh_xstrdup(tempfile);
1096
1097 if (ct->c_cefile.ce_file) {
1098 file = &ct->c_cefile.ce_file;
1099 fp = &ct->c_cefile.ce_fp;
1100 begin = end = 0;
1101 } else if (ct->c_file) {
1102 file = &ct->c_file;
1103 fp = &ct->c_fp;
1104 begin = (size_t) ct->c_begin;
1105 end = (size_t) ct->c_end;
1106 } /* else no input file: shouldn't happen */
1107
1108 if (file && *file && fp) {
1109 if (! *fp) {
1110 if ((*fp = fopen (*file, "r")) == NULL) {
1111 advise (*file, "unable to open for reading");
1112 status = NOTOK;
1113 } else {
1114 opened_input_file = 1;
1115 }
1116 }
1117 }
1118
1119 if (fp && *fp) {
1120 size_t inbytes;
1121 size_t bytes_to_read =
1122 end > 0 && end > begin ? end - begin : sizeof src_buffer;
1123
1124 fseeko (*fp, begin, SEEK_SET);
1125 while ((inbytes = fread (src_buffer, 1,
1126 min (bytes_to_read, sizeof src_buffer),
1127 *fp)) > 0) {
1128 ICONV_CONST char *ib = src_buffer;
1129 char *ob = dest_buffer;
1130 size_t outbytes = dest_buffer_size;
1131 size_t outbytes_before = outbytes;
1132
1133 if (end > 0) bytes_to_read -= inbytes;
1134
1135 iconv_start:
1136 if (iconv (conv_desc, &ib, &inbytes, &ob, &outbytes) ==
1137 (size_t) -1) {
1138 if (errno == E2BIG) {
1139 /*
1140 * Bump up the buffer by at least a factor of 2
1141 * over what we need.
1142 */
1143 size_t bumpup = inbytes * 2, ob_off = ob - dest_buffer;
1144 dest_buffer_size += bumpup;
1145 dest_buffer = mh_xrealloc(dest_buffer,
1146 dest_buffer_size);
1147 ob = dest_buffer + ob_off;
1148 outbytes += bumpup;
1149 outbytes_before += bumpup;
1150 goto iconv_start;
1151 }
1152 if (errno == EINVAL) {
1153 /* middle of multi-byte sequence */
1154 if (write (fd, dest_buffer, outbytes_before - outbytes) < 0) {
1155 advise (dest, "write");
1156 }
1157 fseeko (*fp, -inbytes, SEEK_CUR);
1158 if (end > 0) bytes_to_read += inbytes;
1159 /* inform("convert_charset: EINVAL"); */
1160 continue;
1161 }
1162 if (errno == EILSEQ) {
1163 /* invalid multi-byte sequence */
1164 if (fromutf8) {
1165 for (++ib, --inbytes;
1166 inbytes > 0 &&
1167 (((unsigned char) *ib) & 0xc0) == 0x80;
1168 ++ib, --inbytes)
1169 continue;
1170 } else {
1171 ib++; inbytes--; /* skip it */
1172 }
1173 (*ob++) = '?'; outbytes --;
1174 /* inform("convert_charset: EILSEQ"); */
1175 goto iconv_start;
1176 }
1177 inform("convert_charset: errno = %d", errno);
1178 status = NOTOK;
1179 break;
1180 } else {
1181 if (write (fd, dest_buffer, outbytes_before - outbytes)
1182 < 0) {
1183 advise (dest, "write");
1184 }
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(strlen(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 int status = OK;
1262
1263 #ifdef HAVE_ICONV
1264 /* Using current locale, see if the content needs to be converted. */
1265
1266 /* content_charset() cannot return NULL. */
1267 char *src_charset = content_charset (ct);
1268
1269 if (! check_charset (src_charset, strlen (src_charset))) {
1270 int unused = 0;
1271
1272 char *dest_charset = getcpy (get_charset ());
1273
1274 if (convert_charset (ct, dest_charset, &unused) == 0) {
1275 *file = ct->c_cefile.ce_file;
1276 } else {
1277 status = NOTOK;
1278 }
1279
1280 free (dest_charset);
1281 }
1282 free (src_charset);
1283 #else /* ! HAVE_ICONV */
1284 NMH_UNUSED (ct);
1285 NMH_UNUSED (file);
1286 #endif /* ! HAVE_ICONV */
1287
1288 return status;
1289 }
1290
1291 /*
1292 * Compile our format string and save any parameters we care about.
1293 */
1294
1295 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1296 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1297 "%<{description}%{description}" \
1298 "%?{cdispo-filename}%{cdispo-filename}" \
1299 "%|%{ctype-name}%> " \
1300 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1301
1302 static struct format *
1303 compile_header(char *form)
1304 {
1305 struct format *fmt;
1306 char *fmtstring;
1307 struct comp *comp = NULL;
1308 unsigned int bucket;
1309
1310 fmtstring = new_fs(form, NULL, DEFAULT_HEADER);
1311
1312 (void) fmt_compile(fmtstring, &fmt, 1);
1313 free_fs();
1314
1315 while ((comp = fmt_nextcomp(comp, &bucket)) != NULL) {
1316 if (strcasecmp(comp->c_name, "folder") == 0) {
1317 folder_comp = comp;
1318 }
1319 }
1320
1321 return fmt;
1322 }
1323
1324 static struct format *
1325 compile_marker(char *form)
1326 {
1327 struct format *fmt;
1328 char *fmtstring;
1329 struct comp *comp = NULL;
1330 unsigned int bucket;
1331 struct param_comp_list *pc_entry;
1332
1333 fmtstring = new_fs(form, NULL, DEFAULT_MARKER);
1334
1335 (void) fmt_compile(fmtstring, &fmt, 1);
1336 free_fs();
1337
1338 /*
1339 * Things we care about:
1340 *
1341 * part - Part name (e.g., 1.1)
1342 * content-type - Content-Type
1343 * description - Content-Description
1344 * disposition - Content-Disposition (inline, attachment)
1345 * ctype-<param> - Content-Type parameter
1346 * cdispo-<param> - Content-Disposition parameter
1347 */
1348
1349 while ((comp = fmt_nextcomp(comp, &bucket)) != NULL) {
1350 if (strcasecmp(comp->c_name, "part") == 0) {
1351 part_comp = comp;
1352 } else if (strcasecmp(comp->c_name, "content-type") == 0) {
1353 ctype_comp = comp;
1354 } else if (strcasecmp(comp->c_name, "description") == 0) {
1355 description_comp = comp;
1356 } else if (strcasecmp(comp->c_name, "disposition") == 0) {
1357 dispo_comp = comp;
1358 } else if (strncasecmp(comp->c_name, "ctype-", 6) == 0 &&
1359 strlen(comp->c_name) > 6) {
1360 NEW(pc_entry);
1361 pc_entry->param = mh_xstrdup(comp->c_name + 6);
1362 pc_entry->comp = comp;
1363 pc_entry->next = ctype_pc_list;
1364 ctype_pc_list = pc_entry;
1365 } else if (strncasecmp(comp->c_name, "cdispo-", 7) == 0 &&
1366 strlen(comp->c_name) > 7) {
1367 NEW(pc_entry);
1368 pc_entry->param = mh_xstrdup(comp->c_name + 7);
1369 pc_entry->comp = comp;
1370 pc_entry->next = dispo_pc_list;
1371 dispo_pc_list = pc_entry;
1372 }
1373 }
1374
1375 return fmt;
1376 }
1377
1378 /*
1379 * Output on stdout an appropriate marker for this content, using mh-format
1380 */
1381
1382 static void
1383 output_header(CT ct, struct format *fmt)
1384 {
1385 charstring_t outbuf = charstring_create (BUFSIZ);
1386 int dat[5] = { 0 };
1387 char *endp;
1388 int message = 0;
1389
1390 if (folder_comp)
1391 folder_comp->c_text = getcpy(folder);
1392
1393 if (ct->c_file && *ct->c_file) {
1394 message = strtol(ct->c_file, &endp, 10);
1395 if (*endp) message = 0;
1396 dat[0] = message;
1397 }
1398
1399 /* it would be nice to populate dat[2], for %(size) here,
1400 * but it's not available. it might also be nice to know
1401 * if the message originally had any mime parts or not -- but
1402 * there's also no record of that. (except for MIME-version:)
1403 */
1404
1405 fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
1406
1407 fputs(charstring_buffer (outbuf), stdout);
1408 charstring_free (outbuf);
1409
1410 fmt_freecomptext();
1411 }
1412
1413 static void
1414 output_marker(CT ct, struct format *fmt, int hidden)
1415 {
1416 charstring_t outbuf = charstring_create (BUFSIZ);
1417 struct param_comp_list *pcentry;
1418 int partsize;
1419 int message = 0;
1420 char *endp;
1421 int dat[5] = { 0 };
1422
1423 /*
1424 * Grab any items we care about.
1425 */
1426
1427 if (ctype_comp && ct->c_ctinfo.ci_type) {
1428 ctype_comp->c_text = concat(ct->c_ctinfo.ci_type, "/",
1429 ct->c_ctinfo.ci_subtype, NULL);
1430 }
1431
1432 if (part_comp && ct->c_partno) {
1433 part_comp->c_text = mh_xstrdup(ct->c_partno);
1434 }
1435
1436 if (description_comp && ct->c_descr) {
1437 description_comp->c_text = mh_xstrdup(ct->c_descr);
1438 }
1439
1440 if (dispo_comp && ct->c_dispo_type) {
1441 dispo_comp->c_text = mh_xstrdup(ct->c_dispo_type);
1442 }
1443
1444 for (pcentry = ctype_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1445 pcentry->comp->c_text = get_param(ct->c_ctinfo.ci_first_pm,
1446 pcentry->param, '?', 0);
1447 }
1448
1449 for (pcentry = dispo_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1450 pcentry->comp->c_text = get_param(ct->c_dispo_first,
1451 pcentry->param, '?', 0);
1452 }
1453
1454 if (ct->c_cesizefnx)
1455 partsize = (*ct->c_cesizefnx) (ct);
1456 else
1457 partsize = ct->c_end - ct->c_begin;
1458
1459 if (ct->c_file && *ct->c_file) {
1460 message = strtol(ct->c_file, &endp, 10);
1461 if (*endp) message = 0;
1462 dat[0] = message;
1463 }
1464 dat[2] = partsize;
1465
1466 /* make the part's hidden aspect available by overloading the
1467 * %(unseen) function. make the part's size available via %(size).
1468 * see comments in h/fmt_scan.h.
1469 */
1470 dat[4] = hidden;
1471
1472 fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
1473
1474 fputs(charstring_buffer (outbuf), stdout);
1475 charstring_free (outbuf);
1476
1477 fmt_freecomptext();
1478 }
1479
1480 /*
1481 * Reset (and free) any of the saved marker text
1482 */
1483
1484 static void
1485 free_markercomps(void)
1486 {
1487 struct param_comp_list *pc_entry, *pc2;
1488
1489 folder_comp = NULL;
1490 part_comp = NULL;
1491 ctype_comp = NULL;
1492 description_comp = NULL;
1493 dispo_comp = NULL;
1494
1495 for (pc_entry = ctype_pc_list; pc_entry != NULL; ) {
1496 free(pc_entry->param);
1497 pc2 = pc_entry->next;
1498 free(pc_entry);
1499 pc_entry = pc2;
1500 }
1501
1502 for (pc_entry = dispo_pc_list; pc_entry != NULL; ) {
1503 free(pc_entry->param);
1504 pc2 = pc_entry->next;
1505 free(pc_entry);
1506 pc_entry = pc2;
1507 }
1508 }
1509
1510 /*
1511 * Exit if the display process returned with a nonzero exit code, or terminated
1512 * with a SIGQUIT signal.
1513 */
1514
1515 static int
1516 pidcheck (int status)
1517 {
1518 if ((status & 0xff00) == 0xff00 || (status & 0x007f) != SIGQUIT)
1519 return status;
1520
1521 fflush (stdout);
1522 fflush (stderr);
1523 done (1);
1524 return 1;
1525 }