2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
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.
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.
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.
24 #include "mutt_regex.h"
29 #include "mutt_crypt.h"
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.
41 char *mutt_read_rfc822_line (FILE *f, char *line, size_t *linelen)
49 if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */
50 (ISSPACE (*line) && !offset)) /* end of headers */
56 buf += strlen (buf) - 1;
59 /* we did get a full line. remove trailing space */
60 while (ISSPACE (*buf))
61 *buf-- = 0; /* we cannot come beyond line's beginning because
62 * it begins with a non-space */
64 /* check to see if the next line is a continuation line */
65 if ((ch = fgetc (f)) != ' ' && ch != '\t')
68 return (line); /* next line is a separate header field or EOH */
71 /* eat tabs and spaces from the beginning of the continuation line */
72 while ((ch = fgetc (f)) == ' ' || ch == '\t')
75 *++buf = ' '; /* string is still terminated because we removed
76 at least one whitespace char above */
81 if (*linelen < offset + STRING)
85 safe_realloc (&line, *linelen);
92 static LIST *mutt_parse_references (char *s, int in_reply_to)
98 m = mutt_extract_message_id (s, &sp);
101 t = safe_malloc (sizeof (LIST));
106 m = mutt_extract_message_id (NULL, &sp);
112 int mutt_check_encoding (const char *c)
114 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit")-1) == 0)
116 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit")-1) == 0)
118 else if (ascii_strncasecmp ("binary", c, sizeof ("binary")-1) == 0)
120 else if (ascii_strncasecmp ("quoted-printable", c, sizeof ("quoted-printable")-1) == 0)
121 return (ENCQUOTEDPRINTABLE);
122 else if (ascii_strncasecmp ("base64", c, sizeof("base64")-1) == 0)
124 else if (ascii_strncasecmp ("x-uuencode", c, sizeof("x-uuencode")-1) == 0)
125 return (ENCUUENCODED);
126 #ifdef SUN_ATTACHMENT
127 else if (ascii_strncasecmp ("uuencode", c, sizeof("uuencode")-1) == 0)
128 return (ENCUUENCODED);
134 static PARAMETER *parse_parameters (const char *s)
136 PARAMETER *head = 0, *cur = 0, *new;
137 char buffer[LONG_STRING];
141 dprint (2, (debugfile, "parse_parameters: `%s'\n", s));
145 if ((p = strpbrk (s, "=;")) == NULL)
147 dprint(1, (debugfile, "parse_parameters: malformed parameter: %s\n", s));
151 /* if we hit a ; now the parameter has no value, just skip it */
156 new = mutt_new_parameter ();
158 new->attribute = safe_malloc (i + 1);
159 memcpy (new->attribute, s, i);
160 new->attribute[i] = 0;
162 /* remove whitespace from the end of the attribute name */
163 while (ISSPACE (new->attribute[--i]))
164 new->attribute[i] = 0;
166 s = p + 1; /* skip over the = */
173 for (i=0; *s && i < sizeof (buffer) - 1; i++, s++)
175 if (AssumedCharset && *AssumedCharset) {
176 /* As iso-2022-* has a characer of '"' with non-ascii state,
178 if (*s == 0x1b && i < sizeof (buffer) - 2)
180 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
186 if (state_ascii && *s == '"')
190 /* Quote the next character */
200 s++; /* skip over the " */
204 for (i=0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1; i++, s++)
209 new->value = safe_strdup (buffer);
211 dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n",
212 new->attribute ? new->attribute : "",
213 new->value ? new->value : ""));
215 /* Add this parameter to the list */
226 dprint (1, (debugfile, "parse_parameters(): parameter with no value: %s\n", s));
230 /* Find the next parameter */
231 if (*s != ';' && (s = strchr (s, ';')) == NULL)
232 break; /* no more parameters */
238 /* Move past any leading whitespace */
241 while (*s == ';'); /* skip empty parameters */
246 rfc2231_decode_parameters (&head);
250 int mutt_check_mime_type (const char *s)
252 if (ascii_strcasecmp ("text", s) == 0)
254 else if (ascii_strcasecmp ("multipart", s) == 0)
255 return TYPEMULTIPART;
256 #ifdef SUN_ATTACHMENT
257 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
258 return TYPEMULTIPART;
260 else if (ascii_strcasecmp ("application", s) == 0)
261 return TYPEAPPLICATION;
262 else if (ascii_strcasecmp ("message", s) == 0)
264 else if (ascii_strcasecmp ("image", s) == 0)
266 else if (ascii_strcasecmp ("audio", s) == 0)
268 else if (ascii_strcasecmp ("video", s) == 0)
270 else if (ascii_strcasecmp ("model", s) == 0)
272 else if (ascii_strcasecmp ("*", s) == 0)
274 else if (ascii_strcasecmp (".*", s) == 0)
280 void mutt_parse_content_type (char *s, BODY *ct)
286 mutt_free_parameter(&ct->parameter);
288 /* First extract any existing parameters */
289 if ((pc = strchr(s, ';')) != NULL)
292 while (*pc && ISSPACE (*pc))
294 ct->parameter = parse_parameters(pc);
296 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
297 * but if a filename has already been set in the content-disposition,
298 * let that take precedence, and don't set it here */
299 if ((pc = mutt_get_parameter( "name", ct->parameter)) && !ct->filename)
300 ct->filename = safe_strdup(pc);
302 #ifdef SUN_ATTACHMENT
303 /* this is deep and utter perversion */
304 if ((pc = mutt_get_parameter ("conversions", ct->parameter)))
305 ct->encoding = mutt_check_encoding (pc);
310 /* Now get the subtype */
311 if ((subtype = strchr(s, '/')))
314 for(pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++)
317 ct->subtype = safe_strdup (subtype);
320 /* Finally, get the major type */
321 ct->type = mutt_check_mime_type (s);
323 #ifdef SUN_ATTACHMENT
324 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
325 ct->subtype = safe_strdup ("x-sun-attachment");
328 if (ct->type == TYPEOTHER)
330 ct->xtype = safe_strdup (s);
333 if (ct->subtype == NULL)
335 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
336 * field, so we can attempt to convert the type to BODY here.
338 if (ct->type == TYPETEXT)
339 ct->subtype = safe_strdup ("plain");
340 else if (ct->type == TYPEAUDIO)
341 ct->subtype = safe_strdup ("basic");
342 else if (ct->type == TYPEMESSAGE)
343 ct->subtype = safe_strdup ("rfc822");
344 else if (ct->type == TYPEOTHER)
346 char buffer[SHORT_STRING];
348 ct->type = TYPEAPPLICATION;
349 snprintf (buffer, sizeof (buffer), "x-%s", s);
350 ct->subtype = safe_strdup (buffer);
353 ct->subtype = safe_strdup ("x-unknown");
356 /* Default character set for text types. */
357 if (ct->type == TYPETEXT)
359 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
360 mutt_set_parameter ("charset", (AssumedCharset && *AssumedCharset) ?
361 (const char *) mutt_get_default_charset ()
362 : "us-ascii", &ct->parameter);
367 static void parse_content_disposition (char *s, BODY *ct)
371 if (!ascii_strncasecmp ("inline", s, 6))
372 ct->disposition = DISPINLINE;
373 else if (!ascii_strncasecmp ("form-data", s, 9))
374 ct->disposition = DISPFORMDATA;
376 ct->disposition = DISPATTACH;
378 /* Check to see if a default filename was given */
379 if ((s = strchr (s, ';')) != NULL)
383 if ((s = mutt_get_parameter ("filename", (parms = parse_parameters (s)))))
384 mutt_str_replace (&ct->filename, s);
385 if ((s = mutt_get_parameter ("name", parms)))
386 ct->form_name = safe_strdup (s);
387 mutt_free_parameter (&parms);
392 * fp stream to read from
394 * digest 1 if reading subparts of a multipart/digest, 0
398 BODY *mutt_read_mime_header (FILE *fp, int digest)
400 BODY *p = mutt_new_body();
402 char *line = safe_malloc (LONG_STRING);
403 size_t linelen = LONG_STRING;
405 p->hdr_offset = ftello (fp);
407 p->encoding = ENC7BIT; /* default from RFC1521 */
408 p->type = digest ? TYPEMESSAGE : TYPETEXT;
409 p->disposition = DISPINLINE;
411 while (*(line = mutt_read_rfc822_line (fp, line, &linelen)) != 0)
413 /* Find the value of the current header */
414 if ((c = strchr (line, ':')))
421 dprint (1, (debugfile, "mutt_read_mime_header(): skipping empty header field: %s\n", line));
427 dprint (1, (debugfile, "read_mime_header: bogus MIME header: %s\n", line));
431 if (!ascii_strncasecmp ("content-", line, 8))
433 if (!ascii_strcasecmp ("type", line + 8))
434 mutt_parse_content_type (c, p);
435 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
436 p->encoding = mutt_check_encoding (c);
437 else if (!ascii_strcasecmp ("disposition", line + 8))
438 parse_content_disposition (c, p);
439 else if (!ascii_strcasecmp ("description", line + 8))
441 mutt_str_replace (&p->description, c);
442 rfc2047_decode (&p->description);
445 #ifdef SUN_ATTACHMENT
446 else if (!ascii_strncasecmp ("x-sun-", line, 6))
448 if (!ascii_strcasecmp ("data-type", line + 6))
449 mutt_parse_content_type (c, p);
450 else if (!ascii_strcasecmp ("encoding-info", line + 6))
451 p->encoding = mutt_check_encoding (c);
452 else if (!ascii_strcasecmp ("content-lines", line + 6))
453 mutt_set_parameter ("content-lines", c, &(p->parameter));
454 else if (!ascii_strcasecmp ("data-description", line + 6))
456 mutt_str_replace (&p->description, c);
457 rfc2047_decode (&p->description);
462 p->offset = ftello (fp); /* Mark the start of the real data */
463 if (p->type == TYPETEXT && !p->subtype)
464 p->subtype = safe_strdup ("plain");
465 else if (p->type == TYPEMESSAGE && !p->subtype)
466 p->subtype = safe_strdup ("rfc822");
473 void mutt_parse_part (FILE *fp, BODY *b)
480 #ifdef SUN_ATTACHMENT
481 if ( !ascii_strcasecmp (b->subtype, "x-sun-attachment") )
485 bound = mutt_get_parameter ("boundary", b->parameter);
487 fseeko (fp, b->offset, SEEK_SET);
488 b->parts = mutt_parse_multipart (fp, bound,
489 b->offset + b->length,
490 ascii_strcasecmp ("digest", b->subtype) == 0);
496 fseeko (fp, b->offset, SEEK_SET);
497 if (mutt_is_message_type(b->type, b->subtype))
498 b->parts = mutt_parse_messageRFC822 (fp, b);
499 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
500 b->parts = mutt_read_mime_header (fp, 0);
510 /* try to recover from parsing error */
514 mutt_str_replace (&b->subtype, "plain");
518 /* parse a MESSAGE/RFC822 body
521 * fp stream to read from
523 * parent structure which contains info about the message/rfc822
526 * NOTE: this assumes that `parent->length' has been set!
529 BODY *mutt_parse_messageRFC822 (FILE *fp, BODY *parent)
533 parent->hdr = mutt_new_header ();
534 parent->hdr->offset = ftello (fp);
535 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
536 msg = parent->hdr->content;
538 /* ignore the length given in the content-length since it could be wrong
539 and we already have the info to calculate the correct length */
540 /* if (msg->length == -1) */
541 msg->length = parent->length - (msg->offset - parent->offset);
543 /* if body of this message is empty, we can end up with a negative length */
547 mutt_parse_part(fp, msg);
551 /* parse a multipart structure
554 * fp stream to read from
556 * boundary body separator
558 * end_off length of the multipart body (used when the final
559 * boundary is missing to avoid reading too far)
561 * digest 1 if reading a multipart/digest, 0 otherwise
564 BODY *mutt_parse_multipart (FILE *fp, const char *boundary, LOFF_T end_off, int digest)
566 #ifdef SUN_ATTACHMENT
569 int blen, len, crlf = 0;
570 char buffer[LONG_STRING];
571 BODY *head = 0, *last = 0, *new = 0;
573 int final = 0; /* did we see the ending boundary? */
577 mutt_error _("multipart message has no boundary parameter!");
581 blen = mutt_strlen (boundary);
582 while (ftello (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL)
584 len = mutt_strlen (buffer);
586 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
588 if (buffer[0] == '-' && buffer[1] == '-' &&
589 mutt_strncmp (buffer + 2, boundary, blen) == 0)
593 last->length = ftello (fp) - last->offset - len - 1 - crlf;
594 if (last->parts && last->parts->length == 0)
595 last->parts->length = ftello (fp) - last->parts->offset - len - 1 - crlf;
596 /* if the body is empty, we can end up with a -1 length */
597 if (last->length < 0)
601 /* Remove any trailing whitespace, up to the length of the boundary */
602 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
605 /* Check for the end boundary */
606 if (mutt_strcmp (buffer + blen + 2, "--") == 0)
609 break; /* done parsing */
611 else if (buffer[2 + blen] == 0)
613 new = mutt_read_mime_header (fp, digest);
615 #ifdef SUN_ATTACHMENT
616 if (mutt_get_parameter ("content-lines", new->parameter)) {
617 for (lines = atoi(mutt_get_parameter ("content-lines", new->parameter));
619 if (ftello (fp) >= end_off || fgets (buffer, LONG_STRING, fp) == NULL)
625 * Consistency checking - catch
626 * bad attachment end boundaries
629 if(new->offset > end_off)
631 mutt_free_body(&new);
645 /* in case of missing end boundary, set the length to something reasonable */
646 if (last && last->length == 0 && !final)
647 last->length = end_off - last->offset;
649 /* parse recursive MIME parts */
650 for(last = head; last; last = last->next)
651 mutt_parse_part(fp, last);
656 static const char *uncomment_timezone (char *buf, size_t buflen, const char *tz)
662 return tz; /* no need to do anything */
665 if ((p = strpbrk (tz, " )")) == NULL)
668 if (len > buflen - 1)
670 memcpy (buf, tz, len);
678 unsigned char zhours;
679 unsigned char zminutes;
680 unsigned char zoccident; /* west of UTC? */
684 { "aat", 1, 0, 1 }, /* Atlantic Africa Time */
685 { "adt", 4, 0, 0 }, /* Arabia DST */
686 { "ast", 3, 0, 0 }, /* Arabia */
687 /*{ "ast", 4, 0, 1 },*/ /* Atlantic */
688 { "bst", 1, 0, 0 }, /* British DST */
689 { "cat", 1, 0, 0 }, /* Central Africa */
691 { "cest", 2, 0, 0 }, /* Central Europe DST */
692 { "cet", 1, 0, 0 }, /* Central Europe */
694 /*{ "cst", 8, 0, 0 },*/ /* China */
695 /*{ "cst", 9, 30, 0 },*/ /* Australian Central Standard Time */
696 { "eat", 3, 0, 0 }, /* East Africa */
698 { "eest", 3, 0, 0 }, /* Eastern Europe DST */
699 { "eet", 2, 0, 0 }, /* Eastern Europe */
700 { "egst", 0, 0, 0 }, /* Eastern Greenland DST */
701 { "egt", 1, 0, 1 }, /* Eastern Greenland */
704 { "gst", 4, 0, 0 }, /* Presian Gulf */
705 { "hkt", 8, 0, 0 }, /* Hong Kong */
706 { "ict", 7, 0, 0 }, /* Indochina */
707 { "idt", 3, 0, 0 }, /* Israel DST */
708 { "ist", 2, 0, 0 }, /* Israel */
709 /*{ "ist", 5, 30, 0 },*/ /* India */
710 { "jst", 9, 0, 0 }, /* Japan */
711 { "kst", 9, 0, 0 }, /* Korea */
713 { "met", 1, 0, 0 }, /* this is now officially CET */
714 { "msd", 4, 0, 0 }, /* Moscow DST */
715 { "msk", 3, 0, 0 }, /* Moscow */
717 { "nzdt", 13, 0, 0 }, /* New Zealand DST */
718 { "nzst", 12, 0, 0 }, /* New Zealand */
721 { "sat", 2, 0, 0 }, /* South Africa */
722 { "smt", 4, 0, 0 }, /* Seychelles */
723 { "sst", 11, 0, 1 }, /* Samoa */
724 /*{ "sst", 8, 0, 0 },*/ /* Singapore */
726 { "wat", 0, 0, 0 }, /* West Africa */
727 { "west", 1, 0, 0 }, /* Western Europe DST */
728 { "wet", 0, 0, 0 }, /* Western Europe */
729 { "wgst", 2, 0, 1 }, /* Western Greenland DST */
730 { "wgt", 3, 0, 1 }, /* Western Greenland */
731 { "wst", 8, 0, 0 }, /* Western Australia */
734 /* parses a date string in RFC822 format:
736 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
738 * This routine assumes that `h' has been initialized to 0. the `timezone'
739 * field is optional, defaulting to +0000 if missing.
741 time_t mutt_parse_date (const char *s, HEADER *h)
753 char tzstr[SHORT_STRING];
754 char scratch[SHORT_STRING];
756 /* Don't modify our argument. Fixed-size buffer is ok here since
757 * the date format imposes a natural limit.
760 strfcpy (scratch, s, sizeof (scratch));
762 /* kill the day of the week, if it exists. */
763 if ((t = strchr (scratch, ',')))
769 memset (&tm, 0, sizeof (tm));
771 while ((t = strtok (t, " \t")) != NULL)
775 case 0: /* day of the month */
776 if (!isdigit ((unsigned char) *t))
778 tm.tm_mday = atoi (t);
783 case 1: /* month of the year */
784 if ((i = mutt_check_month (t)) < 0)
790 tm.tm_year = atoi (t);
793 else if (tm.tm_year >= 1900)
797 case 3: /* time of day */
798 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3)
800 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
804 dprint(1, (debugfile, "parse_date: could not process time format: %s\n", t));
812 case 4: /* timezone */
813 /* sometimes we see things like (MST) or (-0700) so attempt to
814 * compensate by uncommenting the string if non-RFC822 compliant
816 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
818 if (*ptz == '+' || *ptz == '-')
820 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
821 && isdigit ((unsigned char) ptz[1]) && isdigit ((unsigned char) ptz[2])
822 && isdigit ((unsigned char) ptz[3]) && isdigit ((unsigned char) ptz[4]))
824 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
825 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
835 tz = bsearch (ptz, TimeZones, sizeof TimeZones/sizeof (struct tz_t),
836 sizeof (struct tz_t),
837 (int (*)(const void *, const void *)) ascii_strcasecmp
838 /* This is safe to do: A pointer to a struct equals
839 * a pointer to its first element*/);
844 zminutes = tz->zminutes;
845 zoccident = tz->zoccident;
848 /* ad hoc support for the European MET (now officially CET) TZ */
849 if (ascii_strcasecmp (t, "MET") == 0)
851 if ((t = strtok (NULL, " \t")) != NULL)
853 if (!ascii_strcasecmp (t, "DST"))
858 tz_offset = zhours * 3600 + zminutes * 60;
860 tz_offset = -tz_offset;
867 if (count < 4) /* don't check for missing timezone */
869 dprint(1,(debugfile, "parse_date(): error parsing date format, using received time\n"));
876 h->zminutes = zminutes;
877 h->zoccident = zoccident;
880 return (mutt_mktime (&tm, 0) + tz_offset);
883 /* extract the first substring that looks like a message-id.
884 * call back with NULL for more (like strtok).
886 char *mutt_extract_message_id (const char *s, const char **saveptr)
888 const char *o, *onull, *p;
898 for (s = NULL, o = NULL, onull = NULL;
899 (p = strpbrk (p, "<> \t;")) != NULL; ++p)
913 size_t olen = onull - o, slen = p - s + 1;
914 ret = safe_malloc (olen + slen + 1);
916 memcpy (ret, o, olen);
917 memcpy (ret + olen, s, slen);
918 ret[olen + slen] = '\0';
920 *saveptr = p + 1; /* next call starts after '>' */
924 /* some idiotic clients break their message-ids between lines */
926 /* step past another whitespace */
929 /* more than two lines, give up */
930 s = o = onull = NULL;
933 /* remember the first line, start looking for the second */
943 void mutt_parse_mime_message (CONTEXT *ctx, HEADER *cur)
948 if (cur->content->type != TYPEMESSAGE &&
949 cur->content->type != TYPEMULTIPART)
950 break; /* nothing to do */
952 if (cur->content->parts)
953 break; /* The message was parsed earlier. */
955 if ((msg = mx_open_message (ctx, cur->msgno)))
957 mutt_parse_part (msg->fp, cur->content);
960 cur->security = crypt_query (cur->content);
962 mx_close_message (&msg);
966 cur->attach_valid = 0;
969 int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short user_hdrs, short weed,
970 short do_2047, LIST **lastp)
978 switch (ascii_tolower (line[0]))
981 if (ascii_strcasecmp (line+1, "pparently-to") == 0)
983 e->to = rfc822_parse_adrlist (e->to, p);
986 else if (ascii_strcasecmp (line+1, "pparently-from") == 0)
988 e->from = rfc822_parse_adrlist (e->from, p);
994 if (ascii_strcasecmp (line+1, "cc") == 0)
996 e->bcc = rfc822_parse_adrlist (e->bcc, p);
1002 if (ascii_strcasecmp (line+1, "c") == 0)
1004 e->cc = rfc822_parse_adrlist (e->cc, p);
1007 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0)
1009 if (ascii_strcasecmp (line+8, "type") == 0)
1012 mutt_parse_content_type (p, hdr->content);
1015 else if (ascii_strcasecmp (line+8, "transfer-encoding") == 0)
1018 hdr->content->encoding = mutt_check_encoding (p);
1021 else if (ascii_strcasecmp (line+8, "length") == 0)
1025 if ((hdr->content->length = atoi (p)) < 0)
1026 hdr->content->length = -1;
1030 else if (ascii_strcasecmp (line+8, "description") == 0)
1034 mutt_str_replace (&hdr->content->description, p);
1035 rfc2047_decode (&hdr->content->description);
1039 else if (ascii_strcasecmp (line+8, "disposition") == 0)
1042 parse_content_disposition (p, hdr->content);
1049 if (!ascii_strcasecmp ("ate", line + 1))
1051 mutt_str_replace (&e->date, p);
1053 hdr->date_sent = mutt_parse_date (p, hdr);
1059 if (!ascii_strcasecmp ("xpires", line + 1) &&
1060 hdr && mutt_parse_date (p, NULL) < time (NULL))
1065 if (!ascii_strcasecmp ("rom", line + 1))
1067 e->from = rfc822_parse_adrlist (e->from, p);
1073 if (!ascii_strcasecmp (line+1, "n-reply-to"))
1075 mutt_free_list (&e->in_reply_to);
1076 e->in_reply_to = mutt_parse_references (p, 1);
1082 if (!ascii_strcasecmp (line + 1, "ines"))
1086 hdr->lines = atoi (p);
1089 * HACK - mutt has, for a very short time, produced negative
1090 * Lines header values. Ignore them.
1098 else if (!ascii_strcasecmp (line + 1, "ist-Post"))
1100 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1101 if (strncmp (p, "NO", 2))
1104 for (beg = strchr (p, '<'); beg; beg = strchr (end, ','))
1107 if (!(end = strchr (beg, '>')))
1110 /* Take the first mailto URL */
1111 if (url_check_scheme (beg) == U_MAILTO)
1113 FREE (&e->list_post);
1114 e->list_post = mutt_substrdup (beg, end);
1124 if (!ascii_strcasecmp (line + 1, "ime-version"))
1130 else if (!ascii_strcasecmp (line + 1, "essage-id"))
1132 /* We add a new "Message-ID:" when building a message */
1133 FREE (&e->message_id);
1134 e->message_id = mutt_extract_message_id (p, NULL);
1137 else if (!ascii_strncasecmp (line + 1, "ail-", 4))
1139 if (!ascii_strcasecmp (line + 5, "reply-to"))
1141 /* override the Reply-To: field */
1142 rfc822_free_address (&e->reply_to);
1143 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1146 else if (!ascii_strcasecmp (line + 5, "followup-to"))
1148 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1155 if (!ascii_strcasecmp (line + 1, "eferences"))
1157 mutt_free_list (&e->references);
1158 e->references = mutt_parse_references (p, 0);
1161 else if (!ascii_strcasecmp (line + 1, "eply-to"))
1163 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1166 else if (!ascii_strcasecmp (line + 1, "eturn-path"))
1168 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1171 else if (!ascii_strcasecmp (line + 1, "eceived"))
1173 if (hdr && !hdr->received)
1175 char *d = strchr (p, ';');
1178 hdr->received = mutt_parse_date (d + 1, NULL);
1184 if (!ascii_strcasecmp (line + 1, "ubject"))
1187 e->subject = safe_strdup (p);
1190 else if (!ascii_strcasecmp (line + 1, "ender"))
1192 e->sender = rfc822_parse_adrlist (e->sender, p);
1195 else if (!ascii_strcasecmp (line + 1, "tatus"))
1218 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1219 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1220 e->supersedes = safe_strdup (p);
1224 if (ascii_strcasecmp (line+1, "o") == 0)
1226 e->to = rfc822_parse_adrlist (e->to, p);
1232 if (ascii_strcasecmp (line+1, "-status") == 0)
1257 else if (ascii_strcasecmp (line+1, "-label") == 0)
1259 e->x_label = safe_strdup(p);
1267 /* Keep track of the user-defined headers */
1268 if (!matched && user_hdrs)
1270 /* restore the original line */
1271 line[strlen (line)] = ':';
1273 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1274 && !mutt_matches_ignore (line, UnIgnore))
1279 last->next = mutt_new_list ();
1283 last = e->userhdrs = mutt_new_list ();
1284 last->data = safe_strdup (line);
1286 rfc2047_decode (&last->data);
1296 /* mutt_read_rfc822_header() -- parses a RFC822 header
1300 * f stream to read from
1302 * hdr header structure of current message (optional).
1304 * user_hdrs If set, store user headers. Used for recall-message and
1307 * weed If this parameter is set and the user has activated the
1308 * $weed option, honor the header weed list for user headers.
1309 * Used for recall-message.
1311 * Returns: newly allocated envelope structure. You should free it by
1312 * mutt_free_envelope() when envelope stay unneeded.
1314 ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
1317 ENVELOPE *e = mutt_new_envelope();
1319 char *line = safe_malloc (LONG_STRING);
1323 size_t linelen = LONG_STRING;
1324 char buf[LONG_STRING+1];
1328 if (hdr->content == NULL)
1330 hdr->content = mutt_new_body ();
1332 /* set the defaults from RFC1521 */
1333 hdr->content->type = TYPETEXT;
1334 hdr->content->subtype = safe_strdup ("plain");
1335 hdr->content->encoding = ENC7BIT;
1336 hdr->content->length = -1;
1338 /* RFC 2183 says this is arbitrary */
1339 hdr->content->disposition = DISPINLINE;
1343 while ((loc = ftello (f)),
1344 *(line = mutt_read_rfc822_line (f, line, &linelen)) != 0)
1348 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':')
1350 char return_path[LONG_STRING];
1353 /* some bogus MTAs will quote the original "From " line */
1354 if (mutt_strncmp (">From ", line, 6) == 0)
1355 continue; /* just ignore */
1356 else if (is_from (line, return_path, sizeof (return_path), &t))
1358 /* MH somtimes has the From_ line in the middle of the header! */
1359 if (hdr && !hdr->received)
1360 hdr->received = t - mutt_local_tz (t);
1365 break; /* end of header */
1370 if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf)))
1372 if (!mutt_match_rx_list(line, NoSpamList))
1375 /* if spam tag already exists, figure out how to amend it */
1376 if (e->spam && *buf)
1378 /* If SpamSep defined, append with separator */
1381 mutt_buffer_addstr(e->spam, SpamSep);
1382 mutt_buffer_addstr(e->spam, buf);
1385 /* else overwrite */
1388 e->spam->dptr = e->spam->data;
1389 *e->spam->dptr = '\0';
1390 mutt_buffer_addstr(e->spam, buf);
1394 /* spam tag is new, and match expr is non-empty; copy */
1395 else if (!e->spam && *buf)
1397 e->spam = mutt_buffer_from(NULL, buf);
1400 /* match expr is empty; plug in null string if no existing tag */
1403 e->spam = mutt_buffer_from(NULL, "");
1406 if (e->spam && e->spam->data)
1407 dprint(5, (debugfile, "p822: spam = %s\n", e->spam->data));
1415 continue; /* skip empty header fields */
1417 matched = mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1425 hdr->content->hdr_offset = hdr->offset;
1426 hdr->content->offset = ftello (f);
1428 /* do RFC2047 decoding */
1429 rfc2047_decode_adrlist (e->from);
1430 rfc2047_decode_adrlist (e->to);
1431 rfc2047_decode_adrlist (e->cc);
1432 rfc2047_decode_adrlist (e->bcc);
1433 rfc2047_decode_adrlist (e->reply_to);
1434 rfc2047_decode_adrlist (e->mail_followup_to);
1435 rfc2047_decode_adrlist (e->return_path);
1436 rfc2047_decode_adrlist (e->sender);
1437 rfc2047_decode (&e->x_label);
1441 regmatch_t pmatch[1];
1443 rfc2047_decode (&e->subject);
1445 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1446 e->real_subj = e->subject + pmatch[0].rm_eo;
1448 e->real_subj = e->subject;
1451 /* check for missing or invalid date */
1452 if (hdr->date_sent <= 0)
1454 dprint(1,(debugfile,"read_rfc822_header(): no date found, using received time from msg separator\n"));
1455 hdr->date_sent = hdr->received;
1462 ADDRESS *mutt_parse_adrlist (ADDRESS *p, const char *s)
1466 /* check for a simple whitespace separated list of addresses */
1467 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL)
1469 char tmp[HUGE_STRING];
1472 strfcpy (tmp, s, sizeof (tmp));
1474 while ((r = strtok (r, " \t")) != NULL)
1476 p = rfc822_parse_adrlist (p, r);
1481 p = rfc822_parse_adrlist (p, s);
1486 /* Compares mime types to the ok and except lists */
1487 static int count_body_parts_check(LIST **checklist, BODY *b, int dflt)
1492 /* If list is null, use default behavior. */
1499 for (type = *checklist; type; type = type->next)
1501 a = (ATTACH_MATCH *)type->data;
1502 dprint(5, (debugfile, "cbpc: %s %d/%s ?? %s/%s [%d]... ",
1503 dflt ? "[OK] " : "[EXCL] ",
1504 b->type, b->subtype, a->major, a->minor, a->major_int));
1505 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1506 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0))
1508 dprint(5, (debugfile, "yes\n"));
1513 dprint(5, (debugfile, "no\n"));
1520 #define AT_COUNT(why) { shallcount = 1; }
1521 #define AT_NOCOUNT(why) { shallcount = 0; }
1523 static int count_body_parts (BODY *body, int flags)
1526 int shallcount, shallrecurse;
1532 for (bp = body; bp != NULL; bp = bp->next)
1534 /* Initial disposition is to count and not to recurse this part. */
1535 AT_COUNT("default");
1538 dprint(5, (debugfile, "bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1539 bp->description ? bp->description : ("none"),
1540 bp->filename ? bp->filename :
1541 bp->d_filename ? bp->d_filename : "(none)",
1542 bp->type, bp->subtype ? bp->subtype : "*"));
1544 if (bp->type == TYPEMESSAGE)
1548 /* If it's an external body pointer, don't recurse it. */
1549 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1552 /* Don't count containers if they're top-level. */
1553 if (flags & M_PARTS_TOPLEVEL)
1554 AT_NOCOUNT("top-level message/*");
1556 else if (bp->type == TYPEMULTIPART)
1558 /* Always recurse multiparts, except multipart/alternative. */
1560 if (!ascii_strcasecmp(bp->subtype, "alternative"))
1563 /* Don't count containers if they're top-level. */
1564 if (flags & M_PARTS_TOPLEVEL)
1565 AT_NOCOUNT("top-level multipart");
1568 if (bp->disposition == DISPINLINE &&
1569 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1570 AT_NOCOUNT("ignore fundamental inlines");
1572 /* If this body isn't scheduled for enumeration already, don't bother
1573 * profiling it further.
1577 /* Turn off shallcount if message type is not in ok list,
1578 * or if it is in except list. Check is done separately for
1579 * inlines vs. attachments.
1582 if (bp->disposition == DISPATTACH)
1584 if (!count_body_parts_check(&AttachAllow, bp, 1))
1585 AT_NOCOUNT("attach not allowed");
1586 if (count_body_parts_check(&AttachExclude, bp, 0))
1587 AT_NOCOUNT("attach excluded");
1591 if (!count_body_parts_check(&InlineAllow, bp, 1))
1592 AT_NOCOUNT("inline not allowed");
1593 if (count_body_parts_check(&InlineExclude, bp, 0))
1594 AT_NOCOUNT("excluded");
1600 bp->attach_qualifies = shallcount ? 1 : 0;
1602 dprint(5, (debugfile, "cbp: %p shallcount = %d\n", (void *)bp, shallcount));
1606 dprint(5, (debugfile, "cbp: %p pre count = %d\n", (void *)bp, count));
1607 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1608 count += bp->attach_count;
1609 dprint(5, (debugfile, "cbp: %p post count = %d\n", (void *)bp, count));
1613 dprint(5, (debugfile, "bp: return %d\n", count < 0 ? 0 : count));
1614 return count < 0 ? 0 : count;
1617 int mutt_count_body_parts (CONTEXT *ctx, HEADER *hdr)
1619 short keep_parts = 0;
1621 if (hdr->attach_valid)
1622 return hdr->attach_total;
1624 if (hdr->content->parts)
1627 mutt_parse_mime_message (ctx, hdr);
1629 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1630 hdr->attach_total = count_body_parts(hdr->content, M_PARTS_TOPLEVEL);
1632 hdr->attach_total = 0;
1634 hdr->attach_valid = 1;
1637 mutt_free_body (&hdr->content->parts);
1639 return hdr->attach_total;