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 !ascii_strcasecmp("digest", a->subtype));
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 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
846 else if (!ascii_strcasecmp ("enriched", b->subtype)
847 && type <= TXTENRICHED) {
851 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
860 /* Finally, look for other possibilities */
867 if (mutt_can_decode (b))
874 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
875 fseeko (s->fpin, choice->hdr_offset, 0);
876 mutt_copy_bytes (s->fpin, s->fpout,
877 choice->offset - choice->hdr_offset);
879 mutt_body_handler (choice, s);
881 else if (s->flags & M_DISPLAY) {
882 /* didn't find anything that we could display! */
883 state_mark_attach (s);
884 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
894 /* handles message/rfc822 body parts */
895 static int message_handler (BODY * a, STATE * s)
902 off_start = ftello (s->fpin);
903 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
904 a->encoding == ENCUUENCODED) {
905 fstat (fileno (s->fpin), &st);
907 b->length = (long) st.st_size;
908 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
914 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
915 (((s->flags & M_WEED)
916 || ((s->flags & (M_DISPLAY | M_PRINTING))
917 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
918 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
922 state_puts (s->prefix, s);
923 state_putc ('\n', s);
925 rc = mutt_body_handler (b->parts, s);
928 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
929 a->encoding == ENCUUENCODED)
935 /* returns 1 if decoding the attachment will produce output */
936 int mutt_can_decode (BODY * a)
940 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
941 if (mutt_is_autoview (a, type))
942 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
943 else if (a->type == TYPETEXT)
945 else if (a->type == TYPEMESSAGE)
947 else if (a->type == TYPEMULTIPART) {
950 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
951 ascii_strcasecmp (a->subtype, "encrypted") == 0)
954 for (p = a->parts; p; p = p->next) {
955 if (mutt_can_decode (p))
960 else if (a->type == TYPEAPPLICATION) {
961 if (mutt_is_application_pgp(a))
963 if (mutt_is_application_smime (a))
970 static int multipart_handler (BODY * a, STATE * s)
978 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
979 a->encoding == ENCUUENCODED) {
980 fstat (fileno (s->fpin), &st);
982 b->length = (long) st.st_size;
983 b->parts = mutt_parse_multipart(s->fpin,
984 parameter_getval(a->parameter, "boundary"),
986 !ascii_strcasecmp("digest", a->subtype));
991 for (p = b->parts, count = 1; p; p = p->next, count++) {
992 if (s->flags & M_DISPLAY) {
993 state_mark_attach (s);
994 state_printf (s, _("[-- Attachment #%d"), count);
995 if (p->description || p->filename || p->form_name) {
996 state_puts (": ", s);
997 state_puts (p->description ? p->description :
998 p->filename ? p->filename : p->form_name, s);
1000 state_puts (" --]\n", s);
1002 mutt_pretty_size (length, sizeof (length), p->length);
1004 state_mark_attach (s);
1005 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1006 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1007 if (!option (OPTWEED)) {
1008 fseeko (s->fpin, p->hdr_offset, 0);
1009 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1012 state_putc ('\n', s);
1015 if (p->description && mutt_can_decode (p))
1016 state_printf (s, "Content-Description: %s\n", p->description);
1019 state_printf (s, "%s: \n", p->form_name);
1022 rc = mutt_body_handler (p, s);
1023 state_putc ('\n', s);
1024 if (rc || ((s->flags & M_REPLYING)
1025 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1029 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1030 a->encoding == ENCUUENCODED)
1036 static int autoview_handler (BODY * a, STATE * s)
1038 rfc1524_entry *entry = rfc1524_entry_new();
1039 char buffer[LONG_STRING];
1041 char command[LONG_STRING];
1042 char tempfile[_POSIX_PATH_MAX] = "";
1051 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1052 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1054 fname = m_strdup(a->filename);
1055 mutt_sanitize_filename (fname, 1);
1056 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1060 if (entry->command) {
1061 m_strcpy(command, sizeof(command), entry->command);
1063 /* rfc1524_expand_command returns 0 if the file is required */
1065 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1067 if (s->flags & M_DISPLAY) {
1068 state_mark_attach (s);
1069 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1070 mutt_message (_("Invoking autoview command: %s"), command);
1073 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1074 mutt_perror ("fopen");
1075 rfc1524_entry_delete(&entry);
1079 mutt_copy_bytes (s->fpin, fpin, a->length);
1083 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1088 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1089 fileno (fpin), -1, -1);
1093 mutt_perror (_("Can't create filter"));
1095 if (s->flags & M_DISPLAY) {
1096 state_mark_attach (s);
1097 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1104 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1105 state_puts (s->prefix, s);
1106 state_puts (buffer, s);
1108 /* check for data on stderr */
1109 if (fgets (buffer, sizeof (buffer), fperr)) {
1110 if (s->flags & M_DISPLAY) {
1111 state_mark_attach (s);
1112 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1115 state_puts (s->prefix, s);
1116 state_puts (buffer, s);
1117 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1118 state_puts (s->prefix, s);
1119 state_puts (buffer, s);
1123 mutt_copy_stream (fpout, s->fpout);
1124 /* Check for stderr messages */
1125 if (fgets (buffer, sizeof (buffer), fperr)) {
1126 if (s->flags & M_DISPLAY) {
1127 state_mark_attach (s);
1128 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1131 state_puts (buffer, s);
1132 mutt_copy_stream (fperr, s->fpout);
1140 mutt_wait_filter (thepid);
1144 mutt_unlink (tempfile);
1146 if (s->flags & M_DISPLAY)
1147 mutt_clear_error ();
1149 rfc1524_entry_delete(&entry);
1153 static int external_body_handler (BODY * b, STATE * s)
1155 const char *access_type;
1156 const char *expiration;
1159 access_type = parameter_getval(b->parameter, "access-type");
1161 if (s->flags & M_DISPLAY) {
1162 state_mark_attach (s);
1163 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1168 expiration = parameter_getval(b->parameter, "expiration");
1170 expire = mutt_parse_date (expiration, NULL);
1174 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1175 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1177 char pretty_size[10];
1179 state_mark_attach (s);
1180 state_printf (s, _("[-- This %s/%s attachment "),
1181 TYPE (b->parts), b->parts->subtype);
1182 length = parameter_getval(b->parameter, "length");
1184 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1185 strtol (length, NULL, 10));
1186 state_printf (s, _("(size %s bytes) "), pretty_size);
1188 state_puts (_("has been deleted --]\n"), s);
1191 state_mark_attach (s);
1192 state_printf (s, _("[-- on %s --]\n"), expiration);
1194 if (b->parts->filename) {
1195 state_mark_attach (s);
1196 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1199 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1200 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1204 else if (expiration && expire < time (NULL)) {
1205 if (s->flags & M_DISPLAY) {
1206 state_mark_attach (s);
1207 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1208 TYPE (b->parts), b->parts->subtype);
1209 state_attach_puts (_("[-- and the indicated external source has --]\n"
1210 "[-- expired. --]\n"), s);
1212 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1213 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1217 if (s->flags & M_DISPLAY) {
1218 state_mark_attach (s);
1220 _("[-- This %s/%s attachment is not included, --]\n"),
1221 TYPE (b->parts), b->parts->subtype);
1222 state_mark_attach (s);
1225 ("[-- and the indicated access-type %s is unsupported --]\n"),
1227 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1228 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1235 void mutt_decode_attachment (BODY * b, STATE * s)
1237 int istext = mutt_is_text_part (b);
1238 iconv_t cd = MUTT_ICONV_ERROR;
1243 if (s->flags & M_CHARCONV) {
1244 const char *charset = parameter_getval(b->parameter, "charset");
1247 charset = charset_getfirst(AssumedCharset);
1248 if (charset && Charset)
1249 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1251 if (b->file_charset)
1252 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1256 fseeko (s->fpin, b->offset, 0);
1257 switch (b->encoding) {
1258 case ENCQUOTEDPRINTABLE:
1259 mutt_decode_quoted(s, b->length,
1260 istext || mutt_is_application_pgp(b), cd);
1263 mutt_decode_base64(s, b->length,
1264 istext || mutt_is_application_pgp(b), cd);
1267 mutt_decode_uuencoded(s, b->length,
1268 istext || mutt_is_application_pgp(b), cd);
1271 mutt_decode_xbit(s, b->length,
1272 istext || mutt_is_application_pgp(b), cd);
1276 if (cd != MUTT_ICONV_ERROR)
1280 int mutt_body_handler (BODY * b, STATE * s)
1285 char tempfile[_POSIX_PATH_MAX];
1286 handler_t handler = NULL;
1288 ssize_t tmplength = 0;
1292 int oflags = s->flags;
1294 /* first determine which handler to use to process this part */
1296 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1297 if (mutt_is_autoview (b, type)) {
1298 rfc1524_entry *entry = rfc1524_entry_new();
1300 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1301 handler = autoview_handler;
1302 s->flags &= ~M_CHARCONV;
1304 rfc1524_entry_delete(&entry);
1306 else if (b->type == TYPETEXT) {
1307 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1308 /* avoid copying this part twice since removing the transfer-encoding is
1309 * the only operation needed.
1311 if (mutt_is_application_pgp (b))
1312 handler = crypt_pgp_application_pgp_handler;
1314 if (!ascii_strcasecmp("flowed", parameter_getval(b->parameter, "format")))
1315 handler = rfc3676_handler;
1319 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1320 handler = text_enriched_handler;
1321 else /* text body type without a handler */
1324 else if (b->type == TYPEMESSAGE) {
1325 if (mutt_is_message_type (b))
1326 handler = message_handler;
1327 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1329 else if (!ascii_strcasecmp ("external-body", b->subtype))
1330 handler = external_body_handler;
1332 else if (b->type == TYPEMULTIPART) {
1335 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1336 handler = alternative_handler;
1337 else if (ascii_strcasecmp ("signed", b->subtype) == 0) {
1338 p = parameter_getval(b->parameter, "protocol");
1341 mutt_error (_("Error: multipart/signed has no protocol."));
1343 else if (s->flags & M_VERIFY)
1344 handler = mutt_signed_handler;
1346 else if (m_strcasecmp("encrypted", b->subtype) == 0) {
1347 p = parameter_getval(b->parameter, "protocol");
1351 ("Error: multipart/encrypted has no protocol parameter!"));
1353 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1354 handler = crypt_pgp_encrypted_handler;
1358 handler = multipart_handler;
1360 else if (b->type == TYPEAPPLICATION) {
1361 if (mutt_is_application_pgp (b))
1362 handler = crypt_pgp_application_pgp_handler;
1363 if (mutt_is_application_smime (b))
1364 handler = crypt_smime_application_smime_handler;
1368 if (plaintext || handler) {
1369 fseeko (s->fpin, b->offset, 0);
1371 /* see if we need to decode this part before processing it */
1372 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE
1373 || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) {
1374 /* text subtypes may require character set conversion even with 8bit
1376 int origType = b->type;
1377 char *savePrefix = NULL;
1380 /* decode to a tempfile, saving the original destination */
1382 s->fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
1384 mutt_error _("Unable to open temporary file!");
1387 /* decoding the attachment changes the size and offset, so save a copy
1388 * of the "real" values now, and restore them after processing
1390 tmplength = b->length;
1391 tmpoffset = b->offset;
1393 /* if we are decoding binary bodies, we don't want to prefix each
1394 * line with the prefix or else the data will get corrupted.
1396 savePrefix = s->prefix;
1404 mutt_decode_attachment (b, s);
1407 b->length = ftello (s->fpout);
1409 m_fclose(&s->fpout);
1411 /* restore final destination and substitute the tempfile for input */
1414 s->fpin = safe_fopen (tempfile, "r");
1417 /* restore the prefix */
1418 s->prefix = savePrefix;
1424 /* process the (decoded) body part */
1426 rc = handler (b, s);
1429 b->length = tmplength;
1430 b->offset = tmpoffset;
1432 /* restore the original source stream */
1437 s->flags |= M_FIRSTDONE;
1439 else if (s->flags & M_DISPLAY) {
1440 state_mark_attach (s);
1441 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1442 if (!option (OPTVIEWATTACH)) {
1444 (type, sizeof (type),
1445 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1446 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1448 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1450 fputs (" --]\n", s->fpout);
1454 s->flags = oflags | (s->flags & M_FIRSTDONE);