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>
16 #include <h/mhparse.h>
32 void content_error(char *, CT, char *, ...);
33 void flush_errors(void);
36 int list_switch(CT, int, int, int);
37 int list_content(CT, int, int, int);
42 void show_all_messages(CT *);
43 int show_content_aux(CT, int, char *, char *);
48 static void show_single_message(CT, char *);
49 static void DisplayMsgHeader(CT, char *);
50 static int show_switch(CT, int);
51 static int show_content(CT, int);
52 static int show_content_aux2(CT, int, char *, char *, int, int, int);
53 static int show_text(CT, int);
54 static int show_multi(CT, int);
55 static int show_multi_internal(CT, int);
56 static int show_multi_aux(CT, int, char *);
57 static int show_message_rfc822(CT, int);
58 static int show_partial(CT, int);
59 static int show_external(CT, int);
63 ** Top level entry point to show/display a group of messages
67 show_all_messages(CT *cts)
72 ** If form is not specified, then get default form
73 ** for showing headers of MIME messages.
76 formsw = getcpy(etcpath("mhl.headers"));
79 ** If form is "mhl.null", suppress display of header.
81 if (strcmp(formsw, "mhl.null")==0)
84 for (ctp = cts; *ctp; ctp++) {
87 if (!type_ok(ct, 1)) { /* top-level type */
94 printf(">>> Message %s\n\n", ct->c_file);
96 show_single_message(ct, formsw);
102 ** Entry point to show/display a single message
106 show_single_message(CT ct, char *form)
109 ** Allow user executable bit so that temporary directories created by
110 ** the viewer (e.g., lynx) are going to be accessible
112 umask(ct->c_umask & ~(0100));
115 ** If you have a format file, then display
116 ** the message headers.
119 DisplayMsgHeader(ct, form);
121 /* Show the body of the message */
128 if (ct->c_ceclosefnx)
129 (*ct->c_ceclosefnx) (ct);
136 ** Use mhl to show the header fields
139 DisplayMsgHeader(CT ct, char *form)
147 vec[vecp++] = "-form";
149 vec[vecp++] = "-nobody";
150 vec[vecp++] = ct->c_file;
155 switch (child_id = fork()) {
157 adios("fork", "unable to");
162 fprintf(stderr, "unable to exec ");
168 pidcheck(pidwait(child_id, NOTOK));
175 ** Switching routine. Call the correct routine
176 ** based on content type.
180 show_switch(CT ct, int alternate)
182 switch (ct->c_type) {
184 return show_multi(ct, alternate);
188 switch (ct->c_subtype) {
189 case MESSAGE_PARTIAL:
190 return show_partial(ct, alternate);
193 case MESSAGE_EXTERNAL:
194 return show_external(ct, alternate);
199 return show_message_rfc822(ct, alternate);
205 return show_text(ct, alternate);
212 return show_content(ct, alternate);
216 adios(NULL, "unknown content type %d", ct->c_type);
220 return 0; /* NOT REACHED */
225 ** Generic method for displaying content
229 show_content(CT ct, int alternate)
231 char *cp, buffer[BUFSIZ];
232 CI ci = &ct->c_ctinfo;
234 /* Check for mhshow-show-type/subtype */
235 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
236 ci->ci_type, ci->ci_subtype);
237 if ((cp = context_find(buffer)) && *cp != '\0')
238 return show_content_aux(ct, alternate, cp, NULL);
240 /* Check for mhshow-show-type */
241 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
242 if ((cp = context_find(buffer)) && *cp != '\0')
243 return show_content_aux(ct, alternate, cp, NULL);
245 if ((cp = ct->c_showproc))
246 return show_content_aux(ct, alternate, cp, NULL);
248 /* complain if we are not a part of a multipart/alternative */
250 return show_content_aux(ct, alternate, "%l", NULL);
257 ** Parse the display string for displaying generic content
260 show_content_aux(CT ct, int alternate, char *cp, char *cracked)
262 int fd, len, buflen, quoted;
264 char *bp, *pp, *file, buffer[BUFSIZ];
265 CI ci = &ct->c_ctinfo;
267 if (!ct->c_ceopenfnx) {
269 content_error(NULL, ct,
270 "don't know how to decode content");
276 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
278 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
279 return (alternate ? DONE : OK);
285 strncpy(buffer, cp, sizeof(buffer));
289 /* get buffer ready to go */
291 buflen = sizeof(buffer) - 1;
292 bp[0] = bp[buflen] = '\0';
295 /* Now parse display string */
296 for ( ; *cp && buflen > 0; cp++) {
302 /* insert parameters from Content-Type field */
307 for (ap = ci->ci_attrs, ep = ci->ci_values;
309 snprintf(bp, buflen, "%s%s=\"%s\"",
321 strncpy(bp, ct->c_charset ? ct->c_charset :
326 /* insert content description */
330 s = trimcpy(ct->c_descr);
331 strncpy(bp, s, buflen);
337 /* %f, and stdin is terminal not content */
342 /* insert filename containing content */
343 snprintf(bp, buflen, "'%s'", file);
345 ** since we've quoted the file argument,
346 ** set things up to look past it, to avoid
347 ** problems with the quoting logic below.
348 ** (I know, I should figure out what's
349 ** broken with the quoting logic, but..)
359 ** display listing prior to displaying content
365 /* insert subtype of content */
366 strncpy(bp, ci->ci_subtype, buflen);
370 /* insert character % */
383 /* Did we actually insert something? */
386 ** Insert single quote if not inside quotes
389 if (!quoted && buflen) {
391 memmove(pp + 1, pp, len);
396 /* Escape existing quotes */
397 while ((pp = strchr(pp, '\'')) && buflen > 3) {
399 memmove(pp + 3, pp, len);
407 ** If pp is still set, that means we ran
412 if (!quoted && buflen) {
430 return show_content_aux2(ct, alternate, cracked, buffer,
436 ** Routine to actually display the content
439 show_content_aux2(CT ct, int alternate, char *cracked,
440 char *buffer, int fd, int xlist, int xstdin)
444 if (debugsw || cracked) {
447 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
450 fprintf(stderr, " part %s", ct->c_partno);
452 fprintf(stderr, " using command (cd %s; %s)\n",
455 fprintf(stderr, " using command %s\n", buffer);
459 if (ct->c_type == CT_MULTIPART)
460 list_content(ct, -1, 0, 0);
462 list_switch(ct, -1, 0, 0);
467 switch (child_id = fork()) {
469 advise("fork", "unable to");
470 (*ct->c_ceclosefnx) (ct);
479 execlp("/bin/sh", "/bin/sh", "-c", buffer, NULL);
480 fprintf(stderr, "unable to exec ");
486 pidcheck(pidXwait(child_id, NULL));
489 (*ct->c_ceclosefnx) (ct);
490 return (alternate ? DONE : OK);
496 ** show content of type "text"
500 show_text(CT ct, int alternate)
502 char *cp, buffer[BUFSIZ];
503 CI ci = &ct->c_ctinfo;
505 /* Check for mhshow-show-type/subtype */
506 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
507 ci->ci_type, ci->ci_subtype);
508 if ((cp = context_find(buffer)) && *cp != '\0')
509 return show_content_aux(ct, alternate, cp, NULL);
511 /* Check for mhshow-show-type */
512 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
513 if ((cp = context_find(buffer)) && *cp != '\0')
514 return show_content_aux(ct, alternate, cp, NULL);
517 ** Use default method if content is text/plain, or if
518 ** if it is not a text part of a multipart/alternative
520 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
521 if (ct->c_charset && !is_native_charset(ct->c_charset)) {
522 snprintf(buffer, sizeof(buffer), "%%liconv -f '%s'",
525 snprintf(buffer, sizeof(buffer), "%%lcat");
527 ct->c_showproc = getcpy(buffer);
528 return show_content_aux(ct, alternate, ct->c_showproc, NULL);
536 ** show message body of type "multipart"
540 show_multi(CT ct, int alternate)
542 char *cp, buffer[BUFSIZ];
543 CI ci = &ct->c_ctinfo;
545 /* Check for mhshow-show-type/subtype */
546 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
547 ci->ci_type, ci->ci_subtype);
548 if ((cp = context_find(buffer)) && *cp != '\0')
549 return show_multi_aux(ct, alternate, cp);
551 /* Check for mhshow-show-type */
552 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
553 if ((cp = context_find(buffer)) && *cp != '\0')
554 return show_multi_aux(ct, alternate, cp);
556 if ((cp = ct->c_showproc))
557 return show_multi_aux(ct, alternate, cp);
560 ** Use default method to display this multipart content
561 ** if it is not a (nested) part of a multipart/alternative,
562 ** or if it is one of the known subtypes of multipart.
564 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
565 return show_multi_internal(ct, alternate);
572 ** show message body of subtypes of multipart that
573 ** we understand directly (mixed, alternate, etc...)
577 show_multi_internal(CT ct, int alternate)
579 int alternating, nowalternate, result;
580 struct multipart *m = (struct multipart *) ct->c_ctparams;
585 nowalternate = alternate;
587 if (ct->c_subtype == MULTI_ALTERNATE) {
593 ** Other possible multipart types are:
594 ** - multipart/parallel
596 ** - multipart/digest
597 ** - unknown subtypes of multipart (treat as mixed per rfc2046)
601 ** alternate -> we are a part inside an multipart/alternative
602 ** alternating -> we are a multipart/alternative
605 result = alternate ? NOTOK : OK;
607 for (part = m->mp_parts; part; part = part->mp_next) {
610 if (part_ok(p, 1) && type_ok(p, 1)) {
613 inneresult = show_switch(p, nowalternate);
614 switch (inneresult) {
616 if (alternate && !alternating) {
629 alternate = nowalternate = 0;
639 if (alternating && !part) {
641 content_error(NULL, ct, "don't know how to display any of the contents");
651 ** Parse display string for multipart content
652 ** and use external program to display it.
656 show_multi_aux(CT ct, int alternate, char *cp)
658 int len, buflen, quoted;
660 char *bp, *pp, *file, buffer[BUFSIZ];
661 struct multipart *m = (struct multipart *) ct->c_ctparams;
663 CI ci = &ct->c_ctinfo;
666 for (part = m->mp_parts; part; part = part->mp_next) {
669 if (!p->c_ceopenfnx) {
671 content_error(NULL, p, "don't know how to decode content");
675 if (p->c_storage == NULL) {
677 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
680 /* I'm not sure if this is necessary? */
681 p->c_storage = getcpy(file);
683 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
684 return (alternate ? DONE : OK);
685 (*p->c_ceclosefnx) (p);
691 /* get buffer ready to go */
693 buflen = sizeof(buffer) - 1;
694 bp[0] = bp[buflen] = '\0';
697 /* Now parse display string */
698 for ( ; *cp && buflen > 0; cp++) {
703 /* insert parameters from Content-Type field */
708 for (ap = ci->ci_attrs, ep = ci->ci_values;
710 snprintf(bp, buflen, "%s%s=\"%s\"",
722 strncpy(bp, ct->c_charset ? ct->c_charset :
727 /* insert content description */
731 s = trimcpy(ct->c_descr);
732 strncpy(bp, s, buflen);
739 /* insert filename(s) containing content */
743 for (part = m->mp_parts; part;
744 part = part->mp_next) {
747 snprintf(bp, buflen, "%s'%s'",
755 ** set our starting pointer back to bp,
756 ** to avoid requoting the filenames we
765 ** display listing prior to displaying content
771 /* insert subtype of content */
772 strncpy(bp, ci->ci_subtype, buflen);
776 /* insert character % */
789 /* Did we actually insert something? */
792 ** Insert single quote if not inside quotes
795 if (!quoted && buflen) {
797 memmove(pp + 1, pp, len);
802 /* Escape existing quotes */
803 while ((pp = strchr(pp, '\'')) && buflen > 3) {
805 memmove(pp + 3, pp, len);
813 ** If pp is still set, that means we ran
818 if (!quoted && buflen) {
835 return show_content_aux2(ct, alternate, NULL, buffer,
841 ** show content of type "message/rfc822"
845 show_message_rfc822(CT ct, int alternate)
847 char *cp, buffer[BUFSIZ];
848 CI ci = &ct->c_ctinfo;
850 /* Check for mhshow-show-type/subtype */
851 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
852 ci->ci_type, ci->ci_subtype);
853 if ((cp = context_find(buffer)) && *cp != '\0')
854 return show_content_aux(ct, alternate, cp, NULL);
856 /* Check for mhshow-show-type */
857 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
858 if ((cp = context_find(buffer)) && *cp != '\0')
859 return show_content_aux(ct, alternate, cp, NULL);
861 if ((cp = ct->c_showproc))
862 return show_content_aux(ct, alternate, cp, NULL);
864 /* default method for message/rfc822 */
865 if (ct->c_subtype == MESSAGE_RFC822) {
866 cp = (ct->c_showproc = getcpy("%lshow -file %F"));
867 return show_content_aux(ct, alternate, cp, NULL);
870 /* complain if we are not a part of a multipart/alternative */
872 return show_content_aux(ct, alternate, "%l", NULL);
879 ** Show content of type "message/partial".
883 show_partial(CT ct, int alternate)
885 show_content_aux(ct, alternate, "%l", NULL);
886 puts("in order to display this message, you must reassemble it");
892 ** Show how to retrieve content of type "message/external".
895 show_external(CT ct, int alternate)
902 msg = add("You need to fetch the contents yourself:\n", NULL);
903 ap = ct->c_ctinfo.ci_attrs;
904 ep = ct->c_ctinfo.ci_values;
905 for (; *ap; ap++, ep++) {
906 msg = add(concat("\t", *ap, ": ", *ep, NULL), msg);
908 if (!(fp = fopen(ct->c_file, "r"))) {
909 adios(ct->c_file, "unable to open");
911 fseek(fp, ct->c_begin, SEEK_SET);
912 while (!feof(fp) && ftell(fp) < ct->c_end) {
913 if (!fgets(buf, sizeof buf, fp)) {
914 adios(ct->c_file, "unable to read");
916 *strchr(buf, '\n') = '\0';
918 continue; /* skip empty lines */
920 msg = add(concat("\t", buf, "\n", NULL), msg);
923 show_content_aux(ct, alternate, "%l", NULL);