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