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 = */
217 for (i=0; *s && *s != '"' && i < sizeof (buffer) - 1; i++, s++)
221 /* Quote the next character */
231 s++; /* skip over the " */
235 for (i=0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1; i++, s++)
240 new->value = safe_strdup (buffer);
242 dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n",
243 new->attribute ? new->attribute : "",
244 new->value ? new->value : ""));
246 /* Add this parameter to the list */
257 dprint (1, (debugfile, "parse_parameters(): parameter with no value: %s\n", s));
261 /* Find the next parameter */
262 if (*s != ';' && (s = strchr (s, ';')) == NULL)
263 break; /* no more parameters */
269 /* Move past any leading whitespace */
272 while (*s == ';'); /* skip empty parameters */
277 rfc2231_decode_parameters (&head);
281 int mutt_check_mime_type (const char *s)
283 if (ascii_strcasecmp ("text", s) == 0)
285 else if (ascii_strcasecmp ("multipart", s) == 0)
286 return TYPEMULTIPART;
287 #ifdef SUN_ATTACHMENT
288 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
289 return TYPEMULTIPART;
291 else if (ascii_strcasecmp ("application", s) == 0)
292 return TYPEAPPLICATION;
293 else if (ascii_strcasecmp ("message", s) == 0)
295 else if (ascii_strcasecmp ("image", s) == 0)
297 else if (ascii_strcasecmp ("audio", s) == 0)
299 else if (ascii_strcasecmp ("video", s) == 0)
301 else if (ascii_strcasecmp ("model", s) == 0)
307 void mutt_parse_content_type (char *s, BODY *ct)
313 mutt_free_parameter(&ct->parameter);
315 /* First extract any existing parameters */
316 if ((pc = strchr(s, ';')) != NULL)
319 while (*pc && ISSPACE (*pc))
321 ct->parameter = parse_parameters(pc);
323 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
324 * but if a filename has already been set in the content-disposition,
325 * let that take precedence, and don't set it here */
326 if ((pc = mutt_get_parameter( "name", ct->parameter)) != 0 && !ct->filename)
327 ct->filename = safe_strdup(pc);
329 #ifdef SUN_ATTACHMENT
330 /* this is deep and utter perversion */
331 if ((pc = mutt_get_parameter ("conversions", ct->parameter)) != 0)
332 ct->encoding = mutt_check_encoding (pc);
337 /* Now get the subtype */
338 if ((subtype = strchr(s, '/')))
341 for(pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++)
344 ct->subtype = safe_strdup (subtype);
347 /* Finally, get the major type */
348 ct->type = mutt_check_mime_type (s);
350 #ifdef SUN_ATTACHMENT
351 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
352 ct->subtype = safe_strdup ("x-sun-attachment");
355 if (ct->type == TYPEOTHER)
357 ct->xtype = safe_strdup (s);
360 if (ct->subtype == NULL)
362 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
363 * field, so we can attempt to convert the type to BODY here.
365 if (ct->type == TYPETEXT)
366 ct->subtype = safe_strdup ("plain");
367 else if (ct->type == TYPEAUDIO)
368 ct->subtype = safe_strdup ("basic");
369 else if (ct->type == TYPEMESSAGE)
370 ct->subtype = safe_strdup ("rfc822");
371 else if (ct->type == TYPEOTHER)
373 char buffer[SHORT_STRING];
375 ct->type = TYPEAPPLICATION;
376 snprintf (buffer, sizeof (buffer), "x-%s", s);
377 ct->subtype = safe_strdup (buffer);
380 ct->subtype = safe_strdup ("x-unknown");
383 /* Default character set for text types. */
384 if (ct->type == TYPETEXT)
386 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
387 mutt_set_parameter ("charset", "us-ascii", &ct->parameter);
392 static void parse_content_disposition (char *s, BODY *ct)
396 if (!ascii_strncasecmp ("inline", s, 6))
397 ct->disposition = DISPINLINE;
398 else if (!ascii_strncasecmp ("form-data", s, 9))
399 ct->disposition = DISPFORMDATA;
401 ct->disposition = DISPATTACH;
403 /* Check to see if a default filename was given */
404 if ((s = strchr (s, ';')) != NULL)
408 if ((s = mutt_get_parameter ("filename", (parms = parse_parameters (s)))) != 0)
409 mutt_str_replace (&ct->filename, s);
410 if ((s = mutt_get_parameter ("name", parms)) != 0)
411 ct->form_name = safe_strdup (s);
412 mutt_free_parameter (&parms);
417 * fp stream to read from
419 * digest 1 if reading subparts of a multipart/digest, 0
423 BODY *mutt_read_mime_header (FILE *fp, int digest)
425 BODY *p = mutt_new_body();
427 char *line = safe_malloc (LONG_STRING);
428 size_t linelen = LONG_STRING;
430 p->hdr_offset = ftell(fp);
432 p->encoding = ENC7BIT; /* default from RFC1521 */
433 p->type = digest ? TYPEMESSAGE : TYPETEXT;
434 p->disposition = DISPINLINE;
436 while (*(line = read_rfc822_line (fp, line, &linelen)) != 0)
438 /* Find the value of the current header */
439 if ((c = strchr (line, ':')))
446 dprint (1, (debugfile, "mutt_read_mime_header(): skipping empty header field: %s\n", line));
452 dprint (1, (debugfile, "read_mime_header: bogus MIME header: %s\n", line));
456 if (!ascii_strncasecmp ("content-", line, 8))
458 if (!ascii_strcasecmp ("type", line + 8))
459 mutt_parse_content_type (c, p);
460 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
461 p->encoding = mutt_check_encoding (c);
462 else if (!ascii_strcasecmp ("disposition", line + 8))
463 parse_content_disposition (c, p);
464 else if (!ascii_strcasecmp ("description", line + 8))
466 mutt_str_replace (&p->description, c);
467 rfc2047_decode (&p->description);
470 #ifdef SUN_ATTACHMENT
471 else if (!ascii_strncasecmp ("x-sun-", line, 6))
473 if (!ascii_strcasecmp ("data-type", line + 6))
474 mutt_parse_content_type (c, p);
475 else if (!ascii_strcasecmp ("encoding-info", line + 6))
476 p->encoding = mutt_check_encoding (c);
477 else if (!ascii_strcasecmp ("content-lines", line + 6))
478 mutt_set_parameter ("content-lines", c, &(p->parameter));
479 else if (!ascii_strcasecmp ("data-description", line + 6))
481 mutt_str_replace (&p->description, c);
482 rfc2047_decode (&p->description);
487 p->offset = ftell (fp); /* Mark the start of the real data */
488 if (p->type == TYPETEXT && !p->subtype)
489 p->subtype = safe_strdup ("plain");
490 else if (p->type == TYPEMESSAGE && !p->subtype)
491 p->subtype = safe_strdup ("rfc822");
498 void mutt_parse_part (FILE *fp, BODY *b)
505 #ifdef SUN_ATTACHMENT
506 if ( !ascii_strcasecmp (b->subtype, "x-sun-attachment") )
510 bound = mutt_get_parameter ("boundary", b->parameter);
512 fseek (fp, b->offset, SEEK_SET);
513 b->parts = mutt_parse_multipart (fp, bound,
514 b->offset + b->length,
515 ascii_strcasecmp ("digest", b->subtype) == 0);
521 fseek (fp, b->offset, SEEK_SET);
522 if (mutt_is_message_type(b->type, b->subtype))
523 b->parts = mutt_parse_messageRFC822 (fp, b);
524 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
525 b->parts = mutt_read_mime_header (fp, 0);
535 /* try to recover from parsing error */
539 mutt_str_replace (&b->subtype, "plain");
543 /* parse a MESSAGE/RFC822 body
546 * fp stream to read from
548 * parent structure which contains info about the message/rfc822
551 * NOTE: this assumes that `parent->length' has been set!
554 BODY *mutt_parse_messageRFC822 (FILE *fp, BODY *parent)
558 parent->hdr = mutt_new_header ();
559 parent->hdr->offset = ftell (fp);
560 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
561 msg = parent->hdr->content;
563 /* ignore the length given in the content-length since it could be wrong
564 and we already have the info to calculate the correct length */
565 /* if (msg->length == -1) */
566 msg->length = parent->length - (msg->offset - parent->offset);
568 /* if body of this message is empty, we can end up with a negative length */
572 mutt_parse_part(fp, msg);
576 /* parse a multipart structure
579 * fp stream to read from
581 * boundary body separator
583 * end_off length of the multipart body (used when the final
584 * boundary is missing to avoid reading too far)
586 * digest 1 if reading a multipart/digest, 0 otherwise
589 BODY *mutt_parse_multipart (FILE *fp, const char *boundary, long end_off, int digest)
591 #ifdef SUN_ATTACHMENT
594 int blen, len, crlf = 0;
595 char buffer[LONG_STRING];
596 BODY *head = 0, *last = 0, *new = 0;
598 int final = 0; /* did we see the ending boundary? */
602 mutt_error _("multipart message has no boundary parameter!");
606 blen = mutt_strlen (boundary);
607 while (ftell (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL)
609 len = mutt_strlen (buffer);
611 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
613 if (buffer[0] == '-' && buffer[1] == '-' &&
614 mutt_strncmp (buffer + 2, boundary, blen) == 0)
618 last->length = ftell (fp) - last->offset - len - 1 - crlf;
619 if (last->parts && last->parts->length == 0)
620 last->parts->length = ftell (fp) - last->parts->offset - len - 1 - crlf;
621 /* if the body is empty, we can end up with a -1 length */
622 if (last->length < 0)
626 /* Remove any trailing whitespace, up to the length of the boundary */
627 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
630 /* Check for the end boundary */
631 if (mutt_strcmp (buffer + blen + 2, "--") == 0)
634 break; /* done parsing */
636 else if (buffer[2 + blen] == 0)
638 new = mutt_read_mime_header (fp, digest);
640 #ifdef SUN_ATTACHMENT
641 if (mutt_get_parameter ("content-lines", new->parameter)) {
642 for (lines = atoi(mutt_get_parameter ("content-lines", new->parameter));
644 if (ftell (fp) >= end_off || fgets (buffer, LONG_STRING, fp) == NULL)
650 * Consistency checking - catch
651 * bad attachment end boundaries
654 if(new->offset > end_off)
656 mutt_free_body(&new);
670 /* in case of missing end boundary, set the length to something reasonable */
671 if (last && last->length == 0 && !final)
672 last->length = end_off - last->offset;
674 /* parse recursive MIME parts */
675 for(last = head; last; last = last->next)
676 mutt_parse_part(fp, last);
681 static const char *uncomment_timezone (char *buf, size_t buflen, const char *tz)
687 return tz; /* no need to do anything */
690 if ((p = strpbrk (tz, " )")) == NULL)
693 if (len > buflen - 1)
695 memcpy (buf, tz, len);
703 unsigned char zhours;
704 unsigned char zminutes;
705 unsigned char zoccident; /* west of UTC? */
709 { "aat", 1, 0, 1 }, /* Atlantic Africa Time */
710 { "adt", 4, 0, 0 }, /* Arabia DST */
711 { "ast", 3, 0, 0 }, /* Arabia */
712 /*{ "ast", 4, 0, 1 },*/ /* Atlantic */
713 { "bst", 1, 0, 0 }, /* British DST */
714 { "cat", 1, 0, 0 }, /* Central Africa */
716 { "cest", 2, 0, 0 }, /* Central Europe DST */
717 { "cet", 1, 0, 0 }, /* Central Europe */
719 /*{ "cst", 8, 0, 0 },*/ /* China */
720 /*{ "cst", 9, 30, 0 },*/ /* Australian Central Standard Time */
721 { "eat", 3, 0, 0 }, /* East Africa */
723 { "eest", 3, 0, 0 }, /* Eastern Europe DST */
724 { "eet", 2, 0, 0 }, /* Eastern Europe */
725 { "egst", 0, 0, 0 }, /* Eastern Greenland DST */
726 { "egt", 1, 0, 1 }, /* Eastern Greenland */
729 { "gst", 4, 0, 0 }, /* Presian Gulf */
730 { "hkt", 8, 0, 0 }, /* Hong Kong */
731 { "ict", 7, 0, 0 }, /* Indochina */
732 { "idt", 3, 0, 0 }, /* Israel DST */
733 { "ist", 2, 0, 0 }, /* Israel */
734 /*{ "ist", 5, 30, 0 },*/ /* India */
735 { "jst", 9, 0, 0 }, /* Japan */
736 { "kst", 9, 0, 0 }, /* Korea */
738 { "met", 1, 0, 0 }, /* this is now officially CET */
739 { "msd", 4, 0, 0 }, /* Moscow DST */
740 { "msk", 3, 0, 0 }, /* Moscow */
742 { "nzdt", 13, 0, 0 }, /* New Zealand DST */
743 { "nzst", 12, 0, 0 }, /* New Zealand */
746 { "sat", 2, 0, 0 }, /* South Africa */
747 { "smt", 4, 0, 0 }, /* Seychelles */
748 { "sst", 11, 0, 1 }, /* Samoa */
749 /*{ "sst", 8, 0, 0 },*/ /* Singapore */
751 { "wat", 0, 0, 0 }, /* West Africa */
752 { "west", 1, 0, 0 }, /* Western Europe DST */
753 { "wet", 0, 0, 0 }, /* Western Europe */
754 { "wgst", 2, 0, 1 }, /* Western Greenland DST */
755 { "wgt", 3, 0, 1 }, /* Western Greenland */
756 { "wst", 8, 0, 0 }, /* Western Australia */
759 /* parses a date string in RFC822 format:
761 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
763 * This routine assumes that `h' has been initialized to 0. the `timezone'
764 * field is optional, defaulting to +0000 if missing.
766 time_t mutt_parse_date (const char *s, HEADER *h)
778 char tzstr[SHORT_STRING];
779 char scratch[SHORT_STRING];
781 /* Don't modify our argument. Fixed-size buffer is ok here since
782 * the date format imposes a natural limit.
785 strfcpy (scratch, s, sizeof (scratch));
787 /* kill the day of the week, if it exists. */
788 if ((t = strchr (scratch, ',')))
794 memset (&tm, 0, sizeof (tm));
796 while ((t = strtok (t, " \t")) != NULL)
800 case 0: /* day of the month */
801 if (!isdigit ((unsigned char) *t))
803 tm.tm_mday = atoi (t);
808 case 1: /* month of the year */
809 if ((i = mutt_check_month (t)) < 0)
815 tm.tm_year = atoi (t);
818 else if (tm.tm_year >= 1900)
822 case 3: /* time of day */
823 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3)
825 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
829 dprint(1, (debugfile, "parse_date: could not process time format: %s\n", t));
837 case 4: /* timezone */
838 /* sometimes we see things like (MST) or (-0700) so attempt to
839 * compensate by uncommenting the string if non-RFC822 compliant
841 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
843 if (*ptz == '+' || *ptz == '-')
845 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
846 && isdigit ((unsigned char) ptz[1]) && isdigit ((unsigned char) ptz[2])
847 && isdigit ((unsigned char) ptz[3]) && isdigit ((unsigned char) ptz[4]))
849 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
850 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
860 tz = bsearch (ptz, TimeZones, sizeof TimeZones/sizeof (struct tz_t),
861 sizeof (struct tz_t),
862 (int (*)(const void *, const void *)) ascii_strcasecmp
863 /* This is safe to do: A pointer to a struct equals
864 * a pointer to its first element*/);
869 zminutes = tz->zminutes;
870 zoccident = tz->zoccident;
873 /* ad hoc support for the European MET (now officially CET) TZ */
874 if (ascii_strcasecmp (t, "MET") == 0)
876 if ((t = strtok (NULL, " \t")) != NULL)
878 if (!ascii_strcasecmp (t, "DST"))
883 tz_offset = zhours * 3600 + zminutes * 60;
885 tz_offset = -tz_offset;
892 if (count < 4) /* don't check for missing timezone */
894 dprint(1,(debugfile, "parse_date(): error parsing date format, using received time\n"));
901 h->zminutes = zminutes;
902 h->zoccident = zoccident;
905 return (mutt_mktime (&tm, 0) + tz_offset);
908 /* extract the first substring that looks like a message-id */
909 static char *extract_message_id (const char *s)
915 if ((s = strchr (s, '<')) == NULL || (p = strchr (s, '>')) == NULL)
917 l = (size_t)(p - s) + 1;
918 r = safe_malloc (l + 1);
924 void mutt_parse_mime_message (CONTEXT *ctx, HEADER *cur)
928 if (cur->content->type != TYPEMESSAGE && cur->content->type != TYPEMULTIPART)
929 return; /* nothing to do */
931 if (cur->content->parts)
932 return; /* The message was parsed earlier. */
934 if ((msg = mx_open_message (ctx, cur->msgno)))
936 mutt_parse_part (msg->fp, cur->content);
939 cur->security = crypt_query (cur->content);
941 mx_close_message (&msg);
945 int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short user_hdrs, short weed,
946 short do_2047, LIST **lastp)
954 switch (ascii_tolower (line[0]))
957 if (ascii_strcasecmp (line+1, "pparently-to") == 0)
959 e->to = rfc822_parse_adrlist (e->to, p);
962 else if (ascii_strcasecmp (line+1, "pparently-from") == 0)
964 e->from = rfc822_parse_adrlist (e->from, p);
970 if (ascii_strcasecmp (line+1, "cc") == 0)
972 e->bcc = rfc822_parse_adrlist (e->bcc, p);
978 if (ascii_strcasecmp (line+1, "c") == 0)
980 e->cc = rfc822_parse_adrlist (e->cc, p);
983 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0)
985 if (ascii_strcasecmp (line+8, "type") == 0)
988 mutt_parse_content_type (p, hdr->content);
991 else if (ascii_strcasecmp (line+8, "transfer-encoding") == 0)
994 hdr->content->encoding = mutt_check_encoding (p);
997 else if (ascii_strcasecmp (line+8, "length") == 0)
1001 if ((hdr->content->length = atoi (p)) < 0)
1002 hdr->content->length = -1;
1006 else if (ascii_strcasecmp (line+8, "description") == 0)
1010 mutt_str_replace (&hdr->content->description, p);
1011 rfc2047_decode (&hdr->content->description);
1015 else if (ascii_strcasecmp (line+8, "disposition") == 0)
1018 parse_content_disposition (p, hdr->content);
1025 if (!ascii_strcasecmp ("ate", line + 1))
1027 mutt_str_replace (&e->date, p);
1029 hdr->date_sent = mutt_parse_date (p, hdr);
1035 if (!ascii_strcasecmp ("xpires", line + 1) &&
1036 hdr && mutt_parse_date (p, NULL) < time (NULL))
1041 if (!ascii_strcasecmp ("rom", line + 1))
1043 e->from = rfc822_parse_adrlist (e->from, p);
1047 else if (!mutt_strcasecmp (line+1, "ollowup-to"))
1049 if (!e->followup_to)
1051 mutt_remove_trailing_ws (p);
1052 e->followup_to = safe_strdup (mutt_skip_whitespace (p));
1060 if (!ascii_strcasecmp (line+1, "n-reply-to"))
1062 mutt_free_list (&e->in_reply_to);
1063 e->in_reply_to = mutt_parse_references (p, 1);
1069 if (!ascii_strcasecmp (line + 1, "ines"))
1073 hdr->lines = atoi (p);
1076 * HACK - mutt has, for a very short time, produced negative
1077 * Lines header values. Ignore them.
1085 else if (!ascii_strcasecmp (line + 1, "ist-Post"))
1087 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1088 if (strncmp (p, "NO", 2))
1091 for (beg = strchr (p, '<'); beg; beg = strchr (end, ','))
1094 if (!(end = strchr (beg, '>')))
1097 /* Take the first mailto URL */
1098 if (url_check_scheme (beg) == U_MAILTO)
1100 FREE (&e->list_post);
1101 e->list_post = mutt_substrdup (beg, end);
1111 if (!ascii_strcasecmp (line + 1, "ime-version"))
1117 else if (!ascii_strcasecmp (line + 1, "essage-id"))
1119 /* We add a new "Message-Id:" when building a message */
1120 FREE (&e->message_id);
1121 e->message_id = extract_message_id (p);
1124 else if (!ascii_strncasecmp (line + 1, "ail-", 4))
1126 if (!ascii_strcasecmp (line + 5, "reply-to"))
1128 /* override the Reply-To: field */
1129 rfc822_free_address (&e->reply_to);
1130 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1133 else if (!ascii_strcasecmp (line + 5, "followup-to"))
1135 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1143 if (!mutt_strcasecmp (line + 1, "ewsgroups"))
1145 FREE (&e->newsgroups);
1146 mutt_remove_trailing_ws (p);
1147 e->newsgroups = safe_strdup (mutt_skip_whitespace (p));
1154 /* field `Organization:' saves only for pager! */
1155 if (!mutt_strcasecmp (line + 1, "rganization"))
1157 if (!e->organization && mutt_strcasecmp (p, "unknown"))
1158 e->organization = safe_strdup (p);
1163 if (!ascii_strcasecmp (line + 1, "eferences"))
1165 mutt_free_list (&e->references);
1166 e->references = mutt_parse_references (p, 0);
1169 else if (!ascii_strcasecmp (line + 1, "eply-to"))
1171 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1174 else if (!ascii_strcasecmp (line + 1, "eturn-path"))
1176 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1179 else if (!ascii_strcasecmp (line + 1, "eceived"))
1181 if (hdr && !hdr->received)
1183 char *d = strchr (p, ';');
1186 hdr->received = mutt_parse_date (d + 1, NULL);
1192 if (!ascii_strcasecmp (line + 1, "ubject"))
1195 e->subject = safe_strdup (p);
1198 else if (!ascii_strcasecmp (line + 1, "ender"))
1200 e->sender = rfc822_parse_adrlist (e->sender, p);
1203 else if (!ascii_strcasecmp (line + 1, "tatus"))
1226 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1227 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1228 e->supersedes = safe_strdup (p);
1232 if (ascii_strcasecmp (line+1, "o") == 0)
1234 e->to = rfc822_parse_adrlist (e->to, p);
1240 if (ascii_strcasecmp (line+1, "-status") == 0)
1265 else if (ascii_strcasecmp (line+1, "-label") == 0)
1267 e->x_label = safe_strdup(p);
1271 else if (!mutt_strcasecmp (line + 1, "-comment-to"))
1273 if (!e->x_comment_to)
1274 e->x_comment_to = safe_strdup (p);
1277 else if (!mutt_strcasecmp (line + 1, "ref"))
1280 e->xref = safe_strdup (p);
1289 /* Keep track of the user-defined headers */
1290 if (!matched && user_hdrs)
1292 /* restore the original line */
1293 line[strlen (line)] = ':';
1295 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1296 && !mutt_matches_ignore (line, UnIgnore))
1301 last->next = mutt_new_list ();
1305 last = e->userhdrs = mutt_new_list ();
1306 last->data = safe_strdup (line);
1308 rfc2047_decode (&last->data);
1318 /* mutt_read_rfc822_header() -- parses a RFC822 header
1322 * f stream to read from
1324 * hdr header structure of current message (optional).
1326 * user_hdrs If set, store user headers. Used for recall-message and
1329 * weed If this parameter is set and the user has activated the
1330 * $weed option, honor the header weed list for user headers.
1331 * Used for recall-message.
1333 * Returns: newly allocated envelope structure. You should free it by
1334 * mutt_free_envelope() when envelope stay unneeded.
1336 ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
1339 ENVELOPE *e = mutt_new_envelope();
1341 char *line = safe_malloc (LONG_STRING);
1345 size_t linelen = LONG_STRING;
1346 char buf[LONG_STRING+1];
1350 if (hdr->content == NULL)
1352 hdr->content = mutt_new_body ();
1354 /* set the defaults from RFC1521 */
1355 hdr->content->type = TYPETEXT;
1356 hdr->content->subtype = safe_strdup ("plain");
1357 hdr->content->encoding = ENC7BIT;
1358 hdr->content->length = -1;
1360 /* RFC 2183 says this is arbitrary */
1361 hdr->content->disposition = DISPINLINE;
1365 while ((loc = ftell (f)),
1366 *(line = read_rfc822_line (f, line, &linelen)) != 0)
1370 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':')
1372 char return_path[LONG_STRING];
1375 /* some bogus MTAs will quote the original "From " line */
1376 if (mutt_strncmp (">From ", line, 6) == 0)
1377 continue; /* just ignore */
1378 else if (is_from (line, return_path, sizeof (return_path), &t))
1380 /* MH somtimes has the From_ line in the middle of the header! */
1381 if (hdr && !hdr->received)
1382 hdr->received = t - mutt_local_tz (t);
1387 break; /* end of header */
1392 if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf)))
1394 if (!mutt_match_rx_list(line, NoSpamList))
1397 /* if spam tag already exists, figure out how to amend it */
1398 if (e->spam && *buf)
1400 /* If SpamSep defined, append with separator */
1403 mutt_buffer_addstr(e->spam, SpamSep);
1404 mutt_buffer_addstr(e->spam, buf);
1407 /* else overwrite */
1410 e->spam->dptr = e->spam->data;
1411 *e->spam->dptr = '\0';
1412 mutt_buffer_addstr(e->spam, buf);
1416 /* spam tag is new, and match expr is non-empty; copy */
1417 else if (!e->spam && *buf)
1419 e->spam = mutt_buffer_from(NULL, buf);
1422 /* match expr is empty; plug in null string if no existing tag */
1425 e->spam = mutt_buffer_from(NULL, "");
1428 if (e->spam && e->spam->data)
1429 dprint(5, (debugfile, "p822: spam = %s\n", e->spam->data));
1437 continue; /* skip empty header fields */
1439 matched = mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1447 hdr->content->hdr_offset = hdr->offset;
1448 hdr->content->offset = ftell (f);
1450 /* do RFC2047 decoding */
1451 rfc2047_decode_adrlist (e->from);
1452 rfc2047_decode_adrlist (e->to);
1453 rfc2047_decode_adrlist (e->cc);
1454 rfc2047_decode_adrlist (e->reply_to);
1455 rfc2047_decode_adrlist (e->mail_followup_to);
1456 rfc2047_decode_adrlist (e->return_path);
1457 rfc2047_decode_adrlist (e->sender);
1461 regmatch_t pmatch[1];
1463 rfc2047_decode (&e->subject);
1465 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1466 e->real_subj = e->subject + pmatch[0].rm_eo;
1468 e->real_subj = e->subject;
1471 /* check for missing or invalid date */
1472 if (hdr->date_sent <= 0)
1474 dprint(1,(debugfile,"read_rfc822_header(): no date found, using received time from msg separator\n"));
1475 hdr->date_sent = hdr->received;
1482 ADDRESS *mutt_parse_adrlist (ADDRESS *p, const char *s)
1486 /* check for a simple whitespace separated list of addresses */
1487 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL)
1489 char tmp[HUGE_STRING];
1492 strfcpy (tmp, s, sizeof (tmp));
1494 while ((r = strtok (r, " \t")) != NULL)
1496 p = rfc822_parse_adrlist (p, r);
1501 p = rfc822_parse_adrlist (p, s);