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.
20 #include "mutt_regex.h"
25 #include "mutt_crypt.h"
32 /* Reads an arbitrarily long header field, and looks ahead for continuation
33 * lines. ``line'' must point to a dynamically allocated string; it is
34 * increased if more space is required to fit the whole line.
36 static char *read_rfc822_line (FILE *f, char *line, size_t *linelen)
44 if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */
45 (ISSPACE (*line) && !offset)) /* end of headers */
51 buf += strlen (buf) - 1;
54 /* we did get a full line. remove trailing space */
55 while (ISSPACE (*buf))
56 *buf-- = 0; /* we cannot come beyond line's beginning because
57 * it begins with a non-space */
59 /* check to see if the next line is a continuation line */
60 if ((ch = fgetc (f)) != ' ' && ch != '\t')
63 return (line); /* next line is a separate header field or EOH */
66 /* eat tabs and spaces from the beginning of the continuation line */
67 while ((ch = fgetc (f)) == ' ' || ch == '\t')
70 *++buf = ' '; /* string is still terminated because we removed
71 at least one whitespace char above */
76 if (*linelen < offset + STRING)
80 safe_realloc (&line, *linelen);
87 LIST *mutt_parse_references (char *s, int in_reply_to)
91 char *o = NULL, *new, *at;
93 while ((s = strtok (s, " \t;")) != NULL)
96 * some mail clients add other garbage besides message-ids, so do a quick
97 * check to make sure this looks like a valid message-id
98 * some idiotic clients also break their message-ids between lines, deal
99 * with that too (give up if it's more than two lines, though)
114 new = safe_strdup (s);
121 new = safe_malloc (sizeof (char) * (n + m + 1));
122 strcpy (new, o); /* __STRCPY_CHECKED__ */
123 strcpy (new + n, s); /* __STRCPY_CHECKED__ */
128 /* make sure that this really does look like a message-id.
129 * it should have exactly one @, and if we're looking at
130 * an in-reply-to header, make sure that the part before
131 * the @ has more than eight characters or it's probably
134 if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
135 || (in_reply_to && at - new <= 8))
139 t = (LIST *) safe_malloc (sizeof (LIST));
152 int mutt_check_encoding (const char *c)
154 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit")-1) == 0)
156 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit")-1) == 0)
158 else if (ascii_strncasecmp ("binary", c, sizeof ("binary")-1) == 0)
160 else if (ascii_strncasecmp ("quoted-printable", c, sizeof ("quoted-printable")-1) == 0)
161 return (ENCQUOTEDPRINTABLE);
162 else if (ascii_strncasecmp ("base64", c, sizeof("base64")-1) == 0)
164 else if (ascii_strncasecmp ("x-uuencode", c, sizeof("x-uuencode")-1) == 0)
165 return (ENCUUENCODED);
166 #ifdef SUN_ATTACHMENT
167 else if (ascii_strncasecmp ("uuencode", c, sizeof("uuencode")-1) == 0)
168 return (ENCUUENCODED);
174 static PARAMETER *parse_parameters (const char *s)
176 PARAMETER *head = 0, *cur = 0, *new;
177 char buffer[LONG_STRING];
181 dprint (2, (debugfile, "parse_parameters: `%s'\n", s));
185 if ((p = strpbrk (s, "=;")) == NULL)
187 dprint(1, (debugfile, "parse_parameters: malformed parameter: %s\n", s));
191 /* if we hit a ; now the parameter has no value, just skip it */
196 new = mutt_new_parameter ();
198 new->attribute = safe_malloc (i + 1);
199 memcpy (new->attribute, s, i);
200 new->attribute[i] = 0;
202 /* remove whitespace from the end of the attribute name */
203 while (ISSPACE (new->attribute[--i]))
204 new->attribute[i] = 0;
206 s = p + 1; /* skip over the = */
212 for (i=0; *s && *s != '"' && i < sizeof (buffer) - 1; i++, s++)
216 /* Quote the next character */
226 s++; /* skip over the " */
230 for (i=0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1; i++, s++)
235 new->value = safe_strdup (buffer);
237 dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n",
238 new->attribute ? new->attribute : "",
239 new->value ? new->value : ""));
241 /* Add this parameter to the list */
252 dprint (1, (debugfile, "parse_parameters(): parameter with no value: %s\n", s));
256 /* Find the next parameter */
257 if (*s != ';' && (s = strchr (s, ';')) == NULL)
258 break; /* no more parameters */
264 /* Move past any leading whitespace */
267 while (*s == ';'); /* skip empty parameters */
272 rfc2231_decode_parameters (&head);
276 int mutt_check_mime_type (const char *s)
278 if (ascii_strcasecmp ("text", s) == 0)
280 else if (ascii_strcasecmp ("multipart", s) == 0)
281 return TYPEMULTIPART;
282 #ifdef SUN_ATTACHMENT
283 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
284 return TYPEMULTIPART;
286 else if (ascii_strcasecmp ("application", s) == 0)
287 return TYPEAPPLICATION;
288 else if (ascii_strcasecmp ("message", s) == 0)
290 else if (ascii_strcasecmp ("image", s) == 0)
292 else if (ascii_strcasecmp ("audio", s) == 0)
294 else if (ascii_strcasecmp ("video", s) == 0)
296 else if (ascii_strcasecmp ("model", s) == 0)
302 void mutt_parse_content_type (char *s, BODY *ct)
308 mutt_free_parameter(&ct->parameter);
310 /* First extract any existing parameters */
311 if ((pc = strchr(s, ';')) != NULL)
314 while (*pc && ISSPACE (*pc))
316 ct->parameter = parse_parameters(pc);
318 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
319 * but if a filename has already been set in the content-disposition,
320 * let that take precedence, and don't set it here */
321 if ((pc = mutt_get_parameter( "name", ct->parameter)) != 0 && !ct->filename)
322 ct->filename = safe_strdup(pc);
324 #ifdef SUN_ATTACHMENT
325 /* this is deep and utter perversion */
326 if ((pc = mutt_get_parameter ("conversions", ct->parameter)) != 0)
327 ct->encoding = mutt_check_encoding (pc);
332 /* Now get the subtype */
333 if ((subtype = strchr(s, '/')))
336 for(pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++)
339 ct->subtype = safe_strdup (subtype);
342 /* Finally, get the major type */
343 ct->type = mutt_check_mime_type (s);
345 #ifdef SUN_ATTACHMENT
346 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
347 ct->subtype = safe_strdup ("x-sun-attachment");
350 if (ct->type == TYPEOTHER)
352 ct->xtype = safe_strdup (s);
355 if (ct->subtype == NULL)
357 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
358 * field, so we can attempt to convert the type to BODY here.
360 if (ct->type == TYPETEXT)
361 ct->subtype = safe_strdup ("plain");
362 else if (ct->type == TYPEAUDIO)
363 ct->subtype = safe_strdup ("basic");
364 else if (ct->type == TYPEMESSAGE)
365 ct->subtype = safe_strdup ("rfc822");
366 else if (ct->type == TYPEOTHER)
368 char buffer[SHORT_STRING];
370 ct->type = TYPEAPPLICATION;
371 snprintf (buffer, sizeof (buffer), "x-%s", s);
372 ct->subtype = safe_strdup (buffer);
375 ct->subtype = safe_strdup ("x-unknown");
378 /* Default character set for text types. */
379 if (ct->type == TYPETEXT)
381 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
382 mutt_set_parameter ("charset", "us-ascii", &ct->parameter);
387 static void parse_content_disposition (char *s, BODY *ct)
391 if (!ascii_strncasecmp ("inline", s, 6))
392 ct->disposition = DISPINLINE;
393 else if (!ascii_strncasecmp ("form-data", s, 9))
394 ct->disposition = DISPFORMDATA;
396 ct->disposition = DISPATTACH;
398 /* Check to see if a default filename was given */
399 if ((s = strchr (s, ';')) != NULL)
403 if ((s = mutt_get_parameter ("filename", (parms = parse_parameters (s)))) != 0)
404 mutt_str_replace (&ct->filename, s);
405 if ((s = mutt_get_parameter ("name", parms)) != 0)
406 ct->form_name = safe_strdup (s);
407 mutt_free_parameter (&parms);
412 * fp stream to read from
414 * digest 1 if reading subparts of a multipart/digest, 0
418 BODY *mutt_read_mime_header (FILE *fp, int digest)
420 BODY *p = mutt_new_body();
422 char *line = safe_malloc (LONG_STRING);
423 size_t linelen = LONG_STRING;
425 p->hdr_offset = ftell(fp);
427 p->encoding = ENC7BIT; /* default from RFC1521 */
428 p->type = digest ? TYPEMESSAGE : TYPETEXT;
429 p->disposition = DISPINLINE;
431 while (*(line = read_rfc822_line (fp, line, &linelen)) != 0)
433 /* Find the value of the current header */
434 if ((c = strchr (line, ':')))
441 dprint (1, (debugfile, "mutt_read_mime_header(): skipping empty header field: %s\n", line));
447 dprint (1, (debugfile, "read_mime_header: bogus MIME header: %s\n", line));
451 if (!ascii_strncasecmp ("content-", line, 8))
453 if (!ascii_strcasecmp ("type", line + 8))
454 mutt_parse_content_type (c, p);
455 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
456 p->encoding = mutt_check_encoding (c);
457 else if (!ascii_strcasecmp ("disposition", line + 8))
458 parse_content_disposition (c, p);
459 else if (!ascii_strcasecmp ("description", line + 8))
461 mutt_str_replace (&p->description, c);
462 rfc2047_decode (&p->description);
465 #ifdef SUN_ATTACHMENT
466 else if (!ascii_strncasecmp ("x-sun-", line, 6))
468 if (!ascii_strcasecmp ("data-type", line + 6))
469 mutt_parse_content_type (c, p);
470 else if (!ascii_strcasecmp ("encoding-info", line + 6))
471 p->encoding = mutt_check_encoding (c);
472 else if (!ascii_strcasecmp ("content-lines", line + 6))
473 mutt_set_parameter ("content-lines", c, &(p->parameter));
474 else if (!ascii_strcasecmp ("data-description", line + 6))
476 mutt_str_replace (&p->description, c);
477 rfc2047_decode (&p->description);
482 p->offset = ftell (fp); /* Mark the start of the real data */
483 if (p->type == TYPETEXT && !p->subtype)
484 p->subtype = safe_strdup ("plain");
485 else if (p->type == TYPEMESSAGE && !p->subtype)
486 p->subtype = safe_strdup ("rfc822");
493 void mutt_parse_part (FILE *fp, BODY *b)
500 #ifdef SUN_ATTACHMENT
501 if ( !ascii_strcasecmp (b->subtype, "x-sun-attachment") )
505 bound = mutt_get_parameter ("boundary", b->parameter);
507 fseek (fp, b->offset, SEEK_SET);
508 b->parts = mutt_parse_multipart (fp, bound,
509 b->offset + b->length,
510 ascii_strcasecmp ("digest", b->subtype) == 0);
516 fseek (fp, b->offset, SEEK_SET);
517 if (mutt_is_message_type(b->type, b->subtype))
518 b->parts = mutt_parse_messageRFC822 (fp, b);
519 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
520 b->parts = mutt_read_mime_header (fp, 0);
530 /* try to recover from parsing error */
534 mutt_str_replace (&b->subtype, "plain");
538 /* parse a MESSAGE/RFC822 body
541 * fp stream to read from
543 * parent structure which contains info about the message/rfc822
546 * NOTE: this assumes that `parent->length' has been set!
549 BODY *mutt_parse_messageRFC822 (FILE *fp, BODY *parent)
553 parent->hdr = mutt_new_header ();
554 parent->hdr->offset = ftell (fp);
555 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
556 msg = parent->hdr->content;
558 /* ignore the length given in the content-length since it could be wrong
559 and we already have the info to calculate the correct length */
560 /* if (msg->length == -1) */
561 msg->length = parent->length - (msg->offset - parent->offset);
563 /* if body of this message is empty, we can end up with a negative length */
567 mutt_parse_part(fp, msg);
571 /* parse a multipart structure
574 * fp stream to read from
576 * boundary body separator
578 * end_off length of the multipart body (used when the final
579 * boundary is missing to avoid reading too far)
581 * digest 1 if reading a multipart/digest, 0 otherwise
584 BODY *mutt_parse_multipart (FILE *fp, const char *boundary, long end_off, int digest)
586 #ifdef SUN_ATTACHMENT
589 int blen, len, crlf = 0;
590 char buffer[LONG_STRING];
591 BODY *head = 0, *last = 0, *new = 0;
593 int final = 0; /* did we see the ending boundary? */
597 mutt_error _("multipart message has no boundary parameter!");
601 blen = mutt_strlen (boundary);
602 while (ftell (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL)
604 len = mutt_strlen (buffer);
606 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
608 if (buffer[0] == '-' && buffer[1] == '-' &&
609 mutt_strncmp (buffer + 2, boundary, blen) == 0)
613 last->length = ftell (fp) - last->offset - len - 1 - crlf;
614 if (last->parts && last->parts->length == 0)
615 last->parts->length = ftell (fp) - last->parts->offset - len - 1 - crlf;
616 /* if the body is empty, we can end up with a -1 length */
617 if (last->length < 0)
621 /* Remove any trailing whitespace, up to the length of the boundary */
622 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
625 /* Check for the end boundary */
626 if (mutt_strcmp (buffer + blen + 2, "--") == 0)
629 break; /* done parsing */
631 else if (buffer[2 + blen] == 0)
633 new = mutt_read_mime_header (fp, digest);
635 #ifdef SUN_ATTACHMENT
636 if (mutt_get_parameter ("content-lines", new->parameter)) {
637 for (lines = atoi(mutt_get_parameter ("content-lines", new->parameter));
639 if (ftell (fp) >= end_off || fgets (buffer, LONG_STRING, fp) == NULL)
645 * Consistency checking - catch
646 * bad attachment end boundaries
649 if(new->offset > end_off)
651 mutt_free_body(&new);
665 /* in case of missing end boundary, set the length to something reasonable */
666 if (last && last->length == 0 && !final)
667 last->length = end_off - last->offset;
669 /* parse recursive MIME parts */
670 for(last = head; last; last = last->next)
671 mutt_parse_part(fp, last);
676 static const char *uncomment_timezone (char *buf, size_t buflen, const char *tz)
682 return tz; /* no need to do anything */
685 if ((p = strpbrk (tz, " )")) == NULL)
688 if (len > buflen - 1)
690 memcpy (buf, tz, len);
698 unsigned char zhours;
699 unsigned char zminutes;
700 unsigned char zoccident; /* west of UTC? */
704 { "aat", 1, 0, 1 }, /* Atlantic Africa Time */
705 { "adt", 4, 0, 0 }, /* Arabia DST */
706 { "ast", 3, 0, 0 }, /* Arabia */
707 /*{ "ast", 4, 0, 1 },*/ /* Atlantic */
708 { "bst", 1, 0, 0 }, /* British DST */
709 { "cat", 1, 0, 0 }, /* Central Africa */
711 { "cest", 2, 0, 0 }, /* Central Europe DST */
712 { "cet", 1, 0, 0 }, /* Central Europe */
714 /*{ "cst", 8, 0, 0 },*/ /* China */
715 /*{ "cst", 9, 30, 0 },*/ /* Australian Central Standard Time */
716 { "eat", 3, 0, 0 }, /* East Africa */
718 { "eest", 3, 0, 0 }, /* Eastern Europe DST */
719 { "eet", 2, 0, 0 }, /* Eastern Europe */
720 { "egst", 0, 0, 0 }, /* Eastern Greenland DST */
721 { "egt", 1, 0, 1 }, /* Eastern Greenland */
724 { "gst", 4, 0, 0 }, /* Presian Gulf */
725 { "hkt", 8, 0, 0 }, /* Hong Kong */
726 { "ict", 7, 0, 0 }, /* Indochina */
727 { "idt", 3, 0, 0 }, /* Israel DST */
728 { "ist", 2, 0, 0 }, /* Israel */
729 /*{ "ist", 5, 30, 0 },*/ /* India */
730 { "jst", 9, 0, 0 }, /* Japan */
731 { "kst", 9, 0, 0 }, /* Korea */
733 { "met", 1, 0, 0 }, /* this is now officially CET */
734 { "msd", 4, 0, 0 }, /* Moscow DST */
735 { "msk", 3, 0, 0 }, /* Moscow */
737 { "nzdt", 13, 0, 0 }, /* New Zealand DST */
738 { "nzst", 12, 0, 0 }, /* New Zealand */
741 { "sat", 2, 0, 0 }, /* South Africa */
742 { "smt", 4, 0, 0 }, /* Seychelles */
743 { "sst", 11, 0, 1 }, /* Samoa */
744 /*{ "sst", 8, 0, 0 },*/ /* Singapore */
746 { "wat", 0, 0, 0 }, /* West Africa */
747 { "west", 1, 0, 0 }, /* Western Europe DST */
748 { "wet", 0, 0, 0 }, /* Western Europe */
749 { "wgst", 2, 0, 1 }, /* Western Greenland DST */
750 { "wgt", 3, 0, 1 }, /* Western Greenland */
751 { "wst", 8, 0, 0 }, /* Western Australia */
754 /* parses a date string in RFC822 format:
756 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
758 * This routine assumes that `h' has been initialized to 0. the `timezone'
759 * field is optional, defaulting to +0000 if missing.
761 time_t mutt_parse_date (const char *s, HEADER *h)
773 char tzstr[SHORT_STRING];
774 char scratch[SHORT_STRING];
776 /* Don't modify our argument. Fixed-size buffer is ok here since
777 * the date format imposes a natural limit.
780 strfcpy (scratch, s, sizeof (scratch));
782 /* kill the day of the week, if it exists. */
783 if ((t = strchr (scratch, ',')))
789 memset (&tm, 0, sizeof (tm));
791 while ((t = strtok (t, " \t")) != NULL)
795 case 0: /* day of the month */
796 if (!isdigit ((unsigned char) *t))
798 tm.tm_mday = atoi (t);
803 case 1: /* month of the year */
804 if ((i = mutt_check_month (t)) < 0)
810 tm.tm_year = atoi (t);
813 else if (tm.tm_year >= 1900)
817 case 3: /* time of day */
818 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3)
820 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
824 dprint(1, (debugfile, "parse_date: could not process time format: %s\n", t));
832 case 4: /* timezone */
833 /* sometimes we see things like (MST) or (-0700) so attempt to
834 * compensate by uncommenting the string if non-RFC822 compliant
836 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
838 if (*ptz == '+' || *ptz == '-')
840 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
841 && isdigit ((unsigned char) ptz[1]) && isdigit ((unsigned char) ptz[2])
842 && isdigit ((unsigned char) ptz[3]) && isdigit ((unsigned char) ptz[4]))
844 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
845 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
855 tz = bsearch (ptz, TimeZones, sizeof TimeZones/sizeof (struct tz_t),
856 sizeof (struct tz_t),
857 (int (*)(const void *, const void *)) ascii_strcasecmp
858 /* This is safe to do: A pointer to a struct equals
859 * a pointer to its first element*/);
864 zminutes = tz->zminutes;
865 zoccident = tz->zoccident;
868 /* ad hoc support for the European MET (now officially CET) TZ */
869 if (ascii_strcasecmp (t, "MET") == 0)
871 if ((t = strtok (NULL, " \t")) != NULL)
873 if (!ascii_strcasecmp (t, "DST"))
878 tz_offset = zhours * 3600 + zminutes * 60;
880 tz_offset = -tz_offset;
887 if (count < 4) /* don't check for missing timezone */
889 dprint(1,(debugfile, "parse_date(): error parsing date format, using received time\n"));
896 h->zminutes = zminutes;
897 h->zoccident = zoccident;
900 return (mutt_mktime (&tm, 0) + tz_offset);
903 /* extract the first substring that looks like a message-id */
904 static char *extract_message_id (const char *s)
910 if ((s = strchr (s, '<')) == NULL || (p = strchr (s, '>')) == NULL)
912 l = (size_t)(p - s) + 1;
913 r = safe_malloc (l + 1);
919 void mutt_parse_mime_message (CONTEXT *ctx, HEADER *cur)
923 if (cur->content->type != TYPEMESSAGE && cur->content->type != TYPEMULTIPART)
924 return; /* nothing to do */
926 if (cur->content->parts)
927 return; /* The message was parsed earlier. */
929 if ((msg = mx_open_message (ctx, cur->msgno)))
931 mutt_parse_part (msg->fp, cur->content);
934 cur->security = crypt_query (cur->content);
936 mx_close_message (&msg);
940 int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short user_hdrs, short weed,
941 short do_2047, LIST **lastp)
949 switch (ascii_tolower (line[0]))
952 if (ascii_strcasecmp (line+1, "pparently-to") == 0)
954 e->to = rfc822_parse_adrlist (e->to, p);
957 else if (ascii_strcasecmp (line+1, "pparently-from") == 0)
959 e->from = rfc822_parse_adrlist (e->from, p);
965 if (ascii_strcasecmp (line+1, "cc") == 0)
967 e->bcc = rfc822_parse_adrlist (e->bcc, p);
973 if (ascii_strcasecmp (line+1, "c") == 0)
975 e->cc = rfc822_parse_adrlist (e->cc, p);
978 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0)
980 if (ascii_strcasecmp (line+8, "type") == 0)
983 mutt_parse_content_type (p, hdr->content);
986 else if (ascii_strcasecmp (line+8, "transfer-encoding") == 0)
989 hdr->content->encoding = mutt_check_encoding (p);
992 else if (ascii_strcasecmp (line+8, "length") == 0)
996 if ((hdr->content->length = atoi (p)) < 0)
997 hdr->content->length = -1;
1001 else if (ascii_strcasecmp (line+8, "description") == 0)
1005 mutt_str_replace (&hdr->content->description, p);
1006 rfc2047_decode (&hdr->content->description);
1010 else if (ascii_strcasecmp (line+8, "disposition") == 0)
1013 parse_content_disposition (p, hdr->content);
1020 if (!ascii_strcasecmp ("ate", line + 1))
1022 mutt_str_replace (&e->date, p);
1024 hdr->date_sent = mutt_parse_date (p, hdr);
1030 if (!ascii_strcasecmp ("xpires", line + 1) &&
1031 hdr && mutt_parse_date (p, NULL) < time (NULL))
1036 if (!ascii_strcasecmp ("rom", line + 1))
1038 e->from = rfc822_parse_adrlist (e->from, p);
1042 else if (!mutt_strcasecmp (line+1, "ollowup-to"))
1044 if (!e->followup_to)
1046 mutt_remove_trailing_ws (p);
1047 e->followup_to = safe_strdup (mutt_skip_whitespace (p));
1055 if (!ascii_strcasecmp (line+1, "n-reply-to"))
1057 mutt_free_list (&e->in_reply_to);
1058 e->in_reply_to = mutt_parse_references (p, 1);
1064 if (!ascii_strcasecmp (line + 1, "ines"))
1067 hdr->lines = atoi (p);
1070 * HACK - mutt has, for a very short time, produced negative
1071 * Lines header values. Ignore them.
1081 if (!ascii_strcasecmp (line + 1, "ime-version"))
1087 else if (!ascii_strcasecmp (line + 1, "essage-id"))
1089 /* We add a new "Message-Id:" when building a message */
1090 FREE (&e->message_id);
1091 e->message_id = extract_message_id (p);
1094 else if (!ascii_strncasecmp (line + 1, "ail-", 4))
1096 if (!ascii_strcasecmp (line + 5, "reply-to"))
1098 /* override the Reply-To: field */
1099 rfc822_free_address (&e->reply_to);
1100 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1103 else if (!ascii_strcasecmp (line + 5, "followup-to"))
1105 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1113 if (!mutt_strcasecmp (line + 1, "ewsgroups"))
1115 FREE (&e->newsgroups);
1116 mutt_remove_trailing_ws (p);
1117 e->newsgroups = safe_strdup (mutt_skip_whitespace (p));
1124 /* field `Organization:' saves only for pager! */
1125 if (!mutt_strcasecmp (line + 1, "rganization"))
1127 if (!e->organization && mutt_strcasecmp (p, "unknown"))
1128 e->organization = safe_strdup (p);
1133 if (!ascii_strcasecmp (line + 1, "eferences"))
1135 mutt_free_list (&e->references);
1136 e->references = mutt_parse_references (p, 0);
1139 else if (!ascii_strcasecmp (line + 1, "eply-to"))
1141 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1144 else if (!ascii_strcasecmp (line + 1, "eturn-path"))
1146 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1149 else if (!ascii_strcasecmp (line + 1, "eceived"))
1151 if (hdr && !hdr->received)
1153 char *d = strchr (p, ';');
1156 hdr->received = mutt_parse_date (d + 1, NULL);
1162 if (!ascii_strcasecmp (line + 1, "ubject"))
1165 e->subject = safe_strdup (p);
1168 else if (!ascii_strcasecmp (line + 1, "ender"))
1170 e->sender = rfc822_parse_adrlist (e->sender, p);
1173 else if (!ascii_strcasecmp (line + 1, "tatus"))
1196 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1197 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1198 e->supersedes = safe_strdup (p);
1202 if (ascii_strcasecmp (line+1, "o") == 0)
1204 e->to = rfc822_parse_adrlist (e->to, p);
1210 if (ascii_strcasecmp (line+1, "-status") == 0)
1235 else if (ascii_strcasecmp (line+1, "-label") == 0)
1237 e->x_label = safe_strdup(p);
1241 else if (!mutt_strcasecmp (line + 1, "-comment-to"))
1243 if (!e->x_comment_to)
1244 e->x_comment_to = safe_strdup (p);
1247 else if (!mutt_strcasecmp (line + 1, "ref"))
1250 e->xref = safe_strdup (p);
1259 /* Keep track of the user-defined headers */
1260 if (!matched && user_hdrs)
1262 /* restore the original line */
1263 line[strlen (line)] = ':';
1265 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1266 && !mutt_matches_ignore (line, UnIgnore))
1271 last->next = mutt_new_list ();
1275 last = e->userhdrs = mutt_new_list ();
1276 last->data = safe_strdup (line);
1278 rfc2047_decode (&last->data);
1288 /* mutt_read_rfc822_header() -- parses a RFC822 header
1292 * f stream to read from
1294 * hdr header structure of current message (optional).
1296 * user_hdrs If set, store user headers. Used for recall-message and
1299 * weed If this parameter is set and the user has activated the
1300 * $weed option, honor the header weed list for user headers.
1301 * Used for recall-message.
1303 * Returns: newly allocated envelope structure. You should free it by
1304 * mutt_free_envelope() when envelope stay unneeded.
1306 ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
1309 ENVELOPE *e = mutt_new_envelope();
1311 char *line = safe_malloc (LONG_STRING);
1315 size_t linelen = LONG_STRING;
1319 if (hdr->content == NULL)
1321 hdr->content = mutt_new_body ();
1323 /* set the defaults from RFC1521 */
1324 hdr->content->type = TYPETEXT;
1325 hdr->content->subtype = safe_strdup ("plain");
1326 hdr->content->encoding = ENC7BIT;
1327 hdr->content->length = -1;
1329 /* RFC 2183 says this is arbitrary */
1330 hdr->content->disposition = DISPINLINE;
1334 while ((loc = ftell (f)),
1335 *(line = read_rfc822_line (f, line, &linelen)) != 0)
1339 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':')
1341 char return_path[LONG_STRING];
1344 /* some bogus MTAs will quote the original "From " line */
1345 if (mutt_strncmp (">From ", line, 6) == 0)
1346 continue; /* just ignore */
1347 else if (is_from (line, return_path, sizeof (return_path), &t))
1349 /* MH somtimes has the From_ line in the middle of the header! */
1350 if (hdr && !hdr->received)
1351 hdr->received = t - mutt_local_tz (t);
1356 break; /* end of header */
1363 continue; /* skip empty header fields */
1365 matched = mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1373 hdr->content->hdr_offset = hdr->offset;
1374 hdr->content->offset = ftell (f);
1376 /* do RFC2047 decoding */
1377 rfc2047_decode_adrlist (e->from);
1378 rfc2047_decode_adrlist (e->to);
1379 rfc2047_decode_adrlist (e->cc);
1380 rfc2047_decode_adrlist (e->reply_to);
1381 rfc2047_decode_adrlist (e->mail_followup_to);
1382 rfc2047_decode_adrlist (e->return_path);
1383 rfc2047_decode_adrlist (e->sender);
1387 regmatch_t pmatch[1];
1389 rfc2047_decode (&e->subject);
1391 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1392 e->real_subj = e->subject + pmatch[0].rm_eo;
1394 e->real_subj = e->subject;
1397 /* check for missing or invalid date */
1398 if (hdr->date_sent <= 0)
1400 dprint(1,(debugfile,"read_rfc822_header(): no date found, using received time from msg separator\n"));
1401 hdr->date_sent = hdr->received;
1408 ADDRESS *mutt_parse_adrlist (ADDRESS *p, const char *s)
1412 /* check for a simple whitespace separated list of addresses */
1413 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL)
1415 char tmp[HUGE_STRING];
1418 strfcpy (tmp, s, sizeof (tmp));
1420 while ((r = strtok (r, " \t")) != NULL)
1422 p = rfc822_parse_adrlist (p, r);
1427 p = rfc822_parse_adrlist (p, s);