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.
18 #include "recvattach.h"
23 #include "mutt_crypt.h"
30 #include "lib/debug.h"
37 /* Reads an arbitrarily long header field, and looks ahead for continuation
38 * lines. ``line'' must point to a dynamically allocated string; it is
39 * increased if more space is required to fit the whole line.
41 char *mutt_read_rfc822_line (FILE * f, char *line, size_t * linelen)
48 if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */
49 (ISSPACE (*line) && !offset)) { /* end of headers */
54 buf += str_len (buf) - 1;
56 /* we did get a full line. remove trailing space */
57 while (ISSPACE (*buf))
58 *buf-- = 0; /* we cannot come beyond line's beginning because
59 * it begins with a non-space */
61 /* check to see if the next line is a continuation line */
62 if ((ch = fgetc (f)) != ' ' && ch != '\t') {
64 return (line); /* next line is a separate header field or EOH */
67 /* eat tabs and spaces from the beginning of the continuation line */
68 while ((ch = fgetc (f)) == ' ' || ch == '\t');
70 *++buf = ' '; /* string is still terminated because we removed
71 at least one whitespace char above */
76 if (*linelen < offset + STRING) {
79 mem_realloc (&line, *linelen);
86 LIST *mutt_parse_references (char *s, int in_reply_to)
90 char *o = NULL, *new, *at;
92 while ((s = strtok (s, " \t;")) != NULL) {
94 * some mail clients add other garbage besides message-ids, so do a quick
95 * check to make sure this looks like a valid message-id
96 * some idiotic clients also break their message-ids between lines, deal
97 * with that too (give up if it's more than two lines, though)
104 if (s[n - 1] != '>') {
114 if (s[m - 1] == '>') {
115 new = mem_malloc (sizeof (char) * (n + m + 1));
116 strcpy (new, o); /* __STRCPY_CHECKED__ */
117 strcpy (new + n, s); /* __STRCPY_CHECKED__ */
121 /* make sure that this really does look like a message-id.
122 * it should have exactly one @, and if we're looking at
123 * an in-reply-to header, make sure that the part before
124 * the @ has more than eight characters or it's probably
127 if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
128 || (in_reply_to && at - new <= 8))
131 t = (LIST *) mem_malloc (sizeof (LIST));
144 int mutt_check_encoding (const char *c)
146 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit") - 1) == 0)
148 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit") - 1) == 0)
150 else if (ascii_strncasecmp ("binary", c, sizeof ("binary") - 1) == 0)
153 if (ascii_strncasecmp
154 ("quoted-printable", c, sizeof ("quoted-printable") - 1) == 0)
155 return (ENCQUOTEDPRINTABLE);
156 else if (ascii_strncasecmp ("base64", c, sizeof ("base64") - 1) == 0)
158 else if (ascii_strncasecmp ("x-uuencode", c, sizeof ("x-uuencode") - 1) ==
160 return (ENCUUENCODED);
161 #ifdef SUN_ATTACHMENT
162 else if (ascii_strncasecmp ("uuencode", c, sizeof ("uuencode") - 1) == 0)
163 return (ENCUUENCODED);
169 static PARAMETER *parse_parameters (const char *s)
171 PARAMETER *head = 0, *cur = 0, *new;
172 char buffer[LONG_STRING];
176 debug_print (2, ("`%s'\n", s));
179 if ((p = strpbrk (s, "=;")) == NULL) {
180 debug_print (1, ("malformed parameter: %s\n", s));
184 /* if we hit a ; now the parameter has no value, just skip it */
188 new = mutt_new_parameter ();
190 new->attribute = mem_malloc (i + 1);
191 memcpy (new->attribute, s, i);
192 new->attribute[i] = 0;
194 /* remove whitespace from the end of the attribute name */
195 while (ISSPACE (new->attribute[--i]))
196 new->attribute[i] = 0;
198 s = p + 1; /* skip over the = */
205 for (i = 0; *s && i < sizeof (buffer) - 1; i++, s++) {
206 if (!option (OPTSTRICTMIME)) {
207 /* As iso-2022-* has a characer of '"' with non-ascii state,
209 if (*s == 0x1b && i < sizeof (buffer) - 2) {
210 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
216 if (state_ascii && *s == '"')
219 /* Quote the next character */
229 s++; /* skip over the " */
232 for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1;
238 new->value = str_dup (buffer);
240 debug_print (2, ("`%s' = `%s'\n", new->attribute ? new->attribute : "",
241 new->value ? new->value : ""));
243 /* Add this parameter to the list */
252 debug_print (1, ("parameter with no value: %s\n", s));
256 /* Find the next parameter */
257 if (*s != ';' && (s = strchr (s, ';')) == NULL)
258 break; /* no more parameters */
263 /* Move past any leading whitespace */
266 while (*s == ';'); /* skip empty parameters */
271 rfc2231_decode_parameters (&head);
275 int mutt_check_mime_type (const char *s)
277 if (ascii_strcasecmp ("text", s) == 0)
279 else if (ascii_strcasecmp ("multipart", s) == 0)
280 return TYPEMULTIPART;
281 #ifdef SUN_ATTACHMENT
282 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
283 return TYPEMULTIPART;
285 else if (ascii_strcasecmp ("application", s) == 0)
286 return TYPEAPPLICATION;
287 else if (ascii_strcasecmp ("message", s) == 0)
289 else if (ascii_strcasecmp ("image", s) == 0)
291 else if (ascii_strcasecmp ("audio", s) == 0)
293 else if (ascii_strcasecmp ("video", s) == 0)
295 else if (ascii_strcasecmp ("model", s) == 0)
297 else if (ascii_strcasecmp ("*", s) == 0)
299 else if (ascii_strcasecmp (".*", s) == 0)
305 void mutt_parse_content_type (char *s, BODY * ct)
310 mem_free (&ct->subtype);
311 mutt_free_parameter (&ct->parameter);
313 /* First extract any existing parameters */
314 if ((pc = strchr (s, ';')) != NULL) {
316 while (*pc && ISSPACE (*pc))
318 ct->parameter = parse_parameters (pc);
320 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
321 * but if a filename has already been set in the content-disposition,
322 * let that take precedence, and don't set it here */
323 if ((pc = mutt_get_parameter ("name", ct->parameter)) != 0
325 ct->filename = str_dup (pc);
327 #ifdef SUN_ATTACHMENT
328 /* this is deep and utter perversion */
329 if ((pc = mutt_get_parameter ("conversions", ct->parameter)) != 0)
330 ct->encoding = mutt_check_encoding (pc);
335 /* Now get the subtype */
336 if ((subtype = strchr (s, '/'))) {
338 for (pc = subtype; *pc && !ISSPACE (*pc) && *pc != ';'; pc++);
340 ct->subtype = str_dup (subtype);
343 /* Finally, get the major type */
344 ct->type = mutt_check_mime_type (s);
346 #ifdef SUN_ATTACHMENT
347 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
348 ct->subtype = str_dup ("x-sun-attachment");
351 if (ct->type == TYPEOTHER) {
352 ct->xtype = str_dup (s);
355 if (ct->subtype == NULL) {
356 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
357 * field, so we can attempt to convert the type to BODY here.
359 if (ct->type == TYPETEXT)
360 ct->subtype = str_dup ("plain");
361 else if (ct->type == TYPEAUDIO)
362 ct->subtype = str_dup ("basic");
363 else if (ct->type == TYPEMESSAGE)
364 ct->subtype = str_dup ("rfc822");
365 else if (ct->type == TYPEOTHER) {
366 char buffer[SHORT_STRING];
368 ct->type = TYPEAPPLICATION;
369 snprintf (buffer, sizeof (buffer), "x-%s", s);
370 ct->subtype = str_dup (buffer);
373 ct->subtype = str_dup ("x-unknown");
376 /* Default character set for text types. */
377 if (ct->type == TYPETEXT) {
378 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
379 mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
381 mutt_get_first_charset (AssumedCharset),
387 static void parse_content_disposition (char *s, BODY * ct)
391 if (!ascii_strncasecmp ("inline", s, 6))
392 ct->disposition = DISPINLINE;
393 else if (!ascii_strncasecmp ("form-data", s, 9))
394 ct->disposition = DISPFORMDATA;
396 ct->disposition = DISPATTACH;
398 /* Check to see if a default filename was given */
399 if ((s = strchr (s, ';')) != NULL) {
403 mutt_get_parameter ("filename",
404 (parms = parse_parameters (s)))) != 0)
405 str_replace (&ct->filename, s);
406 if ((s = mutt_get_parameter ("name", parms)) != 0)
407 ct->form_name = str_dup (s);
408 mutt_free_parameter (&parms);
413 * fp stream to read from
415 * digest 1 if reading subparts of a multipart/digest, 0
419 BODY *mutt_read_mime_header (FILE * fp, int digest)
421 BODY *p = mutt_new_body ();
423 char *line = mem_malloc (LONG_STRING);
424 size_t linelen = LONG_STRING;
426 p->hdr_offset = ftello (fp);
428 p->encoding = ENC7BIT; /* default from RFC1521 */
429 p->type = digest ? TYPEMESSAGE : TYPETEXT;
430 p->disposition = DISPINLINE;
432 while (*(line = mutt_read_rfc822_line (fp, line, &linelen)) != 0) {
433 /* Find the value of the current header */
434 if ((c = strchr (line, ':'))) {
439 debug_print (1, ("skipping empty header field: %s\n", line));
444 debug_print (1, ("bogus MIME header: %s\n", line));
448 if (!ascii_strncasecmp ("content-", line, 8)) {
449 if (!ascii_strcasecmp ("type", line + 8))
450 mutt_parse_content_type (c, p);
451 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
452 p->encoding = mutt_check_encoding (c);
453 else if (!ascii_strcasecmp ("disposition", line + 8))
454 parse_content_disposition (c, p);
455 else if (!ascii_strcasecmp ("description", line + 8)) {
456 str_replace (&p->description, c);
457 rfc2047_decode (&p->description);
460 #ifdef SUN_ATTACHMENT
461 else if (!ascii_strncasecmp ("x-sun-", line, 6)) {
462 if (!ascii_strcasecmp ("data-type", line + 6))
463 mutt_parse_content_type (c, p);
464 else if (!ascii_strcasecmp ("encoding-info", line + 6))
465 p->encoding = mutt_check_encoding (c);
466 else if (!ascii_strcasecmp ("content-lines", line + 6))
467 mutt_set_parameter ("content-lines", c, &(p->parameter));
468 else if (!ascii_strcasecmp ("data-description", line + 6)) {
469 str_replace (&p->description, c);
470 rfc2047_decode (&p->description);
475 p->offset = ftello (fp); /* Mark the start of the real data */
476 if (p->type == TYPETEXT && !p->subtype)
477 p->subtype = str_dup ("plain");
478 else if (p->type == TYPEMESSAGE && !p->subtype)
479 p->subtype = str_dup ("rfc822");
486 void mutt_parse_part (FILE * fp, BODY * b)
492 #ifdef SUN_ATTACHMENT
493 if (!ascii_strcasecmp (b->subtype, "x-sun-attachment"))
497 bound = mutt_get_parameter ("boundary", b->parameter);
499 fseeko (fp, b->offset, SEEK_SET);
500 b->parts = mutt_parse_multipart (fp, bound,
501 b->offset + b->length,
502 ascii_strcasecmp ("digest",
508 fseeko (fp, b->offset, SEEK_SET);
509 if (mutt_is_message_type (b->type, b->subtype))
510 b->parts = mutt_parse_messageRFC822 (fp, b);
511 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
512 b->parts = mutt_read_mime_header (fp, 0);
522 /* try to recover from parsing error */
525 str_replace (&b->subtype, "plain");
529 /* parse a MESSAGE/RFC822 body
532 * fp stream to read from
534 * parent structure which contains info about the message/rfc822
537 * NOTE: this assumes that `parent->length' has been set!
540 BODY *mutt_parse_messageRFC822 (FILE * fp, BODY * parent)
544 parent->hdr = mutt_new_header ();
545 parent->hdr->offset = ftello (fp);
546 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
547 msg = parent->hdr->content;
549 /* ignore the length given in the content-length since it could be wrong
550 and we already have the info to calculate the correct length */
551 /* if (msg->length == -1) */
552 msg->length = parent->length - (msg->offset - parent->offset);
554 /* if body of this message is empty, we can end up with a negative length */
558 mutt_parse_part (fp, msg);
562 /* parse a multipart structure
565 * fp stream to read from
567 * boundary body separator
569 * end_off length of the multipart body (used when the final
570 * boundary is missing to avoid reading too far)
572 * digest 1 if reading a multipart/digest, 0 otherwise
575 BODY *mutt_parse_multipart (FILE * fp, const char *boundary, LOFF_T end_off,
578 #ifdef SUN_ATTACHMENT
581 int blen, len, crlf = 0;
582 char buffer[LONG_STRING];
583 BODY *head = 0, *last = 0, *new = 0;
585 int final = 0; /* did we see the ending boundary? */
588 mutt_error _("multipart message has no boundary parameter!");
593 blen = str_len (boundary);
594 while (ftello (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL) {
595 len = str_len (buffer);
597 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
599 if (buffer[0] == '-' && buffer[1] == '-' &&
600 str_ncmp (buffer + 2, boundary, blen) == 0) {
602 last->length = ftello (fp) - last->offset - len - 1 - crlf;
603 if (last->parts && last->parts->length == 0)
604 last->parts->length =
605 ftello (fp) - last->parts->offset - len - 1 - crlf;
606 /* if the body is empty, we can end up with a -1 length */
607 if (last->length < 0)
611 /* Remove any trailing whitespace, up to the length of the boundary */
612 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
615 /* Check for the end boundary */
616 if (str_cmp (buffer + blen + 2, "--") == 0) {
618 break; /* done parsing */
620 else if (buffer[2 + blen] == 0) {
621 new = mutt_read_mime_header (fp, digest);
623 #ifdef SUN_ATTACHMENT
624 if (mutt_get_parameter ("content-lines", new->parameter)) {
626 atoi (mutt_get_parameter ("content-lines", new->parameter));
628 if (ftello (fp) >= end_off
629 || fgets (buffer, LONG_STRING, fp) == NULL)
635 * Consistency checking - catch
636 * bad attachment end boundaries
639 if (new->offset > end_off) {
640 mutt_free_body (&new);
653 /* in case of missing end boundary, set the length to something reasonable */
654 if (last && last->length == 0 && !final)
655 last->length = end_off - last->offset;
657 /* parse recursive MIME parts */
658 for (last = head; last; last = last->next)
659 mutt_parse_part (fp, last);
664 static const char *uncomment_timezone (char *buf, size_t buflen,
671 return tz; /* no need to do anything */
674 if ((p = strpbrk (tz, " )")) == NULL)
677 if (len > buflen - 1)
679 memcpy (buf, tz, len);
686 unsigned char zhours;
687 unsigned char zminutes;
688 unsigned char zoccident; /* west of UTC? */
691 "aat", 1, 0, 1}, /* Atlantic Africa Time */
693 "adt", 4, 0, 0}, /* Arabia DST */
695 "ast", 3, 0, 0}, /* Arabia */
696 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
698 "bst", 1, 0, 0}, /* British DST */
700 "cat", 1, 0, 0}, /* Central Africa */
703 "cest", 2, 0, 0}, /* Central Europe DST */
705 "cet", 1, 0, 0}, /* Central Europe */
708 /*{ "cst", 8, 0, 0 }, *//* China */
709 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
711 "eat", 3, 0, 0}, /* East Africa */
714 "eest", 3, 0, 0}, /* Eastern Europe DST */
716 "eet", 2, 0, 0}, /* Eastern Europe */
718 "egst", 0, 0, 0}, /* Eastern Greenland DST */
720 "egt", 1, 0, 1}, /* Eastern Greenland */
724 "gst", 4, 0, 0}, /* Presian Gulf */
726 "hkt", 8, 0, 0}, /* Hong Kong */
728 "ict", 7, 0, 0}, /* Indochina */
730 "idt", 3, 0, 0}, /* Israel DST */
732 "ist", 2, 0, 0}, /* Israel */
733 /*{ "ist", 5, 30, 0 }, *//* India */
735 "jst", 9, 0, 0}, /* Japan */
737 "kst", 9, 0, 0}, /* Korea */
740 "met", 1, 0, 0}, /* this is now officially CET */
742 "msd", 4, 0, 0}, /* Moscow DST */
744 "msk", 3, 0, 0}, /* Moscow */
747 "nzdt", 13, 0, 0}, /* New Zealand DST */
749 "nzst", 12, 0, 0}, /* New Zealand */
753 "sat", 2, 0, 0}, /* South Africa */
755 "smt", 4, 0, 0}, /* Seychelles */
757 "sst", 11, 0, 1}, /* Samoa */
758 /*{ "sst", 8, 0, 0 }, *//* Singapore */
761 "wat", 0, 0, 0}, /* West Africa */
763 "west", 1, 0, 0}, /* Western Europe DST */
765 "wet", 0, 0, 0}, /* Western Europe */
767 "wgst", 2, 0, 1}, /* Western Greenland DST */
769 "wgt", 3, 0, 1}, /* Western Greenland */
771 "wst", 8, 0, 0}, /* Western Australia */
774 /* parses a date string in RFC822 format:
776 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
778 * This routine assumes that `h' has been initialized to 0. the `timezone'
779 * field is optional, defaulting to +0000 if missing.
781 time_t mutt_parse_date (const char *s, HEADER * h)
793 char tzstr[SHORT_STRING];
794 char scratch[SHORT_STRING];
796 /* Don't modify our argument. Fixed-size buffer is ok here since
797 * the date format imposes a natural limit.
800 strfcpy (scratch, s, sizeof (scratch));
802 /* kill the day of the week, if it exists. */
803 if ((t = strchr (scratch, ',')))
809 memset (&tm, 0, sizeof (tm));
811 while ((t = strtok (t, " \t")) != NULL) {
813 case 0: /* day of the month */
814 if (!isdigit ((unsigned char) *t))
816 tm.tm_mday = atoi (t);
821 case 1: /* month of the year */
822 if ((i = mutt_check_month (t)) < 0)
828 tm.tm_year = atoi (t);
831 else if (tm.tm_year >= 1900)
835 case 3: /* time of day */
836 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3);
837 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
840 debug_print (1, ("could not process time format: %s\n", t));
848 case 4: /* timezone */
849 /* sometimes we see things like (MST) or (-0700) so attempt to
850 * compensate by uncommenting the string if non-RFC822 compliant
852 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
854 if (*ptz == '+' || *ptz == '-') {
855 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
856 && isdigit ((unsigned char) ptz[1])
857 && isdigit ((unsigned char) ptz[2])
858 && isdigit ((unsigned char) ptz[3])
859 && isdigit ((unsigned char) ptz[4])) {
860 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
861 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
870 tz = bsearch (ptz, TimeZones, sizeof TimeZones / sizeof (struct tz_t),
871 sizeof (struct tz_t),
872 (int (*)(const void *, const void *)) ascii_strcasecmp
873 /* This is safe to do: A pointer to a struct equals
874 * a pointer to its first element*/ );
878 zminutes = tz->zminutes;
879 zoccident = tz->zoccident;
882 /* ad hoc support for the European MET (now officially CET) TZ */
883 if (ascii_strcasecmp (t, "MET") == 0) {
884 if ((t = strtok (NULL, " \t")) != NULL) {
885 if (!ascii_strcasecmp (t, "DST"))
890 tz_offset = zhours * 3600 + zminutes * 60;
892 tz_offset = -tz_offset;
899 if (count < 4) { /* don't check for missing timezone */
900 debug_print (1, ("error parsing date format, using received time\n"));
906 h->zminutes = zminutes;
907 h->zoccident = zoccident;
910 return (mutt_mktime (&tm, 0) + tz_offset);
913 /* extract the first substring that looks like a message-id */
914 static char *extract_message_id (const char *s)
920 if ((s = strchr (s, '<')) == NULL || (p = strchr (s, '>')) == NULL)
922 l = (size_t) (p - s) + 1;
923 r = mem_malloc (l + 1);
929 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
935 if (cur->content->type != TYPEMESSAGE
936 && cur->content->type != TYPEMULTIPART)
937 break; /* nothing to do */
939 if (cur->content->parts)
940 break; /* The message was parsed earlier. */
942 if ((msg = mx_open_message (ctx, cur->msgno))) {
943 mutt_parse_part (msg->fp, cur->content);
946 cur->security = crypt_query (cur->content);
948 mx_close_message (&msg);
951 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);
954 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
955 short user_hdrs, short weed, short do_2047,
964 switch (ascii_tolower (line[0])) {
966 if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
967 e->to = rfc822_parse_adrlist (e->to, p);
970 else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
971 e->from = rfc822_parse_adrlist (e->from, p);
977 if (ascii_strcasecmp (line + 1, "cc") == 0) {
978 e->bcc = rfc822_parse_adrlist (e->bcc, p);
984 if (ascii_strcasecmp (line + 1, "c") == 0) {
985 e->cc = rfc822_parse_adrlist (e->cc, p);
988 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
989 if (ascii_strcasecmp (line + 8, "type") == 0) {
991 mutt_parse_content_type (p, hdr->content);
994 else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
996 hdr->content->encoding = mutt_check_encoding (p);
999 else if (ascii_strcasecmp (line + 8, "length") == 0) {
1001 if ((hdr->content->length = atoi (p)) < 0)
1002 hdr->content->length = -1;
1006 else if (ascii_strcasecmp (line + 8, "description") == 0) {
1008 str_replace (&hdr->content->description, p);
1009 rfc2047_decode (&hdr->content->description);
1013 else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
1015 parse_content_disposition (p, hdr->content);
1022 if (!ascii_strcasecmp ("ate", line + 1)) {
1023 str_replace (&e->date, p);
1025 hdr->date_sent = mutt_parse_date (p, hdr);
1031 if (!ascii_strcasecmp ("xpires", line + 1) &&
1032 hdr && mutt_parse_date (p, NULL) < time (NULL))
1037 if (!ascii_strcasecmp ("rom", line + 1)) {
1038 e->from = rfc822_parse_adrlist (e->from, p);
1039 /* don't leave from info NULL if there's an invalid address (or
1040 * whatever) in From: field; mutt would just display it as empty
1041 * and mark mail/(esp.) news article as your own. aaargh! this
1042 * bothered me for _years_ */
1044 e->from = rfc822_new_address ();
1045 e->from->personal = str_dup (p);
1050 else if (!str_casecmp (line + 1, "ollowup-to")) {
1051 if (!e->followup_to) {
1052 str_skip_trailws (p);
1053 e->followup_to = str_dup (str_skip_initws (p));
1061 if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
1062 mutt_free_list (&e->in_reply_to);
1063 e->in_reply_to = mutt_parse_references (p, 1);
1069 if (!ascii_strcasecmp (line + 1, "ines")) {
1071 hdr->lines = atoi (p);
1074 * HACK - mutt has, for a very short time, produced negative
1075 * Lines header values. Ignore them.
1083 else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
1084 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1085 if (strncmp (p, "NO", 2)) {
1088 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
1090 if (!(end = strchr (beg, '>')))
1093 /* Take the first mailto URL */
1094 if (url_check_scheme (beg) == U_MAILTO) {
1095 mem_free (&e->list_post);
1096 e->list_post = str_substrdup (beg, end);
1106 if (!ascii_strcasecmp (line + 1, "ime-version")) {
1111 else if (!ascii_strcasecmp (line + 1, "essage-id")) {
1112 /* We add a new "Message-ID:" when building a message */
1113 mem_free (&e->message_id);
1114 e->message_id = extract_message_id (p);
1117 else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
1118 if (!ascii_strcasecmp (line + 5, "reply-to")) {
1119 /* override the Reply-To: field */
1120 rfc822_free_address (&e->reply_to);
1121 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1124 else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1125 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1133 if (!str_casecmp (line + 1, "ewsgroups")) {
1134 mem_free (&e->newsgroups);
1135 str_skip_trailws (p);
1136 e->newsgroups = str_dup (str_skip_initws (p));
1143 /* field `Organization:' saves only for pager! */
1144 if (!str_casecmp (line + 1, "rganization")) {
1145 if (!e->organization && str_casecmp (p, "unknown"))
1146 e->organization = str_dup (p);
1151 if (!ascii_strcasecmp (line + 1, "eferences")) {
1152 mutt_free_list (&e->references);
1153 e->references = mutt_parse_references (p, 0);
1156 else if (!ascii_strcasecmp (line + 1, "eply-to")) {
1157 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1160 else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
1161 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1164 else if (!ascii_strcasecmp (line + 1, "eceived")) {
1165 if (hdr && !hdr->received) {
1166 char *d = strchr (p, ';');
1169 hdr->received = mutt_parse_date (d + 1, NULL);
1175 if (!ascii_strcasecmp (line + 1, "ubject")) {
1177 e->subject = str_dup (p);
1180 else if (!ascii_strcasecmp (line + 1, "ender")) {
1181 e->sender = rfc822_parse_adrlist (e->sender, p);
1184 else if (!ascii_strcasecmp (line + 1, "tatus")) {
1203 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1204 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1205 e->supersedes = str_dup (p);
1209 if (ascii_strcasecmp (line + 1, "o") == 0) {
1210 e->to = rfc822_parse_adrlist (e->to, p);
1216 if (ascii_strcasecmp (line + 1, "-status") == 0) {
1237 else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1238 e->x_label = str_dup (p);
1242 else if (!str_casecmp (line + 1, "-comment-to")) {
1243 if (!e->x_comment_to)
1244 e->x_comment_to = str_dup (p);
1247 else if (!str_casecmp (line + 1, "ref")) {
1249 e->xref = str_dup (p);
1258 /* Keep track of the user-defined headers */
1259 if (!matched && user_hdrs) {
1260 /* restore the original line */
1261 line[str_len (line)] = ':';
1263 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1264 && !mutt_matches_ignore (line, UnIgnore))
1268 last->next = mutt_new_list ();
1272 last = e->userhdrs = mutt_new_list ();
1273 last->data = str_dup (line);
1275 rfc2047_decode (&last->data);
1285 /* mutt_read_rfc822_header() -- parses a RFC822 header
1289 * f stream to read from
1291 * hdr header structure of current message (optional).
1293 * user_hdrs If set, store user headers. Used for recall-message and
1296 * weed If this parameter is set and the user has activated the
1297 * $weed option, honor the header weed list for user headers.
1298 * Used for recall-message.
1300 * Returns: newly allocated envelope structure. You should free it by
1301 * mutt_free_envelope() when envelope stay unneeded.
1303 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1306 ENVELOPE *e = mutt_new_envelope ();
1308 char *line = mem_malloc (LONG_STRING);
1312 size_t linelen = LONG_STRING;
1313 char buf[LONG_STRING + 1];
1316 if (hdr->content == NULL) {
1317 hdr->content = mutt_new_body ();
1319 /* set the defaults from RFC1521 */
1320 hdr->content->type = TYPETEXT;
1321 hdr->content->subtype = str_dup ("plain");
1322 hdr->content->encoding = ENC7BIT;
1323 hdr->content->length = -1;
1325 /* RFC 2183 says this is arbitrary */
1326 hdr->content->disposition = DISPINLINE;
1330 while ((loc = ftello (f)),
1331 *(line = mutt_read_rfc822_line (f, line, &linelen)) != 0) {
1334 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1335 char return_path[LONG_STRING];
1338 /* some bogus MTAs will quote the original "From " line */
1339 if (str_ncmp (">From ", line, 6) == 0)
1340 continue; /* just ignore */
1341 else if (is_from (line, return_path, sizeof (return_path), &t)) {
1342 /* MH somtimes has the From_ line in the middle of the header! */
1343 if (hdr && !hdr->received)
1344 hdr->received = t - mutt_local_tz (t);
1349 break; /* end of header */
1354 if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1355 if (!rx_list_match (NoSpamList, line)) {
1357 /* if spam tag already exists, figure out how to amend it */
1358 if (e->spam && *buf) {
1359 /* If SpamSep defined, append with separator */
1361 mutt_buffer_addstr (e->spam, SpamSep);
1362 mutt_buffer_addstr (e->spam, buf);
1365 /* else overwrite */
1367 e->spam->dptr = e->spam->data;
1368 *e->spam->dptr = '\0';
1369 mutt_buffer_addstr (e->spam, buf);
1373 /* spam tag is new, and match expr is non-empty; copy */
1374 else if (!e->spam && *buf) {
1375 e->spam = mutt_buffer_from (NULL, buf);
1378 /* match expr is empty; plug in null string if no existing tag */
1379 else if (!e->spam) {
1380 e->spam = mutt_buffer_from (NULL, "");
1383 if (e->spam && e->spam->data)
1384 debug_print (5, ("spam = %s\n", e->spam->data));
1392 continue; /* skip empty header fields */
1395 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1402 hdr->content->hdr_offset = hdr->offset;
1403 hdr->content->offset = ftello (f);
1404 rfc2047_decode_envelope (e);
1405 /* check for missing or invalid date */
1406 if (hdr->date_sent <= 0) {
1407 debug_print (1, ("no date found, using received "
1408 "time from msg separator\n"));
1409 hdr->date_sent = hdr->received;
1416 ADDRESS *mutt_parse_adrlist (ADDRESS * p, const char *s)
1420 /* check for a simple whitespace separated list of addresses */
1421 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1422 char tmp[HUGE_STRING];
1425 strfcpy (tmp, s, sizeof (tmp));
1427 while ((r = strtok (r, " \t")) != NULL) {
1428 p = rfc822_parse_adrlist (p, r);
1433 p = rfc822_parse_adrlist (p, s);
1439 /* Compares mime types to the ok and except lists */
1440 int count_body_parts_check(LIST **checklist, BODY *b, int dflt) {
1444 /* If list is null, use default behavior. */
1450 for (type = *checklist; type; type = type->next) {
1451 a = (ATTACH_MATCH *)type->data;
1452 debug_print(5, ("cbpc: %s %d/%s ?? %s/%s [%d]... ",
1453 dflt ? "[OK] " : "[EXCL] ",
1454 b->type, b->subtype, a->major, a->minor, a->major_int));
1455 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1456 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1457 debug_print(5, ("yes\n"));
1460 debug_print(5, ("no\n"));
1466 #define AT_COUNT(why) { shallcount = 1; }
1467 #define AT_NOCOUNT(why) { shallcount = 0; }
1469 int count_body_parts (BODY *body, int flags) {
1471 int shallcount, shallrecurse;
1477 for (bp = body; bp != NULL; bp = bp->next) {
1478 /* Initial disposition is to count and not to recurse this part. */
1479 AT_COUNT("default");
1482 debug_print(5, ("bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1483 bp->description ? bp->description : ("none"),
1484 bp->filename ? bp->filename :
1485 bp->d_filename ? bp->d_filename : "(none)",
1486 bp->type, bp->subtype ? bp->subtype : "*"));
1488 if (bp->type == TYPEMESSAGE) {
1491 /* If it's an external body pointer, don't recurse it. */
1492 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1495 /* Don't count containers if they're top-level. */
1496 if (flags & M_PARTS_TOPLEVEL)
1497 AT_NOCOUNT("top-level message/*");
1498 } else if (bp->type == TYPEMULTIPART) {
1499 /* Always recurse multiparts, except multipart/alternative. */
1501 if (!str_casecmp(bp->subtype, "alternative"))
1504 /* Don't count containers if they're top-level. */
1505 if (flags & M_PARTS_TOPLEVEL)
1506 AT_NOCOUNT("top-level multipart");
1509 if (bp->disposition == DISPINLINE &&
1510 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1511 AT_NOCOUNT("ignore fundamental inlines");
1513 /* If this body isn't scheduled for enumeration already, don't bother
1514 * profiling it further. */
1517 /* Turn off shallcount if message type is not in ok list,
1518 * or if it is in except list. Check is done separately for
1519 * inlines vs. attachments.
1522 if (bp->disposition == DISPATTACH) {
1523 if (!count_body_parts_check(&AttachAllow, bp, 1))
1524 AT_NOCOUNT("attach not allowed");
1525 if (count_body_parts_check(&AttachExclude, bp, 0))
1526 AT_NOCOUNT("attach excluded");
1528 if (!count_body_parts_check(&InlineAllow, bp, 1))
1529 AT_NOCOUNT("inline not allowed");
1530 if (count_body_parts_check(&InlineExclude, bp, 0))
1531 AT_NOCOUNT("excluded");
1537 bp->attach_qualifies = shallcount ? 1 : 0;
1539 debug_print(5, ("cbp: %08x shallcount = %d\n", (unsigned int)bp, shallcount));
1542 debug_print(5, ("cbp: %08x pre count = %d\n", (unsigned int)bp, count));
1543 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1544 count += bp->attach_count;
1545 debug_print(5, ("cbp: %08x post count = %d\n", (unsigned int)bp, count));
1549 debug_print(5, ("bp: return %d\n", count < 0 ? 0 : count));
1550 return count < 0 ? 0 : count;
1553 int mutt_count_body_parts (HEADER *hdr, int flags) {
1554 if (!option (OPTCOUNTATTACH))
1556 if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1557 return hdr->attach_total;
1559 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1560 hdr->attach_total = count_body_parts(hdr->content, flags | M_PARTS_TOPLEVEL);
1562 hdr->attach_total = 0;
1564 hdr->attach_valid = 1;
1565 return hdr->attach_total;