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