Andreas Krennmair:
[apps/madmutt.git] / parse.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #include "mutt_regex.h"
25 #include "mailbox.h"
26 #include "mime.h"
27 #include "rfc2047.h"
28 #include "rfc2231.h"
29 #include "mutt_crypt.h"
30 #include "url.h"
31
32 #include <string.h>
33 #include <ctype.h>
34 #include <sys/stat.h>
35 #include <stdlib.h>
36
37 /* Reads an arbitrarily long header field, and looks ahead for continuation
38  * lines.  ``line'' must point to a dynamically allocated string; it is
39  * increased if more space is required to fit the whole line.
40  */
41 static char *read_rfc822_line (FILE * f, char *line, size_t * linelen)
42 {
43   char *buf = line;
44   char ch;
45   size_t offset = 0;
46
47   FOREVER {
48     if (fgets (buf, *linelen - offset, f) == NULL ||    /* end of file or */
49         (ISSPACE (*line) && !offset)) { /* end of headers */
50       *line = 0;
51       return (line);
52     }
53
54     buf += strlen (buf) - 1;
55     if (*buf == '\n') {
56       /* we did get a full line. remove trailing space */
57       while (ISSPACE (*buf))
58         *buf-- = 0;             /* we cannot come beyond line's beginning because
59                                  * it begins with a non-space */
60
61       /* check to see if the next line is a continuation line */
62       if ((ch = fgetc (f)) != ' ' && ch != '\t') {
63         ungetc (ch, f);
64         return (line);          /* next line is a separate header field or EOH */
65       }
66
67       /* eat tabs and spaces from the beginning of the continuation line */
68       while ((ch = fgetc (f)) == ' ' || ch == '\t');
69       ungetc (ch, f);
70       *++buf = ' ';             /* string is still terminated because we removed
71                                    at least one whitespace char above */
72     }
73
74     buf++;
75     offset = buf - line;
76     if (*linelen < offset + STRING) {
77       /* grow the buffer */
78       *linelen += STRING;
79       safe_realloc (&line, *linelen);
80       buf = line + offset;
81     }
82   }
83   /* not reached */
84 }
85
86 LIST *mutt_parse_references (char *s, int in_reply_to)
87 {
88   LIST *t, *lst = NULL;
89   int m, n = 0;
90   char *o = NULL, *new, *at;
91
92   while ((s = strtok (s, " \t;")) != NULL) {
93     /*
94      * some mail clients add other garbage besides message-ids, so do a quick
95      * check to make sure this looks like a valid message-id
96      * some idiotic clients also break their message-ids between lines, deal
97      * with that too (give up if it's more than two lines, though)
98      */
99     t = NULL;
100     new = NULL;
101
102     if (*s == '<') {
103       n = strlen (s);
104       if (s[n - 1] != '>') {
105         o = s;
106         s = NULL;
107         continue;
108       }
109
110       new = safe_strdup (s);
111     }
112     else if (o) {
113       m = strlen (s);
114       if (s[m - 1] == '>') {
115         new = safe_malloc (sizeof (char) * (n + m + 1));
116         strcpy (new, o);        /* __STRCPY_CHECKED__ */
117         strcpy (new + n, s);    /* __STRCPY_CHECKED__ */
118       }
119     }
120     if (new) {
121       /* make sure that this really does look like a message-id.
122        * it should have exactly one @, and if we're looking at
123        * an in-reply-to header, make sure that the part before
124        * the @ has more than eight characters or it's probably
125        * an email address
126        */
127       if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
128           || (in_reply_to && at - new <= 8))
129         FREE (&new);
130       else {
131         t = (LIST *) safe_malloc (sizeof (LIST));
132         t->data = new;
133         t->next = lst;
134         lst = t;
135       }
136     }
137     o = NULL;
138     s = NULL;
139   }
140
141   return (lst);
142 }
143
144 int mutt_check_encoding (const char *c)
145 {
146   if (ascii_strncasecmp ("7bit", c, sizeof ("7bit") - 1) == 0)
147     return (ENC7BIT);
148   else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit") - 1) == 0)
149     return (ENC8BIT);
150   else if (ascii_strncasecmp ("binary", c, sizeof ("binary") - 1) == 0)
151     return (ENCBINARY);
152   else
153     if (ascii_strncasecmp
154         ("quoted-printable", c, sizeof ("quoted-printable") - 1) == 0)
155     return (ENCQUOTEDPRINTABLE);
156   else if (ascii_strncasecmp ("base64", c, sizeof ("base64") - 1) == 0)
157     return (ENCBASE64);
158   else if (ascii_strncasecmp ("x-uuencode", c, sizeof ("x-uuencode") - 1) ==
159            0)
160     return (ENCUUENCODED);
161 #ifdef SUN_ATTACHMENT
162   else if (ascii_strncasecmp ("uuencode", c, sizeof ("uuencode") - 1) == 0)
163     return (ENCUUENCODED);
164 #endif
165   else
166     return (ENCOTHER);
167 }
168
169 static PARAMETER *parse_parameters (const char *s)
170 {
171   PARAMETER *head = 0, *cur = 0, *new;
172   char buffer[LONG_STRING];
173   const char *p;
174   size_t i;
175
176   dprint (2, (debugfile, "parse_parameters: `%s'\n", s));
177
178   while (*s) {
179     if ((p = strpbrk (s, "=;")) == NULL) {
180       dprint (1,
181               (debugfile, "parse_parameters: malformed parameter: %s\n", s));
182       goto bail;
183     }
184
185     /* if we hit a ; now the parameter has no value, just skip it */
186     if (*p != ';') {
187       i = p - s;
188
189       new = mutt_new_parameter ();
190
191       new->attribute = safe_malloc (i + 1);
192       memcpy (new->attribute, s, i);
193       new->attribute[i] = 0;
194
195       /* remove whitespace from the end of the attribute name */
196       while (ISSPACE (new->attribute[--i]))
197         new->attribute[i] = 0;
198
199       s = p + 1;                /* skip over the = */
200       SKIPWS (s);
201
202       if (*s == '"') {
203         int state_ascii = 1;
204
205         s++;
206         for (i = 0; *s && i < sizeof (buffer) - 1; i++, s++) {
207           if (!option (OPTSTRICTMIME)) {
208             /* As iso-2022-* has a characer of '"' with non-ascii state,
209              * ignore it. */
210             if (*s == 0x1b && i < sizeof (buffer) - 2) {
211               if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
212                 state_ascii = 1;
213               else
214                 state_ascii = 0;
215             }
216           }
217           if (state_ascii && *s == '"')
218             break;
219           if (*s == '\\') {
220             /* Quote the next character */
221             buffer[i] = s[1];
222             if (!*++s)
223               break;
224           }
225           else
226             buffer[i] = *s;
227         }
228         buffer[i] = 0;
229         if (*s)
230           s++;                  /* skip over the " */
231       }
232       else {
233         for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1;
234              i++, s++)
235           buffer[i] = *s;
236         buffer[i] = 0;
237       }
238
239       new->value = safe_strdup (buffer);
240
241       dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n",
242                   new->attribute ? new->attribute : "",
243                   new->value ? new->value : ""));
244
245       /* Add this parameter to the list */
246       if (head) {
247         cur->next = new;
248         cur = cur->next;
249       }
250       else
251         head = cur = new;
252     }
253     else {
254       dprint (1,
255               (debugfile, "parse_parameters(): parameter with no value: %s\n",
256                s));
257       s = p;
258     }
259
260     /* Find the next parameter */
261     if (*s != ';' && (s = strchr (s, ';')) == NULL)
262       break;                    /* no more parameters */
263
264     do {
265       s++;
266
267       /* Move past any leading whitespace */
268       SKIPWS (s);
269     }
270     while (*s == ';');          /* skip empty parameters */
271   }
272
273 bail:
274
275   rfc2231_decode_parameters (&head);
276   return (head);
277 }
278
279 int mutt_check_mime_type (const char *s)
280 {
281   if (ascii_strcasecmp ("text", s) == 0)
282     return TYPETEXT;
283   else if (ascii_strcasecmp ("multipart", s) == 0)
284     return TYPEMULTIPART;
285 #ifdef SUN_ATTACHMENT
286   else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
287     return TYPEMULTIPART;
288 #endif
289   else if (ascii_strcasecmp ("application", s) == 0)
290     return TYPEAPPLICATION;
291   else if (ascii_strcasecmp ("message", s) == 0)
292     return TYPEMESSAGE;
293   else if (ascii_strcasecmp ("image", s) == 0)
294     return TYPEIMAGE;
295   else if (ascii_strcasecmp ("audio", s) == 0)
296     return TYPEAUDIO;
297   else if (ascii_strcasecmp ("video", s) == 0)
298     return TYPEVIDEO;
299   else if (ascii_strcasecmp ("model", s) == 0)
300     return TYPEMODEL;
301   else
302     return TYPEOTHER;
303 }
304
305 void mutt_parse_content_type (char *s, BODY * ct)
306 {
307   char *pc;
308   char *subtype;
309
310   FREE (&ct->subtype);
311   mutt_free_parameter (&ct->parameter);
312
313   /* First extract any existing parameters */
314   if ((pc = strchr (s, ';')) != NULL) {
315     *pc++ = 0;
316     while (*pc && ISSPACE (*pc))
317       pc++;
318     ct->parameter = parse_parameters (pc);
319
320     /* Some pre-RFC1521 gateways still use the "name=filename" convention,
321      * but if a filename has already been set in the content-disposition,
322      * let that take precedence, and don't set it here */
323     if ((pc = mutt_get_parameter ("name", ct->parameter)) != 0
324         && !ct->filename)
325       ct->filename = safe_strdup (pc);
326
327 #ifdef SUN_ATTACHMENT
328     /* this is deep and utter perversion */
329     if ((pc = mutt_get_parameter ("conversions", ct->parameter)) != 0)
330       ct->encoding = mutt_check_encoding (pc);
331 #endif
332
333   }
334
335   /* Now get the subtype */
336   if ((subtype = strchr (s, '/'))) {
337     *subtype++ = '\0';
338     for (pc = subtype; *pc && !ISSPACE (*pc) && *pc != ';'; pc++);
339     *pc = '\0';
340     ct->subtype = safe_strdup (subtype);
341   }
342
343   /* Finally, get the major type */
344   ct->type = mutt_check_mime_type (s);
345
346 #ifdef SUN_ATTACHMENT
347   if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
348     ct->subtype = safe_strdup ("x-sun-attachment");
349 #endif
350
351   if (ct->type == TYPEOTHER) {
352     ct->xtype = safe_strdup (s);
353   }
354
355   if (ct->subtype == NULL) {
356     /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
357      * field, so we can attempt to convert the type to BODY here.
358      */
359     if (ct->type == TYPETEXT)
360       ct->subtype = safe_strdup ("plain");
361     else if (ct->type == TYPEAUDIO)
362       ct->subtype = safe_strdup ("basic");
363     else if (ct->type == TYPEMESSAGE)
364       ct->subtype = safe_strdup ("rfc822");
365     else if (ct->type == TYPEOTHER) {
366       char buffer[SHORT_STRING];
367
368       ct->type = TYPEAPPLICATION;
369       snprintf (buffer, sizeof (buffer), "x-%s", s);
370       ct->subtype = safe_strdup (buffer);
371     }
372     else
373       ct->subtype = safe_strdup ("x-unknown");
374   }
375
376   /* Default character set for text types. */
377   if (ct->type == TYPETEXT) {
378     if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
379       mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
380                           (const char *)
381                           mutt_get_first_charset (AssumedCharset),
382                           &ct->parameter);
383   }
384
385 }
386
387 static void parse_content_disposition (char *s, BODY * ct)
388 {
389   PARAMETER *parms;
390
391   if (!ascii_strncasecmp ("inline", s, 6))
392     ct->disposition = DISPINLINE;
393   else if (!ascii_strncasecmp ("form-data", s, 9))
394     ct->disposition = DISPFORMDATA;
395   else
396     ct->disposition = DISPATTACH;
397
398   /* Check to see if a default filename was given */
399   if ((s = strchr (s, ';')) != NULL) {
400     s++;
401     SKIPWS (s);
402     if ((s =
403          mutt_get_parameter ("filename",
404                              (parms = parse_parameters (s)))) != 0)
405       mutt_str_replace (&ct->filename, s);
406     if ((s = mutt_get_parameter ("name", parms)) != 0)
407       ct->form_name = safe_strdup (s);
408     mutt_free_parameter (&parms);
409   }
410 }
411
412 /* args:
413  *      fp      stream to read from
414  *
415  *      digest  1 if reading subparts of a multipart/digest, 0
416  *              otherwise
417  */
418
419 BODY *mutt_read_mime_header (FILE * fp, int digest)
420 {
421   BODY *p = mutt_new_body ();
422   char *c;
423   char *line = safe_malloc (LONG_STRING);
424   size_t linelen = LONG_STRING;
425
426   p->hdr_offset = ftell (fp);
427
428   p->encoding = ENC7BIT;        /* default from RFC1521 */
429   p->type = digest ? TYPEMESSAGE : TYPETEXT;
430   p->disposition = DISPINLINE;
431
432   while (*(line = read_rfc822_line (fp, line, &linelen)) != 0) {
433     /* Find the value of the current header */
434     if ((c = strchr (line, ':'))) {
435       *c = 0;
436       c++;
437       SKIPWS (c);
438       if (!*c) {
439         dprint (1,
440                 (debugfile,
441                  "mutt_read_mime_header(): skipping empty header field: %s\n",
442                  line));
443         continue;
444       }
445     }
446     else {
447       dprint (1,
448               (debugfile, "read_mime_header: bogus MIME header: %s\n", line));
449       break;
450     }
451
452     if (!ascii_strncasecmp ("content-", line, 8)) {
453       if (!ascii_strcasecmp ("type", line + 8))
454         mutt_parse_content_type (c, p);
455       else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
456         p->encoding = mutt_check_encoding (c);
457       else if (!ascii_strcasecmp ("disposition", line + 8))
458         parse_content_disposition (c, p);
459       else if (!ascii_strcasecmp ("description", line + 8)) {
460         mutt_str_replace (&p->description, c);
461         rfc2047_decode (&p->description);
462       }
463     }
464 #ifdef SUN_ATTACHMENT
465     else if (!ascii_strncasecmp ("x-sun-", line, 6)) {
466       if (!ascii_strcasecmp ("data-type", line + 6))
467         mutt_parse_content_type (c, p);
468       else if (!ascii_strcasecmp ("encoding-info", line + 6))
469         p->encoding = mutt_check_encoding (c);
470       else if (!ascii_strcasecmp ("content-lines", line + 6))
471         mutt_set_parameter ("content-lines", c, &(p->parameter));
472       else if (!ascii_strcasecmp ("data-description", line + 6)) {
473         mutt_str_replace (&p->description, c);
474         rfc2047_decode (&p->description);
475       }
476     }
477 #endif
478   }
479   p->offset = ftell (fp);       /* Mark the start of the real data */
480   if (p->type == TYPETEXT && !p->subtype)
481     p->subtype = safe_strdup ("plain");
482   else if (p->type == TYPEMESSAGE && !p->subtype)
483     p->subtype = safe_strdup ("rfc822");
484
485   FREE (&line);
486
487   return (p);
488 }
489
490 void mutt_parse_part (FILE * fp, BODY * b)
491 {
492   char *bound = 0;
493
494   switch (b->type) {
495   case TYPEMULTIPART:
496 #ifdef SUN_ATTACHMENT
497     if (!ascii_strcasecmp (b->subtype, "x-sun-attachment"))
498       bound = "--------";
499     else
500 #endif
501       bound = mutt_get_parameter ("boundary", b->parameter);
502
503     fseek (fp, b->offset, SEEK_SET);
504     b->parts = mutt_parse_multipart (fp, bound,
505                                      b->offset + b->length,
506                                      ascii_strcasecmp ("digest",
507                                                        b->subtype) == 0);
508     break;
509
510   case TYPEMESSAGE:
511     if (b->subtype) {
512       fseek (fp, b->offset, SEEK_SET);
513       if (mutt_is_message_type (b->type, b->subtype))
514         b->parts = mutt_parse_messageRFC822 (fp, b);
515       else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
516         b->parts = mutt_read_mime_header (fp, 0);
517       else
518         return;
519     }
520     break;
521
522   default:
523     return;
524   }
525
526   /* try to recover from parsing error */
527   if (!b->parts) {
528     b->type = TYPETEXT;
529     mutt_str_replace (&b->subtype, "plain");
530   }
531 }
532
533 /* parse a MESSAGE/RFC822 body
534  *
535  * args:
536  *      fp              stream to read from
537  *
538  *      parent          structure which contains info about the message/rfc822
539  *                      body part
540  *
541  * NOTE: this assumes that `parent->length' has been set!
542  */
543
544 BODY *mutt_parse_messageRFC822 (FILE * fp, BODY * parent)
545 {
546   BODY *msg;
547
548   parent->hdr = mutt_new_header ();
549   parent->hdr->offset = ftell (fp);
550   parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
551   msg = parent->hdr->content;
552
553   /* ignore the length given in the content-length since it could be wrong
554      and we already have the info to calculate the correct length */
555   /* if (msg->length == -1) */
556   msg->length = parent->length - (msg->offset - parent->offset);
557
558   /* if body of this message is empty, we can end up with a negative length */
559   if (msg->length < 0)
560     msg->length = 0;
561
562   mutt_parse_part (fp, msg);
563   return (msg);
564 }
565
566 /* parse a multipart structure
567  *
568  * args:
569  *      fp              stream to read from
570  *
571  *      boundary        body separator
572  *
573  *      end_off         length of the multipart body (used when the final
574  *                      boundary is missing to avoid reading too far)
575  *
576  *      digest          1 if reading a multipart/digest, 0 otherwise
577  */
578
579 BODY *mutt_parse_multipart (FILE * fp, const char *boundary, long end_off,
580                             int digest)
581 {
582 #ifdef SUN_ATTACHMENT
583   int lines;
584 #endif
585   int blen, len, crlf = 0;
586   char buffer[LONG_STRING];
587   BODY *head = 0, *last = 0, *new = 0;
588   int i;
589   int final = 0;                /* did we see the ending boundary? */
590
591   if (!boundary) {
592     mutt_error _("multipart message has no boundary parameter!");
593
594     return (NULL);
595   }
596
597   blen = mutt_strlen (boundary);
598   while (ftell (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL) {
599     len = mutt_strlen (buffer);
600
601     crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
602
603     if (buffer[0] == '-' && buffer[1] == '-' &&
604         mutt_strncmp (buffer + 2, boundary, blen) == 0) {
605       if (last) {
606         last->length = ftell (fp) - last->offset - len - 1 - crlf;
607         if (last->parts && last->parts->length == 0)
608           last->parts->length =
609             ftell (fp) - last->parts->offset - len - 1 - crlf;
610         /* if the body is empty, we can end up with a -1 length */
611         if (last->length < 0)
612           last->length = 0;
613       }
614
615       /* Remove any trailing whitespace, up to the length of the boundary */
616       for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
617         buffer[i] = 0;
618
619       /* Check for the end boundary */
620       if (mutt_strcmp (buffer + blen + 2, "--") == 0) {
621         final = 1;
622         break;                  /* done parsing */
623       }
624       else if (buffer[2 + blen] == 0) {
625         new = mutt_read_mime_header (fp, digest);
626
627 #ifdef SUN_ATTACHMENT
628         if (mutt_get_parameter ("content-lines", new->parameter)) {
629           for (lines =
630                atoi (mutt_get_parameter ("content-lines", new->parameter));
631                lines; lines--)
632             if (ftell (fp) >= end_off
633                 || fgets (buffer, LONG_STRING, fp) == NULL)
634               break;
635         }
636 #endif
637
638         /*
639          * Consistency checking - catch
640          * bad attachment end boundaries
641          */
642
643         if (new->offset > end_off) {
644           mutt_free_body (&new);
645           break;
646         }
647         if (head) {
648           last->next = new;
649           last = new;
650         }
651         else
652           last = head = new;
653       }
654     }
655   }
656
657   /* in case of missing end boundary, set the length to something reasonable */
658   if (last && last->length == 0 && !final)
659     last->length = end_off - last->offset;
660
661   /* parse recursive MIME parts */
662   for (last = head; last; last = last->next)
663     mutt_parse_part (fp, last);
664
665   return (head);
666 }
667
668 static const char *uncomment_timezone (char *buf, size_t buflen,
669                                        const char *tz)
670 {
671   char *p;
672   size_t len;
673
674   if (*tz != '(')
675     return tz;                  /* no need to do anything */
676   tz++;
677   SKIPWS (tz);
678   if ((p = strpbrk (tz, " )")) == NULL)
679     return tz;
680   len = p - tz;
681   if (len > buflen - 1)
682     len = buflen - 1;
683   memcpy (buf, tz, len);
684   buf[len] = 0;
685   return buf;
686 }
687
688 static struct tz_t {
689   char tzname[5];
690   unsigned char zhours;
691   unsigned char zminutes;
692   unsigned char zoccident;      /* west of UTC? */
693 } TimeZones[] = {
694   {
695   "aat", 1, 0, 1},              /* Atlantic Africa Time */
696   {
697   "adt", 4, 0, 0},              /* Arabia DST */
698   {
699   "ast", 3, 0, 0},              /* Arabia */
700     /*{ "ast",   4,  0, 1 }, *//* Atlantic */
701   {
702   "bst", 1, 0, 0},              /* British DST */
703   {
704   "cat", 1, 0, 0},              /* Central Africa */
705   {
706   "cdt", 5, 0, 1}, {
707   "cest", 2, 0, 0},             /* Central Europe DST */
708   {
709   "cet", 1, 0, 0},              /* Central Europe */
710   {
711   "cst", 6, 0, 1},
712     /*{ "cst",   8,  0, 0 }, *//* China */
713     /*{ "cst",   9, 30, 0 }, *//* Australian Central Standard Time */
714   {
715   "eat", 3, 0, 0},              /* East Africa */
716   {
717   "edt", 4, 0, 1}, {
718   "eest", 3, 0, 0},             /* Eastern Europe DST */
719   {
720   "eet", 2, 0, 0},              /* Eastern Europe */
721   {
722   "egst", 0, 0, 0},             /* Eastern Greenland DST */
723   {
724   "egt", 1, 0, 1},              /* Eastern Greenland */
725   {
726   "est", 5, 0, 1}, {
727   "gmt", 0, 0, 0}, {
728   "gst", 4, 0, 0},              /* Presian Gulf */
729   {
730   "hkt", 8, 0, 0},              /* Hong Kong */
731   {
732   "ict", 7, 0, 0},              /* Indochina */
733   {
734   "idt", 3, 0, 0},              /* Israel DST */
735   {
736   "ist", 2, 0, 0},              /* Israel */
737     /*{ "ist",   5, 30, 0 }, *//* India */
738   {
739   "jst", 9, 0, 0},              /* Japan */
740   {
741   "kst", 9, 0, 0},              /* Korea */
742   {
743   "mdt", 6, 0, 1}, {
744   "met", 1, 0, 0},              /* this is now officially CET */
745   {
746   "msd", 4, 0, 0},              /* Moscow DST */
747   {
748   "msk", 3, 0, 0},              /* Moscow */
749   {
750   "mst", 7, 0, 1}, {
751   "nzdt", 13, 0, 0},            /* New Zealand DST */
752   {
753   "nzst", 12, 0, 0},            /* New Zealand */
754   {
755   "pdt", 7, 0, 1}, {
756   "pst", 8, 0, 1}, {
757   "sat", 2, 0, 0},              /* South Africa */
758   {
759   "smt", 4, 0, 0},              /* Seychelles */
760   {
761   "sst", 11, 0, 1},             /* Samoa */
762     /*{ "sst",   8,  0, 0 }, *//* Singapore */
763   {
764   "utc", 0, 0, 0}, {
765   "wat", 0, 0, 0},              /* West Africa */
766   {
767   "west", 1, 0, 0},             /* Western Europe DST */
768   {
769   "wet", 0, 0, 0},              /* Western Europe */
770   {
771   "wgst", 2, 0, 1},             /* Western Greenland DST */
772   {
773   "wgt", 3, 0, 1},              /* Western Greenland */
774   {
775   "wst", 8, 0, 0},              /* Western Australia */
776 };
777
778 /* parses a date string in RFC822 format:
779  *
780  * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
781  *
782  * This routine assumes that `h' has been initialized to 0.  the `timezone'
783  * field is optional, defaulting to +0000 if missing.
784  */
785 time_t mutt_parse_date (const char *s, HEADER * h)
786 {
787   int count = 0;
788   char *t;
789   int hour, min, sec;
790   struct tm tm;
791   int i;
792   int tz_offset = 0;
793   int zhours = 0;
794   int zminutes = 0;
795   int zoccident = 0;
796   const char *ptz;
797   char tzstr[SHORT_STRING];
798   char scratch[SHORT_STRING];
799
800   /* Don't modify our argument. Fixed-size buffer is ok here since
801    * the date format imposes a natural limit. 
802    */
803
804   strfcpy (scratch, s, sizeof (scratch));
805
806   /* kill the day of the week, if it exists. */
807   if ((t = strchr (scratch, ',')))
808     t++;
809   else
810     t = scratch;
811   SKIPWS (t);
812
813   memset (&tm, 0, sizeof (tm));
814
815   while ((t = strtok (t, " \t")) != NULL) {
816     switch (count) {
817     case 0:                    /* day of the month */
818       if (!isdigit ((unsigned char) *t))
819         return (-1);
820       tm.tm_mday = atoi (t);
821       if (tm.tm_mday > 31)
822         return (-1);
823       break;
824
825     case 1:                    /* month of the year */
826       if ((i = mutt_check_month (t)) < 0)
827         return (-1);
828       tm.tm_mon = i;
829       break;
830
831     case 2:                    /* year */
832       tm.tm_year = atoi (t);
833       if (tm.tm_year < 50)
834         tm.tm_year += 100;
835       else if (tm.tm_year >= 1900)
836         tm.tm_year -= 1900;
837       break;
838
839     case 3:                    /* time of day */
840       if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3);
841       else if (sscanf (t, "%d:%d", &hour, &min) == 2)
842         sec = 0;
843       else {
844         dprint (1,
845                 (debugfile, "parse_date: could not process time format: %s\n",
846                  t));
847         return (-1);
848       }
849       tm.tm_hour = hour;
850       tm.tm_min = min;
851       tm.tm_sec = sec;
852       break;
853
854     case 4:                    /* timezone */
855       /* sometimes we see things like (MST) or (-0700) so attempt to
856        * compensate by uncommenting the string if non-RFC822 compliant
857        */
858       ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
859
860       if (*ptz == '+' || *ptz == '-') {
861         if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
862             && isdigit ((unsigned char) ptz[1])
863             && isdigit ((unsigned char) ptz[2])
864             && isdigit ((unsigned char) ptz[3])
865             && isdigit ((unsigned char) ptz[4])) {
866           zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
867           zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
868
869           if (ptz[0] == '-')
870             zoccident = 1;
871         }
872       }
873       else {
874         struct tz_t *tz;
875
876         tz = bsearch (ptz, TimeZones, sizeof TimeZones / sizeof (struct tz_t),
877                       sizeof (struct tz_t),
878                       (int (*)(const void *, const void *)) ascii_strcasecmp
879                       /* This is safe to do: A pointer to a struct equals
880                        * a pointer to its first element*/ );
881
882         if (tz) {
883           zhours = tz->zhours;
884           zminutes = tz->zminutes;
885           zoccident = tz->zoccident;
886         }
887
888         /* ad hoc support for the European MET (now officially CET) TZ */
889         if (ascii_strcasecmp (t, "MET") == 0) {
890           if ((t = strtok (NULL, " \t")) != NULL) {
891             if (!ascii_strcasecmp (t, "DST"))
892               zhours++;
893           }
894         }
895       }
896       tz_offset = zhours * 3600 + zminutes * 60;
897       if (!zoccident)
898         tz_offset = -tz_offset;
899       break;
900     }
901     count++;
902     t = 0;
903   }
904
905   if (count < 4) {              /* don't check for missing timezone */
906     dprint (1,
907             (debugfile,
908              "parse_date(): error parsing date format, using received time\n"));
909     return (-1);
910   }
911
912   if (h) {
913     h->zhours = zhours;
914     h->zminutes = zminutes;
915     h->zoccident = zoccident;
916   }
917
918   return (mutt_mktime (&tm, 0) + tz_offset);
919 }
920
921 /* extract the first substring that looks like a message-id */
922 static char *extract_message_id (const char *s)
923 {
924   const char *p;
925   char *r;
926   size_t l;
927
928   if ((s = strchr (s, '<')) == NULL || (p = strchr (s, '>')) == NULL)
929     return (NULL);
930   l = (size_t) (p - s) + 1;
931   r = safe_malloc (l + 1);
932   memcpy (r, s, l);
933   r[l] = 0;
934   return (r);
935 }
936
937 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
938 {
939   MESSAGE *msg;
940
941   if (cur->content->type != TYPEMESSAGE
942       && cur->content->type != TYPEMULTIPART)
943     return;                     /* nothing to do */
944
945   if (cur->content->parts)
946     return;                     /* The message was parsed earlier. */
947
948   if ((msg = mx_open_message (ctx, cur->msgno))) {
949     mutt_parse_part (msg->fp, cur->content);
950
951     if (WithCrypto)
952       cur->security = crypt_query (cur->content);
953
954     mx_close_message (&msg);
955   }
956 }
957
958 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
959                             short user_hdrs, short weed, short do_2047,
960                             LIST ** lastp)
961 {
962   int matched = 0;
963   LIST *last = NULL;
964
965   if (lastp)
966     last = *lastp;
967
968   switch (ascii_tolower (line[0])) {
969   case 'a':
970     if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
971       e->to = rfc822_parse_adrlist (e->to, p);
972       matched = 1;
973     }
974     else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
975       e->from = rfc822_parse_adrlist (e->from, p);
976       matched = 1;
977     }
978     break;
979
980   case 'b':
981     if (ascii_strcasecmp (line + 1, "cc") == 0) {
982       e->bcc = rfc822_parse_adrlist (e->bcc, p);
983       matched = 1;
984     }
985     break;
986
987   case 'c':
988     if (ascii_strcasecmp (line + 1, "c") == 0) {
989       e->cc = rfc822_parse_adrlist (e->cc, p);
990       matched = 1;
991     }
992     else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
993       if (ascii_strcasecmp (line + 8, "type") == 0) {
994         if (hdr)
995           mutt_parse_content_type (p, hdr->content);
996         matched = 1;
997       }
998       else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
999         if (hdr)
1000           hdr->content->encoding = mutt_check_encoding (p);
1001         matched = 1;
1002       }
1003       else if (ascii_strcasecmp (line + 8, "length") == 0) {
1004         if (hdr) {
1005           if ((hdr->content->length = atoi (p)) < 0)
1006             hdr->content->length = -1;
1007         }
1008         matched = 1;
1009       }
1010       else if (ascii_strcasecmp (line + 8, "description") == 0) {
1011         if (hdr) {
1012           mutt_str_replace (&hdr->content->description, p);
1013           rfc2047_decode (&hdr->content->description);
1014         }
1015         matched = 1;
1016       }
1017       else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
1018         if (hdr)
1019           parse_content_disposition (p, hdr->content);
1020         matched = 1;
1021       }
1022     }
1023     break;
1024
1025   case 'd':
1026     if (!ascii_strcasecmp ("ate", line + 1)) {
1027       mutt_str_replace (&e->date, p);
1028       if (hdr)
1029         hdr->date_sent = mutt_parse_date (p, hdr);
1030       matched = 1;
1031     }
1032     break;
1033
1034   case 'e':
1035     if (!ascii_strcasecmp ("xpires", line + 1) &&
1036         hdr && mutt_parse_date (p, NULL) < time (NULL))
1037       hdr->expired = 1;
1038     break;
1039
1040   case 'f':
1041     if (!ascii_strcasecmp ("rom", line + 1)) {
1042       e->from = rfc822_parse_adrlist (e->from, p);
1043       /* don't leave from info NULL if there's an invalid address (or
1044        * whatever) in From: field; mutt would just display it as empty
1045        * and mark mail/(esp.) news article as your own. aaargh! this
1046        * bothered me for _years_ */
1047       if (!e->from) {
1048         e->from = rfc822_new_address ();
1049         e->from->personal = safe_strdup (line + 6);
1050       }
1051       matched = 1;
1052     }
1053 #ifdef USE_NNTP
1054     else if (!mutt_strcasecmp (line + 1, "ollowup-to")) {
1055       if (!e->followup_to) {
1056         mutt_remove_trailing_ws (p);
1057         e->followup_to = safe_strdup (mutt_skip_whitespace (p));
1058       }
1059       matched = 1;
1060     }
1061 #endif
1062     break;
1063
1064   case 'i':
1065     if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
1066       mutt_free_list (&e->in_reply_to);
1067       e->in_reply_to = mutt_parse_references (p, 1);
1068       matched = 1;
1069     }
1070     break;
1071
1072   case 'l':
1073     if (!ascii_strcasecmp (line + 1, "ines")) {
1074       if (hdr) {
1075         hdr->lines = atoi (p);
1076
1077         /* 
1078          * HACK - mutt has, for a very short time, produced negative
1079          * Lines header values.  Ignore them. 
1080          */
1081         if (hdr->lines < 0)
1082           hdr->lines = 0;
1083       }
1084
1085       matched = 1;
1086     }
1087     else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
1088       /* RFC 2369.  FIXME: We should ignore whitespace, but don't. */
1089       if (strncmp (p, "NO", 2)) {
1090         char *beg, *end;
1091
1092         for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
1093           ++beg;
1094           if (!(end = strchr (beg, '>')))
1095             break;
1096
1097           /* Take the first mailto URL */
1098           if (url_check_scheme (beg) == U_MAILTO) {
1099             FREE (&e->list_post);
1100             e->list_post = mutt_substrdup (beg, end);
1101             break;
1102           }
1103         }
1104       }
1105       matched = 1;
1106     }
1107     break;
1108
1109   case 'm':
1110     if (!ascii_strcasecmp (line + 1, "ime-version")) {
1111       if (hdr)
1112         hdr->mime = 1;
1113       matched = 1;
1114     }
1115     else if (!ascii_strcasecmp (line + 1, "essage-id")) {
1116       /* We add a new "Message-Id:" when building a message */
1117       FREE (&e->message_id);
1118       e->message_id = extract_message_id (p);
1119       matched = 1;
1120     }
1121     else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
1122       if (!ascii_strcasecmp (line + 5, "reply-to")) {
1123         /* override the Reply-To: field */
1124         rfc822_free_address (&e->reply_to);
1125         e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1126         matched = 1;
1127       }
1128       else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1129         e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1130         matched = 1;
1131       }
1132     }
1133     break;
1134
1135 #ifdef USE_NNTP
1136   case 'n':
1137     if (!mutt_strcasecmp (line + 1, "ewsgroups")) {
1138       FREE (&e->newsgroups);
1139       mutt_remove_trailing_ws (p);
1140       e->newsgroups = safe_strdup (mutt_skip_whitespace (p));
1141       matched = 1;
1142     }
1143     break;
1144 #endif
1145
1146   case 'o':
1147     /* field `Organization:' saves only for pager! */
1148     if (!mutt_strcasecmp (line + 1, "rganization")) {
1149       if (!e->organization && mutt_strcasecmp (p, "unknown"))
1150         e->organization = safe_strdup (p);
1151     }
1152     break;
1153
1154   case 'r':
1155     if (!ascii_strcasecmp (line + 1, "eferences")) {
1156       mutt_free_list (&e->references);
1157       e->references = mutt_parse_references (p, 0);
1158       matched = 1;
1159     }
1160     else if (!ascii_strcasecmp (line + 1, "eply-to")) {
1161       e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1162       matched = 1;
1163     }
1164     else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
1165       e->return_path = rfc822_parse_adrlist (e->return_path, p);
1166       matched = 1;
1167     }
1168     else if (!ascii_strcasecmp (line + 1, "eceived")) {
1169       if (hdr && !hdr->received) {
1170         char *d = strchr (p, ';');
1171
1172         if (d)
1173           hdr->received = mutt_parse_date (d + 1, NULL);
1174       }
1175     }
1176     break;
1177
1178   case 's':
1179     if (!ascii_strcasecmp (line + 1, "ubject")) {
1180       if (!e->subject)
1181         e->subject = safe_strdup (p);
1182       matched = 1;
1183     }
1184     else if (!ascii_strcasecmp (line + 1, "ender")) {
1185       e->sender = rfc822_parse_adrlist (e->sender, p);
1186       matched = 1;
1187     }
1188     else if (!ascii_strcasecmp (line + 1, "tatus")) {
1189       if (hdr) {
1190         while (*p) {
1191           switch (*p) {
1192           case 'r':
1193             hdr->replied = 1;
1194             break;
1195           case 'O':
1196             hdr->old = 1;
1197             break;
1198           case 'R':
1199             hdr->read = 1;
1200             break;
1201           }
1202           p++;
1203         }
1204       }
1205       matched = 1;
1206     }
1207     else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1208               !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1209       e->supersedes = safe_strdup (p);
1210     break;
1211
1212   case 't':
1213     if (ascii_strcasecmp (line + 1, "o") == 0) {
1214       e->to = rfc822_parse_adrlist (e->to, p);
1215       matched = 1;
1216     }
1217     break;
1218
1219   case 'x':
1220     if (ascii_strcasecmp (line + 1, "-status") == 0) {
1221       if (hdr) {
1222         while (*p) {
1223           switch (*p) {
1224           case 'A':
1225             hdr->replied = 1;
1226             break;
1227           case 'D':
1228             hdr->deleted = 1;
1229             break;
1230           case 'F':
1231             hdr->flagged = 1;
1232             break;
1233           default:
1234             break;
1235           }
1236           p++;
1237         }
1238       }
1239       matched = 1;
1240     }
1241     else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1242       e->x_label = safe_strdup (p);
1243       matched = 1;
1244     }
1245 #ifdef USE_NNTP
1246     else if (!mutt_strcasecmp (line + 1, "-comment-to")) {
1247       if (!e->x_comment_to)
1248         e->x_comment_to = safe_strdup (p);
1249       matched = 1;
1250     }
1251     else if (!mutt_strcasecmp (line + 1, "ref")) {
1252       if (!e->xref)
1253         e->xref = safe_strdup (p);
1254       matched = 1;
1255     }
1256 #endif
1257
1258   default:
1259     break;
1260   }
1261
1262   /* Keep track of the user-defined headers */
1263   if (!matched && user_hdrs) {
1264     /* restore the original line */
1265     line[strlen (line)] = ':';
1266
1267     if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1268         && !mutt_matches_ignore (line, UnIgnore))
1269       goto done;
1270
1271     if (last) {
1272       last->next = mutt_new_list ();
1273       last = last->next;
1274     }
1275     else
1276       last = e->userhdrs = mutt_new_list ();
1277     last->data = safe_strdup (line);
1278     if (do_2047)
1279       rfc2047_decode (&last->data);
1280   }
1281
1282 done:
1283
1284   *lastp = last;
1285   return matched;
1286 }
1287
1288
1289 /* mutt_read_rfc822_header() -- parses a RFC822 header
1290  *
1291  * Args:
1292  *
1293  * f            stream to read from
1294  *
1295  * hdr          header structure of current message (optional).
1296  * 
1297  * user_hdrs    If set, store user headers.  Used for recall-message and
1298  *              postpone modes.
1299  * 
1300  * weed         If this parameter is set and the user has activated the
1301  *              $weed option, honor the header weed list for user headers.
1302  *              Used for recall-message.
1303  * 
1304  * Returns:     newly allocated envelope structure.  You should free it by
1305  *              mutt_free_envelope() when envelope stay unneeded.
1306  */
1307 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1308                                    short weed)
1309 {
1310   ENVELOPE *e = mutt_new_envelope ();
1311   LIST *last = NULL;
1312   char *line = safe_malloc (LONG_STRING);
1313   char *p;
1314   long loc;
1315   int matched;
1316   size_t linelen = LONG_STRING;
1317   char buf[LONG_STRING + 1];
1318
1319   if (hdr) {
1320     if (hdr->content == NULL) {
1321       hdr->content = mutt_new_body ();
1322
1323       /* set the defaults from RFC1521 */
1324       hdr->content->type = TYPETEXT;
1325       hdr->content->subtype = safe_strdup ("plain");
1326       hdr->content->encoding = ENC7BIT;
1327       hdr->content->length = -1;
1328
1329       /* RFC 2183 says this is arbitrary */
1330       hdr->content->disposition = DISPINLINE;
1331     }
1332   }
1333
1334   while ((loc = ftell (f)),
1335          *(line = read_rfc822_line (f, line, &linelen)) != 0) {
1336     matched = 0;
1337
1338     if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1339       char return_path[LONG_STRING];
1340       time_t t;
1341
1342       /* some bogus MTAs will quote the original "From " line */
1343       if (mutt_strncmp (">From ", line, 6) == 0)
1344         continue;               /* just ignore */
1345       else if (is_from (line, return_path, sizeof (return_path), &t)) {
1346         /* MH somtimes has the From_ line in the middle of the header! */
1347         if (hdr && !hdr->received)
1348           hdr->received = t - mutt_local_tz (t);
1349         continue;
1350       }
1351
1352       fseek (f, loc, 0);
1353       break;                    /* end of header */
1354     }
1355
1356     *buf = '\0';
1357
1358     if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1359       if (!mutt_match_rx_list (line, NoSpamList)) {
1360
1361         /* if spam tag already exists, figure out how to amend it */
1362         if (e->spam && *buf) {
1363           /* If SpamSep defined, append with separator */
1364           if (SpamSep) {
1365             mutt_buffer_addstr (e->spam, SpamSep);
1366             mutt_buffer_addstr (e->spam, buf);
1367           }
1368
1369           /* else overwrite */
1370           else {
1371             e->spam->dptr = e->spam->data;
1372             *e->spam->dptr = '\0';
1373             mutt_buffer_addstr (e->spam, buf);
1374           }
1375         }
1376
1377         /* spam tag is new, and match expr is non-empty; copy */
1378         else if (!e->spam && *buf) {
1379           e->spam = mutt_buffer_from (NULL, buf);
1380         }
1381
1382         /* match expr is empty; plug in null string if no existing tag */
1383         else if (!e->spam) {
1384           e->spam = mutt_buffer_from (NULL, "");
1385         }
1386
1387         if (e->spam && e->spam->data)
1388           dprint (5, (debugfile, "p822: spam = %s\n", e->spam->data));
1389       }
1390     }
1391
1392     *p = 0;
1393     p++;
1394     SKIPWS (p);
1395     if (!*p)
1396       continue;                 /* skip empty header fields */
1397
1398     matched =
1399       mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1400
1401   }
1402
1403   FREE (&line);
1404
1405   if (hdr) {
1406     hdr->content->hdr_offset = hdr->offset;
1407     hdr->content->offset = ftell (f);
1408
1409     /* do RFC2047 decoding */
1410     rfc2047_decode_adrlist (e->from);
1411     rfc2047_decode_adrlist (e->to);
1412     rfc2047_decode_adrlist (e->cc);
1413     rfc2047_decode_adrlist (e->bcc);
1414     rfc2047_decode_adrlist (e->reply_to);
1415     rfc2047_decode_adrlist (e->mail_followup_to);
1416     rfc2047_decode_adrlist (e->return_path);
1417     rfc2047_decode_adrlist (e->sender);
1418
1419     if (e->subject) {
1420       regmatch_t pmatch[1];
1421
1422       rfc2047_decode (&e->subject);
1423
1424       if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1425         e->real_subj = e->subject + pmatch[0].rm_eo;
1426       else
1427         e->real_subj = e->subject;
1428     }
1429
1430     /* check for missing or invalid date */
1431     if (hdr->date_sent <= 0) {
1432       dprint (1,
1433               (debugfile,
1434                "read_rfc822_header(): no date found, using received time from msg separator\n"));
1435       hdr->date_sent = hdr->received;
1436     }
1437   }
1438
1439   return (e);
1440 }
1441
1442 ADDRESS *mutt_parse_adrlist (ADDRESS * p, const char *s)
1443 {
1444   const char *q;
1445
1446   /* check for a simple whitespace separated list of addresses */
1447   if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1448     char tmp[HUGE_STRING];
1449     char *r;
1450
1451     strfcpy (tmp, s, sizeof (tmp));
1452     r = tmp;
1453     while ((r = strtok (r, " \t")) != NULL) {
1454       p = rfc822_parse_adrlist (p, r);
1455       r = NULL;
1456     }
1457   }
1458   else
1459     p = rfc822_parse_adrlist (p, s);
1460
1461   return p;
1462 }