2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
15 #include "mutt_regex.h"
20 #include "mutt_crypt.h"
28 /* Reads an arbitrarily long header field, and looks ahead for continuation
29 * lines. ``line'' must point to a dynamically allocated string; it is
30 * increased if more space is required to fit the whole line.
32 static char *read_rfc822_line (FILE * f, char *line, size_t * linelen)
39 if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */
40 (ISSPACE (*line) && !offset)) { /* end of headers */
45 buf += mutt_strlen (buf) - 1;
47 /* we did get a full line. remove trailing space */
48 while (ISSPACE (*buf))
49 *buf-- = 0; /* we cannot come beyond line's beginning because
50 * it begins with a non-space */
52 /* check to see if the next line is a continuation line */
53 if ((ch = fgetc (f)) != ' ' && ch != '\t') {
55 return (line); /* next line is a separate header field or EOH */
58 /* eat tabs and spaces from the beginning of the continuation line */
59 while ((ch = fgetc (f)) == ' ' || ch == '\t');
61 *++buf = ' '; /* string is still terminated because we removed
62 at least one whitespace char above */
67 if (*linelen < offset + STRING) {
70 safe_realloc (&line, *linelen);
77 LIST *mutt_parse_references (char *s, int in_reply_to)
81 char *o = NULL, *new, *at;
83 while ((s = strtok (s, " \t;")) != NULL) {
85 * some mail clients add other garbage besides message-ids, so do a quick
86 * check to make sure this looks like a valid message-id
87 * some idiotic clients also break their message-ids between lines, deal
88 * with that too (give up if it's more than two lines, though)
95 if (s[n - 1] != '>') {
101 new = safe_strdup (s);
105 if (s[m - 1] == '>') {
106 new = safe_malloc (sizeof (char) * (n + m + 1));
107 strcpy (new, o); /* __STRCPY_CHECKED__ */
108 strcpy (new + n, s); /* __STRCPY_CHECKED__ */
112 /* make sure that this really does look like a message-id.
113 * it should have exactly one @, and if we're looking at
114 * an in-reply-to header, make sure that the part before
115 * the @ has more than eight characters or it's probably
118 if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
119 || (in_reply_to && at - new <= 8))
122 t = (LIST *) safe_malloc (sizeof (LIST));
135 int mutt_check_encoding (const char *c)
137 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit") - 1) == 0)
139 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit") - 1) == 0)
141 else if (ascii_strncasecmp ("binary", c, sizeof ("binary") - 1) == 0)
144 if (ascii_strncasecmp
145 ("quoted-printable", c, sizeof ("quoted-printable") - 1) == 0)
146 return (ENCQUOTEDPRINTABLE);
147 else if (ascii_strncasecmp ("base64", c, sizeof ("base64") - 1) == 0)
149 else if (ascii_strncasecmp ("x-uuencode", c, sizeof ("x-uuencode") - 1) ==
151 return (ENCUUENCODED);
152 #ifdef SUN_ATTACHMENT
153 else if (ascii_strncasecmp ("uuencode", c, sizeof ("uuencode") - 1) == 0)
154 return (ENCUUENCODED);
160 static PARAMETER *parse_parameters (const char *s)
162 PARAMETER *head = 0, *cur = 0, *new;
163 char buffer[LONG_STRING];
167 dprint (2, (debugfile, "parse_parameters: `%s'\n", s));
170 if ((p = strpbrk (s, "=;")) == NULL) {
172 (debugfile, "parse_parameters: malformed parameter: %s\n", s));
176 /* if we hit a ; now the parameter has no value, just skip it */
180 new = mutt_new_parameter ();
182 new->attribute = safe_malloc (i + 1);
183 memcpy (new->attribute, s, i);
184 new->attribute[i] = 0;
186 /* remove whitespace from the end of the attribute name */
187 while (ISSPACE (new->attribute[--i]))
188 new->attribute[i] = 0;
190 s = p + 1; /* skip over the = */
197 for (i = 0; *s && i < sizeof (buffer) - 1; i++, s++) {
198 if (!option (OPTSTRICTMIME)) {
199 /* As iso-2022-* has a characer of '"' with non-ascii state,
201 if (*s == 0x1b && i < sizeof (buffer) - 2) {
202 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
208 if (state_ascii && *s == '"')
211 /* Quote the next character */
221 s++; /* skip over the " */
224 for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1;
230 new->value = safe_strdup (buffer);
232 dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n",
233 new->attribute ? new->attribute : "",
234 new->value ? new->value : ""));
236 /* Add this parameter to the list */
246 (debugfile, "parse_parameters(): parameter with no value: %s\n",
251 /* Find the next parameter */
252 if (*s != ';' && (s = strchr (s, ';')) == NULL)
253 break; /* no more parameters */
258 /* Move past any leading whitespace */
261 while (*s == ';'); /* skip empty parameters */
266 rfc2231_decode_parameters (&head);
270 int mutt_check_mime_type (const char *s)
272 if (ascii_strcasecmp ("text", s) == 0)
274 else if (ascii_strcasecmp ("multipart", s) == 0)
275 return TYPEMULTIPART;
276 #ifdef SUN_ATTACHMENT
277 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
278 return TYPEMULTIPART;
280 else if (ascii_strcasecmp ("application", s) == 0)
281 return TYPEAPPLICATION;
282 else if (ascii_strcasecmp ("message", s) == 0)
284 else if (ascii_strcasecmp ("image", s) == 0)
286 else if (ascii_strcasecmp ("audio", s) == 0)
288 else if (ascii_strcasecmp ("video", s) == 0)
290 else if (ascii_strcasecmp ("model", s) == 0)
296 void mutt_parse_content_type (char *s, BODY * ct)
302 mutt_free_parameter (&ct->parameter);
304 /* First extract any existing parameters */
305 if ((pc = strchr (s, ';')) != NULL) {
307 while (*pc && ISSPACE (*pc))
309 ct->parameter = parse_parameters (pc);
311 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
312 * but if a filename has already been set in the content-disposition,
313 * let that take precedence, and don't set it here */
314 if ((pc = mutt_get_parameter ("name", ct->parameter)) != 0
316 ct->filename = safe_strdup (pc);
318 #ifdef SUN_ATTACHMENT
319 /* this is deep and utter perversion */
320 if ((pc = mutt_get_parameter ("conversions", ct->parameter)) != 0)
321 ct->encoding = mutt_check_encoding (pc);
326 /* Now get the subtype */
327 if ((subtype = strchr (s, '/'))) {
329 for (pc = subtype; *pc && !ISSPACE (*pc) && *pc != ';'; pc++);
331 ct->subtype = safe_strdup (subtype);
334 /* Finally, get the major type */
335 ct->type = mutt_check_mime_type (s);
337 #ifdef SUN_ATTACHMENT
338 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
339 ct->subtype = safe_strdup ("x-sun-attachment");
342 if (ct->type == TYPEOTHER) {
343 ct->xtype = safe_strdup (s);
346 if (ct->subtype == NULL) {
347 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
348 * field, so we can attempt to convert the type to BODY here.
350 if (ct->type == TYPETEXT)
351 ct->subtype = safe_strdup ("plain");
352 else if (ct->type == TYPEAUDIO)
353 ct->subtype = safe_strdup ("basic");
354 else if (ct->type == TYPEMESSAGE)
355 ct->subtype = safe_strdup ("rfc822");
356 else if (ct->type == TYPEOTHER) {
357 char buffer[SHORT_STRING];
359 ct->type = TYPEAPPLICATION;
360 snprintf (buffer, sizeof (buffer), "x-%s", s);
361 ct->subtype = safe_strdup (buffer);
364 ct->subtype = safe_strdup ("x-unknown");
367 /* Default character set for text types. */
368 if (ct->type == TYPETEXT) {
369 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
370 mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
372 mutt_get_first_charset (AssumedCharset),
378 static void parse_content_disposition (char *s, BODY * ct)
382 if (!ascii_strncasecmp ("inline", s, 6))
383 ct->disposition = DISPINLINE;
384 else if (!ascii_strncasecmp ("form-data", s, 9))
385 ct->disposition = DISPFORMDATA;
387 ct->disposition = DISPATTACH;
389 /* Check to see if a default filename was given */
390 if ((s = strchr (s, ';')) != NULL) {
394 mutt_get_parameter ("filename",
395 (parms = parse_parameters (s)))) != 0)
396 mutt_str_replace (&ct->filename, s);
397 if ((s = mutt_get_parameter ("name", parms)) != 0)
398 ct->form_name = safe_strdup (s);
399 mutt_free_parameter (&parms);
404 * fp stream to read from
406 * digest 1 if reading subparts of a multipart/digest, 0
410 BODY *mutt_read_mime_header (FILE * fp, int digest)
412 BODY *p = mutt_new_body ();
414 char *line = safe_malloc (LONG_STRING);
415 size_t linelen = LONG_STRING;
417 p->hdr_offset = ftell (fp);
419 p->encoding = ENC7BIT; /* default from RFC1521 */
420 p->type = digest ? TYPEMESSAGE : TYPETEXT;
421 p->disposition = DISPINLINE;
423 while (*(line = read_rfc822_line (fp, line, &linelen)) != 0) {
424 /* Find the value of the current header */
425 if ((c = strchr (line, ':'))) {
432 "mutt_read_mime_header(): skipping empty header field: %s\n",
439 (debugfile, "read_mime_header: bogus MIME header: %s\n", line));
443 if (!ascii_strncasecmp ("content-", line, 8)) {
444 if (!ascii_strcasecmp ("type", line + 8))
445 mutt_parse_content_type (c, p);
446 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
447 p->encoding = mutt_check_encoding (c);
448 else if (!ascii_strcasecmp ("disposition", line + 8))
449 parse_content_disposition (c, p);
450 else if (!ascii_strcasecmp ("description", line + 8)) {
451 mutt_str_replace (&p->description, c);
452 rfc2047_decode (&p->description);
455 #ifdef SUN_ATTACHMENT
456 else if (!ascii_strncasecmp ("x-sun-", line, 6)) {
457 if (!ascii_strcasecmp ("data-type", line + 6))
458 mutt_parse_content_type (c, p);
459 else if (!ascii_strcasecmp ("encoding-info", line + 6))
460 p->encoding = mutt_check_encoding (c);
461 else if (!ascii_strcasecmp ("content-lines", line + 6))
462 mutt_set_parameter ("content-lines", c, &(p->parameter));
463 else if (!ascii_strcasecmp ("data-description", line + 6)) {
464 mutt_str_replace (&p->description, c);
465 rfc2047_decode (&p->description);
470 p->offset = ftell (fp); /* Mark the start of the real data */
471 if (p->type == TYPETEXT && !p->subtype)
472 p->subtype = safe_strdup ("plain");
473 else if (p->type == TYPEMESSAGE && !p->subtype)
474 p->subtype = safe_strdup ("rfc822");
481 void mutt_parse_part (FILE * fp, BODY * b)
487 #ifdef SUN_ATTACHMENT
488 if (!ascii_strcasecmp (b->subtype, "x-sun-attachment"))
492 bound = mutt_get_parameter ("boundary", b->parameter);
494 fseek (fp, b->offset, SEEK_SET);
495 b->parts = mutt_parse_multipart (fp, bound,
496 b->offset + b->length,
497 ascii_strcasecmp ("digest",
503 fseek (fp, b->offset, SEEK_SET);
504 if (mutt_is_message_type (b->type, b->subtype))
505 b->parts = mutt_parse_messageRFC822 (fp, b);
506 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
507 b->parts = mutt_read_mime_header (fp, 0);
517 /* try to recover from parsing error */
520 mutt_str_replace (&b->subtype, "plain");
524 /* parse a MESSAGE/RFC822 body
527 * fp stream to read from
529 * parent structure which contains info about the message/rfc822
532 * NOTE: this assumes that `parent->length' has been set!
535 BODY *mutt_parse_messageRFC822 (FILE * fp, BODY * parent)
539 parent->hdr = mutt_new_header ();
540 parent->hdr->offset = ftell (fp);
541 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
542 msg = parent->hdr->content;
544 /* ignore the length given in the content-length since it could be wrong
545 and we already have the info to calculate the correct length */
546 /* if (msg->length == -1) */
547 msg->length = parent->length - (msg->offset - parent->offset);
549 /* if body of this message is empty, we can end up with a negative length */
553 mutt_parse_part (fp, msg);
557 /* parse a multipart structure
560 * fp stream to read from
562 * boundary body separator
564 * end_off length of the multipart body (used when the final
565 * boundary is missing to avoid reading too far)
567 * digest 1 if reading a multipart/digest, 0 otherwise
570 BODY *mutt_parse_multipart (FILE * fp, const char *boundary, long end_off,
573 #ifdef SUN_ATTACHMENT
576 int blen, len, crlf = 0;
577 char buffer[LONG_STRING];
578 BODY *head = 0, *last = 0, *new = 0;
580 int final = 0; /* did we see the ending boundary? */
583 mutt_error _("multipart message has no boundary parameter!");
588 blen = mutt_strlen (boundary);
589 while (ftell (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL) {
590 len = mutt_strlen (buffer);
592 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
594 if (buffer[0] == '-' && buffer[1] == '-' &&
595 mutt_strncmp (buffer + 2, boundary, blen) == 0) {
597 last->length = ftell (fp) - last->offset - len - 1 - crlf;
598 if (last->parts && last->parts->length == 0)
599 last->parts->length =
600 ftell (fp) - last->parts->offset - len - 1 - crlf;
601 /* if the body is empty, we can end up with a -1 length */
602 if (last->length < 0)
606 /* Remove any trailing whitespace, up to the length of the boundary */
607 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
610 /* Check for the end boundary */
611 if (mutt_strcmp (buffer + blen + 2, "--") == 0) {
613 break; /* done parsing */
615 else if (buffer[2 + blen] == 0) {
616 new = mutt_read_mime_header (fp, digest);
618 #ifdef SUN_ATTACHMENT
619 if (mutt_get_parameter ("content-lines", new->parameter)) {
621 atoi (mutt_get_parameter ("content-lines", new->parameter));
623 if (ftell (fp) >= end_off
624 || fgets (buffer, LONG_STRING, fp) == NULL)
630 * Consistency checking - catch
631 * bad attachment end boundaries
634 if (new->offset > end_off) {
635 mutt_free_body (&new);
648 /* in case of missing end boundary, set the length to something reasonable */
649 if (last && last->length == 0 && !final)
650 last->length = end_off - last->offset;
652 /* parse recursive MIME parts */
653 for (last = head; last; last = last->next)
654 mutt_parse_part (fp, last);
659 static const char *uncomment_timezone (char *buf, size_t buflen,
666 return tz; /* no need to do anything */
669 if ((p = strpbrk (tz, " )")) == NULL)
672 if (len > buflen - 1)
674 memcpy (buf, tz, len);
681 unsigned char zhours;
682 unsigned char zminutes;
683 unsigned char zoccident; /* west of UTC? */
686 "aat", 1, 0, 1}, /* Atlantic Africa Time */
688 "adt", 4, 0, 0}, /* Arabia DST */
690 "ast", 3, 0, 0}, /* Arabia */
691 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
693 "bst", 1, 0, 0}, /* British DST */
695 "cat", 1, 0, 0}, /* Central Africa */
698 "cest", 2, 0, 0}, /* Central Europe DST */
700 "cet", 1, 0, 0}, /* Central Europe */
703 /*{ "cst", 8, 0, 0 }, *//* China */
704 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
706 "eat", 3, 0, 0}, /* East Africa */
709 "eest", 3, 0, 0}, /* Eastern Europe DST */
711 "eet", 2, 0, 0}, /* Eastern Europe */
713 "egst", 0, 0, 0}, /* Eastern Greenland DST */
715 "egt", 1, 0, 1}, /* Eastern Greenland */
719 "gst", 4, 0, 0}, /* Presian Gulf */
721 "hkt", 8, 0, 0}, /* Hong Kong */
723 "ict", 7, 0, 0}, /* Indochina */
725 "idt", 3, 0, 0}, /* Israel DST */
727 "ist", 2, 0, 0}, /* Israel */
728 /*{ "ist", 5, 30, 0 }, *//* India */
730 "jst", 9, 0, 0}, /* Japan */
732 "kst", 9, 0, 0}, /* Korea */
735 "met", 1, 0, 0}, /* this is now officially CET */
737 "msd", 4, 0, 0}, /* Moscow DST */
739 "msk", 3, 0, 0}, /* Moscow */
742 "nzdt", 13, 0, 0}, /* New Zealand DST */
744 "nzst", 12, 0, 0}, /* New Zealand */
748 "sat", 2, 0, 0}, /* South Africa */
750 "smt", 4, 0, 0}, /* Seychelles */
752 "sst", 11, 0, 1}, /* Samoa */
753 /*{ "sst", 8, 0, 0 }, *//* Singapore */
756 "wat", 0, 0, 0}, /* West Africa */
758 "west", 1, 0, 0}, /* Western Europe DST */
760 "wet", 0, 0, 0}, /* Western Europe */
762 "wgst", 2, 0, 1}, /* Western Greenland DST */
764 "wgt", 3, 0, 1}, /* Western Greenland */
766 "wst", 8, 0, 0}, /* Western Australia */
769 /* parses a date string in RFC822 format:
771 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
773 * This routine assumes that `h' has been initialized to 0. the `timezone'
774 * field is optional, defaulting to +0000 if missing.
776 time_t mutt_parse_date (const char *s, HEADER * h)
788 char tzstr[SHORT_STRING];
789 char scratch[SHORT_STRING];
791 /* Don't modify our argument. Fixed-size buffer is ok here since
792 * the date format imposes a natural limit.
795 strfcpy (scratch, s, sizeof (scratch));
797 /* kill the day of the week, if it exists. */
798 if ((t = strchr (scratch, ',')))
804 memset (&tm, 0, sizeof (tm));
806 while ((t = strtok (t, " \t")) != NULL) {
808 case 0: /* day of the month */
809 if (!isdigit ((unsigned char) *t))
811 tm.tm_mday = atoi (t);
816 case 1: /* month of the year */
817 if ((i = mutt_check_month (t)) < 0)
823 tm.tm_year = atoi (t);
826 else if (tm.tm_year >= 1900)
830 case 3: /* time of day */
831 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3);
832 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
836 (debugfile, "parse_date: could not process time format: %s\n",
845 case 4: /* timezone */
846 /* sometimes we see things like (MST) or (-0700) so attempt to
847 * compensate by uncommenting the string if non-RFC822 compliant
849 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
851 if (*ptz == '+' || *ptz == '-') {
852 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
853 && isdigit ((unsigned char) ptz[1])
854 && isdigit ((unsigned char) ptz[2])
855 && isdigit ((unsigned char) ptz[3])
856 && isdigit ((unsigned char) ptz[4])) {
857 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
858 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
867 tz = bsearch (ptz, TimeZones, sizeof TimeZones / sizeof (struct tz_t),
868 sizeof (struct tz_t),
869 (int (*)(const void *, const void *)) ascii_strcasecmp
870 /* This is safe to do: A pointer to a struct equals
871 * a pointer to its first element*/ );
875 zminutes = tz->zminutes;
876 zoccident = tz->zoccident;
879 /* ad hoc support for the European MET (now officially CET) TZ */
880 if (ascii_strcasecmp (t, "MET") == 0) {
881 if ((t = strtok (NULL, " \t")) != NULL) {
882 if (!ascii_strcasecmp (t, "DST"))
887 tz_offset = zhours * 3600 + zminutes * 60;
889 tz_offset = -tz_offset;
896 if (count < 4) { /* don't check for missing timezone */
899 "parse_date(): error parsing date format, using received time\n"));
905 h->zminutes = zminutes;
906 h->zoccident = zoccident;
909 return (mutt_mktime (&tm, 0) + tz_offset);
912 /* extract the first substring that looks like a message-id */
913 static char *extract_message_id (const char *s)
919 if ((s = strchr (s, '<')) == NULL || (p = strchr (s, '>')) == NULL)
921 l = (size_t) (p - s) + 1;
922 r = safe_malloc (l + 1);
928 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
932 if (cur->content->type != TYPEMESSAGE
933 && cur->content->type != TYPEMULTIPART)
934 return; /* nothing to do */
936 if (cur->content->parts)
937 return; /* The message was parsed earlier. */
939 if ((msg = mx_open_message (ctx, cur->msgno))) {
940 mutt_parse_part (msg->fp, cur->content);
943 cur->security = crypt_query (cur->content);
945 mx_close_message (&msg);
949 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
950 short user_hdrs, short weed, short do_2047,
959 switch (ascii_tolower (line[0])) {
961 if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
962 e->to = rfc822_parse_adrlist (e->to, p);
965 else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
966 e->from = rfc822_parse_adrlist (e->from, p);
972 if (ascii_strcasecmp (line + 1, "cc") == 0) {
973 e->bcc = rfc822_parse_adrlist (e->bcc, p);
979 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) {
984 if (ascii_strcasecmp (line + 8, "type") == 0) {
986 mutt_parse_content_type (p, hdr->content);
989 else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
991 hdr->content->encoding = mutt_check_encoding (p);
994 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) {
1003 mutt_str_replace (&hdr->content->description, p);
1004 rfc2047_decode (&hdr->content->description);
1008 else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
1010 parse_content_disposition (p, hdr->content);
1017 if (!ascii_strcasecmp ("ate", line + 1)) {
1018 mutt_str_replace (&e->date, p);
1020 hdr->date_sent = mutt_parse_date (p, hdr);
1026 if (!ascii_strcasecmp ("xpires", line + 1) &&
1027 hdr && mutt_parse_date (p, NULL) < time (NULL))
1032 if (!ascii_strcasecmp ("rom", line + 1)) {
1033 e->from = rfc822_parse_adrlist (e->from, p);
1034 /* don't leave from info NULL if there's an invalid address (or
1035 * whatever) in From: field; mutt would just display it as empty
1036 * and mark mail/(esp.) news article as your own. aaargh! this
1037 * bothered me for _years_ */
1039 e->from = rfc822_new_address ();
1040 e->from->personal = safe_strdup (line + 6);
1045 else if (!mutt_strcasecmp (line + 1, "ollowup-to")) {
1046 if (!e->followup_to) {
1047 mutt_remove_trailing_ws (p);
1048 e->followup_to = safe_strdup (mutt_skip_whitespace (p));
1056 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")) {
1066 hdr->lines = atoi (p);
1069 * HACK - mutt has, for a very short time, produced negative
1070 * Lines header values. Ignore them.
1078 else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
1079 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1080 if (strncmp (p, "NO", 2)) {
1083 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
1085 if (!(end = strchr (beg, '>')))
1088 /* Take the first mailto URL */
1089 if (url_check_scheme (beg) == U_MAILTO) {
1090 FREE (&e->list_post);
1091 e->list_post = mutt_substrdup (beg, end);
1101 if (!ascii_strcasecmp (line + 1, "ime-version")) {
1106 else if (!ascii_strcasecmp (line + 1, "essage-id")) {
1107 /* We add a new "Message-Id:" when building a message */
1108 FREE (&e->message_id);
1109 e->message_id = extract_message_id (p);
1112 else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
1113 if (!ascii_strcasecmp (line + 5, "reply-to")) {
1114 /* override the Reply-To: field */
1115 rfc822_free_address (&e->reply_to);
1116 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1119 else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1120 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1128 if (!mutt_strcasecmp (line + 1, "ewsgroups")) {
1129 FREE (&e->newsgroups);
1130 mutt_remove_trailing_ws (p);
1131 e->newsgroups = safe_strdup (mutt_skip_whitespace (p));
1138 /* field `Organization:' saves only for pager! */
1139 if (!mutt_strcasecmp (line + 1, "rganization")) {
1140 if (!e->organization && mutt_strcasecmp (p, "unknown"))
1141 e->organization = safe_strdup (p);
1146 if (!ascii_strcasecmp (line + 1, "eferences")) {
1147 mutt_free_list (&e->references);
1148 e->references = mutt_parse_references (p, 0);
1151 else if (!ascii_strcasecmp (line + 1, "eply-to")) {
1152 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1155 else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
1156 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1159 else if (!ascii_strcasecmp (line + 1, "eceived")) {
1160 if (hdr && !hdr->received) {
1161 char *d = strchr (p, ';');
1164 hdr->received = mutt_parse_date (d + 1, NULL);
1170 if (!ascii_strcasecmp (line + 1, "ubject")) {
1172 e->subject = safe_strdup (p);
1175 else if (!ascii_strcasecmp (line + 1, "ender")) {
1176 e->sender = rfc822_parse_adrlist (e->sender, p);
1179 else if (!ascii_strcasecmp (line + 1, "tatus")) {
1198 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1199 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1200 e->supersedes = safe_strdup (p);
1204 if (ascii_strcasecmp (line + 1, "o") == 0) {
1205 e->to = rfc822_parse_adrlist (e->to, p);
1211 if (ascii_strcasecmp (line + 1, "-status") == 0) {
1232 else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1233 e->x_label = safe_strdup (p);
1237 else if (!mutt_strcasecmp (line + 1, "-comment-to")) {
1238 if (!e->x_comment_to)
1239 e->x_comment_to = safe_strdup (p);
1242 else if (!mutt_strcasecmp (line + 1, "ref")) {
1244 e->xref = safe_strdup (p);
1253 /* Keep track of the user-defined headers */
1254 if (!matched && user_hdrs) {
1255 /* restore the original line */
1256 line[mutt_strlen (line)] = ':';
1258 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1259 && !mutt_matches_ignore (line, UnIgnore))
1263 last->next = mutt_new_list ();
1267 last = e->userhdrs = mutt_new_list ();
1268 last->data = safe_strdup (line);
1270 rfc2047_decode (&last->data);
1280 /* mutt_read_rfc822_header() -- parses a RFC822 header
1284 * f stream to read from
1286 * hdr header structure of current message (optional).
1288 * user_hdrs If set, store user headers. Used for recall-message and
1291 * weed If this parameter is set and the user has activated the
1292 * $weed option, honor the header weed list for user headers.
1293 * Used for recall-message.
1295 * Returns: newly allocated envelope structure. You should free it by
1296 * mutt_free_envelope() when envelope stay unneeded.
1298 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1301 ENVELOPE *e = mutt_new_envelope ();
1303 char *line = safe_malloc (LONG_STRING);
1307 size_t linelen = LONG_STRING;
1308 char buf[LONG_STRING + 1];
1311 if (hdr->content == NULL) {
1312 hdr->content = mutt_new_body ();
1314 /* set the defaults from RFC1521 */
1315 hdr->content->type = TYPETEXT;
1316 hdr->content->subtype = safe_strdup ("plain");
1317 hdr->content->encoding = ENC7BIT;
1318 hdr->content->length = -1;
1320 /* RFC 2183 says this is arbitrary */
1321 hdr->content->disposition = DISPINLINE;
1325 while ((loc = ftell (f)),
1326 *(line = read_rfc822_line (f, line, &linelen)) != 0) {
1329 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1330 char return_path[LONG_STRING];
1333 /* some bogus MTAs will quote the original "From " line */
1334 if (mutt_strncmp (">From ", line, 6) == 0)
1335 continue; /* just ignore */
1336 else if (is_from (line, return_path, sizeof (return_path), &t)) {
1337 /* MH somtimes has the From_ line in the middle of the header! */
1338 if (hdr && !hdr->received)
1339 hdr->received = t - mutt_local_tz (t);
1344 break; /* end of header */
1349 if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1350 if (!mutt_match_rx_list (line, NoSpamList)) {
1352 /* if spam tag already exists, figure out how to amend it */
1353 if (e->spam && *buf) {
1354 /* If SpamSep defined, append with separator */
1356 mutt_buffer_addstr (e->spam, SpamSep);
1357 mutt_buffer_addstr (e->spam, buf);
1360 /* else overwrite */
1362 e->spam->dptr = e->spam->data;
1363 *e->spam->dptr = '\0';
1364 mutt_buffer_addstr (e->spam, buf);
1368 /* spam tag is new, and match expr is non-empty; copy */
1369 else if (!e->spam && *buf) {
1370 e->spam = mutt_buffer_from (NULL, buf);
1373 /* match expr is empty; plug in null string if no existing tag */
1374 else if (!e->spam) {
1375 e->spam = mutt_buffer_from (NULL, "");
1378 if (e->spam && e->spam->data)
1379 dprint (5, (debugfile, "p822: spam = %s\n", e->spam->data));
1387 continue; /* skip empty header fields */
1390 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1397 hdr->content->hdr_offset = hdr->offset;
1398 hdr->content->offset = ftell (f);
1400 /* do RFC2047 decoding */
1401 rfc2047_decode_adrlist (e->from);
1402 rfc2047_decode_adrlist (e->to);
1403 rfc2047_decode_adrlist (e->cc);
1404 rfc2047_decode_adrlist (e->bcc);
1405 rfc2047_decode_adrlist (e->reply_to);
1406 rfc2047_decode_adrlist (e->mail_followup_to);
1407 rfc2047_decode_adrlist (e->return_path);
1408 rfc2047_decode_adrlist (e->sender);
1411 regmatch_t pmatch[1];
1413 rfc2047_decode (&e->subject);
1415 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1416 e->real_subj = e->subject + pmatch[0].rm_eo;
1418 e->real_subj = e->subject;
1421 /* check for missing or invalid date */
1422 if (hdr->date_sent <= 0) {
1425 "read_rfc822_header(): no date found, using received time from msg separator\n"));
1426 hdr->date_sent = hdr->received;
1433 ADDRESS *mutt_parse_adrlist (ADDRESS * p, const char *s)
1437 /* check for a simple whitespace separated list of addresses */
1438 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1439 char tmp[HUGE_STRING];
1442 strfcpy (tmp, s, sizeof (tmp));
1444 while ((r = strtok (r, " \t")) != NULL) {
1445 p = rfc822_parse_adrlist (p, r);
1450 p = rfc822_parse_adrlist (p, s);