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 static unsigned char decode_byte(int ch)
194 return ch == 96 ? 0 : ch - 32;
197 static void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
200 char linelen, c, l, out;
202 char bufi[BUFI_SIZE];
206 state_set_prefix (s);
209 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
211 len -= m_strlen(tmps);
212 if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
216 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
218 len -= m_strlen(tmps);
219 if (!m_strncmp(tmps, "end", 3))
222 linelen = decode_byte(*pt);
224 for (c = 0; c < linelen;) {
225 for (l = 2; l <= 6; l += 2) {
226 out = decode_byte(*pt) << l;
228 out |= (decode_byte(*pt) >> (6 - l));
234 mutt_convert_to_state (cd, bufi, &k, s);
239 mutt_convert_to_state (cd, bufi, &k, s);
240 mutt_convert_to_state (cd, 0, 0, s);
242 state_reset_prefix (s);
245 /* ----------------------------------------------------------------------------
246 * A (not so) minimal implementation of RFC1563.
249 #define IndentSize (4)
251 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
252 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
253 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
257 const char *tag_name;
260 {"param", RICH_PARAM},
262 {"italic", RICH_ITALIC},
263 {"underline", RICH_UNDERLINE},
264 {"nofill", RICH_NOFILL},
265 {"excerpt", RICH_EXCERPT},
266 {"indent", RICH_INDENT},
267 {"indentright", RICH_INDENT_RIGHT},
268 {"center", RICH_CENTER},
269 {"flushleft", RICH_FLUSHLEFT},
270 {"flushright", RICH_FLUSHRIGHT},
271 {"flushboth", RICH_FLUSHLEFT},
272 {"color", RICH_COLOR},
273 {"x-color", RICH_COLOR},
277 struct enriched_state {
290 int tag_level[RICH_LAST_TAG];
295 static void enriched_wrap (struct enriched_state *stte)
300 if (stte->line_len) {
301 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
302 /* Strip trailing white space */
303 ssize_t y = stte->line_used - 1;
305 while (y && ISSPACE (stte->line[y])) {
306 stte->line[y] = '\0';
311 if (stte->tag_level[RICH_CENTER]) {
312 /* Strip leading whitespace */
315 while (stte->line[y] && ISSPACE (stte->line[y]))
320 for (z = y; z <= stte->line_used; z++) {
321 stte->line[z - y] = stte->line[z];
325 stte->line_used -= y;
330 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
331 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
333 if (stte->tag_level[RICH_CENTER]) {
336 state_putc (' ', stte->s);
340 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
343 state_putc (' ', stte->s);
348 state_puts (stte->line, stte->s);
351 state_putc ('\n', stte->s);
352 stte->line[0] = '\0';
355 stte->indent_len = 0;
356 if (stte->s->prefix) {
357 state_puts (stte->s->prefix, stte->s);
358 stte->indent_len += m_strlen(stte->s->prefix);
361 if (stte->tag_level[RICH_EXCERPT]) {
362 x = stte->tag_level[RICH_EXCERPT];
364 if (stte->s->prefix) {
365 state_puts (stte->s->prefix, stte->s);
366 stte->indent_len += m_strlen(stte->s->prefix);
369 state_puts ("> ", stte->s);
370 stte->indent_len += m_strlen("> ");
376 stte->indent_len = 0;
377 if (stte->tag_level[RICH_INDENT]) {
378 x = stte->tag_level[RICH_INDENT] * IndentSize;
379 stte->indent_len += x;
381 state_putc (' ', stte->s);
387 static void enriched_flush (struct enriched_state *stte, int wrap)
389 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
391 (stte->tag_level[RICH_INDENT_RIGHT] *
392 IndentSize) - stte->indent_len)))
393 enriched_wrap (stte);
395 if (stte->buff_used) {
396 stte->buffer[stte->buff_used] = '\0';
397 stte->line_used += stte->buff_used;
398 if (stte->line_used > stte->line_max) {
399 stte->line_max = stte->line_used;
400 p_realloc(&stte->line, stte->line_max + 1);
402 m_strcat(stte->line, stte->line_max + 1, stte->buffer);
403 stte->line_len += stte->word_len;
408 enriched_wrap (stte);
412 static void enriched_putc (int c, struct enriched_state *stte)
414 if (stte->tag_level[RICH_PARAM]) {
415 if (stte->tag_level[RICH_COLOR]) {
416 if (stte->param_used + 1 >= stte->param_len)
417 p_realloc(&stte->param, (stte->param_len += STRING));
419 stte->param[stte->param_used++] = c;
421 return; /* nothing to do */
424 /* see if more space is needed (plus extra for possible rich characters) */
425 if (stte->buff_len < stte->buff_used + 3) {
426 stte->buff_len += LONG_STRING;
427 p_realloc(&stte->buffer, stte->buff_len + 1);
430 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
432 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
436 stte->buffer[stte->buff_used++] = c;
437 enriched_flush (stte, 0);
440 if (stte->s->flags & M_DISPLAY) {
441 if (stte->tag_level[RICH_BOLD]) {
442 stte->buffer[stte->buff_used++] = c;
443 stte->buffer[stte->buff_used++] = '\010';
444 stte->buffer[stte->buff_used++] = c;
446 else if (stte->tag_level[RICH_UNDERLINE]) {
448 stte->buffer[stte->buff_used++] = '_';
449 stte->buffer[stte->buff_used++] = '\010';
450 stte->buffer[stte->buff_used++] = c;
452 else if (stte->tag_level[RICH_ITALIC]) {
453 stte->buffer[stte->buff_used++] = c;
454 stte->buffer[stte->buff_used++] = '\010';
455 stte->buffer[stte->buff_used++] = '_';
458 stte->buffer[stte->buff_used++] = c;
461 stte->buffer[stte->buff_used++] = c;
467 static void enriched_puts (const char *s, struct enriched_state *stte)
471 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
472 stte->buff_len += LONG_STRING;
473 p_realloc(&stte->buffer, stte->buff_len + 1);
478 stte->buffer[stte->buff_used++] = *p++;
482 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
484 const char *tagptr = tag;
490 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
491 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
492 j = EnrichedTags[i].index;
497 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
498 enriched_flush (stte, 1);
501 if (stte->tag_level[j]) /* make sure not to go negative */
502 stte->tag_level[j]--;
503 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
504 && stte->tag_level[RICH_COLOR]) {
505 stte->param[stte->param_used] = '\0';
506 if (!ascii_strcasecmp (stte->param, "black")) {
507 enriched_puts ("\033[30m", stte);
509 else if (!ascii_strcasecmp (stte->param, "red")) {
510 enriched_puts ("\033[31m", stte);
512 else if (!ascii_strcasecmp (stte->param, "green")) {
513 enriched_puts ("\033[32m", stte);
515 else if (!ascii_strcasecmp (stte->param, "yellow")) {
516 enriched_puts ("\033[33m", stte);
518 else if (!ascii_strcasecmp (stte->param, "blue")) {
519 enriched_puts ("\033[34m", stte);
521 else if (!ascii_strcasecmp (stte->param, "magenta")) {
522 enriched_puts ("\033[35m", stte);
524 else if (!ascii_strcasecmp (stte->param, "cyan")) {
525 enriched_puts ("\033[36m", stte);
527 else if (!ascii_strcasecmp (stte->param, "white")) {
528 enriched_puts ("\033[37m", stte);
531 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
532 enriched_puts ("\033[0m", stte);
535 /* flush parameter buffer when closing the tag */
536 if (j == RICH_PARAM) {
537 stte->param_used = 0;
538 stte->param[0] = '\0';
542 stte->tag_level[j]++;
544 if (j == RICH_EXCERPT)
545 enriched_flush (stte, 1);
549 static int text_enriched_handler (BODY * a, STATE * s)
552 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
555 long bytes = a->length;
556 struct enriched_state stte;
559 char tag[LONG_STRING + 1];
563 stte.WrapMargin = ((s->flags & M_DISPLAY) ? (getmaxx(main_w) - 4) :
564 ((getmaxx(main_w) - 4) < 72) ? (getmaxx(main_w) - 4) : 72);
565 stte.line_max = stte.WrapMargin * 4;
566 stte.line = p_new(char, stte.line_max + 1);
567 stte.param = p_new(char, STRING);
569 stte.param_len = STRING;
573 state_puts (s->prefix, s);
574 stte.indent_len += m_strlen(s->prefix);
577 while (state != DONE) {
578 if (state != ST_EOF) {
579 if (!bytes || (c = fgetc (s->fpin)) == EOF)
593 if (stte.tag_level[RICH_NOFILL]) {
594 enriched_flush (&stte, 1);
597 enriched_putc (' ', &stte);
603 enriched_putc (c, &stte);
609 enriched_putc (c, &stte);
617 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
621 enriched_set_flags (tag, &stte);
624 else if (tag_len < LONG_STRING) /* ignore overly long tags */
637 enriched_flush (&stte, 1);
646 enriched_putc ('\0', &stte);
647 enriched_flush (&stte, 1);
651 case DONE: /* not reached, but gcc complains if this is absent */
656 state_putc ('\n', s); /* add a final newline */
658 p_delete(&(stte.buffer));
659 p_delete(&(stte.line));
660 p_delete(&(stte.param));
667 #define TXTENRICHED 3
669 static int alternative_handler (BODY * a, STATE * s)
679 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
680 a->encoding == ENCUUENCODED) {
684 fstat (fileno (s->fpin), &st);
686 b->length = (long) st.st_size;
687 b->parts = mutt_parse_multipart(s->fpin,
688 parameter_getval(a->parameter, "boundary"),
690 mime_which_token(a->subtype, -1) == MIME_DIGEST);
697 /* First, search list of prefered types */
698 t = AlternativeOrderList;
699 while (t && !choice) {
701 int btlen; /* length of basetype */
702 int wild; /* do we have a wildcard to match all subtypes? */
704 c = strchr (t->data, '/');
706 wild = (c[1] == '*' && c[2] == 0);
711 btlen = m_strlen(t->data);
719 const char *bt = TYPE (b);
721 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
722 /* the basetype matches */
723 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
732 /* Next, look for an autoviewable type */
739 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
740 if (mutt_is_autoview (b, buf)) {
741 rfc1524_entry *entry = rfc1524_entry_new();
743 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
746 rfc1524_entry_delete(&entry);
752 /* Then, look for a text entry */
759 if (b->type == TYPETEXT) {
760 switch (mime_which_token(b->subtype, -1)) {
762 if (type <= TXTPLAIN) {
768 if (type <= TXTENRICHED) {
774 if (type <= TXTHTML) {
787 /* Finally, look for other possibilities */
794 if (mutt_can_decode (b))
801 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
802 fseeko (s->fpin, choice->hdr_offset, 0);
803 mutt_copy_bytes (s->fpin, s->fpout,
804 choice->offset - choice->hdr_offset);
806 mutt_body_handler (choice, s);
808 else if (s->flags & M_DISPLAY) {
809 /* didn't find anything that we could display! */
810 state_mark_attach (s);
811 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
821 /* handles message/rfc822 body parts */
822 static int message_handler (BODY * a, STATE * s)
829 off_start = ftello (s->fpin);
830 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
831 a->encoding == ENCUUENCODED) {
832 fstat (fileno (s->fpin), &st);
834 b->length = (long) st.st_size;
835 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
841 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
842 (((s->flags & M_WEED)
843 || ((s->flags & (M_DISPLAY | M_PRINTING))
844 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
845 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
849 state_puts (s->prefix, s);
850 state_putc ('\n', s);
852 rc = mutt_body_handler (b->parts, s);
855 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
856 a->encoding == ENCUUENCODED)
862 /* returns 1 if decoding the attachment will produce output */
863 int mutt_can_decode (BODY * a)
867 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
868 if (mutt_is_autoview (a, type))
869 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
870 else if (a->type == TYPETEXT)
872 else if (a->type == TYPEMESSAGE)
874 else if (a->type == TYPEMULTIPART) {
876 int tok = mime_which_token(a->subtype, -1);
878 if (tok == MIME_SIGNED || tok == MIME_ENCRYPTED)
881 for (p = a->parts; p; p = p->next) {
882 if (mutt_can_decode (p))
886 else if (a->type == TYPEAPPLICATION) {
887 if (mutt_is_application_pgp(a))
889 if (mutt_is_application_smime (a))
896 static int multipart_handler (BODY * a, STATE * s)
904 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
905 a->encoding == ENCUUENCODED) {
906 fstat (fileno (s->fpin), &st);
908 b->length = (long) st.st_size;
909 b->parts = mutt_parse_multipart(s->fpin,
910 parameter_getval(a->parameter, "boundary"),
912 mime_which_token(a->subtype, -1) == MIME_DIGEST);
917 for (p = b->parts, count = 1; p; p = p->next, count++) {
918 if (s->flags & M_DISPLAY) {
919 state_mark_attach (s);
920 state_printf (s, _("[-- Attachment #%d"), count);
921 if (p->description || p->filename || p->form_name) {
922 state_puts (": ", s);
923 state_puts (p->description ? p->description :
924 p->filename ? p->filename : p->form_name, s);
926 state_puts (" --]\n", s);
928 mutt_pretty_size (length, sizeof (length), p->length);
930 state_mark_attach (s);
931 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
932 TYPE (p), p->subtype, ENCODING (p->encoding), length);
933 if (!option (OPTWEED)) {
934 fseeko (s->fpin, p->hdr_offset, 0);
935 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
938 state_putc ('\n', s);
941 if (p->description && mutt_can_decode (p))
942 state_printf (s, "Content-Description: %s\n", p->description);
945 state_printf (s, "%s: \n", p->form_name);
948 rc = mutt_body_handler (p, s);
949 state_putc ('\n', s);
954 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
955 a->encoding == ENCUUENCODED)
961 static int autoview_handler (BODY * a, STATE * s)
963 rfc1524_entry *entry = rfc1524_entry_new();
964 char buffer[LONG_STRING];
966 char command[LONG_STRING];
967 char tempfile[_POSIX_PATH_MAX] = "";
976 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
977 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
979 fname = m_strdup(a->filename);
980 mutt_sanitize_filename (fname, 1);
981 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
985 if (entry->command) {
986 m_strcpy(command, sizeof(command), entry->command);
988 /* rfc1524_expand_command returns 0 if the file is required */
990 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
992 if (s->flags & M_DISPLAY) {
993 state_mark_attach (s);
994 state_printf (s, _("[-- Autoview using %s --]\n"), command);
995 mutt_message (_("Invoking autoview command: %s"), command);
998 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
999 mutt_perror ("fopen");
1000 rfc1524_entry_delete(&entry);
1004 mutt_copy_bytes (s->fpin, fpin, a->length);
1008 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1013 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1014 fileno (fpin), -1, -1);
1018 mutt_perror (_("Can't create filter"));
1020 if (s->flags & M_DISPLAY) {
1021 state_mark_attach (s);
1022 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1029 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1030 state_puts (s->prefix, s);
1031 state_puts (buffer, s);
1033 /* check for data on stderr */
1034 if (fgets (buffer, sizeof (buffer), fperr)) {
1035 if (s->flags & M_DISPLAY) {
1036 state_mark_attach (s);
1037 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1040 state_puts (s->prefix, s);
1041 state_puts (buffer, s);
1042 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1043 state_puts (s->prefix, s);
1044 state_puts (buffer, s);
1048 mutt_copy_stream (fpout, s->fpout);
1049 /* Check for stderr messages */
1050 if (fgets (buffer, sizeof (buffer), fperr)) {
1051 if (s->flags & M_DISPLAY) {
1052 state_mark_attach (s);
1053 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1056 state_puts (buffer, s);
1057 mutt_copy_stream (fperr, s->fpout);
1065 mutt_wait_filter (thepid);
1069 mutt_unlink (tempfile);
1071 if (s->flags & M_DISPLAY)
1072 mutt_clear_error ();
1074 rfc1524_entry_delete(&entry);
1078 static int external_body_handler (BODY * b, STATE * s)
1080 const char *access_type;
1081 const char *expiration;
1084 access_type = parameter_getval(b->parameter, "access-type");
1086 if (s->flags & M_DISPLAY) {
1087 state_mark_attach (s);
1088 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1093 expiration = parameter_getval(b->parameter, "expiration");
1095 expire = mutt_parse_date (expiration, NULL);
1099 if (mime_which_token(access_type, -1) == MIME_X_MUTT_DELETED) {
1100 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1102 char pretty_size[10];
1104 state_mark_attach (s);
1105 state_printf (s, _("[-- This %s/%s attachment "),
1106 TYPE (b->parts), b->parts->subtype);
1107 length = parameter_getval(b->parameter, "length");
1109 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1110 strtol (length, NULL, 10));
1111 state_printf (s, _("(size %s bytes) "), pretty_size);
1113 state_puts (_("has been deleted --]\n"), s);
1116 state_mark_attach (s);
1117 state_printf (s, _("[-- on %s --]\n"), expiration);
1119 if (b->parts->filename) {
1120 state_mark_attach (s);
1121 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1124 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1125 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1129 else if (expiration && expire < time (NULL)) {
1130 if (s->flags & M_DISPLAY) {
1131 state_mark_attach (s);
1132 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1133 TYPE (b->parts), b->parts->subtype);
1134 state_attach_puts (_("[-- and the indicated external source has --]\n"
1135 "[-- expired. --]\n"), s);
1137 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1138 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1142 if (s->flags & M_DISPLAY) {
1143 state_mark_attach (s);
1145 _("[-- This %s/%s attachment is not included, --]\n"),
1146 TYPE (b->parts), b->parts->subtype);
1147 state_mark_attach (s);
1150 ("[-- and the indicated access-type %s is unsupported --]\n"),
1152 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1153 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1160 static void mutt_decode_base64(STATE *s, long len, int istext, iconv_t cd)
1163 int c1, c2, c3, c4, ch, cr = 0, i;
1164 char bufi[BUFI_SIZE];
1170 state_set_prefix (s);
1173 for (i = 0; i < 4 && len > 0; len--) {
1174 if ((ch = fgetc (s->fpin)) == EOF)
1176 if (base64val(ch) >= 0 || ch == '=')
1183 c1 = base64val(buf[0]);
1184 c2 = base64val(buf[1]);
1185 ch = (c1 << 2) | (c2 >> 4);
1187 if (cr && ch != '\n')
1192 if (istext && ch == '\r')
1199 c3 = base64val(buf[2]);
1200 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
1202 if (cr && ch != '\n')
1207 if (istext && ch == '\r')
1214 c4 = base64val(buf[3]);
1215 ch = ((c3 & 0x3) << 6) | c4;
1217 if (cr && ch != '\n')
1221 if (istext && ch == '\r')
1226 if (l + 8 >= ssizeof (bufi))
1227 mutt_convert_to_state (cd, bufi, &l, s);
1233 mutt_convert_to_state (cd, bufi, &l, s);
1234 mutt_convert_to_state (cd, 0, 0, s);
1236 state_reset_prefix (s);
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);