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