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>
33 void content_error(char *, CT, char *, ...);
34 void flush_errors(void);
37 int list_switch(CT, int, int, int);
38 int list_content(CT, int, int, int);
43 void show_all_messages(CT *);
44 int show_content_aux(CT, int, char *, char *);
49 static void show_single_message(CT, char *);
50 static void DisplayMsgHeader(CT, char *);
51 static int show_switch(CT, int);
52 static int show_content(CT, int);
53 static int show_content_aux2(CT, int, char *, char *, int, int, int);
54 static int show_text(CT, int);
55 static int show_multi(CT, int);
56 static int show_multi_internal(CT, int);
57 static int show_multi_aux(CT, int, char *);
58 static int show_message_rfc822(CT, int);
59 static int show_partial(CT, int);
60 static int show_external(CT, int);
64 ** Top level entry point to show/display a group of messages
68 show_all_messages(CT *cts)
73 ** If form is not specified, then get default form
74 ** for showing headers of MIME messages.
77 formsw = getcpy(etcpath("mhl.headers"));
80 ** If form is "mhl.null", suppress display of header.
82 if (strcmp(formsw, "mhl.null")==0)
85 for (ctp = cts; *ctp; ctp++) {
88 if (!type_ok(ct, 1)) { /* top-level type */
95 printf(">>> Message %s\n\n", ct->c_file);
97 show_single_message(ct, formsw);
103 ** Entry point to show/display a single message
107 show_single_message(CT ct, char *form)
110 ** Allow user executable bit so that temporary directories created by
111 ** the viewer (e.g., lynx) are going to be accessible
113 umask(ct->c_umask & ~(0100));
116 ** If you have a format file, then display
117 ** the message headers.
120 DisplayMsgHeader(ct, form);
122 /* Show the body of the message */
129 if (ct->c_ceclosefnx)
130 (*ct->c_ceclosefnx) (ct);
137 ** Use mhl to show the header fields
140 DisplayMsgHeader(CT ct, char *form)
148 vec[vecp++] = "-form";
150 vec[vecp++] = "-nobody";
151 vec[vecp++] = ct->c_file;
156 switch (child_id = fork()) {
158 adios("fork", "unable to");
163 fprintf(stderr, "unable to exec ");
169 pidcheck(pidwait(child_id, NOTOK));
176 ** Switching routine. Call the correct routine
177 ** based on content type.
181 show_switch(CT ct, int alternate)
183 switch (ct->c_type) {
185 return show_multi(ct, alternate);
189 switch (ct->c_subtype) {
190 case MESSAGE_PARTIAL:
191 return show_partial(ct, alternate);
194 case MESSAGE_EXTERNAL:
195 return show_external(ct, alternate);
200 return show_message_rfc822(ct, alternate);
206 return show_text(ct, alternate);
213 return show_content(ct, alternate);
217 adios(NULL, "unknown content type %d", ct->c_type);
221 return 0; /* NOT REACHED */
226 ** Generic method for displaying content
230 show_content(CT ct, int alternate)
232 char *cp, buffer[BUFSIZ];
233 CI ci = &ct->c_ctinfo;
235 /* Check for mhshow-show-type/subtype */
236 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
237 ci->ci_type, ci->ci_subtype);
238 if ((cp = context_find(buffer)) && *cp != '\0')
239 return show_content_aux(ct, alternate, cp, NULL);
241 /* Check for mhshow-show-type */
242 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
243 if ((cp = context_find(buffer)) && *cp != '\0')
244 return show_content_aux(ct, alternate, cp, NULL);
246 if ((cp = ct->c_showproc))
247 return show_content_aux(ct, alternate, cp, NULL);
249 /* complain if we are not a part of a multipart/alternative */
251 return show_content_aux(ct, alternate, "%l", NULL);
258 ** Parse the display string for displaying generic content
261 show_content_aux(CT ct, int alternate, char *cp, char *cracked)
263 int fd, len, buflen, quoted;
265 char *bp, *pp, *file, buffer[BUFSIZ];
266 CI ci = &ct->c_ctinfo;
268 if (!ct->c_ceopenfnx) {
270 content_error(NULL, ct,
271 "don't know how to decode content");
277 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
279 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
280 return (alternate ? DONE : OK);
286 strncpy(buffer, cp, sizeof(buffer));
290 /* get buffer ready to go */
292 buflen = sizeof(buffer) - 1;
293 bp[0] = bp[buflen] = '\0';
296 /* Now parse display string */
297 for ( ; *cp && buflen > 0; cp++) {
303 /* insert parameters from Content-Type field */
308 for (ap = ci->ci_attrs, ep = ci->ci_values;
310 snprintf(bp, buflen, "%s%s=\"%s\"",
322 strncpy(bp, ct->c_charset ? ct->c_charset :
327 /* insert content description */
331 s = trimcpy(ct->c_descr);
332 strncpy(bp, s, buflen);
338 /* %f, and stdin is terminal not content */
343 /* insert filename containing content */
344 snprintf(bp, buflen, "'%s'", file);
346 ** since we've quoted the file argument,
347 ** set things up to look past it, to avoid
348 ** problems with the quoting logic below.
349 ** (I know, I should figure out what's
350 ** broken with the quoting logic, but..)
360 ** display listing prior to displaying content
366 /* insert subtype of content */
367 strncpy(bp, ci->ci_subtype, buflen);
371 /* insert character % */
384 /* Did we actually insert something? */
387 ** Insert single quote if not inside quotes
390 if (!quoted && buflen) {
392 memmove(pp + 1, pp, len);
397 /* Escape existing quotes */
398 while ((pp = strchr(pp, '\'')) && buflen > 3) {
400 memmove(pp + 3, pp, len);
408 ** If pp is still set, that means we ran
413 if (!quoted && buflen) {
431 return show_content_aux2(ct, alternate, cracked, buffer,
437 ** Routine to actually display the content
440 show_content_aux2(CT ct, int alternate, char *cracked,
441 char *buffer, int fd, int xlist, int xstdin)
445 if (debugsw || cracked) {
448 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
451 fprintf(stderr, " part %s", ct->c_partno);
453 fprintf(stderr, " using command (cd %s; %s)\n",
456 fprintf(stderr, " using command %s\n", buffer);
460 if (ct->c_type == CT_MULTIPART)
461 list_content(ct, -1, 0, 0);
463 list_switch(ct, -1, 0, 0);
468 switch (child_id = fork()) {
470 advise("fork", "unable to");
471 (*ct->c_ceclosefnx) (ct);
480 execlp("/bin/sh", "/bin/sh", "-c", buffer, NULL);
481 fprintf(stderr, "unable to exec ");
487 pidcheck(pidXwait(child_id, NULL));
490 (*ct->c_ceclosefnx) (ct);
491 return (alternate ? DONE : OK);
497 ** show content of type "text"
501 show_text(CT ct, int alternate)
503 char *cp, buffer[BUFSIZ];
504 CI ci = &ct->c_ctinfo;
506 /* Check for mhshow-show-type/subtype */
507 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
508 ci->ci_type, ci->ci_subtype);
509 if ((cp = context_find(buffer)) && *cp != '\0')
510 return show_content_aux(ct, alternate, cp, NULL);
512 /* Check for mhshow-show-type */
513 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
514 if ((cp = context_find(buffer)) && *cp != '\0')
515 return show_content_aux(ct, alternate, cp, NULL);
518 ** Use default method if content is text/plain, or if
519 ** if it is not a text part of a multipart/alternative
521 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
522 if (ct->c_charset && !is_native_charset(ct->c_charset)) {
523 snprintf(buffer, sizeof(buffer), "%%liconv -f '%s'",
526 snprintf(buffer, sizeof(buffer), "%%lcat");
528 ct->c_showproc = getcpy(buffer);
529 return show_content_aux(ct, alternate, ct->c_showproc, NULL);
537 ** show message body of type "multipart"
541 show_multi(CT ct, int alternate)
543 char *cp, buffer[BUFSIZ];
544 CI ci = &ct->c_ctinfo;
546 /* Check for mhshow-show-type/subtype */
547 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
548 ci->ci_type, ci->ci_subtype);
549 if ((cp = context_find(buffer)) && *cp != '\0')
550 return show_multi_aux(ct, alternate, cp);
552 /* Check for mhshow-show-type */
553 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
554 if ((cp = context_find(buffer)) && *cp != '\0')
555 return show_multi_aux(ct, alternate, cp);
557 if ((cp = ct->c_showproc))
558 return show_multi_aux(ct, alternate, cp);
561 ** Use default method to display this multipart content
562 ** if it is not a (nested) part of a multipart/alternative,
563 ** or if it is one of the known subtypes of multipart.
565 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
566 return show_multi_internal(ct, alternate);
573 ** show message body of subtypes of multipart that
574 ** we understand directly (mixed, alternate, etc...)
578 show_multi_internal(CT ct, int alternate)
580 int alternating, nowalternate, result;
581 struct multipart *m = (struct multipart *) ct->c_ctparams;
586 nowalternate = alternate;
588 if (ct->c_subtype == MULTI_ALTERNATE) {
594 ** Other possible multipart types are:
595 ** - multipart/parallel
597 ** - multipart/digest
598 ** - unknown subtypes of multipart (treat as mixed per rfc2046)
602 ** alternate -> we are a part inside an multipart/alternative
603 ** alternating -> we are a multipart/alternative
606 result = alternate ? NOTOK : OK;
608 for (part = m->mp_parts; part; part = part->mp_next) {
611 if (part_ok(p, 1) && type_ok(p, 1)) {
614 inneresult = show_switch(p, nowalternate);
615 switch (inneresult) {
617 if (alternate && !alternating) {
630 alternate = nowalternate = 0;
640 if (alternating && !part) {
642 content_error(NULL, ct, "don't know how to display any of the contents");
652 ** Parse display string for multipart content
653 ** and use external program to display it.
657 show_multi_aux(CT ct, int alternate, char *cp)
659 int len, buflen, quoted;
661 char *bp, *pp, *file, buffer[BUFSIZ];
662 struct multipart *m = (struct multipart *) ct->c_ctparams;
664 CI ci = &ct->c_ctinfo;
667 for (part = m->mp_parts; part; part = part->mp_next) {
670 if (!p->c_ceopenfnx) {
672 content_error(NULL, p, "don't know how to decode content");
676 if (p->c_storage == NULL) {
678 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
681 /* I'm not sure if this is necessary? */
682 p->c_storage = getcpy(file);
684 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
685 return (alternate ? DONE : OK);
686 (*p->c_ceclosefnx) (p);
692 /* get buffer ready to go */
694 buflen = sizeof(buffer) - 1;
695 bp[0] = bp[buflen] = '\0';
698 /* Now parse display string */
699 for ( ; *cp && buflen > 0; cp++) {
704 /* insert parameters from Content-Type field */
709 for (ap = ci->ci_attrs, ep = ci->ci_values;
711 snprintf(bp, buflen, "%s%s=\"%s\"",
723 strncpy(bp, ct->c_charset ? ct->c_charset :
728 /* insert content description */
732 s = trimcpy(ct->c_descr);
733 strncpy(bp, s, buflen);
740 /* insert filename(s) containing content */
744 for (part = m->mp_parts; part;
745 part = part->mp_next) {
748 snprintf(bp, buflen, "%s'%s'",
756 ** set our starting pointer back to bp,
757 ** to avoid requoting the filenames we
766 ** display listing prior to displaying content
772 /* insert subtype of content */
773 strncpy(bp, ci->ci_subtype, buflen);
777 /* insert character % */
790 /* Did we actually insert something? */
793 ** Insert single quote if not inside quotes
796 if (!quoted && buflen) {
798 memmove(pp + 1, pp, len);
803 /* Escape existing quotes */
804 while ((pp = strchr(pp, '\'')) && buflen > 3) {
806 memmove(pp + 3, pp, len);
814 ** If pp is still set, that means we ran
819 if (!quoted && buflen) {
836 return show_content_aux2(ct, alternate, NULL, buffer,
842 ** show content of type "message/rfc822"
846 show_message_rfc822(CT ct, int alternate)
848 char *cp, buffer[BUFSIZ];
849 CI ci = &ct->c_ctinfo;
851 /* Check for mhshow-show-type/subtype */
852 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
853 ci->ci_type, ci->ci_subtype);
854 if ((cp = context_find(buffer)) && *cp != '\0')
855 return show_content_aux(ct, alternate, cp, NULL);
857 /* Check for mhshow-show-type */
858 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
859 if ((cp = context_find(buffer)) && *cp != '\0')
860 return show_content_aux(ct, alternate, cp, NULL);
862 if ((cp = ct->c_showproc))
863 return show_content_aux(ct, alternate, cp, NULL);
865 /* default method for message/rfc822 */
866 if (ct->c_subtype == MESSAGE_RFC822) {
867 cp = (ct->c_showproc = getcpy("%lshow -file %F"));
868 return show_content_aux(ct, alternate, cp, NULL);
871 /* complain if we are not a part of a multipart/alternative */
873 return show_content_aux(ct, alternate, "%l", NULL);
880 ** Show content of type "message/partial".
884 show_partial(CT ct, int alternate)
886 show_content_aux(ct, alternate, "%l", NULL);
887 puts("in order to display this message, you must reassemble it");
893 ** Show how to retrieve content of type "message/external".
896 show_external(CT ct, int alternate)
903 msg = add("You need to fetch the contents yourself:\n", NULL);
904 ap = ct->c_ctinfo.ci_attrs;
905 ep = ct->c_ctinfo.ci_values;
906 for (; *ap; ap++, ep++) {
907 msg = add(concat("\t", *ap, ": ", *ep, NULL), msg);
909 if (!(fp = fopen(ct->c_file, "r"))) {
910 adios(ct->c_file, "unable to open");
912 fseek(fp, ct->c_begin, SEEK_SET);
913 while (!feof(fp) && ftell(fp) < ct->c_end) {
914 if (!fgets(buf, sizeof buf, fp)) {
915 adios(ct->c_file, "unable to read");
917 *strchr(buf, '\n') = '\0';
919 continue; /* skip empty lines */
921 msg = add(concat("\t", buf, "\n", NULL), msg);
924 show_content_aux(ct, alternate, "%l", NULL);