lua is the way to go. drop the lemon things.
[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(int ch)
274 {
275     return ch == 96 ? 0 : ch - 32;
276 }
277
278 static void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
279 {
280   char tmps[STRING];
281   char linelen, c, l, out;
282   char *pt;
283   char bufi[BUFI_SIZE];
284   ssize_t k = 0;
285
286   if (istext)
287     state_set_prefix (s);
288
289   while (len > 0) {
290     if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
291       return;
292     len -= m_strlen(tmps);
293     if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
294       break;
295   }
296   while (len > 0) {
297     if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
298       return;
299     len -= m_strlen(tmps);
300     if (!m_strncmp(tmps, "end", 3))
301       break;
302     pt = tmps;
303     linelen = decode_byte(*pt);
304     pt++;
305     for (c = 0; c < linelen;) {
306       for (l = 2; l <= 6; l += 2) {
307         out = decode_byte(*pt) << l;
308         pt++;
309         out |= (decode_byte(*pt) >> (6 - l));
310         bufi[k++] = out;
311         c++;
312         if (c == linelen)
313           break;
314       }
315       mutt_convert_to_state (cd, bufi, &k, s);
316       pt++;
317     }
318   }
319
320   mutt_convert_to_state (cd, bufi, &k, s);
321   mutt_convert_to_state (cd, 0, 0, s);
322
323   state_reset_prefix (s);
324 }
325
326 /* ----------------------------------------------------------------------------
327  * A (not so) minimal implementation of RFC1563.
328  */
329
330 #define IndentSize (4)
331
332 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
333   RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
334   RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
335 };
336
337 static struct {
338   const char *tag_name;
339   int index;
340 } EnrichedTags[] = {
341   {"param", RICH_PARAM},
342   {"bold", RICH_BOLD},
343   {"italic", RICH_ITALIC},
344   {"underline", RICH_UNDERLINE},
345   {"nofill", RICH_NOFILL},
346   {"excerpt", RICH_EXCERPT},
347   {"indent", RICH_INDENT},
348   {"indentright", RICH_INDENT_RIGHT},
349   {"center", RICH_CENTER},
350   {"flushleft", RICH_FLUSHLEFT},
351   {"flushright", RICH_FLUSHRIGHT},
352   {"flushboth", RICH_FLUSHLEFT},
353   {"color", RICH_COLOR},
354   {"x-color", RICH_COLOR},
355   {NULL, -1}
356 };
357
358 struct enriched_state {
359   char *buffer;
360   char *line;
361   char *param;
362   ssize_t buff_len;
363   ssize_t line_len;
364   ssize_t line_used;
365   ssize_t line_max;
366   ssize_t indent_len;
367   ssize_t word_len;
368   ssize_t buff_used;
369   ssize_t param_used;
370   ssize_t param_len;
371   int tag_level[RICH_LAST_TAG];
372   int WrapMargin;
373   STATE *s;
374 };
375
376 static void enriched_wrap (struct enriched_state *stte)
377 {
378   int x;
379   int extra;
380
381   if (stte->line_len) {
382     if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
383       /* Strip trailing white space */
384       ssize_t y = stte->line_used - 1;
385
386       while (y && ISSPACE (stte->line[y])) {
387         stte->line[y] = '\0';
388         y--;
389         stte->line_used--;
390         stte->line_len--;
391       }
392       if (stte->tag_level[RICH_CENTER]) {
393         /* Strip leading whitespace */
394         y = 0;
395
396         while (stte->line[y] && ISSPACE (stte->line[y]))
397           y++;
398         if (y) {
399           ssize_t z;
400
401           for (z = y; z <= stte->line_used; z++) {
402             stte->line[z - y] = stte->line[z];
403           }
404
405           stte->line_len -= y;
406           stte->line_used -= y;
407         }
408       }
409     }
410
411     extra = stte->WrapMargin - stte->line_len - stte->indent_len -
412       (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
413     if (extra > 0) {
414       if (stte->tag_level[RICH_CENTER]) {
415         x = extra / 2;
416         while (x) {
417           state_putc (' ', stte->s);
418           x--;
419         }
420       }
421       else if (stte->tag_level[RICH_FLUSHRIGHT]) {
422         x = extra - 1;
423         while (x) {
424           state_putc (' ', stte->s);
425           x--;
426         }
427       }
428     }
429     state_puts (stte->line, stte->s);
430   }
431
432   state_putc ('\n', stte->s);
433   stte->line[0] = '\0';
434   stte->line_len = 0;
435   stte->line_used = 0;
436   stte->indent_len = 0;
437   if (stte->s->prefix) {
438     state_puts (stte->s->prefix, stte->s);
439     stte->indent_len += m_strlen(stte->s->prefix);
440   }
441
442   if (stte->tag_level[RICH_EXCERPT]) {
443     x = stte->tag_level[RICH_EXCERPT];
444     while (x) {
445       if (stte->s->prefix) {
446         state_puts (stte->s->prefix, stte->s);
447         stte->indent_len += m_strlen(stte->s->prefix);
448       }
449       else {
450         state_puts ("> ", stte->s);
451         stte->indent_len += m_strlen("> ");
452       }
453       x--;
454     }
455   }
456   else
457     stte->indent_len = 0;
458   if (stte->tag_level[RICH_INDENT]) {
459     x = stte->tag_level[RICH_INDENT] * IndentSize;
460     stte->indent_len += x;
461     while (x) {
462       state_putc (' ', stte->s);
463       x--;
464     }
465   }
466 }
467
468 static void enriched_flush (struct enriched_state *stte, int wrap)
469 {
470   if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
471                                         (stte->WrapMargin -
472                                          (stte->tag_level[RICH_INDENT_RIGHT] *
473                                           IndentSize) - stte->indent_len)))
474     enriched_wrap (stte);
475
476   if (stte->buff_used) {
477     stte->buffer[stte->buff_used] = '\0';
478     stte->line_used += stte->buff_used;
479     if (stte->line_used > stte->line_max) {
480       stte->line_max = stte->line_used;
481       p_realloc(&stte->line, stte->line_max + 1);
482     }
483     m_strcat(stte->line, stte->line_max + 1, stte->buffer);
484     stte->line_len += stte->word_len;
485     stte->word_len = 0;
486     stte->buff_used = 0;
487   }
488   if (wrap)
489     enriched_wrap (stte);
490 }
491
492
493 static void enriched_putc (int c, struct enriched_state *stte)
494 {
495   if (stte->tag_level[RICH_PARAM]) {
496     if (stte->tag_level[RICH_COLOR]) {
497       if (stte->param_used + 1 >= stte->param_len)
498         p_realloc(&stte->param, (stte->param_len += STRING));
499
500       stte->param[stte->param_used++] = c;
501     }
502     return;                     /* nothing to do */
503   }
504
505   /* see if more space is needed (plus extra for possible rich characters) */
506   if (stte->buff_len < stte->buff_used + 3) {
507     stte->buff_len += LONG_STRING;
508     p_realloc(&stte->buffer, stte->buff_len + 1);
509   }
510
511   if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
512     if (c == '\t')
513       stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
514     else
515       stte->word_len++;
516
517     stte->buffer[stte->buff_used++] = c;
518     enriched_flush (stte, 0);
519   }
520   else {
521     if (stte->s->flags & M_DISPLAY) {
522       if (stte->tag_level[RICH_BOLD]) {
523         stte->buffer[stte->buff_used++] = c;
524         stte->buffer[stte->buff_used++] = '\010';
525         stte->buffer[stte->buff_used++] = c;
526       }
527       else if (stte->tag_level[RICH_UNDERLINE]) {
528
529         stte->buffer[stte->buff_used++] = '_';
530         stte->buffer[stte->buff_used++] = '\010';
531         stte->buffer[stte->buff_used++] = c;
532       }
533       else if (stte->tag_level[RICH_ITALIC]) {
534         stte->buffer[stte->buff_used++] = c;
535         stte->buffer[stte->buff_used++] = '\010';
536         stte->buffer[stte->buff_used++] = '_';
537       }
538       else {
539         stte->buffer[stte->buff_used++] = c;
540       }
541     } else {
542       stte->buffer[stte->buff_used++] = c;
543     }
544     stte->word_len++;
545   }
546 }
547
548 static void enriched_puts (const char *s, struct enriched_state *stte)
549 {
550   const char *p;
551
552   if (stte->buff_len < stte->buff_used + m_strlen(s)) {
553     stte->buff_len += LONG_STRING;
554     p_realloc(&stte->buffer, stte->buff_len + 1);
555   }
556
557   p = s;
558   while (*p) {
559     stte->buffer[stte->buff_used++] = *p++;
560   }
561 }
562
563 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
564 {
565   const char *tagptr = tag;
566   int i, j;
567
568   if (*tagptr == '/')
569     tagptr++;
570
571   for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
572     if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
573       j = EnrichedTags[i].index;
574       break;
575     }
576
577   if (j != -1) {
578     if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
579       enriched_flush (stte, 1);
580
581     if (*tag == '/') {
582       if (stte->tag_level[j])   /* make sure not to go negative */
583         stte->tag_level[j]--;
584       if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
585           && stte->tag_level[RICH_COLOR]) {
586         stte->param[stte->param_used] = '\0';
587         if (!ascii_strcasecmp (stte->param, "black")) {
588           enriched_puts ("\033[30m", stte);
589         }
590         else if (!ascii_strcasecmp (stte->param, "red")) {
591           enriched_puts ("\033[31m", stte);
592         }
593         else if (!ascii_strcasecmp (stte->param, "green")) {
594           enriched_puts ("\033[32m", stte);
595         }
596         else if (!ascii_strcasecmp (stte->param, "yellow")) {
597           enriched_puts ("\033[33m", stte);
598         }
599         else if (!ascii_strcasecmp (stte->param, "blue")) {
600           enriched_puts ("\033[34m", stte);
601         }
602         else if (!ascii_strcasecmp (stte->param, "magenta")) {
603           enriched_puts ("\033[35m", stte);
604         }
605         else if (!ascii_strcasecmp (stte->param, "cyan")) {
606           enriched_puts ("\033[36m", stte);
607         }
608         else if (!ascii_strcasecmp (stte->param, "white")) {
609           enriched_puts ("\033[37m", stte);
610         }
611       }
612       if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
613         enriched_puts ("\033[0m", stte);
614       }
615
616       /* flush parameter buffer when closing the tag */
617       if (j == RICH_PARAM) {
618         stte->param_used = 0;
619         stte->param[0] = '\0';
620       }
621     }
622     else
623       stte->tag_level[j]++;
624
625     if (j == RICH_EXCERPT)
626       enriched_flush (stte, 1);
627   }
628 }
629
630 static int text_enriched_handler (BODY * a, STATE * s)
631 {
632   enum {
633     TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
634   } state = TEXT;
635
636   long bytes = a->length;
637   struct enriched_state stte;
638   int c = 0;
639   int tag_len = 0;
640   char tag[LONG_STRING + 1];
641
642   p_clear(&stte, 1);
643   stte.s = s;
644   stte.WrapMargin =
645     ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
646                                             72) ? (COLS - 4) : 72);
647   stte.line_max = stte.WrapMargin * 4;
648   stte.line = p_new(char, stte.line_max + 1);
649   stte.param = p_new(char, STRING);
650
651   stte.param_len = STRING;
652   stte.param_used = 0;
653
654   if (s->prefix) {
655     state_puts (s->prefix, s);
656     stte.indent_len += m_strlen(s->prefix);
657   }
658
659   while (state != DONE) {
660     if (state != ST_EOF) {
661       if (!bytes || (c = fgetc (s->fpin)) == EOF)
662         state = ST_EOF;
663       else
664         bytes--;
665     }
666
667     switch (state) {
668     case TEXT:
669       switch (c) {
670       case '<':
671         state = LANGLE;
672         break;
673
674       case '\n':
675         if (stte.tag_level[RICH_NOFILL]) {
676           enriched_flush (&stte, 1);
677         }
678         else {
679           enriched_putc (' ', &stte);
680           state = NEWLINE;
681         }
682         break;
683
684       default:
685         enriched_putc (c, &stte);
686       }
687       break;
688
689     case LANGLE:
690       if (c == '<') {
691         enriched_putc (c, &stte);
692         state = TEXT;
693         break;
694       }
695       else {
696         tag_len = 0;
697         state = TAG;
698       }
699       /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
700     case TAG:
701       if (c == '>') {
702         tag[tag_len] = '\0';
703         enriched_set_flags (tag, &stte);
704         state = TEXT;
705       }
706       else if (tag_len < LONG_STRING)   /* ignore overly long tags */
707         tag[tag_len++] = c;
708       else
709         state = BOGUS_TAG;
710       break;
711
712     case BOGUS_TAG:
713       if (c == '>')
714         state = TEXT;
715       break;
716
717     case NEWLINE:
718       if (c == '\n')
719         enriched_flush (&stte, 1);
720       else {
721         ungetc (c, s->fpin);
722         bytes++;
723         state = TEXT;
724       }
725       break;
726
727     case ST_EOF:
728       enriched_putc ('\0', &stte);
729       enriched_flush (&stte, 1);
730       state = DONE;
731       break;
732
733     case DONE:                 /* not reached, but gcc complains if this is absent */
734       break;
735     }
736   }
737
738   state_putc ('\n', s);         /* add a final newline */
739
740   p_delete(&(stte.buffer));
741   p_delete(&(stte.line));
742   p_delete(&(stte.param));
743
744   return (0);
745 }
746
747 #define TXTHTML     1
748 #define TXTPLAIN    2
749 #define TXTENRICHED 3
750
751 static int alternative_handler (BODY * a, STATE * s)
752 {
753   BODY *choice = NULL;
754   BODY *b;
755   string_list_t *t;
756   char buf[STRING];
757   int type = 0;
758   int mustfree = 0;
759   int rc = 0;
760
761   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
762       a->encoding == ENCUUENCODED) {
763     struct stat st;
764
765     mustfree = 1;
766     fstat (fileno (s->fpin), &st);
767     b = body_new();
768     b->length = (long) st.st_size;
769     b->parts = mutt_parse_multipart(s->fpin,
770                                     parameter_getval(a->parameter, "boundary"),
771                                     (long)st.st_size,
772                                     mime_which_token(a->subtype, -1) == MIME_DIGEST);
773   }
774   else
775     b = a;
776
777   a = b;
778
779   /* First, search list of prefered types */
780   t = AlternativeOrderList;
781   while (t && !choice) {
782     char *c;
783     int btlen;                  /* length of basetype */
784     int wild;                   /* do we have a wildcard to match all subtypes? */
785
786     c = strchr (t->data, '/');
787     if (c) {
788       wild = (c[1] == '*' && c[2] == 0);
789       btlen = c - t->data;
790     }
791     else {
792       wild = 1;
793       btlen = m_strlen(t->data);
794     }
795
796     if (a && a->parts)
797       b = a->parts;
798     else
799       b = a;
800     while (b) {
801       const char *bt = TYPE (b);
802
803       if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
804         /* the basetype matches */
805         if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
806           choice = b;
807         }
808       }
809       b = b->next;
810     }
811     t = t->next;
812   }
813
814   /* Next, look for an autoviewable type */
815   if (!choice) {
816     if (a && a->parts)
817       b = a->parts;
818     else
819       b = a;
820     while (b) {
821       snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
822       if (mutt_is_autoview (b, buf)) {
823         rfc1524_entry *entry = rfc1524_entry_new();
824
825         if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
826           choice = b;
827         }
828         rfc1524_entry_delete(&entry);
829       }
830       b = b->next;
831     }
832   }
833
834   /* Then, look for a text entry */
835   if (!choice) {
836     if (a && a->parts)
837       b = a->parts;
838     else
839       b = a;
840     while (b) {
841       if (b->type == TYPETEXT) {
842         switch (mime_which_token(b->subtype, -1)) {
843           case MIME_PLAIN:
844             if (type <= TXTPLAIN) {
845               choice = b;
846               type = TXTPLAIN;
847             }
848             break;
849           case MIME_ENRICHED:
850             if (type <= TXTENRICHED) {
851               choice = b;
852               type = TXTENRICHED;
853             }
854             break;
855           case MIME_HTML:
856             if (type <= TXTHTML) {
857               choice = b;
858               type = TXTHTML;
859             }
860             break;
861           default:
862             break;
863         }
864       }
865       b = b->next;
866     }
867   }
868
869   /* Finally, look for other possibilities */
870   if (!choice) {
871     if (a && a->parts)
872       b = a->parts;
873     else
874       b = a;
875     while (b) {
876       if (mutt_can_decode (b))
877         choice = b;
878       b = b->next;
879     }
880   }
881
882   if (choice) {
883     if (s->flags & M_DISPLAY && !option (OPTWEED)) {
884       fseeko (s->fpin, choice->hdr_offset, 0);
885       mutt_copy_bytes (s->fpin, s->fpout,
886                        choice->offset - choice->hdr_offset);
887     }
888     mutt_body_handler (choice, s);
889   }
890   else if (s->flags & M_DISPLAY) {
891     /* didn't find anything that we could display! */
892     state_mark_attach (s);
893     state_puts (_("[-- Error:  Could not display any parts of Multipart/Alternative! --]\n"), s);
894     rc = -1;
895   }
896
897   if (mustfree)
898     body_list_wipe(&a);
899
900   return (rc);
901 }
902
903 /* handles message/rfc822 body parts */
904 static int message_handler (BODY * a, STATE * s)
905 {
906   struct stat st;
907   BODY *b;
908   off_t off_start;
909   int rc = 0;
910
911   off_start = ftello (s->fpin);
912   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
913       a->encoding == ENCUUENCODED) {
914     fstat (fileno (s->fpin), &st);
915     b = body_new();
916     b->length = (long) st.st_size;
917     b->parts = mutt_parse_messageRFC822 (s->fpin, b);
918   }
919   else
920     b = a;
921
922   if (b->parts) {
923     mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
924                    (((s->flags & M_WEED)
925                      || ((s->flags & (M_DISPLAY | M_PRINTING))
926                          && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
927                    (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
928                    s->prefix);
929
930     if (s->prefix)
931       state_puts (s->prefix, s);
932     state_putc ('\n', s);
933
934     rc = mutt_body_handler (b->parts, s);
935   }
936
937   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
938       a->encoding == ENCUUENCODED)
939     body_list_wipe(&b);
940
941   return (rc);
942 }
943
944 /* returns 1 if decoding the attachment will produce output */
945 int mutt_can_decode (BODY * a)
946 {
947   char type[STRING];
948
949   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
950   if (mutt_is_autoview (a, type))
951     return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
952   else if (a->type == TYPETEXT)
953     return (1);
954   else if (a->type == TYPEMESSAGE)
955     return (1);
956   else if (a->type == TYPEMULTIPART) {
957     BODY *p;
958     int tok = mime_which_token(a->subtype, -1);
959
960     if (tok == MIME_SIGNED || tok == MIME_ENCRYPTED)
961       return 1;
962
963     for (p = a->parts; p; p = p->next) {
964       if (mutt_can_decode (p))
965         return (1);
966     }
967   }
968   else if (a->type == TYPEAPPLICATION) {
969     if (mutt_is_application_pgp(a))
970       return (1);
971     if (mutt_is_application_smime (a))
972       return (1);
973   }
974
975   return (0);
976 }
977
978 static int multipart_handler (BODY * a, STATE * s)
979 {
980   BODY *b, *p;
981   char length[5];
982   struct stat st;
983   int count;
984   int rc = 0;
985
986   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
987       a->encoding == ENCUUENCODED) {
988     fstat (fileno (s->fpin), &st);
989     b = body_new();
990     b->length = (long) st.st_size;
991     b->parts = mutt_parse_multipart(s->fpin,
992                                     parameter_getval(a->parameter, "boundary"),
993                                     (long)st.st_size,
994                                     mime_which_token(a->subtype, -1) == MIME_DIGEST);
995   } else {
996     b = a;
997   }
998
999   for (p = b->parts, count = 1; p; p = p->next, count++) {
1000     if (s->flags & M_DISPLAY) {
1001       state_mark_attach (s);
1002       state_printf (s, _("[-- Attachment #%d"), count);
1003       if (p->description || p->filename || p->form_name) {
1004         state_puts (": ", s);
1005         state_puts (p->description ? p->description :
1006                     p->filename ? p->filename : p->form_name, s);
1007       }
1008       state_puts (" --]\n", s);
1009
1010       mutt_pretty_size (length, sizeof (length), p->length);
1011
1012       state_mark_attach (s);
1013       state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1014                     TYPE (p), p->subtype, ENCODING (p->encoding), length);
1015       if (!option (OPTWEED)) {
1016         fseeko (s->fpin, p->hdr_offset, 0);
1017         mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1018       }
1019       else
1020         state_putc ('\n', s);
1021     }
1022     else {
1023       if (p->description && mutt_can_decode (p))
1024         state_printf (s, "Content-Description: %s\n", p->description);
1025
1026       if (p->form_name)
1027         state_printf (s, "%s: \n", p->form_name);
1028
1029     }
1030     rc = mutt_body_handler (p, s);
1031     state_putc ('\n', s);
1032     if (rc || ((s->flags & M_REPLYING)
1033         && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1034       break;
1035   }
1036
1037   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1038       a->encoding == ENCUUENCODED)
1039     body_list_wipe(&b);
1040
1041   return (rc);
1042 }
1043
1044 static int autoview_handler (BODY * a, STATE * s)
1045 {
1046   rfc1524_entry *entry = rfc1524_entry_new();
1047   char buffer[LONG_STRING];
1048   char type[STRING];
1049   char command[LONG_STRING];
1050   char tempfile[_POSIX_PATH_MAX] = "";
1051   char *fname;
1052   FILE *fpin = NULL;
1053   FILE *fpout = NULL;
1054   FILE *fperr = NULL;
1055   int piped = FALSE;
1056   pid_t thepid;
1057   int rc = 0;
1058
1059   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1060   rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1061
1062   fname = m_strdup(a->filename);
1063   mutt_sanitize_filename (fname, 1);
1064   rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1065                            sizeof (tempfile));
1066   p_delete(&fname);
1067
1068   if (entry->command) {
1069     m_strcpy(command, sizeof(command), entry->command);
1070
1071     /* rfc1524_expand_command returns 0 if the file is required */
1072     piped =
1073       rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1074
1075     if (s->flags & M_DISPLAY) {
1076       state_mark_attach (s);
1077       state_printf (s, _("[-- Autoview using %s --]\n"), command);
1078       mutt_message (_("Invoking autoview command: %s"), command);
1079     }
1080
1081     if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1082       mutt_perror ("fopen");
1083       rfc1524_entry_delete(&entry);
1084       return (-1);
1085     }
1086
1087     mutt_copy_bytes (s->fpin, fpin, a->length);
1088
1089     if (!piped) {
1090       m_fclose(&fpin);
1091       thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1092     } else {
1093       unlink (tempfile);
1094       fflush (fpin);
1095       rewind (fpin);
1096       thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1097                                       fileno (fpin), -1, -1);
1098     }
1099
1100     if (thepid < 0) {
1101       mutt_perror (_("Can't create filter"));
1102
1103       if (s->flags & M_DISPLAY) {
1104         state_mark_attach (s);
1105         state_printf (s, _("[-- Can't run %s. --]\n"), command);
1106       }
1107       rc = -1;
1108       goto bail;
1109     }
1110
1111     if (s->prefix) {
1112       while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1113         state_puts (s->prefix, s);
1114         state_puts (buffer, s);
1115       }
1116       /* check for data on stderr */
1117       if (fgets (buffer, sizeof (buffer), fperr)) {
1118         if (s->flags & M_DISPLAY) {
1119           state_mark_attach (s);
1120           state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1121         }
1122
1123         state_puts (s->prefix, s);
1124         state_puts (buffer, s);
1125         while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1126           state_puts (s->prefix, s);
1127           state_puts (buffer, s);
1128         }
1129       }
1130     } else {
1131       mutt_copy_stream (fpout, s->fpout);
1132       /* Check for stderr messages */
1133       if (fgets (buffer, sizeof (buffer), fperr)) {
1134         if (s->flags & M_DISPLAY) {
1135           state_mark_attach (s);
1136           state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1137         }
1138
1139         state_puts (buffer, s);
1140         mutt_copy_stream (fperr, s->fpout);
1141       }
1142     }
1143
1144   bail:
1145     m_fclose(&fpout);
1146     m_fclose(&fperr);
1147
1148     mutt_wait_filter (thepid);
1149     if (piped)
1150       m_fclose(&fpin);
1151     else
1152       mutt_unlink (tempfile);
1153
1154     if (s->flags & M_DISPLAY)
1155       mutt_clear_error ();
1156   }
1157   rfc1524_entry_delete(&entry);
1158   return (rc);
1159 }
1160
1161 static int external_body_handler (BODY * b, STATE * s)
1162 {
1163   const char *access_type;
1164   const char *expiration;
1165   time_t expire;
1166
1167   access_type = parameter_getval(b->parameter, "access-type");
1168   if (!access_type) {
1169     if (s->flags & M_DISPLAY) {
1170       state_mark_attach (s);
1171       state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1172     }
1173     return (-1);
1174   }
1175
1176   expiration = parameter_getval(b->parameter, "expiration");
1177   if (expiration)
1178     expire = mutt_parse_date (expiration, NULL);
1179   else
1180     expire = -1;
1181
1182   if (mime_which_token(access_type, -1) == MIME_X_MUTT_DELETED) {
1183     if (s->flags & (M_DISPLAY | M_PRINTING)) {
1184       char *length;
1185       char pretty_size[10];
1186
1187       state_mark_attach (s);
1188       state_printf (s, _("[-- This %s/%s attachment "),
1189                     TYPE (b->parts), b->parts->subtype);
1190       length = parameter_getval(b->parameter, "length");
1191       if (length) {
1192         mutt_pretty_size (pretty_size, sizeof (pretty_size),
1193                           strtol (length, NULL, 10));
1194         state_printf (s, _("(size %s bytes) "), pretty_size);
1195       }
1196       state_puts (_("has been deleted --]\n"), s);
1197
1198       if (expire != -1) {
1199         state_mark_attach (s);
1200         state_printf (s, _("[-- on %s --]\n"), expiration);
1201       }
1202       if (b->parts->filename) {
1203         state_mark_attach (s);
1204         state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1205       }
1206
1207       mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1208                      (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1209                      CH_DECODE, NULL);
1210     }
1211   }
1212   else if (expiration && expire < time (NULL)) {
1213     if (s->flags & M_DISPLAY) {
1214       state_mark_attach (s);
1215       state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1216                     TYPE (b->parts), b->parts->subtype);
1217       state_attach_puts (_("[-- and the indicated external source has --]\n"
1218                            "[-- expired. --]\n"), s);
1219
1220       mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1221                      (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1222                      CH_DECODE, NULL);
1223     }
1224   } else {
1225     if (s->flags & M_DISPLAY) {
1226       state_mark_attach (s);
1227       state_printf (s,
1228                     _("[-- This %s/%s attachment is not included, --]\n"),
1229                     TYPE (b->parts), b->parts->subtype);
1230       state_mark_attach (s);
1231       state_printf (s,
1232                     _
1233                     ("[-- and the indicated access-type %s is unsupported --]\n"),
1234                     access_type);
1235       mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1236                      (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1237                      CH_DECODE, NULL);
1238     }
1239   }
1240   return (0);
1241 }
1242
1243 void mutt_decode_attachment (BODY * b, STATE * s)
1244 {
1245   int istext = mutt_is_text_part (b);
1246   iconv_t cd = MUTT_ICONV_ERROR;
1247
1248   Quotebuf[0] = '\0';
1249
1250   if (istext) {
1251     if (s->flags & M_CHARCONV) {
1252       const char *charset = parameter_getval(b->parameter, "charset");
1253
1254       if (!charset)
1255         charset = charset_getfirst(AssumedCharset);
1256       if (charset && Charset)
1257         cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1258     } else {
1259       if (b->file_charset)
1260         cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1261     }
1262   }
1263
1264   fseeko (s->fpin, b->offset, 0);
1265   switch (b->encoding) {
1266   case ENCQUOTEDPRINTABLE:
1267     mutt_decode_quoted(s, b->length,
1268                        istext || mutt_is_application_pgp(b), cd);
1269     break;
1270   case ENCBASE64:
1271     mutt_decode_base64(s, b->length,
1272                        istext || mutt_is_application_pgp(b), cd);
1273     break;
1274   case ENCUUENCODED:
1275     mutt_decode_uuencoded(s, b->length,
1276                           istext || mutt_is_application_pgp(b), cd);
1277     break;
1278   default:
1279     mutt_decode_xbit(s, b->length,
1280                      istext || mutt_is_application_pgp(b), cd);
1281     break;
1282   }
1283
1284   if (cd != MUTT_ICONV_ERROR)
1285     iconv_close (cd);
1286 }
1287
1288 int mutt_body_handler (BODY * b, STATE * s)
1289 {
1290   int decode = 0;
1291   int plaintext = 0;
1292   FILE *fp = NULL;
1293   char tempfile[_POSIX_PATH_MAX];
1294   handler_t handler = NULL;
1295   long tmpoffset = 0;
1296   ssize_t tmplength = 0;
1297   char type[STRING];
1298   int rc = 0;
1299
1300   int oflags = s->flags;
1301   int tok = mime_which_token(b->subtype, -1);
1302
1303   /* first determine which handler to use to process this part */
1304
1305   snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1306   if (mutt_is_autoview (b, type)) {
1307     rfc1524_entry *entry = rfc1524_entry_new();
1308
1309     if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1310       handler = autoview_handler;
1311       s->flags &= ~M_CHARCONV;
1312     }
1313     rfc1524_entry_delete(&entry);
1314   }
1315   else if (b->type == TYPETEXT) {
1316     if (tok == MIME_PLAIN) {
1317       /* avoid copying this part twice since removing the transfer-encoding is
1318        * the only operation needed.
1319        */
1320       if (mutt_is_application_pgp (b))
1321         handler = crypt_pgp_application_pgp_handler;
1322       else
1323         if (!ascii_strcasecmp("flowed", parameter_getval(b->parameter, "format")))
1324           handler = rfc3676_handler;
1325       else
1326         plaintext = 1;
1327     }
1328     else if (tok == MIME_ENRICHED)
1329       handler = text_enriched_handler;
1330     else                        /* text body type without a handler */
1331       plaintext = 1;
1332   }
1333   else if (b->type == TYPEMESSAGE) {
1334     if (mutt_is_message_type (b))
1335       handler = message_handler;
1336     else if (tok == MIME_DELIVERY_STATUS)
1337       plaintext = 1;
1338     else if (tok == MIME_EXTERNAL_BODY)
1339       handler = external_body_handler;
1340   }
1341   else if (b->type == TYPEMULTIPART) {
1342     char *p;
1343
1344     if (tok == MIME_ALTERNATIVE)
1345       handler = alternative_handler;
1346     else if (tok == MIME_SIGNED) {
1347       p = parameter_getval(b->parameter, "protocol");
1348
1349       if (!p)
1350         mutt_error (_("Error: multipart/signed has no protocol."));
1351
1352       else if (s->flags & M_VERIFY)
1353         handler = mutt_signed_handler;
1354     }
1355     else if (tok == MIME_ENCRYPTED) {
1356       p = parameter_getval(b->parameter, "protocol");
1357
1358       if (!p)
1359         mutt_error(_("Error: multipart/encrypted has no protocol parameter!"));
1360       else if (mime_which_token(p, -1) == MIME_APPLICATION_PGP_ENCRYPTED)
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   if (plaintext || handler) {
1375     fseeko (s->fpin, b->offset, 0);
1376
1377     /* see if we need to decode this part before processing it */
1378     if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE
1379     ||  b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) {
1380         /* text subtypes may require character set conversion even with 8bit
1381            encoding.  */
1382       int origType = b->type;
1383       char *savePrefix = NULL;
1384
1385       if (!plaintext) {
1386         /* decode to a tempfile, saving the original destination */
1387         fp = s->fpout;
1388         s->fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
1389         if (!s->fpout) {
1390           mutt_error _("Unable to open temporary file!");
1391           goto bail;
1392         }
1393         /* decoding the attachment changes the size and offset, so save a copy
1394          * of the "real" values now, and restore them after processing
1395          */
1396         tmplength = b->length;
1397         tmpoffset = b->offset;
1398
1399         /* if we are decoding binary bodies, we don't want to prefix each
1400          * line with the prefix or else the data will get corrupted.
1401          */
1402         savePrefix = s->prefix;
1403         s->prefix = NULL;
1404
1405         decode = 1;
1406       } else {
1407         b->type = TYPETEXT;
1408       }
1409
1410       mutt_decode_attachment (b, s);
1411
1412       if (decode) {
1413         b->length = ftello (s->fpout);
1414         b->offset = 0;
1415         m_fclose(&s->fpout);
1416
1417         /* restore final destination and substitute the tempfile for input */
1418         s->fpout = fp;
1419         fp = s->fpin;
1420         s->fpin = safe_fopen (tempfile, "r");
1421         unlink (tempfile);
1422
1423         /* restore the prefix */
1424         s->prefix = savePrefix;
1425       }
1426
1427       b->type = origType;
1428     }
1429
1430     /* process the (decoded) body part */
1431     if (handler) {
1432       rc = handler (b, s);
1433
1434       if (decode) {
1435         b->length = tmplength;
1436         b->offset = tmpoffset;
1437
1438         /* restore the original source stream */
1439         m_fclose(&s->fpin);
1440         s->fpin = fp;
1441       }
1442     }
1443     s->flags |= M_FIRSTDONE;
1444   }
1445   else if (s->flags & M_DISPLAY) {
1446     state_mark_attach (s);
1447     state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1448     if (!option (OPTVIEWATTACH)) {
1449       if (km_expand_key
1450           (type, sizeof (type),
1451            km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1452         fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1453       else
1454         fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1455     }
1456     fputs (" --]\n", s->fpout);
1457   }
1458
1459 bail:
1460   s->flags = oflags | (s->flags & M_FIRSTDONE);
1461
1462   return (rc);
1463 }