2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-mime/mime.h>
13 #include <lib-mime/rfc3676.h>
15 #include <lib-ui/curses.h>
17 #include <lib-sys/unix.h>
20 #include "recvattach.h"
25 #include <lib-crypt/crypt.h>
31 typedef int handler_f (BODY *, STATE *);
32 typedef handler_f *handler_t;
34 static void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
43 while ((c = fgetc (s->fpin)) != EOF && len--) {
44 if (c == '\r' && len) {
45 if ((ch = fgetc (s->fpin)) == '\n') {
54 if (l == sizeof (bufi))
55 mutt_convert_to_state (cd, bufi, &l, s);
58 mutt_convert_to_state (cd, bufi, &l, s);
59 mutt_convert_to_state (cd, 0, 0, s);
61 state_reset_prefix (s);
64 mutt_copy_bytes (s->fpin, s->fpout, len);
67 static int qp_decode_triple (char *s, char *d)
70 if (*s == '=' && !(*(s + 1)))
73 /* quoted-printable triple */
75 isxdigit ((unsigned char) *(s + 1)) &&
76 isxdigit ((unsigned char) *(s + 2))) {
77 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
85 static void qp_decode_line (char *dest, char *src, ssize_t * l, int last)
95 for (d = dest, s = src; *s;) {
96 switch ((kind = qp_decode_triple (s, &c))) {
100 break; /* qp triple */
103 break; /* single character */
107 break; /* soft line break */
111 if (!soft && last == '\n')
119 * Decode an attachment encoded with quoted-printable.
121 * Why doesn't this overflow any buffers? First, it's guaranteed
122 * that the length of a line grows when you _en_-code it to
123 * quoted-printable. That means that we always can store the
124 * result in a buffer of at most the _same_ size.
126 * Now, we don't special-case if the line we read with fgets()
127 * isn't terminated. We don't care about this, since STRING > 78,
128 * so corrupted input will just be corrupted a bit more. That
129 * implies that STRING+1 bytes are always sufficient to store the
130 * result of qp_decode_line.
132 * Finally, at soft line breaks, some part of a multibyte character
133 * may have been left over by mutt_convert_to_state(). This shouldn't
134 * be more than 6 characters, so STRING + 7 should be sufficient
135 * memory to store the decoded data.
137 * Just to make sure that I didn't make some off-by-one error
138 * above, we just use STRING*2 for the target buffer's size.
142 static void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
145 char decline[2 * STRING];
147 ssize_t linelen; /* number of input bytes in `line' */
150 int last; /* store the last character in the input line */
153 state_set_prefix (s);
159 * It's ok to use a fixed size buffer for input, even if the line turns
160 * out to be longer than this. Just process the line in chunks. This
161 * really shouldn't happen according the MIME spec, since Q-P encoded
162 * lines are at most 76 characters, but we should be liberal about what
165 if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
168 linelen = m_strlen(line);
172 * inspect the last character we read so we can tell if we got the
175 last = linelen ? line[linelen - 1] : 0;
177 /* chop trailing whitespace if we got the full line */
179 while (linelen > 0 && ISSPACE (line[linelen - 1]))
184 /* decode and do character set conversion */
185 qp_decode_line (decline + l, line, &l3, last);
187 mutt_convert_to_state (cd, decline, &l, s);
190 mutt_convert_to_state (cd, 0, 0, s);
191 state_reset_prefix (s);
194 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
197 int c1, c2, c3, c4, ch, cr = 0, i;
198 char bufi[BUFI_SIZE];
204 state_set_prefix (s);
207 for (i = 0; i < 4 && len > 0; len--) {
208 if ((ch = fgetc (s->fpin)) == EOF)
210 if (base64val(ch) >= 0 || ch == '=')
217 c1 = base64val(buf[0]);
218 c2 = base64val(buf[1]);
219 ch = (c1 << 2) | (c2 >> 4);
221 if (cr && ch != '\n')
226 if (istext && ch == '\r')
233 c3 = base64val(buf[2]);
234 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
236 if (cr && ch != '\n')
241 if (istext && ch == '\r')
248 c4 = base64val(buf[3]);
249 ch = ((c3 & 0x3) << 6) | c4;
251 if (cr && ch != '\n')
255 if (istext && ch == '\r')
260 if (l + 8 >= ssizeof (bufi))
261 mutt_convert_to_state (cd, bufi, &l, s);
267 mutt_convert_to_state (cd, bufi, &l, s);
268 mutt_convert_to_state (cd, 0, 0, s);
270 state_reset_prefix (s);
273 static unsigned char decode_byte (char ch)
280 static void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
282 char tmps[SHORT_STRING];
283 char linelen, c, l, out;
285 char bufi[BUFI_SIZE];
289 state_set_prefix (s);
292 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
294 len -= m_strlen(tmps);
295 if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
299 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
301 len -= m_strlen(tmps);
302 if (!m_strncmp(tmps, "end", 3))
305 linelen = decode_byte (*pt);
307 for (c = 0; c < linelen;) {
308 for (l = 2; l <= 6; l += 2) {
309 out = decode_byte (*pt) << l;
311 out |= (decode_byte (*pt) >> (6 - l));
317 mutt_convert_to_state (cd, bufi, &k, s);
322 mutt_convert_to_state (cd, bufi, &k, s);
323 mutt_convert_to_state (cd, 0, 0, s);
325 state_reset_prefix (s);
328 /* ----------------------------------------------------------------------------
329 * A (not so) minimal implementation of RFC1563.
332 #define IndentSize (4)
334 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
335 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
336 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
340 const char *tag_name;
344 "param", RICH_PARAM}, {
345 "bold", RICH_BOLD}, {
346 "italic", RICH_ITALIC}, {
347 "underline", RICH_UNDERLINE}, {
348 "nofill", RICH_NOFILL}, {
349 "excerpt", RICH_EXCERPT}, {
350 "indent", RICH_INDENT}, {
351 "indentright", RICH_INDENT_RIGHT}, {
352 "center", RICH_CENTER}, {
353 "flushleft", RICH_FLUSHLEFT}, {
354 "flushright", RICH_FLUSHRIGHT}, {
355 "flushboth", RICH_FLUSHLEFT}, {
356 "color", RICH_COLOR}, {
357 "x-color", RICH_COLOR}, {
361 struct enriched_state {
374 int tag_level[RICH_LAST_TAG];
379 static void enriched_wrap (struct enriched_state *stte)
384 if (stte->line_len) {
385 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
386 /* Strip trailing white space */
387 ssize_t y = stte->line_used - 1;
389 while (y && ISSPACE (stte->line[y])) {
390 stte->line[y] = '\0';
395 if (stte->tag_level[RICH_CENTER]) {
396 /* Strip leading whitespace */
399 while (stte->line[y] && ISSPACE (stte->line[y]))
404 for (z = y; z <= stte->line_used; z++) {
405 stte->line[z - y] = stte->line[z];
409 stte->line_used -= y;
414 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
415 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
417 if (stte->tag_level[RICH_CENTER]) {
420 state_putc (' ', stte->s);
424 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
427 state_putc (' ', stte->s);
432 state_puts (stte->line, stte->s);
435 state_putc ('\n', stte->s);
436 stte->line[0] = '\0';
439 stte->indent_len = 0;
440 if (stte->s->prefix) {
441 state_puts (stte->s->prefix, stte->s);
442 stte->indent_len += m_strlen(stte->s->prefix);
445 if (stte->tag_level[RICH_EXCERPT]) {
446 x = stte->tag_level[RICH_EXCERPT];
448 if (stte->s->prefix) {
449 state_puts (stte->s->prefix, stte->s);
450 stte->indent_len += m_strlen(stte->s->prefix);
453 state_puts ("> ", stte->s);
454 stte->indent_len += m_strlen("> ");
460 stte->indent_len = 0;
461 if (stte->tag_level[RICH_INDENT]) {
462 x = stte->tag_level[RICH_INDENT] * IndentSize;
463 stte->indent_len += x;
465 state_putc (' ', stte->s);
471 static void enriched_flush (struct enriched_state *stte, int wrap)
473 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
475 (stte->tag_level[RICH_INDENT_RIGHT] *
476 IndentSize) - stte->indent_len)))
477 enriched_wrap (stte);
479 if (stte->buff_used) {
480 stte->buffer[stte->buff_used] = '\0';
481 stte->line_used += stte->buff_used;
482 if (stte->line_used > stte->line_max) {
483 stte->line_max = stte->line_used;
484 p_realloc(&stte->line, stte->line_max + 1);
486 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
487 stte->line_len += stte->word_len;
492 enriched_wrap (stte);
496 static void enriched_putc (int c, struct enriched_state *stte)
498 if (stte->tag_level[RICH_PARAM]) {
499 if (stte->tag_level[RICH_COLOR]) {
500 if (stte->param_used + 1 >= stte->param_len)
501 p_realloc(&stte->param, (stte->param_len += STRING));
503 stte->param[stte->param_used++] = c;
505 return; /* nothing to do */
508 /* see if more space is needed (plus extra for possible rich characters) */
509 if (stte->buff_len < stte->buff_used + 3) {
510 stte->buff_len += LONG_STRING;
511 p_realloc(&stte->buffer, stte->buff_len + 1);
514 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
516 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
520 stte->buffer[stte->buff_used++] = c;
521 enriched_flush (stte, 0);
524 if (stte->s->flags & M_DISPLAY) {
525 if (stte->tag_level[RICH_BOLD]) {
526 stte->buffer[stte->buff_used++] = c;
527 stte->buffer[stte->buff_used++] = '\010';
528 stte->buffer[stte->buff_used++] = c;
530 else if (stte->tag_level[RICH_UNDERLINE]) {
532 stte->buffer[stte->buff_used++] = '_';
533 stte->buffer[stte->buff_used++] = '\010';
534 stte->buffer[stte->buff_used++] = c;
536 else if (stte->tag_level[RICH_ITALIC]) {
537 stte->buffer[stte->buff_used++] = c;
538 stte->buffer[stte->buff_used++] = '\010';
539 stte->buffer[stte->buff_used++] = '_';
542 stte->buffer[stte->buff_used++] = c;
546 stte->buffer[stte->buff_used++] = c;
552 static void enriched_puts (const char *s, struct enriched_state *stte)
556 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
557 stte->buff_len += LONG_STRING;
558 p_realloc(&stte->buffer, stte->buff_len + 1);
563 stte->buffer[stte->buff_used++] = *p++;
567 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
569 const char *tagptr = tag;
575 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
576 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
577 j = EnrichedTags[i].index;
582 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
583 enriched_flush (stte, 1);
586 if (stte->tag_level[j]) /* make sure not to go negative */
587 stte->tag_level[j]--;
588 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
589 && stte->tag_level[RICH_COLOR]) {
590 stte->param[stte->param_used] = '\0';
591 if (!ascii_strcasecmp (stte->param, "black")) {
592 enriched_puts ("\033[30m", stte);
594 else if (!ascii_strcasecmp (stte->param, "red")) {
595 enriched_puts ("\033[31m", stte);
597 else if (!ascii_strcasecmp (stte->param, "green")) {
598 enriched_puts ("\033[32m", stte);
600 else if (!ascii_strcasecmp (stte->param, "yellow")) {
601 enriched_puts ("\033[33m", stte);
603 else if (!ascii_strcasecmp (stte->param, "blue")) {
604 enriched_puts ("\033[34m", stte);
606 else if (!ascii_strcasecmp (stte->param, "magenta")) {
607 enriched_puts ("\033[35m", stte);
609 else if (!ascii_strcasecmp (stte->param, "cyan")) {
610 enriched_puts ("\033[36m", stte);
612 else if (!ascii_strcasecmp (stte->param, "white")) {
613 enriched_puts ("\033[37m", stte);
616 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
617 enriched_puts ("\033[0m", stte);
620 /* flush parameter buffer when closing the tag */
621 if (j == RICH_PARAM) {
622 stte->param_used = 0;
623 stte->param[0] = '\0';
627 stte->tag_level[j]++;
629 if (j == RICH_EXCERPT)
630 enriched_flush (stte, 1);
634 static int text_enriched_handler (BODY * a, STATE * s)
637 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
640 long bytes = a->length;
641 struct enriched_state stte;
644 char tag[LONG_STRING + 1];
649 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
650 72) ? (COLS - 4) : 72);
651 stte.line_max = stte.WrapMargin * 4;
652 stte.line = p_new(char, stte.line_max + 1);
653 stte.param = p_new(char, STRING);
655 stte.param_len = STRING;
659 state_puts (s->prefix, s);
660 stte.indent_len += m_strlen(s->prefix);
663 while (state != DONE) {
664 if (state != ST_EOF) {
665 if (!bytes || (c = fgetc (s->fpin)) == EOF)
679 if (stte.tag_level[RICH_NOFILL]) {
680 enriched_flush (&stte, 1);
683 enriched_putc (' ', &stte);
689 enriched_putc (c, &stte);
695 enriched_putc (c, &stte);
703 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
707 enriched_set_flags (tag, &stte);
710 else if (tag_len < LONG_STRING) /* ignore overly long tags */
723 enriched_flush (&stte, 1);
732 enriched_putc ('\0', &stte);
733 enriched_flush (&stte, 1);
737 case DONE: /* not reached, but gcc complains if this is absent */
742 state_putc ('\n', s); /* add a final newline */
744 p_delete(&(stte.buffer));
745 p_delete(&(stte.line));
746 p_delete(&(stte.param));
753 #define TXTENRICHED 3
755 static int alternative_handler (BODY * a, STATE * s)
765 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
766 a->encoding == ENCUUENCODED) {
770 fstat (fileno (s->fpin), &st);
772 b->length = (long) st.st_size;
773 b->parts = mutt_parse_multipart(s->fpin,
774 parameter_getval(a->parameter, "boundary"),
776 !ascii_strcasecmp("digest", a->subtype));
783 /* First, search list of prefered types */
784 t = AlternativeOrderList;
785 while (t && !choice) {
787 int btlen; /* length of basetype */
788 int wild; /* do we have a wildcard to match all subtypes? */
790 c = strchr (t->data, '/');
792 wild = (c[1] == '*' && c[2] == 0);
797 btlen = m_strlen(t->data);
805 const char *bt = TYPE (b);
807 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
808 /* the basetype matches */
809 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
818 /* Next, look for an autoviewable type */
825 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
826 if (mutt_is_autoview (b, buf)) {
827 rfc1524_entry *entry = rfc1524_entry_new();
829 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
832 rfc1524_entry_delete(&entry);
838 /* Then, look for a text entry */
845 if (b->type == TYPETEXT) {
846 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
850 else if (!ascii_strcasecmp ("enriched", b->subtype)
851 && type <= TXTENRICHED) {
855 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
864 /* Finally, look for other possibilities */
871 if (mutt_can_decode (b))
878 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
879 fseeko (s->fpin, choice->hdr_offset, 0);
880 mutt_copy_bytes (s->fpin, s->fpout,
881 choice->offset - choice->hdr_offset);
883 mutt_body_handler (choice, s);
885 else if (s->flags & M_DISPLAY) {
886 /* didn't find anything that we could display! */
887 state_mark_attach (s);
888 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
898 /* handles message/rfc822 body parts */
899 static int message_handler (BODY * a, STATE * s)
906 off_start = ftello (s->fpin);
907 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
908 a->encoding == ENCUUENCODED) {
909 fstat (fileno (s->fpin), &st);
911 b->length = (long) st.st_size;
912 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
918 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
919 (((s->flags & M_WEED)
920 || ((s->flags & (M_DISPLAY | M_PRINTING))
921 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
922 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
926 state_puts (s->prefix, s);
927 state_putc ('\n', s);
929 rc = mutt_body_handler (b->parts, s);
932 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
933 a->encoding == ENCUUENCODED)
939 /* returns 1 if decoding the attachment will produce output */
940 int mutt_can_decode (BODY * a)
944 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
945 if (mutt_is_autoview (a, type))
946 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
947 else if (a->type == TYPETEXT)
949 else if (a->type == TYPEMESSAGE)
951 else if (a->type == TYPEMULTIPART) {
954 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
955 ascii_strcasecmp (a->subtype, "encrypted") == 0)
958 for (p = a->parts; p; p = p->next) {
959 if (mutt_can_decode (p))
964 else if (a->type == TYPEAPPLICATION) {
965 if (mutt_is_application_pgp(a))
967 if (mutt_is_application_smime (a))
974 static int multipart_handler (BODY * a, STATE * s)
982 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
983 a->encoding == ENCUUENCODED) {
984 fstat (fileno (s->fpin), &st);
986 b->length = (long) st.st_size;
987 b->parts = mutt_parse_multipart(s->fpin,
988 parameter_getval(a->parameter, "boundary"),
990 !ascii_strcasecmp("digest", a->subtype));
995 for (p = b->parts, count = 1; p; p = p->next, count++) {
996 if (s->flags & M_DISPLAY) {
997 state_mark_attach (s);
998 state_printf (s, _("[-- Attachment #%d"), count);
999 if (p->description || p->filename || p->form_name) {
1000 state_puts (": ", s);
1001 state_puts (p->description ? p->description :
1002 p->filename ? p->filename : p->form_name, s);
1004 state_puts (" --]\n", s);
1006 mutt_pretty_size (length, sizeof (length), p->length);
1008 state_mark_attach (s);
1009 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1010 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1011 if (!option (OPTWEED)) {
1012 fseeko (s->fpin, p->hdr_offset, 0);
1013 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1016 state_putc ('\n', s);
1019 if (p->description && mutt_can_decode (p))
1020 state_printf (s, "Content-Description: %s\n", p->description);
1023 state_printf (s, "%s: \n", p->form_name);
1026 rc = mutt_body_handler (p, s);
1027 state_putc ('\n', s);
1028 if (rc || ((s->flags & M_REPLYING)
1029 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1033 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1034 a->encoding == ENCUUENCODED)
1040 static int autoview_handler (BODY * a, STATE * s)
1042 rfc1524_entry *entry = rfc1524_entry_new();
1043 char buffer[LONG_STRING];
1045 char command[LONG_STRING];
1046 char tempfile[_POSIX_PATH_MAX] = "";
1055 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1056 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1058 fname = m_strdup(a->filename);
1059 mutt_sanitize_filename (fname, 1);
1060 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1064 if (entry->command) {
1065 m_strcpy(command, sizeof(command), entry->command);
1067 /* rfc1524_expand_command returns 0 if the file is required */
1069 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1071 if (s->flags & M_DISPLAY) {
1072 state_mark_attach (s);
1073 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1074 mutt_message (_("Invoking autoview command: %s"), command);
1077 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1078 mutt_perror ("fopen");
1079 rfc1524_entry_delete(&entry);
1083 mutt_copy_bytes (s->fpin, fpin, a->length);
1087 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1093 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1094 fileno (fpin), -1, -1);
1098 mutt_perror (_("Can't create filter"));
1100 if (s->flags & M_DISPLAY) {
1101 state_mark_attach (s);
1102 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1109 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1110 state_puts (s->prefix, s);
1111 state_puts (buffer, s);
1113 /* check for data on stderr */
1114 if (fgets (buffer, sizeof (buffer), fperr)) {
1115 if (s->flags & M_DISPLAY) {
1116 state_mark_attach (s);
1117 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1120 state_puts (s->prefix, s);
1121 state_puts (buffer, s);
1122 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1123 state_puts (s->prefix, s);
1124 state_puts (buffer, s);
1129 mutt_copy_stream (fpout, s->fpout);
1130 /* Check for stderr messages */
1131 if (fgets (buffer, sizeof (buffer), fperr)) {
1132 if (s->flags & M_DISPLAY) {
1133 state_mark_attach (s);
1134 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1137 state_puts (buffer, s);
1138 mutt_copy_stream (fperr, s->fpout);
1146 mutt_wait_filter (thepid);
1150 mutt_unlink (tempfile);
1152 if (s->flags & M_DISPLAY)
1153 mutt_clear_error ();
1155 rfc1524_entry_delete(&entry);
1159 static int external_body_handler (BODY * b, STATE * s)
1161 const char *access_type;
1162 const char *expiration;
1165 access_type = parameter_getval(b->parameter, "access-type");
1167 if (s->flags & M_DISPLAY) {
1168 state_mark_attach (s);
1169 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1174 expiration = parameter_getval(b->parameter, "expiration");
1176 expire = mutt_parse_date (expiration, NULL);
1180 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1181 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1183 char pretty_size[10];
1185 state_mark_attach (s);
1186 state_printf (s, _("[-- This %s/%s attachment "),
1187 TYPE (b->parts), b->parts->subtype);
1188 length = parameter_getval(b->parameter, "length");
1190 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1191 strtol (length, NULL, 10));
1192 state_printf (s, _("(size %s bytes) "), pretty_size);
1194 state_puts (_("has been deleted --]\n"), s);
1197 state_mark_attach (s);
1198 state_printf (s, _("[-- on %s --]\n"), expiration);
1200 if (b->parts->filename) {
1201 state_mark_attach (s);
1202 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1205 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1206 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1210 else if (expiration && expire < time (NULL)) {
1211 if (s->flags & M_DISPLAY) {
1212 state_mark_attach (s);
1213 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1214 TYPE (b->parts), b->parts->subtype);
1215 state_attach_puts (_("[-- and the indicated external source has --]\n"
1216 "[-- expired. --]\n"), s);
1218 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1219 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1224 if (s->flags & M_DISPLAY) {
1225 state_mark_attach (s);
1227 _("[-- This %s/%s attachment is not included, --]\n"),
1228 TYPE (b->parts), b->parts->subtype);
1229 state_mark_attach (s);
1232 ("[-- and the indicated access-type %s is unsupported --]\n"),
1234 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1235 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1242 void mutt_decode_attachment (BODY * b, STATE * s)
1244 int istext = mutt_is_text_part (b);
1245 iconv_t cd = MUTT_ICONV_ERROR;
1250 if (s->flags & M_CHARCONV) {
1251 const char *charset = parameter_getval(b->parameter, "charset");
1254 charset = charset_getfirst(AssumedCharset);
1255 if (charset && Charset)
1256 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1258 if (b->file_charset)
1259 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1263 fseeko (s->fpin, b->offset, 0);
1264 switch (b->encoding) {
1265 case ENCQUOTEDPRINTABLE:
1266 mutt_decode_quoted(s, b->length,
1267 istext || mutt_is_application_pgp(b), cd);
1270 mutt_decode_base64(s, b->length,
1271 istext || mutt_is_application_pgp(b), cd);
1274 mutt_decode_uuencoded(s, b->length,
1275 istext || mutt_is_application_pgp(b), cd);
1278 mutt_decode_xbit(s, b->length,
1279 istext || mutt_is_application_pgp(b), cd);
1283 if (cd != MUTT_ICONV_ERROR)
1287 int mutt_body_handler (BODY * b, STATE * s)
1292 char tempfile[_POSIX_PATH_MAX];
1293 handler_t handler = NULL;
1295 ssize_t tmplength = 0;
1299 int oflags = s->flags;
1301 /* first determine which handler to use to process this part */
1303 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1304 if (mutt_is_autoview (b, type)) {
1305 rfc1524_entry *entry = rfc1524_entry_new();
1307 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1308 handler = autoview_handler;
1309 s->flags &= ~M_CHARCONV;
1311 rfc1524_entry_delete(&entry);
1313 else if (b->type == TYPETEXT) {
1314 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1315 /* avoid copying this part twice since removing the transfer-encoding is
1316 * the only operation needed.
1318 if (mutt_is_application_pgp (b))
1319 handler = crypt_pgp_application_pgp_handler;
1321 if (!ascii_strcasecmp("flowed", parameter_getval(b->parameter, "format")))
1322 handler = rfc3676_handler;
1326 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1327 handler = text_enriched_handler;
1328 else /* text body type without a handler */
1331 else if (b->type == TYPEMESSAGE) {
1332 if (mutt_is_message_type (b->type, b->subtype))
1333 handler = message_handler;
1334 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1336 else if (!ascii_strcasecmp ("external-body", b->subtype))
1337 handler = external_body_handler;
1339 else if (b->type == TYPEMULTIPART) {
1342 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1343 handler = alternative_handler;
1344 else if (ascii_strcasecmp ("signed", b->subtype) == 0) {
1345 p = parameter_getval(b->parameter, "protocol");
1348 mutt_error (_("Error: multipart/signed has no protocol."));
1350 else if (s->flags & M_VERIFY)
1351 handler = mutt_signed_handler;
1353 else if (m_strcasecmp("encrypted", b->subtype) == 0) {
1354 p = parameter_getval(b->parameter, "protocol");
1358 ("Error: multipart/encrypted has no protocol parameter!"));
1360 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1361 handler = crypt_pgp_encrypted_handler;
1365 handler = multipart_handler;
1367 else if (b->type == TYPEAPPLICATION) {
1368 if (mutt_is_application_pgp (b))
1369 handler = crypt_pgp_application_pgp_handler;
1370 if (mutt_is_application_smime (b))
1371 handler = crypt_smime_application_smime_handler;
1375 if (plaintext || handler) {
1376 fseeko (s->fpin, b->offset, 0);
1378 /* see if we need to decode this part before processing it */
1379 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1381 * set conversion even
1382 * with 8bit encoding.
1384 int origType = b->type;
1385 char *savePrefix = NULL;
1388 /* decode to a tempfile, saving the original destination */
1390 s->fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
1392 mutt_error _("Unable to open temporary file!");
1395 /* decoding the attachment changes the size and offset, so save a copy
1396 * of the "real" values now, and restore them after processing
1398 tmplength = b->length;
1399 tmpoffset = b->offset;
1401 /* if we are decoding binary bodies, we don't want to prefix each
1402 * line with the prefix or else the data will get corrupted.
1404 savePrefix = s->prefix;
1412 mutt_decode_attachment (b, s);
1415 b->length = ftello (s->fpout);
1417 m_fclose(&s->fpout);
1419 /* restore final destination and substitute the tempfile for input */
1422 s->fpin = safe_fopen (tempfile, "r");
1425 /* restore the prefix */
1426 s->prefix = savePrefix;
1432 /* process the (decoded) body part */
1434 rc = handler (b, s);
1437 b->length = tmplength;
1438 b->offset = tmpoffset;
1440 /* restore the original source stream */
1445 s->flags |= M_FIRSTDONE;
1447 else if (s->flags & M_DISPLAY) {
1448 state_mark_attach (s);
1449 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1450 if (!option (OPTVIEWATTACH)) {
1452 (type, sizeof (type),
1453 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1454 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1456 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1458 fputs (" --]\n", s->fpout);
1462 s->flags = oflags | (s->flags & M_FIRSTDONE);