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)
48 if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */
49 (ISSPACE (*line) && !offset)) { /* end of headers */
54 buf += strlen (buf) - 1;
56 /* we did get a full line. remove trailing space */
57 while (ISSPACE (*buf))
58 *buf-- = 0; /* we cannot come beyond line's beginning because
59 * it begins with a non-space */
61 /* check to see if the next line is a continuation line */
62 if ((ch = fgetc (f)) != ' ' && ch != '\t') {
64 return (line); /* next line is a separate header field or EOH */
67 /* eat tabs and spaces from the beginning of the continuation line */
68 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) {
79 safe_realloc (&line, *linelen);
86 LIST *mutt_parse_references (char *s, int in_reply_to)
90 char *o = NULL, *new, *at;
92 while ((s = strtok (s, " \t;")) != NULL) {
94 * some mail clients add other garbage besides message-ids, so do a quick
95 * check to make sure this looks like a valid message-id
96 * some idiotic clients also break their message-ids between lines, deal
97 * with that too (give up if it's more than two lines, though)
104 if (s[n - 1] != '>') {
110 new = safe_strdup (s);
114 if (s[m - 1] == '>') {
115 new = safe_malloc (sizeof (char) * (n + m + 1));
116 strcpy (new, o); /* __STRCPY_CHECKED__ */
117 strcpy (new + n, s); /* __STRCPY_CHECKED__ */
121 /* make sure that this really does look like a message-id.
122 * it should have exactly one @, and if we're looking at
123 * an in-reply-to header, make sure that the part before
124 * the @ has more than eight characters or it's probably
127 if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
128 || (in_reply_to && at - new <= 8))
131 t = (LIST *) safe_malloc (sizeof (LIST));
144 int mutt_check_encoding (const char *c)
146 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit") - 1) == 0)
148 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit") - 1) == 0)
150 else if (ascii_strncasecmp ("binary", c, sizeof ("binary") - 1) == 0)
153 if (ascii_strncasecmp
154 ("quoted-printable", c, sizeof ("quoted-printable") - 1) == 0)
155 return (ENCQUOTEDPRINTABLE);
156 else if (ascii_strncasecmp ("base64", c, sizeof ("base64") - 1) == 0)
158 else if (ascii_strncasecmp ("x-uuencode", c, sizeof ("x-uuencode") - 1) ==
160 return (ENCUUENCODED);
161 #ifdef SUN_ATTACHMENT
162 else if (ascii_strncasecmp ("uuencode", c, sizeof ("uuencode") - 1) == 0)
163 return (ENCUUENCODED);
169 static PARAMETER *parse_parameters (const char *s)
171 PARAMETER *head = 0, *cur = 0, *new;
172 char buffer[LONG_STRING];
176 dprint (2, (debugfile, "parse_parameters: `%s'\n", s));
179 if ((p = strpbrk (s, "=;")) == NULL) {
181 (debugfile, "parse_parameters: malformed parameter: %s\n", s));
185 /* if we hit a ; now the parameter has no value, just skip it */
189 new = mutt_new_parameter ();
191 new->attribute = safe_malloc (i + 1);
192 memcpy (new->attribute, s, i);
193 new->attribute[i] = 0;
195 /* remove whitespace from the end of the attribute name */
196 while (ISSPACE (new->attribute[--i]))
197 new->attribute[i] = 0;
199 s = p + 1; /* skip over the = */
206 for (i = 0; *s && i < sizeof (buffer) - 1; i++, s++) {
207 if (!option (OPTSTRICTMIME)) {
208 /* As iso-2022-* has a characer of '"' with non-ascii state,
210 if (*s == 0x1b && i < sizeof (buffer) - 2) {
211 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
217 if (state_ascii && *s == '"')
220 /* Quote the next character */
230 s++; /* skip over the " */
233 for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1;
239 new->value = safe_strdup (buffer);
241 dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n",
242 new->attribute ? new->attribute : "",
243 new->value ? new->value : ""));
245 /* Add this parameter to the list */
255 (debugfile, "parse_parameters(): parameter with no value: %s\n",
260 /* Find the next parameter */
261 if (*s != ';' && (s = strchr (s, ';')) == NULL)
262 break; /* no more parameters */
267 /* Move past any leading whitespace */
270 while (*s == ';'); /* skip empty parameters */
275 rfc2231_decode_parameters (&head);
279 int mutt_check_mime_type (const char *s)
281 if (ascii_strcasecmp ("text", s) == 0)
283 else if (ascii_strcasecmp ("multipart", s) == 0)
284 return TYPEMULTIPART;
285 #ifdef SUN_ATTACHMENT
286 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
287 return TYPEMULTIPART;
289 else if (ascii_strcasecmp ("application", s) == 0)
290 return TYPEAPPLICATION;
291 else if (ascii_strcasecmp ("message", s) == 0)
293 else if (ascii_strcasecmp ("image", s) == 0)
295 else if (ascii_strcasecmp ("audio", s) == 0)
297 else if (ascii_strcasecmp ("video", s) == 0)
299 else if (ascii_strcasecmp ("model", s) == 0)
305 void mutt_parse_content_type (char *s, BODY * ct)
311 mutt_free_parameter (&ct->parameter);
313 /* First extract any existing parameters */
314 if ((pc = strchr (s, ';')) != NULL) {
316 while (*pc && ISSPACE (*pc))
318 ct->parameter = parse_parameters (pc);
320 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
321 * but if a filename has already been set in the content-disposition,
322 * let that take precedence, and don't set it here */
323 if ((pc = mutt_get_parameter ("name", ct->parameter)) != 0
325 ct->filename = safe_strdup (pc);
327 #ifdef SUN_ATTACHMENT
328 /* this is deep and utter perversion */
329 if ((pc = mutt_get_parameter ("conversions", ct->parameter)) != 0)
330 ct->encoding = mutt_check_encoding (pc);
335 /* Now get the subtype */
336 if ((subtype = strchr (s, '/'))) {
338 for (pc = subtype; *pc && !ISSPACE (*pc) && *pc != ';'; pc++);
340 ct->subtype = safe_strdup (subtype);
343 /* Finally, get the major type */
344 ct->type = mutt_check_mime_type (s);
346 #ifdef SUN_ATTACHMENT
347 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
348 ct->subtype = safe_strdup ("x-sun-attachment");
351 if (ct->type == TYPEOTHER) {
352 ct->xtype = safe_strdup (s);
355 if (ct->subtype == NULL) {
356 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
357 * field, so we can attempt to convert the type to BODY here.
359 if (ct->type == TYPETEXT)
360 ct->subtype = safe_strdup ("plain");
361 else if (ct->type == TYPEAUDIO)
362 ct->subtype = safe_strdup ("basic");
363 else if (ct->type == TYPEMESSAGE)
364 ct->subtype = safe_strdup ("rfc822");
365 else if (ct->type == TYPEOTHER) {
366 char buffer[SHORT_STRING];
368 ct->type = TYPEAPPLICATION;
369 snprintf (buffer, sizeof (buffer), "x-%s", s);
370 ct->subtype = safe_strdup (buffer);
373 ct->subtype = safe_strdup ("x-unknown");
376 /* Default character set for text types. */
377 if (ct->type == TYPETEXT) {
378 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
379 mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
381 mutt_get_first_charset (AssumedCharset),
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 mutt_get_parameter ("filename",
404 (parms = parse_parameters (s)))) != 0)
405 mutt_str_replace (&ct->filename, s);
406 if ((s = mutt_get_parameter ("name", parms)) != 0)
407 ct->form_name = safe_strdup (s);
408 mutt_free_parameter (&parms);
413 * fp stream to read from
415 * digest 1 if reading subparts of a multipart/digest, 0
419 BODY *mutt_read_mime_header (FILE * fp, int digest)
421 BODY *p = mutt_new_body ();
423 char *line = safe_malloc (LONG_STRING);
424 size_t linelen = LONG_STRING;
426 p->hdr_offset = ftell (fp);
428 p->encoding = ENC7BIT; /* default from RFC1521 */
429 p->type = digest ? TYPEMESSAGE : TYPETEXT;
430 p->disposition = DISPINLINE;
432 while (*(line = read_rfc822_line (fp, line, &linelen)) != 0) {
433 /* Find the value of the current header */
434 if ((c = strchr (line, ':'))) {
441 "mutt_read_mime_header(): skipping empty header field: %s\n",
448 (debugfile, "read_mime_header: bogus MIME header: %s\n", line));
452 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)) {
460 mutt_str_replace (&p->description, c);
461 rfc2047_decode (&p->description);
464 #ifdef SUN_ATTACHMENT
465 else if (!ascii_strncasecmp ("x-sun-", line, 6)) {
466 if (!ascii_strcasecmp ("data-type", line + 6))
467 mutt_parse_content_type (c, p);
468 else if (!ascii_strcasecmp ("encoding-info", line + 6))
469 p->encoding = mutt_check_encoding (c);
470 else if (!ascii_strcasecmp ("content-lines", line + 6))
471 mutt_set_parameter ("content-lines", c, &(p->parameter));
472 else if (!ascii_strcasecmp ("data-description", line + 6)) {
473 mutt_str_replace (&p->description, c);
474 rfc2047_decode (&p->description);
479 p->offset = ftell (fp); /* Mark the start of the real data */
480 if (p->type == TYPETEXT && !p->subtype)
481 p->subtype = safe_strdup ("plain");
482 else if (p->type == TYPEMESSAGE && !p->subtype)
483 p->subtype = safe_strdup ("rfc822");
490 void mutt_parse_part (FILE * fp, BODY * b)
496 #ifdef SUN_ATTACHMENT
497 if (!ascii_strcasecmp (b->subtype, "x-sun-attachment"))
501 bound = mutt_get_parameter ("boundary", b->parameter);
503 fseek (fp, b->offset, SEEK_SET);
504 b->parts = mutt_parse_multipart (fp, bound,
505 b->offset + b->length,
506 ascii_strcasecmp ("digest",
512 fseek (fp, b->offset, SEEK_SET);
513 if (mutt_is_message_type (b->type, b->subtype))
514 b->parts = mutt_parse_messageRFC822 (fp, b);
515 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
516 b->parts = mutt_read_mime_header (fp, 0);
526 /* try to recover from parsing error */
529 mutt_str_replace (&b->subtype, "plain");
533 /* parse a MESSAGE/RFC822 body
536 * fp stream to read from
538 * parent structure which contains info about the message/rfc822
541 * NOTE: this assumes that `parent->length' has been set!
544 BODY *mutt_parse_messageRFC822 (FILE * fp, BODY * parent)
548 parent->hdr = mutt_new_header ();
549 parent->hdr->offset = ftell (fp);
550 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
551 msg = parent->hdr->content;
553 /* ignore the length given in the content-length since it could be wrong
554 and we already have the info to calculate the correct length */
555 /* if (msg->length == -1) */
556 msg->length = parent->length - (msg->offset - parent->offset);
558 /* if body of this message is empty, we can end up with a negative length */
562 mutt_parse_part (fp, msg);
566 /* parse a multipart structure
569 * fp stream to read from
571 * boundary body separator
573 * end_off length of the multipart body (used when the final
574 * boundary is missing to avoid reading too far)
576 * digest 1 if reading a multipart/digest, 0 otherwise
579 BODY *mutt_parse_multipart (FILE * fp, const char *boundary, long end_off,
582 #ifdef SUN_ATTACHMENT
585 int blen, len, crlf = 0;
586 char buffer[LONG_STRING];
587 BODY *head = 0, *last = 0, *new = 0;
589 int final = 0; /* did we see the ending boundary? */
592 mutt_error _("multipart message has no boundary parameter!");
597 blen = mutt_strlen (boundary);
598 while (ftell (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL) {
599 len = mutt_strlen (buffer);
601 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
603 if (buffer[0] == '-' && buffer[1] == '-' &&
604 mutt_strncmp (buffer + 2, boundary, blen) == 0) {
606 last->length = ftell (fp) - last->offset - len - 1 - crlf;
607 if (last->parts && last->parts->length == 0)
608 last->parts->length =
609 ftell (fp) - last->parts->offset - len - 1 - crlf;
610 /* if the body is empty, we can end up with a -1 length */
611 if (last->length < 0)
615 /* Remove any trailing whitespace, up to the length of the boundary */
616 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
619 /* Check for the end boundary */
620 if (mutt_strcmp (buffer + blen + 2, "--") == 0) {
622 break; /* done parsing */
624 else if (buffer[2 + blen] == 0) {
625 new = mutt_read_mime_header (fp, digest);
627 #ifdef SUN_ATTACHMENT
628 if (mutt_get_parameter ("content-lines", new->parameter)) {
630 atoi (mutt_get_parameter ("content-lines", new->parameter));
632 if (ftell (fp) >= end_off
633 || fgets (buffer, LONG_STRING, fp) == NULL)
639 * Consistency checking - catch
640 * bad attachment end boundaries
643 if (new->offset > end_off) {
644 mutt_free_body (&new);
657 /* in case of missing end boundary, set the length to something reasonable */
658 if (last && last->length == 0 && !final)
659 last->length = end_off - last->offset;
661 /* parse recursive MIME parts */
662 for (last = head; last; last = last->next)
663 mutt_parse_part (fp, last);
668 static const char *uncomment_timezone (char *buf, size_t buflen,
675 return tz; /* no need to do anything */
678 if ((p = strpbrk (tz, " )")) == NULL)
681 if (len > buflen - 1)
683 memcpy (buf, tz, len);
690 unsigned char zhours;
691 unsigned char zminutes;
692 unsigned char zoccident; /* west of UTC? */
695 "aat", 1, 0, 1}, /* Atlantic Africa Time */
697 "adt", 4, 0, 0}, /* Arabia DST */
699 "ast", 3, 0, 0}, /* Arabia */
700 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
702 "bst", 1, 0, 0}, /* British DST */
704 "cat", 1, 0, 0}, /* Central Africa */
707 "cest", 2, 0, 0}, /* Central Europe DST */
709 "cet", 1, 0, 0}, /* Central Europe */
712 /*{ "cst", 8, 0, 0 }, *//* China */
713 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
715 "eat", 3, 0, 0}, /* East Africa */
718 "eest", 3, 0, 0}, /* Eastern Europe DST */
720 "eet", 2, 0, 0}, /* Eastern Europe */
722 "egst", 0, 0, 0}, /* Eastern Greenland DST */
724 "egt", 1, 0, 1}, /* Eastern Greenland */
728 "gst", 4, 0, 0}, /* Presian Gulf */
730 "hkt", 8, 0, 0}, /* Hong Kong */
732 "ict", 7, 0, 0}, /* Indochina */
734 "idt", 3, 0, 0}, /* Israel DST */
736 "ist", 2, 0, 0}, /* Israel */
737 /*{ "ist", 5, 30, 0 }, *//* India */
739 "jst", 9, 0, 0}, /* Japan */
741 "kst", 9, 0, 0}, /* Korea */
744 "met", 1, 0, 0}, /* this is now officially CET */
746 "msd", 4, 0, 0}, /* Moscow DST */
748 "msk", 3, 0, 0}, /* Moscow */
751 "nzdt", 13, 0, 0}, /* New Zealand DST */
753 "nzst", 12, 0, 0}, /* New Zealand */
757 "sat", 2, 0, 0}, /* South Africa */
759 "smt", 4, 0, 0}, /* Seychelles */
761 "sst", 11, 0, 1}, /* Samoa */
762 /*{ "sst", 8, 0, 0 }, *//* Singapore */
765 "wat", 0, 0, 0}, /* West Africa */
767 "west", 1, 0, 0}, /* Western Europe DST */
769 "wet", 0, 0, 0}, /* Western Europe */
771 "wgst", 2, 0, 1}, /* Western Greenland DST */
773 "wgt", 3, 0, 1}, /* Western Greenland */
775 "wst", 8, 0, 0}, /* Western Australia */
778 /* parses a date string in RFC822 format:
780 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
782 * This routine assumes that `h' has been initialized to 0. the `timezone'
783 * field is optional, defaulting to +0000 if missing.
785 time_t mutt_parse_date (const char *s, HEADER * h)
797 char tzstr[SHORT_STRING];
798 char scratch[SHORT_STRING];
800 /* Don't modify our argument. Fixed-size buffer is ok here since
801 * the date format imposes a natural limit.
804 strfcpy (scratch, s, sizeof (scratch));
806 /* kill the day of the week, if it exists. */
807 if ((t = strchr (scratch, ',')))
813 memset (&tm, 0, sizeof (tm));
815 while ((t = strtok (t, " \t")) != NULL) {
817 case 0: /* day of the month */
818 if (!isdigit ((unsigned char) *t))
820 tm.tm_mday = atoi (t);
825 case 1: /* month of the year */
826 if ((i = mutt_check_month (t)) < 0)
832 tm.tm_year = atoi (t);
835 else if (tm.tm_year >= 1900)
839 case 3: /* time of day */
840 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3);
841 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
845 (debugfile, "parse_date: could not process time format: %s\n",
854 case 4: /* timezone */
855 /* sometimes we see things like (MST) or (-0700) so attempt to
856 * compensate by uncommenting the string if non-RFC822 compliant
858 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
860 if (*ptz == '+' || *ptz == '-') {
861 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
862 && isdigit ((unsigned char) ptz[1])
863 && isdigit ((unsigned char) ptz[2])
864 && isdigit ((unsigned char) ptz[3])
865 && isdigit ((unsigned char) ptz[4])) {
866 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
867 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*/ );
884 zminutes = tz->zminutes;
885 zoccident = tz->zoccident;
888 /* ad hoc support for the European MET (now officially CET) TZ */
889 if (ascii_strcasecmp (t, "MET") == 0) {
890 if ((t = strtok (NULL, " \t")) != NULL) {
891 if (!ascii_strcasecmp (t, "DST"))
896 tz_offset = zhours * 3600 + zminutes * 60;
898 tz_offset = -tz_offset;
905 if (count < 4) { /* don't check for missing timezone */
908 "parse_date(): error parsing date format, using received time\n"));
914 h->zminutes = zminutes;
915 h->zoccident = zoccident;
918 return (mutt_mktime (&tm, 0) + tz_offset);
921 /* extract the first substring that looks like a message-id */
922 static char *extract_message_id (const char *s)
928 if ((s = strchr (s, '<')) == NULL || (p = strchr (s, '>')) == NULL)
930 l = (size_t) (p - s) + 1;
931 r = safe_malloc (l + 1);
937 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
941 if (cur->content->type != TYPEMESSAGE
942 && cur->content->type != TYPEMULTIPART)
943 return; /* nothing to do */
945 if (cur->content->parts)
946 return; /* The message was parsed earlier. */
948 if ((msg = mx_open_message (ctx, cur->msgno))) {
949 mutt_parse_part (msg->fp, cur->content);
952 cur->security = crypt_query (cur->content);
954 mx_close_message (&msg);
958 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
959 short user_hdrs, short weed, short do_2047,
968 switch (ascii_tolower (line[0])) {
970 if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
971 e->to = rfc822_parse_adrlist (e->to, p);
974 else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
975 e->from = rfc822_parse_adrlist (e->from, p);
981 if (ascii_strcasecmp (line + 1, "cc") == 0) {
982 e->bcc = rfc822_parse_adrlist (e->bcc, p);
988 if (ascii_strcasecmp (line + 1, "c") == 0) {
989 e->cc = rfc822_parse_adrlist (e->cc, p);
992 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
993 if (ascii_strcasecmp (line + 8, "type") == 0) {
995 mutt_parse_content_type (p, hdr->content);
998 else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
1000 hdr->content->encoding = mutt_check_encoding (p);
1003 else if (ascii_strcasecmp (line + 8, "length") == 0) {
1005 if ((hdr->content->length = atoi (p)) < 0)
1006 hdr->content->length = -1;
1010 else if (ascii_strcasecmp (line + 8, "description") == 0) {
1012 mutt_str_replace (&hdr->content->description, p);
1013 rfc2047_decode (&hdr->content->description);
1017 else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
1019 parse_content_disposition (p, hdr->content);
1026 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)) {
1042 e->from = rfc822_parse_adrlist (e->from, p);
1043 /* don't leave from info NULL if there's an invalid address (or
1044 * whatever) in From: field; mutt would just display it as empty
1045 * and mark mail/(esp.) news article as your own. aaargh! this
1046 * bothered me for _years_ */
1048 e->from = rfc822_new_address ();
1049 e->from->personal = safe_strdup (line + 6);
1054 else if (!mutt_strcasecmp (line + 1, "ollowup-to")) {
1055 if (!e->followup_to) {
1056 mutt_remove_trailing_ws (p);
1057 e->followup_to = safe_strdup (mutt_skip_whitespace (p));
1065 if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
1066 mutt_free_list (&e->in_reply_to);
1067 e->in_reply_to = mutt_parse_references (p, 1);
1073 if (!ascii_strcasecmp (line + 1, "ines")) {
1075 hdr->lines = atoi (p);
1078 * HACK - mutt has, for a very short time, produced negative
1079 * Lines header values. Ignore them.
1087 else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
1088 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1089 if (strncmp (p, "NO", 2)) {
1092 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) {
1099 FREE (&e->list_post);
1100 e->list_post = mutt_substrdup (beg, end);
1110 if (!ascii_strcasecmp (line + 1, "ime-version")) {
1115 else if (!ascii_strcasecmp (line + 1, "essage-id")) {
1116 /* We add a new "Message-Id:" when building a message */
1117 FREE (&e->message_id);
1118 e->message_id = extract_message_id (p);
1121 else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
1122 if (!ascii_strcasecmp (line + 5, "reply-to")) {
1123 /* override the Reply-To: field */
1124 rfc822_free_address (&e->reply_to);
1125 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1128 else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1129 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1137 if (!mutt_strcasecmp (line + 1, "ewsgroups")) {
1138 FREE (&e->newsgroups);
1139 mutt_remove_trailing_ws (p);
1140 e->newsgroups = safe_strdup (mutt_skip_whitespace (p));
1147 /* field `Organization:' saves only for pager! */
1148 if (!mutt_strcasecmp (line + 1, "rganization")) {
1149 if (!e->organization && mutt_strcasecmp (p, "unknown"))
1150 e->organization = safe_strdup (p);
1155 if (!ascii_strcasecmp (line + 1, "eferences")) {
1156 mutt_free_list (&e->references);
1157 e->references = mutt_parse_references (p, 0);
1160 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")) {
1165 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1168 else if (!ascii_strcasecmp (line + 1, "eceived")) {
1169 if (hdr && !hdr->received) {
1170 char *d = strchr (p, ';');
1173 hdr->received = mutt_parse_date (d + 1, NULL);
1179 if (!ascii_strcasecmp (line + 1, "ubject")) {
1181 e->subject = safe_strdup (p);
1184 else if (!ascii_strcasecmp (line + 1, "ender")) {
1185 e->sender = rfc822_parse_adrlist (e->sender, p);
1188 else if (!ascii_strcasecmp (line + 1, "tatus")) {
1207 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1208 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1209 e->supersedes = safe_strdup (p);
1213 if (ascii_strcasecmp (line + 1, "o") == 0) {
1214 e->to = rfc822_parse_adrlist (e->to, p);
1220 if (ascii_strcasecmp (line + 1, "-status") == 0) {
1241 else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1242 e->x_label = safe_strdup (p);
1246 else if (!mutt_strcasecmp (line + 1, "-comment-to")) {
1247 if (!e->x_comment_to)
1248 e->x_comment_to = safe_strdup (p);
1251 else if (!mutt_strcasecmp (line + 1, "ref")) {
1253 e->xref = safe_strdup (p);
1262 /* Keep track of the user-defined headers */
1263 if (!matched && user_hdrs) {
1264 /* restore the original line */
1265 line[strlen (line)] = ':';
1267 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1268 && !mutt_matches_ignore (line, UnIgnore))
1272 last->next = mutt_new_list ();
1276 last = e->userhdrs = mutt_new_list ();
1277 last->data = safe_strdup (line);
1279 rfc2047_decode (&last->data);
1289 /* mutt_read_rfc822_header() -- parses a RFC822 header
1293 * f stream to read from
1295 * hdr header structure of current message (optional).
1297 * user_hdrs If set, store user headers. Used for recall-message and
1300 * weed If this parameter is set and the user has activated the
1301 * $weed option, honor the header weed list for user headers.
1302 * Used for recall-message.
1304 * Returns: newly allocated envelope structure. You should free it by
1305 * mutt_free_envelope() when envelope stay unneeded.
1307 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1310 ENVELOPE *e = mutt_new_envelope ();
1312 char *line = safe_malloc (LONG_STRING);
1316 size_t linelen = LONG_STRING;
1317 char buf[LONG_STRING + 1];
1320 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) {
1338 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1339 char return_path[LONG_STRING];
1342 /* some bogus MTAs will quote the original "From " line */
1343 if (mutt_strncmp (">From ", line, 6) == 0)
1344 continue; /* just ignore */
1345 else if (is_from (line, return_path, sizeof (return_path), &t)) {
1346 /* MH somtimes has the From_ line in the middle of the header! */
1347 if (hdr && !hdr->received)
1348 hdr->received = t - mutt_local_tz (t);
1353 break; /* end of header */
1358 if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1359 if (!mutt_match_rx_list (line, NoSpamList)) {
1361 /* if spam tag already exists, figure out how to amend it */
1362 if (e->spam && *buf) {
1363 /* If SpamSep defined, append with separator */
1365 mutt_buffer_addstr (e->spam, SpamSep);
1366 mutt_buffer_addstr (e->spam, buf);
1369 /* else overwrite */
1371 e->spam->dptr = e->spam->data;
1372 *e->spam->dptr = '\0';
1373 mutt_buffer_addstr (e->spam, buf);
1377 /* spam tag is new, and match expr is non-empty; copy */
1378 else if (!e->spam && *buf) {
1379 e->spam = mutt_buffer_from (NULL, buf);
1382 /* match expr is empty; plug in null string if no existing tag */
1383 else if (!e->spam) {
1384 e->spam = mutt_buffer_from (NULL, "");
1387 if (e->spam && e->spam->data)
1388 dprint (5, (debugfile, "p822: spam = %s\n", e->spam->data));
1396 continue; /* skip empty header fields */
1399 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1406 hdr->content->hdr_offset = hdr->offset;
1407 hdr->content->offset = ftell (f);
1409 /* do RFC2047 decoding */
1410 rfc2047_decode_adrlist (e->from);
1411 rfc2047_decode_adrlist (e->to);
1412 rfc2047_decode_adrlist (e->cc);
1413 rfc2047_decode_adrlist (e->bcc);
1414 rfc2047_decode_adrlist (e->reply_to);
1415 rfc2047_decode_adrlist (e->mail_followup_to);
1416 rfc2047_decode_adrlist (e->return_path);
1417 rfc2047_decode_adrlist (e->sender);
1420 regmatch_t pmatch[1];
1422 rfc2047_decode (&e->subject);
1424 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1425 e->real_subj = e->subject + pmatch[0].rm_eo;
1427 e->real_subj = e->subject;
1430 /* check for missing or invalid date */
1431 if (hdr->date_sent <= 0) {
1434 "read_rfc822_header(): no date found, using received time from msg separator\n"));
1435 hdr->date_sent = hdr->received;
1442 ADDRESS *mutt_parse_adrlist (ADDRESS * p, const char *s)
1446 /* check for a simple whitespace separated list of addresses */
1447 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1448 char tmp[HUGE_STRING];
1451 strfcpy (tmp, s, sizeof (tmp));
1453 while ((r = strtok (r, " \t")) != NULL) {
1454 p = rfc822_parse_adrlist (p, r);
1459 p = rfc822_parse_adrlist (p, s);