2 * Copyright (C) 1996-2000,2012-2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "mutt_regex.h"
29 #include "mutt_crypt.h"
33 #include "autocrypt/autocrypt.h"
41 /* Reads an arbitrarily long header field, and looks ahead for continuation
42 * lines. ``line'' must point to a dynamically allocated string; it is
43 * increased if more space is required to fit the whole line.
45 char *mutt_read_rfc822_line (FILE *f, char *line, size_t *linelen)
54 if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */
55 (ISSPACE (*line) && !offset)) /* end of headers */
61 len = mutt_strlen (buf);
68 /* we did get a full line. remove trailing space */
69 while (ISSPACE (*buf))
70 *buf-- = 0; /* we cannot come beyond line's beginning because
71 * it begins with a non-space */
73 /* check to see if the next line is a continuation line */
74 if ((ch = fgetc (f)) != ' ' && ch != '\t')
77 return (line); /* next line is a separate header field or EOH */
80 /* eat tabs and spaces from the beginning of the continuation line */
81 while ((ch = fgetc (f)) == ' ' || ch == '\t')
84 *++buf = ' '; /* string is still terminated because we removed
85 at least one whitespace char above */
90 if (*linelen < offset + STRING)
94 safe_realloc (&line, *linelen);
101 static LIST *mutt_parse_references (char *s, int in_reply_to)
103 LIST *t, *lst = NULL;
107 m = mutt_extract_message_id (s, &sp);
110 t = safe_malloc (sizeof (LIST));
115 m = mutt_extract_message_id (NULL, &sp);
121 int mutt_check_encoding (const char *c)
123 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit")-1) == 0)
125 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit")-1) == 0)
127 else if (ascii_strncasecmp ("binary", c, sizeof ("binary")-1) == 0)
129 else if (ascii_strncasecmp ("quoted-printable", c, sizeof ("quoted-printable")-1) == 0)
130 return (ENCQUOTEDPRINTABLE);
131 else if (ascii_strncasecmp ("base64", c, sizeof("base64")-1) == 0)
133 else if (ascii_strncasecmp ("x-uuencode", c, sizeof("x-uuencode")-1) == 0)
134 return (ENCUUENCODED);
135 #ifdef SUN_ATTACHMENT
136 else if (ascii_strncasecmp ("uuencode", c, sizeof("uuencode")-1) == 0)
137 return (ENCUUENCODED);
143 /* Performs rfc2231 parameter parsing on s.
145 * Autocrypt defines an irregular parameter format that doesn't follow the
146 * rfc. It splits keydata across multiple lines without parameter continuations.
147 * The allow_value_spaces parameter allows parsing those values which
148 * are split by spaces when unfolded.
150 static PARAMETER *parse_parameters (const char *s, int allow_value_spaces)
152 PARAMETER *head = 0, *cur = 0, *new;
153 BUFFER *buffer = NULL;
157 buffer = mutt_buffer_pool_get ();
158 /* allow_value_spaces, especially with autocrypt keydata, can result
159 * in quite large parameter values. avoid frequent reallocs by
161 if (allow_value_spaces)
162 mutt_buffer_increase_size (buffer, mutt_strlen (s));
164 dprint (2, (debugfile, "parse_parameters: `%s'\n", s));
168 mutt_buffer_clear (buffer);
170 if ((p = strpbrk (s, "=;")) == NULL)
172 dprint(1, (debugfile, "parse_parameters: malformed parameter: %s\n", s));
176 /* if we hit a ; now the parameter has no value, just skip it */
180 /* remove whitespace from the end of the attribute name */
181 while (i > 0 && is_email_wsp(s[i-1]))
184 /* the check for the missing parameter token is here so that we can skip
185 * over any quoted value that may be present.
189 dprint(1, (debugfile, "parse_parameters: missing attribute: %s\n", s));
194 new = mutt_new_parameter ();
195 new->attribute = mutt_substrdup(s, s + i);
200 s = skip_email_wsp(p + 1); /* skip over the =, or space if we loop */
210 /* As iso-2022-* has a character of '"' with non-ascii state,
214 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
220 if (state_ascii && *s == '"')
227 /* Quote the next character */
228 mutt_buffer_addch (buffer, *s);
232 mutt_buffer_addch (buffer, *s);
235 s++; /* skip over the " */
239 for (; *s && *s != ' ' && *s != ';'; s++)
240 mutt_buffer_addch (buffer, *s);
244 } while (allow_value_spaces && (*s == ' '));
246 /* if the attribute token was missing, 'new' will be NULL */
249 new->value = safe_strdup (mutt_b2s (buffer));
251 dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n",
252 new->attribute ? new->attribute : "",
253 new->value ? new->value : ""));
255 /* Add this parameter to the list */
267 dprint (1, (debugfile, "parse_parameters(): parameter with no value: %s\n", s));
271 /* Find the next parameter */
272 if (*s != ';' && (s = strchr (s, ';')) == NULL)
273 break; /* no more parameters */
277 /* Move past any leading whitespace. the +1 skips over the semicolon */
278 s = skip_email_wsp(s + 1);
280 while (*s == ';'); /* skip empty parameters */
285 rfc2231_decode_parameters (&head);
286 mutt_buffer_pool_release (&buffer);
290 int mutt_check_mime_type (const char *s)
292 if (ascii_strcasecmp ("text", s) == 0)
294 else if (ascii_strcasecmp ("multipart", s) == 0)
295 return TYPEMULTIPART;
296 #ifdef SUN_ATTACHMENT
297 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
298 return TYPEMULTIPART;
300 else if (ascii_strcasecmp ("application", s) == 0)
301 return TYPEAPPLICATION;
302 else if (ascii_strcasecmp ("message", s) == 0)
304 else if (ascii_strcasecmp ("image", s) == 0)
306 else if (ascii_strcasecmp ("audio", s) == 0)
308 else if (ascii_strcasecmp ("video", s) == 0)
310 else if (ascii_strcasecmp ("model", s) == 0)
312 else if (ascii_strcasecmp ("*", s) == 0)
314 else if (ascii_strcasecmp (".*", s) == 0)
320 void mutt_parse_content_type (char *s, BODY *ct)
326 mutt_free_parameter(&ct->parameter);
328 /* First extract any existing parameters */
329 if ((pc = strchr(s, ';')) != NULL)
332 while (*pc && ISSPACE (*pc))
334 ct->parameter = parse_parameters(pc, 0);
336 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
337 * but if a filename has already been set in the content-disposition,
338 * let that take precedence, and don't set it here */
339 if ((pc = mutt_get_parameter( "name", ct->parameter)) && !ct->filename)
340 ct->filename = safe_strdup(pc);
342 #ifdef SUN_ATTACHMENT
343 /* this is deep and utter perversion */
344 if ((pc = mutt_get_parameter ("conversions", ct->parameter)))
345 ct->encoding = mutt_check_encoding (pc);
350 /* Now get the subtype */
351 if ((subtype = strchr(s, '/')))
354 for (pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++)
357 ct->subtype = safe_strdup (subtype);
360 /* Finally, get the major type */
361 ct->type = mutt_check_mime_type (s);
363 #ifdef SUN_ATTACHMENT
364 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
365 ct->subtype = safe_strdup ("x-sun-attachment");
368 if (ct->type == TYPEOTHER)
370 ct->xtype = safe_strdup (s);
373 if (ct->subtype == NULL)
375 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
376 * field, so we can attempt to convert the type to BODY here.
378 if (ct->type == TYPETEXT)
379 ct->subtype = safe_strdup ("plain");
380 else if (ct->type == TYPEAUDIO)
381 ct->subtype = safe_strdup ("basic");
382 else if (ct->type == TYPEMESSAGE)
383 ct->subtype = safe_strdup ("rfc822");
384 else if (ct->type == TYPEOTHER)
386 char buffer[SHORT_STRING];
388 ct->type = TYPEAPPLICATION;
389 snprintf (buffer, sizeof (buffer), "x-%s", s);
390 ct->subtype = safe_strdup (buffer);
393 ct->subtype = safe_strdup ("x-unknown");
396 /* Default character set for text types. */
397 if (ct->type == TYPETEXT)
399 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
400 mutt_set_parameter ("charset", AssumedCharset ?
401 (const char *) mutt_get_default_charset ()
402 : "us-ascii", &ct->parameter);
407 static void parse_content_disposition (const char *s, BODY *ct)
411 if (!ascii_strncasecmp ("inline", s, 6))
412 ct->disposition = DISPINLINE;
413 else if (!ascii_strncasecmp ("form-data", s, 9))
414 ct->disposition = DISPFORMDATA;
416 ct->disposition = DISPATTACH;
418 /* Check to see if a default filename was given */
419 if ((s = strchr (s, ';')) != NULL)
421 s = skip_email_wsp(s + 1);
422 if ((s = mutt_get_parameter ("filename", (parms = parse_parameters (s, 0)))))
423 mutt_str_replace (&ct->filename, s);
424 if ((s = mutt_get_parameter ("name", parms)))
425 ct->form_name = safe_strdup (s);
426 mutt_free_parameter (&parms);
431 static AUTOCRYPTHDR *parse_autocrypt (AUTOCRYPTHDR *head, const char *s)
433 AUTOCRYPTHDR *autocrypt;
434 PARAMETER *params = NULL, *param;
436 autocrypt = mutt_new_autocrypthdr ();
437 autocrypt->next = head;
439 param = params = parse_parameters (s, 1);
442 autocrypt->invalid = 1;
448 if (!ascii_strcasecmp (param->attribute, "addr"))
452 autocrypt->invalid = 1;
455 autocrypt->addr = param->value;
458 else if (!ascii_strcasecmp (param->attribute, "prefer-encrypt"))
460 if (!ascii_strcasecmp (param->value, "mutual"))
461 autocrypt->prefer_encrypt = 1;
463 else if (!ascii_strcasecmp (param->attribute, "keydata"))
465 if (autocrypt->keydata)
467 autocrypt->invalid = 1;
470 autocrypt->keydata = param->value;
473 else if (param->attribute && (param->attribute[0] != '_'))
475 autocrypt->invalid = 1;
482 /* Checking the addr against From, and for multiple valid headers
483 * occurs later, after all the headers are parsed. */
484 if (!autocrypt->addr || !autocrypt->keydata)
485 autocrypt->invalid = 1;
488 mutt_free_parameter (¶ms);
494 * fp stream to read from
496 * digest 1 if reading subparts of a multipart/digest, 0
500 BODY *mutt_read_mime_header (FILE *fp, int digest)
502 BODY *p = mutt_new_body();
503 ENVELOPE *e = mutt_new_envelope ();
505 char *line = safe_malloc (LONG_STRING);
506 size_t linelen = LONG_STRING;
508 p->hdr_offset = ftello (fp);
510 p->encoding = ENC7BIT; /* default from RFC1521 */
511 p->type = digest ? TYPEMESSAGE : TYPETEXT;
512 p->disposition = DISPINLINE;
514 while (*(line = mutt_read_rfc822_line (fp, line, &linelen)) != 0)
516 /* Find the value of the current header */
517 if ((c = strchr (line, ':')))
520 c = skip_email_wsp(c + 1);
523 dprint (1, (debugfile, "mutt_read_mime_header(): skipping empty header field: %s\n", line));
529 dprint (1, (debugfile, "read_mime_header: bogus MIME header: %s\n", line));
533 if (!ascii_strncasecmp ("content-", line, 8))
535 if (!ascii_strcasecmp ("type", line + 8))
536 mutt_parse_content_type (c, p);
537 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
538 p->encoding = mutt_check_encoding (c);
539 else if (!ascii_strcasecmp ("disposition", line + 8))
540 parse_content_disposition (c, p);
541 else if (!ascii_strcasecmp ("description", line + 8))
543 mutt_str_replace (&p->description, c);
544 rfc2047_decode (&p->description);
547 #ifdef SUN_ATTACHMENT
548 else if (!ascii_strncasecmp ("x-sun-", line, 6))
550 if (!ascii_strcasecmp ("data-type", line + 6))
551 mutt_parse_content_type (c, p);
552 else if (!ascii_strcasecmp ("encoding-info", line + 6))
553 p->encoding = mutt_check_encoding (c);
554 else if (!ascii_strcasecmp ("content-lines", line + 6))
555 mutt_set_parameter ("content-lines", c, &(p->parameter));
556 else if (!ascii_strcasecmp ("data-description", line + 6))
558 mutt_str_replace (&p->description, c);
559 rfc2047_decode (&p->description);
565 if (mutt_parse_rfc822_line (e, NULL, line, c, 0, 0, 0, NULL))
569 p->offset = ftello (fp); /* Mark the start of the real data */
570 if (p->type == TYPETEXT && !p->subtype)
571 p->subtype = safe_strdup ("plain");
572 else if (p->type == TYPEMESSAGE && !p->subtype)
573 p->subtype = safe_strdup ("rfc822");
578 rfc2047_decode_envelope (p->mime_headers);
580 mutt_free_envelope (&e);
585 void mutt_parse_part (FILE *fp, BODY *b)
592 #ifdef SUN_ATTACHMENT
593 if ( !ascii_strcasecmp (b->subtype, "x-sun-attachment") )
597 bound = mutt_get_parameter ("boundary", b->parameter);
599 fseeko (fp, b->offset, SEEK_SET);
600 b->parts = mutt_parse_multipart (fp, bound,
601 b->offset + b->length,
602 ascii_strcasecmp ("digest", b->subtype) == 0);
608 fseeko (fp, b->offset, SEEK_SET);
609 if (mutt_is_message_type(b->type, b->subtype))
610 b->parts = mutt_parse_messageRFC822 (fp, b);
611 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
612 b->parts = mutt_read_mime_header (fp, 0);
622 /* try to recover from parsing error */
626 mutt_str_replace (&b->subtype, "plain");
630 /* parse a MESSAGE/RFC822 body
633 * fp stream to read from
635 * parent structure which contains info about the message/rfc822
638 * NOTE: this assumes that `parent->length' has been set!
641 BODY *mutt_parse_messageRFC822 (FILE *fp, BODY *parent)
645 parent->hdr = mutt_new_header ();
646 parent->hdr->offset = ftello (fp);
647 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
648 msg = parent->hdr->content;
650 /* ignore the length given in the content-length since it could be wrong
651 and we already have the info to calculate the correct length */
652 /* if (msg->length == -1) */
653 msg->length = parent->length - (msg->offset - parent->offset);
655 /* if body of this message is empty, we can end up with a negative length */
659 mutt_parse_part(fp, msg);
663 /* parse a multipart structure
666 * fp stream to read from
668 * boundary body separator
670 * end_off length of the multipart body (used when the final
671 * boundary is missing to avoid reading too far)
673 * digest 1 if reading a multipart/digest, 0 otherwise
676 BODY *mutt_parse_multipart (FILE *fp, const char *boundary, LOFF_T end_off, int digest)
678 #ifdef SUN_ATTACHMENT
681 int blen, len, crlf = 0;
682 char buffer[LONG_STRING];
683 BODY *head = 0, *last = 0, *new = 0;
685 int final = 0; /* did we see the ending boundary? */
689 mutt_error _("multipart message has no boundary parameter!");
693 blen = mutt_strlen (boundary);
694 while (ftello (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL)
696 len = mutt_strlen (buffer);
698 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
700 if (buffer[0] == '-' && buffer[1] == '-' &&
701 mutt_strncmp (buffer + 2, boundary, blen) == 0)
705 last->length = ftello (fp) - last->offset - len - 1 - crlf;
706 if (last->parts && last->parts->length == 0)
707 last->parts->length = ftello (fp) - last->parts->offset - len - 1 - crlf;
708 /* if the body is empty, we can end up with a -1 length */
709 if (last->length < 0)
713 /* Remove any trailing whitespace, up to the length of the boundary */
714 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
717 /* Check for the end boundary */
718 if (mutt_strcmp (buffer + blen + 2, "--") == 0)
721 break; /* done parsing */
723 else if (buffer[2 + blen] == 0)
725 new = mutt_read_mime_header (fp, digest);
727 #ifdef SUN_ATTACHMENT
728 if (mutt_get_parameter ("content-lines", new->parameter))
730 mutt_atoi (mutt_get_parameter ("content-lines", new->parameter), &lines);
731 for ( ; lines; lines-- )
732 if (ftello (fp) >= end_off || fgets (buffer, LONG_STRING, fp) == NULL)
738 * Consistency checking - catch
739 * bad attachment end boundaries
742 if (new->offset > end_off)
744 mutt_free_body(&new);
758 /* in case of missing end boundary, set the length to something reasonable */
759 if (last && last->length == 0 && !final)
760 last->length = end_off - last->offset;
762 /* parse recursive MIME parts */
763 for (last = head; last; last = last->next)
764 mutt_parse_part(fp, last);
769 static const char *uncomment_timezone (char *buf, size_t buflen, const char *tz)
775 return tz; /* no need to do anything */
776 tz = skip_email_wsp(tz + 1);
777 if ((p = strpbrk (tz, " )")) == NULL)
780 if (len > buflen - 1)
782 memcpy (buf, tz, len);
787 static const struct tz_t
790 unsigned char zhours;
791 unsigned char zminutes;
792 unsigned char zoccident; /* west of UTC? */
796 { "aat", 1, 0, 1 }, /* Atlantic Africa Time */
797 { "adt", 4, 0, 0 }, /* Arabia DST */
798 { "ast", 3, 0, 0 }, /* Arabia */
799 /*{ "ast", 4, 0, 1 },*/ /* Atlantic */
800 { "bst", 1, 0, 0 }, /* British DST */
801 { "cat", 1, 0, 0 }, /* Central Africa */
803 { "cest", 2, 0, 0 }, /* Central Europe DST */
804 { "cet", 1, 0, 0 }, /* Central Europe */
806 /*{ "cst", 8, 0, 0 },*/ /* China */
807 /*{ "cst", 9, 30, 0 },*/ /* Australian Central Standard Time */
808 { "eat", 3, 0, 0 }, /* East Africa */
810 { "eest", 3, 0, 0 }, /* Eastern Europe DST */
811 { "eet", 2, 0, 0 }, /* Eastern Europe */
812 { "egst", 0, 0, 0 }, /* Eastern Greenland DST */
813 { "egt", 1, 0, 1 }, /* Eastern Greenland */
816 { "gst", 4, 0, 0 }, /* Presian Gulf */
817 { "hkt", 8, 0, 0 }, /* Hong Kong */
818 { "ict", 7, 0, 0 }, /* Indochina */
819 { "idt", 3, 0, 0 }, /* Israel DST */
820 { "ist", 2, 0, 0 }, /* Israel */
821 /*{ "ist", 5, 30, 0 },*/ /* India */
822 { "jst", 9, 0, 0 }, /* Japan */
823 { "kst", 9, 0, 0 }, /* Korea */
825 { "met", 1, 0, 0 }, /* this is now officially CET */
826 { "msd", 4, 0, 0 }, /* Moscow DST */
827 { "msk", 3, 0, 0 }, /* Moscow */
829 { "nzdt", 13, 0, 0 }, /* New Zealand DST */
830 { "nzst", 12, 0, 0 }, /* New Zealand */
833 { "sat", 2, 0, 0 }, /* South Africa */
834 { "smt", 4, 0, 0 }, /* Seychelles */
835 { "sst", 11, 0, 1 }, /* Samoa */
836 /*{ "sst", 8, 0, 0 },*/ /* Singapore */
838 { "wat", 0, 0, 0 }, /* West Africa */
839 { "west", 1, 0, 0 }, /* Western Europe DST */
840 { "wet", 0, 0, 0 }, /* Western Europe */
841 { "wgst", 2, 0, 1 }, /* Western Greenland DST */
842 { "wgt", 3, 0, 1 }, /* Western Greenland */
843 { "wst", 8, 0, 0 }, /* Western Australia */
846 /* parses a date string in RFC822 format:
848 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
850 * This routine assumes that `h' has been initialized to 0. the `timezone'
851 * field is optional, defaulting to +0000 if missing.
853 time_t mutt_parse_date (const char *s, HEADER *h)
865 char tzstr[SHORT_STRING];
866 char scratch[SHORT_STRING];
868 /* Don't modify our argument. Fixed-size buffer is ok here since
869 * the date format imposes a natural limit.
872 strfcpy (scratch, s, sizeof (scratch));
874 /* kill the day of the week, if it exists. */
875 if ((t = strchr (scratch, ',')))
879 t = skip_email_wsp(t);
881 memset (&tm, 0, sizeof (tm));
883 while ((t = strtok (t, " \t")) != NULL)
887 case 0: /* day of the month */
888 if (mutt_atoi (t, &tm.tm_mday) < 0 || tm.tm_mday < 0)
894 case 1: /* month of the year */
895 if ((i = mutt_check_month (t)) < 0)
901 if (mutt_atoi (t, &tm.tm_year) < 0 || tm.tm_year < 0)
905 else if (tm.tm_year >= 1900)
909 case 3: /* time of day */
910 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3)
912 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
916 dprint(1, (debugfile, "parse_date: could not process time format: %s\n", t));
924 case 4: /* timezone */
925 /* sometimes we see things like (MST) or (-0700) so attempt to
926 * compensate by uncommenting the string if non-RFC822 compliant
928 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
930 if (*ptz == '+' || *ptz == '-')
932 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
933 && isdigit ((unsigned char) ptz[1]) && isdigit ((unsigned char) ptz[2])
934 && isdigit ((unsigned char) ptz[3]) && isdigit ((unsigned char) ptz[4]))
936 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
937 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
947 tz = bsearch (ptz, TimeZones, sizeof TimeZones/sizeof (struct tz_t),
948 sizeof (struct tz_t),
949 (int (*)(const void *, const void *)) ascii_strcasecmp
950 /* This is safe to do: A pointer to a struct equals
951 * a pointer to its first element*/);
956 zminutes = tz->zminutes;
957 zoccident = tz->zoccident;
960 /* ad hoc support for the European MET (now officially CET) TZ */
961 if (ascii_strcasecmp (t, "MET") == 0)
963 if ((t = strtok (NULL, " \t")) != NULL)
965 if (!ascii_strcasecmp (t, "DST"))
970 tz_offset = zhours * 3600 + zminutes * 60;
972 tz_offset = -tz_offset;
979 if (count < 4) /* don't check for missing timezone */
981 dprint(1,(debugfile, "parse_date(): error parsing date format, using received time\n"));
988 h->zminutes = zminutes;
989 h->zoccident = zoccident;
992 return (mutt_mktime (&tm, 0) + tz_offset);
995 /* extract the first substring that looks like a message-id.
996 * call back with NULL for more (like strtok).
998 char *mutt_extract_message_id (const char *s, const char **saveptr)
1000 const char *o, *onull, *p;
1010 for (s = NULL, o = NULL, onull = NULL;
1011 (p = strpbrk (p, "<> \t;")) != NULL; ++p)
1025 size_t olen = onull - o, slen = p - s + 1;
1026 ret = safe_malloc (olen + slen + 1);
1028 memcpy (ret, o, olen);
1029 memcpy (ret + olen, s, slen);
1030 ret[olen + slen] = '\0';
1032 *saveptr = p + 1; /* next call starts after '>' */
1036 /* some idiotic clients break their message-ids between lines */
1038 /* step past another whitespace */
1041 /* more than two lines, give up */
1042 s = o = onull = NULL;
1045 /* remember the first line, start looking for the second */
1055 void mutt_parse_mime_message (CONTEXT *ctx, HEADER *cur)
1061 if (cur->content->type != TYPEMESSAGE &&
1062 cur->content->type != TYPEMULTIPART)
1063 break; /* nothing to do */
1065 if (cur->content->parts)
1066 break; /* The message was parsed earlier. */
1068 if ((msg = mx_open_message (ctx, cur->msgno)))
1070 mutt_parse_part (msg->fp, cur->content);
1073 cur->security = crypt_query (cur->content);
1075 mx_close_message (ctx, &msg);
1079 cur->attach_valid = 0;
1082 void mutt_auto_subscribe (const char *mailto)
1086 if (!AutoSubscribeCache)
1087 AutoSubscribeCache = hash_create (200, MUTT_HASH_STRCASECMP | MUTT_HASH_STRDUP_KEYS);
1089 if (!mailto || hash_find (AutoSubscribeCache, mailto))
1092 hash_insert (AutoSubscribeCache, mailto, AutoSubscribeCache);
1094 lpenv = mutt_new_envelope (); /* parsed envelope from the List-Post mailto: URL */
1096 if ((url_parse_mailto (lpenv, NULL, mailto) != -1) &&
1097 lpenv->to && lpenv->to->mailbox &&
1098 !mutt_match_rx_list (lpenv->to->mailbox, SubscribedLists) &&
1099 !mutt_match_rx_list (lpenv->to->mailbox, UnMailLists) &&
1100 !mutt_match_rx_list (lpenv->to->mailbox, UnSubscribedLists))
1103 char errbuf[STRING];
1105 memset (&err, 0, sizeof(err));
1107 err.dsize = sizeof(errbuf);
1109 /* mutt_add_to_rx_list() detects duplicates, so it is safe to
1110 * try to add here without any checks. */
1111 mutt_add_to_rx_list (&MailLists, lpenv->to->mailbox, REG_ICASE, &err);
1112 mutt_add_to_rx_list (&SubscribedLists, lpenv->to->mailbox, REG_ICASE, &err);
1114 mutt_free_envelope (&lpenv);
1117 int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short user_hdrs, short weed,
1118 short do_2047, LIST **lastp)
1122 switch (ascii_tolower (line[0]))
1125 if (ascii_strcasecmp (line+1, "pparently-to") == 0)
1127 e->to = rfc822_parse_adrlist (e->to, p);
1130 else if (ascii_strcasecmp (line+1, "pparently-from") == 0)
1132 e->from = rfc822_parse_adrlist (e->from, p);
1135 #ifdef USE_AUTOCRYPT
1136 else if (ascii_strcasecmp (line+1, "utocrypt") == 0)
1138 if (option (OPTAUTOCRYPT))
1140 e->autocrypt = parse_autocrypt (e->autocrypt, p);
1144 else if (ascii_strcasecmp (line+1, "utocrypt-gossip") == 0)
1146 if (option (OPTAUTOCRYPT))
1148 e->autocrypt_gossip = parse_autocrypt (e->autocrypt_gossip, p);
1156 if (ascii_strcasecmp (line+1, "cc") == 0)
1158 e->bcc = rfc822_parse_adrlist (e->bcc, p);
1164 if (ascii_strcasecmp (line+1, "c") == 0)
1166 e->cc = rfc822_parse_adrlist (e->cc, p);
1169 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0)
1171 if (ascii_strcasecmp (line+8, "type") == 0)
1174 mutt_parse_content_type (p, hdr->content);
1177 else if (ascii_strcasecmp (line+8, "transfer-encoding") == 0)
1180 hdr->content->encoding = mutt_check_encoding (p);
1183 else if (ascii_strcasecmp (line+8, "length") == 0)
1187 if ((hdr->content->length = atol (p)) < 0)
1188 hdr->content->length = -1;
1192 else if (ascii_strcasecmp (line+8, "description") == 0)
1196 mutt_str_replace (&hdr->content->description, p);
1197 rfc2047_decode (&hdr->content->description);
1201 else if (ascii_strcasecmp (line+8, "disposition") == 0)
1204 parse_content_disposition (p, hdr->content);
1211 if (!ascii_strcasecmp ("ate", line + 1))
1213 mutt_str_replace (&e->date, p);
1215 hdr->date_sent = mutt_parse_date (p, hdr);
1221 if (!ascii_strcasecmp ("xpires", line + 1) &&
1222 hdr && mutt_parse_date (p, NULL) < time (NULL))
1227 if (!ascii_strcasecmp ("rom", line + 1))
1229 e->from = rfc822_parse_adrlist (e->from, p);
1235 if (!ascii_strcasecmp (line+1, "n-reply-to"))
1237 mutt_free_list (&e->in_reply_to);
1238 e->in_reply_to = mutt_parse_references (p, 1);
1244 if (!ascii_strcasecmp (line + 1, "ines"))
1249 * HACK - mutt has, for a very short time, produced negative
1250 * Lines header values. Ignore them.
1252 if (mutt_atoi (p, &hdr->lines) < 0 || hdr->lines < 0)
1258 else if (!ascii_strcasecmp (line + 1, "ist-Post"))
1260 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1261 if (strncmp (p, "NO", 2))
1264 for (beg = strchr (p, '<'); beg; beg = strchr (end, ','))
1267 if (!(end = strchr (beg, '>')))
1270 /* Take the first mailto URL */
1271 if (url_check_scheme (beg) == U_MAILTO)
1273 FREE (&e->list_post);
1274 e->list_post = mutt_substrdup (beg, end);
1275 if (option (OPTAUTOSUBSCRIBE))
1276 mutt_auto_subscribe (e->list_post);
1286 if (!ascii_strcasecmp (line + 1, "ime-version"))
1292 else if (!ascii_strcasecmp (line + 1, "essage-id"))
1294 /* We add a new "Message-ID:" when building a message */
1295 FREE (&e->message_id);
1296 e->message_id = mutt_extract_message_id (p, NULL);
1299 else if (!ascii_strncasecmp (line + 1, "ail-", 4))
1301 if (!ascii_strcasecmp (line + 5, "reply-to"))
1303 /* override the Reply-To: field */
1304 rfc822_free_address (&e->reply_to);
1305 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1308 else if (!ascii_strcasecmp (line + 5, "followup-to"))
1310 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1317 if (!ascii_strcasecmp (line + 1, "eferences"))
1319 mutt_free_list (&e->references);
1320 e->references = mutt_parse_references (p, 0);
1323 else if (!ascii_strcasecmp (line + 1, "eply-to"))
1325 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1328 else if (!ascii_strcasecmp (line + 1, "eturn-path"))
1330 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1333 else if (!ascii_strcasecmp (line + 1, "eceived"))
1335 if (hdr && !hdr->received)
1337 char *d = strrchr (p, ';');
1340 hdr->received = mutt_parse_date (d + 1, NULL);
1346 if (!ascii_strcasecmp (line + 1, "ubject"))
1349 e->subject = safe_strdup (p);
1352 else if (!ascii_strcasecmp (line + 1, "ender"))
1354 e->sender = rfc822_parse_adrlist (e->sender, p);
1357 else if (!ascii_strcasecmp (line + 1, "tatus"))
1380 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1381 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1383 FREE(&e->supersedes);
1384 e->supersedes = safe_strdup (p);
1389 if (ascii_strcasecmp (line+1, "o") == 0)
1391 e->to = rfc822_parse_adrlist (e->to, p);
1397 if (ascii_strcasecmp (line+1, "-status") == 0)
1422 else if (ascii_strcasecmp (line+1, "-label") == 0)
1425 e->x_label = safe_strdup(p);
1433 /* Keep track of the user-defined headers */
1434 if (!matched && user_hdrs)
1441 /* restore the original line */
1442 line[strlen (line)] = ':';
1444 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1445 && !mutt_matches_ignore (line, UnIgnore))
1450 last->next = mutt_new_list ();
1454 last = e->userhdrs = mutt_new_list ();
1455 last->data = safe_strdup (line);
1457 rfc2047_decode (&last->data);
1468 /* mutt_read_rfc822_header() -- parses a RFC822 header
1472 * f stream to read from
1474 * hdr header structure of current message (optional).
1476 * user_hdrs If set, store user headers. Used for recall-message and
1479 * weed If this parameter is set and the user has activated the
1480 * $weed option, honor the header weed list for user headers.
1481 * Used for recall-message.
1483 * Returns: newly allocated envelope structure. You should free it by
1484 * mutt_free_envelope() when envelope stay unneeded.
1486 ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
1489 ENVELOPE *e = mutt_new_envelope();
1491 char *line = safe_malloc (LONG_STRING);
1494 size_t linelen = LONG_STRING;
1495 char buf[LONG_STRING+1];
1499 if (hdr->content == NULL)
1501 hdr->content = mutt_new_body ();
1503 /* set the defaults from RFC1521 */
1504 hdr->content->type = TYPETEXT;
1505 hdr->content->subtype = safe_strdup ("plain");
1506 hdr->content->encoding = ENC7BIT;
1507 hdr->content->length = -1;
1509 /* RFC 2183 says this is arbitrary */
1510 hdr->content->disposition = DISPINLINE;
1514 while ((loc = ftello (f)),
1515 *(line = mutt_read_rfc822_line (f, line, &linelen)) != 0)
1517 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':')
1519 char return_path[LONG_STRING];
1522 /* some bogus MTAs will quote the original "From " line */
1523 if (mutt_strncmp (">From ", line, 6) == 0)
1524 continue; /* just ignore */
1525 else if (is_from (line, return_path, sizeof (return_path), &t))
1527 /* MH sometimes has the From_ line in the middle of the header! */
1528 if (hdr && !hdr->received)
1529 hdr->received = t - mutt_local_tz (t);
1534 break; /* end of header */
1539 if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf)))
1541 if (!mutt_match_rx_list(line, NoSpamList))
1544 /* if spam tag already exists, figure out how to amend it */
1545 if (e->spam && *buf)
1547 /* If SpamSep defined, append with separator */
1550 mutt_buffer_addstr(e->spam, SpamSep);
1551 mutt_buffer_addstr(e->spam, buf);
1554 /* else overwrite */
1557 mutt_buffer_clear (e->spam);
1558 mutt_buffer_addstr(e->spam, buf);
1562 /* spam tag is new, and match expr is non-empty; copy */
1563 else if (!e->spam && *buf)
1565 e->spam = mutt_buffer_from (buf);
1568 /* match expr is empty; plug in null string if no existing tag */
1571 e->spam = mutt_buffer_from("");
1574 if (e->spam && e->spam->data)
1575 dprint(5, (debugfile, "p822: spam = %s\n", e->spam->data));
1580 p = skip_email_wsp(p + 1);
1582 continue; /* skip empty header fields */
1584 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1591 hdr->content->hdr_offset = hdr->offset;
1592 hdr->content->offset = ftello (f);
1594 rfc2047_decode_envelope (e);
1598 regmatch_t pmatch[1];
1600 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1601 e->real_subj = e->subject + pmatch[0].rm_eo;
1603 e->real_subj = e->subject;
1606 if (hdr->received < 0)
1608 dprint(1,(debugfile,"read_rfc822_header(): resetting invalid received time to 0\n"));
1612 /* check for missing or invalid date */
1613 if (hdr->date_sent <= 0)
1615 dprint(1,(debugfile,"read_rfc822_header(): no date found, using received time from msg separator\n"));
1616 hdr->date_sent = hdr->received;
1619 #ifdef USE_AUTOCRYPT
1620 if (option (OPTAUTOCRYPT))
1622 mutt_autocrypt_process_autocrypt_header (hdr, e);
1623 /* No sense in taking up memory after the header is processed */
1624 mutt_free_autocrypthdr (&e->autocrypt);
1632 ADDRESS *mutt_parse_adrlist (ADDRESS *p, const char *s)
1636 /* check for a simple whitespace separated list of addresses */
1637 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL)
1642 tmp = mutt_buffer_pool_get ();
1643 mutt_buffer_strcpy (tmp, s);
1645 while ((r = strtok (r, " \t")) != NULL)
1647 p = rfc822_parse_adrlist (p, r);
1650 mutt_buffer_pool_release (&tmp);
1653 p = rfc822_parse_adrlist (p, s);
1658 /* Compares mime types to the ok and except lists */
1659 static int count_body_parts_check(LIST **checklist, BODY *b, int dflt)
1664 /* If list is null, use default behavior. */
1671 for (type = *checklist; type; type = type->next)
1673 a = (ATTACH_MATCH *)type->data;
1674 dprint(5, (debugfile, "cbpc: %s %d/%s ?? %s/%s [%d]... ",
1675 dflt ? "[OK] " : "[EXCL] ",
1676 b->type, b->subtype, a->major, a->minor, a->major_int));
1677 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1678 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0))
1680 dprint(5, (debugfile, "yes\n"));
1685 dprint(5, (debugfile, "no\n"));
1692 #define AT_COUNT(why) { shallcount = 1; }
1693 #define AT_NOCOUNT(why) { shallcount = 0; }
1695 static int count_body_parts (BODY *body, int flags)
1698 int shallcount, shallrecurse;
1704 for (bp = body; bp != NULL; bp = bp->next)
1706 /* Initial disposition is to count and not to recurse this part. */
1707 AT_COUNT("default");
1710 dprint(5, (debugfile, "bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1711 bp->description ? bp->description : ("none"),
1712 bp->filename ? bp->filename :
1713 bp->d_filename ? bp->d_filename : "(none)",
1714 bp->type, bp->subtype ? bp->subtype : "*"));
1716 if (bp->type == TYPEMESSAGE)
1720 /* If it's an external body pointer, don't recurse it. */
1721 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1724 /* Don't count containers if they're top-level. */
1725 if (flags & MUTT_PARTS_TOPLEVEL)
1726 AT_NOCOUNT("top-level message/*");
1728 else if (bp->type == TYPEMULTIPART)
1730 /* Always recurse multiparts, except multipart/alternative. */
1732 if (!ascii_strcasecmp(bp->subtype, "alternative"))
1735 /* Don't count containers if they're top-level. */
1736 if (flags & MUTT_PARTS_TOPLEVEL)
1737 AT_NOCOUNT("top-level multipart");
1740 if (bp->disposition == DISPINLINE &&
1741 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1742 AT_NOCOUNT("ignore fundamental inlines");
1744 /* If this body isn't scheduled for enumeration already, don't bother
1745 * profiling it further.
1749 /* Turn off shallcount if message type is not in ok list,
1750 * or if it is in except list. Check is done separately for
1751 * inlines vs. attachments.
1754 if (bp->disposition == DISPATTACH)
1756 if (!count_body_parts_check(&AttachAllow, bp, 1))
1757 AT_NOCOUNT("attach not allowed");
1758 if (count_body_parts_check(&AttachExclude, bp, 0))
1759 AT_NOCOUNT("attach excluded");
1763 if (!count_body_parts_check(&InlineAllow, bp, 1))
1764 AT_NOCOUNT("inline not allowed");
1765 if (count_body_parts_check(&InlineExclude, bp, 0))
1766 AT_NOCOUNT("excluded");
1772 bp->attach_qualifies = shallcount ? 1 : 0;
1774 dprint(5, (debugfile, "cbp: %p shallcount = %d\n", (void *)bp, shallcount));
1778 dprint(5, (debugfile, "cbp: %p pre count = %d\n", (void *)bp, count));
1779 bp->attach_count = count_body_parts(bp->parts, flags & ~MUTT_PARTS_TOPLEVEL);
1780 count += bp->attach_count;
1781 dprint(5, (debugfile, "cbp: %p post count = %d\n", (void *)bp, count));
1785 dprint(5, (debugfile, "bp: return %d\n", count < 0 ? 0 : count));
1786 return count < 0 ? 0 : count;
1789 int mutt_count_body_parts (CONTEXT *ctx, HEADER *hdr)
1791 short keep_parts = 0;
1793 if (hdr->attach_valid)
1794 return hdr->attach_total;
1796 if (hdr->content->parts)
1799 mutt_parse_mime_message (ctx, hdr);
1801 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1802 hdr->attach_total = count_body_parts(hdr->content, MUTT_PARTS_TOPLEVEL);
1804 hdr->attach_total = 0;
1806 hdr->attach_valid = 1;
1809 mutt_free_body (&hdr->content->parts);
1811 return hdr->attach_total;