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"
29 typedef int handler_f (BODY *, STATE *);
30 typedef handler_f *handler_t;
32 static void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
41 while ((c = fgetc (s->fpin)) != EOF && len--) {
42 if (c == '\r' && len) {
43 if ((ch = fgetc (s->fpin)) == '\n') {
52 if (l == sizeof (bufi))
53 mutt_convert_to_state (cd, bufi, &l, s);
56 mutt_convert_to_state (cd, bufi, &l, s);
57 mutt_convert_to_state (cd, 0, 0, s);
59 state_reset_prefix (s);
62 mutt_copy_bytes (s->fpin, s->fpout, len);
65 static int qp_decode_triple (char *s, char *d)
68 if (*s == '=' && !(*(s + 1)))
71 /* quoted-printable triple */
73 isxdigit ((unsigned char) *(s + 1)) &&
74 isxdigit ((unsigned char) *(s + 2))) {
75 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
83 static void qp_decode_line (char *dest, char *src, ssize_t * l, int last)
93 for (d = dest, s = src; *s;) {
94 switch ((kind = qp_decode_triple (s, &c))) {
98 break; /* qp triple */
101 break; /* single character */
105 break; /* soft line break */
109 if (!soft && last == '\n')
117 * Decode an attachment encoded with quoted-printable.
119 * Why doesn't this overflow any buffers? First, it's guaranteed
120 * that the length of a line grows when you _en_-code it to
121 * quoted-printable. That means that we always can store the
122 * result in a buffer of at most the _same_ size.
124 * Now, we don't special-case if the line we read with fgets()
125 * isn't terminated. We don't care about this, since STRING > 78,
126 * so corrupted input will just be corrupted a bit more. That
127 * implies that STRING+1 bytes are always sufficient to store the
128 * result of qp_decode_line.
130 * Finally, at soft line breaks, some part of a multibyte character
131 * may have been left over by mutt_convert_to_state(). This shouldn't
132 * be more than 6 characters, so STRING + 7 should be sufficient
133 * memory to store the decoded data.
135 * Just to make sure that I didn't make some off-by-one error
136 * above, we just use STRING*2 for the target buffer's size.
140 static void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
143 char decline[2 * STRING];
145 ssize_t linelen; /* number of input bytes in `line' */
148 int last; /* store the last character in the input line */
151 state_set_prefix (s);
157 * It's ok to use a fixed size buffer for input, even if the line turns
158 * out to be longer than this. Just process the line in chunks. This
159 * really shouldn't happen according the MIME spec, since Q-P encoded
160 * lines are at most 76 characters, but we should be liberal about what
163 if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
166 linelen = m_strlen(line);
170 * inspect the last character we read so we can tell if we got the
173 last = linelen ? line[linelen - 1] : 0;
175 /* chop trailing whitespace if we got the full line */
177 while (linelen > 0 && ISSPACE (line[linelen - 1]))
182 /* decode and do character set conversion */
183 qp_decode_line (decline + l, line, &l3, last);
185 mutt_convert_to_state (cd, decline, &l, s);
188 mutt_convert_to_state (cd, 0, 0, s);
189 state_reset_prefix (s);
192 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
195 int c1, c2, c3, c4, ch, cr = 0, i;
196 char bufi[BUFI_SIZE];
202 state_set_prefix (s);
205 for (i = 0; i < 4 && len > 0; len--) {
206 if ((ch = fgetc (s->fpin)) == EOF)
208 if (base64val(ch) >= 0 || ch == '=')
215 c1 = base64val(buf[0]);
216 c2 = base64val(buf[1]);
217 ch = (c1 << 2) | (c2 >> 4);
219 if (cr && ch != '\n')
224 if (istext && ch == '\r')
231 c3 = base64val(buf[2]);
232 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
234 if (cr && ch != '\n')
239 if (istext && ch == '\r')
246 c4 = base64val(buf[3]);
247 ch = ((c3 & 0x3) << 6) | c4;
249 if (cr && ch != '\n')
253 if (istext && ch == '\r')
258 if (l + 8 >= ssizeof (bufi))
259 mutt_convert_to_state (cd, bufi, &l, s);
265 mutt_convert_to_state (cd, bufi, &l, s);
266 mutt_convert_to_state (cd, 0, 0, s);
268 state_reset_prefix (s);
271 static unsigned char decode_byte(int ch)
273 return ch == 96 ? 0 : ch - 32;
276 static void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
279 char linelen, c, l, out;
281 char bufi[BUFI_SIZE];
285 state_set_prefix (s);
288 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
290 len -= m_strlen(tmps);
291 if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
295 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
297 len -= m_strlen(tmps);
298 if (!m_strncmp(tmps, "end", 3))
301 linelen = decode_byte(*pt);
303 for (c = 0; c < linelen;) {
304 for (l = 2; l <= 6; l += 2) {
305 out = decode_byte(*pt) << l;
307 out |= (decode_byte(*pt) >> (6 - l));
313 mutt_convert_to_state (cd, bufi, &k, s);
318 mutt_convert_to_state (cd, bufi, &k, s);
319 mutt_convert_to_state (cd, 0, 0, s);
321 state_reset_prefix (s);
324 /* ----------------------------------------------------------------------------
325 * A (not so) minimal implementation of RFC1563.
328 #define IndentSize (4)
330 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
331 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
332 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
336 const char *tag_name;
339 {"param", RICH_PARAM},
341 {"italic", RICH_ITALIC},
342 {"underline", RICH_UNDERLINE},
343 {"nofill", RICH_NOFILL},
344 {"excerpt", RICH_EXCERPT},
345 {"indent", RICH_INDENT},
346 {"indentright", RICH_INDENT_RIGHT},
347 {"center", RICH_CENTER},
348 {"flushleft", RICH_FLUSHLEFT},
349 {"flushright", RICH_FLUSHRIGHT},
350 {"flushboth", RICH_FLUSHLEFT},
351 {"color", RICH_COLOR},
352 {"x-color", RICH_COLOR},
356 struct enriched_state {
369 int tag_level[RICH_LAST_TAG];
374 static void enriched_wrap (struct enriched_state *stte)
379 if (stte->line_len) {
380 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
381 /* Strip trailing white space */
382 ssize_t y = stte->line_used - 1;
384 while (y && ISSPACE (stte->line[y])) {
385 stte->line[y] = '\0';
390 if (stte->tag_level[RICH_CENTER]) {
391 /* Strip leading whitespace */
394 while (stte->line[y] && ISSPACE (stte->line[y]))
399 for (z = y; z <= stte->line_used; z++) {
400 stte->line[z - y] = stte->line[z];
404 stte->line_used -= y;
409 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
410 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
412 if (stte->tag_level[RICH_CENTER]) {
415 state_putc (' ', stte->s);
419 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
422 state_putc (' ', stte->s);
427 state_puts (stte->line, stte->s);
430 state_putc ('\n', stte->s);
431 stte->line[0] = '\0';
434 stte->indent_len = 0;
435 if (stte->s->prefix) {
436 state_puts (stte->s->prefix, stte->s);
437 stte->indent_len += m_strlen(stte->s->prefix);
440 if (stte->tag_level[RICH_EXCERPT]) {
441 x = stte->tag_level[RICH_EXCERPT];
443 if (stte->s->prefix) {
444 state_puts (stte->s->prefix, stte->s);
445 stte->indent_len += m_strlen(stte->s->prefix);
448 state_puts ("> ", stte->s);
449 stte->indent_len += m_strlen("> ");
455 stte->indent_len = 0;
456 if (stte->tag_level[RICH_INDENT]) {
457 x = stte->tag_level[RICH_INDENT] * IndentSize;
458 stte->indent_len += x;
460 state_putc (' ', stte->s);
466 static void enriched_flush (struct enriched_state *stte, int wrap)
468 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
470 (stte->tag_level[RICH_INDENT_RIGHT] *
471 IndentSize) - stte->indent_len)))
472 enriched_wrap (stte);
474 if (stte->buff_used) {
475 stte->buffer[stte->buff_used] = '\0';
476 stte->line_used += stte->buff_used;
477 if (stte->line_used > stte->line_max) {
478 stte->line_max = stte->line_used;
479 p_realloc(&stte->line, stte->line_max + 1);
481 m_strcat(stte->line, stte->line_max + 1, stte->buffer);
482 stte->line_len += stte->word_len;
487 enriched_wrap (stte);
491 static void enriched_putc (int c, struct enriched_state *stte)
493 if (stte->tag_level[RICH_PARAM]) {
494 if (stte->tag_level[RICH_COLOR]) {
495 if (stte->param_used + 1 >= stte->param_len)
496 p_realloc(&stte->param, (stte->param_len += STRING));
498 stte->param[stte->param_used++] = c;
500 return; /* nothing to do */
503 /* see if more space is needed (plus extra for possible rich characters) */
504 if (stte->buff_len < stte->buff_used + 3) {
505 stte->buff_len += LONG_STRING;
506 p_realloc(&stte->buffer, stte->buff_len + 1);
509 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
511 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
515 stte->buffer[stte->buff_used++] = c;
516 enriched_flush (stte, 0);
519 if (stte->s->flags & M_DISPLAY) {
520 if (stte->tag_level[RICH_BOLD]) {
521 stte->buffer[stte->buff_used++] = c;
522 stte->buffer[stte->buff_used++] = '\010';
523 stte->buffer[stte->buff_used++] = c;
525 else if (stte->tag_level[RICH_UNDERLINE]) {
527 stte->buffer[stte->buff_used++] = '_';
528 stte->buffer[stte->buff_used++] = '\010';
529 stte->buffer[stte->buff_used++] = c;
531 else if (stte->tag_level[RICH_ITALIC]) {
532 stte->buffer[stte->buff_used++] = c;
533 stte->buffer[stte->buff_used++] = '\010';
534 stte->buffer[stte->buff_used++] = '_';
537 stte->buffer[stte->buff_used++] = c;
540 stte->buffer[stte->buff_used++] = c;
546 static void enriched_puts (const char *s, struct enriched_state *stte)
550 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
551 stte->buff_len += LONG_STRING;
552 p_realloc(&stte->buffer, stte->buff_len + 1);
557 stte->buffer[stte->buff_used++] = *p++;
561 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
563 const char *tagptr = tag;
569 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
570 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
571 j = EnrichedTags[i].index;
576 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
577 enriched_flush (stte, 1);
580 if (stte->tag_level[j]) /* make sure not to go negative */
581 stte->tag_level[j]--;
582 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
583 && stte->tag_level[RICH_COLOR]) {
584 stte->param[stte->param_used] = '\0';
585 if (!ascii_strcasecmp (stte->param, "black")) {
586 enriched_puts ("\033[30m", stte);
588 else if (!ascii_strcasecmp (stte->param, "red")) {
589 enriched_puts ("\033[31m", stte);
591 else if (!ascii_strcasecmp (stte->param, "green")) {
592 enriched_puts ("\033[32m", stte);
594 else if (!ascii_strcasecmp (stte->param, "yellow")) {
595 enriched_puts ("\033[33m", stte);
597 else if (!ascii_strcasecmp (stte->param, "blue")) {
598 enriched_puts ("\033[34m", stte);
600 else if (!ascii_strcasecmp (stte->param, "magenta")) {
601 enriched_puts ("\033[35m", stte);
603 else if (!ascii_strcasecmp (stte->param, "cyan")) {
604 enriched_puts ("\033[36m", stte);
606 else if (!ascii_strcasecmp (stte->param, "white")) {
607 enriched_puts ("\033[37m", stte);
610 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
611 enriched_puts ("\033[0m", stte);
614 /* flush parameter buffer when closing the tag */
615 if (j == RICH_PARAM) {
616 stte->param_used = 0;
617 stte->param[0] = '\0';
621 stte->tag_level[j]++;
623 if (j == RICH_EXCERPT)
624 enriched_flush (stte, 1);
628 static int text_enriched_handler (BODY * a, STATE * s)
631 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
634 long bytes = a->length;
635 struct enriched_state stte;
638 char tag[LONG_STRING + 1];
643 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
644 72) ? (COLS - 4) : 72);
645 stte.line_max = stte.WrapMargin * 4;
646 stte.line = p_new(char, stte.line_max + 1);
647 stte.param = p_new(char, STRING);
649 stte.param_len = STRING;
653 state_puts (s->prefix, s);
654 stte.indent_len += m_strlen(s->prefix);
657 while (state != DONE) {
658 if (state != ST_EOF) {
659 if (!bytes || (c = fgetc (s->fpin)) == EOF)
673 if (stte.tag_level[RICH_NOFILL]) {
674 enriched_flush (&stte, 1);
677 enriched_putc (' ', &stte);
683 enriched_putc (c, &stte);
689 enriched_putc (c, &stte);
697 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
701 enriched_set_flags (tag, &stte);
704 else if (tag_len < LONG_STRING) /* ignore overly long tags */
717 enriched_flush (&stte, 1);
726 enriched_putc ('\0', &stte);
727 enriched_flush (&stte, 1);
731 case DONE: /* not reached, but gcc complains if this is absent */
736 state_putc ('\n', s); /* add a final newline */
738 p_delete(&(stte.buffer));
739 p_delete(&(stte.line));
740 p_delete(&(stte.param));
747 #define TXTENRICHED 3
749 static int alternative_handler (BODY * a, STATE * s)
759 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
760 a->encoding == ENCUUENCODED) {
764 fstat (fileno (s->fpin), &st);
766 b->length = (long) st.st_size;
767 b->parts = mutt_parse_multipart(s->fpin,
768 parameter_getval(a->parameter, "boundary"),
770 mime_which_token(a->subtype, -1) == MIME_DIGEST);
777 /* First, search list of prefered types */
778 t = AlternativeOrderList;
779 while (t && !choice) {
781 int btlen; /* length of basetype */
782 int wild; /* do we have a wildcard to match all subtypes? */
784 c = strchr (t->data, '/');
786 wild = (c[1] == '*' && c[2] == 0);
791 btlen = m_strlen(t->data);
799 const char *bt = TYPE (b);
801 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
802 /* the basetype matches */
803 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
812 /* Next, look for an autoviewable type */
819 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
820 if (mutt_is_autoview (b, buf)) {
821 rfc1524_entry *entry = rfc1524_entry_new();
823 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
826 rfc1524_entry_delete(&entry);
832 /* Then, look for a text entry */
839 if (b->type == TYPETEXT) {
840 switch (mime_which_token(b->subtype, -1)) {
842 if (type <= TXTPLAIN) {
848 if (type <= TXTENRICHED) {
854 if (type <= TXTHTML) {
867 /* Finally, look for other possibilities */
874 if (mutt_can_decode (b))
881 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
882 fseeko (s->fpin, choice->hdr_offset, 0);
883 mutt_copy_bytes (s->fpin, s->fpout,
884 choice->offset - choice->hdr_offset);
886 mutt_body_handler (choice, s);
888 else if (s->flags & M_DISPLAY) {
889 /* didn't find anything that we could display! */
890 state_mark_attach (s);
891 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
901 /* handles message/rfc822 body parts */
902 static int message_handler (BODY * a, STATE * s)
909 off_start = ftello (s->fpin);
910 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
911 a->encoding == ENCUUENCODED) {
912 fstat (fileno (s->fpin), &st);
914 b->length = (long) st.st_size;
915 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
921 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
922 (((s->flags & M_WEED)
923 || ((s->flags & (M_DISPLAY | M_PRINTING))
924 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
925 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
929 state_puts (s->prefix, s);
930 state_putc ('\n', s);
932 rc = mutt_body_handler (b->parts, s);
935 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
936 a->encoding == ENCUUENCODED)
942 /* returns 1 if decoding the attachment will produce output */
943 int mutt_can_decode (BODY * a)
947 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
948 if (mutt_is_autoview (a, type))
949 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
950 else if (a->type == TYPETEXT)
952 else if (a->type == TYPEMESSAGE)
954 else if (a->type == TYPEMULTIPART) {
956 int tok = mime_which_token(a->subtype, -1);
958 if (tok == MIME_SIGNED || tok == MIME_ENCRYPTED)
961 for (p = a->parts; p; p = p->next) {
962 if (mutt_can_decode (p))
966 else if (a->type == TYPEAPPLICATION) {
967 if (mutt_is_application_pgp(a))
969 if (mutt_is_application_smime (a))
976 static int multipart_handler (BODY * a, STATE * s)
984 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
985 a->encoding == ENCUUENCODED) {
986 fstat (fileno (s->fpin), &st);
988 b->length = (long) st.st_size;
989 b->parts = mutt_parse_multipart(s->fpin,
990 parameter_getval(a->parameter, "boundary"),
992 mime_which_token(a->subtype, -1) == MIME_DIGEST);
997 for (p = b->parts, count = 1; p; p = p->next, count++) {
998 if (s->flags & M_DISPLAY) {
999 state_mark_attach (s);
1000 state_printf (s, _("[-- Attachment #%d"), count);
1001 if (p->description || p->filename || p->form_name) {
1002 state_puts (": ", s);
1003 state_puts (p->description ? p->description :
1004 p->filename ? p->filename : p->form_name, s);
1006 state_puts (" --]\n", s);
1008 mutt_pretty_size (length, sizeof (length), p->length);
1010 state_mark_attach (s);
1011 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1012 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1013 if (!option (OPTWEED)) {
1014 fseeko (s->fpin, p->hdr_offset, 0);
1015 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1018 state_putc ('\n', s);
1021 if (p->description && mutt_can_decode (p))
1022 state_printf (s, "Content-Description: %s\n", p->description);
1025 state_printf (s, "%s: \n", p->form_name);
1028 rc = mutt_body_handler (p, s);
1029 state_putc ('\n', s);
1030 if (rc || ((s->flags & M_REPLYING)
1031 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1035 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1036 a->encoding == ENCUUENCODED)
1042 static int autoview_handler (BODY * a, STATE * s)
1044 rfc1524_entry *entry = rfc1524_entry_new();
1045 char buffer[LONG_STRING];
1047 char command[LONG_STRING];
1048 char tempfile[_POSIX_PATH_MAX] = "";
1057 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1058 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1060 fname = m_strdup(a->filename);
1061 mutt_sanitize_filename (fname, 1);
1062 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1066 if (entry->command) {
1067 m_strcpy(command, sizeof(command), entry->command);
1069 /* rfc1524_expand_command returns 0 if the file is required */
1071 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1073 if (s->flags & M_DISPLAY) {
1074 state_mark_attach (s);
1075 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1076 mutt_message (_("Invoking autoview command: %s"), command);
1079 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1080 mutt_perror ("fopen");
1081 rfc1524_entry_delete(&entry);
1085 mutt_copy_bytes (s->fpin, fpin, a->length);
1089 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1094 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1095 fileno (fpin), -1, -1);
1099 mutt_perror (_("Can't create filter"));
1101 if (s->flags & M_DISPLAY) {
1102 state_mark_attach (s);
1103 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1110 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1111 state_puts (s->prefix, s);
1112 state_puts (buffer, s);
1114 /* check for data on stderr */
1115 if (fgets (buffer, sizeof (buffer), fperr)) {
1116 if (s->flags & M_DISPLAY) {
1117 state_mark_attach (s);
1118 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1121 state_puts (s->prefix, s);
1122 state_puts (buffer, s);
1123 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1124 state_puts (s->prefix, s);
1125 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 (mime_which_token(access_type, -1) == MIME_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) |
1223 if (s->flags & M_DISPLAY) {
1224 state_mark_attach (s);
1226 _("[-- This %s/%s attachment is not included, --]\n"),
1227 TYPE (b->parts), b->parts->subtype);
1228 state_mark_attach (s);
1231 ("[-- and the indicated access-type %s is unsupported --]\n"),
1233 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1234 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1241 void mutt_decode_attachment (BODY * b, STATE * s)
1243 int istext = mutt_is_text_part (b);
1244 iconv_t cd = MUTT_ICONV_ERROR;
1249 if (s->flags & M_CHARCONV) {
1250 const char *charset = parameter_getval(b->parameter, "charset");
1253 charset = charset_getfirst(MCharset.assumed_charset);
1254 if (charset && MCharset.charset)
1255 cd = mutt_iconv_open (MCharset.charset, charset, M_ICONV_HOOK_FROM);
1257 if (b->file_charset)
1258 cd = mutt_iconv_open (MCharset.charset, b->file_charset, M_ICONV_HOOK_FROM);
1262 fseeko (s->fpin, b->offset, 0);
1263 switch (b->encoding) {
1264 case ENCQUOTEDPRINTABLE:
1265 mutt_decode_quoted(s, b->length,
1266 istext || mutt_is_application_pgp(b), cd);
1269 mutt_decode_base64(s, b->length,
1270 istext || mutt_is_application_pgp(b), cd);
1273 mutt_decode_uuencoded(s, b->length,
1274 istext || mutt_is_application_pgp(b), cd);
1277 mutt_decode_xbit(s, b->length,
1278 istext || mutt_is_application_pgp(b), cd);
1282 if (cd != MUTT_ICONV_ERROR)
1286 int mutt_body_handler (BODY * b, STATE * s)
1291 char tempfile[_POSIX_PATH_MAX];
1292 handler_t handler = NULL;
1294 ssize_t tmplength = 0;
1298 int oflags = s->flags;
1299 int tok = mime_which_token(b->subtype, -1);
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 (tok == MIME_PLAIN) {
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 (tok == MIME_ENRICHED)
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))
1333 handler = message_handler;
1334 else if (tok == MIME_DELIVERY_STATUS)
1336 else if (tok == MIME_EXTERNAL_BODY)
1337 handler = external_body_handler;
1339 else if (b->type == TYPEMULTIPART) {
1342 if (tok == MIME_ALTERNATIVE)
1343 handler = alternative_handler;
1344 else if (tok == MIME_SIGNED) {
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 (tok == MIME_ENCRYPTED) {
1354 p = parameter_getval(b->parameter, "protocol");
1357 mutt_error(_("Error: multipart/encrypted has no protocol parameter!"));
1358 else if (mime_which_token(p, -1) == MIME_APPLICATION_PGP_ENCRYPTED)
1359 handler = crypt_pgp_encrypted_handler;
1363 handler = multipart_handler;
1365 else if (b->type == TYPEAPPLICATION) {
1366 if (mutt_is_application_pgp (b))
1367 handler = crypt_pgp_application_pgp_handler;
1368 if (mutt_is_application_smime (b))
1369 handler = crypt_smime_application_smime_handler;
1372 if (plaintext || handler) {
1373 fseeko (s->fpin, b->offset, 0);
1375 /* see if we need to decode this part before processing it */
1376 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE
1377 || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) {
1378 /* text subtypes may require character set conversion even with 8bit
1380 int origType = b->type;
1381 char *savePrefix = NULL;
1384 /* decode to a tempfile, saving the original destination */
1386 s->fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
1388 mutt_error _("Unable to open temporary file!");
1391 /* decoding the attachment changes the size and offset, so save a copy
1392 * of the "real" values now, and restore them after processing
1394 tmplength = b->length;
1395 tmpoffset = b->offset;
1397 /* if we are decoding binary bodies, we don't want to prefix each
1398 * line with the prefix or else the data will get corrupted.
1400 savePrefix = s->prefix;
1408 mutt_decode_attachment (b, s);
1411 b->length = ftello (s->fpout);
1413 m_fclose(&s->fpout);
1415 /* restore final destination and substitute the tempfile for input */
1418 s->fpin = safe_fopen (tempfile, "r");
1421 /* restore the prefix */
1422 s->prefix = savePrefix;
1428 /* process the (decoded) body part */
1430 rc = handler (b, s);
1433 b->length = tmplength;
1434 b->offset = tmpoffset;
1436 /* restore the original source stream */
1441 s->flags |= M_FIRSTDONE;
1443 else if (s->flags & M_DISPLAY) {
1444 state_mark_attach (s);
1445 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1446 if (!option (OPTVIEWATTACH)) {
1448 (type, sizeof (type),
1449 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1450 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1452 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1454 fputs (" --]\n", s->fpout);
1458 s->flags = oflags | (s->flags & M_FIRSTDONE);