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.
21 #include <lib-lib/mem.h>
22 #include <lib-lib/ascii.h>
23 #include <lib-lib/str.h>
24 #include <lib-lib/macros.h>
25 #include <lib-lib/file.h>
27 #include <lib-mime/mime.h>
28 #include <lib-mime/rfc3676.h>
30 #include <lib-ui/curses.h>
33 #include "recvattach.h"
38 #include <lib-crypt/crypt.h>
44 typedef int handler_f (BODY *, STATE *);
45 typedef handler_f *handler_t;
47 static void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
56 while ((c = fgetc (s->fpin)) != EOF && len--) {
57 if (c == '\r' && len) {
58 if ((ch = fgetc (s->fpin)) == '\n') {
67 if (l == sizeof (bufi))
68 mutt_convert_to_state (cd, bufi, &l, s);
71 mutt_convert_to_state (cd, bufi, &l, s);
72 mutt_convert_to_state (cd, 0, 0, s);
74 state_reset_prefix (s);
77 mutt_copy_bytes (s->fpin, s->fpout, len);
80 static int qp_decode_triple (char *s, char *d)
83 if (*s == '=' && !(*(s + 1)))
86 /* quoted-printable triple */
88 isxdigit ((unsigned char) *(s + 1)) &&
89 isxdigit ((unsigned char) *(s + 2))) {
90 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
98 static void qp_decode_line (char *dest, char *src, ssize_t * l, int last)
106 /* decode the line */
108 for (d = dest, s = src; *s;) {
109 switch ((kind = qp_decode_triple (s, &c))) {
113 break; /* qp triple */
116 break; /* single character */
120 break; /* soft line break */
124 if (!soft && last == '\n')
132 * Decode an attachment encoded with quoted-printable.
134 * Why doesn't this overflow any buffers? First, it's guaranteed
135 * that the length of a line grows when you _en_-code it to
136 * quoted-printable. That means that we always can store the
137 * result in a buffer of at most the _same_ size.
139 * Now, we don't special-case if the line we read with fgets()
140 * isn't terminated. We don't care about this, since STRING > 78,
141 * so corrupted input will just be corrupted a bit more. That
142 * implies that STRING+1 bytes are always sufficient to store the
143 * result of qp_decode_line.
145 * Finally, at soft line breaks, some part of a multibyte character
146 * may have been left over by mutt_convert_to_state(). This shouldn't
147 * be more than 6 characters, so STRING + 7 should be sufficient
148 * memory to store the decoded data.
150 * Just to make sure that I didn't make some off-by-one error
151 * above, we just use STRING*2 for the target buffer's size.
155 void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
158 char decline[2 * STRING];
160 ssize_t linelen; /* number of input bytes in `line' */
163 int last; /* store the last character in the input line */
166 state_set_prefix (s);
172 * It's ok to use a fixed size buffer for input, even if the line turns
173 * out to be longer than this. Just process the line in chunks. This
174 * really shouldn't happen according the MIME spec, since Q-P encoded
175 * lines are at most 76 characters, but we should be liberal about what
178 if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
181 linelen = m_strlen(line);
185 * inspect the last character we read so we can tell if we got the
188 last = linelen ? line[linelen - 1] : 0;
190 /* chop trailing whitespace if we got the full line */
192 while (linelen > 0 && ISSPACE (line[linelen - 1]))
197 /* decode and do character set conversion */
198 qp_decode_line (decline + l, line, &l3, last);
200 mutt_convert_to_state (cd, decline, &l, s);
203 mutt_convert_to_state (cd, 0, 0, s);
204 state_reset_prefix (s);
207 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
210 int c1, c2, c3, c4, ch, cr = 0, i;
211 char bufi[BUFI_SIZE];
217 state_set_prefix (s);
220 for (i = 0; i < 4 && len > 0; len--) {
221 if ((ch = fgetc (s->fpin)) == EOF)
223 if (base64val(ch) >= 0 || ch == '=')
230 c1 = base64val(buf[0]);
231 c2 = base64val(buf[1]);
232 ch = (c1 << 2) | (c2 >> 4);
234 if (cr && ch != '\n')
239 if (istext && ch == '\r')
246 c3 = base64val(buf[2]);
247 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
249 if (cr && ch != '\n')
254 if (istext && ch == '\r')
261 c4 = base64val(buf[3]);
262 ch = ((c3 & 0x3) << 6) | c4;
264 if (cr && ch != '\n')
268 if (istext && ch == '\r')
273 if (l + 8 >= ssizeof (bufi))
274 mutt_convert_to_state (cd, bufi, &l, s);
280 mutt_convert_to_state (cd, bufi, &l, s);
281 mutt_convert_to_state (cd, 0, 0, s);
283 state_reset_prefix (s);
286 unsigned char decode_byte (char ch)
293 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
295 char tmps[SHORT_STRING];
296 char linelen, c, l, out;
298 char bufi[BUFI_SIZE];
302 state_set_prefix (s);
305 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
307 len -= m_strlen(tmps);
308 if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
312 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
314 len -= m_strlen(tmps);
315 if (!m_strncmp(tmps, "end", 3))
318 linelen = decode_byte (*pt);
320 for (c = 0; c < linelen;) {
321 for (l = 2; l <= 6; l += 2) {
322 out = decode_byte (*pt) << l;
324 out |= (decode_byte (*pt) >> (6 - l));
330 mutt_convert_to_state (cd, bufi, &k, s);
335 mutt_convert_to_state (cd, bufi, &k, s);
336 mutt_convert_to_state (cd, 0, 0, s);
338 state_reset_prefix (s);
341 /* ----------------------------------------------------------------------------
342 * A (not so) minimal implementation of RFC1563.
345 #define IndentSize (4)
347 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
348 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
349 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
353 const char *tag_name;
357 "param", RICH_PARAM}, {
358 "bold", RICH_BOLD}, {
359 "italic", RICH_ITALIC}, {
360 "underline", RICH_UNDERLINE}, {
361 "nofill", RICH_NOFILL}, {
362 "excerpt", RICH_EXCERPT}, {
363 "indent", RICH_INDENT}, {
364 "indentright", RICH_INDENT_RIGHT}, {
365 "center", RICH_CENTER}, {
366 "flushleft", RICH_FLUSHLEFT}, {
367 "flushright", RICH_FLUSHRIGHT}, {
368 "flushboth", RICH_FLUSHLEFT}, {
369 "color", RICH_COLOR}, {
370 "x-color", RICH_COLOR}, {
374 struct enriched_state {
387 int tag_level[RICH_LAST_TAG];
392 static void enriched_wrap (struct enriched_state *stte)
397 if (stte->line_len) {
398 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
399 /* Strip trailing white space */
400 ssize_t y = stte->line_used - 1;
402 while (y && ISSPACE (stte->line[y])) {
403 stte->line[y] = '\0';
408 if (stte->tag_level[RICH_CENTER]) {
409 /* Strip leading whitespace */
412 while (stte->line[y] && ISSPACE (stte->line[y]))
417 for (z = y; z <= stte->line_used; z++) {
418 stte->line[z - y] = stte->line[z];
422 stte->line_used -= y;
427 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
428 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
430 if (stte->tag_level[RICH_CENTER]) {
433 state_putc (' ', stte->s);
437 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
440 state_putc (' ', stte->s);
445 state_puts (stte->line, stte->s);
448 state_putc ('\n', stte->s);
449 stte->line[0] = '\0';
452 stte->indent_len = 0;
453 if (stte->s->prefix) {
454 state_puts (stte->s->prefix, stte->s);
455 stte->indent_len += m_strlen(stte->s->prefix);
458 if (stte->tag_level[RICH_EXCERPT]) {
459 x = stte->tag_level[RICH_EXCERPT];
461 if (stte->s->prefix) {
462 state_puts (stte->s->prefix, stte->s);
463 stte->indent_len += m_strlen(stte->s->prefix);
466 state_puts ("> ", stte->s);
467 stte->indent_len += m_strlen("> ");
473 stte->indent_len = 0;
474 if (stte->tag_level[RICH_INDENT]) {
475 x = stte->tag_level[RICH_INDENT] * IndentSize;
476 stte->indent_len += x;
478 state_putc (' ', stte->s);
484 static void enriched_flush (struct enriched_state *stte, int wrap)
486 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
488 (stte->tag_level[RICH_INDENT_RIGHT] *
489 IndentSize) - stte->indent_len)))
490 enriched_wrap (stte);
492 if (stte->buff_used) {
493 stte->buffer[stte->buff_used] = '\0';
494 stte->line_used += stte->buff_used;
495 if (stte->line_used > stte->line_max) {
496 stte->line_max = stte->line_used;
497 p_realloc(&stte->line, stte->line_max + 1);
499 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
500 stte->line_len += stte->word_len;
505 enriched_wrap (stte);
509 static void enriched_putc (int c, struct enriched_state *stte)
511 if (stte->tag_level[RICH_PARAM]) {
512 if (stte->tag_level[RICH_COLOR]) {
513 if (stte->param_used + 1 >= stte->param_len)
514 p_realloc(&stte->param, (stte->param_len += STRING));
516 stte->param[stte->param_used++] = c;
518 return; /* nothing to do */
521 /* see if more space is needed (plus extra for possible rich characters) */
522 if (stte->buff_len < stte->buff_used + 3) {
523 stte->buff_len += LONG_STRING;
524 p_realloc(&stte->buffer, stte->buff_len + 1);
527 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
529 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
533 stte->buffer[stte->buff_used++] = c;
534 enriched_flush (stte, 0);
537 if (stte->s->flags & M_DISPLAY) {
538 if (stte->tag_level[RICH_BOLD]) {
539 stte->buffer[stte->buff_used++] = c;
540 stte->buffer[stte->buff_used++] = '\010';
541 stte->buffer[stte->buff_used++] = c;
543 else if (stte->tag_level[RICH_UNDERLINE]) {
545 stte->buffer[stte->buff_used++] = '_';
546 stte->buffer[stte->buff_used++] = '\010';
547 stte->buffer[stte->buff_used++] = c;
549 else if (stte->tag_level[RICH_ITALIC]) {
550 stte->buffer[stte->buff_used++] = c;
551 stte->buffer[stte->buff_used++] = '\010';
552 stte->buffer[stte->buff_used++] = '_';
555 stte->buffer[stte->buff_used++] = c;
559 stte->buffer[stte->buff_used++] = c;
565 static void enriched_puts (const char *s, struct enriched_state *stte)
569 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
570 stte->buff_len += LONG_STRING;
571 p_realloc(&stte->buffer, stte->buff_len + 1);
576 stte->buffer[stte->buff_used++] = *p++;
580 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
582 const char *tagptr = tag;
588 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
589 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
590 j = EnrichedTags[i].index;
595 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
596 enriched_flush (stte, 1);
599 if (stte->tag_level[j]) /* make sure not to go negative */
600 stte->tag_level[j]--;
601 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
602 && stte->tag_level[RICH_COLOR]) {
603 stte->param[stte->param_used] = '\0';
604 if (!ascii_strcasecmp (stte->param, "black")) {
605 enriched_puts ("\033[30m", stte);
607 else if (!ascii_strcasecmp (stte->param, "red")) {
608 enriched_puts ("\033[31m", stte);
610 else if (!ascii_strcasecmp (stte->param, "green")) {
611 enriched_puts ("\033[32m", stte);
613 else if (!ascii_strcasecmp (stte->param, "yellow")) {
614 enriched_puts ("\033[33m", stte);
616 else if (!ascii_strcasecmp (stte->param, "blue")) {
617 enriched_puts ("\033[34m", stte);
619 else if (!ascii_strcasecmp (stte->param, "magenta")) {
620 enriched_puts ("\033[35m", stte);
622 else if (!ascii_strcasecmp (stte->param, "cyan")) {
623 enriched_puts ("\033[36m", stte);
625 else if (!ascii_strcasecmp (stte->param, "white")) {
626 enriched_puts ("\033[37m", stte);
629 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
630 enriched_puts ("\033[0m", stte);
633 /* flush parameter buffer when closing the tag */
634 if (j == RICH_PARAM) {
635 stte->param_used = 0;
636 stte->param[0] = '\0';
640 stte->tag_level[j]++;
642 if (j == RICH_EXCERPT)
643 enriched_flush (stte, 1);
647 int text_enriched_handler (BODY * a, STATE * s)
650 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
653 long bytes = a->length;
654 struct enriched_state stte;
657 char tag[LONG_STRING + 1];
662 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
663 72) ? (COLS - 4) : 72);
664 stte.line_max = stte.WrapMargin * 4;
665 stte.line = p_new(char, stte.line_max + 1);
666 stte.param = p_new(char, STRING);
668 stte.param_len = STRING;
672 state_puts (s->prefix, s);
673 stte.indent_len += m_strlen(s->prefix);
676 while (state != DONE) {
677 if (state != ST_EOF) {
678 if (!bytes || (c = fgetc (s->fpin)) == EOF)
692 if (stte.tag_level[RICH_NOFILL]) {
693 enriched_flush (&stte, 1);
696 enriched_putc (' ', &stte);
702 enriched_putc (c, &stte);
708 enriched_putc (c, &stte);
716 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
720 enriched_set_flags (tag, &stte);
723 else if (tag_len < LONG_STRING) /* ignore overly long tags */
736 enriched_flush (&stte, 1);
745 enriched_putc ('\0', &stte);
746 enriched_flush (&stte, 1);
750 case DONE: /* not reached, but gcc complains if this is absent */
755 state_putc ('\n', s); /* add a final newline */
757 p_delete(&(stte.buffer));
758 p_delete(&(stte.line));
759 p_delete(&(stte.param));
766 #define TXTENRICHED 3
768 static int alternative_handler (BODY * a, STATE * s)
778 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
779 a->encoding == ENCUUENCODED) {
783 fstat (fileno (s->fpin), &st);
785 b->length = (long) st.st_size;
786 b->parts = mutt_parse_multipart(s->fpin,
787 parameter_getval(a->parameter, "boundary"),
789 !ascii_strcasecmp("digest", a->subtype));
796 /* First, search list of prefered types */
797 t = AlternativeOrderList;
798 while (t && !choice) {
800 int btlen; /* length of basetype */
801 int wild; /* do we have a wildcard to match all subtypes? */
803 c = strchr (t->data, '/');
805 wild = (c[1] == '*' && c[2] == 0);
810 btlen = m_strlen(t->data);
818 const char *bt = TYPE (b);
820 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
821 /* the basetype matches */
822 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
831 /* Next, look for an autoviewable type */
838 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
839 if (mutt_is_autoview (b, buf)) {
840 rfc1524_entry *entry = rfc1524_entry_new();
842 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
845 rfc1524_entry_delete(&entry);
851 /* Then, look for a text entry */
858 if (b->type == TYPETEXT) {
859 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
863 else if (!ascii_strcasecmp ("enriched", b->subtype)
864 && type <= TXTENRICHED) {
868 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
877 /* Finally, look for other possibilities */
884 if (mutt_can_decode (b))
891 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
892 fseeko (s->fpin, choice->hdr_offset, 0);
893 mutt_copy_bytes (s->fpin, s->fpout,
894 choice->offset - choice->hdr_offset);
896 mutt_body_handler (choice, s);
898 else if (s->flags & M_DISPLAY) {
899 /* didn't find anything that we could display! */
900 state_mark_attach (s);
901 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
911 /* handles message/rfc822 body parts */
912 static int message_handler (BODY * a, STATE * s)
919 off_start = ftello (s->fpin);
920 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
921 a->encoding == ENCUUENCODED) {
922 fstat (fileno (s->fpin), &st);
924 b->length = (long) st.st_size;
925 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
931 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
932 (((s->flags & M_WEED)
933 || ((s->flags & (M_DISPLAY | M_PRINTING))
934 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
935 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
939 state_puts (s->prefix, s);
940 state_putc ('\n', s);
942 rc = mutt_body_handler (b->parts, s);
945 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
946 a->encoding == ENCUUENCODED)
952 /* returns 1 if decoding the attachment will produce output */
953 int mutt_can_decode (BODY * a)
957 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
958 if (mutt_is_autoview (a, type))
959 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
960 else if (a->type == TYPETEXT)
962 else if (a->type == TYPEMESSAGE)
964 else if (a->type == TYPEMULTIPART) {
967 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
968 ascii_strcasecmp (a->subtype, "encrypted") == 0)
971 for (p = a->parts; p; p = p->next) {
972 if (mutt_can_decode (p))
977 else if (a->type == TYPEAPPLICATION) {
978 if (mutt_is_application_pgp(a))
980 if (mutt_is_application_smime (a))
987 static int multipart_handler (BODY * a, STATE * s)
995 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
996 a->encoding == ENCUUENCODED) {
997 fstat (fileno (s->fpin), &st);
999 b->length = (long) st.st_size;
1000 b->parts = mutt_parse_multipart(s->fpin,
1001 parameter_getval(a->parameter, "boundary"),
1003 !ascii_strcasecmp("digest", a->subtype));
1008 for (p = b->parts, count = 1; p; p = p->next, count++) {
1009 if (s->flags & M_DISPLAY) {
1010 state_mark_attach (s);
1011 state_printf (s, _("[-- Attachment #%d"), count);
1012 if (p->description || p->filename || p->form_name) {
1013 state_puts (": ", s);
1014 state_puts (p->description ? p->description :
1015 p->filename ? p->filename : p->form_name, s);
1017 state_puts (" --]\n", s);
1019 mutt_pretty_size (length, sizeof (length), p->length);
1021 state_mark_attach (s);
1022 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1023 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1024 if (!option (OPTWEED)) {
1025 fseeko (s->fpin, p->hdr_offset, 0);
1026 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1029 state_putc ('\n', s);
1032 if (p->description && mutt_can_decode (p))
1033 state_printf (s, "Content-Description: %s\n", p->description);
1036 state_printf (s, "%s: \n", p->form_name);
1039 rc = mutt_body_handler (p, s);
1040 state_putc ('\n', s);
1041 if (rc || ((s->flags & M_REPLYING)
1042 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1046 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1047 a->encoding == ENCUUENCODED)
1053 static int autoview_handler (BODY * a, STATE * s)
1055 rfc1524_entry *entry = rfc1524_entry_new();
1056 char buffer[LONG_STRING];
1058 char command[LONG_STRING];
1059 char tempfile[_POSIX_PATH_MAX] = "";
1068 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1069 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1071 fname = m_strdup(a->filename);
1072 mutt_sanitize_filename (fname, 1);
1073 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1077 if (entry->command) {
1078 m_strcpy(command, sizeof(command), entry->command);
1080 /* rfc1524_expand_command returns 0 if the file is required */
1082 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1084 if (s->flags & M_DISPLAY) {
1085 state_mark_attach (s);
1086 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1087 mutt_message (_("Invoking autoview command: %s"), command);
1090 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1091 mutt_perror ("fopen");
1092 rfc1524_entry_delete(&entry);
1096 mutt_copy_bytes (s->fpin, fpin, a->length);
1099 safe_fclose (&fpin);
1100 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1106 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1107 fileno (fpin), -1, -1);
1111 mutt_perror (_("Can't create filter"));
1113 if (s->flags & M_DISPLAY) {
1114 state_mark_attach (s);
1115 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1122 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1123 state_puts (s->prefix, s);
1124 state_puts (buffer, s);
1126 /* check for data on stderr */
1127 if (fgets (buffer, sizeof (buffer), fperr)) {
1128 if (s->flags & M_DISPLAY) {
1129 state_mark_attach (s);
1130 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1133 state_puts (s->prefix, s);
1134 state_puts (buffer, s);
1135 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1136 state_puts (s->prefix, s);
1137 state_puts (buffer, s);
1142 mutt_copy_stream (fpout, s->fpout);
1143 /* Check for stderr messages */
1144 if (fgets (buffer, sizeof (buffer), fperr)) {
1145 if (s->flags & M_DISPLAY) {
1146 state_mark_attach (s);
1147 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1150 state_puts (buffer, s);
1151 mutt_copy_stream (fperr, s->fpout);
1156 safe_fclose (&fpout);
1157 safe_fclose (&fperr);
1159 mutt_wait_filter (thepid);
1161 safe_fclose (&fpin);
1163 mutt_unlink (tempfile);
1165 if (s->flags & M_DISPLAY)
1166 mutt_clear_error ();
1168 rfc1524_entry_delete(&entry);
1172 static int external_body_handler (BODY * b, STATE * s)
1174 const char *access_type;
1175 const char *expiration;
1178 access_type = parameter_getval(b->parameter, "access-type");
1180 if (s->flags & M_DISPLAY) {
1181 state_mark_attach (s);
1182 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1187 expiration = parameter_getval(b->parameter, "expiration");
1189 expire = mutt_parse_date (expiration, NULL);
1193 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1194 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1196 char pretty_size[10];
1198 state_mark_attach (s);
1199 state_printf (s, _("[-- This %s/%s attachment "),
1200 TYPE (b->parts), b->parts->subtype);
1201 length = parameter_getval(b->parameter, "length");
1203 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1204 strtol (length, NULL, 10));
1205 state_printf (s, _("(size %s bytes) "), pretty_size);
1207 state_puts (_("has been deleted --]\n"), s);
1210 state_mark_attach (s);
1211 state_printf (s, _("[-- on %s --]\n"), expiration);
1213 if (b->parts->filename) {
1214 state_mark_attach (s);
1215 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1218 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1219 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1223 else if (expiration && expire < time (NULL)) {
1224 if (s->flags & M_DISPLAY) {
1225 state_mark_attach (s);
1226 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1227 TYPE (b->parts), b->parts->subtype);
1228 state_attach_puts (_("[-- and the indicated external source has --]\n"
1229 "[-- expired. --]\n"), s);
1231 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1232 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1237 if (s->flags & M_DISPLAY) {
1238 state_mark_attach (s);
1240 _("[-- This %s/%s attachment is not included, --]\n"),
1241 TYPE (b->parts), b->parts->subtype);
1242 state_mark_attach (s);
1245 ("[-- and the indicated access-type %s is unsupported --]\n"),
1247 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1248 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1255 void mutt_decode_attachment (BODY * b, STATE * s)
1257 int istext = mutt_is_text_part (b);
1258 iconv_t cd = MUTT_ICONV_ERROR;
1263 if (s->flags & M_CHARCONV) {
1264 const char *charset = parameter_getval(b->parameter, "charset");
1266 if (!option (OPTSTRICTMIME) && !charset)
1267 charset = charset_getfirst(AssumedCharset);
1268 if (charset && Charset)
1269 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1272 if (b->file_charset)
1273 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1277 fseeko (s->fpin, b->offset, 0);
1278 switch (b->encoding) {
1279 case ENCQUOTEDPRINTABLE:
1280 mutt_decode_quoted(s, b->length,
1281 istext || mutt_is_application_pgp(b), cd);
1284 mutt_decode_base64(s, b->length,
1285 istext || mutt_is_application_pgp(b), cd);
1288 mutt_decode_uuencoded(s, b->length,
1289 istext || mutt_is_application_pgp(b), cd);
1292 mutt_decode_xbit(s, b->length,
1293 istext || mutt_is_application_pgp(b), cd);
1297 if (cd != MUTT_ICONV_ERROR)
1301 int mutt_body_handler (BODY * b, STATE * s)
1306 char tempfile[_POSIX_PATH_MAX];
1307 handler_t handler = NULL;
1309 ssize_t tmplength = 0;
1313 int oflags = s->flags;
1315 /* first determine which handler to use to process this part */
1317 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1318 if (mutt_is_autoview (b, type)) {
1319 rfc1524_entry *entry = rfc1524_entry_new();
1321 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1322 handler = autoview_handler;
1323 s->flags &= ~M_CHARCONV;
1325 rfc1524_entry_delete(&entry);
1327 else if (b->type == TYPETEXT) {
1328 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1329 /* avoid copying this part twice since removing the transfer-encoding is
1330 * the only operation needed.
1332 if (mutt_is_application_pgp (b))
1333 handler = crypt_pgp_application_pgp_handler;
1335 if (!ascii_strcasecmp("flowed", parameter_getval(b->parameter, "format")))
1336 handler = rfc3676_handler;
1340 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1341 handler = text_enriched_handler;
1342 else /* text body type without a handler */
1345 else if (b->type == TYPEMESSAGE) {
1346 if (mutt_is_message_type (b->type, b->subtype))
1347 handler = message_handler;
1348 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1350 else if (!ascii_strcasecmp ("external-body", b->subtype))
1351 handler = external_body_handler;
1353 else if (b->type == TYPEMULTIPART) {
1356 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1357 handler = alternative_handler;
1358 else if (ascii_strcasecmp ("signed", b->subtype) == 0) {
1359 p = parameter_getval(b->parameter, "protocol");
1362 mutt_error (_("Error: multipart/signed has no protocol."));
1364 else if (s->flags & M_VERIFY)
1365 handler = mutt_signed_handler;
1367 else if (m_strcasecmp("encrypted", b->subtype) == 0) {
1368 p = parameter_getval(b->parameter, "protocol");
1372 ("Error: multipart/encrypted has no protocol parameter!"));
1374 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1375 handler = crypt_pgp_encrypted_handler;
1379 handler = multipart_handler;
1381 else if (b->type == TYPEAPPLICATION) {
1382 if (mutt_is_application_pgp (b))
1383 handler = crypt_pgp_application_pgp_handler;
1384 if (mutt_is_application_smime (b))
1385 handler = crypt_smime_application_smime_handler;
1389 if (plaintext || handler) {
1390 fseeko (s->fpin, b->offset, 0);
1392 /* see if we need to decode this part before processing it */
1393 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1395 * set conversion even
1396 * with 8bit encoding.
1398 int origType = b->type;
1399 char *savePrefix = NULL;
1402 /* decode to a tempfile, saving the original destination */
1404 mutt_mktemp (tempfile);
1405 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1406 mutt_error _("Unable to open temporary file!");
1410 /* decoding the attachment changes the size and offset, so save a copy
1411 * of the "real" values now, and restore them after processing
1413 tmplength = b->length;
1414 tmpoffset = b->offset;
1416 /* if we are decoding binary bodies, we don't want to prefix each
1417 * line with the prefix or else the data will get corrupted.
1419 savePrefix = s->prefix;
1427 mutt_decode_attachment (b, s);
1430 b->length = ftello (s->fpout);
1434 /* restore final destination and substitute the tempfile for input */
1437 s->fpin = safe_fopen (tempfile, "r");
1440 /* restore the prefix */
1441 s->prefix = savePrefix;
1447 /* process the (decoded) body part */
1449 rc = handler (b, s);
1452 b->length = tmplength;
1453 b->offset = tmpoffset;
1455 /* restore the original source stream */
1460 s->flags |= M_FIRSTDONE;
1462 else if (s->flags & M_DISPLAY) {
1463 state_mark_attach (s);
1464 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1465 if (!option (OPTVIEWATTACH)) {
1467 (type, sizeof (type),
1468 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1469 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1471 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1473 fputs (" --]\n", s->fpout);
1477 s->flags = oflags | (s->flags & M_FIRSTDONE);