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