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