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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 static char *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 LIST *mutt_parse_references (char *s, int in_reply_to)
96 char *o = NULL, *new, *at;
98 while ((s = strtok (s, " \t;")) != NULL)
101 * some mail clients add other garbage besides message-ids, so do a quick
102 * check to make sure this looks like a valid message-id
103 * some idiotic clients also break their message-ids between lines, deal
104 * with that too (give up if it's more than two lines, though)
119 new = safe_strdup (s);
126 new = safe_malloc (sizeof (char) * (n + m + 1));
127 strcpy (new, o); /* __STRCPY_CHECKED__ */
128 strcpy (new + n, s); /* __STRCPY_CHECKED__ */
133 /* make sure that this really does look like a message-id.
134 * it should have exactly one @, and if we're looking at
135 * an in-reply-to header, make sure that the part before
136 * the @ has more than eight characters or it's probably
139 if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
140 || (in_reply_to && at - new <= 8))
144 t = (LIST *) safe_malloc (sizeof (LIST));
157 int mutt_check_encoding (const char *c)
159 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit")-1) == 0)
161 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit")-1) == 0)
163 else if (ascii_strncasecmp ("binary", c, sizeof ("binary")-1) == 0)
165 else if (ascii_strncasecmp ("quoted-printable", c, sizeof ("quoted-printable")-1) == 0)
166 return (ENCQUOTEDPRINTABLE);
167 else if (ascii_strncasecmp ("base64", c, sizeof("base64")-1) == 0)
169 else if (ascii_strncasecmp ("x-uuencode", c, sizeof("x-uuencode")-1) == 0)
170 return (ENCUUENCODED);
171 #ifdef SUN_ATTACHMENT
172 else if (ascii_strncasecmp ("uuencode", c, sizeof("uuencode")-1) == 0)
173 return (ENCUUENCODED);
179 static PARAMETER *parse_parameters (const char *s)
181 PARAMETER *head = 0, *cur = 0, *new;
182 char buffer[LONG_STRING];
186 dprint (2, (debugfile, "parse_parameters: `%s'\n", s));
190 if ((p = strpbrk (s, "=;")) == NULL)
192 dprint(1, (debugfile, "parse_parameters: malformed parameter: %s\n", s));
196 /* if we hit a ; now the parameter has no value, just skip it */
201 new = mutt_new_parameter ();
203 new->attribute = safe_malloc (i + 1);
204 memcpy (new->attribute, s, i);
205 new->attribute[i] = 0;
207 /* remove whitespace from the end of the attribute name */
208 while (ISSPACE (new->attribute[--i]))
209 new->attribute[i] = 0;
211 s = p + 1; /* skip over the = */
218 for (i=0; *s && i < sizeof (buffer) - 1; i++, s++)
220 if (!option (OPTSTRICTMIME)) {
221 /* As iso-2022-* has a characer of '"' with non-ascii state,
223 if (*s == 0x1b && i < sizeof (buffer) - 2)
225 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
231 if (state_ascii && *s == '"')
235 /* Quote the next character */
245 s++; /* skip over the " */
249 for (i=0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1; i++, s++)
254 new->value = safe_strdup (buffer);
256 dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n",
257 new->attribute ? new->attribute : "",
258 new->value ? new->value : ""));
260 /* Add this parameter to the list */
271 dprint (1, (debugfile, "parse_parameters(): parameter with no value: %s\n", s));
275 /* Find the next parameter */
276 if (*s != ';' && (s = strchr (s, ';')) == NULL)
277 break; /* no more parameters */
283 /* Move past any leading whitespace */
286 while (*s == ';'); /* skip empty parameters */
291 rfc2231_decode_parameters (&head);
295 int mutt_check_mime_type (const char *s)
297 if (ascii_strcasecmp ("text", s) == 0)
299 else if (ascii_strcasecmp ("multipart", s) == 0)
300 return TYPEMULTIPART;
301 #ifdef SUN_ATTACHMENT
302 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
303 return TYPEMULTIPART;
305 else if (ascii_strcasecmp ("application", s) == 0)
306 return TYPEAPPLICATION;
307 else if (ascii_strcasecmp ("message", s) == 0)
309 else if (ascii_strcasecmp ("image", s) == 0)
311 else if (ascii_strcasecmp ("audio", s) == 0)
313 else if (ascii_strcasecmp ("video", s) == 0)
315 else if (ascii_strcasecmp ("model", s) == 0)
321 void mutt_parse_content_type (char *s, BODY *ct)
327 mutt_free_parameter(&ct->parameter);
329 /* First extract any existing parameters */
330 if ((pc = strchr(s, ';')) != NULL)
333 while (*pc && ISSPACE (*pc))
335 ct->parameter = parse_parameters(pc);
337 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
338 * but if a filename has already been set in the content-disposition,
339 * let that take precedence, and don't set it here */
340 if ((pc = mutt_get_parameter( "name", ct->parameter)) != 0 && !ct->filename)
341 ct->filename = safe_strdup(pc);
343 #ifdef SUN_ATTACHMENT
344 /* this is deep and utter perversion */
345 if ((pc = mutt_get_parameter ("conversions", ct->parameter)) != 0)
346 ct->encoding = mutt_check_encoding (pc);
351 /* Now get the subtype */
352 if ((subtype = strchr(s, '/')))
355 for(pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++)
358 ct->subtype = safe_strdup (subtype);
361 /* Finally, get the major type */
362 ct->type = mutt_check_mime_type (s);
364 #ifdef SUN_ATTACHMENT
365 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
366 ct->subtype = safe_strdup ("x-sun-attachment");
369 if (ct->type == TYPEOTHER)
371 ct->xtype = safe_strdup (s);
374 if (ct->subtype == NULL)
376 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
377 * field, so we can attempt to convert the type to BODY here.
379 if (ct->type == TYPETEXT)
380 ct->subtype = safe_strdup ("plain");
381 else if (ct->type == TYPEAUDIO)
382 ct->subtype = safe_strdup ("basic");
383 else if (ct->type == TYPEMESSAGE)
384 ct->subtype = safe_strdup ("rfc822");
385 else if (ct->type == TYPEOTHER)
387 char buffer[SHORT_STRING];
389 ct->type = TYPEAPPLICATION;
390 snprintf (buffer, sizeof (buffer), "x-%s", s);
391 ct->subtype = safe_strdup (buffer);
394 ct->subtype = safe_strdup ("x-unknown");
397 /* Default character set for text types. */
398 if (ct->type == TYPETEXT)
400 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
401 mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
402 (const char *) mutt_get_first_charset (AssumedCharset),
408 static void parse_content_disposition (char *s, BODY *ct)
412 if (!ascii_strncasecmp ("inline", s, 6))
413 ct->disposition = DISPINLINE;
414 else if (!ascii_strncasecmp ("form-data", s, 9))
415 ct->disposition = DISPFORMDATA;
417 ct->disposition = DISPATTACH;
419 /* Check to see if a default filename was given */
420 if ((s = strchr (s, ';')) != NULL)
424 if ((s = mutt_get_parameter ("filename", (parms = parse_parameters (s)))) != 0)
425 mutt_str_replace (&ct->filename, s);
426 if ((s = mutt_get_parameter ("name", parms)) != 0)
427 ct->form_name = safe_strdup (s);
428 mutt_free_parameter (&parms);
433 * fp stream to read from
435 * digest 1 if reading subparts of a multipart/digest, 0
439 BODY *mutt_read_mime_header (FILE *fp, int digest)
441 BODY *p = mutt_new_body();
443 char *line = safe_malloc (LONG_STRING);
444 size_t linelen = LONG_STRING;
446 p->hdr_offset = ftell(fp);
448 p->encoding = ENC7BIT; /* default from RFC1521 */
449 p->type = digest ? TYPEMESSAGE : TYPETEXT;
450 p->disposition = DISPINLINE;
452 while (*(line = read_rfc822_line (fp, line, &linelen)) != 0)
454 /* Find the value of the current header */
455 if ((c = strchr (line, ':')))
462 dprint (1, (debugfile, "mutt_read_mime_header(): skipping empty header field: %s\n", line));
468 dprint (1, (debugfile, "read_mime_header: bogus MIME header: %s\n", line));
472 if (!ascii_strncasecmp ("content-", line, 8))
474 if (!ascii_strcasecmp ("type", line + 8))
475 mutt_parse_content_type (c, p);
476 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
477 p->encoding = mutt_check_encoding (c);
478 else if (!ascii_strcasecmp ("disposition", line + 8))
479 parse_content_disposition (c, p);
480 else if (!ascii_strcasecmp ("description", line + 8))
482 mutt_str_replace (&p->description, c);
483 rfc2047_decode (&p->description);
486 #ifdef SUN_ATTACHMENT
487 else if (!ascii_strncasecmp ("x-sun-", line, 6))
489 if (!ascii_strcasecmp ("data-type", line + 6))
490 mutt_parse_content_type (c, p);
491 else if (!ascii_strcasecmp ("encoding-info", line + 6))
492 p->encoding = mutt_check_encoding (c);
493 else if (!ascii_strcasecmp ("content-lines", line + 6))
494 mutt_set_parameter ("content-lines", c, &(p->parameter));
495 else if (!ascii_strcasecmp ("data-description", line + 6))
497 mutt_str_replace (&p->description, c);
498 rfc2047_decode (&p->description);
503 p->offset = ftell (fp); /* Mark the start of the real data */
504 if (p->type == TYPETEXT && !p->subtype)
505 p->subtype = safe_strdup ("plain");
506 else if (p->type == TYPEMESSAGE && !p->subtype)
507 p->subtype = safe_strdup ("rfc822");
514 void mutt_parse_part (FILE *fp, BODY *b)
521 #ifdef SUN_ATTACHMENT
522 if ( !ascii_strcasecmp (b->subtype, "x-sun-attachment") )
526 bound = mutt_get_parameter ("boundary", b->parameter);
528 fseek (fp, b->offset, SEEK_SET);
529 b->parts = mutt_parse_multipart (fp, bound,
530 b->offset + b->length,
531 ascii_strcasecmp ("digest", b->subtype) == 0);
537 fseek (fp, b->offset, SEEK_SET);
538 if (mutt_is_message_type(b->type, b->subtype))
539 b->parts = mutt_parse_messageRFC822 (fp, b);
540 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
541 b->parts = mutt_read_mime_header (fp, 0);
551 /* try to recover from parsing error */
555 mutt_str_replace (&b->subtype, "plain");
559 /* parse a MESSAGE/RFC822 body
562 * fp stream to read from
564 * parent structure which contains info about the message/rfc822
567 * NOTE: this assumes that `parent->length' has been set!
570 BODY *mutt_parse_messageRFC822 (FILE *fp, BODY *parent)
574 parent->hdr = mutt_new_header ();
575 parent->hdr->offset = ftell (fp);
576 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
577 msg = parent->hdr->content;
579 /* ignore the length given in the content-length since it could be wrong
580 and we already have the info to calculate the correct length */
581 /* if (msg->length == -1) */
582 msg->length = parent->length - (msg->offset - parent->offset);
584 /* if body of this message is empty, we can end up with a negative length */
588 mutt_parse_part(fp, msg);
592 /* parse a multipart structure
595 * fp stream to read from
597 * boundary body separator
599 * end_off length of the multipart body (used when the final
600 * boundary is missing to avoid reading too far)
602 * digest 1 if reading a multipart/digest, 0 otherwise
605 BODY *mutt_parse_multipart (FILE *fp, const char *boundary, long end_off, int digest)
607 #ifdef SUN_ATTACHMENT
610 int blen, len, crlf = 0;
611 char buffer[LONG_STRING];
612 BODY *head = 0, *last = 0, *new = 0;
614 int final = 0; /* did we see the ending boundary? */
618 mutt_error _("multipart message has no boundary parameter!");
622 blen = mutt_strlen (boundary);
623 while (ftell (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL)
625 len = mutt_strlen (buffer);
627 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
629 if (buffer[0] == '-' && buffer[1] == '-' &&
630 mutt_strncmp (buffer + 2, boundary, blen) == 0)
634 last->length = ftell (fp) - last->offset - len - 1 - crlf;
635 if (last->parts && last->parts->length == 0)
636 last->parts->length = ftell (fp) - last->parts->offset - len - 1 - crlf;
637 /* if the body is empty, we can end up with a -1 length */
638 if (last->length < 0)
642 /* Remove any trailing whitespace, up to the length of the boundary */
643 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
646 /* Check for the end boundary */
647 if (mutt_strcmp (buffer + blen + 2, "--") == 0)
650 break; /* done parsing */
652 else if (buffer[2 + blen] == 0)
654 new = mutt_read_mime_header (fp, digest);
656 #ifdef SUN_ATTACHMENT
657 if (mutt_get_parameter ("content-lines", new->parameter)) {
658 for (lines = atoi(mutt_get_parameter ("content-lines", new->parameter));
660 if (ftell (fp) >= end_off || fgets (buffer, LONG_STRING, fp) == NULL)
666 * Consistency checking - catch
667 * bad attachment end boundaries
670 if(new->offset > end_off)
672 mutt_free_body(&new);
686 /* in case of missing end boundary, set the length to something reasonable */
687 if (last && last->length == 0 && !final)
688 last->length = end_off - last->offset;
690 /* parse recursive MIME parts */
691 for(last = head; last; last = last->next)
692 mutt_parse_part(fp, last);
697 static const char *uncomment_timezone (char *buf, size_t buflen, const char *tz)
703 return tz; /* no need to do anything */
706 if ((p = strpbrk (tz, " )")) == NULL)
709 if (len > buflen - 1)
711 memcpy (buf, tz, len);
719 unsigned char zhours;
720 unsigned char zminutes;
721 unsigned char zoccident; /* west of UTC? */
725 { "aat", 1, 0, 1 }, /* Atlantic Africa Time */
726 { "adt", 4, 0, 0 }, /* Arabia DST */
727 { "ast", 3, 0, 0 }, /* Arabia */
728 /*{ "ast", 4, 0, 1 },*/ /* Atlantic */
729 { "bst", 1, 0, 0 }, /* British DST */
730 { "cat", 1, 0, 0 }, /* Central Africa */
732 { "cest", 2, 0, 0 }, /* Central Europe DST */
733 { "cet", 1, 0, 0 }, /* Central Europe */
735 /*{ "cst", 8, 0, 0 },*/ /* China */
736 /*{ "cst", 9, 30, 0 },*/ /* Australian Central Standard Time */
737 { "eat", 3, 0, 0 }, /* East Africa */
739 { "eest", 3, 0, 0 }, /* Eastern Europe DST */
740 { "eet", 2, 0, 0 }, /* Eastern Europe */
741 { "egst", 0, 0, 0 }, /* Eastern Greenland DST */
742 { "egt", 1, 0, 1 }, /* Eastern Greenland */
745 { "gst", 4, 0, 0 }, /* Presian Gulf */
746 { "hkt", 8, 0, 0 }, /* Hong Kong */
747 { "ict", 7, 0, 0 }, /* Indochina */
748 { "idt", 3, 0, 0 }, /* Israel DST */
749 { "ist", 2, 0, 0 }, /* Israel */
750 /*{ "ist", 5, 30, 0 },*/ /* India */
751 { "jst", 9, 0, 0 }, /* Japan */
752 { "kst", 9, 0, 0 }, /* Korea */
754 { "met", 1, 0, 0 }, /* this is now officially CET */
755 { "msd", 4, 0, 0 }, /* Moscow DST */
756 { "msk", 3, 0, 0 }, /* Moscow */
758 { "nzdt", 13, 0, 0 }, /* New Zealand DST */
759 { "nzst", 12, 0, 0 }, /* New Zealand */
762 { "sat", 2, 0, 0 }, /* South Africa */
763 { "smt", 4, 0, 0 }, /* Seychelles */
764 { "sst", 11, 0, 1 }, /* Samoa */
765 /*{ "sst", 8, 0, 0 },*/ /* Singapore */
767 { "wat", 0, 0, 0 }, /* West Africa */
768 { "west", 1, 0, 0 }, /* Western Europe DST */
769 { "wet", 0, 0, 0 }, /* Western Europe */
770 { "wgst", 2, 0, 1 }, /* Western Greenland DST */
771 { "wgt", 3, 0, 1 }, /* Western Greenland */
772 { "wst", 8, 0, 0 }, /* Western Australia */
775 /* parses a date string in RFC822 format:
777 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
779 * This routine assumes that `h' has been initialized to 0. the `timezone'
780 * field is optional, defaulting to +0000 if missing.
782 time_t mutt_parse_date (const char *s, HEADER *h)
794 char tzstr[SHORT_STRING];
795 char scratch[SHORT_STRING];
797 /* Don't modify our argument. Fixed-size buffer is ok here since
798 * the date format imposes a natural limit.
801 strfcpy (scratch, s, sizeof (scratch));
803 /* kill the day of the week, if it exists. */
804 if ((t = strchr (scratch, ',')))
810 memset (&tm, 0, sizeof (tm));
812 while ((t = strtok (t, " \t")) != NULL)
816 case 0: /* day of the month */
817 if (!isdigit ((unsigned char) *t))
819 tm.tm_mday = atoi (t);
824 case 1: /* month of the year */
825 if ((i = mutt_check_month (t)) < 0)
831 tm.tm_year = atoi (t);
834 else if (tm.tm_year >= 1900)
838 case 3: /* time of day */
839 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3)
841 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
845 dprint(1, (debugfile, "parse_date: could not process time format: %s\n", t));
853 case 4: /* timezone */
854 /* sometimes we see things like (MST) or (-0700) so attempt to
855 * compensate by uncommenting the string if non-RFC822 compliant
857 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
859 if (*ptz == '+' || *ptz == '-')
861 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
862 && isdigit ((unsigned char) ptz[1]) && isdigit ((unsigned char) ptz[2])
863 && isdigit ((unsigned char) ptz[3]) && isdigit ((unsigned char) ptz[4]))
865 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
866 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
876 tz = bsearch (ptz, TimeZones, sizeof TimeZones/sizeof (struct tz_t),
877 sizeof (struct tz_t),
878 (int (*)(const void *, const void *)) ascii_strcasecmp
879 /* This is safe to do: A pointer to a struct equals
880 * a pointer to its first element*/);
885 zminutes = tz->zminutes;
886 zoccident = tz->zoccident;
889 /* ad hoc support for the European MET (now officially CET) TZ */
890 if (ascii_strcasecmp (t, "MET") == 0)
892 if ((t = strtok (NULL, " \t")) != NULL)
894 if (!ascii_strcasecmp (t, "DST"))
899 tz_offset = zhours * 3600 + zminutes * 60;
901 tz_offset = -tz_offset;
908 if (count < 4) /* don't check for missing timezone */
910 dprint(1,(debugfile, "parse_date(): error parsing date format, using received time\n"));
917 h->zminutes = zminutes;
918 h->zoccident = zoccident;
921 return (mutt_mktime (&tm, 0) + tz_offset);
924 /* extract the first substring that looks like a message-id */
925 static char *extract_message_id (const char *s)
931 if ((s = strchr (s, '<')) == NULL || (p = strchr (s, '>')) == NULL)
933 l = (size_t)(p - s) + 1;
934 r = safe_malloc (l + 1);
940 void mutt_parse_mime_message (CONTEXT *ctx, HEADER *cur)
944 if (cur->content->type != TYPEMESSAGE && cur->content->type != TYPEMULTIPART)
945 return; /* nothing to do */
947 if (cur->content->parts)
948 return; /* The message was parsed earlier. */
950 if ((msg = mx_open_message (ctx, cur->msgno)))
952 mutt_parse_part (msg->fp, cur->content);
955 cur->security = crypt_query (cur->content);
957 mx_close_message (&msg);
961 int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short user_hdrs, short weed,
962 short do_2047, LIST **lastp)
970 switch (ascii_tolower (line[0]))
973 if (ascii_strcasecmp (line+1, "pparently-to") == 0)
975 e->to = rfc822_parse_adrlist (e->to, p);
978 else if (ascii_strcasecmp (line+1, "pparently-from") == 0)
980 e->from = rfc822_parse_adrlist (e->from, p);
986 if (ascii_strcasecmp (line+1, "cc") == 0)
988 e->bcc = rfc822_parse_adrlist (e->bcc, p);
994 if (ascii_strcasecmp (line+1, "c") == 0)
996 e->cc = rfc822_parse_adrlist (e->cc, p);
999 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0)
1001 if (ascii_strcasecmp (line+8, "type") == 0)
1004 mutt_parse_content_type (p, hdr->content);
1007 else if (ascii_strcasecmp (line+8, "transfer-encoding") == 0)
1010 hdr->content->encoding = mutt_check_encoding (p);
1013 else if (ascii_strcasecmp (line+8, "length") == 0)
1017 if ((hdr->content->length = atoi (p)) < 0)
1018 hdr->content->length = -1;
1022 else if (ascii_strcasecmp (line+8, "description") == 0)
1026 mutt_str_replace (&hdr->content->description, p);
1027 rfc2047_decode (&hdr->content->description);
1031 else if (ascii_strcasecmp (line+8, "disposition") == 0)
1034 parse_content_disposition (p, hdr->content);
1041 if (!ascii_strcasecmp ("ate", line + 1))
1043 mutt_str_replace (&e->date, p);
1045 hdr->date_sent = mutt_parse_date (p, hdr);
1051 if (!ascii_strcasecmp ("xpires", line + 1) &&
1052 hdr && mutt_parse_date (p, NULL) < time (NULL))
1057 if (!ascii_strcasecmp ("rom", line + 1))
1059 e->from = rfc822_parse_adrlist (e->from, p);
1060 /* don't leave from info NULL if there's an invalid address (or
1061 * whatever) in From: field; mutt would just display it as empty
1062 * and mark mail/(esp.) news article as your own. aaargh! this
1063 * bothered me for _years_ */
1066 e->from = rfc822_new_address ();
1067 e->from->personal = safe_strdup (line+6);
1072 else if (!mutt_strcasecmp (line+1, "ollowup-to"))
1074 if (!e->followup_to)
1076 mutt_remove_trailing_ws (p);
1077 e->followup_to = safe_strdup (mutt_skip_whitespace (p));
1085 if (!ascii_strcasecmp (line+1, "n-reply-to"))
1087 mutt_free_list (&e->in_reply_to);
1088 e->in_reply_to = mutt_parse_references (p, 1);
1094 if (!ascii_strcasecmp (line + 1, "ines"))
1098 hdr->lines = atoi (p);
1101 * HACK - mutt has, for a very short time, produced negative
1102 * Lines header values. Ignore them.
1110 else if (!ascii_strcasecmp (line + 1, "ist-Post"))
1112 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1113 if (strncmp (p, "NO", 2))
1116 for (beg = strchr (p, '<'); beg; beg = strchr (end, ','))
1119 if (!(end = strchr (beg, '>')))
1122 /* Take the first mailto URL */
1123 if (url_check_scheme (beg) == U_MAILTO)
1125 FREE (&e->list_post);
1126 e->list_post = mutt_substrdup (beg, end);
1136 if (!ascii_strcasecmp (line + 1, "ime-version"))
1142 else if (!ascii_strcasecmp (line + 1, "essage-id"))
1144 /* We add a new "Message-Id:" when building a message */
1145 FREE (&e->message_id);
1146 e->message_id = extract_message_id (p);
1149 else if (!ascii_strncasecmp (line + 1, "ail-", 4))
1151 if (!ascii_strcasecmp (line + 5, "reply-to"))
1153 /* override the Reply-To: field */
1154 rfc822_free_address (&e->reply_to);
1155 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1158 else if (!ascii_strcasecmp (line + 5, "followup-to"))
1160 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1168 if (!mutt_strcasecmp (line + 1, "ewsgroups"))
1170 FREE (&e->newsgroups);
1171 mutt_remove_trailing_ws (p);
1172 e->newsgroups = safe_strdup (mutt_skip_whitespace (p));
1179 /* field `Organization:' saves only for pager! */
1180 if (!mutt_strcasecmp (line + 1, "rganization"))
1182 if (!e->organization && mutt_strcasecmp (p, "unknown"))
1183 e->organization = safe_strdup (p);
1188 if (!ascii_strcasecmp (line + 1, "eferences"))
1190 mutt_free_list (&e->references);
1191 e->references = mutt_parse_references (p, 0);
1194 else if (!ascii_strcasecmp (line + 1, "eply-to"))
1196 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1199 else if (!ascii_strcasecmp (line + 1, "eturn-path"))
1201 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1204 else if (!ascii_strcasecmp (line + 1, "eceived"))
1206 if (hdr && !hdr->received)
1208 char *d = strchr (p, ';');
1211 hdr->received = mutt_parse_date (d + 1, NULL);
1217 if (!ascii_strcasecmp (line + 1, "ubject"))
1220 e->subject = safe_strdup (p);
1223 else if (!ascii_strcasecmp (line + 1, "ender"))
1225 e->sender = rfc822_parse_adrlist (e->sender, p);
1228 else if (!ascii_strcasecmp (line + 1, "tatus"))
1251 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1252 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1253 e->supersedes = safe_strdup (p);
1257 if (ascii_strcasecmp (line+1, "o") == 0)
1259 e->to = rfc822_parse_adrlist (e->to, p);
1265 if (ascii_strcasecmp (line+1, "-status") == 0)
1290 else if (ascii_strcasecmp (line+1, "-label") == 0)
1292 e->x_label = safe_strdup(p);
1296 else if (!mutt_strcasecmp (line + 1, "-comment-to"))
1298 if (!e->x_comment_to)
1299 e->x_comment_to = safe_strdup (p);
1302 else if (!mutt_strcasecmp (line + 1, "ref"))
1305 e->xref = safe_strdup (p);
1314 /* Keep track of the user-defined headers */
1315 if (!matched && user_hdrs)
1317 /* restore the original line */
1318 line[strlen (line)] = ':';
1320 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1321 && !mutt_matches_ignore (line, UnIgnore))
1326 last->next = mutt_new_list ();
1330 last = e->userhdrs = mutt_new_list ();
1331 last->data = safe_strdup (line);
1333 rfc2047_decode (&last->data);
1343 /* mutt_read_rfc822_header() -- parses a RFC822 header
1347 * f stream to read from
1349 * hdr header structure of current message (optional).
1351 * user_hdrs If set, store user headers. Used for recall-message and
1354 * weed If this parameter is set and the user has activated the
1355 * $weed option, honor the header weed list for user headers.
1356 * Used for recall-message.
1358 * Returns: newly allocated envelope structure. You should free it by
1359 * mutt_free_envelope() when envelope stay unneeded.
1361 ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
1364 ENVELOPE *e = mutt_new_envelope();
1366 char *line = safe_malloc (LONG_STRING);
1370 size_t linelen = LONG_STRING;
1371 char buf[LONG_STRING+1];
1375 if (hdr->content == NULL)
1377 hdr->content = mutt_new_body ();
1379 /* set the defaults from RFC1521 */
1380 hdr->content->type = TYPETEXT;
1381 hdr->content->subtype = safe_strdup ("plain");
1382 hdr->content->encoding = ENC7BIT;
1383 hdr->content->length = -1;
1385 /* RFC 2183 says this is arbitrary */
1386 hdr->content->disposition = DISPINLINE;
1390 while ((loc = ftell (f)),
1391 *(line = read_rfc822_line (f, line, &linelen)) != 0)
1395 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':')
1397 char return_path[LONG_STRING];
1400 /* some bogus MTAs will quote the original "From " line */
1401 if (mutt_strncmp (">From ", line, 6) == 0)
1402 continue; /* just ignore */
1403 else if (is_from (line, return_path, sizeof (return_path), &t))
1405 /* MH somtimes has the From_ line in the middle of the header! */
1406 if (hdr && !hdr->received)
1407 hdr->received = t - mutt_local_tz (t);
1412 break; /* end of header */
1417 if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf)))
1419 if (!mutt_match_rx_list(line, NoSpamList))
1422 /* if spam tag already exists, figure out how to amend it */
1423 if (e->spam && *buf)
1425 /* If SpamSep defined, append with separator */
1428 mutt_buffer_addstr(e->spam, SpamSep);
1429 mutt_buffer_addstr(e->spam, buf);
1432 /* else overwrite */
1435 e->spam->dptr = e->spam->data;
1436 *e->spam->dptr = '\0';
1437 mutt_buffer_addstr(e->spam, buf);
1441 /* spam tag is new, and match expr is non-empty; copy */
1442 else if (!e->spam && *buf)
1444 e->spam = mutt_buffer_from(NULL, buf);
1447 /* match expr is empty; plug in null string if no existing tag */
1450 e->spam = mutt_buffer_from(NULL, "");
1453 if (e->spam && e->spam->data)
1454 dprint(5, (debugfile, "p822: spam = %s\n", e->spam->data));
1462 continue; /* skip empty header fields */
1464 matched = mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1472 hdr->content->hdr_offset = hdr->offset;
1473 hdr->content->offset = ftell (f);
1475 /* do RFC2047 decoding */
1476 rfc2047_decode_adrlist (e->from);
1477 rfc2047_decode_adrlist (e->to);
1478 rfc2047_decode_adrlist (e->cc);
1479 rfc2047_decode_adrlist (e->bcc);
1480 rfc2047_decode_adrlist (e->reply_to);
1481 rfc2047_decode_adrlist (e->mail_followup_to);
1482 rfc2047_decode_adrlist (e->return_path);
1483 rfc2047_decode_adrlist (e->sender);
1487 regmatch_t pmatch[1];
1489 rfc2047_decode (&e->subject);
1491 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1492 e->real_subj = e->subject + pmatch[0].rm_eo;
1494 e->real_subj = e->subject;
1497 /* check for missing or invalid date */
1498 if (hdr->date_sent <= 0)
1500 dprint(1,(debugfile,"read_rfc822_header(): no date found, using received time from msg separator\n"));
1501 hdr->date_sent = hdr->received;
1508 ADDRESS *mutt_parse_adrlist (ADDRESS *p, const char *s)
1512 /* check for a simple whitespace separated list of addresses */
1513 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL)
1515 char tmp[HUGE_STRING];
1518 strfcpy (tmp, s, sizeof (tmp));
1520 while ((r = strtok (r, " \t")) != NULL)
1522 p = rfc822_parse_adrlist (p, r);
1527 p = rfc822_parse_adrlist (p, s);