2 ** mhshowsbr.c -- routines to display the contents of MIME messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 #include <h/signals.h>
17 #include <h/mhparse.h>
34 void content_error(char *, CT, char *, ...);
35 void flush_errors(void);
38 int list_switch(CT, int, int, int, int);
39 int list_content(CT, int, int, int, int);
44 void show_all_messages(CT *);
45 int show_content_aux(CT, int, char *, char *);
50 static void show_single_message(CT, char *);
51 static void DisplayMsgHeader(CT, char *);
52 static int show_switch(CT, int);
53 static int show_content(CT, int);
54 static int show_content_aux2(CT, int, char *, char *, int, int, int, int);
55 static int show_text(CT, int);
56 static int show_multi(CT, int);
57 static int show_multi_internal(CT, int);
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);
65 ** Top level entry point to show/display a group of messages
69 show_all_messages(CT *cts)
74 ** If form is not specified, then get default form
75 ** for showing headers of MIME messages.
78 formsw = getcpy(etcpath("mhl.headers"));
81 ** If form is "mhl.null", suppress display of header.
83 if (strcmp(formsw, "mhl.null")==0)
86 for (ctp = cts; *ctp; ctp++) {
89 /* if top-level type is ok, then display message */
91 show_single_message(ct, formsw);
97 ** Entry point to show/display a single message
101 show_single_message(CT ct, char *form)
107 ** Allow user executable bit so that temporary directories created by
108 ** the viewer (e.g., lynx) are going to be accessible
110 umask(ct->c_umask & ~(0100));
113 ** If you have a format file, then display
114 ** the message headers.
117 DisplayMsgHeader(ct, form);
121 /* Show the body of the message */
128 if (ct->c_ceclosefnx)
129 (*ct->c_ceclosefnx) (ct);
131 /* block a few signals */
133 sigaddset(&set, SIGHUP);
134 sigaddset(&set, SIGINT);
135 sigaddset(&set, SIGQUIT);
136 sigaddset(&set, SIGTERM);
137 sigprocmask(SIG_BLOCK, &set, &oset);
139 while (wait(&status) != NOTOK) {
144 /* reset the signal mask */
145 sigprocmask(SIG_SETMASK, &oset, &set);
153 ** Use mhl to show the header fields
156 DisplayMsgHeader(CT ct, char *form)
164 vec[vecp++] = "-form";
166 vec[vecp++] = "-nobody";
167 vec[vecp++] = ct->c_file;
172 switch (child_id = fork()) {
174 adios("fork", "unable to");
179 fprintf(stderr, "unable to exec ");
192 ** Switching routine. Call the correct routine
193 ** based on content type.
197 show_switch(CT ct, int alternate)
199 switch (ct->c_type) {
201 return show_multi(ct, alternate);
205 switch (ct->c_subtype) {
206 case MESSAGE_PARTIAL:
207 return show_partial(ct, alternate);
210 case MESSAGE_EXTERNAL:
211 return show_external(ct, alternate);
216 return show_message_rfc822(ct, alternate);
222 return show_text(ct, alternate);
229 return show_content(ct, alternate);
233 adios(NULL, "unknown content type %d", ct->c_type);
237 return 0; /* NOT REACHED */
242 ** Generic method for displaying content
246 show_content(CT ct, int alternate)
248 char *cp, buffer[BUFSIZ];
249 CI ci = &ct->c_ctinfo;
251 /* Check for mhshow-show-type/subtype */
252 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
253 invo_name, ci->ci_type, ci->ci_subtype);
254 if ((cp = context_find(buffer)) && *cp != '\0')
255 return show_content_aux(ct, alternate, cp, NULL);
257 /* Check for mhshow-show-type */
258 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
259 if ((cp = context_find(buffer)) && *cp != '\0')
260 return show_content_aux(ct, alternate, cp, NULL);
262 if ((cp = ct->c_showproc))
263 return show_content_aux(ct, alternate, cp, NULL);
265 /* complain if we are not a part of a multipart/alternative */
267 content_error(NULL, ct, "don't know how to display content");
274 ** Parse the display string for displaying generic content
278 show_content_aux(CT ct, int alternate, char *cp, char *cracked)
280 int fd, len, buflen, quoted;
281 int xstdin, xlist, xtty;
282 char *bp, *pp, *file, buffer[BUFSIZ];
283 CI ci = &ct->c_ctinfo;
285 if (!ct->c_ceopenfnx) {
287 content_error(NULL, ct,
288 "don't know how to decode content");
294 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
296 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
297 return (alternate ? DONE : OK);
304 strncpy(buffer, cp, sizeof(buffer));
308 /* get buffer ready to go */
310 buflen = sizeof(buffer) - 1;
311 bp[0] = bp[buflen] = '\0';
314 /* Now parse display string */
315 for ( ; *cp && buflen > 0; cp++) {
321 /* insert parameters from Content-Type field */
326 for (ap = ci->ci_attrs, ep = ci->ci_values;
328 snprintf(bp, buflen, "%s%s=\"%s\"",
339 /* insert content description */
343 s = trimcpy(ct->c_descr);
344 strncpy(bp, s, buflen);
350 /* exclusive execution */
355 /* %e, %f, and stdin is terminal not content */
361 /* insert filename containing content */
362 snprintf(bp, buflen, "'%s'", file);
364 ** since we've quoted the file argument,
365 ** set things up to look past it, to avoid
366 ** problems with the quoting logic below.
367 ** (I know, I should figure out what's
368 ** broken with the quoting logic, but..)
379 ** display listing prior to displaying content
385 /* insert subtype of content */
386 strncpy(bp, ci->ci_subtype, buflen);
390 /* insert character % */
403 /* Did we actually insert something? */
406 ** Insert single quote if not inside quotes
409 if (!quoted && buflen) {
411 memmove(pp + 1, pp, len);
416 /* Escape existing quotes */
417 while ((pp = strchr(pp, '\'')) && buflen > 3) {
419 memmove(pp + 3, pp, len);
427 ** If pp is still set, that means we ran
432 if (!quoted && buflen) {
449 if (buflen <= 0 || (ct->c_termproc &&
450 (size_t)buflen <= strlen(ct->c_termproc))) {
452 ** content_error would provide a more useful error message
453 ** here, except that if we got overrun, it probably would
456 fprintf(stderr, "Buffer overflow constructing show command!\n");
460 /* use charset string to modify display method */
461 if (ct->c_termproc) {
464 strncpy(term, buffer, sizeof(term));
465 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
469 return show_content_aux2(ct, alternate, cracked, buffer,
470 fd, xlist, xstdin, xtty);
475 ** Routine to actually display the content
479 show_content_aux2(CT ct, int alternate, char *cracked,
480 char *buffer, int fd, int xlist, int xstdin, int xtty)
483 char *vec[4], exec[BUFSIZ + sizeof "exec "];
485 if (debugsw || cracked) {
488 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
491 fprintf(stderr, " part %s", ct->c_partno);
493 fprintf(stderr, " using command (cd %s; %s)\n",
496 fprintf(stderr, " using command %s\n", buffer);
499 if (xpid < 0 || (xtty && xpid)) {
502 pidcheck(pidwait(xpid, NOTOK));
507 if (ct->c_type == CT_MULTIPART)
508 list_content(ct, -1, 1, 0, 0);
510 list_switch(ct, -1, 1, 0, 0);
513 snprintf(exec, sizeof(exec), "exec %s", buffer);
522 switch (child_id = fork()) {
524 advise("fork", "unable to");
525 (*ct->c_ceclosefnx) (ct);
534 execvp("/bin/sh", vec);
535 fprintf(stderr, "unable to exec ");
541 pidcheck(pidXwait(child_id, NULL));
544 (*ct->c_ceclosefnx) (ct);
545 return (alternate ? DONE : OK);
551 ** show content of type "text"
555 show_text(CT ct, int alternate)
557 char *cp, buffer[BUFSIZ];
558 CI ci = &ct->c_ctinfo;
560 /* Check for mhshow-show-type/subtype */
561 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
562 invo_name, ci->ci_type, ci->ci_subtype);
563 if ((cp = context_find(buffer)) && *cp != '\0')
564 return show_content_aux(ct, alternate, cp, NULL);
566 /* Check for mhshow-show-type */
567 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
568 if ((cp = context_find(buffer)) && *cp != '\0')
569 return show_content_aux(ct, alternate, cp, NULL);
572 ** Use default method if content is text/plain, or if
573 ** if it is not a text part of a multipart/alternative
575 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
576 snprintf(buffer, sizeof(buffer), "%%p%s '%%F'", defaultpager);
577 cp = (ct->c_showproc = getcpy(buffer));
578 return show_content_aux(ct, alternate, cp, NULL);
586 ** show message body of type "multipart"
590 show_multi(CT ct, int alternate)
592 char *cp, buffer[BUFSIZ];
593 CI ci = &ct->c_ctinfo;
595 /* Check for mhshow-show-type/subtype */
596 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
597 invo_name, ci->ci_type, ci->ci_subtype);
598 if ((cp = context_find(buffer)) && *cp != '\0')
599 return show_multi_aux(ct, alternate, cp);
601 /* Check for mhshow-show-type */
602 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
603 if ((cp = context_find(buffer)) && *cp != '\0')
604 return show_multi_aux(ct, alternate, cp);
606 if ((cp = ct->c_showproc))
607 return show_multi_aux(ct, alternate, cp);
610 ** Use default method to display this multipart content
611 ** if it is not a (nested) part of a multipart/alternative,
612 ** or if it is one of the known subtypes of multipart.
614 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
615 return show_multi_internal(ct, alternate);
622 ** show message body of subtypes of multipart that
623 ** we understand directly (mixed, alternate, etc...)
627 show_multi_internal(CT ct, int alternate)
629 int alternating, nowalternate, result;
630 struct multipart *m = (struct multipart *) ct->c_ctparams;
635 nowalternate = alternate;
637 if (ct->c_subtype == MULTI_ALTERNATE) {
643 ** Other possible multipart types are:
644 ** - multipart/parallel
646 ** - multipart/digest
647 ** - unknown subtypes of multipart (treat as mixed per rfc2046)
651 ** alternate -> we are a part inside an multipart/alternative
652 ** alternating -> we are a multipart/alternative
655 result = alternate ? NOTOK : OK;
657 for (part = m->mp_parts; part; part = part->mp_next) {
660 if (part_ok(p, 1) && type_ok(p, 1)) {
663 inneresult = show_switch(p, nowalternate);
664 switch (inneresult) {
666 if (alternate && !alternating) {
679 alternate = nowalternate = 0;
689 if (alternating && !part) {
691 content_error(NULL, ct, "don't know how to display any of the contents");
701 ** Parse display string for multipart content
702 ** and use external program to display it.
706 show_multi_aux(CT ct, int alternate, char *cp)
708 int len, buflen, quoted;
710 char *bp, *pp, *file, buffer[BUFSIZ];
711 struct multipart *m = (struct multipart *) ct->c_ctparams;
713 CI ci = &ct->c_ctinfo;
716 for (part = m->mp_parts; part; part = part->mp_next) {
719 if (!p->c_ceopenfnx) {
721 content_error(NULL, p, "don't know how to decode content");
725 if (p->c_storage == NULL) {
727 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
730 /* I'm not sure if this is necessary? */
731 p->c_storage = getcpy(file);
733 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
734 return (alternate ? DONE : OK);
735 (*p->c_ceclosefnx) (p);
742 /* get buffer ready to go */
744 buflen = sizeof(buffer) - 1;
745 bp[0] = bp[buflen] = '\0';
748 /* Now parse display string */
749 for ( ; *cp && buflen > 0; cp++) {
754 /* insert parameters from Content-Type field */
759 for (ap = ci->ci_attrs, ep = ci->ci_values;
761 snprintf(bp, buflen, "%s%s=\"%s\"",
772 /* insert content description */
776 s = trimcpy(ct->c_descr);
777 strncpy(bp, s, buflen);
783 /* exclusive execution */
793 /* insert filename(s) containing content */
797 for (part = m->mp_parts; part;
798 part = part->mp_next) {
801 snprintf(bp, buflen, "%s'%s'",
809 ** set our starting pointer back to bp,
810 ** to avoid requoting the filenames we
820 ** display listing prior to displaying content
826 /* insert subtype of content */
827 strncpy(bp, ci->ci_subtype, buflen);
831 /* insert character % */
844 /* Did we actually insert something? */
847 ** Insert single quote if not inside quotes
850 if (!quoted && buflen) {
852 memmove(pp + 1, pp, len);
857 /* Escape existing quotes */
858 while ((pp = strchr(pp, '\'')) && buflen > 3) {
860 memmove(pp + 3, pp, len);
868 ** If pp is still set, that means we ran
873 if (!quoted && buflen) {
890 if (buflen <= 0 || (ct->c_termproc &&
891 (size_t)buflen <= strlen(ct->c_termproc))) {
893 ** content_error would provide a more useful error message
894 ** here, except that if we got overrun, it probably would
897 fprintf(stderr, "Buffer overflow constructing show command!\n");
901 /* use charset string to modify display method */
902 if (ct->c_termproc) {
905 strncpy(term, buffer, sizeof(term));
906 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
909 return show_content_aux2(ct, alternate, NULL, buffer,
910 NOTOK, xlist, 0, xtty);
915 ** show content of type "message/rfc822"
919 show_message_rfc822(CT ct, int alternate)
921 char *cp, buffer[BUFSIZ];
922 CI ci = &ct->c_ctinfo;
924 /* Check for mhshow-show-type/subtype */
925 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
926 invo_name, ci->ci_type, ci->ci_subtype);
927 if ((cp = context_find(buffer)) && *cp != '\0')
928 return show_content_aux(ct, alternate, cp, NULL);
930 /* Check for mhshow-show-type */
931 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
932 if ((cp = context_find(buffer)) && *cp != '\0')
933 return show_content_aux(ct, alternate, cp, NULL);
935 if ((cp = ct->c_showproc))
936 return show_content_aux(ct, alternate, cp, NULL);
938 /* default method for message/rfc822 */
939 if (ct->c_subtype == MESSAGE_RFC822) {
940 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
941 return show_content_aux(ct, alternate, cp, NULL);
944 /* complain if we are not a part of a multipart/alternative */
946 content_error(NULL, ct, "don't know how to display content");
953 ** Show content of type "message/partial".
957 show_partial(CT ct, int alternate)
959 content_error(NULL, ct,
960 "in order to display this message, you must reassemble it");
966 ** Show how to retrieve content of type "message/external".
969 show_external(CT ct, int alternate)
976 msg = add("You need to fetch the contents yourself:", NULL);
977 ap = ct->c_ctinfo.ci_attrs;
978 ep = ct->c_ctinfo.ci_values;
979 for (; *ap; ap++, ep++) {
980 msg = add(concat("\n\t", *ap, ": ", *ep, NULL), msg);
982 if (!(fp = fopen(ct->c_file, "r"))) {
983 adios(ct->c_file, "unable to open");
985 fseek(fp, ct->c_begin, SEEK_SET);
986 while (!feof(fp) && ftell(fp) < ct->c_end) {
987 if (!fgets(buf, sizeof buf, fp)) {
988 adios(ct->c_file, "unable to read");
990 *strchr(buf, '\n') = '\0';
991 msg = add(concat("\n\t", buf, NULL), msg);
994 content_error(NULL, ct, msg);