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 mutt_atoi (mutt_get_parameter ("content-lines", new->parameter), &lines);
618 for ( ; lines; lines-- )
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 (mutt_atoi (t, &tm.tm_mday) < 0 || tm.tm_mday < 0)
782 case 1: /* month of the year */
783 if ((i = mutt_check_month (t)) < 0)
789 if (mutt_atoi (t, &tm.tm_year) < 0 || tm.tm_year < 0)
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 = atol (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"))
1087 * HACK - mutt has, for a very short time, produced negative
1088 * Lines header values. Ignore them.
1090 if (mutt_atoi (p, &hdr->lines) < 0 || hdr->lines < 0)
1096 else if (!ascii_strcasecmp (line + 1, "ist-Post"))
1098 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1099 if (strncmp (p, "NO", 2))
1102 for (beg = strchr (p, '<'); beg; beg = strchr (end, ','))
1105 if (!(end = strchr (beg, '>')))
1108 /* Take the first mailto URL */
1109 if (url_check_scheme (beg) == U_MAILTO)
1111 FREE (&e->list_post);
1112 e->list_post = mutt_substrdup (beg, end);
1122 if (!ascii_strcasecmp (line + 1, "ime-version"))
1128 else if (!ascii_strcasecmp (line + 1, "essage-id"))
1130 /* We add a new "Message-ID:" when building a message */
1131 FREE (&e->message_id);
1132 e->message_id = mutt_extract_message_id (p, NULL);
1135 else if (!ascii_strncasecmp (line + 1, "ail-", 4))
1137 if (!ascii_strcasecmp (line + 5, "reply-to"))
1139 /* override the Reply-To: field */
1140 rfc822_free_address (&e->reply_to);
1141 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1144 else if (!ascii_strcasecmp (line + 5, "followup-to"))
1146 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1153 if (!ascii_strcasecmp (line + 1, "eferences"))
1155 mutt_free_list (&e->references);
1156 e->references = mutt_parse_references (p, 0);
1159 else if (!ascii_strcasecmp (line + 1, "eply-to"))
1161 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1164 else if (!ascii_strcasecmp (line + 1, "eturn-path"))
1166 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1169 else if (!ascii_strcasecmp (line + 1, "eceived"))
1171 if (hdr && !hdr->received)
1173 char *d = strchr (p, ';');
1176 hdr->received = mutt_parse_date (d + 1, NULL);
1182 if (!ascii_strcasecmp (line + 1, "ubject"))
1185 e->subject = safe_strdup (p);
1188 else if (!ascii_strcasecmp (line + 1, "ender"))
1190 e->sender = rfc822_parse_adrlist (e->sender, p);
1193 else if (!ascii_strcasecmp (line + 1, "tatus"))
1216 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1217 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1218 e->supersedes = safe_strdup (p);
1222 if (ascii_strcasecmp (line+1, "o") == 0)
1224 e->to = rfc822_parse_adrlist (e->to, p);
1230 if (ascii_strcasecmp (line+1, "-status") == 0)
1255 else if (ascii_strcasecmp (line+1, "-label") == 0)
1257 e->x_label = safe_strdup(p);
1265 /* Keep track of the user-defined headers */
1266 if (!matched && user_hdrs)
1268 /* restore the original line */
1269 line[strlen (line)] = ':';
1271 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1272 && !mutt_matches_ignore (line, UnIgnore))
1277 last->next = mutt_new_list ();
1281 last = e->userhdrs = mutt_new_list ();
1282 last->data = safe_strdup (line);
1284 rfc2047_decode (&last->data);
1294 /* mutt_read_rfc822_header() -- parses a RFC822 header
1298 * f stream to read from
1300 * hdr header structure of current message (optional).
1302 * user_hdrs If set, store user headers. Used for recall-message and
1305 * weed If this parameter is set and the user has activated the
1306 * $weed option, honor the header weed list for user headers.
1307 * Used for recall-message.
1309 * Returns: newly allocated envelope structure. You should free it by
1310 * mutt_free_envelope() when envelope stay unneeded.
1312 ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
1315 ENVELOPE *e = mutt_new_envelope();
1317 char *line = safe_malloc (LONG_STRING);
1321 size_t linelen = LONG_STRING;
1322 char buf[LONG_STRING+1];
1326 if (hdr->content == NULL)
1328 hdr->content = mutt_new_body ();
1330 /* set the defaults from RFC1521 */
1331 hdr->content->type = TYPETEXT;
1332 hdr->content->subtype = safe_strdup ("plain");
1333 hdr->content->encoding = ENC7BIT;
1334 hdr->content->length = -1;
1336 /* RFC 2183 says this is arbitrary */
1337 hdr->content->disposition = DISPINLINE;
1341 while ((loc = ftello (f)),
1342 *(line = mutt_read_rfc822_line (f, line, &linelen)) != 0)
1346 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':')
1348 char return_path[LONG_STRING];
1351 /* some bogus MTAs will quote the original "From " line */
1352 if (mutt_strncmp (">From ", line, 6) == 0)
1353 continue; /* just ignore */
1354 else if (is_from (line, return_path, sizeof (return_path), &t))
1356 /* MH somtimes has the From_ line in the middle of the header! */
1357 if (hdr && !hdr->received)
1358 hdr->received = t - mutt_local_tz (t);
1363 break; /* end of header */
1368 if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf)))
1370 if (!mutt_match_rx_list(line, NoSpamList))
1373 /* if spam tag already exists, figure out how to amend it */
1374 if (e->spam && *buf)
1376 /* If SpamSep defined, append with separator */
1379 mutt_buffer_addstr(e->spam, SpamSep);
1380 mutt_buffer_addstr(e->spam, buf);
1383 /* else overwrite */
1386 e->spam->dptr = e->spam->data;
1387 *e->spam->dptr = '\0';
1388 mutt_buffer_addstr(e->spam, buf);
1392 /* spam tag is new, and match expr is non-empty; copy */
1393 else if (!e->spam && *buf)
1395 e->spam = mutt_buffer_from(NULL, buf);
1398 /* match expr is empty; plug in null string if no existing tag */
1401 e->spam = mutt_buffer_from(NULL, "");
1404 if (e->spam && e->spam->data)
1405 dprint(5, (debugfile, "p822: spam = %s\n", e->spam->data));
1413 continue; /* skip empty header fields */
1415 matched = mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1423 hdr->content->hdr_offset = hdr->offset;
1424 hdr->content->offset = ftello (f);
1426 /* do RFC2047 decoding */
1427 rfc2047_decode_adrlist (e->from);
1428 rfc2047_decode_adrlist (e->to);
1429 rfc2047_decode_adrlist (e->cc);
1430 rfc2047_decode_adrlist (e->bcc);
1431 rfc2047_decode_adrlist (e->reply_to);
1432 rfc2047_decode_adrlist (e->mail_followup_to);
1433 rfc2047_decode_adrlist (e->return_path);
1434 rfc2047_decode_adrlist (e->sender);
1435 rfc2047_decode (&e->x_label);
1439 regmatch_t pmatch[1];
1441 rfc2047_decode (&e->subject);
1443 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1444 e->real_subj = e->subject + pmatch[0].rm_eo;
1446 e->real_subj = e->subject;
1449 /* check for missing or invalid date */
1450 if (hdr->date_sent <= 0)
1452 dprint(1,(debugfile,"read_rfc822_header(): no date found, using received time from msg separator\n"));
1453 hdr->date_sent = hdr->received;
1460 ADDRESS *mutt_parse_adrlist (ADDRESS *p, const char *s)
1464 /* check for a simple whitespace separated list of addresses */
1465 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL)
1467 char tmp[HUGE_STRING];
1470 strfcpy (tmp, s, sizeof (tmp));
1472 while ((r = strtok (r, " \t")) != NULL)
1474 p = rfc822_parse_adrlist (p, r);
1479 p = rfc822_parse_adrlist (p, s);
1484 /* Compares mime types to the ok and except lists */
1485 static int count_body_parts_check(LIST **checklist, BODY *b, int dflt)
1490 /* If list is null, use default behavior. */
1497 for (type = *checklist; type; type = type->next)
1499 a = (ATTACH_MATCH *)type->data;
1500 dprint(5, (debugfile, "cbpc: %s %d/%s ?? %s/%s [%d]... ",
1501 dflt ? "[OK] " : "[EXCL] ",
1502 b->type, b->subtype, a->major, a->minor, a->major_int));
1503 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1504 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0))
1506 dprint(5, (debugfile, "yes\n"));
1511 dprint(5, (debugfile, "no\n"));
1518 #define AT_COUNT(why) { shallcount = 1; }
1519 #define AT_NOCOUNT(why) { shallcount = 0; }
1521 static int count_body_parts (BODY *body, int flags)
1524 int shallcount, shallrecurse;
1530 for (bp = body; bp != NULL; bp = bp->next)
1532 /* Initial disposition is to count and not to recurse this part. */
1533 AT_COUNT("default");
1536 dprint(5, (debugfile, "bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1537 bp->description ? bp->description : ("none"),
1538 bp->filename ? bp->filename :
1539 bp->d_filename ? bp->d_filename : "(none)",
1540 bp->type, bp->subtype ? bp->subtype : "*"));
1542 if (bp->type == TYPEMESSAGE)
1546 /* If it's an external body pointer, don't recurse it. */
1547 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1550 /* Don't count containers if they're top-level. */
1551 if (flags & M_PARTS_TOPLEVEL)
1552 AT_NOCOUNT("top-level message/*");
1554 else if (bp->type == TYPEMULTIPART)
1556 /* Always recurse multiparts, except multipart/alternative. */
1558 if (!ascii_strcasecmp(bp->subtype, "alternative"))
1561 /* Don't count containers if they're top-level. */
1562 if (flags & M_PARTS_TOPLEVEL)
1563 AT_NOCOUNT("top-level multipart");
1566 if (bp->disposition == DISPINLINE &&
1567 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1568 AT_NOCOUNT("ignore fundamental inlines");
1570 /* If this body isn't scheduled for enumeration already, don't bother
1571 * profiling it further.
1575 /* Turn off shallcount if message type is not in ok list,
1576 * or if it is in except list. Check is done separately for
1577 * inlines vs. attachments.
1580 if (bp->disposition == DISPATTACH)
1582 if (!count_body_parts_check(&AttachAllow, bp, 1))
1583 AT_NOCOUNT("attach not allowed");
1584 if (count_body_parts_check(&AttachExclude, bp, 0))
1585 AT_NOCOUNT("attach excluded");
1589 if (!count_body_parts_check(&InlineAllow, bp, 1))
1590 AT_NOCOUNT("inline not allowed");
1591 if (count_body_parts_check(&InlineExclude, bp, 0))
1592 AT_NOCOUNT("excluded");
1598 bp->attach_qualifies = shallcount ? 1 : 0;
1600 dprint(5, (debugfile, "cbp: %p shallcount = %d\n", (void *)bp, shallcount));
1604 dprint(5, (debugfile, "cbp: %p pre count = %d\n", (void *)bp, count));
1605 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1606 count += bp->attach_count;
1607 dprint(5, (debugfile, "cbp: %p post count = %d\n", (void *)bp, count));
1611 dprint(5, (debugfile, "bp: return %d\n", count < 0 ? 0 : count));
1612 return count < 0 ? 0 : count;
1615 int mutt_count_body_parts (CONTEXT *ctx, HEADER *hdr)
1617 short keep_parts = 0;
1619 if (hdr->attach_valid)
1620 return hdr->attach_total;
1622 if (hdr->content->parts)
1625 mutt_parse_mime_message (ctx, hdr);
1627 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1628 hdr->attach_total = count_body_parts(hdr->content, M_PARTS_TOPLEVEL);
1630 hdr->attach_total = 0;
1632 hdr->attach_valid = 1;
1635 mutt_free_body (&hdr->content->parts);
1637 return hdr->attach_total;