d4e18c1bd7958bb0417be84021d3c36a495d5ac7
[apps/madmutt.git] / lib-mime / rfc822parse.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 /*
21  * Copyright notice from original mutt:
22  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
23  *
24  * This file is part of mutt-ng, see http://www.muttng.org/.
25  * It's licensed under the GNU General Public License,
26  * please see the file GPL in the top level source directory.
27  */
28
29 #if HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34
35 #include <lib-lib/mem.h>
36 #include <lib-lib/str.h>
37 #include <lib-lib/ascii.h>
38 #include <lib-lib/macros.h>
39 #include <lib-lib/buffer.h>
40 #include <lib-lib/date.h>
41 #include <lib-lib/debug.h>
42 #include <lib-lib/url.h>
43
44 #include "recvattach.h"
45
46 #include "charset.h"
47 #include "mime.h"
48
49 /* Reads an arbitrarily long header field, and looks ahead for continuation
50  * lines.  ``line'' must point to a dynamically allocated string; it is
51  * increased if more space is required to fit the whole line.
52  */
53 ssize_t mutt_read_rfc822_line(FILE *f, char **line, ssize_t *n)
54 {
55     ssize_t pos = 0;
56
57     for (;;) {
58         char *p = *line;
59
60         /* end of file or end of headers */
61         if (!fgets(p + pos, *n - pos, f) || (ISSPACE(*p) && pos == 0)) {
62             *p = '\0';
63             return 0;
64         }
65
66         pos += m_strlen(p + pos);
67         if (p[pos - 1] == '\n') {
68             int c;
69
70             /* remove trailing spaces. safe: p[0] is not a space */
71             do {
72                 p[--pos] = '\0';
73             } while (ISSPACE(p[pos]));
74
75             /* check to see if the next line is a continuation line */
76             c = fgetc(f);
77             if (c != ' ' && c != '\t') {
78                 /* next line is a separate header field or EOH */
79                 ungetc(c, f);
80                 return pos;
81             }
82
83             /* eat tabs and spaces from the beginning of the continuation line */
84             do {
85                 c = fgetc(f);
86             } while (c == ' ' || c == '\t');
87             ungetc(c, f);
88
89             /* string is still terminated because we removed at least one
90                whitespace char above */
91             p[pos++] = ' ';
92         }
93
94         if (*n < pos + STRING) {
95             /* grow the buffer */
96             *n += STRING;
97             p_realloc(line, *n);
98         }
99     }
100 }
101
102 /* TODO: Make that a string list somehow */
103 string_list_t *mutt_parse_references(char *s, int in_reply_to)
104 {
105     string_list_t *lst = NULL;
106     int n = 0;
107     char *o = NULL;
108
109     /* some mail clients add other garbage besides message-ids, so do a quick
110      * check to make sure this looks like a valid message-id
111      * some idiotic clients also break their message-ids between lines, deal
112      * with that too (give up if it's more than two lines, though)
113      */
114
115     for (s = strtok(s, " \t;"); s; s = strtok(NULL, " \t;")) {
116         char *new = NULL;
117
118         if (*s == '<') {
119             n = m_strlen(s);
120             if (s[n - 1] != '>') {
121                 o = s;
122                 continue;
123             }
124
125             new = m_strdup(s);
126         } else if (o) {
127             ssize_t m = m_strlen(s);
128
129             if (s[m - 1] != '>') {
130                 o = NULL;
131             } else {
132                 new = p_new(char, n + m + 1);
133                 strcpy(new, o);
134                 strcpy(new + n, s);
135             }
136         }
137
138         /* make sure that this really does look like a message-id.
139          * it should have exactly one @, and if we're looking at
140          * an in-reply-to header, make sure that the part before
141          * the @ has more than eight characters or it's probably
142          * an email address
143          */
144         if (new) {
145             char *at = strchr(new, '@');
146             string_list_t *tmp;
147
148             if (!at || strchr(at + 1, '@') || (in_reply_to && at - new <= 8)) {
149                 p_delete(&new);
150                 continue;
151             }
152
153             tmp = p_new(string_list_t, 1);
154             tmp->data = new;
155             tmp->next = lst;
156             lst = tmp;
157         }
158     }
159
160     return lst;
161 }
162
163 int mutt_check_encoding(const char *s)
164 {
165     int tok = mime_which_token(s, -1);
166     switch (tok) {
167       case MIME_7BIT:
168         return ENC7BIT;
169       case MIME_8BIT:
170         return ENC8BIT;
171       case MIME_BINARY:
172         return ENCBINARY;
173       case MIME_QUOTED_PRINTABLE:
174         return ENCQUOTEDPRINTABLE;
175       case MIME_BASE64:
176         return ENCBASE64;
177       case MIME_X_UUENCODE:
178         return ENCUUENCODED;
179       default:
180         return ENCOTHER;
181     }
182 }
183
184 int mutt_check_mime_type(const char *s)
185 {
186     int tok;
187
188     if (!m_strcmp(s, "*") || !m_strcmp(s, ".*"))
189         return TYPEANY;
190
191     tok = mime_which_token(s, -1);
192     switch (tok) {
193       case MIME_TEXT:        return TYPETEXT;
194       case MIME_MULTIPART:   return TYPEMULTIPART;
195       case MIME_APPLICATION: return TYPEAPPLICATION;
196       case MIME_MESSAGE:     return TYPEMESSAGE;
197       case MIME_IMAGE:       return TYPEIMAGE;
198       case MIME_AUDIO:       return TYPEAUDIO;
199       case MIME_VIDEO:       return TYPEVIDEO;
200       case MIME_MODEL:       return TYPEMODEL;
201       default:               return TYPEOTHER;
202     }
203 }
204
205 static PARAMETER *parse_parameters(const char *s)
206 {
207     PARAMETER *res = NULL;
208     PARAMETER **list = &res;
209
210     while (*s) {
211         const char *p;
212         PARAMETER *new;
213         int i;
214
215         s = skipspaces(s);
216         if (*s == '=')             /* parameters are fucked up, go away */
217             break;
218
219         p = strpbrk(s, "=;");
220         if (!p)
221             break;
222
223         if (*p == ';') {
224             /* if we hit a ; now the parameter has no value, just skip it */
225             s = p + 1;
226             continue;
227         }
228
229         i = p - s;
230         new = parameter_new();
231         new->attribute = p_dupstr(s, i);
232
233         while (--i >= 0 && ISSPACE(new->attribute[i])) {
234             new->attribute[i] = '\0';
235         }
236         s = skipspaces(p + 1);                      /* skip over the = */
237
238         if (*s == '"') {
239             char buffer[LONG_STRING];
240             int state_ascii = 1;
241
242             s++;
243             for (i = 0; *s && i < ssizeof(buffer) - 1; i++, s++) {
244                 if (!option(OPTSTRICTMIME)) {
245                     /* As iso-2022-* has a characer of '"' with non-ascii state,
246                      * ignore it. */
247                     if (*s == 0x1b && i < ssizeof(buffer) - 2) {
248                         state_ascii = s[1] == '(' && (s[2] == 'B' || s[2] == 'J');
249                     }
250                 }
251                 if (state_ascii && *s == '"')
252                     break;
253
254                 if (*s == '\\') {
255                     buffer[i] = *++s;
256                 } else {
257                     buffer[i] = *s;
258                 }
259             }
260
261             new->value = p_dupstr(buffer, i);
262         } else {
263             for (p = s; *p && *p != ' ' && *p != ';'; p++);
264             new->value = p_dupstr(s, p - s);
265         }
266
267         *list = new;
268         list = &new->next;
269
270         s = strchr(s, ';');           /* Find the next parameter */
271         if (!s)
272             break;                    /* no more parameters */
273     }
274
275     rfc2231_decode_parameters(&res);
276     return res;
277 }
278
279 void mutt_parse_content_type(char *s, BODY *ct)
280 {
281     char *pc;
282     char *subtype;
283
284     p_delete(&ct->subtype);
285     parameter_list_wipe(&ct->parameter);
286
287     /* First extract any existing parameters */
288     if ((pc = strchr(s, ';')) != NULL) {
289         *pc++ = '\0';
290         ct->parameter = parse_parameters(vskipspaces(pc));
291
292         /* Some pre-RFC1521 gateways still use the "name=filename" convention,
293          * but if a filename has already been set in the content-disposition,
294          * let that take precedence, and don't set it here */
295         pc = mutt_get_parameter("name", ct->parameter);
296         if (pc && !ct->filename)
297             ct->filename = m_strdup(pc);
298     }
299
300     /* Now get the subtype */
301     if ((subtype = strchr (s, '/'))) {
302         *subtype++ = '\0';
303         for (pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++);
304         ct->subtype = p_dupstr(subtype, pc - subtype);
305     }
306
307     /* Finally, get the major type */
308     ct->type = mutt_check_mime_type(s);
309
310     if (ct->type == TYPEOTHER) {
311         ct->xtype = m_strdup(s);
312     }
313
314     if (!ct->subtype) {
315         /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
316          * field, so we can attempt to convert the type to BODY here.
317          */
318         switch (ct->type) {
319             char buffer[SHORT_STRING];
320
321           case TYPETEXT:
322             ct->subtype = m_strdup("plain");
323             break;
324
325           case TYPEAUDIO:
326             ct->subtype = m_strdup("basic");
327             break;
328
329           case TYPEMESSAGE:
330             ct->subtype = m_strdup("rfc822");
331             break;
332
333           case TYPEOTHER:
334             ct->type = TYPEAPPLICATION;
335             snprintf(buffer, sizeof(buffer), "x-%s", s);
336             ct->subtype = m_strdup(buffer);
337             break;
338
339           default:
340             ct->subtype = m_strdup("x-unknown");
341             break;
342         }
343     }
344
345     /* Default character set for text types. */
346     if (ct->type == TYPETEXT) {
347         pc = mutt_get_parameter("charset", ct->parameter);
348         if (!pc) {
349             mutt_set_parameter("charset",
350                                option(OPTSTRICTMIME) ? "us-ascii" :
351                                mutt_get_first_charset(AssumedCharset),
352                                &ct->parameter);
353         }
354     }
355 }
356
357 static void parse_content_disposition(char *s, BODY *ct)
358 {
359     if (!ascii_strncasecmp(s, "inline", 6)) {
360         ct->disposition = DISPINLINE;
361     } else if (!ascii_strncasecmp(s, "form-data", 9)) {
362         ct->disposition = DISPFORMDATA;
363     } else {
364         ct->disposition = DISPATTACH;
365     }
366
367     /* Check to see if a default filename was given */
368     if ((s = strchr (s, ';'))) {
369         PARAMETER *parms = parse_parameters(vskipspaces(s));
370
371         if ((s = mutt_get_parameter("filename", parms)))
372             m_strreplace(&ct->filename, s);
373         if ((s = mutt_get_parameter ("name", parms)))
374             ct->form_name = m_strdup(s);
375
376         parameter_list_wipe(&parms);
377     }
378 }
379
380 /* args:
381  *      fp      stream to read from
382  *
383  *      digest  1 if reading subparts of a multipart/digest, 0
384  *              otherwise
385  */
386 BODY *mutt_read_mime_header(FILE *fp, int digest)
387 {
388     BODY *body = mutt_new_body ();
389     char *line = p_new(char, LONG_STRING);
390     ssize_t linelen = LONG_STRING;
391     char *p;
392
393     body->hdr_offset  = ftello(fp);
394     body->encoding    = ENC7BIT;    /* default from RFC1521 */
395     body->disposition = DISPINLINE;
396     body->type        = digest ? TYPEMESSAGE : TYPETEXT;
397
398     while (mutt_read_rfc822_line(fp, &line, &linelen)) {
399         /* Find the value of the current header */
400         if ((p = strchr(line, ':'))) {
401             *p++ = '\0';
402             p = vskipspaces(p);
403             if (!*p)
404                 continue;
405         } else {
406             debug_print (1, ("bogus MIME header: %s\n", line));
407             break;
408         }
409
410         switch (mime_which_token(line, -1)) {
411           case MIME_CONTENT_TYPE:
412             mutt_parse_content_type (p, body);
413             break;
414
415           case MIME_CONTENT_TRANSFER_ENCODING:
416             body->encoding = mutt_check_encoding (p);
417             break;
418
419           case MIME_CONTENT_DISPOSITION:
420             parse_content_disposition(p, body);
421             break;
422
423           case MIME_CONTENT_DESCRIPTION:
424             m_strreplace(&body->description, p);
425             rfc2047_decode(&body->description);
426             break;
427
428           default: break;
429         }
430     }
431
432     body->offset = ftello(fp);       /* Mark the start of the real data */
433     if (!body->subtype) {
434         if (body->type == TYPETEXT)
435             body->subtype = m_strdup("plain");
436         if (body->type == TYPEMESSAGE)
437             body->subtype = m_strdup("rfc822");
438     }
439
440     p_delete(&line);
441     return (body);
442 }
443
444 void mutt_parse_part(FILE *fp, BODY *b)
445 {
446     char *bound = 0;
447
448     switch (b->type) {
449       case TYPEMULTIPART:
450         bound = mutt_get_parameter("boundary", b->parameter);
451         fseeko(fp, b->offset, SEEK_SET);
452         b->parts = mutt_parse_multipart(fp, bound, b->offset + b->length,
453                                         mime_which_token(b->subtype, -1) == MIME_DIGEST);
454         break;
455
456       case TYPEMESSAGE:
457         if (b->subtype) {
458             fseeko(fp, b->offset, SEEK_SET);
459
460             if (mutt_is_message_type(b->type, b->subtype)) {
461                 b->parts = mutt_parse_messageRFC822(fp, b);
462             } else
463             if (mime_which_token(b->subtype, -1) == MIME_EXTERNAL_BODY) {
464                 b->parts = mutt_read_mime_header(fp, 0);
465             } else {
466                 return;
467             }
468         }
469         break;
470
471       default:
472         return;
473     }
474
475     /* try to recover from parsing error */
476     if (!b->parts) {
477         b->type = TYPETEXT;
478         m_strreplace(&b->subtype, "plain");
479     }
480 }
481
482 /* parse a MESSAGE/RFC822 body
483  *
484  * args:
485  *      fp              stream to read from
486  *
487  *      parent          structure which contains info about the message/rfc822
488  *                      body part
489  *
490  * NOTE: this assumes that `parent->length' has been set!
491  */
492 BODY *mutt_parse_messageRFC822(FILE * fp, BODY * parent)
493 {
494     BODY *msg;
495
496     parent->hdr = header_new();
497     parent->hdr->offset = ftello(fp);
498     parent->hdr->env    = mutt_read_rfc822_header(fp, parent->hdr, 0, 0);
499
500     msg = parent->hdr->content;
501
502     /* ignore the length given in the content-length since it could be wrong
503        and we already have the info to calculate the correct length */
504     /* if (msg->length == -1) */
505     /* if body of this message is empty, we can end up with a negative length */
506     msg->length = MAX(0, parent->length - (msg->offset - parent->offset));
507
508     mutt_parse_part(fp, msg);
509
510     return msg;
511 }
512
513 /* parse a multipart structure
514  *
515  * args:
516  *      fp              stream to read from
517  *
518  *      bound           body separator
519  *
520  *      end_off         length of the multipart body (used when the final
521  *                      boundary is missing to avoid reading too far)
522  *
523  *      digest          1 if reading a multipart/digest, 0 otherwise
524  */
525
526 BODY *
527 mutt_parse_multipart(FILE *fp, const char *bound, off_t end_off, int digest)
528 {
529     char buffer[LONG_STRING];
530     BODY *head = NULL;
531     BODY **last = &head;
532     int blen = m_strlen(bound);
533     int final = 0;                /* did we see the ending boundary? */
534
535     if (!blen) {
536         mutt_error _("multipart message has no boundary parameter!");
537         return NULL;
538     }
539
540     while (ftello(fp) < end_off && fgets(buffer, sizeof(buffer), fp)) {
541         int len, crlf, i;
542
543         len  = m_strlen(buffer);
544         crlf = len > 1 && buffer[len - 2] == '\r';
545
546         if (buffer[0] == '-' && buffer[1] == '-'
547         && !m_strncmp(buffer + 2, bound, blen))
548         {
549             if (*last) {
550                 BODY *b = *last;
551
552                 /* if the body is empty, we can end up with a -1 length */
553                 b->length = MAX(0, ftello(fp) - b->offset - len - 1 - crlf);
554                 if (b->parts && b->parts->length == 0) {
555                     b->parts->length = ftello(fp) - b->parts->offset
556                                      - len - 1 - crlf;
557                 }
558             }
559
560             /* Remove any trailing whitespace, up to the length of the boundary */
561             for (i = len - 1; ISSPACE(buffer[i]) && i >= blen + 2; i--)
562                 buffer[i] = '\0';
563
564             /* Check for the end boundary */
565             final = buffer[blen + 3] == '-' && buffer[blen + 4] == '-';
566             if (final)
567                 break;
568
569             if (buffer[2 + blen] == '\0') {
570                 BODY *new = mutt_read_mime_header(fp, digest);
571
572                 /*
573                  * Consistency checking - catch
574                  * bad attachment end boundaries
575                  */
576
577                 if (new->offset > end_off) {
578                     mutt_free_body(&new);
579                     break;
580                 }
581
582                 if (*last)
583                     last = &(*last)->next;
584                 *last = new;
585             }
586         }
587     }
588
589     /* in case of missing end boundary, set the length to something reasonable */
590     if (*last && (*last)->length == 0 && !final)
591         (*last)->length = end_off - (*last)->offset;
592
593     /* parse recursive MIME parts */
594     {
595         BODY *b;
596         for (b = head; b; b = b->next)
597             mutt_parse_part(fp, b);
598     }
599
600     return (head);
601 }
602
603 static const char *
604 uncomment_timezone(char *buf, size_t buflen, const char *tz)
605 {
606     char *p;
607
608     if (*tz != '(')
609         return tz;                  /* no need to do anything */
610
611     tz = vskipspaces(tz + 1);
612     p = strpbrk(tz, " )");
613     if (!p)
614         return tz;
615
616     m_strncpy(buf, buflen, tz, p - tz);
617     return buf;
618 }
619
620 static struct tz_t {
621     char tzname[5];
622     unsigned char zhours;
623     unsigned char zminutes;
624     unsigned char zoccident;      /* west of UTC? */
625 } TimeZones[] = {
626     {"aat", 1, 0, 1},             /* Atlantic Africa Time */
627     {"adt", 4, 0, 0},             /* Arabia DST */
628     {"ast", 3, 0, 0},             /* Arabia */
629     /*{ "ast",   4,  0, 1 }, *//* Atlantic */
630     {"bst", 1, 0, 0},             /* British DST */
631     {"cat", 1, 0, 0},             /* Central Africa */
632     {"cdt", 5, 0, 1},
633     {"cest", 2, 0, 0},            /* Central Europe DST */
634     {"cet", 1, 0, 0},             /* Central Europe */
635     {"cst", 6, 0, 1},
636     /*{ "cst",   8,  0, 0 }, *//* China */
637     /*{ "cst",   9, 30, 0 }, *//* Australian Central Standard Time */
638     {"eat", 3, 0, 0},             /* East Africa */
639     {"edt", 4, 0, 1},
640     {"eest", 3, 0, 0},            /* Eastern Europe DST */
641     {"eet", 2, 0, 0},             /* Eastern Europe */
642     {"egst", 0, 0, 0},            /* Eastern Greenland DST */
643     {"egt", 1, 0, 1},             /* Eastern Greenland */
644     {"est", 5, 0, 1},
645     {"gmt", 0, 0, 0},
646     {"gst", 4, 0, 0},             /* Presian Gulf */
647     {"hkt", 8, 0, 0},             /* Hong Kong */
648     {"ict", 7, 0, 0},             /* Indochina */
649     {"idt", 3, 0, 0},             /* Israel DST */
650     {"ist", 2, 0, 0},             /* Israel */
651     /*{ "ist",   5, 30, 0 }, *//* India */
652     {"jst", 9, 0, 0},             /* Japan */
653     {"kst", 9, 0, 0},             /* Korea */
654     {"mdt", 6, 0, 1},
655     {"met", 1, 0, 0},             /* this is now officially CET */
656     {"msd", 4, 0, 0},             /* Moscow DST */
657     {"msk", 3, 0, 0},             /* Moscow */
658     {"mst", 7, 0, 1},
659     {"nzdt", 13, 0, 0},           /* New Zealand DST */
660     {"nzst", 12, 0, 0},           /* New Zealand */
661     {"pdt", 7, 0, 1},
662     {"pst", 8, 0, 1},
663     {"sat", 2, 0, 0},             /* South Africa */
664     {"smt", 4, 0, 0},             /* Seychelles */
665     {"sst", 11, 0, 1},            /* Samoa */
666     /*{ "sst",   8,  0, 0 }, *//* Singapore */
667     {"utc", 0, 0, 0},
668     {"wat", 0, 0, 0},             /* West Africa */
669     {"west", 1, 0, 0},            /* Western Europe DST */
670     {"wet", 0, 0, 0},             /* Western Europe */
671     {"wgst", 2, 0, 1},            /* Western Greenland DST */
672     {"wgt", 3, 0, 1},             /* Western Greenland */
673     {"wst", 8, 0, 0},             /* Western Australia */
674 };
675
676 /* parses a date string in RFC822 format:
677  *
678  * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
679  *
680  * This routine assumes that `h' has been initialized to 0.  the `timezone'
681  * field is optional, defaulting to +0000 if missing.
682  */
683 time_t mutt_parse_date(const char *s, HEADER *h)
684 {
685     int zhours = 0, zminutes = 0, zoccident = 0;
686     char scratch[SHORT_STRING];
687     struct tm tm;
688     int count = 0;
689     char *p;
690
691     /* Don't modify our argument. Fixed-size buffer is ok here since
692        the date format imposes a natural limit.  */
693
694     m_strcpy(scratch, sizeof(scratch), s);
695
696     /* kill the day of the week, if it exists. */
697     p = strchr(scratch, ',');
698     p = vskipspaces(p ? p + 1 : scratch);
699
700     p_clear(&tm, 1);
701
702     while ((p = strtok (p, " \t")) != NULL) {
703         char tzstr[SHORT_STRING];
704         const char *ptz;
705
706         switch (count) {
707           case 0:                    /* day of the month */
708             if (!isdigit((unsigned char)*p))
709                 return -1;
710             tm.tm_mday = atoi(p);
711             if (tm.tm_mday > 31)
712                 return -1;
713             break;
714
715           case 1:                    /* month of the year */
716             tm.tm_mon = mutt_check_month(p);
717             if (tm.tm_mon < 0)
718                 return -1;
719             break;
720
721           case 2:                    /* year */
722             tm.tm_year = atoi(p);
723             if (tm.tm_year < 50)
724                 tm.tm_year += 100;
725             else if (tm.tm_year >= 1900)
726                 tm.tm_year -= 1900;
727             break;
728
729           case 3:                    /* time of day */
730             tm.tm_hour = strtol(p, &p, 10);
731             if (*p++ != ':')
732                 return -1;
733             tm.tm_min  = strtol(p, &p, 10);
734             if (*p++ == ':') {
735                 tm.tm_sec = strtol(p, &p, 10);
736             } else {
737                 tm.tm_sec = 0;
738             }
739             break;
740
741           case 4:                    /* timezone */
742             /* sometimes we see things like (MST) or (-0700) so attempt to
743              * compensate by uncommenting the string if non-RFC822 compliant
744              */
745             ptz = uncomment_timezone(tzstr, sizeof(tzstr), p);
746
747             if (*ptz == '+' || *ptz == '-') {
748                 if (isdigit((unsigned char)ptz[1])
749                 &&  isdigit((unsigned char)ptz[2])
750                 &&  isdigit((unsigned char)ptz[3])
751                 &&  isdigit((unsigned char)ptz[4]))
752                 {
753                     zoccident = ptz[0] == '-';
754                     zhours    = (ptz[1] - '0') * 10 + (ptz[2] - '0');
755                     zminutes  = (ptz[3] - '0') * 10 + (ptz[4] - '0');
756                 }
757             } else {
758                 struct tz_t *tz;
759
760                 /* This is safe to do: A pointer to a struct equals a pointer to its
761                  * first element*/
762                 tz = bsearch(ptz, TimeZones, countof(TimeZones), sizeof(TimeZones[0]),
763                              (int (*)(const void *, const void *))ascii_strcasecmp);
764
765                 if (tz) {
766                     zhours = tz->zhours;
767                     zminutes = tz->zminutes;
768                     zoccident = tz->zoccident;
769                 }
770
771                 /* ad hoc support for the European MET (now officially CET) TZ */
772                 if (ascii_strcasecmp(p, "MET") == 0) {
773                     if ((p = strtok (NULL, " \t")) && !ascii_strcasecmp(p, "DST")) {
774                         zhours++;
775                     }
776                 }
777             }
778             break;
779         }
780         count++;
781         p = NULL;
782     }
783
784     if (count < 4) {  /* don't check for missing timezone */
785         debug_print (1, ("error parsing date format, using received time\n"));
786         return -1;
787     }
788
789     if (h) {
790         h->zhours    = zhours;
791         h->zminutes  = zminutes;
792         h->zoccident = zoccident;
793     }
794
795     return mutt_mktime(&tm, 0) + (zoccident ? 1 : -1) * (zhours * 3600 + zminutes * 60);
796 }
797
798 string_list_t **mutt_parse_rfc822_line(ENVELOPE *e, HEADER *hdr, char *line, char *p,
799                               short weed, short do_2047, string_list_t **user_hdrs)
800 {
801     switch (mime_which_token(line, -1)) {
802       case MIME_APPARENTLY_FROM:
803         e->from = rfc822_parse_adrlist (e->from, p);
804         break;
805
806       case MIME_APPARENTLY_TO:
807         e->to = rfc822_parse_adrlist (e->to, p);
808         break;
809
810       case MIME_BCC:
811         e->bcc = rfc822_parse_adrlist (e->bcc, p);
812         break;
813
814       case MIME_CC:
815         e->cc = rfc822_parse_adrlist (e->cc, p);
816         break;
817
818       case MIME_CONTENT_DESCRIPTION:
819         if (hdr) {
820             m_strreplace(&hdr->content->description, p);
821             rfc2047_decode(&hdr->content->description);
822         }
823         break;
824
825       case MIME_CONTENT_DISPOSITION:
826         if (hdr)
827             parse_content_disposition(p, hdr->content);
828         break;
829
830       case MIME_CONTENT_LENGTH:
831         if (hdr) {
832             if ((hdr->content->length = atoi(p)) < 0)
833                 hdr->content->length = -1;
834         }
835         break;
836
837       case MIME_CONTENT_TRANSFER_ENCODING:
838         if (hdr)
839             hdr->content->encoding = mutt_check_encoding(p);
840         break;
841
842       case MIME_CONTENT_TYPE:
843         if (hdr)
844             mutt_parse_content_type (p, hdr->content);
845         break;
846
847       case MIME_DATE:
848         m_strreplace(&e->date, p);
849         if (hdr)
850             hdr->date_sent = mutt_parse_date (p, hdr);
851         break;
852
853       case MIME_EXPIRES:
854         if (hdr && mutt_parse_date (p, NULL) < time (NULL))
855             hdr->expired = 1;
856         break;
857
858 #ifdef USE_NNTP
859       case MIME_FOLLOWUP_TO:
860         if (!e->followup_to) {
861             m_strrtrim(p);
862             e->followup_to = m_strdup(skipspaces(p));
863         }
864         break;
865 #endif
866
867       case MIME_FROM:
868         e->from = rfc822_parse_adrlist(e->from, p);
869         /* don't leave from info NULL if there's an invalid address (or
870          * whatever) in From: field; mutt would just display it as empty
871          * and mark mail/(esp.) news article as your own. aaargh! this
872          * bothered me for _years_ */
873         if (!e->from) {
874             e->from = address_new();
875             e->from->personal = m_strdup(p);
876         }
877         break;
878
879       case MIME_IN_REPLY_TO:
880         string_list_wipe(&e->in_reply_to);
881         e->in_reply_to = mutt_parse_references(p, 1);
882         break;
883
884       case MIME_LINES:
885         if (hdr) {
886             /* HACK - mutt has, for a very short time, produced negative
887                Lines header values.  Ignore them. */
888             hdr->lines = MAX(0, atoi(p));
889         }
890         break;
891
892       case MIME_LIST_POST:
893         /* RFC 2369.  FIXME: We should ignore whitespace, but don't. */
894         if (strncmp(p, "NO", 2)) {
895             char *beg, *end;
896
897             for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
898                 ++beg;
899                 if (!(end = strchr (beg, '>')))
900                     break;
901
902                 /* Take the first mailto URL */
903                 if (url_check_scheme (beg) == U_MAILTO) {
904                     p_delete(&e->list_post);
905                     e->list_post = p_dupstr(beg, end - beg);
906                     break;
907                 }
908             }
909         }
910         break;
911
912       case MIME_MAIL_FOLLOWUP_TO:
913         e->mail_followup_to = rfc822_parse_adrlist(e->mail_followup_to, p);
914         break;
915
916       case MIME_MAIL_REPLY_TO:
917         address_list_wipe(&e->reply_to);
918         e->reply_to = rfc822_parse_adrlist(e->reply_to, p);
919         break;
920
921       case MIME_MESSAGE_ID:
922         {
923             const char *beg, *end;
924
925             /* We add a new "Message-ID:" when building a message */
926             p_delete(&e->message_id);
927
928             if ((beg = strchr(p, '<')) && (end = strchr(beg, '>')))
929                 e->message_id = p_dupstr(beg, (end - beg) + 1);
930         }
931         break;
932
933       case MIME_MIME_VERSION:
934         if (hdr)
935             hdr->mime = 1;
936         break;
937
938 #ifdef USE_NNTP
939       case MIME_NEWSGROUPS:
940         p_delete(&e->newsgroups);
941         m_strrtrim(p);
942         e->newsgroups = m_strdup(skipspaces(p));
943         break;
944 #endif
945
946       case MIME_ORGANIZATION:
947         if (!e->organization && mime_which_token(p, -1) == MIME_UNKNOWN)
948             e->organization = m_strdup(p);
949         break;
950
951       case MIME_RECEIVED:
952         if (hdr && !hdr->received) {
953             char *d = strchr(p, ';');
954             if (d)
955                 hdr->received = mutt_parse_date(d + 1, NULL);
956         }
957         break;
958
959       case MIME_REFERENCES:
960         string_list_wipe(&e->references);
961         e->references = mutt_parse_references(p, 0);
962         break;
963
964       case MIME_REPLY_TO:
965         e->reply_to = rfc822_parse_adrlist(e->reply_to, p);
966         break;
967
968       case MIME_RETURN_PATH:
969         e->return_path = rfc822_parse_adrlist(e->return_path, p);
970         break;
971
972       case MIME_SENDER:
973         e->sender = rfc822_parse_adrlist (e->sender, p);
974         break;
975
976       case MIME_STATUS:
977         if (hdr) {
978             while (*p) {
979                 switch (*p) {
980                   case 'r':
981                     hdr->replied = 1;
982                     break;
983                   case 'O':
984                     hdr->old = 1;
985                     break;
986                   case 'R':
987                     hdr->read = 1;
988                     break;
989                 }
990                 p++;
991             }
992         }
993         break;
994
995       case MIME_SUBJECT:
996         if (!e->subject)
997             e->subject = m_strdup(p);
998         break;
999
1000       case MIME_SUPERCEDES:
1001       case MIME_SUPERSEDES:
1002         if (hdr)
1003             e->supersedes = m_strdup(p);
1004         break;
1005
1006       case MIME_TO:
1007         e->to = rfc822_parse_adrlist(e->to, p);
1008         break;
1009
1010 #ifdef USE_NNTP
1011       case MIME_X_COMMENT_TO:
1012         if (!e->x_comment_to)
1013             e->x_comment_to = m_strdup(p);
1014         break;
1015 #endif
1016
1017       case MIME_X_LABEL:
1018         e->x_label = m_strdup(p);
1019         break;
1020
1021 #ifdef USE_NNTP
1022       case MIME_XREF:
1023         if (!e->xref)
1024             e->xref = m_strdup(p);
1025         break;
1026 #endif
1027
1028       case MIME_X_STATUS:
1029         if (hdr) {
1030             while (*p) {
1031                 switch (*p) {
1032                   case 'A':
1033                     hdr->replied = 1;
1034                     break;
1035                   case 'D':
1036                     hdr->deleted = 1;
1037                     break;
1038                   case 'F':
1039                     hdr->flagged = 1;
1040                     break;
1041                   default:
1042                     break;
1043                 }
1044                 p++;
1045             }
1046         }
1047         break;
1048
1049       default:
1050         if (!user_hdrs)
1051             break;
1052
1053         /* restore the original line */
1054         line[m_strlen(line)] = ':';
1055
1056         if (weed && option(OPTWEED) && mutt_matches_ignore(line, Ignore)
1057         && !mutt_matches_ignore(line, UnIgnore)) {
1058             break;
1059         }
1060
1061         *user_hdrs = string_item_new();
1062         (*user_hdrs)->data = m_strdup(line);
1063         if (do_2047)
1064             rfc2047_decode(&(*user_hdrs)->data);
1065         return &(*user_hdrs)->next;
1066     }
1067
1068     return user_hdrs;
1069 }
1070
1071 /* mutt_read_rfc822_header() -- parses a RFC822 header
1072  *
1073  * Args:
1074  *
1075  * f            stream to read from
1076  *
1077  * hdr          header structure of current message (optional).
1078  *
1079  * user_hdrs    If set, store user headers.  Used for recall-message and
1080  *              postpone modes.
1081  *
1082  * weed         If this parameter is set and the user has activated the
1083  *              $weed option, honor the header weed list for user headers.
1084  *              Used for recall-message.
1085  *
1086  * Returns:     newly allocated envelope structure.  You should free it by
1087  *              envelope_delete() when envelope stay unneeded.
1088  */
1089 ENVELOPE *
1090 mutt_read_rfc822_header(FILE *f, HEADER *hdr, short user_hdrs, short weed)
1091 {
1092     ENVELOPE *e = envelope_new();
1093     string_list_t **last = user_hdrs ? &e->userhdrs : NULL;
1094
1095     char *line = p_new(char, LONG_STRING);
1096     ssize_t linelen = LONG_STRING;
1097     off_t loc;
1098
1099     if (hdr && !hdr->content) {
1100         hdr->content = mutt_new_body ();
1101
1102         /* set the defaults from RFC1521 */
1103         hdr->content->type     = TYPETEXT;
1104         hdr->content->subtype  = m_strdup("plain");
1105         hdr->content->encoding = ENC7BIT;
1106         hdr->content->length   = -1;
1107
1108         /* RFC 2183 says this is arbitrary */
1109         hdr->content->disposition = DISPINLINE;
1110     }
1111
1112     while ((loc = ftello(f)),
1113            mutt_read_rfc822_line(f, &line, &linelen))
1114     {
1115         char buf[LONG_STRING + 1] = "";
1116         char *p;
1117
1118         p = strpbrk(line, ": \t");
1119         if (!p || *p != ':') {
1120             char return_path[LONG_STRING];
1121             time_t t;
1122
1123             /* some bogus MTAs will quote the original "From " line */
1124             if (!m_strncmp(">From ", line, 6))
1125                 continue;               /* just ignore */
1126
1127             if (is_from(line, return_path, sizeof(return_path), &t)) {
1128                 /* MH somtimes has the From_ line in the middle of the header! */
1129                 if (hdr && !hdr->received)
1130                     hdr->received = t - mutt_local_tz(t);
1131                 continue;
1132             }
1133
1134             fseeko(f, loc, 0);
1135             break;                    /* end of header */
1136         }
1137
1138         if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf))) {
1139             if (!rx_list_match(NoSpamList, line)) {
1140                 /* if spam tag already exists, figure out how to amend it */
1141                 if (e->spam && *buf) {
1142                     if (SpamSep) {
1143                         /* If SpamSep defined, append with separator */
1144                         mutt_buffer_addstr(e->spam, SpamSep);
1145                         mutt_buffer_addstr(e->spam, buf);
1146                     } else {
1147                         /* else overwrite */
1148                         mutt_buffer_reset(e->spam);
1149                         mutt_buffer_addstr(e->spam, buf);
1150                     }
1151                 }
1152                 else if (!e->spam && *buf) {
1153                     /* spam tag is new, and match expr is non-empty; copy */
1154                     e->spam = mutt_buffer_from(NULL, buf);
1155                 }
1156                 else if (!e->spam) {
1157                     /* match expr is empty; plug in null string if no existing tag */
1158                     e->spam = mutt_buffer_from(NULL, "");
1159                 }
1160             }
1161         }
1162
1163         *p++ = '\0';
1164         p = vskipspaces(p);
1165         if (!*p)
1166             continue;                 /* skip empty header fields */
1167
1168         last = mutt_parse_rfc822_line(e, hdr, line, p, weed, 1, last);
1169     }
1170
1171     p_delete(&line);
1172
1173     if (hdr) {
1174         hdr->content->hdr_offset = hdr->offset;
1175         hdr->content->offset     = ftello(f);
1176         rfc2047_decode_envelope(e);
1177         /* check for missing or invalid date */
1178         if (hdr->date_sent <= 0) {
1179             debug_print(1, ("no date found, using received "
1180                             "time from msg separator\n"));
1181             hdr->date_sent = hdr->received;
1182         }
1183     }
1184
1185     return e;
1186 }
1187
1188 /* Compares mime types to the ok and except lists */
1189 static int count_body_parts_check(string_list_t **checklist, BODY *b)
1190 {
1191     string_list_t *type;
1192
1193     for (type = *checklist; type; type = type->next) {
1194         ATTACH_MATCH *a = (ATTACH_MATCH *)type->data;
1195
1196         if ((a->major_int == TYPEANY || a->major_int == b->type)
1197         &&  !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1198             return 1;
1199         }
1200     }
1201
1202     return 0;
1203 }
1204
1205 static int count_body_parts (BODY *body, int flags)
1206 {
1207     int count = 0;
1208     BODY *bp;
1209
1210     if (!body)
1211         return 0;
1212
1213     for (bp = body; bp != NULL; bp = bp->next) {
1214         /* Initial disposition is to count and not to recurse this part. */
1215         int shallcount, shallrecurse, iscontainer;
1216         int tok = mime_which_token(bp->subtype, -1);
1217
1218         iscontainer  = bp->type == TYPEMESSAGE || bp->type == TYPEMULTIPART;
1219
1220         /* don't recurse in external bodies or multipart/alternatives */
1221         shallrecurse = (bp->type == TYPEMESSAGE && tok != MIME_EXTERNAL_BODY)
1222                     || (bp->type == TYPEMULTIPART && tok != MIME_ALTERNATIVE);
1223
1224         /* Don't count top level containers and fundamental inlines */
1225         shallcount   = !(iscontainer && (flags & M_PARTS_TOPLEVEL))
1226                     && !(!iscontainer && bp->disposition == DISPINLINE && bp == body);
1227
1228         if (shallcount) {
1229             /* Turn off shallcount if message type is not in ok list,
1230              * or if it is in except list. Check is done separately for
1231              * inlines vs. attachments.
1232              */
1233
1234             if (bp->disposition == DISPATTACH) {
1235                 if (!count_body_parts_check(&AttachAllow, bp))
1236                     shallcount = 0;
1237                 if (count_body_parts_check(&AttachExclude, bp))
1238                     shallcount = 0;
1239             } else {
1240                 if (!count_body_parts_check(&InlineAllow, bp))
1241                     shallcount = 0;
1242                 if (count_body_parts_check(&InlineExclude, bp))
1243                     shallcount = 0;
1244             }
1245         }
1246
1247         bp->attach_qualifies = shallcount;
1248         count += shallcount;
1249
1250         if (shallrecurse) {
1251             bp->attach_count = count_body_parts(bp->parts,
1252                                                 flags & ~M_PARTS_TOPLEVEL);
1253             count += bp->attach_count;
1254         }
1255     }
1256
1257     return count;
1258 }
1259
1260 int mutt_count_body_parts(HEADER *hdr, int flags)
1261 {
1262     if (!option(OPTCOUNTATTACH))
1263         return 0;
1264
1265     if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1266         return hdr->attach_total;
1267
1268     if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1269         hdr->attach_total = count_body_parts(hdr->content,
1270                                              flags | M_PARTS_TOPLEVEL);
1271     else
1272         hdr->attach_total = 0;
1273
1274     hdr->attach_valid = 1;
1275     return hdr->attach_total;
1276 }