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)
142 char line[10 * STRING];
143 char decline[20 * 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 assert (l < sizeof(decline));
186 mutt_convert_to_state (cd, decline, &l, s);
189 mutt_convert_to_state (cd, 0, 0, s);
190 state_reset_prefix (s);
193 static unsigned char decode_byte(int ch)
195 return ch == 96 ? 0 : ch - 32;
198 static void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
201 char linelen, c, l, out;
203 char bufi[BUFI_SIZE];
207 state_set_prefix (s);
210 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
212 len -= m_strlen(tmps);
213 if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
217 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
219 len -= m_strlen(tmps);
220 if (!m_strncmp(tmps, "end", 3))
223 linelen = decode_byte(*pt);
225 for (c = 0; c < linelen;) {
226 for (l = 2; l <= 6; l += 2) {
227 out = decode_byte(*pt) << l;
229 out |= (decode_byte(*pt) >> (6 - l));
235 mutt_convert_to_state (cd, bufi, &k, s);
240 mutt_convert_to_state (cd, bufi, &k, s);
241 mutt_convert_to_state (cd, 0, 0, s);
243 state_reset_prefix (s);
246 /* ----------------------------------------------------------------------------
247 * A (not so) minimal implementation of RFC1563.
250 #define IndentSize (4)
252 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
253 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
254 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
258 const char *tag_name;
261 {"param", RICH_PARAM},
263 {"italic", RICH_ITALIC},
264 {"underline", RICH_UNDERLINE},
265 {"nofill", RICH_NOFILL},
266 {"excerpt", RICH_EXCERPT},
267 {"indent", RICH_INDENT},
268 {"indentright", RICH_INDENT_RIGHT},
269 {"center", RICH_CENTER},
270 {"flushleft", RICH_FLUSHLEFT},
271 {"flushright", RICH_FLUSHRIGHT},
272 {"flushboth", RICH_FLUSHLEFT},
273 {"color", RICH_COLOR},
274 {"x-color", RICH_COLOR},
278 struct enriched_state {
291 int tag_level[RICH_LAST_TAG];
296 static void enriched_wrap (struct enriched_state *stte)
301 if (stte->line_len) {
302 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
303 /* Strip trailing white space */
304 ssize_t y = stte->line_used - 1;
306 while (y && ISSPACE (stte->line[y])) {
307 stte->line[y] = '\0';
312 if (stte->tag_level[RICH_CENTER]) {
313 /* Strip leading whitespace */
316 while (stte->line[y] && ISSPACE (stte->line[y]))
321 for (z = y; z <= stte->line_used; z++) {
322 stte->line[z - y] = stte->line[z];
326 stte->line_used -= y;
331 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
332 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
334 if (stte->tag_level[RICH_CENTER]) {
337 state_putc (' ', stte->s);
341 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
344 state_putc (' ', stte->s);
349 state_puts (stte->line, stte->s);
352 state_putc ('\n', stte->s);
353 stte->line[0] = '\0';
356 stte->indent_len = 0;
357 if (stte->s->prefix) {
358 state_puts (stte->s->prefix, stte->s);
359 stte->indent_len += m_strlen(stte->s->prefix);
362 if (stte->tag_level[RICH_EXCERPT]) {
363 x = stte->tag_level[RICH_EXCERPT];
365 if (stte->s->prefix) {
366 state_puts (stte->s->prefix, stte->s);
367 stte->indent_len += m_strlen(stte->s->prefix);
370 state_puts ("> ", stte->s);
371 stte->indent_len += m_strlen("> ");
377 stte->indent_len = 0;
378 if (stte->tag_level[RICH_INDENT]) {
379 x = stte->tag_level[RICH_INDENT] * IndentSize;
380 stte->indent_len += x;
382 state_putc (' ', stte->s);
388 static void enriched_flush (struct enriched_state *stte, int wrap)
390 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
392 (stte->tag_level[RICH_INDENT_RIGHT] *
393 IndentSize) - stte->indent_len)))
394 enriched_wrap (stte);
396 if (stte->buff_used) {
397 stte->buffer[stte->buff_used] = '\0';
398 stte->line_used += stte->buff_used;
399 if (stte->line_used > stte->line_max) {
400 stte->line_max = stte->line_used;
401 p_realloc(&stte->line, stte->line_max + 1);
403 m_strcat(stte->line, stte->line_max + 1, stte->buffer);
404 stte->line_len += stte->word_len;
409 enriched_wrap (stte);
413 static void enriched_putc (int c, struct enriched_state *stte)
415 if (stte->tag_level[RICH_PARAM]) {
416 if (stte->tag_level[RICH_COLOR]) {
417 if (stte->param_used + 1 >= stte->param_len)
418 p_realloc(&stte->param, (stte->param_len += STRING));
420 stte->param[stte->param_used++] = c;
422 return; /* nothing to do */
425 /* see if more space is needed (plus extra for possible rich characters) */
426 if (stte->buff_len < stte->buff_used + 3) {
427 stte->buff_len += LONG_STRING;
428 p_realloc(&stte->buffer, stte->buff_len + 1);
431 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
433 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
437 stte->buffer[stte->buff_used++] = c;
438 enriched_flush (stte, 0);
441 if (stte->s->flags & M_DISPLAY) {
442 if (stte->tag_level[RICH_BOLD]) {
443 stte->buffer[stte->buff_used++] = c;
444 stte->buffer[stte->buff_used++] = '\010';
445 stte->buffer[stte->buff_used++] = c;
447 else if (stte->tag_level[RICH_UNDERLINE]) {
449 stte->buffer[stte->buff_used++] = '_';
450 stte->buffer[stte->buff_used++] = '\010';
451 stte->buffer[stte->buff_used++] = c;
453 else if (stte->tag_level[RICH_ITALIC]) {
454 stte->buffer[stte->buff_used++] = c;
455 stte->buffer[stte->buff_used++] = '\010';
456 stte->buffer[stte->buff_used++] = '_';
459 stte->buffer[stte->buff_used++] = c;
462 stte->buffer[stte->buff_used++] = c;
468 static void enriched_puts (const char *s, struct enriched_state *stte)
472 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
473 stte->buff_len += LONG_STRING;
474 p_realloc(&stte->buffer, stte->buff_len + 1);
479 stte->buffer[stte->buff_used++] = *p++;
483 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
485 const char *tagptr = tag;
491 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
492 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
493 j = EnrichedTags[i].index;
498 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
499 enriched_flush (stte, 1);
502 if (stte->tag_level[j]) /* make sure not to go negative */
503 stte->tag_level[j]--;
504 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
505 && stte->tag_level[RICH_COLOR]) {
506 stte->param[stte->param_used] = '\0';
507 if (!ascii_strcasecmp (stte->param, "black")) {
508 enriched_puts ("\033[30m", stte);
510 else if (!ascii_strcasecmp (stte->param, "red")) {
511 enriched_puts ("\033[31m", stte);
513 else if (!ascii_strcasecmp (stte->param, "green")) {
514 enriched_puts ("\033[32m", stte);
516 else if (!ascii_strcasecmp (stte->param, "yellow")) {
517 enriched_puts ("\033[33m", stte);
519 else if (!ascii_strcasecmp (stte->param, "blue")) {
520 enriched_puts ("\033[34m", stte);
522 else if (!ascii_strcasecmp (stte->param, "magenta")) {
523 enriched_puts ("\033[35m", stte);
525 else if (!ascii_strcasecmp (stte->param, "cyan")) {
526 enriched_puts ("\033[36m", stte);
528 else if (!ascii_strcasecmp (stte->param, "white")) {
529 enriched_puts ("\033[37m", stte);
532 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
533 enriched_puts ("\033[0m", stte);
536 /* flush parameter buffer when closing the tag */
537 if (j == RICH_PARAM) {
538 stte->param_used = 0;
539 stte->param[0] = '\0';
543 stte->tag_level[j]++;
545 if (j == RICH_EXCERPT)
546 enriched_flush (stte, 1);
550 static int text_enriched_handler (BODY * a, STATE * s)
553 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
556 long bytes = a->length;
557 struct enriched_state stte;
560 char tag[LONG_STRING + 1];
564 stte.WrapMargin = ((s->flags & M_DISPLAY) ? (getmaxx(main_w) - 4) :
565 ((getmaxx(main_w) - 4) < 72) ? (getmaxx(main_w) - 4) : 72);
566 stte.line_max = stte.WrapMargin * 4;
567 stte.line = p_new(char, stte.line_max + 1);
568 stte.param = p_new(char, STRING);
570 stte.param_len = STRING;
574 state_puts (s->prefix, s);
575 stte.indent_len += m_strlen(s->prefix);
578 while (state != DONE) {
579 if (state != ST_EOF) {
580 if (!bytes || (c = fgetc (s->fpin)) == EOF)
594 if (stte.tag_level[RICH_NOFILL]) {
595 enriched_flush (&stte, 1);
598 enriched_putc (' ', &stte);
604 enriched_putc (c, &stte);
610 enriched_putc (c, &stte);
618 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
622 enriched_set_flags (tag, &stte);
625 else if (tag_len < LONG_STRING) /* ignore overly long tags */
638 enriched_flush (&stte, 1);
647 enriched_putc ('\0', &stte);
648 enriched_flush (&stte, 1);
652 case DONE: /* not reached, but gcc complains if this is absent */
657 state_putc ('\n', s); /* add a final newline */
659 p_delete(&(stte.buffer));
660 p_delete(&(stte.line));
661 p_delete(&(stte.param));
668 #define TXTENRICHED 3
670 static int alternative_handler (BODY * a, STATE * s)
680 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
681 a->encoding == ENCUUENCODED) {
685 fstat (fileno (s->fpin), &st);
687 b->length = (long) st.st_size;
688 b->parts = mutt_parse_multipart(s->fpin,
689 parameter_getval(a->parameter, "boundary"),
691 mime_which_token(a->subtype, -1) == MIME_DIGEST);
698 /* First, search list of prefered types */
699 t = AlternativeOrderList;
700 while (t && !choice) {
702 int btlen; /* length of basetype */
703 int wild; /* do we have a wildcard to match all subtypes? */
705 c = strchr (t->data, '/');
707 wild = (c[1] == '*' && c[2] == 0);
712 btlen = m_strlen(t->data);
720 const char *bt = TYPE (b);
722 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
723 /* the basetype matches */
724 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
733 /* Next, look for an autoviewable type */
740 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
741 if (mutt_is_autoview (b, buf)) {
742 rfc1524_entry *entry = rfc1524_entry_new();
744 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
747 rfc1524_entry_delete(&entry);
753 /* Then, look for a text entry */
760 if (b->type == TYPETEXT) {
761 switch (mime_which_token(b->subtype, -1)) {
763 if (type <= TXTPLAIN) {
769 if (type <= TXTENRICHED) {
775 if (type <= TXTHTML) {
788 /* Finally, look for other possibilities */
795 if (mutt_can_decode (b))
802 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
803 fseeko (s->fpin, choice->hdr_offset, 0);
804 mutt_copy_bytes (s->fpin, s->fpout,
805 choice->offset - choice->hdr_offset);
807 mutt_body_handler (choice, s);
809 else if (s->flags & M_DISPLAY) {
810 /* didn't find anything that we could display! */
811 state_mark_attach (s);
812 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
822 /* handles message/rfc822 body parts */
823 static int message_handler (BODY * a, STATE * s)
830 off_start = ftello (s->fpin);
831 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
832 a->encoding == ENCUUENCODED) {
833 fstat (fileno (s->fpin), &st);
835 b->length = (long) st.st_size;
836 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
842 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
843 (((s->flags & M_WEED)
844 || ((s->flags & (M_DISPLAY | M_PRINTING))
845 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
846 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
850 state_puts (s->prefix, s);
851 state_putc ('\n', s);
853 rc = mutt_body_handler (b->parts, s);
856 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
857 a->encoding == ENCUUENCODED)
863 /* returns 1 if decoding the attachment will produce output */
864 int mutt_can_decode (BODY * a)
868 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
869 if (mutt_is_autoview (a, type))
870 return rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW);
871 else if (a->type == TYPETEXT)
873 else if (a->type == TYPEMESSAGE)
875 else if (a->type == TYPEMULTIPART) {
877 int tok = mime_which_token(a->subtype, -1);
879 if (tok == MIME_SIGNED || tok == MIME_ENCRYPTED)
882 for (p = a->parts; p; p = p->next) {
883 if (mutt_can_decode (p))
887 else if (a->type == TYPEAPPLICATION) {
888 if (mutt_is_application_pgp(a))
890 if (mutt_is_application_smime (a))
897 static int multipart_handler (BODY * a, STATE * s)
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_multipart(s->fpin,
911 parameter_getval(a->parameter, "boundary"),
913 mime_which_token(a->subtype, -1) == MIME_DIGEST);
918 for (p = b->parts, count = 1; p; p = p->next, count++) {
919 if (s->flags & M_DISPLAY) {
920 state_mark_attach (s);
921 state_printf (s, _("[-- Attachment #%d"), count);
922 if (p->description || p->filename || p->form_name) {
923 state_puts (": ", s);
924 state_puts (p->description ? p->description :
925 p->filename ? p->filename : p->form_name, s);
927 state_puts (" --]\n", s);
929 mutt_pretty_size (length, sizeof (length), p->length);
931 state_mark_attach (s);
932 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
933 TYPE (p), p->subtype, ENCODING (p->encoding), length);
934 if (!option (OPTWEED)) {
935 fseeko (s->fpin, p->hdr_offset, 0);
936 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
939 state_putc ('\n', s);
942 if (p->description && mutt_can_decode (p))
943 state_printf (s, "Content-Description: %s\n", p->description);
946 state_printf (s, "%s: \n", p->form_name);
949 rc = mutt_body_handler (p, s);
950 state_putc ('\n', s);
955 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
956 a->encoding == ENCUUENCODED)
962 static int autoview_handler (BODY * a, STATE * s)
964 rfc1524_entry *entry = rfc1524_entry_new();
965 char buffer[LONG_STRING];
967 char command[LONG_STRING];
968 char tempfile[_POSIX_PATH_MAX] = "";
977 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
978 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
980 fname = m_strdup(a->filename);
981 mutt_sanitize_filename (fname, 1);
982 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
986 if (entry->command) {
987 m_strcpy(command, sizeof(command), entry->command);
989 /* rfc1524_expand_command returns 0 if the file is required */
991 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
993 if (s->flags & M_DISPLAY) {
994 state_mark_attach (s);
995 state_printf (s, _("[-- Autoview using %s --]\n"), command);
996 mutt_message (_("Invoking autoview command: %s"), command);
999 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1000 mutt_perror ("fopen");
1001 rfc1524_entry_delete(&entry);
1005 mutt_copy_bytes (s->fpin, fpin, a->length);
1009 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1014 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1015 fileno (fpin), -1, -1);
1019 mutt_perror (_("Can't create filter"));
1021 if (s->flags & M_DISPLAY) {
1022 state_mark_attach (s);
1023 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1030 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1031 state_puts (s->prefix, s);
1032 state_puts (buffer, s);
1034 /* check for data on stderr */
1035 if (fgets (buffer, sizeof (buffer), fperr)) {
1036 if (s->flags & M_DISPLAY) {
1037 state_mark_attach (s);
1038 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1041 state_puts (s->prefix, s);
1042 state_puts (buffer, s);
1043 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1044 state_puts (s->prefix, s);
1045 state_puts (buffer, s);
1049 mutt_copy_stream (fpout, s->fpout);
1050 /* Check for stderr messages */
1051 if (fgets (buffer, sizeof (buffer), fperr)) {
1052 if (s->flags & M_DISPLAY) {
1053 state_mark_attach (s);
1054 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1057 state_puts (buffer, s);
1058 mutt_copy_stream (fperr, s->fpout);
1066 mutt_wait_filter (thepid);
1070 mutt_unlink (tempfile);
1072 if (s->flags & M_DISPLAY)
1073 mutt_clear_error ();
1075 rfc1524_entry_delete(&entry);
1079 static int external_body_handler (BODY * b, STATE * s)
1081 const char *access_type;
1082 const char *expiration;
1085 access_type = parameter_getval(b->parameter, "access-type");
1087 if (s->flags & M_DISPLAY) {
1088 state_mark_attach (s);
1089 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1094 expiration = parameter_getval(b->parameter, "expiration");
1096 expire = mutt_parse_date (expiration, NULL);
1100 if (mime_which_token(access_type, -1) == MIME_X_MUTT_DELETED) {
1101 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1103 char pretty_size[10];
1105 state_mark_attach (s);
1106 state_printf (s, _("[-- This %s/%s attachment "),
1107 TYPE (b->parts), b->parts->subtype);
1108 length = parameter_getval(b->parameter, "length");
1110 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1111 strtol (length, NULL, 10));
1112 state_printf (s, _("(size %s bytes) "), pretty_size);
1114 state_puts (_("has been deleted --]\n"), s);
1117 state_mark_attach (s);
1118 state_printf (s, _("[-- on %s --]\n"), expiration);
1120 if (b->parts->filename) {
1121 state_mark_attach (s);
1122 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1125 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1126 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1130 else if (expiration && expire < time (NULL)) {
1131 if (s->flags & M_DISPLAY) {
1132 state_mark_attach (s);
1133 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1134 TYPE (b->parts), b->parts->subtype);
1135 state_attach_puts (_("[-- and the indicated external source has --]\n"
1136 "[-- expired. --]\n"), s);
1138 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1139 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1143 if (s->flags & M_DISPLAY) {
1144 state_mark_attach (s);
1146 _("[-- This %s/%s attachment is not included, --]\n"),
1147 TYPE (b->parts), b->parts->subtype);
1148 state_mark_attach (s);
1151 ("[-- and the indicated access-type %s is unsupported --]\n"),
1153 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1154 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1161 static void mutt_decode_base64(STATE *s, long len, int istext, iconv_t cd)
1164 int c1, c2, c3, c4, ch, cr = 0, i;
1165 char bufi[BUFI_SIZE];
1171 state_set_prefix (s);
1174 for (i = 0; i < 4 && len > 0; len--) {
1175 if ((ch = fgetc (s->fpin)) == EOF)
1177 if (base64val(ch) >= 0 || ch == '=')
1184 c1 = base64val(buf[0]);
1185 c2 = base64val(buf[1]);
1186 ch = (c1 << 2) | (c2 >> 4);
1188 if (cr && ch != '\n')
1193 if (istext && ch == '\r')
1200 c3 = base64val(buf[2]);
1201 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
1203 if (cr && ch != '\n')
1208 if (istext && ch == '\r')
1215 c4 = base64val(buf[3]);
1216 ch = ((c3 & 0x3) << 6) | c4;
1218 if (cr && ch != '\n')
1222 if (istext && ch == '\r')
1227 if (l + 8 >= ssizeof (bufi))
1228 mutt_convert_to_state (cd, bufi, &l, s);
1234 mutt_convert_to_state (cd, bufi, &l, s);
1235 mutt_convert_to_state (cd, 0, 0, s);
1237 state_reset_prefix (s);
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");
1252 charset = charset_getfirst(mod_cset.assumed_charset);
1253 if (charset && mod_cset.charset)
1254 cd = mutt_iconv_open (mod_cset.charset, charset, M_ICONV_HOOK_FROM);
1256 if (b->file_charset)
1257 cd = mutt_iconv_open (mod_cset.charset, b->file_charset, M_ICONV_HOOK_FROM);
1261 fseeko (s->fpin, b->offset, 0);
1262 switch (b->encoding) {
1263 case ENCQUOTEDPRINTABLE:
1264 mutt_decode_quoted(s, b->length,
1265 istext || mutt_is_application_pgp(b), cd);
1268 mutt_decode_base64(s, b->length,
1269 istext || mutt_is_application_pgp(b), cd);
1272 mutt_decode_uuencoded(s, b->length,
1273 istext || mutt_is_application_pgp(b), cd);
1276 mutt_decode_xbit(s, b->length,
1277 istext || mutt_is_application_pgp(b), cd);
1281 if (cd != MUTT_ICONV_ERROR)
1285 int mutt_body_handler (BODY * b, STATE * s)
1290 char tempfile[_POSIX_PATH_MAX];
1291 handler_t handler = NULL;
1293 ssize_t tmplength = 0;
1297 int oflags = s->flags;
1298 int tok = mime_which_token(b->subtype, -1);
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 (tok == MIME_PLAIN) {
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 (tok == MIME_ENRICHED)
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))
1332 handler = message_handler;
1333 else if (tok == MIME_DELIVERY_STATUS)
1335 else if (tok == MIME_EXTERNAL_BODY)
1336 handler = external_body_handler;
1338 else if (b->type == TYPEMULTIPART) {
1341 if (tok == MIME_ALTERNATIVE)
1342 handler = alternative_handler;
1343 else if (tok == MIME_SIGNED) {
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 (tok == MIME_ENCRYPTED) {
1353 p = parameter_getval(b->parameter, "protocol");
1356 mutt_error(_("Error: multipart/encrypted has no protocol parameter!"));
1357 else if (mime_which_token(p, -1) == MIME_APPLICATION_PGP_ENCRYPTED)
1358 handler = crypt_pgp_encrypted_handler;
1362 handler = multipart_handler;
1364 else if (b->type == TYPEAPPLICATION) {
1365 if (mutt_is_application_pgp (b))
1366 handler = crypt_pgp_application_pgp_handler;
1367 if (mutt_is_application_smime (b))
1368 handler = crypt_smime_application_smime_handler;
1371 if (plaintext || handler) {
1372 fseeko (s->fpin, b->offset, 0);
1374 /* see if we need to decode this part before processing it */
1375 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE
1376 || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) {
1377 /* text subtypes may require character set conversion even with 8bit
1379 int origType = b->type;
1380 char *savePrefix = NULL;
1383 /* decode to a tempfile, saving the original destination */
1385 s->fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
1387 mutt_error _("Unable to open temporary file!");
1390 /* decoding the attachment changes the size and offset, so save a copy
1391 * of the "real" values now, and restore them after processing
1393 tmplength = b->length;
1394 tmpoffset = b->offset;
1396 /* if we are decoding binary bodies, we don't want to prefix each
1397 * line with the prefix or else the data will get corrupted.
1399 savePrefix = s->prefix;
1407 mutt_decode_attachment (b, s);
1410 b->length = ftello (s->fpout);
1412 m_fclose(&s->fpout);
1414 /* restore final destination and substitute the tempfile for input */
1417 s->fpin = safe_fopen (tempfile, "r");
1420 /* restore the prefix */
1421 s->prefix = savePrefix;
1427 /* process the (decoded) body part */
1429 rc = handler (b, s);
1432 b->length = tmplength;
1433 b->offset = tmpoffset;
1435 /* restore the original source stream */
1441 else if (s->flags & M_DISPLAY) {
1442 state_mark_attach (s);
1443 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1444 if (!option (OPTVIEWATTACH)) {
1446 (type, sizeof (type),
1447 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1448 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1450 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1452 fputs (" --]\n", s->fpout);