simplifications around mutt_is_message_type
[apps/madmutt.git] / handler.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
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.
8  */
9
10 #include <lib-lib/lib-lib.h>
11
12 #include <lib-mime/mime.h>
13 #include <lib-mime/rfc3676.h>
14
15 #include <lib-ui/curses.h>
16
17 #include <lib-sys/unix.h>
18
19 #include "mutt.h"
20 #include "recvattach.h"
21 #include "handler.h"
22 #include "keymap.h"
23 #include "copy.h"
24 #include "charset.h"
25 #include <lib-crypt/crypt.h>
26 #include "state.h"
27 #include "attach.h"
28 #include "lib.h"
29
30
31 typedef int handler_f (BODY *, STATE *);
32 typedef handler_f *handler_t;
33
34 static void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
35 {
36   int c, ch;
37   char bufi[BUFI_SIZE];
38   ssize_t l = 0;
39
40   if (istext) {
41     state_set_prefix (s);
42
43     while ((c = fgetc (s->fpin)) != EOF && len--) {
44       if (c == '\r' && len) {
45         if ((ch = fgetc (s->fpin)) == '\n') {
46           c = ch;
47           len--;
48         }
49         else
50           ungetc (ch, s->fpin);
51       }
52
53       bufi[l++] = c;
54       if (l == sizeof (bufi))
55         mutt_convert_to_state (cd, bufi, &l, s);
56     }
57
58     mutt_convert_to_state (cd, bufi, &l, s);
59     mutt_convert_to_state (cd, 0, 0, s);
60
61     state_reset_prefix (s);
62   }
63   else
64     mutt_copy_bytes (s->fpin, s->fpout, len);
65 }
66
67 static int qp_decode_triple (char *s, char *d)
68 {
69   /* soft line break */
70   if (*s == '=' && !(*(s + 1)))
71     return 1;
72
73   /* quoted-printable triple */
74   if (*s == '=' &&
75       isxdigit ((unsigned char) *(s + 1)) &&
76       isxdigit ((unsigned char) *(s + 2))) {
77     *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
78     return 0;
79   }
80
81   /* something else */
82   return -1;
83 }
84
85 static void qp_decode_line (char *dest, char *src, ssize_t * l, int last)
86 {
87   char *d, *s;
88   char c;
89
90   int kind;
91   int soft = 0;
92
93   /* decode the line */
94
95   for (d = dest, s = src; *s;) {
96     switch ((kind = qp_decode_triple (s, &c))) {
97     case 0:
98       *d++ = c;
99       s += 3;
100       break;                    /* qp triple */
101     case -1:
102       *d++ = *s++;
103       break;                    /* single character */
104     case 1:
105       soft = 1;
106       s++;
107       break;                    /* soft line break */
108     }
109   }
110
111   if (!soft && last == '\n')
112     *d++ = '\n';
113
114   *d = '\0';
115   *l = d - dest;
116 }
117
118 /* 
119  * Decode an attachment encoded with quoted-printable.
120  * 
121  * Why doesn't this overflow any buffers?  First, it's guaranteed
122  * that the length of a line grows when you _en_-code it to
123  * quoted-printable.  That means that we always can store the
124  * result in a buffer of at most the _same_ size.
125  * 
126  * Now, we don't special-case if the line we read with fgets()
127  * isn't terminated.  We don't care about this, since STRING > 78,
128  * so corrupted input will just be corrupted a bit more.  That
129  * implies that STRING+1 bytes are always sufficient to store the
130  * result of qp_decode_line.
131  * 
132  * Finally, at soft line breaks, some part of a multibyte character
133  * may have been left over by mutt_convert_to_state().  This shouldn't
134  * be more than 6 characters, so STRING + 7 should be sufficient
135  * memory to store the decoded data.
136  * 
137  * Just to make sure that I didn't make some off-by-one error
138  * above, we just use STRING*2 for the target buffer's size.
139  * 
140  */
141
142 static void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
143 {
144   char line[STRING];
145   char decline[2 * STRING];
146   ssize_t l = 0;
147   ssize_t linelen;               /* number of input bytes in `line' */
148   ssize_t l3;
149
150   int last;                     /* store the last character in the input line */
151
152   if (istext)
153     state_set_prefix (s);
154
155   while (len > 0) {
156     last = 0;
157
158     /*
159      * It's ok to use a fixed size buffer for input, even if the line turns
160      * out to be longer than this.  Just process the line in chunks.  This
161      * really shouldn't happen according the MIME spec, since Q-P encoded
162      * lines are at most 76 characters, but we should be liberal about what
163      * we accept.
164      */
165     if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
166       break;
167
168     linelen = m_strlen(line);
169     len -= linelen;
170
171     /*
172      * inspect the last character we read so we can tell if we got the
173      * entire line.
174      */
175     last = linelen ? line[linelen - 1] : 0;
176
177     /* chop trailing whitespace if we got the full line */
178     if (last == '\n') {
179       while (linelen > 0 && ISSPACE (line[linelen - 1]))
180         linelen--;
181       line[linelen] = 0;
182     }
183
184     /* decode and do character set conversion */
185     qp_decode_line (decline + l, line, &l3, last);
186     l += l3;
187     mutt_convert_to_state (cd, decline, &l, s);
188   }
189
190   mutt_convert_to_state (cd, 0, 0, s);
191   state_reset_prefix (s);
192 }
193
194 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
195 {
196   char buf[5];
197   int c1, c2, c3, c4, ch, cr = 0, i;
198   char bufi[BUFI_SIZE];
199   ssize_t l = 0;
200
201   buf[4] = 0;
202
203   if (istext)
204     state_set_prefix (s);
205
206   while (len > 0) {
207     for (i = 0; i < 4 && len > 0; len--) {
208       if ((ch = fgetc (s->fpin)) == EOF)
209         break;
210       if (base64val(ch) >= 0 || ch == '=')
211         buf[i++] = ch;
212     }
213     if (i != 4) {
214       break;
215     }
216
217     c1 = base64val(buf[0]);
218     c2 = base64val(buf[1]);
219     ch = (c1 << 2) | (c2 >> 4);
220
221     if (cr && ch != '\n')
222       bufi[l++] = '\r';
223
224     cr = 0;
225
226     if (istext && ch == '\r')
227       cr = 1;
228     else
229       bufi[l++] = ch;
230
231     if (buf[2] == '=')
232       break;
233     c3 = base64val(buf[2]);
234     ch = ((c2 & 0xf) << 4) | (c3 >> 2);
235
236     if (cr && ch != '\n')
237       bufi[l++] = '\r';
238
239     cr = 0;
240
241     if (istext && ch == '\r')
242       cr = 1;
243     else
244       bufi[l++] = ch;
245
246     if (buf[3] == '=')
247       break;
248     c4 = base64val(buf[3]);
249     ch = ((c3 & 0x3) << 6) | c4;
250
251     if (cr && ch != '\n')
252       bufi[l++] = '\r';
253     cr = 0;
254
255     if (istext && ch == '\r')
256       cr = 1;
257     else
258       bufi[l++] = ch;
259
260     if (l + 8 >= ssizeof (bufi))
261       mutt_convert_to_state (cd, bufi, &l, s);
262   }
263
264   if (cr)
265     bufi[l++] = '\r';
266
267   mutt_convert_to_state (cd, bufi, &l, s);
268   mutt_convert_to_state (cd, 0, 0, s);
269
270   state_reset_prefix (s);
271 }
272
273 static unsigned char decode_byte (char ch)
274 {
275   if (ch == 96)
276     return 0;
277   return ch - 32;
278 }
279
280 static void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
281 {
282   char tmps[SHORT_STRING];
283   char linelen, c, l, out;
284   char *pt;
285   char bufi[BUFI_SIZE];
286   ssize_t k = 0;
287
288   if (istext)
289     state_set_prefix (s);
290
291   while (len > 0) {
292     if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
293       return;
294     len -= m_strlen(tmps);
295     if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
296       break;
297   }
298   while (len > 0) {
299     if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
300       return;
301     len -= m_strlen(tmps);
302     if (!m_strncmp(tmps, "end", 3))
303       break;
304     pt = tmps;
305     linelen = decode_byte (*pt);
306     pt++;
307     for (c = 0; c < linelen;) {
308       for (l = 2; l <= 6; l += 2) {
309         out = decode_byte (*pt) << l;
310         pt++;
311         out |= (decode_byte (*pt) >> (6 - l));
312         bufi[k++] = out;
313         c++;
314         if (c == linelen)
315           break;
316       }
317       mutt_convert_to_state (cd, bufi, &k, s);
318       pt++;
319     }
320   }
321
322   mutt_convert_to_state (cd, bufi, &k, s);
323   mutt_convert_to_state (cd, 0, 0, s);
324
325   state_reset_prefix (s);
326 }
327
328 /* ----------------------------------------------------------------------------
329  * A (not so) minimal implementation of RFC1563.
330  */
331
332 #define IndentSize (4)
333
334 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
335   RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
336   RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
337 };
338
339 static struct {
340   const char *tag_name;
341   int index;
342 } EnrichedTags[] = {
343   {
344   "param", RICH_PARAM}, {
345   "bold", RICH_BOLD}, {
346   "italic", RICH_ITALIC}, {
347   "underline", RICH_UNDERLINE}, {
348   "nofill", RICH_NOFILL}, {
349   "excerpt", RICH_EXCERPT}, {
350   "indent", RICH_INDENT}, {
351   "indentright", RICH_INDENT_RIGHT}, {
352   "center", RICH_CENTER}, {
353   "flushleft", RICH_FLUSHLEFT}, {
354   "flushright", RICH_FLUSHRIGHT}, {
355   "flushboth", RICH_FLUSHLEFT}, {
356   "color", RICH_COLOR}, {
357   "x-color", RICH_COLOR}, {
358   NULL, -1}
359 };
360
361 struct enriched_state {
362   char *buffer;
363   char *line;
364   char *param;
365   ssize_t buff_len;
366   ssize_t line_len;
367   ssize_t line_used;
368   ssize_t line_max;
369   ssize_t indent_len;
370   ssize_t word_len;
371   ssize_t buff_used;
372   ssize_t param_used;
373   ssize_t param_len;
374   int tag_level[RICH_LAST_TAG];
375   int WrapMargin;
376   STATE *s;
377 };
378
379 static void enriched_wrap (struct enriched_state *stte)
380 {
381   int x;
382   int extra;
383
384   if (stte->line_len) {
385     if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
386       /* Strip trailing white space */
387       ssize_t y = stte->line_used - 1;
388
389       while (y && ISSPACE (stte->line[y])) {
390         stte->line[y] = '\0';
391         y--;
392         stte->line_used--;
393         stte->line_len--;
394       }
395       if (stte->tag_level[RICH_CENTER]) {
396         /* Strip leading whitespace */
397         y = 0;
398
399         while (stte->line[y] && ISSPACE (stte->line[y]))
400           y++;
401         if (y) {
402           ssize_t z;
403
404           for (z = y; z <= stte->line_used; z++) {
405             stte->line[z - y] = stte->line[z];
406           }
407
408           stte->line_len -= y;
409           stte->line_used -= y;
410         }
411       }
412     }
413
414     extra = stte->WrapMargin - stte->line_len - stte->indent_len -
415       (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
416     if (extra > 0) {
417       if (stte->tag_level[RICH_CENTER]) {
418         x = extra / 2;
419         while (x) {
420           state_putc (' ', stte->s);
421           x--;
422         }
423       }
424       else if (stte->tag_level[RICH_FLUSHRIGHT]) {
425         x = extra - 1;
426         while (x) {
427           state_putc (' ', stte->s);
428           x--;
429         }
430       }
431     }
432     state_puts (stte->line, stte->s);
433   }
434
435   state_putc ('\n', stte->s);
436   stte->line[0] = '\0';
437   stte->line_len = 0;
438   stte->line_used = 0;
439   stte->indent_len = 0;
440   if (stte->s->prefix) {
441     state_puts (stte->s->prefix, stte->s);
442     stte->indent_len += m_strlen(stte->s->prefix);
443   }
444
445   if (stte->tag_level[RICH_EXCERPT]) {
446     x = stte->tag_level[RICH_EXCERPT];
447     while (x) {
448       if (stte->s->prefix) {
449         state_puts (stte->s->prefix, stte->s);
450         stte->indent_len += m_strlen(stte->s->prefix);
451       }
452       else {
453         state_puts ("> ", stte->s);
454         stte->indent_len += m_strlen("> ");
455       }
456       x--;
457     }
458   }
459   else
460     stte->indent_len = 0;
461   if (stte->tag_level[RICH_INDENT]) {
462     x = stte->tag_level[RICH_INDENT] * IndentSize;
463     stte->indent_len += x;
464     while (x) {
465       state_putc (' ', stte->s);
466       x--;
467     }
468   }
469 }
470
471 static void enriched_flush (struct enriched_state *stte, int wrap)
472 {
473   if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
474                                         (stte->WrapMargin -
475                                          (stte->tag_level[RICH_INDENT_RIGHT] *
476                                           IndentSize) - stte->indent_len)))
477     enriched_wrap (stte);
478
479   if (stte->buff_used) {
480     stte->buffer[stte->buff_used] = '\0';
481     stte->line_used += stte->buff_used;
482     if (stte->line_used > stte->line_max) {
483       stte->line_max = stte->line_used;
484       p_realloc(&stte->line, stte->line_max + 1);
485     }
486     m_strcat(stte->line, stte->line_max + 1, stte->buffer);
487     stte->line_len += stte->word_len;
488     stte->word_len = 0;
489     stte->buff_used = 0;
490   }
491   if (wrap)
492     enriched_wrap (stte);
493 }
494
495
496 static void enriched_putc (int c, struct enriched_state *stte)
497 {
498   if (stte->tag_level[RICH_PARAM]) {
499     if (stte->tag_level[RICH_COLOR]) {
500       if (stte->param_used + 1 >= stte->param_len)
501         p_realloc(&stte->param, (stte->param_len += STRING));
502
503       stte->param[stte->param_used++] = c;
504     }
505     return;                     /* nothing to do */
506   }
507
508   /* see if more space is needed (plus extra for possible rich characters) */
509   if (stte->buff_len < stte->buff_used + 3) {
510     stte->buff_len += LONG_STRING;
511     p_realloc(&stte->buffer, stte->buff_len + 1);
512   }
513
514   if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
515     if (c == '\t')
516       stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
517     else
518       stte->word_len++;
519
520     stte->buffer[stte->buff_used++] = c;
521     enriched_flush (stte, 0);
522   }
523   else {
524     if (stte->s->flags & M_DISPLAY) {
525       if (stte->tag_level[RICH_BOLD]) {
526         stte->buffer[stte->buff_used++] = c;
527         stte->buffer[stte->buff_used++] = '\010';
528         stte->buffer[stte->buff_used++] = c;
529       }
530       else if (stte->tag_level[RICH_UNDERLINE]) {
531
532         stte->buffer[stte->buff_used++] = '_';
533         stte->buffer[stte->buff_used++] = '\010';
534         stte->buffer[stte->buff_used++] = c;
535       }
536       else if (stte->tag_level[RICH_ITALIC]) {
537         stte->buffer[stte->buff_used++] = c;
538         stte->buffer[stte->buff_used++] = '\010';
539         stte->buffer[stte->buff_used++] = '_';
540       }
541       else {
542         stte->buffer[stte->buff_used++] = c;
543       }
544     }
545     else {
546       stte->buffer[stte->buff_used++] = c;
547     }
548     stte->word_len++;
549   }
550 }
551
552 static void enriched_puts (const char *s, struct enriched_state *stte)
553 {
554   const char *p;
555
556   if (stte->buff_len < stte->buff_used + m_strlen(s)) {
557     stte->buff_len += LONG_STRING;
558     p_realloc(&stte->buffer, stte->buff_len + 1);
559   }
560
561   p = s;
562   while (*p) {
563     stte->buffer[stte->buff_used++] = *p++;
564   }
565 }
566
567 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
568 {
569   const char *tagptr = tag;
570   int i, j;
571
572   if (*tagptr == '/')
573     tagptr++;
574
575   for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
576     if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
577       j = EnrichedTags[i].index;
578       break;
579     }
580
581   if (j != -1) {
582     if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
583       enriched_flush (stte, 1);
584
585     if (*tag == '/') {
586       if (stte->tag_level[j])   /* make sure not to go negative */
587         stte->tag_level[j]--;
588       if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
589           && stte->tag_level[RICH_COLOR]) {
590         stte->param[stte->param_used] = '\0';
591         if (!ascii_strcasecmp (stte->param, "black")) {
592           enriched_puts ("\033[30m", stte);
593         }
594         else if (!ascii_strcasecmp (stte->param, "red")) {
595           enriched_puts ("\033[31m", stte);
596         }
597         else if (!ascii_strcasecmp (stte->param, "green")) {
598           enriched_puts ("\033[32m", stte);
599         }
600         else if (!ascii_strcasecmp (stte->param, "yellow")) {
601           enriched_puts ("\033[33m", stte);
602         }
603         else if (!ascii_strcasecmp (stte->param, "blue")) {
604           enriched_puts ("\033[34m", stte);
605         }
606         else if (!ascii_strcasecmp (stte->param, "magenta")) {
607           enriched_puts ("\033[35m", stte);
608         }
609         else if (!ascii_strcasecmp (stte->param, "cyan")) {
610           enriched_puts ("\033[36m", stte);
611         }
612         else if (!ascii_strcasecmp (stte->param, "white")) {
613           enriched_puts ("\033[37m", stte);
614         }
615       }
616       if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
617         enriched_puts ("\033[0m", stte);
618       }
619
620       /* flush parameter buffer when closing the tag */
621       if (j == RICH_PARAM) {
622         stte->param_used = 0;
623         stte->param[0] = '\0';
624       }
625     }
626     else
627       stte->tag_level[j]++;
628
629     if (j == RICH_EXCERPT)
630       enriched_flush (stte, 1);
631   }
632 }
633
634 static int text_enriched_handler (BODY * a, STATE * s)
635 {
636   enum {
637     TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
638   } state = TEXT;
639
640   long bytes = a->length;
641   struct enriched_state stte;
642   int c = 0;
643   int tag_len = 0;
644   char tag[LONG_STRING + 1];
645
646   p_clear(&stte, 1);
647   stte.s = s;
648   stte.WrapMargin =
649     ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
650                                             72) ? (COLS - 4) : 72);
651   stte.line_max = stte.WrapMargin * 4;
652   stte.line = p_new(char, stte.line_max + 1);
653   stte.param = p_new(char, STRING);
654
655   stte.param_len = STRING;
656   stte.param_used = 0;
657
658   if (s->prefix) {
659     state_puts (s->prefix, s);
660     stte.indent_len += m_strlen(s->prefix);
661   }
662
663   while (state != DONE) {
664     if (state != ST_EOF) {
665       if (!bytes || (c = fgetc (s->fpin)) == EOF)
666         state = ST_EOF;
667       else
668         bytes--;
669     }
670
671     switch (state) {
672     case TEXT:
673       switch (c) {
674       case '<':
675         state = LANGLE;
676         break;
677
678       case '\n':
679         if (stte.tag_level[RICH_NOFILL]) {
680           enriched_flush (&stte, 1);
681         }
682         else {
683           enriched_putc (' ', &stte);
684           state = NEWLINE;
685         }
686         break;
687
688       default:
689         enriched_putc (c, &stte);
690       }
691       break;
692
693     case LANGLE:
694       if (c == '<') {
695         enriched_putc (c, &stte);
696         state = TEXT;
697         break;
698       }
699       else {
700         tag_len = 0;
701         state = TAG;
702       }
703       /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
704     case TAG:
705       if (c == '>') {
706         tag[tag_len] = '\0';
707         enriched_set_flags (tag, &stte);
708         state = TEXT;
709       }
710       else if (tag_len < LONG_STRING)   /* ignore overly long tags */
711         tag[tag_len++] = c;
712       else
713         state = BOGUS_TAG;
714       break;
715
716     case BOGUS_TAG:
717       if (c == '>')
718         state = TEXT;
719       break;
720
721     case NEWLINE:
722       if (c == '\n')
723         enriched_flush (&stte, 1);
724       else {
725         ungetc (c, s->fpin);
726         bytes++;
727         state = TEXT;
728       }
729       break;
730
731     case ST_EOF:
732       enriched_putc ('\0', &stte);
733       enriched_flush (&stte, 1);
734       state = DONE;
735       break;
736
737     case DONE:                 /* not reached, but gcc complains if this is absent */
738       break;
739     }
740   }
741
742   state_putc ('\n', s);         /* add a final newline */
743
744   p_delete(&(stte.buffer));
745   p_delete(&(stte.line));
746   p_delete(&(stte.param));
747
748   return (0);
749 }
750
751 #define TXTHTML     1
752 #define TXTPLAIN    2
753 #define TXTENRICHED 3
754
755 static int alternative_handler (BODY * a, STATE * s)
756 {
757   BODY *choice = NULL;
758   BODY *b;
759   string_list_t *t;
760   char buf[STRING];
761   int type = 0;
762   int mustfree = 0;
763   int rc = 0;
764
765   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
766       a->encoding == ENCUUENCODED) {
767     struct stat st;
768
769     mustfree = 1;
770     fstat (fileno (s->fpin), &st);
771     b = body_new();
772     b->length = (long) st.st_size;
773     b->parts = mutt_parse_multipart(s->fpin,
774                                     parameter_getval(a->parameter, "boundary"),
775                                     (long)st.st_size,
776                                     !ascii_strcasecmp("digest", a->subtype));
777   }
778   else
779     b = a;
780
781   a = b;
782
783   /* First, search list of prefered types */
784   t = AlternativeOrderList;
785   while (t && !choice) {
786     char *c;
787     int btlen;                  /* length of basetype */
788     int wild;                   /* do we have a wildcard to match all subtypes? */
789
790     c = strchr (t->data, '/');
791     if (c) {
792       wild = (c[1] == '*' && c[2] == 0);
793       btlen = c - t->data;
794     }
795     else {
796       wild = 1;
797       btlen = m_strlen(t->data);
798     }
799
800     if (a && a->parts)
801       b = a->parts;
802     else
803       b = a;
804     while (b) {
805       const char *bt = TYPE (b);
806
807       if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
808         /* the basetype matches */
809         if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
810           choice = b;
811         }
812       }
813       b = b->next;
814     }
815     t = t->next;
816   }
817
818   /* Next, look for an autoviewable type */
819   if (!choice) {
820     if (a && a->parts)
821       b = a->parts;
822     else
823       b = a;
824     while (b) {
825       snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
826       if (mutt_is_autoview (b, buf)) {
827         rfc1524_entry *entry = rfc1524_entry_new();
828
829         if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
830           choice = b;
831         }
832         rfc1524_entry_delete(&entry);
833       }
834       b = b->next;
835     }
836   }
837
838   /* Then, look for a text entry */
839   if (!choice) {
840     if (a && a->parts)
841       b = a->parts;
842     else
843       b = a;
844     while (b) {
845       if (b->type == TYPETEXT) {
846         if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
847           choice = b;
848           type = TXTPLAIN;
849         }
850         else if (!ascii_strcasecmp ("enriched", b->subtype)
851                  && type <= TXTENRICHED) {
852           choice = b;
853           type = TXTENRICHED;
854         }
855         else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
856           choice = b;
857           type = TXTHTML;
858         }
859       }
860       b = b->next;
861     }
862   }
863
864   /* Finally, look for other possibilities */
865   if (!choice) {
866     if (a && a->parts)
867       b = a->parts;
868     else
869       b = a;
870     while (b) {
871       if (mutt_can_decode (b))
872         choice = b;
873       b = b->next;
874     }
875   }
876
877   if (choice) {
878     if (s->flags & M_DISPLAY && !option (OPTWEED)) {
879       fseeko (s->fpin, choice->hdr_offset, 0);
880       mutt_copy_bytes (s->fpin, s->fpout,
881                        choice->offset - choice->hdr_offset);
882     }
883     mutt_body_handler (choice, s);
884   }
885   else if (s->flags & M_DISPLAY) {
886     /* didn't find anything that we could display! */
887     state_mark_attach (s);
888     state_puts (_("[-- Error:  Could not display any parts of Multipart/Alternative! --]\n"), s);
889     rc = -1;
890   }
891
892   if (mustfree)
893     body_list_wipe(&a);
894
895   return (rc);
896 }
897
898 /* handles message/rfc822 body parts */
899 static int message_handler (BODY * a, STATE * s)
900 {
901   struct stat st;
902   BODY *b;
903   off_t off_start;
904   int rc = 0;
905
906   off_start = ftello (s->fpin);
907   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
908       a->encoding == ENCUUENCODED) {
909     fstat (fileno (s->fpin), &st);
910     b = body_new();
911     b->length = (long) st.st_size;
912     b->parts = mutt_parse_messageRFC822 (s->fpin, b);
913   }
914   else
915     b = a;
916
917   if (b->parts) {
918     mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
919                    (((s->flags & M_WEED)
920                      || ((s->flags & (M_DISPLAY | M_PRINTING))
921                          && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
922                    (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
923                    s->prefix);
924
925     if (s->prefix)
926       state_puts (s->prefix, s);
927     state_putc ('\n', s);
928
929     rc = mutt_body_handler (b->parts, s);
930   }
931
932   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
933       a->encoding == ENCUUENCODED)
934     body_list_wipe(&b);
935
936   return (rc);
937 }
938
939 /* returns 1 if decoding the attachment will produce output */
940 int mutt_can_decode (BODY * a)
941 {
942   char type[STRING];
943
944   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
945   if (mutt_is_autoview (a, type))
946     return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
947   else if (a->type == TYPETEXT)
948     return (1);
949   else if (a->type == TYPEMESSAGE)
950     return (1);
951   else if (a->type == TYPEMULTIPART) {
952     BODY *p;
953
954     if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
955         ascii_strcasecmp (a->subtype, "encrypted") == 0)
956       return (1);
957
958     for (p = a->parts; p; p = p->next) {
959       if (mutt_can_decode (p))
960         return (1);
961     }
962
963   }
964   else if (a->type == TYPEAPPLICATION) {
965     if (mutt_is_application_pgp(a))
966       return (1);
967     if (mutt_is_application_smime (a))
968       return (1);
969   }
970
971   return (0);
972 }
973
974 static int multipart_handler (BODY * a, STATE * s)
975 {
976   BODY *b, *p;
977   char length[5];
978   struct stat st;
979   int count;
980   int rc = 0;
981
982   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
983       a->encoding == ENCUUENCODED) {
984     fstat (fileno (s->fpin), &st);
985     b = body_new();
986     b->length = (long) st.st_size;
987     b->parts = mutt_parse_multipart(s->fpin,
988                                     parameter_getval(a->parameter, "boundary"),
989                                     (long)st.st_size,
990                                     !ascii_strcasecmp("digest", a->subtype));
991   }
992   else
993     b = a;
994
995   for (p = b->parts, count = 1; p; p = p->next, count++) {
996     if (s->flags & M_DISPLAY) {
997       state_mark_attach (s);
998       state_printf (s, _("[-- Attachment #%d"), count);
999       if (p->description || p->filename || p->form_name) {
1000         state_puts (": ", s);
1001         state_puts (p->description ? p->description :
1002                     p->filename ? p->filename : p->form_name, s);
1003       }
1004       state_puts (" --]\n", s);
1005
1006       mutt_pretty_size (length, sizeof (length), p->length);
1007
1008       state_mark_attach (s);
1009       state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1010                     TYPE (p), p->subtype, ENCODING (p->encoding), length);
1011       if (!option (OPTWEED)) {
1012         fseeko (s->fpin, p->hdr_offset, 0);
1013         mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1014       }
1015       else
1016         state_putc ('\n', s);
1017     }
1018     else {
1019       if (p->description && mutt_can_decode (p))
1020         state_printf (s, "Content-Description: %s\n", p->description);
1021
1022       if (p->form_name)
1023         state_printf (s, "%s: \n", p->form_name);
1024
1025     }
1026     rc = mutt_body_handler (p, s);
1027     state_putc ('\n', s);
1028     if (rc || ((s->flags & M_REPLYING)
1029         && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1030       break;
1031   }
1032
1033   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1034       a->encoding == ENCUUENCODED)
1035     body_list_wipe(&b);
1036
1037   return (rc);
1038 }
1039
1040 static int autoview_handler (BODY * a, STATE * s)
1041 {
1042   rfc1524_entry *entry = rfc1524_entry_new();
1043   char buffer[LONG_STRING];
1044   char type[STRING];
1045   char command[LONG_STRING];
1046   char tempfile[_POSIX_PATH_MAX] = "";
1047   char *fname;
1048   FILE *fpin = NULL;
1049   FILE *fpout = NULL;
1050   FILE *fperr = NULL;
1051   int piped = FALSE;
1052   pid_t thepid;
1053   int rc = 0;
1054
1055   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1056   rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1057
1058   fname = m_strdup(a->filename);
1059   mutt_sanitize_filename (fname, 1);
1060   rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1061                            sizeof (tempfile));
1062   p_delete(&fname);
1063
1064   if (entry->command) {
1065     m_strcpy(command, sizeof(command), entry->command);
1066
1067     /* rfc1524_expand_command returns 0 if the file is required */
1068     piped =
1069       rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1070
1071     if (s->flags & M_DISPLAY) {
1072       state_mark_attach (s);
1073       state_printf (s, _("[-- Autoview using %s --]\n"), command);
1074       mutt_message (_("Invoking autoview command: %s"), command);
1075     }
1076
1077     if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1078       mutt_perror ("fopen");
1079       rfc1524_entry_delete(&entry);
1080       return (-1);
1081     }
1082
1083     mutt_copy_bytes (s->fpin, fpin, a->length);
1084
1085     if (!piped) {
1086       m_fclose(&fpin);
1087       thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1088     }
1089     else {
1090       unlink (tempfile);
1091       fflush (fpin);
1092       rewind (fpin);
1093       thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1094                                       fileno (fpin), -1, -1);
1095     }
1096
1097     if (thepid < 0) {
1098       mutt_perror (_("Can't create filter"));
1099
1100       if (s->flags & M_DISPLAY) {
1101         state_mark_attach (s);
1102         state_printf (s, _("[-- Can't run %s. --]\n"), command);
1103       }
1104       rc = -1;
1105       goto bail;
1106     }
1107
1108     if (s->prefix) {
1109       while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1110         state_puts (s->prefix, s);
1111         state_puts (buffer, s);
1112       }
1113       /* check for data on stderr */
1114       if (fgets (buffer, sizeof (buffer), fperr)) {
1115         if (s->flags & M_DISPLAY) {
1116           state_mark_attach (s);
1117           state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1118         }
1119
1120         state_puts (s->prefix, s);
1121         state_puts (buffer, s);
1122         while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1123           state_puts (s->prefix, s);
1124           state_puts (buffer, s);
1125         }
1126       }
1127     }
1128     else {
1129       mutt_copy_stream (fpout, s->fpout);
1130       /* Check for stderr messages */
1131       if (fgets (buffer, sizeof (buffer), fperr)) {
1132         if (s->flags & M_DISPLAY) {
1133           state_mark_attach (s);
1134           state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1135         }
1136
1137         state_puts (buffer, s);
1138         mutt_copy_stream (fperr, s->fpout);
1139       }
1140     }
1141
1142   bail:
1143     m_fclose(&fpout);
1144     m_fclose(&fperr);
1145
1146     mutt_wait_filter (thepid);
1147     if (piped)
1148       m_fclose(&fpin);
1149     else
1150       mutt_unlink (tempfile);
1151
1152     if (s->flags & M_DISPLAY)
1153       mutt_clear_error ();
1154   }
1155   rfc1524_entry_delete(&entry);
1156   return (rc);
1157 }
1158
1159 static int external_body_handler (BODY * b, STATE * s)
1160 {
1161   const char *access_type;
1162   const char *expiration;
1163   time_t expire;
1164
1165   access_type = parameter_getval(b->parameter, "access-type");
1166   if (!access_type) {
1167     if (s->flags & M_DISPLAY) {
1168       state_mark_attach (s);
1169       state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1170     }
1171     return (-1);
1172   }
1173
1174   expiration = parameter_getval(b->parameter, "expiration");
1175   if (expiration)
1176     expire = mutt_parse_date (expiration, NULL);
1177   else
1178     expire = -1;
1179
1180   if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1181     if (s->flags & (M_DISPLAY | M_PRINTING)) {
1182       char *length;
1183       char pretty_size[10];
1184
1185       state_mark_attach (s);
1186       state_printf (s, _("[-- This %s/%s attachment "),
1187                     TYPE (b->parts), b->parts->subtype);
1188       length = parameter_getval(b->parameter, "length");
1189       if (length) {
1190         mutt_pretty_size (pretty_size, sizeof (pretty_size),
1191                           strtol (length, NULL, 10));
1192         state_printf (s, _("(size %s bytes) "), pretty_size);
1193       }
1194       state_puts (_("has been deleted --]\n"), s);
1195
1196       if (expire != -1) {
1197         state_mark_attach (s);
1198         state_printf (s, _("[-- on %s --]\n"), expiration);
1199       }
1200       if (b->parts->filename) {
1201         state_mark_attach (s);
1202         state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1203       }
1204
1205       mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1206                      (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1207                      CH_DECODE, NULL);
1208     }
1209   }
1210   else if (expiration && expire < time (NULL)) {
1211     if (s->flags & M_DISPLAY) {
1212       state_mark_attach (s);
1213       state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1214                     TYPE (b->parts), b->parts->subtype);
1215       state_attach_puts (_("[-- and the indicated external source has --]\n"
1216                            "[-- expired. --]\n"), s);
1217
1218       mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1219                      (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1220                      CH_DECODE, NULL);
1221     }
1222   }
1223   else {
1224     if (s->flags & M_DISPLAY) {
1225       state_mark_attach (s);
1226       state_printf (s,
1227                     _("[-- This %s/%s attachment is not included, --]\n"),
1228                     TYPE (b->parts), b->parts->subtype);
1229       state_mark_attach (s);
1230       state_printf (s,
1231                     _
1232                     ("[-- and the indicated access-type %s is unsupported --]\n"),
1233                     access_type);
1234       mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1235                      (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1236                      CH_DECODE, NULL);
1237     }
1238   }
1239   return (0);
1240 }
1241
1242 void mutt_decode_attachment (BODY * b, STATE * s)
1243 {
1244   int istext = mutt_is_text_part (b);
1245   iconv_t cd = MUTT_ICONV_ERROR;
1246
1247   Quotebuf[0] = '\0';
1248
1249   if (istext) {
1250     if (s->flags & M_CHARCONV) {
1251       const char *charset = parameter_getval(b->parameter, "charset");
1252
1253       if (!charset)
1254         charset = charset_getfirst(AssumedCharset);
1255       if (charset && Charset)
1256         cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1257     } else {
1258       if (b->file_charset)
1259         cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1260     }
1261   }
1262
1263   fseeko (s->fpin, b->offset, 0);
1264   switch (b->encoding) {
1265   case ENCQUOTEDPRINTABLE:
1266     mutt_decode_quoted(s, b->length,
1267                        istext || mutt_is_application_pgp(b), cd);
1268     break;
1269   case ENCBASE64:
1270     mutt_decode_base64(s, b->length,
1271                        istext || mutt_is_application_pgp(b), cd);
1272     break;
1273   case ENCUUENCODED:
1274     mutt_decode_uuencoded(s, b->length,
1275                           istext || mutt_is_application_pgp(b), cd);
1276     break;
1277   default:
1278     mutt_decode_xbit(s, b->length,
1279                      istext || mutt_is_application_pgp(b), cd);
1280     break;
1281   }
1282
1283   if (cd != MUTT_ICONV_ERROR)
1284     iconv_close (cd);
1285 }
1286
1287 int mutt_body_handler (BODY * b, STATE * s)
1288 {
1289   int decode = 0;
1290   int plaintext = 0;
1291   FILE *fp = NULL;
1292   char tempfile[_POSIX_PATH_MAX];
1293   handler_t handler = NULL;
1294   long tmpoffset = 0;
1295   ssize_t tmplength = 0;
1296   char type[STRING];
1297   int rc = 0;
1298
1299   int oflags = s->flags;
1300
1301   /* first determine which handler to use to process this part */
1302
1303   snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1304   if (mutt_is_autoview (b, type)) {
1305     rfc1524_entry *entry = rfc1524_entry_new();
1306
1307     if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1308       handler = autoview_handler;
1309       s->flags &= ~M_CHARCONV;
1310     }
1311     rfc1524_entry_delete(&entry);
1312   }
1313   else if (b->type == TYPETEXT) {
1314     if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1315       /* avoid copying this part twice since removing the transfer-encoding is
1316        * the only operation needed.
1317        */
1318       if (mutt_is_application_pgp (b))
1319         handler = crypt_pgp_application_pgp_handler;
1320       else
1321         if (!ascii_strcasecmp("flowed", parameter_getval(b->parameter, "format")))
1322           handler = rfc3676_handler;
1323       else
1324         plaintext = 1;
1325     }
1326     else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1327       handler = text_enriched_handler;
1328     else                        /* text body type without a handler */
1329       plaintext = 1;
1330   }
1331   else if (b->type == TYPEMESSAGE) {
1332     if (mutt_is_message_type (b))
1333       handler = message_handler;
1334     else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1335       plaintext = 1;
1336     else if (!ascii_strcasecmp ("external-body", b->subtype))
1337       handler = external_body_handler;
1338   }
1339   else if (b->type == TYPEMULTIPART) {
1340     char *p;
1341
1342     if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1343       handler = alternative_handler;
1344     else if (ascii_strcasecmp ("signed", b->subtype) == 0) {
1345       p = parameter_getval(b->parameter, "protocol");
1346
1347       if (!p)
1348         mutt_error (_("Error: multipart/signed has no protocol."));
1349
1350       else if (s->flags & M_VERIFY)
1351         handler = mutt_signed_handler;
1352     }
1353     else if (m_strcasecmp("encrypted", b->subtype) == 0) {
1354       p = parameter_getval(b->parameter, "protocol");
1355
1356       if (!p)
1357         mutt_error (_
1358                     ("Error: multipart/encrypted has no protocol parameter!"));
1359
1360       else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1361         handler = crypt_pgp_encrypted_handler;
1362     }
1363
1364     if (!handler)
1365       handler = multipart_handler;
1366   }
1367   else if (b->type == TYPEAPPLICATION) {
1368     if (mutt_is_application_pgp (b))
1369       handler = crypt_pgp_application_pgp_handler;
1370     if (mutt_is_application_smime (b))
1371       handler = crypt_smime_application_smime_handler;
1372   }
1373
1374
1375   if (plaintext || handler) {
1376     fseeko (s->fpin, b->offset, 0);
1377
1378     /* see if we need to decode this part before processing it */
1379     if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) {   /* text subtypes may
1380                                                                                                                                                  * require character
1381                                                                                                                                                  * set conversion even
1382                                                                                                                                                  * with 8bit encoding.
1383                                                                                                                                                  */
1384       int origType = b->type;
1385       char *savePrefix = NULL;
1386
1387       if (!plaintext) {
1388         /* decode to a tempfile, saving the original destination */
1389         fp = s->fpout;
1390         s->fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
1391         if (!s->fpout) {
1392           mutt_error _("Unable to open temporary file!");
1393           goto bail;
1394         }
1395         /* decoding the attachment changes the size and offset, so save a copy
1396          * of the "real" values now, and restore them after processing
1397          */
1398         tmplength = b->length;
1399         tmpoffset = b->offset;
1400
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.
1403          */
1404         savePrefix = s->prefix;
1405         s->prefix = NULL;
1406
1407         decode = 1;
1408       } else {
1409         b->type = TYPETEXT;
1410       }
1411
1412       mutt_decode_attachment (b, s);
1413
1414       if (decode) {
1415         b->length = ftello (s->fpout);
1416         b->offset = 0;
1417         m_fclose(&s->fpout);
1418
1419         /* restore final destination and substitute the tempfile for input */
1420         s->fpout = fp;
1421         fp = s->fpin;
1422         s->fpin = safe_fopen (tempfile, "r");
1423         unlink (tempfile);
1424
1425         /* restore the prefix */
1426         s->prefix = savePrefix;
1427       }
1428
1429       b->type = origType;
1430     }
1431
1432     /* process the (decoded) body part */
1433     if (handler) {
1434       rc = handler (b, s);
1435
1436       if (decode) {
1437         b->length = tmplength;
1438         b->offset = tmpoffset;
1439
1440         /* restore the original source stream */
1441         m_fclose(&s->fpin);
1442         s->fpin = fp;
1443       }
1444     }
1445     s->flags |= M_FIRSTDONE;
1446   }
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)) {
1451       if (km_expand_key
1452           (type, sizeof (type),
1453            km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1454         fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1455       else
1456         fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1457     }
1458     fputs (" --]\n", s->fpout);
1459   }
1460
1461 bail:
1462   s->flags = oflags | (s->flags & M_FIRSTDONE);
1463
1464   return (rc);
1465 }