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>
18 #include "recvattach.h"
23 #include <lib-crypt/crypt.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 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 unsigned char decode_byte (char ch)
278 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
280 char tmps[SHORT_STRING];
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;
342 "param", RICH_PARAM}, {
343 "bold", RICH_BOLD}, {
344 "italic", RICH_ITALIC}, {
345 "underline", RICH_UNDERLINE}, {
346 "nofill", RICH_NOFILL}, {
347 "excerpt", RICH_EXCERPT}, {
348 "indent", RICH_INDENT}, {
349 "indentright", RICH_INDENT_RIGHT}, {
350 "center", RICH_CENTER}, {
351 "flushleft", RICH_FLUSHLEFT}, {
352 "flushright", RICH_FLUSHRIGHT}, {
353 "flushboth", RICH_FLUSHLEFT}, {
354 "color", RICH_COLOR}, {
355 "x-color", RICH_COLOR}, {
359 struct enriched_state {
372 int tag_level[RICH_LAST_TAG];
377 static void enriched_wrap (struct enriched_state *stte)
382 if (stte->line_len) {
383 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
384 /* Strip trailing white space */
385 ssize_t y = stte->line_used - 1;
387 while (y && ISSPACE (stte->line[y])) {
388 stte->line[y] = '\0';
393 if (stte->tag_level[RICH_CENTER]) {
394 /* Strip leading whitespace */
397 while (stte->line[y] && ISSPACE (stte->line[y]))
402 for (z = y; z <= stte->line_used; z++) {
403 stte->line[z - y] = stte->line[z];
407 stte->line_used -= y;
412 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
413 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
415 if (stte->tag_level[RICH_CENTER]) {
418 state_putc (' ', stte->s);
422 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
425 state_putc (' ', stte->s);
430 state_puts (stte->line, stte->s);
433 state_putc ('\n', stte->s);
434 stte->line[0] = '\0';
437 stte->indent_len = 0;
438 if (stte->s->prefix) {
439 state_puts (stte->s->prefix, stte->s);
440 stte->indent_len += m_strlen(stte->s->prefix);
443 if (stte->tag_level[RICH_EXCERPT]) {
444 x = stte->tag_level[RICH_EXCERPT];
446 if (stte->s->prefix) {
447 state_puts (stte->s->prefix, stte->s);
448 stte->indent_len += m_strlen(stte->s->prefix);
451 state_puts ("> ", stte->s);
452 stte->indent_len += m_strlen("> ");
458 stte->indent_len = 0;
459 if (stte->tag_level[RICH_INDENT]) {
460 x = stte->tag_level[RICH_INDENT] * IndentSize;
461 stte->indent_len += x;
463 state_putc (' ', stte->s);
469 static void enriched_flush (struct enriched_state *stte, int wrap)
471 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
473 (stte->tag_level[RICH_INDENT_RIGHT] *
474 IndentSize) - stte->indent_len)))
475 enriched_wrap (stte);
477 if (stte->buff_used) {
478 stte->buffer[stte->buff_used] = '\0';
479 stte->line_used += stte->buff_used;
480 if (stte->line_used > stte->line_max) {
481 stte->line_max = stte->line_used;
482 p_realloc(&stte->line, stte->line_max + 1);
484 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
485 stte->line_len += stte->word_len;
490 enriched_wrap (stte);
494 static void enriched_putc (int c, struct enriched_state *stte)
496 if (stte->tag_level[RICH_PARAM]) {
497 if (stte->tag_level[RICH_COLOR]) {
498 if (stte->param_used + 1 >= stte->param_len)
499 p_realloc(&stte->param, (stte->param_len += STRING));
501 stte->param[stte->param_used++] = c;
503 return; /* nothing to do */
506 /* see if more space is needed (plus extra for possible rich characters) */
507 if (stte->buff_len < stte->buff_used + 3) {
508 stte->buff_len += LONG_STRING;
509 p_realloc(&stte->buffer, stte->buff_len + 1);
512 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
514 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
518 stte->buffer[stte->buff_used++] = c;
519 enriched_flush (stte, 0);
522 if (stte->s->flags & M_DISPLAY) {
523 if (stte->tag_level[RICH_BOLD]) {
524 stte->buffer[stte->buff_used++] = c;
525 stte->buffer[stte->buff_used++] = '\010';
526 stte->buffer[stte->buff_used++] = c;
528 else if (stte->tag_level[RICH_UNDERLINE]) {
530 stte->buffer[stte->buff_used++] = '_';
531 stte->buffer[stte->buff_used++] = '\010';
532 stte->buffer[stte->buff_used++] = c;
534 else if (stte->tag_level[RICH_ITALIC]) {
535 stte->buffer[stte->buff_used++] = c;
536 stte->buffer[stte->buff_used++] = '\010';
537 stte->buffer[stte->buff_used++] = '_';
540 stte->buffer[stte->buff_used++] = c;
544 stte->buffer[stte->buff_used++] = c;
550 static void enriched_puts (const char *s, struct enriched_state *stte)
554 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
555 stte->buff_len += LONG_STRING;
556 p_realloc(&stte->buffer, stte->buff_len + 1);
561 stte->buffer[stte->buff_used++] = *p++;
565 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
567 const char *tagptr = tag;
573 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
574 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
575 j = EnrichedTags[i].index;
580 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
581 enriched_flush (stte, 1);
584 if (stte->tag_level[j]) /* make sure not to go negative */
585 stte->tag_level[j]--;
586 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
587 && stte->tag_level[RICH_COLOR]) {
588 stte->param[stte->param_used] = '\0';
589 if (!ascii_strcasecmp (stte->param, "black")) {
590 enriched_puts ("\033[30m", stte);
592 else if (!ascii_strcasecmp (stte->param, "red")) {
593 enriched_puts ("\033[31m", stte);
595 else if (!ascii_strcasecmp (stte->param, "green")) {
596 enriched_puts ("\033[32m", stte);
598 else if (!ascii_strcasecmp (stte->param, "yellow")) {
599 enriched_puts ("\033[33m", stte);
601 else if (!ascii_strcasecmp (stte->param, "blue")) {
602 enriched_puts ("\033[34m", stte);
604 else if (!ascii_strcasecmp (stte->param, "magenta")) {
605 enriched_puts ("\033[35m", stte);
607 else if (!ascii_strcasecmp (stte->param, "cyan")) {
608 enriched_puts ("\033[36m", stte);
610 else if (!ascii_strcasecmp (stte->param, "white")) {
611 enriched_puts ("\033[37m", stte);
614 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
615 enriched_puts ("\033[0m", stte);
618 /* flush parameter buffer when closing the tag */
619 if (j == RICH_PARAM) {
620 stte->param_used = 0;
621 stte->param[0] = '\0';
625 stte->tag_level[j]++;
627 if (j == RICH_EXCERPT)
628 enriched_flush (stte, 1);
632 int text_enriched_handler (BODY * a, STATE * s)
635 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
638 long bytes = a->length;
639 struct enriched_state stte;
642 char tag[LONG_STRING + 1];
647 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
648 72) ? (COLS - 4) : 72);
649 stte.line_max = stte.WrapMargin * 4;
650 stte.line = p_new(char, stte.line_max + 1);
651 stte.param = p_new(char, STRING);
653 stte.param_len = STRING;
657 state_puts (s->prefix, s);
658 stte.indent_len += m_strlen(s->prefix);
661 while (state != DONE) {
662 if (state != ST_EOF) {
663 if (!bytes || (c = fgetc (s->fpin)) == EOF)
677 if (stte.tag_level[RICH_NOFILL]) {
678 enriched_flush (&stte, 1);
681 enriched_putc (' ', &stte);
687 enriched_putc (c, &stte);
693 enriched_putc (c, &stte);
701 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
705 enriched_set_flags (tag, &stte);
708 else if (tag_len < LONG_STRING) /* ignore overly long tags */
721 enriched_flush (&stte, 1);
730 enriched_putc ('\0', &stte);
731 enriched_flush (&stte, 1);
735 case DONE: /* not reached, but gcc complains if this is absent */
740 state_putc ('\n', s); /* add a final newline */
742 p_delete(&(stte.buffer));
743 p_delete(&(stte.line));
744 p_delete(&(stte.param));
751 #define TXTENRICHED 3
753 static int alternative_handler (BODY * a, STATE * s)
763 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
764 a->encoding == ENCUUENCODED) {
768 fstat (fileno (s->fpin), &st);
770 b->length = (long) st.st_size;
771 b->parts = mutt_parse_multipart(s->fpin,
772 parameter_getval(a->parameter, "boundary"),
774 !ascii_strcasecmp("digest", a->subtype));
781 /* First, search list of prefered types */
782 t = AlternativeOrderList;
783 while (t && !choice) {
785 int btlen; /* length of basetype */
786 int wild; /* do we have a wildcard to match all subtypes? */
788 c = strchr (t->data, '/');
790 wild = (c[1] == '*' && c[2] == 0);
795 btlen = m_strlen(t->data);
803 const char *bt = TYPE (b);
805 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
806 /* the basetype matches */
807 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
816 /* Next, look for an autoviewable type */
823 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
824 if (mutt_is_autoview (b, buf)) {
825 rfc1524_entry *entry = rfc1524_entry_new();
827 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
830 rfc1524_entry_delete(&entry);
836 /* Then, look for a text entry */
843 if (b->type == TYPETEXT) {
844 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
848 else if (!ascii_strcasecmp ("enriched", b->subtype)
849 && type <= TXTENRICHED) {
853 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
862 /* Finally, look for other possibilities */
869 if (mutt_can_decode (b))
876 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
877 fseeko (s->fpin, choice->hdr_offset, 0);
878 mutt_copy_bytes (s->fpin, s->fpout,
879 choice->offset - choice->hdr_offset);
881 mutt_body_handler (choice, s);
883 else if (s->flags & M_DISPLAY) {
884 /* didn't find anything that we could display! */
885 state_mark_attach (s);
886 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
896 /* handles message/rfc822 body parts */
897 static int message_handler (BODY * a, STATE * s)
904 off_start = ftello (s->fpin);
905 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
906 a->encoding == ENCUUENCODED) {
907 fstat (fileno (s->fpin), &st);
909 b->length = (long) st.st_size;
910 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
916 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
917 (((s->flags & M_WEED)
918 || ((s->flags & (M_DISPLAY | M_PRINTING))
919 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
920 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
924 state_puts (s->prefix, s);
925 state_putc ('\n', s);
927 rc = mutt_body_handler (b->parts, s);
930 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
931 a->encoding == ENCUUENCODED)
937 /* returns 1 if decoding the attachment will produce output */
938 int mutt_can_decode (BODY * a)
942 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
943 if (mutt_is_autoview (a, type))
944 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
945 else if (a->type == TYPETEXT)
947 else if (a->type == TYPEMESSAGE)
949 else if (a->type == TYPEMULTIPART) {
952 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
953 ascii_strcasecmp (a->subtype, "encrypted") == 0)
956 for (p = a->parts; p; p = p->next) {
957 if (mutt_can_decode (p))
962 else if (a->type == TYPEAPPLICATION) {
963 if (mutt_is_application_pgp(a))
965 if (mutt_is_application_smime (a))
972 static int multipart_handler (BODY * a, STATE * s)
980 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
981 a->encoding == ENCUUENCODED) {
982 fstat (fileno (s->fpin), &st);
984 b->length = (long) st.st_size;
985 b->parts = mutt_parse_multipart(s->fpin,
986 parameter_getval(a->parameter, "boundary"),
988 !ascii_strcasecmp("digest", a->subtype));
993 for (p = b->parts, count = 1; p; p = p->next, count++) {
994 if (s->flags & M_DISPLAY) {
995 state_mark_attach (s);
996 state_printf (s, _("[-- Attachment #%d"), count);
997 if (p->description || p->filename || p->form_name) {
998 state_puts (": ", s);
999 state_puts (p->description ? p->description :
1000 p->filename ? p->filename : p->form_name, s);
1002 state_puts (" --]\n", s);
1004 mutt_pretty_size (length, sizeof (length), p->length);
1006 state_mark_attach (s);
1007 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1008 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1009 if (!option (OPTWEED)) {
1010 fseeko (s->fpin, p->hdr_offset, 0);
1011 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1014 state_putc ('\n', s);
1017 if (p->description && mutt_can_decode (p))
1018 state_printf (s, "Content-Description: %s\n", p->description);
1021 state_printf (s, "%s: \n", p->form_name);
1024 rc = mutt_body_handler (p, s);
1025 state_putc ('\n', s);
1026 if (rc || ((s->flags & M_REPLYING)
1027 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1031 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1032 a->encoding == ENCUUENCODED)
1038 static int autoview_handler (BODY * a, STATE * s)
1040 rfc1524_entry *entry = rfc1524_entry_new();
1041 char buffer[LONG_STRING];
1043 char command[LONG_STRING];
1044 char tempfile[_POSIX_PATH_MAX] = "";
1053 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1054 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1056 fname = m_strdup(a->filename);
1057 mutt_sanitize_filename (fname, 1);
1058 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1062 if (entry->command) {
1063 m_strcpy(command, sizeof(command), entry->command);
1065 /* rfc1524_expand_command returns 0 if the file is required */
1067 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1069 if (s->flags & M_DISPLAY) {
1070 state_mark_attach (s);
1071 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1072 mutt_message (_("Invoking autoview command: %s"), command);
1075 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1076 mutt_perror ("fopen");
1077 rfc1524_entry_delete(&entry);
1081 mutt_copy_bytes (s->fpin, fpin, a->length);
1084 safe_fclose (&fpin);
1085 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1091 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1092 fileno (fpin), -1, -1);
1096 mutt_perror (_("Can't create filter"));
1098 if (s->flags & M_DISPLAY) {
1099 state_mark_attach (s);
1100 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1107 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1108 state_puts (s->prefix, s);
1109 state_puts (buffer, s);
1111 /* check for data on stderr */
1112 if (fgets (buffer, sizeof (buffer), fperr)) {
1113 if (s->flags & M_DISPLAY) {
1114 state_mark_attach (s);
1115 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1118 state_puts (s->prefix, s);
1119 state_puts (buffer, s);
1120 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1121 state_puts (s->prefix, s);
1122 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);
1141 safe_fclose (&fpout);
1142 safe_fclose (&fperr);
1144 mutt_wait_filter (thepid);
1146 safe_fclose (&fpin);
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 (!ascii_strcasecmp (access_type, "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) |
1222 if (s->flags & M_DISPLAY) {
1223 state_mark_attach (s);
1225 _("[-- This %s/%s attachment is not included, --]\n"),
1226 TYPE (b->parts), b->parts->subtype);
1227 state_mark_attach (s);
1230 ("[-- and the indicated access-type %s is unsupported --]\n"),
1232 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1233 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1240 void mutt_decode_attachment (BODY * b, STATE * s)
1242 int istext = mutt_is_text_part (b);
1243 iconv_t cd = MUTT_ICONV_ERROR;
1248 if (s->flags & M_CHARCONV) {
1249 const char *charset = parameter_getval(b->parameter, "charset");
1251 if (!option (OPTSTRICTMIME) && !charset)
1252 charset = charset_getfirst(AssumedCharset);
1253 if (charset && Charset)
1254 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1257 if (b->file_charset)
1258 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1262 fseeko (s->fpin, b->offset, 0);
1263 switch (b->encoding) {
1264 case ENCQUOTEDPRINTABLE:
1265 mutt_decode_quoted(s, b->length,
1266 istext || mutt_is_application_pgp(b), cd);
1269 mutt_decode_base64(s, b->length,
1270 istext || mutt_is_application_pgp(b), cd);
1273 mutt_decode_uuencoded(s, b->length,
1274 istext || mutt_is_application_pgp(b), cd);
1277 mutt_decode_xbit(s, b->length,
1278 istext || mutt_is_application_pgp(b), cd);
1282 if (cd != MUTT_ICONV_ERROR)
1286 int mutt_body_handler (BODY * b, STATE * s)
1291 char tempfile[_POSIX_PATH_MAX];
1292 handler_t handler = NULL;
1294 ssize_t tmplength = 0;
1298 int oflags = s->flags;
1300 /* first determine which handler to use to process this part */
1302 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1303 if (mutt_is_autoview (b, type)) {
1304 rfc1524_entry *entry = rfc1524_entry_new();
1306 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1307 handler = autoview_handler;
1308 s->flags &= ~M_CHARCONV;
1310 rfc1524_entry_delete(&entry);
1312 else if (b->type == TYPETEXT) {
1313 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1314 /* avoid copying this part twice since removing the transfer-encoding is
1315 * the only operation needed.
1317 if (mutt_is_application_pgp (b))
1318 handler = crypt_pgp_application_pgp_handler;
1320 if (!ascii_strcasecmp("flowed", parameter_getval(b->parameter, "format")))
1321 handler = rfc3676_handler;
1325 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1326 handler = text_enriched_handler;
1327 else /* text body type without a handler */
1330 else if (b->type == TYPEMESSAGE) {
1331 if (mutt_is_message_type (b->type, b->subtype))
1332 handler = message_handler;
1333 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1335 else if (!ascii_strcasecmp ("external-body", b->subtype))
1336 handler = external_body_handler;
1338 else if (b->type == TYPEMULTIPART) {
1341 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1342 handler = alternative_handler;
1343 else if (ascii_strcasecmp ("signed", b->subtype) == 0) {
1344 p = parameter_getval(b->parameter, "protocol");
1347 mutt_error (_("Error: multipart/signed has no protocol."));
1349 else if (s->flags & M_VERIFY)
1350 handler = mutt_signed_handler;
1352 else if (m_strcasecmp("encrypted", b->subtype) == 0) {
1353 p = parameter_getval(b->parameter, "protocol");
1357 ("Error: multipart/encrypted has no protocol parameter!"));
1359 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1360 handler = crypt_pgp_encrypted_handler;
1364 handler = multipart_handler;
1366 else if (b->type == TYPEAPPLICATION) {
1367 if (mutt_is_application_pgp (b))
1368 handler = crypt_pgp_application_pgp_handler;
1369 if (mutt_is_application_smime (b))
1370 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 || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1380 * set conversion even
1381 * with 8bit encoding.
1383 int origType = b->type;
1384 char *savePrefix = NULL;
1387 /* decode to a tempfile, saving the original destination */
1389 mutt_mktemp (tempfile);
1390 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1391 mutt_error _("Unable to open temporary file!");
1395 /* decoding the attachment changes the size and offset, so save a copy
1396 * of the "real" values now, and restore them after processing
1398 tmplength = b->length;
1399 tmpoffset = b->offset;
1401 /* if we are decoding binary bodies, we don't want to prefix each
1402 * line with the prefix or else the data will get corrupted.
1404 savePrefix = s->prefix;
1412 mutt_decode_attachment (b, s);
1415 b->length = ftello (s->fpout);
1419 /* restore final destination and substitute the tempfile for input */
1422 s->fpin = safe_fopen (tempfile, "r");
1425 /* restore the prefix */
1426 s->prefix = savePrefix;
1432 /* process the (decoded) body part */
1434 rc = handler (b, s);
1437 b->length = tmplength;
1438 b->offset = tmpoffset;
1440 /* restore the original source stream */
1445 s->flags |= M_FIRSTDONE;
1447 else if (s->flags & M_DISPLAY) {
1448 state_mark_attach (s);
1449 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1450 if (!option (OPTVIEWATTACH)) {
1452 (type, sizeof (type),
1453 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1454 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1456 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1458 fputs (" --]\n", s->fpout);
1462 s->flags = oflags | (s->flags & M_FIRSTDONE);