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\"",
341 strncpy(bp, ct->c_charset, buflen);
346 /* insert content description */
350 s = trimcpy(ct->c_descr);
351 strncpy(bp, s, buflen);
357 /* exclusive execution */
362 /* %e, %f, and stdin is terminal not content */
368 /* insert filename containing content */
369 snprintf(bp, buflen, "'%s'", file);
371 ** since we've quoted the file argument,
372 ** set things up to look past it, to avoid
373 ** problems with the quoting logic below.
374 ** (I know, I should figure out what's
375 ** broken with the quoting logic, but..)
386 ** display listing prior to displaying content
392 /* insert subtype of content */
393 strncpy(bp, ci->ci_subtype, buflen);
397 /* insert character % */
410 /* Did we actually insert something? */
413 ** Insert single quote if not inside quotes
416 if (!quoted && buflen) {
418 memmove(pp + 1, pp, len);
423 /* Escape existing quotes */
424 while ((pp = strchr(pp, '\'')) && buflen > 3) {
426 memmove(pp + 3, pp, len);
434 ** If pp is still set, that means we ran
439 if (!quoted && buflen) {
457 return show_content_aux2(ct, alternate, cracked, buffer,
458 fd, xlist, xstdin, xtty);
463 ** Routine to actually display the content
467 show_content_aux2(CT ct, int alternate, char *cracked,
468 char *buffer, int fd, int xlist, int xstdin, int xtty)
472 if (debugsw || cracked) {
475 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
478 fprintf(stderr, " part %s", ct->c_partno);
480 fprintf(stderr, " using command (cd %s; %s)\n",
483 fprintf(stderr, " using command %s\n", buffer);
486 if (xpid < 0 || (xtty && xpid)) {
489 pidcheck(pidwait(xpid, NOTOK));
494 if (ct->c_type == CT_MULTIPART)
495 list_content(ct, -1, 1, 0, 0);
497 list_switch(ct, -1, 1, 0, 0);
502 switch (child_id = fork()) {
504 advise("fork", "unable to");
505 (*ct->c_ceclosefnx) (ct);
514 execlp("/bin/sh", "/bin/sh", "-c", buffer, NULL);
515 fprintf(stderr, "unable to exec ");
521 pidcheck(pidXwait(child_id, NULL));
524 (*ct->c_ceclosefnx) (ct);
525 return (alternate ? DONE : OK);
531 ** show content of type "text"
535 show_text(CT ct, int alternate)
537 char *cp, buffer[BUFSIZ];
538 CI ci = &ct->c_ctinfo;
540 /* Check for mhshow-show-type/subtype */
541 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
542 invo_name, ci->ci_type, ci->ci_subtype);
543 if ((cp = context_find(buffer)) && *cp != '\0')
544 return show_content_aux(ct, alternate, cp, NULL);
546 /* Check for mhshow-show-type */
547 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
548 if ((cp = context_find(buffer)) && *cp != '\0')
549 return show_content_aux(ct, alternate, cp, NULL);
552 ** Use default method if content is text/plain, or if
553 ** if it is not a text part of a multipart/alternative
555 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
556 if (!check_charset(ct->c_charset, strlen(ct->c_charset))) {
557 snprintf(buffer, sizeof(buffer), "%%liconv -f '%s'",
560 snprintf(buffer, sizeof(buffer), "%%lcat");
562 cp = (ct->c_showproc = getcpy(buffer));
563 return show_content_aux(ct, alternate, cp, NULL);
571 ** show message body of type "multipart"
575 show_multi(CT ct, int alternate)
577 char *cp, buffer[BUFSIZ];
578 CI ci = &ct->c_ctinfo;
580 /* Check for mhshow-show-type/subtype */
581 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
582 invo_name, ci->ci_type, ci->ci_subtype);
583 if ((cp = context_find(buffer)) && *cp != '\0')
584 return show_multi_aux(ct, alternate, cp);
586 /* Check for mhshow-show-type */
587 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
588 if ((cp = context_find(buffer)) && *cp != '\0')
589 return show_multi_aux(ct, alternate, cp);
591 if ((cp = ct->c_showproc))
592 return show_multi_aux(ct, alternate, cp);
595 ** Use default method to display this multipart content
596 ** if it is not a (nested) part of a multipart/alternative,
597 ** or if it is one of the known subtypes of multipart.
599 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
600 return show_multi_internal(ct, alternate);
607 ** show message body of subtypes of multipart that
608 ** we understand directly (mixed, alternate, etc...)
612 show_multi_internal(CT ct, int alternate)
614 int alternating, nowalternate, result;
615 struct multipart *m = (struct multipart *) ct->c_ctparams;
620 nowalternate = alternate;
622 if (ct->c_subtype == MULTI_ALTERNATE) {
628 ** Other possible multipart types are:
629 ** - multipart/parallel
631 ** - multipart/digest
632 ** - unknown subtypes of multipart (treat as mixed per rfc2046)
636 ** alternate -> we are a part inside an multipart/alternative
637 ** alternating -> we are a multipart/alternative
640 result = alternate ? NOTOK : OK;
642 for (part = m->mp_parts; part; part = part->mp_next) {
645 if (part_ok(p, 1) && type_ok(p, 1)) {
648 inneresult = show_switch(p, nowalternate);
649 switch (inneresult) {
651 if (alternate && !alternating) {
664 alternate = nowalternate = 0;
674 if (alternating && !part) {
676 content_error(NULL, ct, "don't know how to display any of the contents");
686 ** Parse display string for multipart content
687 ** and use external program to display it.
691 show_multi_aux(CT ct, int alternate, char *cp)
693 int len, buflen, quoted;
695 char *bp, *pp, *file, buffer[BUFSIZ];
696 struct multipart *m = (struct multipart *) ct->c_ctparams;
698 CI ci = &ct->c_ctinfo;
701 for (part = m->mp_parts; part; part = part->mp_next) {
704 if (!p->c_ceopenfnx) {
706 content_error(NULL, p, "don't know how to decode content");
710 if (p->c_storage == NULL) {
712 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
715 /* I'm not sure if this is necessary? */
716 p->c_storage = getcpy(file);
718 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
719 return (alternate ? DONE : OK);
720 (*p->c_ceclosefnx) (p);
727 /* get buffer ready to go */
729 buflen = sizeof(buffer) - 1;
730 bp[0] = bp[buflen] = '\0';
733 /* Now parse display string */
734 for ( ; *cp && buflen > 0; cp++) {
739 /* insert parameters from Content-Type field */
744 for (ap = ci->ci_attrs, ep = ci->ci_values;
746 snprintf(bp, buflen, "%s%s=\"%s\"",
759 strncpy(bp, ct->c_charset, buflen);
764 /* insert content description */
768 s = trimcpy(ct->c_descr);
769 strncpy(bp, s, buflen);
775 /* exclusive execution */
785 /* insert filename(s) containing content */
789 for (part = m->mp_parts; part;
790 part = part->mp_next) {
793 snprintf(bp, buflen, "%s'%s'",
801 ** set our starting pointer back to bp,
802 ** to avoid requoting the filenames we
812 ** display listing prior to displaying content
818 /* insert subtype of content */
819 strncpy(bp, ci->ci_subtype, buflen);
823 /* insert character % */
836 /* Did we actually insert something? */
839 ** Insert single quote if not inside quotes
842 if (!quoted && buflen) {
844 memmove(pp + 1, pp, len);
849 /* Escape existing quotes */
850 while ((pp = strchr(pp, '\'')) && buflen > 3) {
852 memmove(pp + 3, pp, len);
860 ** If pp is still set, that means we ran
865 if (!quoted && buflen) {
882 return show_content_aux2(ct, alternate, NULL, buffer,
883 NOTOK, xlist, 0, xtty);
888 ** show content of type "message/rfc822"
892 show_message_rfc822(CT ct, int alternate)
894 char *cp, buffer[BUFSIZ];
895 CI ci = &ct->c_ctinfo;
897 /* Check for mhshow-show-type/subtype */
898 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
899 invo_name, ci->ci_type, ci->ci_subtype);
900 if ((cp = context_find(buffer)) && *cp != '\0')
901 return show_content_aux(ct, alternate, cp, NULL);
903 /* Check for mhshow-show-type */
904 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
905 if ((cp = context_find(buffer)) && *cp != '\0')
906 return show_content_aux(ct, alternate, cp, NULL);
908 if ((cp = ct->c_showproc))
909 return show_content_aux(ct, alternate, cp, NULL);
911 /* default method for message/rfc822 */
912 if (ct->c_subtype == MESSAGE_RFC822) {
913 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
914 return show_content_aux(ct, alternate, cp, NULL);
917 /* complain if we are not a part of a multipart/alternative */
919 content_error(NULL, ct, "don't know how to display content");
926 ** Show content of type "message/partial".
930 show_partial(CT ct, int alternate)
932 content_error(NULL, ct,
933 "in order to display this message, you must reassemble it");
939 ** Show how to retrieve content of type "message/external".
942 show_external(CT ct, int alternate)
949 msg = add("You need to fetch the contents yourself:", NULL);
950 ap = ct->c_ctinfo.ci_attrs;
951 ep = ct->c_ctinfo.ci_values;
952 for (; *ap; ap++, ep++) {
953 msg = add(concat("\n\t", *ap, ": ", *ep, NULL), msg);
955 if (!(fp = fopen(ct->c_file, "r"))) {
956 adios(ct->c_file, "unable to open");
958 fseek(fp, ct->c_begin, SEEK_SET);
959 while (!feof(fp) && ftell(fp) < ct->c_end) {
960 if (!fgets(buf, sizeof buf, fp)) {
961 adios(ct->c_file, "unable to read");
963 *strchr(buf, '\n') = '\0';
964 msg = add(concat("\n\t", buf, NULL), msg);
967 content_error(NULL, ct, msg);