]> granicus.if.org Git - mutt/blob - parse.c
Convert smime_invoke_import() and helpers to use buffer pool.
[mutt] / parse.c
1 /*
2  * Copyright (C) 1996-2000,2012-2013 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #ifdef USE_AUTOCRYPT
33 #include "autocrypt/autocrypt.h"
34 #endif
35
36 #include <string.h>
37 #include <ctype.h>
38 #include <sys/stat.h>
39 #include <stdlib.h>
40
41 /* Reads an arbitrarily long header field, and looks ahead for continuation
42  * lines.  ``line'' must point to a dynamically allocated string; it is
43  * increased if more space is required to fit the whole line.
44  */
45 char *mutt_read_rfc822_line (FILE *f, char *line, size_t *linelen)
46 {
47   char *buf = line;
48   int ch;
49   size_t offset = 0;
50   size_t len = 0;
51
52   FOREVER
53   {
54     if (fgets (buf, *linelen - offset, f) == NULL ||    /* end of file or */
55         (ISSPACE (*line) && !offset))                   /* end of headers */
56     {
57       *line = 0;
58       return (line);
59     }
60
61     len = mutt_strlen (buf);
62     if (! len)
63       return (line);
64
65     buf += len - 1;
66     if (*buf == '\n')
67     {
68       /* we did get a full line. remove trailing space */
69       while (ISSPACE (*buf))
70         *buf-- = 0;     /* we cannot come beyond line's beginning because
71                          * it begins with a non-space */
72
73       /* check to see if the next line is a continuation line */
74       if ((ch = fgetc (f)) != ' ' && ch != '\t')
75       {
76         ungetc (ch, f);
77         return (line); /* next line is a separate header field or EOH */
78       }
79
80       /* eat tabs and spaces from the beginning of the continuation line */
81       while ((ch = fgetc (f)) == ' ' || ch == '\t')
82         ;
83       ungetc (ch, f);
84       *++buf = ' '; /* string is still terminated because we removed
85                        at least one whitespace char above */
86     }
87
88     buf++;
89     offset = buf - line;
90     if (*linelen < offset + STRING)
91     {
92       /* grow the buffer */
93       *linelen += STRING;
94       safe_realloc (&line, *linelen);
95       buf = line + offset;
96     }
97   }
98   /* not reached */
99 }
100
101 static LIST *mutt_parse_references (char *s, int in_reply_to)
102 {
103   LIST *t, *lst = NULL;
104   char *m;
105   const char *sp;
106
107   m = mutt_extract_message_id (s, &sp);
108   while (m)
109   {
110     t = safe_malloc (sizeof (LIST));
111     t->data = m;
112     t->next = lst;
113     lst = t;
114
115     m = mutt_extract_message_id (NULL, &sp);
116   }
117
118   return lst;
119 }
120
121 int mutt_check_encoding (const char *c)
122 {
123   if (ascii_strncasecmp ("7bit", c, sizeof ("7bit")-1) == 0)
124     return (ENC7BIT);
125   else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit")-1) == 0)
126     return (ENC8BIT);
127   else if (ascii_strncasecmp ("binary", c, sizeof ("binary")-1) == 0)
128     return (ENCBINARY);
129   else if (ascii_strncasecmp ("quoted-printable", c, sizeof ("quoted-printable")-1) == 0)
130     return (ENCQUOTEDPRINTABLE);
131   else if (ascii_strncasecmp ("base64", c, sizeof("base64")-1) == 0)
132     return (ENCBASE64);
133   else if (ascii_strncasecmp ("x-uuencode", c, sizeof("x-uuencode")-1) == 0)
134     return (ENCUUENCODED);
135 #ifdef SUN_ATTACHMENT
136   else if (ascii_strncasecmp ("uuencode", c, sizeof("uuencode")-1) == 0)
137     return (ENCUUENCODED);
138 #endif
139   else
140     return (ENCOTHER);
141 }
142
143 /* Performs rfc2231 parameter parsing on s.
144  *
145  * Autocrypt defines an irregular parameter format that doesn't follow the
146  * rfc.  It splits keydata across multiple lines without parameter continuations.
147  * The allow_value_spaces parameter allows parsing those values which
148  * are split by spaces when unfolded.
149  */
150 static PARAMETER *parse_parameters (const char *s, int allow_value_spaces)
151 {
152   PARAMETER *head = 0, *cur = 0, *new;
153   BUFFER *buffer = NULL;
154   const char *p;
155   size_t i;
156
157   buffer = mutt_buffer_pool_get ();
158   /* allow_value_spaces, especially with autocrypt keydata, can result
159    * in quite large parameter values.  avoid frequent reallocs by
160    * pre-sizing */
161   if (allow_value_spaces)
162     mutt_buffer_increase_size (buffer, mutt_strlen (s));
163
164   dprint (2, (debugfile, "parse_parameters: `%s'\n", s));
165
166   while (*s)
167   {
168     mutt_buffer_clear (buffer);
169
170     if ((p = strpbrk (s, "=;")) == NULL)
171     {
172       dprint(1, (debugfile, "parse_parameters: malformed parameter: %s\n", s));
173       goto bail;
174     }
175
176     /* if we hit a ; now the parameter has no value, just skip it */
177     if (*p != ';')
178     {
179       i = p - s;
180       /* remove whitespace from the end of the attribute name */
181       while (i > 0 && is_email_wsp(s[i-1]))
182         --i;
183
184       /* the check for the missing parameter token is here so that we can skip
185        * over any quoted value that may be present.
186        */
187       if (i == 0)
188       {
189         dprint(1, (debugfile, "parse_parameters: missing attribute: %s\n", s));
190         new = NULL;
191       }
192       else
193       {
194         new = mutt_new_parameter ();
195         new->attribute = mutt_substrdup(s, s + i);
196       }
197
198       do
199       {
200         s = skip_email_wsp(p + 1); /* skip over the =, or space if we loop */
201
202         if (*s == '"')
203         {
204           int state_ascii = 1;
205           s++;
206           for (; *s; s++)
207           {
208             if (AssumedCharset)
209             {
210               /* As iso-2022-* has a character of '"' with non-ascii state,
211                * ignore it. */
212               if (*s == 0x1b)
213               {
214                 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
215                   state_ascii = 1;
216                 else
217                   state_ascii = 0;
218               }
219             }
220             if (state_ascii && *s == '"')
221               break;
222             if (*s == '\\')
223             {
224               if (s[1])
225               {
226                 s++;
227                 /* Quote the next character */
228                 mutt_buffer_addch (buffer, *s);
229               }
230             }
231             else
232               mutt_buffer_addch (buffer, *s);
233           }
234           if (*s)
235             s++; /* skip over the " */
236         }
237         else
238         {
239           for (; *s && *s != ' ' && *s != ';'; s++)
240             mutt_buffer_addch (buffer, *s);
241         }
242
243         p = s;
244       } while (allow_value_spaces && (*s == ' '));
245
246       /* if the attribute token was missing, 'new' will be NULL */
247       if (new)
248       {
249         new->value = safe_strdup (mutt_b2s (buffer));
250
251         dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n",
252                     new->attribute ? new->attribute : "",
253                     new->value ? new->value : ""));
254
255         /* Add this parameter to the list */
256         if (head)
257         {
258           cur->next = new;
259           cur = cur->next;
260         }
261         else
262           head = cur = new;
263       }
264     }
265     else
266     {
267       dprint (1, (debugfile, "parse_parameters(): parameter with no value: %s\n", s));
268       s = p;
269     }
270
271     /* Find the next parameter */
272     if (*s != ';' && (s = strchr (s, ';')) == NULL)
273       break; /* no more parameters */
274
275     do
276     {
277       /* Move past any leading whitespace. the +1 skips over the semicolon */
278       s = skip_email_wsp(s + 1);
279     }
280     while (*s == ';'); /* skip empty parameters */
281   }
282
283 bail:
284
285   rfc2231_decode_parameters (&head);
286   mutt_buffer_pool_release (&buffer);
287   return (head);
288 }
289
290 int mutt_check_mime_type (const char *s)
291 {
292   if (ascii_strcasecmp ("text", s) == 0)
293     return TYPETEXT;
294   else if (ascii_strcasecmp ("multipart", s) == 0)
295     return TYPEMULTIPART;
296 #ifdef SUN_ATTACHMENT
297   else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
298     return TYPEMULTIPART;
299 #endif
300   else if (ascii_strcasecmp ("application", s) == 0)
301     return TYPEAPPLICATION;
302   else if (ascii_strcasecmp ("message", s) == 0)
303     return TYPEMESSAGE;
304   else if (ascii_strcasecmp ("image", s) == 0)
305     return TYPEIMAGE;
306   else if (ascii_strcasecmp ("audio", s) == 0)
307     return TYPEAUDIO;
308   else if (ascii_strcasecmp ("video", s) == 0)
309     return TYPEVIDEO;
310   else if (ascii_strcasecmp ("model", s) == 0)
311     return TYPEMODEL;
312   else if (ascii_strcasecmp ("*", s) == 0)
313     return TYPEANY;
314   else if (ascii_strcasecmp (".*", s) == 0)
315     return TYPEANY;
316   else
317     return TYPEOTHER;
318 }
319
320 void mutt_parse_content_type (char *s, BODY *ct)
321 {
322   char *pc;
323   char *subtype;
324
325   FREE (&ct->subtype);
326   mutt_free_parameter(&ct->parameter);
327
328   /* First extract any existing parameters */
329   if ((pc = strchr(s, ';')) != NULL)
330   {
331     *pc++ = 0;
332     while (*pc && ISSPACE (*pc))
333       pc++;
334     ct->parameter = parse_parameters(pc, 0);
335
336     /* Some pre-RFC1521 gateways still use the "name=filename" convention,
337      * but if a filename has already been set in the content-disposition,
338      * let that take precedence, and don't set it here */
339     if ((pc = mutt_get_parameter( "name", ct->parameter)) && !ct->filename)
340       ct->filename = safe_strdup(pc);
341
342 #ifdef SUN_ATTACHMENT
343     /* this is deep and utter perversion */
344     if ((pc = mutt_get_parameter ("conversions", ct->parameter)))
345       ct->encoding = mutt_check_encoding (pc);
346 #endif
347
348   }
349
350   /* Now get the subtype */
351   if ((subtype = strchr(s, '/')))
352   {
353     *subtype++ = '\0';
354     for (pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++)
355       ;
356     *pc = '\0';
357     ct->subtype = safe_strdup (subtype);
358   }
359
360   /* Finally, get the major type */
361   ct->type = mutt_check_mime_type (s);
362
363 #ifdef SUN_ATTACHMENT
364   if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
365     ct->subtype = safe_strdup ("x-sun-attachment");
366 #endif
367
368   if (ct->type == TYPEOTHER)
369   {
370     ct->xtype = safe_strdup (s);
371   }
372
373   if (ct->subtype == NULL)
374   {
375     /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
376      * field, so we can attempt to convert the type to BODY here.
377      */
378     if (ct->type == TYPETEXT)
379       ct->subtype = safe_strdup ("plain");
380     else if (ct->type == TYPEAUDIO)
381       ct->subtype = safe_strdup ("basic");
382     else if (ct->type == TYPEMESSAGE)
383       ct->subtype = safe_strdup ("rfc822");
384     else if (ct->type == TYPEOTHER)
385     {
386       char buffer[SHORT_STRING];
387
388       ct->type = TYPEAPPLICATION;
389       snprintf (buffer, sizeof (buffer), "x-%s", s);
390       ct->subtype = safe_strdup (buffer);
391     }
392     else
393       ct->subtype = safe_strdup ("x-unknown");
394   }
395
396   /* Default character set for text types. */
397   if (ct->type == TYPETEXT)
398   {
399     if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
400       mutt_set_parameter ("charset", AssumedCharset ?
401                           (const char *) mutt_get_default_charset ()
402                           : "us-ascii", &ct->parameter);
403   }
404
405 }
406
407 static void parse_content_disposition (const char *s, BODY *ct)
408 {
409   PARAMETER *parms;
410
411   if (!ascii_strncasecmp ("inline", s, 6))
412     ct->disposition = DISPINLINE;
413   else if (!ascii_strncasecmp ("form-data", s, 9))
414     ct->disposition = DISPFORMDATA;
415   else
416     ct->disposition = DISPATTACH;
417
418   /* Check to see if a default filename was given */
419   if ((s = strchr (s, ';')) != NULL)
420   {
421     s = skip_email_wsp(s + 1);
422     if ((s = mutt_get_parameter ("filename", (parms = parse_parameters (s, 0)))))
423       mutt_str_replace (&ct->filename, s);
424     if ((s = mutt_get_parameter ("name", parms)))
425       ct->form_name = safe_strdup (s);
426     mutt_free_parameter (&parms);
427   }
428 }
429
430 #ifdef USE_AUTOCRYPT
431 static AUTOCRYPTHDR *parse_autocrypt (AUTOCRYPTHDR *head, const char *s)
432 {
433   AUTOCRYPTHDR *autocrypt;
434   PARAMETER *params = NULL, *param;
435
436   autocrypt = mutt_new_autocrypthdr ();
437   autocrypt->next = head;
438
439   param = params = parse_parameters (s, 1);
440   if (!params)
441   {
442     autocrypt->invalid = 1;
443     goto cleanup;
444   }
445
446   while (param)
447   {
448     if (!ascii_strcasecmp (param->attribute, "addr"))
449     {
450       if (autocrypt->addr)
451       {
452         autocrypt->invalid = 1;
453         goto cleanup;
454       }
455       autocrypt->addr = param->value;
456       param->value = NULL;
457     }
458     else if (!ascii_strcasecmp (param->attribute, "prefer-encrypt"))
459     {
460       if (!ascii_strcasecmp (param->value, "mutual"))
461         autocrypt->prefer_encrypt = 1;
462     }
463     else if (!ascii_strcasecmp (param->attribute, "keydata"))
464     {
465       if (autocrypt->keydata)
466       {
467         autocrypt->invalid = 1;
468         goto cleanup;
469       }
470       autocrypt->keydata = param->value;
471       param->value = NULL;
472     }
473     else if (param->attribute && (param->attribute[0] != '_'))
474     {
475       autocrypt->invalid = 1;
476       goto cleanup;
477     }
478
479     param = param->next;
480   }
481
482   /* Checking the addr against From, and for multiple valid headers
483    * occurs later, after all the headers are parsed. */
484   if (!autocrypt->addr || !autocrypt->keydata)
485     autocrypt->invalid = 1;
486
487 cleanup:
488   mutt_free_parameter (&params);
489   return autocrypt;
490 }
491 #endif
492
493 /* args:
494  *      fp      stream to read from
495  *
496  *      digest  1 if reading subparts of a multipart/digest, 0
497  *              otherwise
498  */
499
500 BODY *mutt_read_mime_header (FILE *fp, int digest)
501 {
502   BODY *p = mutt_new_body();
503   ENVELOPE *e = mutt_new_envelope ();
504   char *c;
505   char *line = safe_malloc (LONG_STRING);
506   size_t linelen = LONG_STRING;
507
508   p->hdr_offset  = ftello (fp);
509
510   p->encoding    = ENC7BIT; /* default from RFC1521 */
511   p->type        = digest ? TYPEMESSAGE : TYPETEXT;
512   p->disposition = DISPINLINE;
513
514   while (*(line = mutt_read_rfc822_line (fp, line, &linelen)) != 0)
515   {
516     /* Find the value of the current header */
517     if ((c = strchr (line, ':')))
518     {
519       *c = 0;
520       c = skip_email_wsp(c + 1);
521       if (!*c)
522       {
523         dprint (1, (debugfile, "mutt_read_mime_header(): skipping empty header field: %s\n", line));
524         continue;
525       }
526     }
527     else
528     {
529       dprint (1, (debugfile, "read_mime_header: bogus MIME header: %s\n", line));
530       break;
531     }
532
533     if (!ascii_strncasecmp ("content-", line, 8))
534     {
535       if (!ascii_strcasecmp ("type", line + 8))
536         mutt_parse_content_type (c, p);
537       else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
538         p->encoding = mutt_check_encoding (c);
539       else if (!ascii_strcasecmp ("disposition", line + 8))
540         parse_content_disposition (c, p);
541       else if (!ascii_strcasecmp ("description", line + 8))
542       {
543         mutt_str_replace (&p->description, c);
544         rfc2047_decode (&p->description);
545       }
546     }
547 #ifdef SUN_ATTACHMENT
548     else if (!ascii_strncasecmp ("x-sun-", line, 6))
549     {
550       if (!ascii_strcasecmp ("data-type", line + 6))
551         mutt_parse_content_type (c, p);
552       else if (!ascii_strcasecmp ("encoding-info", line + 6))
553         p->encoding = mutt_check_encoding (c);
554       else if (!ascii_strcasecmp ("content-lines", line + 6))
555         mutt_set_parameter ("content-lines", c, &(p->parameter));
556       else if (!ascii_strcasecmp ("data-description", line + 6))
557       {
558         mutt_str_replace (&p->description, c);
559         rfc2047_decode (&p->description);
560       }
561     }
562 #endif
563     else
564     {
565       if (mutt_parse_rfc822_line (e, NULL, line, c, 0, 0, 0, NULL))
566         p->mime_headers = e;
567     }
568   }
569   p->offset = ftello (fp); /* Mark the start of the real data */
570   if (p->type == TYPETEXT && !p->subtype)
571     p->subtype = safe_strdup ("plain");
572   else if (p->type == TYPEMESSAGE && !p->subtype)
573     p->subtype = safe_strdup ("rfc822");
574
575   FREE (&line);
576
577   if (p->mime_headers)
578     rfc2047_decode_envelope (p->mime_headers);
579   else
580     mutt_free_envelope (&e);
581
582   return (p);
583 }
584
585 void mutt_parse_part (FILE *fp, BODY *b)
586 {
587   char *bound = 0;
588
589   switch (b->type)
590   {
591     case TYPEMULTIPART:
592 #ifdef SUN_ATTACHMENT
593       if ( !ascii_strcasecmp (b->subtype, "x-sun-attachment") )
594         bound = "--------";
595       else
596 #endif
597         bound = mutt_get_parameter ("boundary", b->parameter);
598
599       fseeko (fp, b->offset, SEEK_SET);
600       b->parts =  mutt_parse_multipart (fp, bound,
601                                         b->offset + b->length,
602                                         ascii_strcasecmp ("digest", b->subtype) == 0);
603       break;
604
605     case TYPEMESSAGE:
606       if (b->subtype)
607       {
608         fseeko (fp, b->offset, SEEK_SET);
609         if (mutt_is_message_type(b->type, b->subtype))
610           b->parts = mutt_parse_messageRFC822 (fp, b);
611         else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
612           b->parts = mutt_read_mime_header (fp, 0);
613         else
614           return;
615       }
616       break;
617
618     default:
619       return;
620   }
621
622   /* try to recover from parsing error */
623   if (!b->parts)
624   {
625     b->type = TYPETEXT;
626     mutt_str_replace (&b->subtype, "plain");
627   }
628 }
629
630 /* parse a MESSAGE/RFC822 body
631  *
632  * args:
633  *      fp              stream to read from
634  *
635  *      parent          structure which contains info about the message/rfc822
636  *                      body part
637  *
638  * NOTE: this assumes that `parent->length' has been set!
639  */
640
641 BODY *mutt_parse_messageRFC822 (FILE *fp, BODY *parent)
642 {
643   BODY *msg;
644
645   parent->hdr = mutt_new_header ();
646   parent->hdr->offset = ftello (fp);
647   parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
648   msg = parent->hdr->content;
649
650   /* ignore the length given in the content-length since it could be wrong
651      and we already have the info to calculate the correct length */
652   /* if (msg->length == -1) */
653   msg->length = parent->length - (msg->offset - parent->offset);
654
655   /* if body of this message is empty, we can end up with a negative length */
656   if (msg->length < 0)
657     msg->length = 0;
658
659   mutt_parse_part(fp, msg);
660   return (msg);
661 }
662
663 /* parse a multipart structure
664  *
665  * args:
666  *      fp              stream to read from
667  *
668  *      boundary        body separator
669  *
670  *      end_off         length of the multipart body (used when the final
671  *                      boundary is missing to avoid reading too far)
672  *
673  *      digest          1 if reading a multipart/digest, 0 otherwise
674  */
675
676 BODY *mutt_parse_multipart (FILE *fp, const char *boundary, LOFF_T end_off, int digest)
677 {
678 #ifdef SUN_ATTACHMENT
679   int lines;
680 #endif
681   int blen, len, crlf = 0;
682   char buffer[LONG_STRING];
683   BODY *head = 0, *last = 0, *new = 0;
684   int i;
685   int final = 0; /* did we see the ending boundary? */
686
687   if (!boundary)
688   {
689     mutt_error _("multipart message has no boundary parameter!");
690     return (NULL);
691   }
692
693   blen = mutt_strlen (boundary);
694   while (ftello (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL)
695   {
696     len = mutt_strlen (buffer);
697
698     crlf =  (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
699
700     if (buffer[0] == '-' && buffer[1] == '-' &&
701         mutt_strncmp (buffer + 2, boundary, blen) == 0)
702     {
703       if (last)
704       {
705         last->length = ftello (fp) - last->offset - len - 1 - crlf;
706         if (last->parts && last->parts->length == 0)
707           last->parts->length = ftello (fp) - last->parts->offset - len - 1 - crlf;
708         /* if the body is empty, we can end up with a -1 length */
709         if (last->length < 0)
710           last->length = 0;
711       }
712
713       /* Remove any trailing whitespace, up to the length of the boundary */
714       for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
715         buffer[i] = 0;
716
717       /* Check for the end boundary */
718       if (mutt_strcmp (buffer + blen + 2, "--") == 0)
719       {
720         final = 1;
721         break; /* done parsing */
722       }
723       else if (buffer[2 + blen] == 0)
724       {
725         new = mutt_read_mime_header (fp, digest);
726
727 #ifdef SUN_ATTACHMENT
728         if (mutt_get_parameter ("content-lines", new->parameter))
729         {
730           mutt_atoi (mutt_get_parameter ("content-lines", new->parameter), &lines);
731           for ( ; lines; lines-- )
732             if (ftello (fp) >= end_off || fgets (buffer, LONG_STRING, fp) == NULL)
733               break;
734         }
735 #endif
736
737         /*
738          * Consistency checking - catch
739          * bad attachment end boundaries
740          */
741
742         if (new->offset > end_off)
743         {
744           mutt_free_body(&new);
745           break;
746         }
747         if (head)
748         {
749           last->next = new;
750           last = new;
751         }
752         else
753           last = head = new;
754       }
755     }
756   }
757
758   /* in case of missing end boundary, set the length to something reasonable */
759   if (last && last->length == 0 && !final)
760     last->length = end_off - last->offset;
761
762   /* parse recursive MIME parts */
763   for (last = head; last; last = last->next)
764     mutt_parse_part(fp, last);
765
766   return (head);
767 }
768
769 static const char *uncomment_timezone (char *buf, size_t buflen, const char *tz)
770 {
771   char *p;
772   size_t len;
773
774   if (*tz != '(')
775     return tz; /* no need to do anything */
776   tz = skip_email_wsp(tz + 1);
777   if ((p = strpbrk (tz, " )")) == NULL)
778     return tz;
779   len = p - tz;
780   if (len > buflen - 1)
781     len = buflen - 1;
782   memcpy (buf, tz, len);
783   buf[len] = 0;
784   return buf;
785 }
786
787 static const struct tz_t
788 {
789   char tzname[5];
790   unsigned char zhours;
791   unsigned char zminutes;
792   unsigned char zoccident; /* west of UTC? */
793 }
794 TimeZones[] =
795 {
796   { "aat",   1,  0, 1 }, /* Atlantic Africa Time */
797   { "adt",   4,  0, 0 }, /* Arabia DST */
798   { "ast",   3,  0, 0 }, /* Arabia */
799 /*{ "ast",   4,  0, 1 },*/ /* Atlantic */
800   { "bst",   1,  0, 0 }, /* British DST */
801   { "cat",   1,  0, 0 }, /* Central Africa */
802   { "cdt",   5,  0, 1 },
803   { "cest",  2,  0, 0 }, /* Central Europe DST */
804   { "cet",   1,  0, 0 }, /* Central Europe */
805   { "cst",   6,  0, 1 },
806 /*{ "cst",   8,  0, 0 },*/ /* China */
807 /*{ "cst",   9, 30, 0 },*/ /* Australian Central Standard Time */
808   { "eat",   3,  0, 0 }, /* East Africa */
809   { "edt",   4,  0, 1 },
810   { "eest",  3,  0, 0 }, /* Eastern Europe DST */
811   { "eet",   2,  0, 0 }, /* Eastern Europe */
812   { "egst",  0,  0, 0 }, /* Eastern Greenland DST */
813   { "egt",   1,  0, 1 }, /* Eastern Greenland */
814   { "est",   5,  0, 1 },
815   { "gmt",   0,  0, 0 },
816   { "gst",   4,  0, 0 }, /* Presian Gulf */
817   { "hkt",   8,  0, 0 }, /* Hong Kong */
818   { "ict",   7,  0, 0 }, /* Indochina */
819   { "idt",   3,  0, 0 }, /* Israel DST */
820   { "ist",   2,  0, 0 }, /* Israel */
821 /*{ "ist",   5, 30, 0 },*/ /* India */
822   { "jst",   9,  0, 0 }, /* Japan */
823   { "kst",   9,  0, 0 }, /* Korea */
824   { "mdt",   6,  0, 1 },
825   { "met",   1,  0, 0 }, /* this is now officially CET */
826   { "msd",   4,  0, 0 }, /* Moscow DST */
827   { "msk",   3,  0, 0 }, /* Moscow */
828   { "mst",   7,  0, 1 },
829   { "nzdt", 13,  0, 0 }, /* New Zealand DST */
830   { "nzst", 12,  0, 0 }, /* New Zealand */
831   { "pdt",   7,  0, 1 },
832   { "pst",   8,  0, 1 },
833   { "sat",   2,  0, 0 }, /* South Africa */
834   { "smt",   4,  0, 0 }, /* Seychelles */
835   { "sst",  11,  0, 1 }, /* Samoa */
836 /*{ "sst",   8,  0, 0 },*/ /* Singapore */
837   { "utc",   0,  0, 0 },
838   { "wat",   0,  0, 0 }, /* West Africa */
839   { "west",  1,  0, 0 }, /* Western Europe DST */
840   { "wet",   0,  0, 0 }, /* Western Europe */
841   { "wgst",  2,  0, 1 }, /* Western Greenland DST */
842   { "wgt",   3,  0, 1 }, /* Western Greenland */
843   { "wst",   8,  0, 0 }, /* Western Australia */
844 };
845
846 /* parses a date string in RFC822 format:
847  *
848  * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
849  *
850  * This routine assumes that `h' has been initialized to 0.  the `timezone'
851  * field is optional, defaulting to +0000 if missing.
852  */
853 time_t mutt_parse_date (const char *s, HEADER *h)
854 {
855   int count = 0;
856   char *t;
857   int hour, min, sec;
858   struct tm tm;
859   int i;
860   int tz_offset = 0;
861   int zhours = 0;
862   int zminutes = 0;
863   int zoccident = 0;
864   const char *ptz;
865   char tzstr[SHORT_STRING];
866   char scratch[SHORT_STRING];
867
868   /* Don't modify our argument. Fixed-size buffer is ok here since
869    * the date format imposes a natural limit.
870    */
871
872   strfcpy (scratch, s, sizeof (scratch));
873
874   /* kill the day of the week, if it exists. */
875   if ((t = strchr (scratch, ',')))
876     t++;
877   else
878     t = scratch;
879   t = skip_email_wsp(t);
880
881   memset (&tm, 0, sizeof (tm));
882
883   while ((t = strtok (t, " \t")) != NULL)
884   {
885     switch (count)
886     {
887       case 0: /* day of the month */
888         if (mutt_atoi (t, &tm.tm_mday) < 0 || tm.tm_mday < 0)
889           return (-1);
890         if (tm.tm_mday > 31)
891           return (-1);
892         break;
893
894       case 1: /* month of the year */
895         if ((i = mutt_check_month (t)) < 0)
896           return (-1);
897         tm.tm_mon = i;
898         break;
899
900       case 2: /* year */
901         if (mutt_atoi (t, &tm.tm_year) < 0 || tm.tm_year < 0)
902           return (-1);
903         if (tm.tm_year < 50)
904           tm.tm_year += 100;
905         else if (tm.tm_year >= 1900)
906           tm.tm_year -= 1900;
907         break;
908
909       case 3: /* time of day */
910         if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3)
911           ;
912         else if (sscanf (t, "%d:%d", &hour, &min) == 2)
913           sec = 0;
914         else
915         {
916           dprint(1, (debugfile, "parse_date: could not process time format: %s\n", t));
917           return(-1);
918         }
919         tm.tm_hour = hour;
920         tm.tm_min = min;
921         tm.tm_sec = sec;
922         break;
923
924       case 4: /* timezone */
925         /* sometimes we see things like (MST) or (-0700) so attempt to
926          * compensate by uncommenting the string if non-RFC822 compliant
927          */
928         ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
929
930         if (*ptz == '+' || *ptz == '-')
931         {
932           if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
933               && isdigit ((unsigned char) ptz[1]) && isdigit ((unsigned char) ptz[2])
934               && isdigit ((unsigned char) ptz[3]) && isdigit ((unsigned char) ptz[4]))
935           {
936             zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
937             zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
938
939             if (ptz[0] == '-')
940               zoccident = 1;
941           }
942         }
943         else
944         {
945           struct tz_t *tz;
946
947           tz = bsearch (ptz, TimeZones, sizeof TimeZones/sizeof (struct tz_t),
948                         sizeof (struct tz_t),
949                         (int (*)(const void *, const void *)) ascii_strcasecmp
950                         /* This is safe to do: A pointer to a struct equals
951                          * a pointer to its first element*/);
952
953           if (tz)
954           {
955             zhours = tz->zhours;
956             zminutes = tz->zminutes;
957             zoccident = tz->zoccident;
958           }
959
960           /* ad hoc support for the European MET (now officially CET) TZ */
961           if (ascii_strcasecmp (t, "MET") == 0)
962           {
963             if ((t = strtok (NULL, " \t")) != NULL)
964             {
965               if (!ascii_strcasecmp (t, "DST"))
966                 zhours++;
967             }
968           }
969         }
970         tz_offset = zhours * 3600 + zminutes * 60;
971         if (!zoccident)
972           tz_offset = -tz_offset;
973         break;
974     }
975     count++;
976     t = 0;
977   }
978
979   if (count < 4) /* don't check for missing timezone */
980   {
981     dprint(1,(debugfile, "parse_date(): error parsing date format, using received time\n"));
982     return (-1);
983   }
984
985   if (h)
986   {
987     h->zhours = zhours;
988     h->zminutes = zminutes;
989     h->zoccident = zoccident;
990   }
991
992   return (mutt_mktime (&tm, 0) + tz_offset);
993 }
994
995 /* extract the first substring that looks like a message-id.
996  * call back with NULL for more (like strtok).
997  */
998 char *mutt_extract_message_id (const char *s, const char **saveptr)
999 {
1000   const char *o, *onull, *p;
1001   char *ret = NULL;
1002
1003   if (s)
1004     p = s;
1005   else if (saveptr)
1006     p = *saveptr;
1007   else
1008     return NULL;
1009
1010   for (s = NULL, o = NULL, onull = NULL;
1011        (p = strpbrk (p, "<> \t;")) != NULL; ++p)
1012   {
1013     if (*p == '<')
1014     {
1015       s = p;
1016       o = onull = NULL;
1017       continue;
1018     }
1019
1020     if (!s)
1021       continue;
1022
1023     if (*p == '>')
1024     {
1025       size_t olen = onull - o, slen = p - s + 1;
1026       ret = safe_malloc (olen + slen + 1);
1027       if (o)
1028         memcpy (ret, o, olen);
1029       memcpy (ret + olen, s, slen);
1030       ret[olen + slen] = '\0';
1031       if (saveptr)
1032         *saveptr = p + 1; /* next call starts after '>' */
1033       return ret;
1034     }
1035
1036     /* some idiotic clients break their message-ids between lines */
1037     if (s == p)
1038       /* step past another whitespace */
1039       s = p + 1;
1040     else if (o)
1041       /* more than two lines, give up */
1042       s = o = onull = NULL;
1043     else
1044     {
1045       /* remember the first line, start looking for the second */
1046       o = s;
1047       onull = p;
1048       s = p + 1;
1049     }
1050   }
1051
1052   return NULL;
1053 }
1054
1055 void mutt_parse_mime_message (CONTEXT *ctx, HEADER *cur)
1056 {
1057   MESSAGE *msg;
1058
1059   do
1060   {
1061     if (cur->content->type != TYPEMESSAGE &&
1062         cur->content->type != TYPEMULTIPART)
1063       break; /* nothing to do */
1064
1065     if (cur->content->parts)
1066       break; /* The message was parsed earlier. */
1067
1068     if ((msg = mx_open_message (ctx, cur->msgno)))
1069     {
1070       mutt_parse_part (msg->fp, cur->content);
1071
1072       if (WithCrypto)
1073         cur->security = crypt_query (cur->content);
1074
1075       mx_close_message (ctx, &msg);
1076     }
1077   } while (0);
1078
1079   cur->attach_valid = 0;
1080 }
1081
1082 void mutt_auto_subscribe (const char *mailto)
1083 {
1084   ENVELOPE *lpenv;
1085
1086   if (!AutoSubscribeCache)
1087     AutoSubscribeCache = hash_create (200, MUTT_HASH_STRCASECMP | MUTT_HASH_STRDUP_KEYS);
1088
1089   if (!mailto || hash_find (AutoSubscribeCache, mailto))
1090     return;
1091
1092   hash_insert (AutoSubscribeCache, mailto, AutoSubscribeCache);
1093
1094   lpenv = mutt_new_envelope (); /* parsed envelope from the List-Post mailto: URL */
1095
1096   if ((url_parse_mailto (lpenv, NULL, mailto) != -1) &&
1097       lpenv->to && lpenv->to->mailbox &&
1098       !mutt_match_rx_list (lpenv->to->mailbox, SubscribedLists) &&
1099       !mutt_match_rx_list (lpenv->to->mailbox, UnMailLists) &&
1100       !mutt_match_rx_list (lpenv->to->mailbox, UnSubscribedLists))
1101   {
1102     BUFFER err;
1103     char errbuf[STRING];
1104
1105     memset (&err, 0, sizeof(err));
1106     err.data = errbuf;
1107     err.dsize = sizeof(errbuf);
1108
1109     /* mutt_add_to_rx_list() detects duplicates, so it is safe to
1110      * try to add here without any checks. */
1111     mutt_add_to_rx_list (&MailLists, lpenv->to->mailbox, REG_ICASE, &err);
1112     mutt_add_to_rx_list (&SubscribedLists, lpenv->to->mailbox, REG_ICASE, &err);
1113   }
1114   mutt_free_envelope (&lpenv);
1115 }
1116
1117 int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short user_hdrs, short weed,
1118                             short do_2047, LIST **lastp)
1119 {
1120   int matched = 0;
1121
1122   switch (ascii_tolower (line[0]))
1123   {
1124     case 'a':
1125       if (ascii_strcasecmp (line+1, "pparently-to") == 0)
1126       {
1127         e->to = rfc822_parse_adrlist (e->to, p);
1128         matched = 1;
1129       }
1130       else if (ascii_strcasecmp (line+1, "pparently-from") == 0)
1131       {
1132         e->from = rfc822_parse_adrlist (e->from, p);
1133         matched = 1;
1134       }
1135 #ifdef USE_AUTOCRYPT
1136       else if (ascii_strcasecmp (line+1, "utocrypt") == 0)
1137       {
1138         if (option (OPTAUTOCRYPT))
1139         {
1140           e->autocrypt = parse_autocrypt (e->autocrypt, p);
1141           matched = 1;
1142         }
1143       }
1144       else if (ascii_strcasecmp (line+1, "utocrypt-gossip") == 0)
1145       {
1146         if (option (OPTAUTOCRYPT))
1147         {
1148           e->autocrypt_gossip = parse_autocrypt (e->autocrypt_gossip, p);
1149           matched = 1;
1150         }
1151       }
1152 #endif
1153       break;
1154
1155     case 'b':
1156       if (ascii_strcasecmp (line+1, "cc") == 0)
1157       {
1158         e->bcc = rfc822_parse_adrlist (e->bcc, p);
1159         matched = 1;
1160       }
1161       break;
1162
1163     case 'c':
1164       if (ascii_strcasecmp (line+1, "c") == 0)
1165       {
1166         e->cc = rfc822_parse_adrlist (e->cc, p);
1167         matched = 1;
1168       }
1169       else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0)
1170       {
1171         if (ascii_strcasecmp (line+8, "type") == 0)
1172         {
1173           if (hdr)
1174             mutt_parse_content_type (p, hdr->content);
1175           matched = 1;
1176         }
1177         else if (ascii_strcasecmp (line+8, "transfer-encoding") == 0)
1178         {
1179           if (hdr)
1180             hdr->content->encoding = mutt_check_encoding (p);
1181           matched = 1;
1182         }
1183         else if (ascii_strcasecmp (line+8, "length") == 0)
1184         {
1185           if (hdr)
1186           {
1187             if ((hdr->content->length = atol (p)) < 0)
1188               hdr->content->length = -1;
1189           }
1190           matched = 1;
1191         }
1192         else if (ascii_strcasecmp (line+8, "description") == 0)
1193         {
1194           if (hdr)
1195           {
1196             mutt_str_replace (&hdr->content->description, p);
1197             rfc2047_decode (&hdr->content->description);
1198           }
1199           matched = 1;
1200         }
1201         else if (ascii_strcasecmp (line+8, "disposition") == 0)
1202         {
1203           if (hdr)
1204             parse_content_disposition (p, hdr->content);
1205           matched = 1;
1206         }
1207       }
1208       break;
1209
1210     case 'd':
1211       if (!ascii_strcasecmp ("ate", line + 1))
1212       {
1213         mutt_str_replace (&e->date, p);
1214         if (hdr)
1215           hdr->date_sent = mutt_parse_date (p, hdr);
1216         matched = 1;
1217       }
1218       break;
1219
1220     case 'e':
1221       if (!ascii_strcasecmp ("xpires", line + 1) &&
1222           hdr && mutt_parse_date (p, NULL) < time (NULL))
1223         hdr->expired = 1;
1224       break;
1225
1226     case 'f':
1227       if (!ascii_strcasecmp ("rom", line + 1))
1228       {
1229         e->from = rfc822_parse_adrlist (e->from, p);
1230         matched = 1;
1231       }
1232       break;
1233
1234     case 'i':
1235       if (!ascii_strcasecmp (line+1, "n-reply-to"))
1236       {
1237         mutt_free_list (&e->in_reply_to);
1238         e->in_reply_to = mutt_parse_references (p, 1);
1239         matched = 1;
1240       }
1241       break;
1242
1243     case 'l':
1244       if (!ascii_strcasecmp (line + 1, "ines"))
1245       {
1246         if (hdr)
1247         {
1248           /*
1249            * HACK - mutt has, for a very short time, produced negative
1250            * Lines header values.  Ignore them.
1251            */
1252           if (mutt_atoi (p, &hdr->lines) < 0 || hdr->lines < 0)
1253             hdr->lines = 0;
1254         }
1255
1256         matched = 1;
1257       }
1258       else if (!ascii_strcasecmp (line + 1, "ist-Post"))
1259       {
1260         /* RFC 2369.  FIXME: We should ignore whitespace, but don't. */
1261         if (strncmp (p, "NO", 2))
1262         {
1263           char *beg, *end;
1264           for (beg = strchr (p, '<'); beg; beg = strchr (end, ','))
1265           {
1266             ++beg;
1267             if (!(end = strchr (beg, '>')))
1268               break;
1269
1270             /* Take the first mailto URL */
1271             if (url_check_scheme (beg) == U_MAILTO)
1272             {
1273               FREE (&e->list_post);
1274               e->list_post = mutt_substrdup (beg, end);
1275               if (option (OPTAUTOSUBSCRIBE))
1276                 mutt_auto_subscribe (e->list_post);
1277               break;
1278             }
1279           }
1280         }
1281         matched = 1;
1282       }
1283       break;
1284
1285     case 'm':
1286       if (!ascii_strcasecmp (line + 1, "ime-version"))
1287       {
1288         if (hdr)
1289           hdr->mime = 1;
1290         matched = 1;
1291       }
1292       else if (!ascii_strcasecmp (line + 1, "essage-id"))
1293       {
1294         /* We add a new "Message-ID:" when building a message */
1295         FREE (&e->message_id);
1296         e->message_id = mutt_extract_message_id (p, NULL);
1297         matched = 1;
1298       }
1299       else if (!ascii_strncasecmp (line + 1, "ail-", 4))
1300       {
1301         if (!ascii_strcasecmp (line + 5, "reply-to"))
1302         {
1303           /* override the Reply-To: field */
1304           rfc822_free_address (&e->reply_to);
1305           e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1306           matched = 1;
1307         }
1308         else if (!ascii_strcasecmp (line + 5, "followup-to"))
1309         {
1310           e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1311           matched = 1;
1312         }
1313       }
1314       break;
1315
1316     case 'r':
1317       if (!ascii_strcasecmp (line + 1, "eferences"))
1318       {
1319         mutt_free_list (&e->references);
1320         e->references = mutt_parse_references (p, 0);
1321         matched = 1;
1322       }
1323       else if (!ascii_strcasecmp (line + 1, "eply-to"))
1324       {
1325         e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1326         matched = 1;
1327       }
1328       else if (!ascii_strcasecmp (line + 1, "eturn-path"))
1329       {
1330         e->return_path = rfc822_parse_adrlist (e->return_path, p);
1331         matched = 1;
1332       }
1333       else if (!ascii_strcasecmp (line + 1, "eceived"))
1334       {
1335         if (hdr && !hdr->received)
1336         {
1337           char *d = strrchr (p, ';');
1338
1339           if (d)
1340             hdr->received = mutt_parse_date (d + 1, NULL);
1341         }
1342       }
1343       break;
1344
1345     case 's':
1346       if (!ascii_strcasecmp (line + 1, "ubject"))
1347       {
1348         if (!e->subject)
1349           e->subject = safe_strdup (p);
1350         matched = 1;
1351       }
1352       else if (!ascii_strcasecmp (line + 1, "ender"))
1353       {
1354         e->sender = rfc822_parse_adrlist (e->sender, p);
1355         matched = 1;
1356       }
1357       else if (!ascii_strcasecmp (line + 1, "tatus"))
1358       {
1359         if (hdr)
1360         {
1361           while (*p)
1362           {
1363             switch (*p)
1364             {
1365               case 'r':
1366                 hdr->replied = 1;
1367                 break;
1368               case 'O':
1369                 hdr->old = 1;
1370                 break;
1371               case 'R':
1372                 hdr->read = 1;
1373                 break;
1374             }
1375             p++;
1376           }
1377         }
1378         matched = 1;
1379       }
1380       else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1381                 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1382       {
1383         FREE(&e->supersedes);
1384         e->supersedes = safe_strdup (p);
1385       }
1386       break;
1387
1388     case 't':
1389       if (ascii_strcasecmp (line+1, "o") == 0)
1390       {
1391         e->to = rfc822_parse_adrlist (e->to, p);
1392         matched = 1;
1393       }
1394       break;
1395
1396     case 'x':
1397       if (ascii_strcasecmp (line+1, "-status") == 0)
1398       {
1399         if (hdr)
1400         {
1401           while (*p)
1402           {
1403             switch (*p)
1404             {
1405               case 'A':
1406                 hdr->replied = 1;
1407                 break;
1408               case 'D':
1409                 hdr->deleted = 1;
1410                 break;
1411               case 'F':
1412                 hdr->flagged = 1;
1413                 break;
1414               default:
1415                 break;
1416             }
1417             p++;
1418           }
1419         }
1420         matched = 1;
1421       }
1422       else if (ascii_strcasecmp (line+1, "-label") == 0)
1423       {
1424         FREE(&e->x_label);
1425         e->x_label = safe_strdup(p);
1426         matched = 1;
1427       }
1428
1429     default:
1430       break;
1431   }
1432
1433   /* Keep track of the user-defined headers */
1434   if (!matched && user_hdrs)
1435   {
1436     LIST *last = NULL;
1437
1438     if (lastp)
1439       last = *lastp;
1440
1441     /* restore the original line */
1442     line[strlen (line)] = ':';
1443
1444     if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1445         && !mutt_matches_ignore (line, UnIgnore))
1446       goto done;
1447
1448     if (last)
1449     {
1450       last->next = mutt_new_list ();
1451       last = last->next;
1452     }
1453     else
1454       last = e->userhdrs = mutt_new_list ();
1455     last->data = safe_strdup (line);
1456     if (do_2047)
1457       rfc2047_decode (&last->data);
1458
1459     if (lastp)
1460       *lastp = last;
1461   }
1462
1463 done:
1464   return matched;
1465 }
1466
1467
1468 /* mutt_read_rfc822_header() -- parses a RFC822 header
1469  *
1470  * Args:
1471  *
1472  * f            stream to read from
1473  *
1474  * hdr          header structure of current message (optional).
1475  *
1476  * user_hdrs    If set, store user headers.  Used for recall-message and
1477  *              postpone modes.
1478  *
1479  * weed         If this parameter is set and the user has activated the
1480  *              $weed option, honor the header weed list for user headers.
1481  *              Used for recall-message.
1482  *
1483  * Returns:     newly allocated envelope structure.  You should free it by
1484  *              mutt_free_envelope() when envelope stay unneeded.
1485  */
1486 ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
1487                                    short weed)
1488 {
1489   ENVELOPE *e = mutt_new_envelope();
1490   LIST *last = NULL;
1491   char *line = safe_malloc (LONG_STRING);
1492   char *p;
1493   LOFF_T loc;
1494   size_t linelen = LONG_STRING;
1495   char buf[LONG_STRING+1];
1496
1497   if (hdr)
1498   {
1499     if (hdr->content == NULL)
1500     {
1501       hdr->content = mutt_new_body ();
1502
1503       /* set the defaults from RFC1521 */
1504       hdr->content->type        = TYPETEXT;
1505       hdr->content->subtype     = safe_strdup ("plain");
1506       hdr->content->encoding    = ENC7BIT;
1507       hdr->content->length      = -1;
1508
1509       /* RFC 2183 says this is arbitrary */
1510       hdr->content->disposition = DISPINLINE;
1511     }
1512   }
1513
1514   while ((loc = ftello (f)),
1515          *(line = mutt_read_rfc822_line (f, line, &linelen)) != 0)
1516   {
1517     if ((p = strpbrk (line, ": \t")) == NULL || *p != ':')
1518     {
1519       char return_path[LONG_STRING];
1520       time_t t;
1521
1522       /* some bogus MTAs will quote the original "From " line */
1523       if (mutt_strncmp (">From ", line, 6) == 0)
1524         continue; /* just ignore */
1525       else if (is_from (line, return_path, sizeof (return_path), &t))
1526       {
1527         /* MH sometimes has the From_ line in the middle of the header! */
1528         if (hdr && !hdr->received)
1529           hdr->received = t - mutt_local_tz (t);
1530         continue;
1531       }
1532
1533       fseeko (f, loc, 0);
1534       break; /* end of header */
1535     }
1536
1537     *buf = '\0';
1538
1539     if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf)))
1540     {
1541       if (!mutt_match_rx_list(line, NoSpamList))
1542       {
1543
1544         /* if spam tag already exists, figure out how to amend it */
1545         if (e->spam && *buf)
1546         {
1547           /* If SpamSep defined, append with separator */
1548           if (SpamSep)
1549           {
1550             mutt_buffer_addstr(e->spam, SpamSep);
1551             mutt_buffer_addstr(e->spam, buf);
1552           }
1553
1554           /* else overwrite */
1555           else
1556           {
1557             mutt_buffer_clear (e->spam);
1558             mutt_buffer_addstr(e->spam, buf);
1559           }
1560         }
1561
1562         /* spam tag is new, and match expr is non-empty; copy */
1563         else if (!e->spam && *buf)
1564         {
1565           e->spam = mutt_buffer_from (buf);
1566         }
1567
1568         /* match expr is empty; plug in null string if no existing tag */
1569         else if (!e->spam)
1570         {
1571           e->spam = mutt_buffer_from("");
1572         }
1573
1574         if (e->spam && e->spam->data)
1575           dprint(5, (debugfile, "p822: spam = %s\n", e->spam->data));
1576       }
1577     }
1578
1579     *p = 0;
1580     p = skip_email_wsp(p + 1);
1581     if (!*p)
1582       continue; /* skip empty header fields */
1583
1584     mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1585   }
1586
1587   FREE (&line);
1588
1589   if (hdr)
1590   {
1591     hdr->content->hdr_offset = hdr->offset;
1592     hdr->content->offset = ftello (f);
1593
1594     rfc2047_decode_envelope (e);
1595
1596     if (e->subject)
1597     {
1598       regmatch_t pmatch[1];
1599
1600       if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1601         e->real_subj = e->subject + pmatch[0].rm_eo;
1602       else
1603         e->real_subj = e->subject;
1604     }
1605
1606     if (hdr->received < 0)
1607     {
1608       dprint(1,(debugfile,"read_rfc822_header(): resetting invalid received time to 0\n"));
1609       hdr->received = 0;
1610     }
1611
1612     /* check for missing or invalid date */
1613     if (hdr->date_sent <= 0)
1614     {
1615       dprint(1,(debugfile,"read_rfc822_header(): no date found, using received time from msg separator\n"));
1616       hdr->date_sent = hdr->received;
1617     }
1618
1619 #ifdef USE_AUTOCRYPT
1620     if (option (OPTAUTOCRYPT))
1621     {
1622       mutt_autocrypt_process_autocrypt_header (hdr, e);
1623       /* No sense in taking up memory after the header is processed */
1624       mutt_free_autocrypthdr (&e->autocrypt);
1625     }
1626 #endif
1627   }
1628
1629   return (e);
1630 }
1631
1632 ADDRESS *mutt_parse_adrlist (ADDRESS *p, const char *s)
1633 {
1634   const char *q;
1635
1636   /* check for a simple whitespace separated list of addresses */
1637   if ((q = strpbrk (s, "\"<>():;,\\")) == NULL)
1638   {
1639     BUFFER *tmp;
1640     char *r;
1641
1642     tmp = mutt_buffer_pool_get ();
1643     mutt_buffer_strcpy (tmp, s);
1644     r = tmp->data;
1645     while ((r = strtok (r, " \t")) != NULL)
1646     {
1647       p = rfc822_parse_adrlist (p, r);
1648       r = NULL;
1649     }
1650     mutt_buffer_pool_release (&tmp);
1651   }
1652   else
1653     p = rfc822_parse_adrlist (p, s);
1654
1655   return p;
1656 }
1657
1658 /* Compares mime types to the ok and except lists */
1659 static int count_body_parts_check(LIST **checklist, BODY *b, int dflt)
1660 {
1661   LIST *type;
1662   ATTACH_MATCH *a;
1663
1664   /* If list is null, use default behavior. */
1665   if (! *checklist)
1666   {
1667     /*return dflt;*/
1668     return 0;
1669   }
1670
1671   for (type = *checklist; type; type = type->next)
1672   {
1673     a = (ATTACH_MATCH *)type->data;
1674     dprint(5, (debugfile, "cbpc: %s %d/%s ?? %s/%s [%d]... ",
1675                dflt ? "[OK]   " : "[EXCL] ",
1676                b->type, b->subtype, a->major, a->minor, a->major_int));
1677     if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1678         !regexec(&a->minor_rx, b->subtype, 0, NULL, 0))
1679     {
1680       dprint(5, (debugfile, "yes\n"));
1681       return 1;
1682     }
1683     else
1684     {
1685       dprint(5, (debugfile, "no\n"));
1686     }
1687   }
1688
1689   return 0;
1690 }
1691
1692 #define AT_COUNT(why)   { shallcount = 1; }
1693 #define AT_NOCOUNT(why) { shallcount = 0; }
1694
1695 static int count_body_parts (BODY *body, int flags)
1696 {
1697   int count = 0;
1698   int shallcount, shallrecurse;
1699   BODY *bp;
1700
1701   if (body == NULL)
1702     return 0;
1703
1704   for (bp = body; bp != NULL; bp = bp->next)
1705   {
1706     /* Initial disposition is to count and not to recurse this part. */
1707     AT_COUNT("default");
1708     shallrecurse = 0;
1709
1710     dprint(5, (debugfile, "bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1711                bp->description ? bp->description : ("none"),
1712                bp->filename ? bp->filename :
1713                bp->d_filename ? bp->d_filename : "(none)",
1714                bp->type, bp->subtype ? bp->subtype : "*"));
1715
1716     if (bp->type == TYPEMESSAGE)
1717     {
1718       shallrecurse = 1;
1719
1720       /* If it's an external body pointer, don't recurse it. */
1721       if (!ascii_strcasecmp (bp->subtype, "external-body"))
1722         shallrecurse = 0;
1723
1724       /* Don't count containers if they're top-level. */
1725       if (flags & MUTT_PARTS_TOPLEVEL)
1726         AT_NOCOUNT("top-level message/*");
1727     }
1728     else if (bp->type == TYPEMULTIPART)
1729     {
1730       /* Always recurse multiparts, except multipart/alternative. */
1731       shallrecurse = 1;
1732       if (!ascii_strcasecmp(bp->subtype, "alternative"))
1733         shallrecurse = 0;
1734
1735       /* Don't count containers if they're top-level. */
1736       if (flags & MUTT_PARTS_TOPLEVEL)
1737         AT_NOCOUNT("top-level multipart");
1738     }
1739
1740     if (bp->disposition == DISPINLINE &&
1741         bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1742       AT_NOCOUNT("ignore fundamental inlines");
1743
1744     /* If this body isn't scheduled for enumeration already, don't bother
1745      * profiling it further.
1746      */
1747     if (shallcount)
1748     {
1749       /* Turn off shallcount if message type is not in ok list,
1750        * or if it is in except list. Check is done separately for
1751        * inlines vs. attachments.
1752        */
1753
1754       if (bp->disposition == DISPATTACH)
1755       {
1756         if (!count_body_parts_check(&AttachAllow, bp, 1))
1757           AT_NOCOUNT("attach not allowed");
1758         if (count_body_parts_check(&AttachExclude, bp, 0))
1759           AT_NOCOUNT("attach excluded");
1760       }
1761       else
1762       {
1763         if (!count_body_parts_check(&InlineAllow, bp, 1))
1764           AT_NOCOUNT("inline not allowed");
1765         if (count_body_parts_check(&InlineExclude, bp, 0))
1766           AT_NOCOUNT("excluded");
1767       }
1768     }
1769
1770     if (shallcount)
1771       count++;
1772     bp->attach_qualifies = shallcount ? 1 : 0;
1773
1774     dprint(5, (debugfile, "cbp: %p shallcount = %d\n", (void *)bp, shallcount));
1775
1776     if (shallrecurse)
1777     {
1778       dprint(5, (debugfile, "cbp: %p pre count = %d\n", (void *)bp, count));
1779       bp->attach_count = count_body_parts(bp->parts, flags & ~MUTT_PARTS_TOPLEVEL);
1780       count += bp->attach_count;
1781       dprint(5, (debugfile, "cbp: %p post count = %d\n", (void *)bp, count));
1782     }
1783   }
1784
1785   dprint(5, (debugfile, "bp: return %d\n", count < 0 ? 0 : count));
1786   return count < 0 ? 0 : count;
1787 }
1788
1789 int mutt_count_body_parts (CONTEXT *ctx, HEADER *hdr)
1790 {
1791   short keep_parts = 0;
1792
1793   if (hdr->attach_valid)
1794     return hdr->attach_total;
1795
1796   if (hdr->content->parts)
1797     keep_parts = 1;
1798   else
1799     mutt_parse_mime_message (ctx, hdr);
1800
1801   if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1802     hdr->attach_total = count_body_parts(hdr->content, MUTT_PARTS_TOPLEVEL);
1803   else
1804     hdr->attach_total = 0;
1805
1806   hdr->attach_valid = 1;
1807
1808   if (!keep_parts)
1809     mutt_free_body (&hdr->content->parts);
1810
1811   return hdr->attach_total;
1812 }