]> diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
Another pass at cleaning up (some of) the manpages.
[nmh] / uip / mhshowsbr.c
1
2 /*
3 * mhshowsbr.c -- routines to display the contents of MIME messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/fmt_scan.h>
19 #include <h/utils.h>
20 #ifdef HAVE_ICONV
21 # include <iconv.h>
22 #endif /* ! HAVE_ICONV */
23
24 extern int debugsw;
25 extern int npart;
26 extern int ntype;
27
28 int nolist = 0;
29
30 char *progsw = NULL;
31
32 /* flags for moreproc/header display */
33 int nomore = 0;
34 char *formsw = NULL;
35
36 /* for output markerss and headers */
37 char *folder = NULL;
38 char *markerform;
39 char *headerform;
40 int headersw = -1;
41
42
43 /* mhmisc.c */
44 int part_ok (CT);
45 int part_exact (CT);
46 int type_ok (CT, int);
47 void content_error (char *, CT, char *, ...);
48 void flush_errors (void);
49
50 /*
51 * static prototypes
52 */
53 static void show_single_message (CT, char *, int, int, int, struct format *);
54 static void DisplayMsgHeader (CT, char *, int);
55 static int show_switch (CT, int, int, int, int, struct format *);
56 static int show_content (CT, int, int, int, struct format *);
57 static int show_content_aux2 (CT, int, char *, char *, int, int, int, struct format *);
58 static int show_text (CT, int, int, struct format *);
59 static int show_multi (CT, int, int, int, int, struct format *);
60 static int show_multi_internal (CT, int, int, int, int, struct format *);
61 static int show_multi_aux (CT, int, char *, struct format *);
62 static int show_message_rfc822 (CT, int, struct format *);
63 static int show_partial (CT, int);
64 static int show_external (CT, int, int, int, int, struct format *);
65 static int parse_display_string (CT, char *, int *, int *, char *, char *,
66 size_t, int multipart);
67 static int convert_content_charset (CT, char **);
68 static struct format *compile_header(char *);
69 static struct format *compile_marker(char *);
70 static void output_header (CT, struct format *);
71 static void output_marker (CT, struct format *, int);
72 static void free_markercomps (void);
73 static int pidcheck(int);
74
75 /*
76 * Components (and list of parameters/components) we care about for the
77 * content marker display.
78 */
79
80 static struct comp *folder_comp = NULL;
81 static struct comp *part_comp = NULL;
82 static struct comp *ctype_comp = NULL;
83 static struct comp *description_comp = NULL;
84 static struct comp *dispo_comp = NULL;
85
86 struct param_comp_list {
87 char *param;
88 struct comp *comp;
89 struct param_comp_list *next;
90 };
91
92 static struct param_comp_list *ctype_pc_list = NULL;
93 static struct param_comp_list *dispo_pc_list = NULL;
94
95
96 /*
97 * Top level entry point to show/display a group of messages
98 */
99
100 void
101 show_all_messages (CT *cts, int concatsw, int textonly, int inlineonly)
102 {
103 CT ct, *ctp;
104 struct format *hfmt, *mfmt;
105
106 /*
107 * If form is not specified, then get default form
108 * for showing headers of MIME messages.
109 */
110 if (!formsw)
111 formsw = getcpy (etcpath ("mhl.headers"));
112
113 /*
114 * Compile the content marker and header format lines
115 */
116 mfmt = compile_marker(markerform);
117 hfmt = compile_header(headerform);
118
119 /*
120 * If form is "mhl.null", suppress display of header.
121 */
122 if (!strcmp (formsw, "mhl.null"))
123 formsw = NULL;
124
125 for (ctp = cts; *ctp; ctp++) {
126 ct = *ctp;
127
128 /* if top-level type is ok, then display message */
129 if (type_ok (ct, 1))
130 if (headersw) output_header(ct, hfmt);
131
132 show_single_message (ct, formsw, concatsw, textonly, inlineonly,
133 mfmt);
134 }
135
136 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++] = getcpy("-form");
209 vec[vecp++] = getcpy(form);
210 vec[vecp++] = getcpy("-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++] = getcpy("-nomoreproc");
219 } else if (progsw) {
220 vec[vecp++] = getcpy("-moreproc");
221 vec[vecp++] = getcpy(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 admonish (NULL, "unable to convert character set%s%s to %s",
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 admonish (NULL, "Buffer overflow constructing show command!\n");
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 advise(NULL, "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 putc('\n', stdout);
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 = add (buffer, NULL));
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 admonish (NULL, "Buffer overflow constructing show command!\n");
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 = add ("%pshow -file %F", NULL));
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 /* and fall... */
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 /* and fall... */
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 admonish(NULL,
946 "no closing brace for display string escape %s",
947 cp);
948 }
949 break;
950 }
951
952 default:
953 *bp++ = *--cp;
954 *bp = '\0';
955 buflen--;
956 continue;
957 }
958 len = strlen (bp);
959 bp += len;
960 buflen -= len;
961 *bp = '\0';
962
963 /* Did we actually insert something? */
964 if (bp != pp) {
965 /* Insert single quote if not inside quotes already */
966 if (!quoted && buflen) {
967 len = strlen (pp);
968 memmove (pp + 1, pp, len+1);
969 *pp++ = '\'';
970 buflen--;
971 bp++;
972 quoted = 1;
973 }
974 /* Escape existing quotes */
975 while ((pp = strchr (pp, '\'')) && buflen > 3) {
976 len = strlen (pp++);
977 if (quoted) {
978 /* Quoted. Let this quote close that quoting.
979 Insert an escaped quote to replace it and
980 another quote to reopen quoting, which will be
981 closed below. */
982 memmove (pp + 2, pp, len);
983 *pp++ = '\\';
984 *pp++ = '\'';
985 buflen -= 2;
986 bp += 2;
987 quoted = 0;
988 } else {
989 /* Not quoted. This should not be reached with
990 the current code, but handle the condition
991 in case the code changes. Just escape the
992 quote. */
993 memmove (pp, pp-1, len+1);
994 *(pp++-1) = '\\';
995 buflen -= 1;
996 bp += 1;
997 }
998 }
999 /* If pp is still set, that means we ran out of space. */
1000 if (pp)
1001 buflen = 0;
1002 /* Close quoting. */
1003 if (quoted && buflen) {
1004 /* See if we need to close the quote by looking
1005 for an odd number of unescaped close quotes in
1006 the remainder of the display string. */
1007 int found_quote = 0, escaped = 0;
1008 char *c;
1009
1010 for (c = cp+1; *c; ++c) {
1011 if (*c == '\\') {
1012 escaped = ! escaped;
1013 } else {
1014 if (escaped) {
1015 escaped = 0;
1016 } else {
1017 if (*c == '\'') {
1018 found_quote = ! found_quote;
1019 }
1020 }
1021 }
1022 }
1023 if (! found_quote) {
1024 *bp++ = '\'';
1025 buflen--;
1026 quoted = 0;
1027 }
1028 }
1029 }
1030 } else {
1031 raw:
1032 *bp++ = *cp;
1033 buflen--;
1034
1035 if (*cp == '\'')
1036 quoted = !quoted;
1037 }
1038
1039 *bp = '\0';
1040 }
1041
1042 if (buflen <= 0 ||
1043 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1044 /* content_error would provide a more useful error message
1045 * here, except that if we got overrun, it probably would
1046 * too.
1047 */
1048 return NOTOK;
1049 }
1050
1051 /* use charset string to modify display method */
1052 if (ct->c_termproc) {
1053 char term[BUFSIZ];
1054
1055 strncpy (term, buffer, sizeof(term));
1056 snprintf (buffer, buflen, ct->c_termproc, term);
1057 }
1058
1059 return OK;
1060 }
1061
1062
1063 int
1064 convert_charset (CT ct, char *dest_charset, int *message_mods) {
1065 char *src_charset = content_charset (ct);
1066 int status = OK;
1067
1068 if (strcasecmp (src_charset, dest_charset)) {
1069 #ifdef HAVE_ICONV
1070 iconv_t conv_desc = NULL;
1071 char *dest;
1072 int fd = -1;
1073 char **file = NULL;
1074 FILE **fp = NULL;
1075 size_t begin;
1076 size_t end;
1077 int opened_input_file = 0;
1078 char src_buffer[BUFSIZ];
1079 size_t dest_buffer_size = BUFSIZ;
1080 char *dest_buffer = mh_xmalloc(dest_buffer_size);
1081 HF hf;
1082 char *tempfile;
1083 int fromutf8 = !strcasecmp(src_charset, "UTF-8");
1084
1085 if ((conv_desc = iconv_open (dest_charset, src_charset)) ==
1086 (iconv_t) -1) {
1087 advise (NULL, "Can't convert %s to %s", src_charset, dest_charset);
1088 free (src_charset);
1089 return NOTOK;
1090 }
1091
1092 if ((tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
1093 adios (NULL, "unable to create temporary file in %s",
1094 get_temp_dir());
1095 }
1096 dest = add (tempfile, NULL);
1097
1098 if (ct->c_cefile.ce_file) {
1099 file = &ct->c_cefile.ce_file;
1100 fp = &ct->c_cefile.ce_fp;
1101 begin = end = 0;
1102 } else if (ct->c_file) {
1103 file = &ct->c_file;
1104 fp = &ct->c_fp;
1105 begin = (size_t) ct->c_begin;
1106 end = (size_t) ct->c_end;
1107 } /* else no input file: shouldn't happen */
1108
1109 if (file && *file && fp) {
1110 if (! *fp) {
1111 if ((*fp = fopen (*file, "r")) == NULL) {
1112 advise (*file, "unable to open for reading");
1113 status = NOTOK;
1114 } else {
1115 opened_input_file = 1;
1116 }
1117 }
1118 }
1119
1120 if (fp && *fp) {
1121 size_t inbytes;
1122 size_t bytes_to_read =
1123 end > 0 && end > begin ? end - begin : sizeof src_buffer;
1124
1125 fseeko (*fp, begin, SEEK_SET);
1126 while ((inbytes = fread (src_buffer, 1,
1127 min (bytes_to_read, sizeof src_buffer),
1128 *fp)) > 0) {
1129 ICONV_CONST char *ib = src_buffer;
1130 char *ob = dest_buffer;
1131 size_t outbytes = dest_buffer_size;
1132 size_t outbytes_before = outbytes;
1133
1134 if (end > 0) bytes_to_read -= inbytes;
1135
1136 iconv_start:
1137 if (iconv (conv_desc, &ib, &inbytes, &ob, &outbytes) ==
1138 (size_t) -1) {
1139 if (errno == E2BIG) {
1140 /*
1141 * Bump up the buffer by at least a factor of 2
1142 * over what we need.
1143 */
1144 size_t bumpup = inbytes * 2, ob_off = ob - dest_buffer;
1145 dest_buffer_size += bumpup;
1146 dest_buffer = mh_xrealloc(dest_buffer,
1147 dest_buffer_size);
1148 ob = dest_buffer + ob_off;
1149 outbytes += bumpup;
1150 outbytes_before += bumpup;
1151 goto iconv_start;
1152 }
1153 if (errno == EINVAL) {
1154 /* middle of multi-byte sequence */
1155 if (write (fd, dest_buffer, outbytes_before - outbytes) < 0) {
1156 advise (dest, "write");
1157 }
1158 fseeko (*fp, -inbytes, SEEK_CUR);
1159 if (end > 0) bytes_to_read += inbytes;
1160 /* advise(NULL, "convert_charset: EINVAL"); */
1161 continue;
1162 }
1163 if (errno == EILSEQ) {
1164 /* invalid multi-byte sequence */
1165 if (fromutf8) {
1166 for (++ib, --inbytes;
1167 inbytes > 0 &&
1168 (((unsigned char) *ib) & 0xc0) == 0x80;
1169 ++ib, --inbytes)
1170 continue;
1171 } else {
1172 ib++; inbytes--; /* skip it */
1173 }
1174 (*ob++) = '?'; outbytes --;
1175 /* advise(NULL, "convert_charset: EILSEQ"); */
1176 goto iconv_start;
1177 }
1178 advise (NULL, "convert_charset: errno = %d", errno);
1179 status = NOTOK;
1180 break;
1181 } else {
1182 if (write (fd, dest_buffer, outbytes_before - outbytes)
1183 < 0) {
1184 advise (dest, "write");
1185 }
1186 }
1187 }
1188
1189 if (opened_input_file) {
1190 fclose (*fp);
1191 *fp = NULL;
1192 }
1193 }
1194
1195 iconv_close (conv_desc);
1196 close (fd);
1197
1198 if (status == OK) {
1199 /* Replace the decoded file with the converted one. */
1200 if (ct->c_cefile.ce_file) {
1201 if (ct->c_cefile.ce_unlink) {
1202 (void) m_unlink (ct->c_cefile.ce_file);
1203 }
1204 free (ct->c_cefile.ce_file);
1205 }
1206 ct->c_cefile.ce_file = dest;
1207 ct->c_cefile.ce_unlink = 1;
1208
1209 ++*message_mods;
1210
1211 /* Update ct->c_ctline. */
1212 if (ct->c_ctline) {
1213 char *ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
1214 ct->c_ctinfo.ci_subtype, NULL);
1215 char *outline;
1216
1217 replace_param(&ct->c_ctinfo.ci_first_pm,
1218 &ct->c_ctinfo.ci_last_pm, "charset",
1219 dest_charset, 0);
1220 outline = output_params(strlen(TYPE_FIELD) + 1 + strlen(ctline),
1221 ct->c_ctinfo.ci_first_pm, NULL, 0);
1222 if (outline) {
1223 ctline = add(outline, ctline);
1224 free(outline);
1225 }
1226
1227 free (ct->c_ctline);
1228 ct->c_ctline = ctline;
1229 } /* else no CT line, which is odd */
1230
1231 /* Update Content-Type header field. */
1232 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1233 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1234 char *ctline = concat (ct->c_ctline, "\n", NULL);
1235
1236 free (hf->value);
1237 hf->value = ctline;
1238 break;
1239 }
1240 }
1241 } else {
1242 (void) m_unlink (dest);
1243 }
1244 free(dest_buffer);
1245 #else /* ! HAVE_ICONV */
1246 NMH_UNUSED (message_mods);
1247
1248 advise (NULL, "Can't convert %s to %s without iconv", src_charset,
1249 dest_charset);
1250 errno = ENOSYS;
1251 status = NOTOK;
1252 #endif /* ! HAVE_ICONV */
1253 }
1254
1255 free (src_charset);
1256 return status;
1257 }
1258
1259
1260 static int
1261 convert_content_charset (CT ct, char **file) {
1262 int status = OK;
1263
1264 #ifdef HAVE_ICONV
1265 /* Using current locale, see if the content needs to be converted. */
1266
1267 /* content_charset() cannot return NULL. */
1268 char *charset = content_charset (ct);
1269
1270 if (! check_charset (charset, strlen (charset))) {
1271 int unused = 0;
1272
1273 char *charset = getcpy (get_charset ());
1274
1275 if (convert_charset (ct, charset, &unused) == 0) {
1276 *file = ct->c_cefile.ce_file;
1277 } else {
1278 status = NOTOK;
1279 }
1280 }
1281 free (charset);
1282 #else /* ! HAVE_ICONV */
1283 NMH_UNUSED (ct);
1284 NMH_UNUSED (file);
1285 #endif /* ! HAVE_ICONV */
1286
1287 return status;
1288 }
1289
1290 /*
1291 * Compile our format string and save any parameters we care about.
1292 */
1293
1294 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1295 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1296 "%<{description}%{description}" \
1297 "%?{cdispo-filename}%{cdispo-filename}" \
1298 "%|%{ctype-name}%> " \
1299 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1300
1301 static struct format *
1302 compile_header(char *form)
1303 {
1304 struct format *fmt;
1305 char *fmtstring;
1306 struct comp *comp = NULL;
1307 unsigned int bucket;
1308
1309 fmtstring = new_fs(form, NULL, DEFAULT_HEADER);
1310
1311 (void) fmt_compile(fmtstring, &fmt, 1);
1312 free_fs();
1313
1314 while ((comp = fmt_nextcomp(comp, &bucket)) != NULL) {
1315 if (strcasecmp(comp->c_name, "folder") == 0) {
1316 folder_comp = comp;
1317 }
1318 }
1319
1320 return fmt;
1321 }
1322
1323 static struct format *
1324 compile_marker(char *form)
1325 {
1326 struct format *fmt;
1327 char *fmtstring;
1328 struct comp *comp = NULL;
1329 unsigned int bucket;
1330 struct param_comp_list *pc_entry;
1331
1332 fmtstring = new_fs(form, NULL, DEFAULT_MARKER);
1333
1334 (void) fmt_compile(fmtstring, &fmt, 1);
1335 free_fs();
1336
1337 /*
1338 * Things we care about:
1339 *
1340 * part - Part name (e.g., 1.1)
1341 * content-type - Content-Type
1342 * description - Content-Description
1343 * disposition - Content-Disposition (inline, attachment)
1344 * ctype-<param> - Content-Type parameter
1345 * cdispo-<param> - Content-Disposition parameter
1346 */
1347
1348 while ((comp = fmt_nextcomp(comp, &bucket)) != NULL) {
1349 if (strcasecmp(comp->c_name, "part") == 0) {
1350 part_comp = comp;
1351 } else if (strcasecmp(comp->c_name, "content-type") == 0) {
1352 ctype_comp = comp;
1353 } else if (strcasecmp(comp->c_name, "description") == 0) {
1354 description_comp = comp;
1355 } else if (strcasecmp(comp->c_name, "disposition") == 0) {
1356 dispo_comp = comp;
1357 } else if (strncasecmp(comp->c_name, "ctype-", 6) == 0 &&
1358 strlen(comp->c_name) > 6) {
1359 pc_entry = mh_xmalloc(sizeof(*pc_entry));
1360 pc_entry->param = getcpy(comp->c_name + 6);
1361 pc_entry->comp = comp;
1362 pc_entry->next = ctype_pc_list;
1363 ctype_pc_list = pc_entry;
1364 } else if (strncasecmp(comp->c_name, "cdispo-", 7) == 0 &&
1365 strlen(comp->c_name) > 7) {
1366 pc_entry = mh_xmalloc(sizeof(*pc_entry));
1367 pc_entry->param = getcpy(comp->c_name + 7);
1368 pc_entry->comp = comp;
1369 pc_entry->next = dispo_pc_list;
1370 dispo_pc_list = pc_entry;
1371 }
1372 }
1373
1374 return fmt;
1375 }
1376
1377 /*
1378 * Output on stdout an appropriate marker for this content, using mh-format
1379 */
1380
1381 static void
1382 output_header(CT ct, struct format *fmt)
1383 {
1384 charstring_t outbuf = charstring_create (BUFSIZ);
1385 int dat[5] = { 0 };
1386 char *endp;
1387 int message = 0;
1388
1389 if (folder_comp)
1390 folder_comp->c_text = getcpy(folder);
1391
1392 if (ct->c_file && *ct->c_file) {
1393 message = strtol(ct->c_file, &endp, 10);
1394 if (*endp) message = 0;
1395 dat[0] = message;
1396 }
1397
1398 /* it would be nice to populate dat[2], for %(size) here,
1399 * but it's not available. it might also be nice to know
1400 * if the message originally had any mime parts or not -- but
1401 * there's also no record of that. (except for MIME-version:)
1402 */
1403
1404 fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
1405
1406 fputs(charstring_buffer (outbuf), stdout);
1407 charstring_free (outbuf);
1408
1409 fmt_freecomptext();
1410 }
1411
1412 static void
1413 output_marker(CT ct, struct format *fmt, int hidden)
1414 {
1415 charstring_t outbuf = charstring_create (BUFSIZ);
1416 struct param_comp_list *pcentry;
1417 int partsize;
1418 int message = 0;
1419 char *endp;
1420 int dat[5] = { 0 };
1421
1422 /*
1423 * Grab any items we care about.
1424 */
1425
1426 if (ctype_comp && ct->c_ctinfo.ci_type) {
1427 ctype_comp->c_text = concat(ct->c_ctinfo.ci_type, "/",
1428 ct->c_ctinfo.ci_subtype, NULL);
1429 }
1430
1431 if (part_comp && ct->c_partno) {
1432 part_comp->c_text = getcpy(ct->c_partno);
1433 }
1434
1435 if (description_comp && ct->c_descr) {
1436 description_comp->c_text = getcpy(ct->c_descr);
1437 }
1438
1439 if (dispo_comp && ct->c_dispo_type) {
1440 dispo_comp->c_text = getcpy(ct->c_dispo_type);
1441 }
1442
1443 for (pcentry = ctype_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1444 pcentry->comp->c_text = get_param(ct->c_ctinfo.ci_first_pm,
1445 pcentry->param, '?', 0);
1446 }
1447
1448 for (pcentry = dispo_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1449 pcentry->comp->c_text = get_param(ct->c_dispo_first,
1450 pcentry->param, '?', 0);
1451 }
1452
1453 if (ct->c_cesizefnx)
1454 partsize = (*ct->c_cesizefnx) (ct);
1455 else
1456 partsize = ct->c_end - ct->c_begin;
1457
1458 if (ct->c_file && *ct->c_file) {
1459 message = strtol(ct->c_file, &endp, 10);
1460 if (*endp) message = 0;
1461 dat[0] = message;
1462 }
1463 dat[2] = partsize;
1464
1465 /* make the part's hidden aspect available by overloading the
1466 * %(unseen) function. make the part's size available via %(size).
1467 * see comments in h/fmt_scan.h.
1468 */
1469 dat[4] = hidden;
1470
1471 fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
1472
1473 fputs(charstring_buffer (outbuf), stdout);
1474 charstring_free (outbuf);
1475
1476 fmt_freecomptext();
1477 }
1478
1479 /*
1480 * Reset (and free) any of the saved marker text
1481 */
1482
1483 static void
1484 free_markercomps(void)
1485 {
1486 struct param_comp_list *pc_entry, *pc2;
1487
1488 folder_comp = NULL;
1489 part_comp = NULL;
1490 ctype_comp = NULL;
1491 description_comp = NULL;
1492 dispo_comp = NULL;
1493
1494 for (pc_entry = ctype_pc_list; pc_entry != NULL; ) {
1495 free(pc_entry->param);
1496 pc2 = pc_entry->next;
1497 free(pc_entry);
1498 pc_entry = pc2;
1499 }
1500
1501 for (pc_entry = dispo_pc_list; pc_entry != NULL; ) {
1502 free(pc_entry->param);
1503 pc2 = pc_entry->next;
1504 free(pc_entry);
1505 pc_entry = pc2;
1506 }
1507 }
1508
1509 /*
1510 * Exit if the display process returned with a nonzero exit code, or terminated
1511 * with a SIGQUIT signal.
1512 */
1513
1514 static int
1515 pidcheck (int status)
1516 {
1517 if ((status & 0xff00) == 0xff00 || (status & 0x007f) != SIGQUIT)
1518 return status;
1519
1520 fflush (stdout);
1521 fflush (stderr);
1522 done (1);
1523 return 1;
1524 }