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/lib-ui.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];
642 stte.WrapMargin = ((s->flags & M_DISPLAY) ? (getmaxx(main_w) - 4) :
643 ((getmaxx(main_w) - 4) < 72) ? (getmaxx(main_w) - 4) : 72);
644 stte.line_max = stte.WrapMargin * 4;
645 stte.line = p_new(char, stte.line_max + 1);
646 stte.param = p_new(char, STRING);
648 stte.param_len = STRING;
652 state_puts (s->prefix, s);
653 stte.indent_len += m_strlen(s->prefix);
656 while (state != DONE) {
657 if (state != ST_EOF) {
658 if (!bytes || (c = fgetc (s->fpin)) == EOF)
672 if (stte.tag_level[RICH_NOFILL]) {
673 enriched_flush (&stte, 1);
676 enriched_putc (' ', &stte);
682 enriched_putc (c, &stte);
688 enriched_putc (c, &stte);
696 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
700 enriched_set_flags (tag, &stte);
703 else if (tag_len < LONG_STRING) /* ignore overly long tags */
716 enriched_flush (&stte, 1);
725 enriched_putc ('\0', &stte);
726 enriched_flush (&stte, 1);
730 case DONE: /* not reached, but gcc complains if this is absent */
735 state_putc ('\n', s); /* add a final newline */
737 p_delete(&(stte.buffer));
738 p_delete(&(stte.line));
739 p_delete(&(stte.param));
746 #define TXTENRICHED 3
748 static int alternative_handler (BODY * a, STATE * s)
758 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
759 a->encoding == ENCUUENCODED) {
763 fstat (fileno (s->fpin), &st);
765 b->length = (long) st.st_size;
766 b->parts = mutt_parse_multipart(s->fpin,
767 parameter_getval(a->parameter, "boundary"),
769 mime_which_token(a->subtype, -1) == MIME_DIGEST);
776 /* First, search list of prefered types */
777 t = AlternativeOrderList;
778 while (t && !choice) {
780 int btlen; /* length of basetype */
781 int wild; /* do we have a wildcard to match all subtypes? */
783 c = strchr (t->data, '/');
785 wild = (c[1] == '*' && c[2] == 0);
790 btlen = m_strlen(t->data);
798 const char *bt = TYPE (b);
800 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
801 /* the basetype matches */
802 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
811 /* Next, look for an autoviewable type */
818 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
819 if (mutt_is_autoview (b, buf)) {
820 rfc1524_entry *entry = rfc1524_entry_new();
822 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
825 rfc1524_entry_delete(&entry);
831 /* Then, look for a text entry */
838 if (b->type == TYPETEXT) {
839 switch (mime_which_token(b->subtype, -1)) {
841 if (type <= TXTPLAIN) {
847 if (type <= TXTENRICHED) {
853 if (type <= TXTHTML) {
866 /* Finally, look for other possibilities */
873 if (mutt_can_decode (b))
880 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
881 fseeko (s->fpin, choice->hdr_offset, 0);
882 mutt_copy_bytes (s->fpin, s->fpout,
883 choice->offset - choice->hdr_offset);
885 mutt_body_handler (choice, s);
887 else if (s->flags & M_DISPLAY) {
888 /* didn't find anything that we could display! */
889 state_mark_attach (s);
890 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
900 /* handles message/rfc822 body parts */
901 static int message_handler (BODY * a, STATE * s)
908 off_start = ftello (s->fpin);
909 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
910 a->encoding == ENCUUENCODED) {
911 fstat (fileno (s->fpin), &st);
913 b->length = (long) st.st_size;
914 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
920 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
921 (((s->flags & M_WEED)
922 || ((s->flags & (M_DISPLAY | M_PRINTING))
923 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
924 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
928 state_puts (s->prefix, s);
929 state_putc ('\n', s);
931 rc = mutt_body_handler (b->parts, s);
934 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
935 a->encoding == ENCUUENCODED)
941 /* returns 1 if decoding the attachment will produce output */
942 int mutt_can_decode (BODY * a)
946 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
947 if (mutt_is_autoview (a, type))
948 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
949 else if (a->type == TYPETEXT)
951 else if (a->type == TYPEMESSAGE)
953 else if (a->type == TYPEMULTIPART) {
955 int tok = mime_which_token(a->subtype, -1);
957 if (tok == MIME_SIGNED || tok == MIME_ENCRYPTED)
960 for (p = a->parts; p; p = p->next) {
961 if (mutt_can_decode (p))
965 else if (a->type == TYPEAPPLICATION) {
966 if (mutt_is_application_pgp(a))
968 if (mutt_is_application_smime (a))
975 static int multipart_handler (BODY * a, STATE * s)
983 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
984 a->encoding == ENCUUENCODED) {
985 fstat (fileno (s->fpin), &st);
987 b->length = (long) st.st_size;
988 b->parts = mutt_parse_multipart(s->fpin,
989 parameter_getval(a->parameter, "boundary"),
991 mime_which_token(a->subtype, -1) == MIME_DIGEST);
996 for (p = b->parts, count = 1; p; p = p->next, count++) {
997 if (s->flags & M_DISPLAY) {
998 state_mark_attach (s);
999 state_printf (s, _("[-- Attachment #%d"), count);
1000 if (p->description || p->filename || p->form_name) {
1001 state_puts (": ", s);
1002 state_puts (p->description ? p->description :
1003 p->filename ? p->filename : p->form_name, s);
1005 state_puts (" --]\n", s);
1007 mutt_pretty_size (length, sizeof (length), p->length);
1009 state_mark_attach (s);
1010 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1011 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1012 if (!option (OPTWEED)) {
1013 fseeko (s->fpin, p->hdr_offset, 0);
1014 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1017 state_putc ('\n', s);
1020 if (p->description && mutt_can_decode (p))
1021 state_printf (s, "Content-Description: %s\n", p->description);
1024 state_printf (s, "%s: \n", p->form_name);
1027 rc = mutt_body_handler (p, s);
1028 state_putc ('\n', s);
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);
1092 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1093 fileno (fpin), -1, -1);
1097 mutt_perror (_("Can't create filter"));
1099 if (s->flags & M_DISPLAY) {
1100 state_mark_attach (s);
1101 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1108 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1109 state_puts (s->prefix, s);
1110 state_puts (buffer, s);
1112 /* check for data on stderr */
1113 if (fgets (buffer, sizeof (buffer), fperr)) {
1114 if (s->flags & M_DISPLAY) {
1115 state_mark_attach (s);
1116 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1119 state_puts (s->prefix, s);
1120 state_puts (buffer, s);
1121 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1122 state_puts (s->prefix, s);
1123 state_puts (buffer, s);
1127 mutt_copy_stream (fpout, s->fpout);
1128 /* Check for stderr messages */
1129 if (fgets (buffer, sizeof (buffer), fperr)) {
1130 if (s->flags & M_DISPLAY) {
1131 state_mark_attach (s);
1132 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1135 state_puts (buffer, s);
1136 mutt_copy_stream (fperr, s->fpout);
1144 mutt_wait_filter (thepid);
1148 mutt_unlink (tempfile);
1150 if (s->flags & M_DISPLAY)
1151 mutt_clear_error ();
1153 rfc1524_entry_delete(&entry);
1157 static int external_body_handler (BODY * b, STATE * s)
1159 const char *access_type;
1160 const char *expiration;
1163 access_type = parameter_getval(b->parameter, "access-type");
1165 if (s->flags & M_DISPLAY) {
1166 state_mark_attach (s);
1167 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1172 expiration = parameter_getval(b->parameter, "expiration");
1174 expire = mutt_parse_date (expiration, NULL);
1178 if (mime_which_token(access_type, -1) == MIME_X_MUTT_DELETED) {
1179 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1181 char pretty_size[10];
1183 state_mark_attach (s);
1184 state_printf (s, _("[-- This %s/%s attachment "),
1185 TYPE (b->parts), b->parts->subtype);
1186 length = parameter_getval(b->parameter, "length");
1188 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1189 strtol (length, NULL, 10));
1190 state_printf (s, _("(size %s bytes) "), pretty_size);
1192 state_puts (_("has been deleted --]\n"), s);
1195 state_mark_attach (s);
1196 state_printf (s, _("[-- on %s --]\n"), expiration);
1198 if (b->parts->filename) {
1199 state_mark_attach (s);
1200 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1203 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1204 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1208 else if (expiration && expire < time (NULL)) {
1209 if (s->flags & M_DISPLAY) {
1210 state_mark_attach (s);
1211 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1212 TYPE (b->parts), b->parts->subtype);
1213 state_attach_puts (_("[-- and the indicated external source has --]\n"
1214 "[-- expired. --]\n"), s);
1216 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1217 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1221 if (s->flags & M_DISPLAY) {
1222 state_mark_attach (s);
1224 _("[-- This %s/%s attachment is not included, --]\n"),
1225 TYPE (b->parts), b->parts->subtype);
1226 state_mark_attach (s);
1229 ("[-- and the indicated access-type %s is unsupported --]\n"),
1231 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1232 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1239 void mutt_decode_attachment (BODY * b, STATE * s)
1241 int istext = mutt_is_text_part (b);
1242 iconv_t cd = MUTT_ICONV_ERROR;
1247 if (s->flags & M_CHARCONV) {
1248 const char *charset = parameter_getval(b->parameter, "charset");
1251 charset = charset_getfirst(mod_cset.assumed_charset);
1252 if (charset && mod_cset.charset)
1253 cd = mutt_iconv_open (mod_cset.charset, charset, M_ICONV_HOOK_FROM);
1255 if (b->file_charset)
1256 cd = mutt_iconv_open (mod_cset.charset, b->file_charset, M_ICONV_HOOK_FROM);
1260 fseeko (s->fpin, b->offset, 0);
1261 switch (b->encoding) {
1262 case ENCQUOTEDPRINTABLE:
1263 mutt_decode_quoted(s, b->length,
1264 istext || mutt_is_application_pgp(b), cd);
1267 mutt_decode_base64(s, b->length,
1268 istext || mutt_is_application_pgp(b), cd);
1271 mutt_decode_uuencoded(s, b->length,
1272 istext || mutt_is_application_pgp(b), cd);
1275 mutt_decode_xbit(s, b->length,
1276 istext || mutt_is_application_pgp(b), cd);
1280 if (cd != MUTT_ICONV_ERROR)
1284 int mutt_body_handler (BODY * b, STATE * s)
1289 char tempfile[_POSIX_PATH_MAX];
1290 handler_t handler = NULL;
1292 ssize_t tmplength = 0;
1296 int oflags = s->flags;
1297 int tok = mime_which_token(b->subtype, -1);
1299 /* first determine which handler to use to process this part */
1301 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1302 if (mutt_is_autoview (b, type)) {
1303 rfc1524_entry *entry = rfc1524_entry_new();
1305 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1306 handler = autoview_handler;
1307 s->flags &= ~M_CHARCONV;
1309 rfc1524_entry_delete(&entry);
1311 else if (b->type == TYPETEXT) {
1312 if (tok == MIME_PLAIN) {
1313 /* avoid copying this part twice since removing the transfer-encoding is
1314 * the only operation needed.
1316 if (mutt_is_application_pgp (b))
1317 handler = crypt_pgp_application_pgp_handler;
1319 if (!ascii_strcasecmp("flowed", parameter_getval(b->parameter, "format")))
1320 handler = rfc3676_handler;
1324 else if (tok == MIME_ENRICHED)
1325 handler = text_enriched_handler;
1326 else /* text body type without a handler */
1329 else if (b->type == TYPEMESSAGE) {
1330 if (mutt_is_message_type (b))
1331 handler = message_handler;
1332 else if (tok == MIME_DELIVERY_STATUS)
1334 else if (tok == MIME_EXTERNAL_BODY)
1335 handler = external_body_handler;
1337 else if (b->type == TYPEMULTIPART) {
1340 if (tok == MIME_ALTERNATIVE)
1341 handler = alternative_handler;
1342 else if (tok == MIME_SIGNED) {
1343 p = parameter_getval(b->parameter, "protocol");
1346 mutt_error (_("Error: multipart/signed has no protocol."));
1348 else if (s->flags & M_VERIFY)
1349 handler = mutt_signed_handler;
1351 else if (tok == MIME_ENCRYPTED) {
1352 p = parameter_getval(b->parameter, "protocol");
1355 mutt_error(_("Error: multipart/encrypted has no protocol parameter!"));
1356 else if (mime_which_token(p, -1) == MIME_APPLICATION_PGP_ENCRYPTED)
1357 handler = crypt_pgp_encrypted_handler;
1361 handler = multipart_handler;
1363 else if (b->type == TYPEAPPLICATION) {
1364 if (mutt_is_application_pgp (b))
1365 handler = crypt_pgp_application_pgp_handler;
1366 if (mutt_is_application_smime (b))
1367 handler = crypt_smime_application_smime_handler;
1370 if (plaintext || handler) {
1371 fseeko (s->fpin, b->offset, 0);
1373 /* see if we need to decode this part before processing it */
1374 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE
1375 || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) {
1376 /* text subtypes may require character set conversion even with 8bit
1378 int origType = b->type;
1379 char *savePrefix = NULL;
1382 /* decode to a tempfile, saving the original destination */
1384 s->fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
1386 mutt_error _("Unable to open temporary file!");
1389 /* decoding the attachment changes the size and offset, so save a copy
1390 * of the "real" values now, and restore them after processing
1392 tmplength = b->length;
1393 tmpoffset = b->offset;
1395 /* if we are decoding binary bodies, we don't want to prefix each
1396 * line with the prefix or else the data will get corrupted.
1398 savePrefix = s->prefix;
1406 mutt_decode_attachment (b, s);
1409 b->length = ftello (s->fpout);
1411 m_fclose(&s->fpout);
1413 /* restore final destination and substitute the tempfile for input */
1416 s->fpin = safe_fopen (tempfile, "r");
1419 /* restore the prefix */
1420 s->prefix = savePrefix;
1426 /* process the (decoded) body part */
1428 rc = handler (b, s);
1431 b->length = tmplength;
1432 b->offset = tmpoffset;
1434 /* restore the original source stream */
1440 else if (s->flags & M_DISPLAY) {
1441 state_mark_attach (s);
1442 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1443 if (!option (OPTVIEWATTACH)) {
1445 (type, sizeof (type),
1446 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1447 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1449 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1451 fputs (" --]\n", s->fpout);