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.
22 #include "mutt_curses.h"
28 #include "mutt_crypt.h"
36 #include "lib/debug.h"
38 typedef void handler_f (BODY *, STATE *);
39 typedef handler_f *handler_t;
41 int Index_hex[128] = {
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
46 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
56 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
57 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
58 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
59 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
60 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
63 void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
72 while ((c = fgetc (s->fpin)) != EOF && len--) {
73 if (c == '\r' && len) {
74 if ((ch = fgetc (s->fpin)) == '\n') {
83 if (l == sizeof (bufi))
84 mutt_convert_to_state (cd, bufi, &l, s);
87 mutt_convert_to_state (cd, bufi, &l, s);
88 mutt_convert_to_state (cd, 0, 0, s);
90 state_reset_prefix (s);
93 mutt_copy_bytes (s->fpin, s->fpout, len);
96 static int qp_decode_triple (char *s, char *d)
99 if (*s == '=' && !(*(s + 1)))
102 /* quoted-printable triple */
104 isxdigit ((unsigned char) *(s + 1)) &&
105 isxdigit ((unsigned char) *(s + 2))) {
106 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
114 static void qp_decode_line (char *dest, char *src, size_t * l, int last)
122 /* decode the line */
124 for (d = dest, s = src; *s;) {
125 switch ((kind = qp_decode_triple (s, &c))) {
129 break; /* qp triple */
132 break; /* single character */
136 break; /* soft line break */
140 if (!soft && last == '\n')
148 * Decode an attachment encoded with quoted-printable.
150 * Why doesn't this overflow any buffers? First, it's guaranteed
151 * that the length of a line grows when you _en_-code it to
152 * quoted-printable. That means that we always can store the
153 * result in a buffer of at most the _same_ size.
155 * Now, we don't special-case if the line we read with fgets()
156 * isn't terminated. We don't care about this, since STRING > 78,
157 * so corrupted input will just be corrupted a bit more. That
158 * implies that STRING+1 bytes are always sufficient to store the
159 * result of qp_decode_line.
161 * Finally, at soft line breaks, some part of a multibyte character
162 * may have been left over by mutt_convert_to_state(). This shouldn't
163 * be more than 6 characters, so STRING + 7 should be sufficient
164 * memory to store the decoded data.
166 * Just to make sure that I didn't make some off-by-one error
167 * above, we just use STRING*2 for the target buffer's size.
171 void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
174 char decline[2 * STRING];
176 size_t linelen; /* number of input bytes in `line' */
179 int last; /* store the last character in the input line */
182 state_set_prefix (s);
188 * It's ok to use a fixed size buffer for input, even if the line turns
189 * out to be longer than this. Just process the line in chunks. This
190 * really shouldn't happen according the MIME spec, since Q-P encoded
191 * lines are at most 76 characters, but we should be liberal about what
194 if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
197 linelen = str_len (line);
201 * inspect the last character we read so we can tell if we got the
204 last = linelen ? line[linelen - 1] : 0;
206 /* chop trailing whitespace if we got the full line */
208 while (linelen > 0 && ISSPACE (line[linelen - 1]))
213 /* decode and do character set conversion */
214 qp_decode_line (decline + l, line, &l3, last);
216 mutt_convert_to_state (cd, decline, &l, s);
219 mutt_convert_to_state (cd, 0, 0, s);
220 state_reset_prefix (s);
223 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
226 int c1, c2, c3, c4, ch, cr = 0, i;
227 char bufi[BUFI_SIZE];
233 state_set_prefix (s);
236 for (i = 0; i < 4 && len > 0; len--) {
237 if ((ch = fgetc (s->fpin)) == EOF)
239 if (ch >= 0 && ch < 128 && (base64val (ch) != -1 || ch == '='))
243 debug_print (2, ("didn't get a multiple of 4 chars.\n"));
247 c1 = base64val (buf[0]);
248 c2 = base64val (buf[1]);
249 ch = (c1 << 2) | (c2 >> 4);
251 if (cr && ch != '\n')
256 if (istext && ch == '\r')
263 c3 = base64val (buf[2]);
264 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
266 if (cr && ch != '\n')
271 if (istext && ch == '\r')
278 c4 = base64val (buf[3]);
279 ch = ((c3 & 0x3) << 6) | c4;
281 if (cr && ch != '\n')
285 if (istext && ch == '\r')
290 if (l + 8 >= sizeof (bufi))
291 mutt_convert_to_state (cd, bufi, &l, s);
297 mutt_convert_to_state (cd, bufi, &l, s);
298 mutt_convert_to_state (cd, 0, 0, s);
300 state_reset_prefix (s);
303 unsigned char decode_byte (char ch)
310 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
312 char tmps[SHORT_STRING];
313 char linelen, c, l, out;
315 char bufi[BUFI_SIZE];
319 state_set_prefix (s);
322 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
324 len -= str_len (tmps);
325 if ((!str_ncmp (tmps, "begin", 5)) && ISSPACE (tmps[5]))
329 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
331 len -= str_len (tmps);
332 if (!str_ncmp (tmps, "end", 3))
335 linelen = decode_byte (*pt);
337 for (c = 0; c < linelen;) {
338 for (l = 2; l <= 6; l += 2) {
339 out = decode_byte (*pt) << l;
341 out |= (decode_byte (*pt) >> (6 - l));
347 mutt_convert_to_state (cd, bufi, &k, s);
352 mutt_convert_to_state (cd, bufi, &k, s);
353 mutt_convert_to_state (cd, 0, 0, s);
355 state_reset_prefix (s);
358 /* ----------------------------------------------------------------------------
359 * A (not so) minimal implementation of RFC1563.
362 #define IndentSize (4)
364 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
365 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
366 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
370 const char *tag_name;
374 "param", RICH_PARAM}, {
375 "bold", RICH_BOLD}, {
376 "italic", RICH_ITALIC}, {
377 "underline", RICH_UNDERLINE}, {
378 "nofill", RICH_NOFILL}, {
379 "excerpt", RICH_EXCERPT}, {
380 "indent", RICH_INDENT}, {
381 "indentright", RICH_INDENT_RIGHT}, {
382 "center", RICH_CENTER}, {
383 "flushleft", RICH_FLUSHLEFT}, {
384 "flushright", RICH_FLUSHRIGHT}, {
385 "flushboth", RICH_FLUSHLEFT}, {
386 "color", RICH_COLOR}, {
387 "x-color", RICH_COLOR}, {
391 struct enriched_state {
404 int tag_level[RICH_LAST_TAG];
409 static void enriched_wrap (struct enriched_state *stte)
414 if (stte->line_len) {
415 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
416 /* Strip trailing white space */
417 size_t y = stte->line_used - 1;
419 while (y && ISSPACE (stte->line[y])) {
420 stte->line[y] = '\0';
425 if (stte->tag_level[RICH_CENTER]) {
426 /* Strip leading whitespace */
429 while (stte->line[y] && ISSPACE (stte->line[y]))
434 for (z = y; z <= stte->line_used; z++) {
435 stte->line[z - y] = stte->line[z];
439 stte->line_used -= y;
444 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
445 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
447 if (stte->tag_level[RICH_CENTER]) {
450 state_putc (' ', stte->s);
454 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
457 state_putc (' ', stte->s);
462 state_puts (stte->line, stte->s);
465 state_putc ('\n', stte->s);
466 stte->line[0] = '\0';
469 stte->indent_len = 0;
470 if (stte->s->prefix) {
471 state_puts (stte->s->prefix, stte->s);
472 stte->indent_len += str_len (stte->s->prefix);
475 if (stte->tag_level[RICH_EXCERPT]) {
476 x = stte->tag_level[RICH_EXCERPT];
478 if (stte->s->prefix) {
479 state_puts (stte->s->prefix, stte->s);
480 stte->indent_len += str_len (stte->s->prefix);
483 state_puts ("> ", stte->s);
484 stte->indent_len += str_len ("> ");
490 stte->indent_len = 0;
491 if (stte->tag_level[RICH_INDENT]) {
492 x = stte->tag_level[RICH_INDENT] * IndentSize;
493 stte->indent_len += x;
495 state_putc (' ', stte->s);
501 static void enriched_flush (struct enriched_state *stte, int wrap)
503 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
505 (stte->tag_level[RICH_INDENT_RIGHT] *
506 IndentSize) - stte->indent_len)))
507 enriched_wrap (stte);
509 if (stte->buff_used) {
510 stte->buffer[stte->buff_used] = '\0';
511 stte->line_used += stte->buff_used;
512 if (stte->line_used > stte->line_max) {
513 stte->line_max = stte->line_used;
514 mem_realloc (&stte->line, stte->line_max + 1);
516 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
517 stte->line_len += stte->word_len;
522 enriched_wrap (stte);
526 static void enriched_putc (int c, struct enriched_state *stte)
528 if (stte->tag_level[RICH_PARAM]) {
529 if (stte->tag_level[RICH_COLOR]) {
530 if (stte->param_used + 1 >= stte->param_len)
531 mem_realloc (&stte->param, (stte->param_len += STRING));
533 stte->param[stte->param_used++] = c;
535 return; /* nothing to do */
538 /* see if more space is needed (plus extra for possible rich characters) */
539 if (stte->buff_len < stte->buff_used + 3) {
540 stte->buff_len += LONG_STRING;
541 mem_realloc (&stte->buffer, stte->buff_len + 1);
544 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
546 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
550 stte->buffer[stte->buff_used++] = c;
551 enriched_flush (stte, 0);
554 if (stte->s->flags & M_DISPLAY) {
555 if (stte->tag_level[RICH_BOLD]) {
556 stte->buffer[stte->buff_used++] = c;
557 stte->buffer[stte->buff_used++] = '\010';
558 stte->buffer[stte->buff_used++] = c;
560 else if (stte->tag_level[RICH_UNDERLINE]) {
562 stte->buffer[stte->buff_used++] = '_';
563 stte->buffer[stte->buff_used++] = '\010';
564 stte->buffer[stte->buff_used++] = c;
566 else if (stte->tag_level[RICH_ITALIC]) {
567 stte->buffer[stte->buff_used++] = c;
568 stte->buffer[stte->buff_used++] = '\010';
569 stte->buffer[stte->buff_used++] = '_';
572 stte->buffer[stte->buff_used++] = c;
576 stte->buffer[stte->buff_used++] = c;
582 static void enriched_puts (char *s, struct enriched_state *stte)
586 if (stte->buff_len < stte->buff_used + str_len (s)) {
587 stte->buff_len += LONG_STRING;
588 mem_realloc (&stte->buffer, stte->buff_len + 1);
592 stte->buffer[stte->buff_used++] = *c;
597 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
599 const char *tagptr = tag;
605 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
606 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
607 j = EnrichedTags[i].index;
612 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
613 enriched_flush (stte, 1);
616 if (stte->tag_level[j]) /* make sure not to go negative */
617 stte->tag_level[j]--;
618 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
619 && stte->tag_level[RICH_COLOR]) {
620 stte->param[stte->param_used] = '\0';
621 if (!ascii_strcasecmp (stte->param, "black")) {
622 enriched_puts ("\033[30m", stte);
624 else if (!ascii_strcasecmp (stte->param, "red")) {
625 enriched_puts ("\033[31m", stte);
627 else if (!ascii_strcasecmp (stte->param, "green")) {
628 enriched_puts ("\033[32m", stte);
630 else if (!ascii_strcasecmp (stte->param, "yellow")) {
631 enriched_puts ("\033[33m", stte);
633 else if (!ascii_strcasecmp (stte->param, "blue")) {
634 enriched_puts ("\033[34m", stte);
636 else if (!ascii_strcasecmp (stte->param, "magenta")) {
637 enriched_puts ("\033[35m", stte);
639 else if (!ascii_strcasecmp (stte->param, "cyan")) {
640 enriched_puts ("\033[36m", stte);
642 else if (!ascii_strcasecmp (stte->param, "white")) {
643 enriched_puts ("\033[37m", stte);
646 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
647 enriched_puts ("\033[0m", stte);
650 /* flush parameter buffer when closing the tag */
651 if (j == RICH_PARAM) {
652 stte->param_used = 0;
653 stte->param[0] = '\0';
657 stte->tag_level[j]++;
659 if (j == RICH_EXCERPT)
660 enriched_flush (stte, 1);
664 void text_enriched_handler (BODY * a, STATE * s)
667 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
670 long bytes = a->length;
671 struct enriched_state stte;
674 char tag[LONG_STRING + 1];
676 memset (&stte, 0, sizeof (stte));
679 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
680 72) ? (COLS - 4) : 72);
681 stte.line_max = stte.WrapMargin * 4;
682 stte.line = (char *) mem_calloc (1, stte.line_max + 1);
683 stte.param = (char *) mem_calloc (1, STRING);
685 stte.param_len = STRING;
689 state_puts (s->prefix, s);
690 stte.indent_len += str_len (s->prefix);
693 while (state != DONE) {
694 if (state != ST_EOF) {
695 if (!bytes || (c = fgetc (s->fpin)) == EOF)
709 if (stte.tag_level[RICH_NOFILL]) {
710 enriched_flush (&stte, 1);
713 enriched_putc (' ', &stte);
719 enriched_putc (c, &stte);
725 enriched_putc (c, &stte);
733 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
737 enriched_set_flags (tag, &stte);
740 else if (tag_len < LONG_STRING) /* ignore overly long tags */
753 enriched_flush (&stte, 1);
762 enriched_putc ('\0', &stte);
763 enriched_flush (&stte, 1);
767 case DONE: /* not reached, but gcc complains if this is absent */
772 state_putc ('\n', s); /* add a final newline */
774 mem_free (&(stte.buffer));
775 mem_free (&(stte.line));
776 mem_free (&(stte.param));
780 * An implementation of RFC 2646.
782 * NOTE: This still has to be made UTF-8 aware.
786 #define FLOWED_MAX 77
788 static int get_quote_level (char *line)
792 for (quoted = 0; line[quoted] == '>'; quoted++);
796 static void print_flowed_line (char *line, STATE * s, int ql)
800 int len = str_len (line);
803 if (MaxLineLength > 0) {
804 width = MaxLineLength - WrapMargin - ql - 1;
805 if (option (OPTSTUFFQUOTED))
808 width = MaxLineLength;
811 if (option (OPTMBOXPANE))
812 width = COLS - SidebarWidth - WrapMargin - ql - 1;
814 width = COLS - WrapMargin - ql - 1;
816 if (option (OPTSTUFFQUOTED))
822 /* fprintf(stderr,"print_flowed_line will print `%s' with ql = %d\n",line,ql); */
824 if (str_len (line) == 0) {
825 if (option (OPTQUOTEEMPTY)) {
827 state_puts(s->prefix,s);
828 for (i=0;i<ql;++i) state_putc('>',s);
829 if (option(OPTSTUFFQUOTED))
836 pos = line + ql + width;
838 if (ql > 0 && ISBLANK (*oldpos))
841 /* fprintf(stderr,"oldpos = %p line+len = %p\n",oldpos,line+len); */
843 for (; oldpos < line + len; pos += width) {
844 /* fprintf(stderr,"outer for loop\n"); */
845 if (pos < line + len) { /* only search a new position when we're not over the end of the string w/ pos */
846 /* fprintf(stderr,"if 1\n"); */
848 /* fprintf(stderr,"if 2: good luck! found a space\n"); */
853 /* fprintf(stderr,"if 2: else\n"); */
856 while (pos >= oldpos && !isspace (*pos)) {
857 /* fprintf(stderr,"pos(%p) > oldpos(%p)\n",pos,oldpos); */
861 /* fprintf(stderr,"wow, no space found, searching the other direction\n"); */
863 while (pos < line + len && *pos && !isspace (*pos)) {
864 /* fprintf(stderr,"pos(%p) < line+len(%p)\n",pos,line+len); */
867 /* fprintf(stderr,"found a space pos = %p\n",pos); */
874 /* fprintf(stderr,"if 1 else\n"); */
877 state_puts (s->prefix, s);
878 for (i = 0; i < ql; ++i)
880 if (option (OPTSTUFFQUOTED) && (ql > 0 || s->prefix))
882 state_puts (oldpos, s);
883 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
884 if (pos < line + len)
886 state_putc ('\n', s);
889 /*state_puts(line,s);
890 state_putc('\n',s); */
893 static void text_plain_flowed_handler (BODY * a, STATE * s)
895 int bytes = a->length;
896 char buf[LONG_STRING];
897 char *curline = strdup ("");
899 unsigned int curline_len = 1;
900 unsigned int quotelevel = 0, newql = 0;
903 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
905 bytes -= str_len (buf);
907 newql = get_quote_level (buf);
909 if ((t = strrchr (buf, '\n')) || (t = strrchr (buf, '\r'))) {
911 if (str_len (curline) > 0 && curline[str_len (curline) - 1] == ' '
912 && newql == quotelevel
913 && strcmp (curline + quotelevel, "-- ") != 0) {
914 if (buf[newql] == ' ')
915 curline[str_len (curline) - 1] = '\0';
917 curline = realloc (curline, curline_len + str_len (buf));
918 if (curline_len == 1)
920 curline_len += str_len (buf);
921 str_ncat (curline, curline_len, buf + newql,
922 str_len (buf + newql));
929 print_flowed_line (curline, s, quotelevel);
933 curline = realloc (curline, curline_len + str_len (buf));
934 if (curline_len == 1)
936 curline_len += str_len (buf);
937 str_ncat (curline, curline_len, buf, str_len (buf));
941 else if (bytes == 0) {
942 /* in case there's no line end it's likely the last line
943 * so append to current (if any) */
944 if (buf[newql] == ' ')
945 curline[str_len (curline) - 1] = '\0';
946 curline = realloc (curline, curline_len + str_len (buf));
947 if (curline_len == 1)
949 curline_len += str_len (buf);
950 str_ncat (curline, curline_len, buf + newql,
951 str_len (buf + newql));
956 print_flowed_line (curline, s, quotelevel);
963 #define TXTENRICHED 3
965 static void alternative_handler (BODY * a, STATE * s)
974 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
975 a->encoding == ENCUUENCODED) {
979 fstat (fileno (s->fpin), &st);
980 b = mutt_new_body ();
981 b->length = (long) st.st_size;
982 b->parts = mutt_parse_multipart (s->fpin,
983 mutt_get_parameter ("boundary",
986 ascii_strcasecmp ("digest",
994 /* First, search list of prefered types */
995 t = AlternativeOrderList;
996 while (t && !choice) {
998 int btlen; /* length of basetype */
999 int wild; /* do we have a wildcard to match all subtypes? */
1001 c = strchr (t->data, '/');
1003 wild = (c[1] == '*' && c[2] == 0);
1004 btlen = c - t->data;
1008 btlen = str_len (t->data);
1016 const char *bt = TYPE (b);
1018 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
1019 /* the basetype matches */
1020 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
1029 /* Next, look for an autoviewable type */
1036 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
1037 if (mutt_is_autoview (b, buf)) {
1038 rfc1524_entry *entry = rfc1524_new_entry ();
1040 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
1043 rfc1524_free_entry (&entry);
1049 /* Then, look for a text entry */
1056 if (b->type == TYPETEXT) {
1057 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
1061 else if (!ascii_strcasecmp ("enriched", b->subtype)
1062 && type <= TXTENRICHED) {
1066 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
1075 /* Finally, look for other possibilities */
1082 if (mutt_can_decode (b))
1089 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
1090 fseek (s->fpin, choice->hdr_offset, 0);
1091 mutt_copy_bytes (s->fpin, s->fpout,
1092 choice->offset - choice->hdr_offset);
1094 mutt_body_handler (choice, s);
1096 else if (s->flags & M_DISPLAY) {
1097 /* didn't find anything that we could display! */
1098 state_mark_attach (s);
1100 ("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"),
1105 mutt_free_body (&a);
1108 /* handles message/rfc822 body parts */
1109 void message_handler (BODY * a, STATE * s)
1115 off_start = ftell (s->fpin);
1116 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1117 a->encoding == ENCUUENCODED) {
1118 fstat (fileno (s->fpin), &st);
1119 b = mutt_new_body ();
1120 b->length = (long) st.st_size;
1121 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
1127 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
1128 (((s->flags & M_WEED)
1129 || ((s->flags & (M_DISPLAY | M_PRINTING))
1130 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
1131 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
1135 state_puts (s->prefix, s);
1136 state_putc ('\n', s);
1138 mutt_body_handler (b->parts, s);
1141 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1142 a->encoding == ENCUUENCODED)
1143 mutt_free_body (&b);
1146 /* returns 1 if decoding the attachment will produce output */
1147 int mutt_can_decode (BODY * a)
1151 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1152 if (mutt_is_autoview (a, type))
1153 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
1154 else if (a->type == TYPETEXT)
1156 else if (a->type == TYPEMESSAGE)
1158 else if (a->type == TYPEMULTIPART) {
1162 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
1163 ascii_strcasecmp (a->subtype, "encrypted") == 0)
1167 for (p = a->parts; p; p = p->next) {
1168 if (mutt_can_decode (p))
1173 else if (WithCrypto && a->type == TYPEAPPLICATION) {
1174 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (a))
1176 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (a))
1183 void multipart_handler (BODY * a, STATE * s)
1190 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1191 a->encoding == ENCUUENCODED) {
1192 fstat (fileno (s->fpin), &st);
1193 b = mutt_new_body ();
1194 b->length = (long) st.st_size;
1195 b->parts = mutt_parse_multipart (s->fpin,
1196 mutt_get_parameter ("boundary",
1199 ascii_strcasecmp ("digest",
1205 for (p = b->parts, count = 1; p; p = p->next, count++) {
1206 if (s->flags & M_DISPLAY) {
1207 state_mark_attach (s);
1208 state_printf (s, _("[-- Attachment #%d"), count);
1209 if (p->description || p->filename || p->form_name) {
1210 state_puts (": ", s);
1211 state_puts (p->description ? p->description :
1212 p->filename ? p->filename : p->form_name, s);
1214 state_puts (" --]\n", s);
1216 mutt_pretty_size (length, sizeof (length), p->length);
1218 state_mark_attach (s);
1219 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1220 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1221 if (!option (OPTWEED)) {
1222 fseek (s->fpin, p->hdr_offset, 0);
1223 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1226 state_putc ('\n', s);
1229 if (p->description && mutt_can_decode (p))
1230 state_printf (s, "Content-Description: %s\n", p->description);
1233 state_printf (s, "%s: \n", p->form_name);
1236 mutt_body_handler (p, s);
1237 state_putc ('\n', s);
1238 if ((s->flags & M_REPLYING)
1239 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE))
1243 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1244 a->encoding == ENCUUENCODED)
1245 mutt_free_body (&b);
1248 void autoview_handler (BODY * a, STATE * s)
1250 rfc1524_entry *entry = rfc1524_new_entry ();
1251 char buffer[LONG_STRING];
1253 char command[LONG_STRING];
1254 char tempfile[_POSIX_PATH_MAX] = "";
1262 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1263 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1265 fname = str_dup (a->filename);
1266 mutt_sanitize_filename (fname, 1);
1267 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1271 if (entry->command) {
1272 strfcpy (command, entry->command, sizeof (command));
1274 /* rfc1524_expand_command returns 0 if the file is required */
1276 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1278 if (s->flags & M_DISPLAY) {
1279 state_mark_attach (s);
1280 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1281 mutt_message (_("Invoking autoview command: %s"), command);
1284 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1285 mutt_perror ("fopen");
1286 rfc1524_free_entry (&entry);
1290 mutt_copy_bytes (s->fpin, fpin, a->length);
1293 safe_fclose (&fpin);
1294 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1300 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1301 fileno (fpin), -1, -1);
1305 mutt_perror (_("Can't create filter"));
1307 if (s->flags & M_DISPLAY) {
1308 state_mark_attach (s);
1309 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1315 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1316 state_puts (s->prefix, s);
1317 state_puts (buffer, s);
1319 /* check for data on stderr */
1320 if (fgets (buffer, sizeof (buffer), fperr)) {
1321 if (s->flags & M_DISPLAY) {
1322 state_mark_attach (s);
1323 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1326 state_puts (s->prefix, s);
1327 state_puts (buffer, s);
1328 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1329 state_puts (s->prefix, s);
1330 state_puts (buffer, s);
1335 mutt_copy_stream (fpout, s->fpout);
1336 /* Check for stderr messages */
1337 if (fgets (buffer, sizeof (buffer), fperr)) {
1338 if (s->flags & M_DISPLAY) {
1339 state_mark_attach (s);
1340 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1343 state_puts (buffer, s);
1344 mutt_copy_stream (fperr, s->fpout);
1349 safe_fclose (&fpout);
1350 safe_fclose (&fperr);
1352 mutt_wait_filter (thepid);
1354 safe_fclose (&fpin);
1356 mutt_unlink (tempfile);
1358 if (s->flags & M_DISPLAY)
1359 mutt_clear_error ();
1361 rfc1524_free_entry (&entry);
1364 static void external_body_handler (BODY * b, STATE * s)
1366 const char *access_type;
1367 const char *expiration;
1370 access_type = mutt_get_parameter ("access-type", b->parameter);
1372 if (s->flags & M_DISPLAY) {
1373 state_mark_attach (s);
1375 ("[-- Error: message/external-body has no access-type parameter --]\n"),
1381 expiration = mutt_get_parameter ("expiration", b->parameter);
1383 expire = mutt_parse_date (expiration, NULL);
1387 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1388 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1390 char pretty_size[10];
1392 state_mark_attach (s);
1393 state_printf (s, _("[-- This %s/%s attachment "),
1394 TYPE (b->parts), b->parts->subtype);
1395 length = mutt_get_parameter ("length", b->parameter);
1397 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1398 strtol (length, NULL, 10));
1399 state_printf (s, _("(size %s bytes) "), pretty_size);
1401 state_puts (_("has been deleted --]\n"), s);
1404 state_mark_attach (s);
1405 state_printf (s, _("[-- on %s --]\n"), expiration);
1407 if (b->parts->filename) {
1408 state_mark_attach (s);
1409 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1412 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1413 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1417 else if (expiration && expire < time (NULL)) {
1418 if (s->flags & M_DISPLAY) {
1419 state_mark_attach (s);
1420 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1421 TYPE (b->parts), b->parts->subtype);
1422 state_attach_puts (_("[-- and the indicated external source has --]\n"
1423 "[-- expired. --]\n"), s);
1425 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1426 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1431 if (s->flags & M_DISPLAY) {
1432 state_mark_attach (s);
1434 _("[-- This %s/%s attachment is not included, --]\n"),
1435 TYPE (b->parts), b->parts->subtype);
1436 state_mark_attach (s);
1439 ("[-- and the indicated access-type %s is unsupported --]\n"),
1441 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1442 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1448 void mutt_decode_attachment (BODY * b, STATE * s)
1450 int istext = mutt_is_text_part (b);
1451 iconv_t cd = (iconv_t) (-1);
1456 if (s->flags & M_CHARCONV) {
1457 char *charset = mutt_get_parameter ("charset", b->parameter);
1459 if (!option (OPTSTRICTMIME) && !charset)
1460 charset = mutt_get_first_charset (AssumedCharset);
1461 if (charset && Charset)
1462 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1465 if (b->file_charset)
1466 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1470 fseek (s->fpin, b->offset, 0);
1471 switch (b->encoding) {
1472 case ENCQUOTEDPRINTABLE:
1473 mutt_decode_quoted (s, b->length, istext, cd);
1476 mutt_decode_base64 (s, b->length, istext, cd);
1479 mutt_decode_uuencoded (s, b->length, istext, cd);
1482 mutt_decode_xbit (s, b->length, istext, cd);
1486 if (cd != (iconv_t) (-1))
1490 void mutt_body_handler (BODY * b, STATE * s)
1495 char tempfile[_POSIX_PATH_MAX];
1496 handler_t handler = NULL;
1498 size_t tmplength = 0;
1501 int oflags = s->flags;
1503 /* first determine which handler to use to process this part */
1505 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1506 if (mutt_is_autoview (b, type)) {
1507 rfc1524_entry *entry = rfc1524_new_entry ();
1509 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1510 handler = autoview_handler;
1511 s->flags &= ~M_CHARCONV;
1513 rfc1524_free_entry (&entry);
1515 else if (b->type == TYPETEXT) {
1516 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1517 /* avoid copying this part twice since removing the transfer-encoding is
1518 * the only operation needed.
1520 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1521 handler = crypt_pgp_application_pgp_handler;
1523 if (ascii_strcasecmp
1524 ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1525 handler = text_plain_flowed_handler;
1529 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1530 handler = text_enriched_handler;
1531 else /* text body type without a handler */
1534 else if (b->type == TYPEMESSAGE) {
1535 if (mutt_is_message_type (b->type, b->subtype))
1536 handler = message_handler;
1537 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1539 else if (!ascii_strcasecmp ("external-body", b->subtype))
1540 handler = external_body_handler;
1542 else if (b->type == TYPEMULTIPART) {
1545 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1546 handler = alternative_handler;
1547 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0) {
1548 p = mutt_get_parameter ("protocol", b->parameter);
1551 mutt_error (_("Error: multipart/signed has no protocol."));
1553 else if (s->flags & M_VERIFY)
1554 handler = mutt_signed_handler;
1556 else if ((WithCrypto & APPLICATION_PGP)
1557 && str_casecmp ("encrypted", b->subtype) == 0) {
1558 p = mutt_get_parameter ("protocol", b->parameter);
1562 ("Error: multipart/encrypted has no protocol parameter!"));
1564 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1565 handler = crypt_pgp_encrypted_handler;
1569 handler = multipart_handler;
1571 else if (WithCrypto && b->type == TYPEAPPLICATION) {
1572 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1573 handler = crypt_pgp_application_pgp_handler;
1574 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (b))
1575 handler = crypt_smime_application_smime_handler;
1579 if (plaintext || handler) {
1580 fseek (s->fpin, b->offset, 0);
1582 /* see if we need to decode this part before processing it */
1583 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1585 * set conversion even
1586 * with 8bit encoding.
1588 int origType = b->type;
1589 char *savePrefix = NULL;
1592 /* decode to a tempfile, saving the original destination */
1594 mutt_mktemp (tempfile);
1595 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1596 mutt_error _("Unable to open temporary file!");
1600 /* decoding the attachment changes the size and offset, so save a copy
1601 * of the "real" values now, and restore them after processing
1603 tmplength = b->length;
1604 tmpoffset = b->offset;
1606 /* if we are decoding binary bodies, we don't want to prefix each
1607 * line with the prefix or else the data will get corrupted.
1609 savePrefix = s->prefix;
1617 mutt_decode_attachment (b, s);
1620 b->length = ftell (s->fpout);
1624 /* restore final destination and substitute the tempfile for input */
1627 s->fpin = safe_fopen (tempfile, "r");
1630 /* restore the prefix */
1631 s->prefix = savePrefix;
1637 /* process the (decoded) body part */
1642 b->length = tmplength;
1643 b->offset = tmpoffset;
1645 /* restore the original source stream */
1650 s->flags |= M_FIRSTDONE;
1652 else if (s->flags & M_DISPLAY) {
1653 state_mark_attach (s);
1654 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1655 if (!option (OPTVIEWATTACH)) {
1657 (type, sizeof (type),
1658 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1659 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1661 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1663 fputs (" --]\n", s->fpout);
1667 s->flags = oflags | (s->flags & M_FIRSTDONE);