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