reorganize code so that the rfc822parse.c does not needs lib-crypt nor
[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
42 #include "recvattach.h"
43 #include "url.h"
44
45 #include "lib/debug.h"
46
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 LIST *mutt_parse_references(char *s, int in_reply_to)
104 {
105     LIST *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             LIST *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(LIST, 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 #define COMPARE(tok, value)                             \
166     if (!ascii_strncasecmp(tok, s, sizeof(tok) - 1)) {  \
167         return value;                                   \
168     }
169     COMPARE("7bit", ENC7BIT);
170     COMPARE("8bit", ENC8BIT);
171     COMPARE("binary", ENCBINARY);
172     COMPARE("quoted-printable", ENCQUOTEDPRINTABLE);
173     COMPARE("base64", ENCBASE64);
174     COMPARE("x-uuencode", ENCUUENCODED);
175 #undef COMPARE
176
177     return ENCOTHER;
178 }
179
180 int mutt_check_mime_type(const char *s)
181 {
182 #define COMPARE(tok, value)                             \
183     if (!ascii_strncasecmp(tok, s, sizeof(tok) - 1)) {  \
184         return value;                                   \
185     }
186   COMPARE("text", TYPETEXT);
187   COMPARE("multipart", TYPEMULTIPART);
188   COMPARE("application", TYPEAPPLICATION);
189   COMPARE("message", TYPEMESSAGE);
190   COMPARE("image", TYPEIMAGE);
191   COMPARE("audio", TYPEAUDIO);
192   COMPARE("video", TYPEVIDEO);
193   COMPARE("model", TYPEMODEL);
194   COMPARE("*",  TYPEANY);
195   COMPARE(".*", TYPEANY);
196 #undef COMPARE
197
198   return TYPEOTHER;
199 }
200
201 static PARAMETER *parse_parameters(const char *s)
202 {
203     PARAMETER *res = NULL;
204     PARAMETER **list = &res;
205
206     while (*s) {
207         const char *p;
208         PARAMETER *new;
209         int i;
210
211         s = skipspaces(s);
212         if (*s == '=')             /* parameters are fucked up, go away */
213             break;
214
215         p = strpbrk(s, "=;");
216         if (!p)
217             break;
218
219         if (*p == ';') {
220             /* if we hit a ; now the parameter has no value, just skip it */
221             s = p + 1;
222             continue;
223         }
224
225         i = p - s;
226         new = parameter_new();
227         new->attribute = p_dupstr(s, i);
228
229         while (--i >= 0 && ISSPACE(new->attribute[i])) {
230             new->attribute[i] = '\0';
231         }
232         s = skipspaces(p + 1);                      /* skip over the = */
233
234         if (*s == '"') {
235             char buffer[LONG_STRING];
236             int state_ascii = 1;
237
238             s++;
239             for (i = 0; *s && i < ssizeof(buffer) - 1; i++, s++) {
240                 if (!option(OPTSTRICTMIME)) {
241                     /* As iso-2022-* has a characer of '"' with non-ascii state,
242                      * ignore it. */
243                     if (*s == 0x1b && i < ssizeof(buffer) - 2) {
244                         state_ascii = s[1] == '(' && (s[2] == 'B' || s[2] == 'J');
245                     }
246                 }
247                 if (state_ascii && *s == '"')
248                     break;
249
250                 if (*s == '\\') {
251                     buffer[i] = *++s;
252                 } else {
253                     buffer[i] = *s;
254                 }
255             }
256
257             new->value = p_dupstr(buffer, i);
258         } else {
259             for (p = s; *p && *p != ' ' && *p != ';'; p++);
260             new->value = p_dupstr(s, p - s);
261         }
262
263         *list = new;
264         list = &new->next;
265
266         s = strchr(s, ';');           /* Find the next parameter */
267         if (!s)
268             break;                    /* no more parameters */
269     }
270
271     rfc2231_decode_parameters(&res);
272     return res;
273 }
274
275 void mutt_parse_content_type(char *s, BODY *ct)
276 {
277     char *pc;
278     char *subtype;
279
280     p_delete(&ct->subtype);
281     parameter_delete(&ct->parameter);
282
283     /* First extract any existing parameters */
284     if ((pc = strchr(s, ';')) != NULL) {
285         *pc++ = '\0';
286         ct->parameter = parse_parameters(vskipspaces(pc));
287
288         /* Some pre-RFC1521 gateways still use the "name=filename" convention,
289          * but if a filename has already been set in the content-disposition,
290          * let that take precedence, and don't set it here */
291         pc = mutt_get_parameter("name", ct->parameter);
292         if (pc && !ct->filename)
293             ct->filename = m_strdup(pc);
294     }
295
296     /* Now get the subtype */
297     if ((subtype = strchr (s, '/'))) {
298         *subtype++ = '\0';
299         for (pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++);
300         ct->subtype = p_dupstr(subtype, pc - subtype);
301     }
302
303     /* Finally, get the major type */
304     ct->type = mutt_check_mime_type(s);
305
306     if (ct->type == TYPEOTHER) {
307         ct->xtype = m_strdup(s);
308     }
309
310     if (!ct->subtype) {
311         /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
312          * field, so we can attempt to convert the type to BODY here.
313          */
314         switch (ct->type) {
315             char buffer[SHORT_STRING];
316
317           case TYPETEXT:
318             ct->subtype = m_strdup("plain");
319             break;
320
321           case TYPEAUDIO:
322             ct->subtype = m_strdup("basic");
323             break;
324
325           case TYPEMESSAGE:
326             ct->subtype = m_strdup("rfc822");
327             break;
328
329           case TYPEOTHER:
330             ct->type = TYPEAPPLICATION;
331             snprintf(buffer, sizeof(buffer), "x-%s", s);
332             ct->subtype = m_strdup(buffer);
333             break;
334
335           default:
336             ct->subtype = m_strdup("x-unknown");
337             break;
338         }
339     }
340
341     /* Default character set for text types. */
342     if (ct->type == TYPETEXT) {
343         pc = mutt_get_parameter("charset", ct->parameter);
344         if (!pc) {
345             mutt_set_parameter("charset",
346                                option(OPTSTRICTMIME) ? "us-ascii" :
347                                mutt_get_first_charset(AssumedCharset),
348                                &ct->parameter);
349         }
350     }
351 }
352
353 static void parse_content_disposition(char *s, BODY *ct)
354 {
355     if (!ascii_strncasecmp(s, "inline", 6)) {
356         ct->disposition = DISPINLINE;
357     } else if (!ascii_strncasecmp(s, "form-data", 9)) {
358         ct->disposition = DISPFORMDATA;
359     } else {
360         ct->disposition = DISPATTACH;
361     }
362
363     /* Check to see if a default filename was given */
364     if ((s = strchr (s, ';'))) {
365         PARAMETER *parms = parse_parameters(vskipspaces(s));
366
367         if ((s = mutt_get_parameter("filename", parms)))
368             m_strreplace(&ct->filename, s);
369         if ((s = mutt_get_parameter ("name", parms)))
370             ct->form_name = m_strdup(s);
371
372         parameter_delete(&parms);
373     }
374 }
375
376 /* args:
377  *      fp      stream to read from
378  *
379  *      digest  1 if reading subparts of a multipart/digest, 0
380  *              otherwise
381  */
382 BODY *mutt_read_mime_header(FILE *fp, int digest)
383 {
384     BODY *body = mutt_new_body ();
385     char *line = p_new(char, LONG_STRING);
386     ssize_t linelen = LONG_STRING;
387     char *p;
388
389     body->hdr_offset  = ftello(fp);
390     body->encoding    = ENC7BIT;    /* default from RFC1521 */
391     body->disposition = DISPINLINE;
392     body->type        = digest ? TYPEMESSAGE : TYPETEXT;
393
394     while (mutt_read_rfc822_line(fp, &line, &linelen)) {
395         /* Find the value of the current header */
396         if ((p = strchr(line, ':'))) {
397             *p++ = '\0';
398             p = vskipspaces(p);
399             if (!*p)
400                 continue;
401         } else {
402             debug_print (1, ("bogus MIME header: %s\n", line));
403             break;
404         }
405
406         if (!ascii_strncasecmp(line, "content-", 8)) {
407             if (!ascii_strcasecmp("type", line + 8))
408                 mutt_parse_content_type (p, body);
409             else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
410                 body->encoding = mutt_check_encoding (p);
411             else if (!ascii_strcasecmp ("disposition", line + 8))
412                 parse_content_disposition(p, body);
413             else if (!ascii_strcasecmp ("description", line + 8)) {
414                 m_strreplace(&body->description, p);
415                 rfc2047_decode(&body->description);
416             }
417         }
418     }
419
420     body->offset = ftello(fp);       /* Mark the start of the real data */
421     if (!body->subtype) {
422         if (body->type == TYPETEXT)
423             body->subtype = m_strdup("plain");
424         if (body->type == TYPEMESSAGE)
425             body->subtype = m_strdup("rfc822");
426     }
427
428     p_delete(&line);
429     return (body);
430 }
431
432 void mutt_parse_part(FILE *fp, BODY *b)
433 {
434     char *bound = 0;
435
436     switch (b->type) {
437       case TYPEMULTIPART:
438         bound = mutt_get_parameter("boundary", b->parameter);
439         fseeko(fp, b->offset, SEEK_SET);
440         b->parts = mutt_parse_multipart(fp, bound, b->offset + b->length,
441                            !ascii_strcasecmp("digest", b->subtype));
442         break;
443
444       case TYPEMESSAGE:
445         if (b->subtype) {
446             fseeko(fp, b->offset, SEEK_SET);
447
448             if (mutt_is_message_type(b->type, b->subtype)) {
449                 b->parts = mutt_parse_messageRFC822(fp, b);
450             } else
451             if (!ascii_strcasecmp(b->subtype, "external-body") == 0) {
452                 b->parts = mutt_read_mime_header(fp, 0);
453             } else {
454                 return;
455             }
456         }
457         break;
458
459       default:
460         return;
461     }
462
463     /* try to recover from parsing error */
464     if (!b->parts) {
465         b->type = TYPETEXT;
466         m_strreplace(&b->subtype, "plain");
467     }
468 }
469
470 /* parse a MESSAGE/RFC822 body
471  *
472  * args:
473  *      fp              stream to read from
474  *
475  *      parent          structure which contains info about the message/rfc822
476  *                      body part
477  *
478  * NOTE: this assumes that `parent->length' has been set!
479  */
480 BODY *mutt_parse_messageRFC822(FILE * fp, BODY * parent)
481 {
482     BODY *msg;
483
484     parent->hdr = header_new();
485     parent->hdr->offset = ftello(fp);
486     parent->hdr->env    = mutt_read_rfc822_header(fp, parent->hdr, 0, 0);
487
488     msg = parent->hdr->content;
489
490     /* ignore the length given in the content-length since it could be wrong
491        and we already have the info to calculate the correct length */
492     /* if (msg->length == -1) */
493     /* if body of this message is empty, we can end up with a negative length */
494     msg->length = MAX(0, parent->length - (msg->offset - parent->offset));
495
496     mutt_parse_part(fp, msg);
497
498     return msg;
499 }
500
501 /* parse a multipart structure
502  *
503  * args:
504  *      fp              stream to read from
505  *
506  *      bound           body separator
507  *
508  *      end_off         length of the multipart body (used when the final
509  *                      boundary is missing to avoid reading too far)
510  *
511  *      digest          1 if reading a multipart/digest, 0 otherwise
512  */
513
514 BODY *
515 mutt_parse_multipart(FILE *fp, const char *bound, off_t end_off, int digest)
516 {
517     char buffer[LONG_STRING];
518     BODY *head = NULL;
519     BODY **last = &head;
520     int blen = m_strlen(bound);
521     int final = 0;                /* did we see the ending boundary? */
522
523     if (!blen) {
524         mutt_error _("multipart message has no boundary parameter!");
525         return NULL;
526     }
527
528     while (ftello(fp) < end_off && fgets(buffer, sizeof(buffer), fp)) {
529         int len, crlf, i;
530
531         len  = m_strlen(buffer);
532         crlf = len > 1 && buffer[len - 2] == '\r';
533
534         if (buffer[0] == '-' && buffer[1] == '-'
535         && !m_strncmp(buffer + 2, bound, blen))
536         {
537             if (*last) {
538                 BODY *b = *last;
539
540                 /* if the body is empty, we can end up with a -1 length */
541                 b->length = MAX(0, ftello(fp) - b->offset - len - 1 - crlf);
542                 if (b->parts && b->parts->length == 0) {
543                     b->parts->length = ftello(fp) - b->parts->offset
544                                      - len - 1 - crlf;
545                 }
546             }
547
548             /* Remove any trailing whitespace, up to the length of the boundary */
549             for (i = len - 1; ISSPACE(buffer[i]) && i >= blen + 2; i--)
550                 buffer[i] = '\0';
551
552             /* Check for the end boundary */
553             final = buffer[blen + 3] == '-' && buffer[blen + 4] == '-';
554             if (final)
555                 break;
556
557             if (buffer[2 + blen] == '\0') {
558                 BODY *new = mutt_read_mime_header(fp, digest);
559
560                 /*
561                  * Consistency checking - catch
562                  * bad attachment end boundaries
563                  */
564
565                 if (new->offset > end_off) {
566                     mutt_free_body(&new);
567                     break;
568                 }
569
570                 if (*last)
571                     last = &(*last)->next;
572                 *last = new;
573             }
574         }
575     }
576
577     /* in case of missing end boundary, set the length to something reasonable */
578     if (*last && (*last)->length == 0 && !final)
579         (*last)->length = end_off - (*last)->offset;
580
581     /* parse recursive MIME parts */
582     {
583         BODY *b;
584         for (b = head; b; b = b->next)
585             mutt_parse_part(fp, b);
586     }
587
588     return (head);
589 }
590
591 static const char *
592 uncomment_timezone(char *buf, size_t buflen, const char *tz)
593 {
594     char *p;
595
596     if (*tz != '(')
597         return tz;                  /* no need to do anything */
598
599     tz = vskipspaces(tz + 1);
600     p = strpbrk(tz, " )");
601     if (!p)
602         return tz;
603
604     m_strncpy(buf, buflen, tz, p - tz);
605     return buf;
606 }
607
608 static struct tz_t {
609     char tzname[5];
610     unsigned char zhours;
611     unsigned char zminutes;
612     unsigned char zoccident;      /* west of UTC? */
613 } TimeZones[] = {
614     {"aat", 1, 0, 1},             /* Atlantic Africa Time */
615     {"adt", 4, 0, 0},             /* Arabia DST */
616     {"ast", 3, 0, 0},             /* Arabia */
617     /*{ "ast",   4,  0, 1 }, *//* Atlantic */
618     {"bst", 1, 0, 0},             /* British DST */
619     {"cat", 1, 0, 0},             /* Central Africa */
620     {"cdt", 5, 0, 1},
621     {"cest", 2, 0, 0},            /* Central Europe DST */
622     {"cet", 1, 0, 0},             /* Central Europe */
623     {"cst", 6, 0, 1},
624     /*{ "cst",   8,  0, 0 }, *//* China */
625     /*{ "cst",   9, 30, 0 }, *//* Australian Central Standard Time */
626     {"eat", 3, 0, 0},             /* East Africa */
627     {"edt", 4, 0, 1},
628     {"eest", 3, 0, 0},            /* Eastern Europe DST */
629     {"eet", 2, 0, 0},             /* Eastern Europe */
630     {"egst", 0, 0, 0},            /* Eastern Greenland DST */
631     {"egt", 1, 0, 1},             /* Eastern Greenland */
632     {"est", 5, 0, 1},
633     {"gmt", 0, 0, 0},
634     {"gst", 4, 0, 0},             /* Presian Gulf */
635     {"hkt", 8, 0, 0},             /* Hong Kong */
636     {"ict", 7, 0, 0},             /* Indochina */
637     {"idt", 3, 0, 0},             /* Israel DST */
638     {"ist", 2, 0, 0},             /* Israel */
639     /*{ "ist",   5, 30, 0 }, *//* India */
640     {"jst", 9, 0, 0},             /* Japan */
641     {"kst", 9, 0, 0},             /* Korea */
642     {"mdt", 6, 0, 1},
643     {"met", 1, 0, 0},             /* this is now officially CET */
644     {"msd", 4, 0, 0},             /* Moscow DST */
645     {"msk", 3, 0, 0},             /* Moscow */
646     {"mst", 7, 0, 1},
647     {"nzdt", 13, 0, 0},           /* New Zealand DST */
648     {"nzst", 12, 0, 0},           /* New Zealand */
649     {"pdt", 7, 0, 1},
650     {"pst", 8, 0, 1},
651     {"sat", 2, 0, 0},             /* South Africa */
652     {"smt", 4, 0, 0},             /* Seychelles */
653     {"sst", 11, 0, 1},            /* Samoa */
654     /*{ "sst",   8,  0, 0 }, *//* Singapore */
655     {"utc", 0, 0, 0},
656     {"wat", 0, 0, 0},             /* West Africa */
657     {"west", 1, 0, 0},            /* Western Europe DST */
658     {"wet", 0, 0, 0},             /* Western Europe */
659     {"wgst", 2, 0, 1},            /* Western Greenland DST */
660     {"wgt", 3, 0, 1},             /* Western Greenland */
661     {"wst", 8, 0, 0},             /* Western Australia */
662 };
663
664 /* parses a date string in RFC822 format:
665  *
666  * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
667  *
668  * This routine assumes that `h' has been initialized to 0.  the `timezone'
669  * field is optional, defaulting to +0000 if missing.
670  */
671 time_t mutt_parse_date(const char *s, HEADER *h)
672 {
673     int zhours = 0, zminutes = 0, zoccident = 0;
674     char scratch[SHORT_STRING];
675     struct tm tm;
676     int count = 0;
677     char *p;
678
679     /* Don't modify our argument. Fixed-size buffer is ok here since
680        the date format imposes a natural limit.  */
681
682     m_strcpy(scratch, sizeof(scratch), s);
683
684     /* kill the day of the week, if it exists. */
685     p = strchr(scratch, ',');
686     p = vskipspaces(p ? p + 1 : scratch);
687
688     p_clear(&tm, 1);
689
690     while ((p = strtok (p, " \t")) != NULL) {
691         char tzstr[SHORT_STRING];
692         const char *ptz;
693
694         switch (count) {
695           case 0:                    /* day of the month */
696             if (!isdigit((unsigned char)*p))
697                 return -1;
698             tm.tm_mday = atoi(p);
699             if (tm.tm_mday > 31)
700                 return -1;
701             break;
702
703           case 1:                    /* month of the year */
704             tm.tm_mon = mutt_check_month(p);
705             if (tm.tm_mon < 0)
706                 return -1;
707             break;
708
709           case 2:                    /* year */
710             tm.tm_year = atoi(p);
711             if (tm.tm_year < 50)
712                 tm.tm_year += 100;
713             else if (tm.tm_year >= 1900)
714                 tm.tm_year -= 1900;
715             break;
716
717           case 3:                    /* time of day */
718             tm.tm_hour = strtol(p, &p, 10);
719             if (*p++ != ':')
720                 return -1;
721             tm.tm_min  = strtol(p, &p, 10);
722             if (*p++ == ':') {
723                 tm.tm_sec = strtol(p, &p, 10);
724             } else {
725                 tm.tm_sec = 0;
726             }
727             break;
728
729           case 4:                    /* timezone */
730             /* sometimes we see things like (MST) or (-0700) so attempt to
731              * compensate by uncommenting the string if non-RFC822 compliant
732              */
733             ptz = uncomment_timezone(tzstr, sizeof(tzstr), p);
734
735             if (*ptz == '+' || *ptz == '-') {
736                 if (isdigit((unsigned char)ptz[1])
737                 &&  isdigit((unsigned char)ptz[2])
738                 &&  isdigit((unsigned char)ptz[3])
739                 &&  isdigit((unsigned char)ptz[4]))
740                 {
741                     zoccident = ptz[0] == '-';
742                     zhours    = (ptz[1] - '0') * 10 + (ptz[2] - '0');
743                     zminutes  = (ptz[3] - '0') * 10 + (ptz[4] - '0');
744                 }
745             } else {
746                 struct tz_t *tz;
747
748                 /* This is safe to do: A pointer to a struct equals a pointer to its
749                  * first element*/
750                 tz = bsearch(ptz, TimeZones, countof(TimeZones), sizeof(TimeZones[0]),
751                              (int (*)(const void *, const void *))ascii_strcasecmp);
752
753                 if (tz) {
754                     zhours = tz->zhours;
755                     zminutes = tz->zminutes;
756                     zoccident = tz->zoccident;
757                 }
758
759                 /* ad hoc support for the European MET (now officially CET) TZ */
760                 if (ascii_strcasecmp(p, "MET") == 0) {
761                     if ((p = strtok (NULL, " \t")) && !ascii_strcasecmp(p, "DST")) {
762                         zhours++;
763                     }
764                 }
765             }
766             break;
767         }
768         count++;
769         p = NULL;
770     }
771
772     if (count < 4) {  /* don't check for missing timezone */
773         debug_print (1, ("error parsing date format, using received time\n"));
774         return -1;
775     }
776
777     if (h) {
778         h->zhours    = zhours;
779         h->zminutes  = zminutes;
780         h->zoccident = zoccident;
781     }
782
783     return mutt_mktime(&tm, 0) + (zoccident ? 1 : -1) * (zhours * 3600 + zminutes * 60);
784 }
785
786 /*** XXX: MC READ MARK ***/
787
788 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
789                             short user_hdrs, short weed, short do_2047,
790                             LIST ** lastp)
791 {
792   int matched = 0;
793   LIST *last = NULL;
794
795   if (lastp)
796     last = *lastp;
797
798   switch (ascii_tolower (line[0])) {
799   case 'a':
800     if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
801       e->to = rfc822_parse_adrlist (e->to, p);
802       matched = 1;
803     }
804     else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
805       e->from = rfc822_parse_adrlist (e->from, p);
806       matched = 1;
807     }
808     break;
809
810   case 'b':
811     if (ascii_strcasecmp (line + 1, "cc") == 0) {
812       e->bcc = rfc822_parse_adrlist (e->bcc, p);
813       matched = 1;
814     }
815     break;
816
817   case 'c':
818     if (ascii_strcasecmp (line + 1, "c") == 0) {
819       e->cc = rfc822_parse_adrlist (e->cc, p);
820       matched = 1;
821     }
822     else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
823       if (ascii_strcasecmp (line + 8, "type") == 0) {
824         if (hdr)
825           mutt_parse_content_type (p, hdr->content);
826         matched = 1;
827       }
828       else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
829         if (hdr)
830           hdr->content->encoding = mutt_check_encoding (p);
831         matched = 1;
832       }
833       else if (ascii_strcasecmp (line + 8, "length") == 0) {
834         if (hdr) {
835           if ((hdr->content->length = atoi (p)) < 0)
836             hdr->content->length = -1;
837         }
838         matched = 1;
839       }
840       else if (ascii_strcasecmp (line + 8, "description") == 0) {
841         if (hdr) {
842           m_strreplace(&hdr->content->description, p);
843           rfc2047_decode (&hdr->content->description);
844         }
845         matched = 1;
846       }
847       else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
848         if (hdr)
849           parse_content_disposition (p, hdr->content);
850         matched = 1;
851       }
852     }
853     break;
854
855   case 'd':
856     if (!ascii_strcasecmp ("ate", line + 1)) {
857       m_strreplace(&e->date, p);
858       if (hdr)
859         hdr->date_sent = mutt_parse_date (p, hdr);
860       matched = 1;
861     }
862     break;
863
864   case 'e':
865     if (!ascii_strcasecmp ("xpires", line + 1) &&
866         hdr && mutt_parse_date (p, NULL) < time (NULL))
867       hdr->expired = 1;
868     break;
869
870   case 'f':
871     if (!ascii_strcasecmp ("rom", line + 1)) {
872       e->from = rfc822_parse_adrlist (e->from, p);
873       /* don't leave from info NULL if there's an invalid address (or
874        * whatever) in From: field; mutt would just display it as empty
875        * and mark mail/(esp.) news article as your own. aaargh! this
876        * bothered me for _years_ */
877       if (!e->from) {
878         e->from = address_new ();
879         e->from->personal = m_strdup(p);
880       }
881       matched = 1;
882     }
883 #ifdef USE_NNTP
884     else if (!m_strcasecmp(line + 1, "ollowup-to")) {
885       if (!e->followup_to) {
886         m_strrtrim(p);
887         e->followup_to = m_strdup(skipspaces(p));
888       }
889       matched = 1;
890     }
891 #endif
892     break;
893
894   case 'i':
895     if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
896       mutt_free_list (&e->in_reply_to);
897       e->in_reply_to = mutt_parse_references (p, 1);
898       matched = 1;
899     }
900     break;
901
902   case 'l':
903     if (!ascii_strcasecmp (line + 1, "ines")) {
904       if (hdr) {
905         hdr->lines = atoi (p);
906
907         /*
908          * HACK - mutt has, for a very short time, produced negative
909          * Lines header values.  Ignore them.
910          */
911         if (hdr->lines < 0)
912           hdr->lines = 0;
913       }
914
915       matched = 1;
916     }
917     else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
918       /* RFC 2369.  FIXME: We should ignore whitespace, but don't. */
919       if (strncmp (p, "NO", 2)) {
920         char *beg, *end;
921
922         for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
923           ++beg;
924           if (!(end = strchr (beg, '>')))
925             break;
926
927           /* Take the first mailto URL */
928           if (url_check_scheme (beg) == U_MAILTO) {
929             p_delete(&e->list_post);
930             e->list_post = p_dupstr(beg, end - beg);
931             break;
932           }
933         }
934       }
935       matched = 1;
936     }
937     break;
938
939   case 'm':
940     if (!ascii_strcasecmp (line + 1, "ime-version")) {
941       if (hdr)
942         hdr->mime = 1;
943       matched = 1;
944     }
945     else if (!ascii_strcasecmp (line + 1, "essage-id")) {
946       const char *beg, *end;
947
948       /* We add a new "Message-ID:" when building a message */
949       p_delete(&e->message_id);
950
951       if ((beg = strchr(p, '<')) && (end = strchr(beg, '>')))
952           e->message_id = p_dupstr(beg, (end - beg) + 1);
953       matched = 1;
954     }
955     else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
956       if (!ascii_strcasecmp (line + 5, "reply-to")) {
957         /* override the Reply-To: field */
958         address_delete (&e->reply_to);
959         e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
960         matched = 1;
961       }
962       else if (!ascii_strcasecmp (line + 5, "followup-to")) {
963         e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
964         matched = 1;
965       }
966     }
967     break;
968
969 #ifdef USE_NNTP
970   case 'n':
971     if (!m_strcasecmp(line + 1, "ewsgroups")) {
972       p_delete(&e->newsgroups);
973       m_strrtrim(p);
974       e->newsgroups = m_strdup(skipspaces(p));
975       matched = 1;
976     }
977     break;
978 #endif
979
980   case 'o':
981     /* field `Organization:' saves only for pager! */
982     if (!m_strcasecmp(line + 1, "rganization")) {
983       if (!e->organization && m_strcasecmp(p, "unknown"))
984         e->organization = m_strdup(p);
985     }
986     break;
987
988   case 'r':
989     if (!ascii_strcasecmp (line + 1, "eferences")) {
990       mutt_free_list (&e->references);
991       e->references = mutt_parse_references (p, 0);
992       matched = 1;
993     }
994     else if (!ascii_strcasecmp (line + 1, "eply-to")) {
995       e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
996       matched = 1;
997     }
998     else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
999       e->return_path = rfc822_parse_adrlist (e->return_path, p);
1000       matched = 1;
1001     }
1002     else if (!ascii_strcasecmp (line + 1, "eceived")) {
1003       if (hdr && !hdr->received) {
1004         char *d = strchr (p, ';');
1005
1006         if (d)
1007           hdr->received = mutt_parse_date (d + 1, NULL);
1008       }
1009     }
1010     break;
1011
1012   case 's':
1013     if (!ascii_strcasecmp (line + 1, "ubject")) {
1014       if (!e->subject)
1015         e->subject = m_strdup(p);
1016       matched = 1;
1017     }
1018     else if (!ascii_strcasecmp (line + 1, "ender")) {
1019       e->sender = rfc822_parse_adrlist (e->sender, p);
1020       matched = 1;
1021     }
1022     else if (!ascii_strcasecmp (line + 1, "tatus")) {
1023       if (hdr) {
1024         while (*p) {
1025           switch (*p) {
1026           case 'r':
1027             hdr->replied = 1;
1028             break;
1029           case 'O':
1030             hdr->old = 1;
1031             break;
1032           case 'R':
1033             hdr->read = 1;
1034             break;
1035           }
1036           p++;
1037         }
1038       }
1039       matched = 1;
1040     }
1041     else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1042               !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1043       e->supersedes = m_strdup(p);
1044     break;
1045
1046   case 't':
1047     if (ascii_strcasecmp (line + 1, "o") == 0) {
1048       e->to = rfc822_parse_adrlist (e->to, p);
1049       matched = 1;
1050     }
1051     break;
1052
1053   case 'x':
1054     if (ascii_strcasecmp (line + 1, "-status") == 0) {
1055       if (hdr) {
1056         while (*p) {
1057           switch (*p) {
1058           case 'A':
1059             hdr->replied = 1;
1060             break;
1061           case 'D':
1062             hdr->deleted = 1;
1063             break;
1064           case 'F':
1065             hdr->flagged = 1;
1066             break;
1067           default:
1068             break;
1069           }
1070           p++;
1071         }
1072       }
1073       matched = 1;
1074     }
1075     else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1076       e->x_label = m_strdup(p);
1077       matched = 1;
1078     }
1079 #ifdef USE_NNTP
1080     else if (!m_strcasecmp(line + 1, "-comment-to")) {
1081       if (!e->x_comment_to)
1082         e->x_comment_to = m_strdup(p);
1083       matched = 1;
1084     }
1085     else if (!m_strcasecmp(line + 1, "ref")) {
1086       if (!e->xref)
1087         e->xref = m_strdup(p);
1088       matched = 1;
1089     }
1090 #endif
1091
1092   default:
1093     break;
1094   }
1095
1096   /* Keep track of the user-defined headers */
1097   if (!matched && user_hdrs) {
1098     /* restore the original line */
1099     line[m_strlen(line)] = ':';
1100
1101     if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1102         && !mutt_matches_ignore (line, UnIgnore))
1103       goto done;
1104
1105     if (last) {
1106       last->next = mutt_new_list ();
1107       last = last->next;
1108     }
1109     else
1110       last = e->userhdrs = mutt_new_list ();
1111     last->data = m_strdup(line);
1112     if (do_2047)
1113       rfc2047_decode (&last->data);
1114   }
1115
1116 done:
1117
1118   *lastp = last;
1119   return matched;
1120 }
1121
1122
1123 /* mutt_read_rfc822_header() -- parses a RFC822 header
1124  *
1125  * Args:
1126  *
1127  * f            stream to read from
1128  *
1129  * hdr          header structure of current message (optional).
1130  *
1131  * user_hdrs    If set, store user headers.  Used for recall-message and
1132  *              postpone modes.
1133  *
1134  * weed         If this parameter is set and the user has activated the
1135  *              $weed option, honor the header weed list for user headers.
1136  *              Used for recall-message.
1137  *
1138  * Returns:     newly allocated envelope structure.  You should free it by
1139  *              envelope_delete() when envelope stay unneeded.
1140  */
1141 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1142                                    short weed)
1143 {
1144   ENVELOPE *e = envelope_new();
1145   LIST *last = NULL;
1146   char *line = p_new(char, LONG_STRING);
1147   char *p;
1148   off_t loc;
1149   int matched;
1150   ssize_t linelen = LONG_STRING;
1151   char buf[LONG_STRING + 1];
1152
1153   if (hdr) {
1154     if (hdr->content == NULL) {
1155       hdr->content = mutt_new_body ();
1156
1157       /* set the defaults from RFC1521 */
1158       hdr->content->type = TYPETEXT;
1159       hdr->content->subtype = m_strdup("plain");
1160       hdr->content->encoding = ENC7BIT;
1161       hdr->content->length = -1;
1162
1163       /* RFC 2183 says this is arbitrary */
1164       hdr->content->disposition = DISPINLINE;
1165     }
1166   }
1167
1168   while ((loc = ftello (f)),
1169          mutt_read_rfc822_line (f, &line, &linelen))
1170   {
1171     matched = 0;
1172
1173     if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1174       char return_path[LONG_STRING];
1175       time_t t;
1176
1177       /* some bogus MTAs will quote the original "From " line */
1178       if (m_strncmp(">From ", line, 6) == 0)
1179         continue;               /* just ignore */
1180       else if (is_from (line, return_path, sizeof (return_path), &t)) {
1181         /* MH somtimes has the From_ line in the middle of the header! */
1182         if (hdr && !hdr->received)
1183           hdr->received = t - mutt_local_tz (t);
1184         continue;
1185       }
1186
1187       fseeko (f, loc, 0);
1188       break;                    /* end of header */
1189     }
1190
1191     *buf = '\0';
1192
1193     if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1194       if (!rx_list_match (NoSpamList, line)) {
1195
1196         /* if spam tag already exists, figure out how to amend it */
1197         if (e->spam && *buf) {
1198           /* If SpamSep defined, append with separator */
1199           if (SpamSep) {
1200             mutt_buffer_addstr (e->spam, SpamSep);
1201             mutt_buffer_addstr (e->spam, buf);
1202           }
1203
1204           /* else overwrite */
1205           else {
1206             e->spam->dptr = e->spam->data;
1207             *e->spam->dptr = '\0';
1208             mutt_buffer_addstr (e->spam, buf);
1209           }
1210         }
1211
1212         /* spam tag is new, and match expr is non-empty; copy */
1213         else if (!e->spam && *buf) {
1214           e->spam = mutt_buffer_from (NULL, buf);
1215         }
1216
1217         /* match expr is empty; plug in null string if no existing tag */
1218         else if (!e->spam) {
1219           e->spam = mutt_buffer_from (NULL, "");
1220         }
1221
1222         if (e->spam && e->spam->data)
1223           debug_print (5, ("spam = %s\n", e->spam->data));
1224       }
1225     }
1226
1227     *p++ = 0;
1228     p = vskipspaces(p);
1229     if (!*p)
1230       continue;                 /* skip empty header fields */
1231
1232     matched =
1233       mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1234
1235   }
1236
1237   p_delete(&line);
1238
1239   if (hdr) {
1240     hdr->content->hdr_offset = hdr->offset;
1241     hdr->content->offset = ftello (f);
1242     rfc2047_decode_envelope(e);
1243     /* check for missing or invalid date */
1244     if (hdr->date_sent <= 0) {
1245       debug_print (1, ("no date found, using received "
1246                        "time from msg separator\n"));
1247       hdr->date_sent = hdr->received;
1248     }
1249   }
1250
1251   return (e);
1252 }
1253
1254 address_t *mutt_parse_adrlist (address_t * p, const char *s)
1255 {
1256   const char *q;
1257
1258   /* check for a simple whitespace separated list of addresses */
1259   if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1260     char tmp[HUGE_STRING];
1261     char *r;
1262
1263     m_strcpy(tmp, sizeof(tmp), s);
1264     r = tmp;
1265     while ((r = strtok (r, " \t")) != NULL) {
1266       p = rfc822_parse_adrlist (p, r);
1267       r = NULL;
1268     }
1269   }
1270   else
1271     p = rfc822_parse_adrlist (p, s);
1272
1273   return p;
1274 }
1275
1276
1277 /* Compares mime types to the ok and except lists */
1278 int count_body_parts_check(LIST **checklist, BODY *b, int dflt) {
1279   LIST *type;
1280   ATTACH_MATCH *a;
1281
1282   /* If list is null, use default behavior. */
1283   if (! *checklist) {
1284     /*return dflt;*/
1285     return 0;
1286   }
1287
1288   for (type = *checklist; type; type = type->next) {
1289     a = (ATTACH_MATCH *)type->data;
1290     debug_print(5, ("cbpc: %s %d/%s ?? %s/%s [%d]... ",
1291                dflt ? "[OK] " : "[EXCL] ",
1292                b->type, b->subtype, a->major, a->minor, a->major_int));
1293     if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1294         !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1295       debug_print(5, ("yes\n"));
1296       return 1;
1297     } else {
1298       debug_print(5, ("no\n"));
1299     }
1300   }
1301   return 0;
1302 }
1303
1304 #define AT_COUNT(why) { shallcount = 1; }
1305 #define AT_NOCOUNT(why) { shallcount = 0; }
1306
1307 int count_body_parts (BODY *body, int flags) {
1308   int count = 0;
1309   int shallcount, shallrecurse;
1310   BODY *bp;
1311
1312   if (body == NULL)
1313     return 0;
1314
1315   for (bp = body; bp != NULL; bp = bp->next) {
1316     /* Initial disposition is to count and not to recurse this part. */
1317     AT_COUNT("default");
1318     shallrecurse = 0;
1319
1320     debug_print(5, ("bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1321                bp->description ? bp->description : ("none"),
1322                bp->filename ? bp->filename :
1323                bp->d_filename ? bp->d_filename : "(none)",
1324                bp->type, bp->subtype ? bp->subtype : "*"));
1325
1326     if (bp->type == TYPEMESSAGE) {
1327       shallrecurse = 1;
1328
1329       /* If it's an external body pointer, don't recurse it. */
1330       if (!ascii_strcasecmp (bp->subtype, "external-body"))
1331         shallrecurse = 0;
1332
1333       /* Don't count containers if they're top-level. */
1334       if (flags & M_PARTS_TOPLEVEL)
1335         AT_NOCOUNT("top-level message/*");
1336     } else if (bp->type == TYPEMULTIPART) {
1337       /* Always recurse multiparts, except multipart/alternative. */
1338       shallrecurse = 1;
1339       if (!m_strcasecmp(bp->subtype, "alternative"))
1340         shallrecurse = 0;
1341
1342       /* Don't count containers if they're top-level. */
1343       if (flags & M_PARTS_TOPLEVEL)
1344         AT_NOCOUNT("top-level multipart");
1345     }
1346
1347     if (bp->disposition == DISPINLINE &&
1348         bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1349       AT_NOCOUNT("ignore fundamental inlines");
1350
1351     /* If this body isn't scheduled for enumeration already, don't bother
1352      * profiling it further. */
1353
1354     if (shallcount) {
1355       /* Turn off shallcount if message type is not in ok list,
1356        * or if it is in except list. Check is done separately for
1357        * inlines vs. attachments.
1358        */
1359
1360       if (bp->disposition == DISPATTACH) {
1361         if (!count_body_parts_check(&AttachAllow, bp, 1))
1362           AT_NOCOUNT("attach not allowed");
1363         if (count_body_parts_check(&AttachExclude, bp, 0))
1364           AT_NOCOUNT("attach excluded");
1365       } else {
1366         if (!count_body_parts_check(&InlineAllow, bp, 1))
1367           AT_NOCOUNT("inline not allowed");
1368         if (count_body_parts_check(&InlineExclude, bp, 0))
1369           AT_NOCOUNT("excluded");
1370       }
1371     }
1372
1373     if (shallcount)
1374       count++;
1375     bp->attach_qualifies = shallcount ? 1 : 0;
1376
1377     debug_print(5, ("cbp: %p shallcount = %d\n", bp, shallcount));
1378
1379     if (shallrecurse) {
1380       debug_print(5, ("cbp: %p pre count = %d\n", bp, count));
1381       bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1382       count += bp->attach_count;
1383       debug_print(5, ("cbp: %p post count = %d\n", bp, count));
1384     }
1385   }
1386
1387   debug_print(5, ("bp: return %d\n", count < 0 ? 0 : count));
1388   return count < 0 ? 0 : count;
1389 }
1390
1391 int mutt_count_body_parts (HEADER *hdr, int flags) {
1392   if (!option (OPTCOUNTATTACH))
1393     return (0);
1394   if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1395     return hdr->attach_total;
1396
1397   if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1398     hdr->attach_total = count_body_parts(hdr->content, flags | M_PARTS_TOPLEVEL);
1399   else
1400     hdr->attach_total = 0;
1401
1402   hdr->attach_valid = 1;
1403   return hdr->attach_total;
1404 }