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(int ch)
275 return ch == 96 ? 0 : ch - 32;
278 static void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
281 char linelen, c, l, out;
283 char bufi[BUFI_SIZE];
287 state_set_prefix (s);
290 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
292 len -= m_strlen(tmps);
293 if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
297 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
299 len -= m_strlen(tmps);
300 if (!m_strncmp(tmps, "end", 3))
303 linelen = decode_byte(*pt);
305 for (c = 0; c < linelen;) {
306 for (l = 2; l <= 6; l += 2) {
307 out = decode_byte(*pt) << l;
309 out |= (decode_byte(*pt) >> (6 - l));
315 mutt_convert_to_state (cd, bufi, &k, s);
320 mutt_convert_to_state (cd, bufi, &k, s);
321 mutt_convert_to_state (cd, 0, 0, s);
323 state_reset_prefix (s);
326 /* ----------------------------------------------------------------------------
327 * A (not so) minimal implementation of RFC1563.
330 #define IndentSize (4)
332 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
333 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
334 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
338 const char *tag_name;
341 {"param", RICH_PARAM},
343 {"italic", RICH_ITALIC},
344 {"underline", RICH_UNDERLINE},
345 {"nofill", RICH_NOFILL},
346 {"excerpt", RICH_EXCERPT},
347 {"indent", RICH_INDENT},
348 {"indentright", RICH_INDENT_RIGHT},
349 {"center", RICH_CENTER},
350 {"flushleft", RICH_FLUSHLEFT},
351 {"flushright", RICH_FLUSHRIGHT},
352 {"flushboth", RICH_FLUSHLEFT},
353 {"color", RICH_COLOR},
354 {"x-color", RICH_COLOR},
358 struct enriched_state {
371 int tag_level[RICH_LAST_TAG];
376 static void enriched_wrap (struct enriched_state *stte)
381 if (stte->line_len) {
382 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
383 /* Strip trailing white space */
384 ssize_t y = stte->line_used - 1;
386 while (y && ISSPACE (stte->line[y])) {
387 stte->line[y] = '\0';
392 if (stte->tag_level[RICH_CENTER]) {
393 /* Strip leading whitespace */
396 while (stte->line[y] && ISSPACE (stte->line[y]))
401 for (z = y; z <= stte->line_used; z++) {
402 stte->line[z - y] = stte->line[z];
406 stte->line_used -= y;
411 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
412 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
414 if (stte->tag_level[RICH_CENTER]) {
417 state_putc (' ', stte->s);
421 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
424 state_putc (' ', stte->s);
429 state_puts (stte->line, stte->s);
432 state_putc ('\n', stte->s);
433 stte->line[0] = '\0';
436 stte->indent_len = 0;
437 if (stte->s->prefix) {
438 state_puts (stte->s->prefix, stte->s);
439 stte->indent_len += m_strlen(stte->s->prefix);
442 if (stte->tag_level[RICH_EXCERPT]) {
443 x = stte->tag_level[RICH_EXCERPT];
445 if (stte->s->prefix) {
446 state_puts (stte->s->prefix, stte->s);
447 stte->indent_len += m_strlen(stte->s->prefix);
450 state_puts ("> ", stte->s);
451 stte->indent_len += m_strlen("> ");
457 stte->indent_len = 0;
458 if (stte->tag_level[RICH_INDENT]) {
459 x = stte->tag_level[RICH_INDENT] * IndentSize;
460 stte->indent_len += x;
462 state_putc (' ', stte->s);
468 static void enriched_flush (struct enriched_state *stte, int wrap)
470 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
472 (stte->tag_level[RICH_INDENT_RIGHT] *
473 IndentSize) - stte->indent_len)))
474 enriched_wrap (stte);
476 if (stte->buff_used) {
477 stte->buffer[stte->buff_used] = '\0';
478 stte->line_used += stte->buff_used;
479 if (stte->line_used > stte->line_max) {
480 stte->line_max = stte->line_used;
481 p_realloc(&stte->line, stte->line_max + 1);
483 m_strcat(stte->line, stte->line_max + 1, stte->buffer);
484 stte->line_len += stte->word_len;
489 enriched_wrap (stte);
493 static void enriched_putc (int c, struct enriched_state *stte)
495 if (stte->tag_level[RICH_PARAM]) {
496 if (stte->tag_level[RICH_COLOR]) {
497 if (stte->param_used + 1 >= stte->param_len)
498 p_realloc(&stte->param, (stte->param_len += STRING));
500 stte->param[stte->param_used++] = c;
502 return; /* nothing to do */
505 /* see if more space is needed (plus extra for possible rich characters) */
506 if (stte->buff_len < stte->buff_used + 3) {
507 stte->buff_len += LONG_STRING;
508 p_realloc(&stte->buffer, stte->buff_len + 1);
511 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
513 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
517 stte->buffer[stte->buff_used++] = c;
518 enriched_flush (stte, 0);
521 if (stte->s->flags & M_DISPLAY) {
522 if (stte->tag_level[RICH_BOLD]) {
523 stte->buffer[stte->buff_used++] = c;
524 stte->buffer[stte->buff_used++] = '\010';
525 stte->buffer[stte->buff_used++] = c;
527 else if (stte->tag_level[RICH_UNDERLINE]) {
529 stte->buffer[stte->buff_used++] = '_';
530 stte->buffer[stte->buff_used++] = '\010';
531 stte->buffer[stte->buff_used++] = c;
533 else if (stte->tag_level[RICH_ITALIC]) {
534 stte->buffer[stte->buff_used++] = c;
535 stte->buffer[stte->buff_used++] = '\010';
536 stte->buffer[stte->buff_used++] = '_';
539 stte->buffer[stte->buff_used++] = c;
542 stte->buffer[stte->buff_used++] = c;
548 static void enriched_puts (const char *s, struct enriched_state *stte)
552 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
553 stte->buff_len += LONG_STRING;
554 p_realloc(&stte->buffer, stte->buff_len + 1);
559 stte->buffer[stte->buff_used++] = *p++;
563 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
565 const char *tagptr = tag;
571 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
572 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
573 j = EnrichedTags[i].index;
578 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
579 enriched_flush (stte, 1);
582 if (stte->tag_level[j]) /* make sure not to go negative */
583 stte->tag_level[j]--;
584 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
585 && stte->tag_level[RICH_COLOR]) {
586 stte->param[stte->param_used] = '\0';
587 if (!ascii_strcasecmp (stte->param, "black")) {
588 enriched_puts ("\033[30m", stte);
590 else if (!ascii_strcasecmp (stte->param, "red")) {
591 enriched_puts ("\033[31m", stte);
593 else if (!ascii_strcasecmp (stte->param, "green")) {
594 enriched_puts ("\033[32m", stte);
596 else if (!ascii_strcasecmp (stte->param, "yellow")) {
597 enriched_puts ("\033[33m", stte);
599 else if (!ascii_strcasecmp (stte->param, "blue")) {
600 enriched_puts ("\033[34m", stte);
602 else if (!ascii_strcasecmp (stte->param, "magenta")) {
603 enriched_puts ("\033[35m", stte);
605 else if (!ascii_strcasecmp (stte->param, "cyan")) {
606 enriched_puts ("\033[36m", stte);
608 else if (!ascii_strcasecmp (stte->param, "white")) {
609 enriched_puts ("\033[37m", stte);
612 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
613 enriched_puts ("\033[0m", stte);
616 /* flush parameter buffer when closing the tag */
617 if (j == RICH_PARAM) {
618 stte->param_used = 0;
619 stte->param[0] = '\0';
623 stte->tag_level[j]++;
625 if (j == RICH_EXCERPT)
626 enriched_flush (stte, 1);
630 static int text_enriched_handler (BODY * a, STATE * s)
633 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
636 long bytes = a->length;
637 struct enriched_state stte;
640 char tag[LONG_STRING + 1];
645 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
646 72) ? (COLS - 4) : 72);
647 stte.line_max = stte.WrapMargin * 4;
648 stte.line = p_new(char, stte.line_max + 1);
649 stte.param = p_new(char, STRING);
651 stte.param_len = STRING;
655 state_puts (s->prefix, s);
656 stte.indent_len += m_strlen(s->prefix);
659 while (state != DONE) {
660 if (state != ST_EOF) {
661 if (!bytes || (c = fgetc (s->fpin)) == EOF)
675 if (stte.tag_level[RICH_NOFILL]) {
676 enriched_flush (&stte, 1);
679 enriched_putc (' ', &stte);
685 enriched_putc (c, &stte);
691 enriched_putc (c, &stte);
699 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
703 enriched_set_flags (tag, &stte);
706 else if (tag_len < LONG_STRING) /* ignore overly long tags */
719 enriched_flush (&stte, 1);
728 enriched_putc ('\0', &stte);
729 enriched_flush (&stte, 1);
733 case DONE: /* not reached, but gcc complains if this is absent */
738 state_putc ('\n', s); /* add a final newline */
740 p_delete(&(stte.buffer));
741 p_delete(&(stte.line));
742 p_delete(&(stte.param));
749 #define TXTENRICHED 3
751 static int alternative_handler (BODY * a, STATE * s)
761 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
762 a->encoding == ENCUUENCODED) {
766 fstat (fileno (s->fpin), &st);
768 b->length = (long) st.st_size;
769 b->parts = mutt_parse_multipart(s->fpin,
770 parameter_getval(a->parameter, "boundary"),
772 mime_which_token(a->subtype, -1) == MIME_DIGEST);
779 /* First, search list of prefered types */
780 t = AlternativeOrderList;
781 while (t && !choice) {
783 int btlen; /* length of basetype */
784 int wild; /* do we have a wildcard to match all subtypes? */
786 c = strchr (t->data, '/');
788 wild = (c[1] == '*' && c[2] == 0);
793 btlen = m_strlen(t->data);
801 const char *bt = TYPE (b);
803 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
804 /* the basetype matches */
805 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
814 /* Next, look for an autoviewable type */
821 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
822 if (mutt_is_autoview (b, buf)) {
823 rfc1524_entry *entry = rfc1524_entry_new();
825 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
828 rfc1524_entry_delete(&entry);
834 /* Then, look for a text entry */
841 if (b->type == TYPETEXT) {
842 switch (mime_which_token(b->subtype, -1)) {
844 if (type <= TXTPLAIN) {
850 if (type <= TXTENRICHED) {
856 if (type <= TXTHTML) {
869 /* Finally, look for other possibilities */
876 if (mutt_can_decode (b))
883 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
884 fseeko (s->fpin, choice->hdr_offset, 0);
885 mutt_copy_bytes (s->fpin, s->fpout,
886 choice->offset - choice->hdr_offset);
888 mutt_body_handler (choice, s);
890 else if (s->flags & M_DISPLAY) {
891 /* didn't find anything that we could display! */
892 state_mark_attach (s);
893 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
903 /* handles message/rfc822 body parts */
904 static int message_handler (BODY * a, STATE * s)
911 off_start = ftello (s->fpin);
912 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
913 a->encoding == ENCUUENCODED) {
914 fstat (fileno (s->fpin), &st);
916 b->length = (long) st.st_size;
917 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
923 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
924 (((s->flags & M_WEED)
925 || ((s->flags & (M_DISPLAY | M_PRINTING))
926 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
927 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
931 state_puts (s->prefix, s);
932 state_putc ('\n', s);
934 rc = mutt_body_handler (b->parts, s);
937 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
938 a->encoding == ENCUUENCODED)
944 /* returns 1 if decoding the attachment will produce output */
945 int mutt_can_decode (BODY * a)
949 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
950 if (mutt_is_autoview (a, type))
951 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
952 else if (a->type == TYPETEXT)
954 else if (a->type == TYPEMESSAGE)
956 else if (a->type == TYPEMULTIPART) {
958 int tok = mime_which_token(a->subtype, -1);
960 if (tok == MIME_SIGNED || tok == MIME_ENCRYPTED)
963 for (p = a->parts; p; p = p->next) {
964 if (mutt_can_decode (p))
968 else if (a->type == TYPEAPPLICATION) {
969 if (mutt_is_application_pgp(a))
971 if (mutt_is_application_smime (a))
978 static int multipart_handler (BODY * a, STATE * s)
986 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
987 a->encoding == ENCUUENCODED) {
988 fstat (fileno (s->fpin), &st);
990 b->length = (long) st.st_size;
991 b->parts = mutt_parse_multipart(s->fpin,
992 parameter_getval(a->parameter, "boundary"),
994 mime_which_token(a->subtype, -1) == MIME_DIGEST);
999 for (p = b->parts, count = 1; p; p = p->next, count++) {
1000 if (s->flags & M_DISPLAY) {
1001 state_mark_attach (s);
1002 state_printf (s, _("[-- Attachment #%d"), count);
1003 if (p->description || p->filename || p->form_name) {
1004 state_puts (": ", s);
1005 state_puts (p->description ? p->description :
1006 p->filename ? p->filename : p->form_name, s);
1008 state_puts (" --]\n", s);
1010 mutt_pretty_size (length, sizeof (length), p->length);
1012 state_mark_attach (s);
1013 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1014 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1015 if (!option (OPTWEED)) {
1016 fseeko (s->fpin, p->hdr_offset, 0);
1017 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1020 state_putc ('\n', s);
1023 if (p->description && mutt_can_decode (p))
1024 state_printf (s, "Content-Description: %s\n", p->description);
1027 state_printf (s, "%s: \n", p->form_name);
1030 rc = mutt_body_handler (p, s);
1031 state_putc ('\n', s);
1032 if (rc || ((s->flags & M_REPLYING)
1033 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1037 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1038 a->encoding == ENCUUENCODED)
1044 static int autoview_handler (BODY * a, STATE * s)
1046 rfc1524_entry *entry = rfc1524_entry_new();
1047 char buffer[LONG_STRING];
1049 char command[LONG_STRING];
1050 char tempfile[_POSIX_PATH_MAX] = "";
1059 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1060 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1062 fname = m_strdup(a->filename);
1063 mutt_sanitize_filename (fname, 1);
1064 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1068 if (entry->command) {
1069 m_strcpy(command, sizeof(command), entry->command);
1071 /* rfc1524_expand_command returns 0 if the file is required */
1073 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1075 if (s->flags & M_DISPLAY) {
1076 state_mark_attach (s);
1077 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1078 mutt_message (_("Invoking autoview command: %s"), command);
1081 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1082 mutt_perror ("fopen");
1083 rfc1524_entry_delete(&entry);
1087 mutt_copy_bytes (s->fpin, fpin, a->length);
1091 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1096 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1097 fileno (fpin), -1, -1);
1101 mutt_perror (_("Can't create filter"));
1103 if (s->flags & M_DISPLAY) {
1104 state_mark_attach (s);
1105 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1112 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1113 state_puts (s->prefix, s);
1114 state_puts (buffer, s);
1116 /* check for data on stderr */
1117 if (fgets (buffer, sizeof (buffer), fperr)) {
1118 if (s->flags & M_DISPLAY) {
1119 state_mark_attach (s);
1120 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1123 state_puts (s->prefix, s);
1124 state_puts (buffer, s);
1125 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1126 state_puts (s->prefix, s);
1127 state_puts (buffer, s);
1131 mutt_copy_stream (fpout, s->fpout);
1132 /* Check for stderr messages */
1133 if (fgets (buffer, sizeof (buffer), fperr)) {
1134 if (s->flags & M_DISPLAY) {
1135 state_mark_attach (s);
1136 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1139 state_puts (buffer, s);
1140 mutt_copy_stream (fperr, s->fpout);
1148 mutt_wait_filter (thepid);
1152 mutt_unlink (tempfile);
1154 if (s->flags & M_DISPLAY)
1155 mutt_clear_error ();
1157 rfc1524_entry_delete(&entry);
1161 static int external_body_handler (BODY * b, STATE * s)
1163 const char *access_type;
1164 const char *expiration;
1167 access_type = parameter_getval(b->parameter, "access-type");
1169 if (s->flags & M_DISPLAY) {
1170 state_mark_attach (s);
1171 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1176 expiration = parameter_getval(b->parameter, "expiration");
1178 expire = mutt_parse_date (expiration, NULL);
1182 if (mime_which_token(access_type, -1) == MIME_X_MUTT_DELETED) {
1183 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1185 char pretty_size[10];
1187 state_mark_attach (s);
1188 state_printf (s, _("[-- This %s/%s attachment "),
1189 TYPE (b->parts), b->parts->subtype);
1190 length = parameter_getval(b->parameter, "length");
1192 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1193 strtol (length, NULL, 10));
1194 state_printf (s, _("(size %s bytes) "), pretty_size);
1196 state_puts (_("has been deleted --]\n"), s);
1199 state_mark_attach (s);
1200 state_printf (s, _("[-- on %s --]\n"), expiration);
1202 if (b->parts->filename) {
1203 state_mark_attach (s);
1204 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1207 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1208 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1212 else if (expiration && expire < time (NULL)) {
1213 if (s->flags & M_DISPLAY) {
1214 state_mark_attach (s);
1215 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1216 TYPE (b->parts), b->parts->subtype);
1217 state_attach_puts (_("[-- and the indicated external source has --]\n"
1218 "[-- expired. --]\n"), s);
1220 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1221 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1225 if (s->flags & M_DISPLAY) {
1226 state_mark_attach (s);
1228 _("[-- This %s/%s attachment is not included, --]\n"),
1229 TYPE (b->parts), b->parts->subtype);
1230 state_mark_attach (s);
1233 ("[-- and the indicated access-type %s is unsupported --]\n"),
1235 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1236 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1243 void mutt_decode_attachment (BODY * b, STATE * s)
1245 int istext = mutt_is_text_part (b);
1246 iconv_t cd = MUTT_ICONV_ERROR;
1251 if (s->flags & M_CHARCONV) {
1252 const char *charset = parameter_getval(b->parameter, "charset");
1255 charset = charset_getfirst(AssumedCharset);
1256 if (charset && Charset)
1257 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1259 if (b->file_charset)
1260 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1264 fseeko (s->fpin, b->offset, 0);
1265 switch (b->encoding) {
1266 case ENCQUOTEDPRINTABLE:
1267 mutt_decode_quoted(s, b->length,
1268 istext || mutt_is_application_pgp(b), cd);
1271 mutt_decode_base64(s, b->length,
1272 istext || mutt_is_application_pgp(b), cd);
1275 mutt_decode_uuencoded(s, b->length,
1276 istext || mutt_is_application_pgp(b), cd);
1279 mutt_decode_xbit(s, b->length,
1280 istext || mutt_is_application_pgp(b), cd);
1284 if (cd != MUTT_ICONV_ERROR)
1288 int mutt_body_handler (BODY * b, STATE * s)
1293 char tempfile[_POSIX_PATH_MAX];
1294 handler_t handler = NULL;
1296 ssize_t tmplength = 0;
1300 int oflags = s->flags;
1301 int tok = mime_which_token(b->subtype, -1);
1303 /* first determine which handler to use to process this part */
1305 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1306 if (mutt_is_autoview (b, type)) {
1307 rfc1524_entry *entry = rfc1524_entry_new();
1309 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1310 handler = autoview_handler;
1311 s->flags &= ~M_CHARCONV;
1313 rfc1524_entry_delete(&entry);
1315 else if (b->type == TYPETEXT) {
1316 if (tok == MIME_PLAIN) {
1317 /* avoid copying this part twice since removing the transfer-encoding is
1318 * the only operation needed.
1320 if (mutt_is_application_pgp (b))
1321 handler = crypt_pgp_application_pgp_handler;
1323 if (!ascii_strcasecmp("flowed", parameter_getval(b->parameter, "format")))
1324 handler = rfc3676_handler;
1328 else if (tok == MIME_ENRICHED)
1329 handler = text_enriched_handler;
1330 else /* text body type without a handler */
1333 else if (b->type == TYPEMESSAGE) {
1334 if (mutt_is_message_type (b))
1335 handler = message_handler;
1336 else if (tok == MIME_DELIVERY_STATUS)
1338 else if (tok == MIME_EXTERNAL_BODY)
1339 handler = external_body_handler;
1341 else if (b->type == TYPEMULTIPART) {
1344 if (tok == MIME_ALTERNATIVE)
1345 handler = alternative_handler;
1346 else if (tok == MIME_SIGNED) {
1347 p = parameter_getval(b->parameter, "protocol");
1350 mutt_error (_("Error: multipart/signed has no protocol."));
1352 else if (s->flags & M_VERIFY)
1353 handler = mutt_signed_handler;
1355 else if (tok == MIME_ENCRYPTED) {
1356 p = parameter_getval(b->parameter, "protocol");
1359 mutt_error(_("Error: multipart/encrypted has no protocol parameter!"));
1360 else if (tok == MIME_APPLICATION_PGP_ENCRYPTED)
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;
1374 if (plaintext || handler) {
1375 fseeko (s->fpin, b->offset, 0);
1377 /* see if we need to decode this part before processing it */
1378 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE
1379 || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) {
1380 /* text subtypes may require character set conversion even with 8bit
1382 int origType = b->type;
1383 char *savePrefix = NULL;
1386 /* decode to a tempfile, saving the original destination */
1388 s->fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
1390 mutt_error _("Unable to open temporary file!");
1393 /* decoding the attachment changes the size and offset, so save a copy
1394 * of the "real" values now, and restore them after processing
1396 tmplength = b->length;
1397 tmpoffset = b->offset;
1399 /* if we are decoding binary bodies, we don't want to prefix each
1400 * line with the prefix or else the data will get corrupted.
1402 savePrefix = s->prefix;
1410 mutt_decode_attachment (b, s);
1413 b->length = ftello (s->fpout);
1415 m_fclose(&s->fpout);
1417 /* restore final destination and substitute the tempfile for input */
1420 s->fpin = safe_fopen (tempfile, "r");
1423 /* restore the prefix */
1424 s->prefix = savePrefix;
1430 /* process the (decoded) body part */
1432 rc = handler (b, s);
1435 b->length = tmplength;
1436 b->offset = tmpoffset;
1438 /* restore the original source stream */
1443 s->flags |= M_FIRSTDONE;
1445 else if (s->flags & M_DISPLAY) {
1446 state_mark_attach (s);
1447 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1448 if (!option (OPTVIEWATTACH)) {
1450 (type, sizeof (type),
1451 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1452 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1454 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1456 fputs (" --]\n", s->fpout);
1460 s->flags = oflags | (s->flags & M_FIRSTDONE);