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