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)
471 char *vec[4], exec[BUFSIZ + sizeof "exec "];
473 if (debugsw || cracked) {
476 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
479 fprintf(stderr, " part %s", ct->c_partno);
481 fprintf(stderr, " using command (cd %s; %s)\n",
484 fprintf(stderr, " using command %s\n", buffer);
487 if (xpid < 0 || (xtty && xpid)) {
490 pidcheck(pidwait(xpid, NOTOK));
495 if (ct->c_type == CT_MULTIPART)
496 list_content(ct, -1, 1, 0, 0);
498 list_switch(ct, -1, 1, 0, 0);
501 snprintf(exec, sizeof(exec), "exec %s", buffer);
510 switch (child_id = fork()) {
512 advise("fork", "unable to");
513 (*ct->c_ceclosefnx) (ct);
522 execvp("/bin/sh", vec);
523 fprintf(stderr, "unable to exec ");
529 pidcheck(pidXwait(child_id, NULL));
532 (*ct->c_ceclosefnx) (ct);
533 return (alternate ? DONE : OK);
539 ** show content of type "text"
543 show_text(CT ct, int alternate)
545 char *cp, buffer[BUFSIZ];
546 CI ci = &ct->c_ctinfo;
548 /* Check for mhshow-show-type/subtype */
549 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
550 invo_name, ci->ci_type, ci->ci_subtype);
551 if ((cp = context_find(buffer)) && *cp != '\0')
552 return show_content_aux(ct, alternate, cp, NULL);
554 /* Check for mhshow-show-type */
555 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
556 if ((cp = context_find(buffer)) && *cp != '\0')
557 return show_content_aux(ct, alternate, cp, NULL);
560 ** Use default method if content is text/plain, or if
561 ** if it is not a text part of a multipart/alternative
563 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
564 if (!check_charset(ct->c_charset, strlen(ct->c_charset))) {
565 snprintf(buffer, sizeof(buffer), "%%liconv -f '%s'",
568 snprintf(buffer, sizeof(buffer), "%%lcat");
570 cp = (ct->c_showproc = getcpy(buffer));
571 return show_content_aux(ct, alternate, cp, NULL);
579 ** show message body of type "multipart"
583 show_multi(CT ct, int alternate)
585 char *cp, buffer[BUFSIZ];
586 CI ci = &ct->c_ctinfo;
588 /* Check for mhshow-show-type/subtype */
589 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
590 invo_name, ci->ci_type, ci->ci_subtype);
591 if ((cp = context_find(buffer)) && *cp != '\0')
592 return show_multi_aux(ct, alternate, cp);
594 /* Check for mhshow-show-type */
595 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
596 if ((cp = context_find(buffer)) && *cp != '\0')
597 return show_multi_aux(ct, alternate, cp);
599 if ((cp = ct->c_showproc))
600 return show_multi_aux(ct, alternate, cp);
603 ** Use default method to display this multipart content
604 ** if it is not a (nested) part of a multipart/alternative,
605 ** or if it is one of the known subtypes of multipart.
607 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
608 return show_multi_internal(ct, alternate);
615 ** show message body of subtypes of multipart that
616 ** we understand directly (mixed, alternate, etc...)
620 show_multi_internal(CT ct, int alternate)
622 int alternating, nowalternate, result;
623 struct multipart *m = (struct multipart *) ct->c_ctparams;
628 nowalternate = alternate;
630 if (ct->c_subtype == MULTI_ALTERNATE) {
636 ** Other possible multipart types are:
637 ** - multipart/parallel
639 ** - multipart/digest
640 ** - unknown subtypes of multipart (treat as mixed per rfc2046)
644 ** alternate -> we are a part inside an multipart/alternative
645 ** alternating -> we are a multipart/alternative
648 result = alternate ? NOTOK : OK;
650 for (part = m->mp_parts; part; part = part->mp_next) {
653 if (part_ok(p, 1) && type_ok(p, 1)) {
656 inneresult = show_switch(p, nowalternate);
657 switch (inneresult) {
659 if (alternate && !alternating) {
672 alternate = nowalternate = 0;
682 if (alternating && !part) {
684 content_error(NULL, ct, "don't know how to display any of the contents");
694 ** Parse display string for multipart content
695 ** and use external program to display it.
699 show_multi_aux(CT ct, int alternate, char *cp)
701 int len, buflen, quoted;
703 char *bp, *pp, *file, buffer[BUFSIZ];
704 struct multipart *m = (struct multipart *) ct->c_ctparams;
706 CI ci = &ct->c_ctinfo;
709 for (part = m->mp_parts; part; part = part->mp_next) {
712 if (!p->c_ceopenfnx) {
714 content_error(NULL, p, "don't know how to decode content");
718 if (p->c_storage == NULL) {
720 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
723 /* I'm not sure if this is necessary? */
724 p->c_storage = getcpy(file);
726 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
727 return (alternate ? DONE : OK);
728 (*p->c_ceclosefnx) (p);
735 /* get buffer ready to go */
737 buflen = sizeof(buffer) - 1;
738 bp[0] = bp[buflen] = '\0';
741 /* Now parse display string */
742 for ( ; *cp && buflen > 0; cp++) {
747 /* insert parameters from Content-Type field */
752 for (ap = ci->ci_attrs, ep = ci->ci_values;
754 snprintf(bp, buflen, "%s%s=\"%s\"",
767 strncpy(bp, ct->c_charset, buflen);
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 return show_content_aux2(ct, alternate, NULL, buffer,
891 NOTOK, xlist, 0, xtty);
896 ** show content of type "message/rfc822"
900 show_message_rfc822(CT ct, int alternate)
902 char *cp, buffer[BUFSIZ];
903 CI ci = &ct->c_ctinfo;
905 /* Check for mhshow-show-type/subtype */
906 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
907 invo_name, ci->ci_type, ci->ci_subtype);
908 if ((cp = context_find(buffer)) && *cp != '\0')
909 return show_content_aux(ct, alternate, cp, NULL);
911 /* Check for mhshow-show-type */
912 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
913 if ((cp = context_find(buffer)) && *cp != '\0')
914 return show_content_aux(ct, alternate, cp, NULL);
916 if ((cp = ct->c_showproc))
917 return show_content_aux(ct, alternate, cp, NULL);
919 /* default method for message/rfc822 */
920 if (ct->c_subtype == MESSAGE_RFC822) {
921 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
922 return show_content_aux(ct, alternate, cp, NULL);
925 /* complain if we are not a part of a multipart/alternative */
927 content_error(NULL, ct, "don't know how to display content");
934 ** Show content of type "message/partial".
938 show_partial(CT ct, int alternate)
940 content_error(NULL, ct,
941 "in order to display this message, you must reassemble it");
947 ** Show how to retrieve content of type "message/external".
950 show_external(CT ct, int alternate)
957 msg = add("You need to fetch the contents yourself:", NULL);
958 ap = ct->c_ctinfo.ci_attrs;
959 ep = ct->c_ctinfo.ci_values;
960 for (; *ap; ap++, ep++) {
961 msg = add(concat("\n\t", *ap, ": ", *ep, NULL), msg);
963 if (!(fp = fopen(ct->c_file, "r"))) {
964 adios(ct->c_file, "unable to open");
966 fseek(fp, ct->c_begin, SEEK_SET);
967 while (!feof(fp) && ftell(fp) < ct->c_end) {
968 if (!fgets(buf, sizeof buf, fp)) {
969 adios(ct->c_file, "unable to read");
971 *strchr(buf, '\n') = '\0';
972 msg = add(concat("\n\t", buf, NULL), msg);
975 content_error(NULL, ct, msg);