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.
19 #include "mutt_crypt.h"
26 #include "lib/debug.h"
33 /* Reads an arbitrarily long header field, and looks ahead for continuation
34 * lines. ``line'' must point to a dynamically allocated string; it is
35 * increased if more space is required to fit the whole line.
37 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 */
50 buf += safe_strlen (buf) - 1;
52 /* we did get a full line. remove trailing space */
53 while (ISSPACE (*buf))
54 *buf-- = 0; /* we cannot come beyond line's beginning because
55 * it begins with a non-space */
57 /* check to see if the next line is a continuation line */
58 if ((ch = fgetc (f)) != ' ' && ch != '\t') {
60 return (line); /* next line is a separate header field or EOH */
63 /* eat tabs and spaces from the beginning of the continuation line */
64 while ((ch = fgetc (f)) == ' ' || ch == '\t');
66 *++buf = ' '; /* string is still terminated because we removed
67 at least one whitespace char above */
72 if (*linelen < offset + STRING) {
75 safe_realloc (&line, *linelen);
82 LIST *mutt_parse_references (char *s, int in_reply_to)
86 char *o = NULL, *new, *at;
88 while ((s = strtok (s, " \t;")) != NULL) {
90 * some mail clients add other garbage besides message-ids, so do a quick
91 * check to make sure this looks like a valid message-id
92 * some idiotic clients also break their message-ids between lines, deal
93 * with that too (give up if it's more than two lines, though)
100 if (s[n - 1] != '>') {
106 new = safe_strdup (s);
110 if (s[m - 1] == '>') {
111 new = safe_malloc (sizeof (char) * (n + m + 1));
112 strcpy (new, o); /* __STRCPY_CHECKED__ */
113 strcpy (new + n, s); /* __STRCPY_CHECKED__ */
117 /* make sure that this really does look like a message-id.
118 * it should have exactly one @, and if we're looking at
119 * an in-reply-to header, make sure that the part before
120 * the @ has more than eight characters or it's probably
123 if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
124 || (in_reply_to && at - new <= 8))
127 t = (LIST *) safe_malloc (sizeof (LIST));
140 int mutt_check_encoding (const char *c)
142 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit") - 1) == 0)
144 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit") - 1) == 0)
146 else if (ascii_strncasecmp ("binary", c, sizeof ("binary") - 1) == 0)
149 if (ascii_strncasecmp
150 ("quoted-printable", c, sizeof ("quoted-printable") - 1) == 0)
151 return (ENCQUOTEDPRINTABLE);
152 else if (ascii_strncasecmp ("base64", c, sizeof ("base64") - 1) == 0)
154 else if (ascii_strncasecmp ("x-uuencode", c, sizeof ("x-uuencode") - 1) ==
156 return (ENCUUENCODED);
157 #ifdef SUN_ATTACHMENT
158 else if (ascii_strncasecmp ("uuencode", c, sizeof ("uuencode") - 1) == 0)
159 return (ENCUUENCODED);
165 static PARAMETER *parse_parameters (const char *s)
167 PARAMETER *head = 0, *cur = 0, *new;
168 char buffer[LONG_STRING];
172 debug_print (2, ("`%s'\n", s));
175 if ((p = strpbrk (s, "=;")) == NULL) {
176 debug_print (1, ("malformed parameter: %s\n", s));
180 /* if we hit a ; now the parameter has no value, just skip it */
184 new = mutt_new_parameter ();
186 new->attribute = safe_malloc (i + 1);
187 memcpy (new->attribute, s, i);
188 new->attribute[i] = 0;
190 /* remove whitespace from the end of the attribute name */
191 while (ISSPACE (new->attribute[--i]))
192 new->attribute[i] = 0;
194 s = p + 1; /* skip over the = */
201 for (i = 0; *s && i < sizeof (buffer) - 1; i++, s++) {
202 if (!option (OPTSTRICTMIME)) {
203 /* As iso-2022-* has a characer of '"' with non-ascii state,
205 if (*s == 0x1b && i < sizeof (buffer) - 2) {
206 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
212 if (state_ascii && *s == '"')
215 /* Quote the next character */
225 s++; /* skip over the " */
228 for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1;
234 new->value = safe_strdup (buffer);
236 debug_print (2, ("`%s' = `%s'\n", new->attribute ? new->attribute : "",
237 new->value ? new->value : ""));
239 /* Add this parameter to the list */
248 debug_print (1, ("parameter with no value: %s\n", s));
252 /* Find the next parameter */
253 if (*s != ';' && (s = strchr (s, ';')) == NULL)
254 break; /* no more parameters */
259 /* Move past any leading whitespace */
262 while (*s == ';'); /* skip empty parameters */
267 rfc2231_decode_parameters (&head);
271 int mutt_check_mime_type (const char *s)
273 if (ascii_strcasecmp ("text", s) == 0)
275 else if (ascii_strcasecmp ("multipart", s) == 0)
276 return TYPEMULTIPART;
277 #ifdef SUN_ATTACHMENT
278 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
279 return TYPEMULTIPART;
281 else if (ascii_strcasecmp ("application", s) == 0)
282 return TYPEAPPLICATION;
283 else if (ascii_strcasecmp ("message", s) == 0)
285 else if (ascii_strcasecmp ("image", s) == 0)
287 else if (ascii_strcasecmp ("audio", s) == 0)
289 else if (ascii_strcasecmp ("video", s) == 0)
291 else if (ascii_strcasecmp ("model", s) == 0)
297 void mutt_parse_content_type (char *s, BODY * ct)
303 mutt_free_parameter (&ct->parameter);
305 /* First extract any existing parameters */
306 if ((pc = strchr (s, ';')) != NULL) {
308 while (*pc && ISSPACE (*pc))
310 ct->parameter = parse_parameters (pc);
312 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
313 * but if a filename has already been set in the content-disposition,
314 * let that take precedence, and don't set it here */
315 if ((pc = mutt_get_parameter ("name", ct->parameter)) != 0
317 ct->filename = safe_strdup (pc);
319 #ifdef SUN_ATTACHMENT
320 /* this is deep and utter perversion */
321 if ((pc = mutt_get_parameter ("conversions", ct->parameter)) != 0)
322 ct->encoding = mutt_check_encoding (pc);
327 /* Now get the subtype */
328 if ((subtype = strchr (s, '/'))) {
330 for (pc = subtype; *pc && !ISSPACE (*pc) && *pc != ';'; pc++);
332 ct->subtype = safe_strdup (subtype);
335 /* Finally, get the major type */
336 ct->type = mutt_check_mime_type (s);
338 #ifdef SUN_ATTACHMENT
339 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
340 ct->subtype = safe_strdup ("x-sun-attachment");
343 if (ct->type == TYPEOTHER) {
344 ct->xtype = safe_strdup (s);
347 if (ct->subtype == NULL) {
348 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
349 * field, so we can attempt to convert the type to BODY here.
351 if (ct->type == TYPETEXT)
352 ct->subtype = safe_strdup ("plain");
353 else if (ct->type == TYPEAUDIO)
354 ct->subtype = safe_strdup ("basic");
355 else if (ct->type == TYPEMESSAGE)
356 ct->subtype = safe_strdup ("rfc822");
357 else if (ct->type == TYPEOTHER) {
358 char buffer[SHORT_STRING];
360 ct->type = TYPEAPPLICATION;
361 snprintf (buffer, sizeof (buffer), "x-%s", s);
362 ct->subtype = safe_strdup (buffer);
365 ct->subtype = safe_strdup ("x-unknown");
368 /* Default character set for text types. */
369 if (ct->type == TYPETEXT) {
370 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
371 mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
373 mutt_get_first_charset (AssumedCharset),
379 static void parse_content_disposition (char *s, BODY * ct)
383 if (!ascii_strncasecmp ("inline", s, 6))
384 ct->disposition = DISPINLINE;
385 else if (!ascii_strncasecmp ("form-data", s, 9))
386 ct->disposition = DISPFORMDATA;
388 ct->disposition = DISPATTACH;
390 /* Check to see if a default filename was given */
391 if ((s = strchr (s, ';')) != NULL) {
395 mutt_get_parameter ("filename",
396 (parms = parse_parameters (s)))) != 0)
397 str_replace (&ct->filename, s);
398 if ((s = mutt_get_parameter ("name", parms)) != 0)
399 ct->form_name = safe_strdup (s);
400 mutt_free_parameter (&parms);
405 * fp stream to read from
407 * digest 1 if reading subparts of a multipart/digest, 0
411 BODY *mutt_read_mime_header (FILE * fp, int digest)
413 BODY *p = mutt_new_body ();
415 char *line = safe_malloc (LONG_STRING);
416 size_t linelen = LONG_STRING;
418 p->hdr_offset = ftell (fp);
420 p->encoding = ENC7BIT; /* default from RFC1521 */
421 p->type = digest ? TYPEMESSAGE : TYPETEXT;
422 p->disposition = DISPINLINE;
424 while (*(line = read_rfc822_line (fp, line, &linelen)) != 0) {
425 /* Find the value of the current header */
426 if ((c = strchr (line, ':'))) {
431 debug_print (1, ("skipping empty header field: %s\n", line));
436 debug_print (1, ("bogus MIME header: %s\n", line));
440 if (!ascii_strncasecmp ("content-", line, 8)) {
441 if (!ascii_strcasecmp ("type", line + 8))
442 mutt_parse_content_type (c, p);
443 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
444 p->encoding = mutt_check_encoding (c);
445 else if (!ascii_strcasecmp ("disposition", line + 8))
446 parse_content_disposition (c, p);
447 else if (!ascii_strcasecmp ("description", line + 8)) {
448 str_replace (&p->description, c);
449 rfc2047_decode (&p->description);
452 #ifdef SUN_ATTACHMENT
453 else if (!ascii_strncasecmp ("x-sun-", line, 6)) {
454 if (!ascii_strcasecmp ("data-type", line + 6))
455 mutt_parse_content_type (c, p);
456 else if (!ascii_strcasecmp ("encoding-info", line + 6))
457 p->encoding = mutt_check_encoding (c);
458 else if (!ascii_strcasecmp ("content-lines", line + 6))
459 mutt_set_parameter ("content-lines", c, &(p->parameter));
460 else if (!ascii_strcasecmp ("data-description", line + 6)) {
461 str_replace (&p->description, c);
462 rfc2047_decode (&p->description);
467 p->offset = ftell (fp); /* Mark the start of the real data */
468 if (p->type == TYPETEXT && !p->subtype)
469 p->subtype = safe_strdup ("plain");
470 else if (p->type == TYPEMESSAGE && !p->subtype)
471 p->subtype = safe_strdup ("rfc822");
478 void mutt_parse_part (FILE * fp, BODY * b)
484 #ifdef SUN_ATTACHMENT
485 if (!ascii_strcasecmp (b->subtype, "x-sun-attachment"))
489 bound = mutt_get_parameter ("boundary", b->parameter);
491 fseek (fp, b->offset, SEEK_SET);
492 b->parts = mutt_parse_multipart (fp, bound,
493 b->offset + b->length,
494 ascii_strcasecmp ("digest",
500 fseek (fp, b->offset, SEEK_SET);
501 if (mutt_is_message_type (b->type, b->subtype))
502 b->parts = mutt_parse_messageRFC822 (fp, b);
503 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
504 b->parts = mutt_read_mime_header (fp, 0);
514 /* try to recover from parsing error */
517 str_replace (&b->subtype, "plain");
521 /* parse a MESSAGE/RFC822 body
524 * fp stream to read from
526 * parent structure which contains info about the message/rfc822
529 * NOTE: this assumes that `parent->length' has been set!
532 BODY *mutt_parse_messageRFC822 (FILE * fp, BODY * parent)
536 parent->hdr = mutt_new_header ();
537 parent->hdr->offset = ftell (fp);
538 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
539 msg = parent->hdr->content;
541 /* ignore the length given in the content-length since it could be wrong
542 and we already have the info to calculate the correct length */
543 /* if (msg->length == -1) */
544 msg->length = parent->length - (msg->offset - parent->offset);
546 /* if body of this message is empty, we can end up with a negative length */
550 mutt_parse_part (fp, msg);
554 /* parse a multipart structure
557 * fp stream to read from
559 * boundary body separator
561 * end_off length of the multipart body (used when the final
562 * boundary is missing to avoid reading too far)
564 * digest 1 if reading a multipart/digest, 0 otherwise
567 BODY *mutt_parse_multipart (FILE * fp, const char *boundary, long end_off,
570 #ifdef SUN_ATTACHMENT
573 int blen, len, crlf = 0;
574 char buffer[LONG_STRING];
575 BODY *head = 0, *last = 0, *new = 0;
577 int final = 0; /* did we see the ending boundary? */
580 mutt_error _("multipart message has no boundary parameter!");
585 blen = safe_strlen (boundary);
586 while (ftell (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL) {
587 len = safe_strlen (buffer);
589 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
591 if (buffer[0] == '-' && buffer[1] == '-' &&
592 safe_strncmp (buffer + 2, boundary, blen) == 0) {
594 last->length = ftell (fp) - last->offset - len - 1 - crlf;
595 if (last->parts && last->parts->length == 0)
596 last->parts->length =
597 ftell (fp) - last->parts->offset - len - 1 - crlf;
598 /* if the body is empty, we can end up with a -1 length */
599 if (last->length < 0)
603 /* Remove any trailing whitespace, up to the length of the boundary */
604 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
607 /* Check for the end boundary */
608 if (safe_strcmp (buffer + blen + 2, "--") == 0) {
610 break; /* done parsing */
612 else if (buffer[2 + blen] == 0) {
613 new = mutt_read_mime_header (fp, digest);
615 #ifdef SUN_ATTACHMENT
616 if (mutt_get_parameter ("content-lines", new->parameter)) {
618 atoi (mutt_get_parameter ("content-lines", new->parameter));
620 if (ftell (fp) >= end_off
621 || fgets (buffer, LONG_STRING, fp) == NULL)
627 * Consistency checking - catch
628 * bad attachment end boundaries
631 if (new->offset > end_off) {
632 mutt_free_body (&new);
645 /* in case of missing end boundary, set the length to something reasonable */
646 if (last && last->length == 0 && !final)
647 last->length = end_off - last->offset;
649 /* parse recursive MIME parts */
650 for (last = head; last; last = last->next)
651 mutt_parse_part (fp, last);
656 static const char *uncomment_timezone (char *buf, size_t buflen,
663 return tz; /* no need to do anything */
666 if ((p = strpbrk (tz, " )")) == NULL)
669 if (len > buflen - 1)
671 memcpy (buf, tz, len);
678 unsigned char zhours;
679 unsigned char zminutes;
680 unsigned char zoccident; /* west of UTC? */
683 "aat", 1, 0, 1}, /* Atlantic Africa Time */
685 "adt", 4, 0, 0}, /* Arabia DST */
687 "ast", 3, 0, 0}, /* Arabia */
688 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
690 "bst", 1, 0, 0}, /* British DST */
692 "cat", 1, 0, 0}, /* Central Africa */
695 "cest", 2, 0, 0}, /* Central Europe DST */
697 "cet", 1, 0, 0}, /* Central Europe */
700 /*{ "cst", 8, 0, 0 }, *//* China */
701 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
703 "eat", 3, 0, 0}, /* East Africa */
706 "eest", 3, 0, 0}, /* Eastern Europe DST */
708 "eet", 2, 0, 0}, /* Eastern Europe */
710 "egst", 0, 0, 0}, /* Eastern Greenland DST */
712 "egt", 1, 0, 1}, /* Eastern Greenland */
716 "gst", 4, 0, 0}, /* Presian Gulf */
718 "hkt", 8, 0, 0}, /* Hong Kong */
720 "ict", 7, 0, 0}, /* Indochina */
722 "idt", 3, 0, 0}, /* Israel DST */
724 "ist", 2, 0, 0}, /* Israel */
725 /*{ "ist", 5, 30, 0 }, *//* India */
727 "jst", 9, 0, 0}, /* Japan */
729 "kst", 9, 0, 0}, /* Korea */
732 "met", 1, 0, 0}, /* this is now officially CET */
734 "msd", 4, 0, 0}, /* Moscow DST */
736 "msk", 3, 0, 0}, /* Moscow */
739 "nzdt", 13, 0, 0}, /* New Zealand DST */
741 "nzst", 12, 0, 0}, /* New Zealand */
745 "sat", 2, 0, 0}, /* South Africa */
747 "smt", 4, 0, 0}, /* Seychelles */
749 "sst", 11, 0, 1}, /* Samoa */
750 /*{ "sst", 8, 0, 0 }, *//* Singapore */
753 "wat", 0, 0, 0}, /* West Africa */
755 "west", 1, 0, 0}, /* Western Europe DST */
757 "wet", 0, 0, 0}, /* Western Europe */
759 "wgst", 2, 0, 1}, /* Western Greenland DST */
761 "wgt", 3, 0, 1}, /* Western Greenland */
763 "wst", 8, 0, 0}, /* Western Australia */
766 /* parses a date string in RFC822 format:
768 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
770 * This routine assumes that `h' has been initialized to 0. the `timezone'
771 * field is optional, defaulting to +0000 if missing.
773 time_t mutt_parse_date (const char *s, HEADER * h)
785 char tzstr[SHORT_STRING];
786 char scratch[SHORT_STRING];
788 /* Don't modify our argument. Fixed-size buffer is ok here since
789 * the date format imposes a natural limit.
792 strfcpy (scratch, s, sizeof (scratch));
794 /* kill the day of the week, if it exists. */
795 if ((t = strchr (scratch, ',')))
801 memset (&tm, 0, sizeof (tm));
803 while ((t = strtok (t, " \t")) != NULL) {
805 case 0: /* day of the month */
806 if (!isdigit ((unsigned char) *t))
808 tm.tm_mday = atoi (t);
813 case 1: /* month of the year */
814 if ((i = mutt_check_month (t)) < 0)
820 tm.tm_year = atoi (t);
823 else if (tm.tm_year >= 1900)
827 case 3: /* time of day */
828 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3);
829 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
832 debug_print (1, ("could not process time format: %s\n", t));
840 case 4: /* timezone */
841 /* sometimes we see things like (MST) or (-0700) so attempt to
842 * compensate by uncommenting the string if non-RFC822 compliant
844 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
846 if (*ptz == '+' || *ptz == '-') {
847 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
848 && isdigit ((unsigned char) ptz[1])
849 && isdigit ((unsigned char) ptz[2])
850 && isdigit ((unsigned char) ptz[3])
851 && isdigit ((unsigned char) ptz[4])) {
852 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
853 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
862 tz = bsearch (ptz, TimeZones, sizeof TimeZones / sizeof (struct tz_t),
863 sizeof (struct tz_t),
864 (int (*)(const void *, const void *)) ascii_strcasecmp
865 /* This is safe to do: A pointer to a struct equals
866 * a pointer to its first element*/ );
870 zminutes = tz->zminutes;
871 zoccident = tz->zoccident;
874 /* ad hoc support for the European MET (now officially CET) TZ */
875 if (ascii_strcasecmp (t, "MET") == 0) {
876 if ((t = strtok (NULL, " \t")) != NULL) {
877 if (!ascii_strcasecmp (t, "DST"))
882 tz_offset = zhours * 3600 + zminutes * 60;
884 tz_offset = -tz_offset;
891 if (count < 4) { /* don't check for missing timezone */
892 debug_print (1, ("error parsing date format, using received time\n"));
898 h->zminutes = zminutes;
899 h->zoccident = zoccident;
902 return (mutt_mktime (&tm, 0) + tz_offset);
905 /* extract the first substring that looks like a message-id */
906 static char *extract_message_id (const char *s)
912 if ((s = strchr (s, '<')) == NULL || (p = strchr (s, '>')) == NULL)
914 l = (size_t) (p - s) + 1;
915 r = safe_malloc (l + 1);
921 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
925 if (cur->content->type != TYPEMESSAGE
926 && cur->content->type != TYPEMULTIPART)
927 return; /* nothing to do */
929 if (cur->content->parts)
930 return; /* The message was parsed earlier. */
932 if ((msg = mx_open_message (ctx, cur->msgno))) {
933 mutt_parse_part (msg->fp, cur->content);
936 cur->security = crypt_query (cur->content);
938 mx_close_message (&msg);
942 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
943 short user_hdrs, short weed, short do_2047,
952 switch (ascii_tolower (line[0])) {
954 if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
955 e->to = rfc822_parse_adrlist (e->to, p);
958 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) {
966 e->bcc = rfc822_parse_adrlist (e->bcc, p);
972 if (ascii_strcasecmp (line + 1, "c") == 0) {
973 e->cc = rfc822_parse_adrlist (e->cc, p);
976 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
977 if (ascii_strcasecmp (line + 8, "type") == 0) {
979 mutt_parse_content_type (p, hdr->content);
982 else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
984 hdr->content->encoding = mutt_check_encoding (p);
987 else if (ascii_strcasecmp (line + 8, "length") == 0) {
989 if ((hdr->content->length = atoi (p)) < 0)
990 hdr->content->length = -1;
994 else if (ascii_strcasecmp (line + 8, "description") == 0) {
996 str_replace (&hdr->content->description, p);
997 rfc2047_decode (&hdr->content->description);
1001 else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
1003 parse_content_disposition (p, hdr->content);
1010 if (!ascii_strcasecmp ("ate", line + 1)) {
1011 str_replace (&e->date, p);
1013 hdr->date_sent = mutt_parse_date (p, hdr);
1019 if (!ascii_strcasecmp ("xpires", line + 1) &&
1020 hdr && mutt_parse_date (p, NULL) < time (NULL))
1025 if (!ascii_strcasecmp ("rom", line + 1)) {
1026 e->from = rfc822_parse_adrlist (e->from, p);
1027 /* don't leave from info NULL if there's an invalid address (or
1028 * whatever) in From: field; mutt would just display it as empty
1029 * and mark mail/(esp.) news article as your own. aaargh! this
1030 * bothered me for _years_ */
1032 e->from = rfc822_new_address ();
1033 e->from->personal = safe_strdup (line + 6);
1038 else if (!safe_strcasecmp (line + 1, "ollowup-to")) {
1039 if (!e->followup_to) {
1040 mutt_remove_trailing_ws (p);
1041 e->followup_to = safe_strdup (mutt_skip_whitespace (p));
1049 if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
1050 mutt_free_list (&e->in_reply_to);
1051 e->in_reply_to = mutt_parse_references (p, 1);
1057 if (!ascii_strcasecmp (line + 1, "ines")) {
1059 hdr->lines = atoi (p);
1062 * HACK - mutt has, for a very short time, produced negative
1063 * Lines header values. Ignore them.
1071 else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
1072 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1073 if (strncmp (p, "NO", 2)) {
1076 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
1078 if (!(end = strchr (beg, '>')))
1081 /* Take the first mailto URL */
1082 if (url_check_scheme (beg) == U_MAILTO) {
1083 FREE (&e->list_post);
1084 e->list_post = str_substrdup (beg, end);
1094 if (!ascii_strcasecmp (line + 1, "ime-version")) {
1099 else if (!ascii_strcasecmp (line + 1, "essage-id")) {
1100 /* We add a new "Message-Id:" when building a message */
1101 FREE (&e->message_id);
1102 e->message_id = extract_message_id (p);
1105 else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
1106 if (!ascii_strcasecmp (line + 5, "reply-to")) {
1107 /* override the Reply-To: field */
1108 rfc822_free_address (&e->reply_to);
1109 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1112 else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1113 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1121 if (!safe_strcasecmp (line + 1, "ewsgroups")) {
1122 FREE (&e->newsgroups);
1123 mutt_remove_trailing_ws (p);
1124 e->newsgroups = safe_strdup (mutt_skip_whitespace (p));
1131 /* field `Organization:' saves only for pager! */
1132 if (!safe_strcasecmp (line + 1, "rganization")) {
1133 if (!e->organization && safe_strcasecmp (p, "unknown"))
1134 e->organization = safe_strdup (p);
1139 if (!ascii_strcasecmp (line + 1, "eferences")) {
1140 mutt_free_list (&e->references);
1141 e->references = mutt_parse_references (p, 0);
1144 else if (!ascii_strcasecmp (line + 1, "eply-to")) {
1145 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1148 else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
1149 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1152 else if (!ascii_strcasecmp (line + 1, "eceived")) {
1153 if (hdr && !hdr->received) {
1154 char *d = strchr (p, ';');
1157 hdr->received = mutt_parse_date (d + 1, NULL);
1163 if (!ascii_strcasecmp (line + 1, "ubject")) {
1165 e->subject = safe_strdup (p);
1168 else if (!ascii_strcasecmp (line + 1, "ender")) {
1169 e->sender = rfc822_parse_adrlist (e->sender, p);
1172 else if (!ascii_strcasecmp (line + 1, "tatus")) {
1191 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1192 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1193 e->supersedes = safe_strdup (p);
1197 if (ascii_strcasecmp (line + 1, "o") == 0) {
1198 e->to = rfc822_parse_adrlist (e->to, p);
1204 if (ascii_strcasecmp (line + 1, "-status") == 0) {
1225 else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1226 e->x_label = safe_strdup (p);
1230 else if (!safe_strcasecmp (line + 1, "-comment-to")) {
1231 if (!e->x_comment_to)
1232 e->x_comment_to = safe_strdup (p);
1235 else if (!safe_strcasecmp (line + 1, "ref")) {
1237 e->xref = safe_strdup (p);
1246 /* Keep track of the user-defined headers */
1247 if (!matched && user_hdrs) {
1248 /* restore the original line */
1249 line[safe_strlen (line)] = ':';
1251 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1252 && !mutt_matches_ignore (line, UnIgnore))
1256 last->next = mutt_new_list ();
1260 last = e->userhdrs = mutt_new_list ();
1261 last->data = safe_strdup (line);
1263 rfc2047_decode (&last->data);
1273 /* mutt_read_rfc822_header() -- parses a RFC822 header
1277 * f stream to read from
1279 * hdr header structure of current message (optional).
1281 * user_hdrs If set, store user headers. Used for recall-message and
1284 * weed If this parameter is set and the user has activated the
1285 * $weed option, honor the header weed list for user headers.
1286 * Used for recall-message.
1288 * Returns: newly allocated envelope structure. You should free it by
1289 * mutt_free_envelope() when envelope stay unneeded.
1291 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1294 ENVELOPE *e = mutt_new_envelope ();
1296 char *line = safe_malloc (LONG_STRING);
1300 size_t linelen = LONG_STRING;
1301 char buf[LONG_STRING + 1];
1304 if (hdr->content == NULL) {
1305 hdr->content = mutt_new_body ();
1307 /* set the defaults from RFC1521 */
1308 hdr->content->type = TYPETEXT;
1309 hdr->content->subtype = safe_strdup ("plain");
1310 hdr->content->encoding = ENC7BIT;
1311 hdr->content->length = -1;
1313 /* RFC 2183 says this is arbitrary */
1314 hdr->content->disposition = DISPINLINE;
1318 while ((loc = ftell (f)),
1319 *(line = read_rfc822_line (f, line, &linelen)) != 0) {
1322 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1323 char return_path[LONG_STRING];
1326 /* some bogus MTAs will quote the original "From " line */
1327 if (safe_strncmp (">From ", line, 6) == 0)
1328 continue; /* just ignore */
1329 else if (is_from (line, return_path, sizeof (return_path), &t)) {
1330 /* MH somtimes has the From_ line in the middle of the header! */
1331 if (hdr && !hdr->received)
1332 hdr->received = t - mutt_local_tz (t);
1337 break; /* end of header */
1342 if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1343 if (!rx_list_match (NoSpamList, line)) {
1345 /* if spam tag already exists, figure out how to amend it */
1346 if (e->spam && *buf) {
1347 /* If SpamSep defined, append with separator */
1349 mutt_buffer_addstr (e->spam, SpamSep);
1350 mutt_buffer_addstr (e->spam, buf);
1353 /* else overwrite */
1355 e->spam->dptr = e->spam->data;
1356 *e->spam->dptr = '\0';
1357 mutt_buffer_addstr (e->spam, buf);
1361 /* spam tag is new, and match expr is non-empty; copy */
1362 else if (!e->spam && *buf) {
1363 e->spam = mutt_buffer_from (NULL, buf);
1366 /* match expr is empty; plug in null string if no existing tag */
1367 else if (!e->spam) {
1368 e->spam = mutt_buffer_from (NULL, "");
1371 if (e->spam && e->spam->data)
1372 debug_print (5, ("spam = %s\n", e->spam->data));
1380 continue; /* skip empty header fields */
1383 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1390 hdr->content->hdr_offset = hdr->offset;
1391 hdr->content->offset = ftell (f);
1393 /* do RFC2047 decoding */
1394 rfc2047_decode_adrlist (e->from);
1395 rfc2047_decode_adrlist (e->to);
1396 rfc2047_decode_adrlist (e->cc);
1397 rfc2047_decode_adrlist (e->bcc);
1398 rfc2047_decode_adrlist (e->reply_to);
1399 rfc2047_decode_adrlist (e->mail_followup_to);
1400 rfc2047_decode_adrlist (e->return_path);
1401 rfc2047_decode_adrlist (e->sender);
1404 regmatch_t pmatch[1];
1406 rfc2047_decode (&e->subject);
1408 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1409 e->real_subj = e->subject + pmatch[0].rm_eo;
1411 e->real_subj = e->subject;
1414 /* check for missing or invalid date */
1415 if (hdr->date_sent <= 0) {
1416 debug_print (1, ("no date found, using received time from msg separator\n"));
1417 hdr->date_sent = hdr->received;
1424 ADDRESS *mutt_parse_adrlist (ADDRESS * p, const char *s)
1428 /* check for a simple whitespace separated list of addresses */
1429 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1430 char tmp[HUGE_STRING];
1433 strfcpy (tmp, s, sizeof (tmp));
1435 while ((r = strtok (r, " \t")) != NULL) {
1436 p = rfc822_parse_adrlist (p, r);
1441 p = rfc822_parse_adrlist (p, s);