2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
21 * Copyright notice from original mutt:
22 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
24 * This file is part of mutt-ng, see http://www.muttng.org/.
25 * It's licensed under the GNU General Public License,
26 * please see the file GPL in the top level source directory.
31 #include <lib-lib/mem.h>
32 #include <lib-lib/str.h>
33 #include <lib-lib/ascii.h>
34 #include <lib-lib/macros.h>
35 #include <lib-lib/buffer.h>
36 #include <lib-lib/date.h>
38 #include <lib-crypt/crypt.h>
41 #include "recvattach.h"
46 #include "lib/debug.h"
50 /* Reads an arbitrarily long header field, and looks ahead for continuation
51 * lines. ``line'' must point to a dynamically allocated string; it is
52 * increased if more space is required to fit the whole line.
54 char *mutt_read_rfc822_line (FILE * f, char *line, size_t * linelen)
61 if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */
62 (ISSPACE (*line) && !offset)) { /* end of headers */
67 buf += m_strlen(buf) - 1;
69 /* we did get a full line. remove trailing space */
70 while (ISSPACE (*buf))
71 *buf-- = 0; /* we cannot come beyond line's beginning because
72 * it begins with a non-space */
74 /* check to see if the next line is a continuation line */
75 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');
83 *++buf = ' '; /* string is still terminated because we removed
84 at least one whitespace char above */
89 if (*linelen < offset + STRING) {
92 p_realloc(&line, *linelen);
99 LIST *mutt_parse_references (char *s, int in_reply_to)
101 LIST *t, *lst = NULL;
103 char *o = NULL, *new, *at;
105 while ((s = strtok (s, " \t;")) != NULL) {
107 * some mail clients add other garbage besides message-ids, so do a quick
108 * check to make sure this looks like a valid message-id
109 * some idiotic clients also break their message-ids between lines, deal
110 * with that too (give up if it's more than two lines, though)
117 if (s[n - 1] != '>') {
127 if (s[m - 1] == '>') {
128 new = p_new(char, n + m + 1);
129 strcpy (new, o); /* __STRCPY_CHECKED__ */
130 strcpy (new + n, s); /* __STRCPY_CHECKED__ */
134 /* make sure that this really does look like a message-id.
135 * it should have exactly one @, and if we're looking at
136 * an in-reply-to header, make sure that the part before
137 * the @ has more than eight characters or it's probably
140 if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
141 || (in_reply_to && at - new <= 8))
157 int mutt_check_encoding (const char *c)
159 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit") - 1) == 0)
161 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit") - 1) == 0)
163 else if (ascii_strncasecmp ("binary", c, sizeof ("binary") - 1) == 0)
166 if (ascii_strncasecmp
167 ("quoted-printable", c, sizeof ("quoted-printable") - 1) == 0)
168 return (ENCQUOTEDPRINTABLE);
169 else if (ascii_strncasecmp ("base64", c, sizeof ("base64") - 1) == 0)
171 else if (ascii_strncasecmp ("x-uuencode", c, sizeof ("x-uuencode") - 1) == 0)
172 return (ENCUUENCODED);
177 static PARAMETER *parse_parameters (const char *s)
179 PARAMETER *head = 0, *cur = 0, *new;
180 char buffer[LONG_STRING];
184 debug_print (2, ("`%s'\n", s));
187 if ((p = strpbrk (s, "=;")) == NULL) {
188 debug_print (1, ("malformed parameter: %s\n", s));
192 /* if we hit a ; now the parameter has no value, just skip it */
196 new = mutt_new_parameter ();
198 new->attribute = p_dupstr(s, i);
200 /* remove whitespace from the end of the attribute name */
201 while (ISSPACE (new->attribute[--i]))
202 new->attribute[i] = 0;
204 s = vskipspaces(p + 1); /* skip over the = */
210 for (i = 0; *s && i < sizeof (buffer) - 1; i++, s++) {
211 if (!option (OPTSTRICTMIME)) {
212 /* As iso-2022-* has a characer of '"' with non-ascii state,
214 if (*s == 0x1b && i < sizeof (buffer) - 2) {
215 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
221 if (state_ascii && *s == '"')
224 /* Quote the next character */
234 s++; /* skip over the " */
237 for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1;
243 new->value = m_strdup(buffer);
245 debug_print (2, ("`%s' = `%s'\n", new->attribute ? new->attribute : "",
246 new->value ? new->value : ""));
248 /* Add this parameter to the list */
257 debug_print (1, ("parameter with no value: %s\n", s));
261 /* Find the next parameter */
262 if (*s != ';' && (s = strchr (s, ';')) == NULL)
263 break; /* no more parameters */
266 /* Move past any leading whitespace */
267 s = vskipspaces(s + 1);
269 while (*s == ';'); /* skip empty parameters */
274 rfc2231_decode_parameters (&head);
278 int mutt_check_mime_type (const char *s)
280 if (ascii_strcasecmp ("text", s) == 0)
282 else if (ascii_strcasecmp ("multipart", s) == 0)
283 return TYPEMULTIPART;
284 else if (ascii_strcasecmp ("application", s) == 0)
285 return TYPEAPPLICATION;
286 else if (ascii_strcasecmp ("message", s) == 0)
288 else if (ascii_strcasecmp ("image", s) == 0)
290 else if (ascii_strcasecmp ("audio", s) == 0)
292 else if (ascii_strcasecmp ("video", s) == 0)
294 else if (ascii_strcasecmp ("model", s) == 0)
296 else if (ascii_strcasecmp ("*", s) == 0)
298 else if (ascii_strcasecmp (".*", s) == 0)
304 void mutt_parse_content_type (char *s, BODY * ct)
309 p_delete(&ct->subtype);
310 mutt_free_parameter (&ct->parameter);
312 /* First extract any existing parameters */
313 if ((pc = strchr (s, ';')) != NULL) {
315 while (*pc && ISSPACE (*pc))
317 ct->parameter = parse_parameters (pc);
319 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
320 * but if a filename has already been set in the content-disposition,
321 * let that take precedence, and don't set it here */
322 if ((pc = mutt_get_parameter ("name", ct->parameter)) != 0
324 ct->filename = m_strdup(pc);
327 /* Now get the subtype */
328 if ((subtype = strchr (s, '/'))) {
330 for (pc = subtype; *pc && !ISSPACE (*pc) && *pc != ';'; pc++);
332 ct->subtype = m_strdup(subtype);
335 /* Finally, get the major type */
336 ct->type = mutt_check_mime_type (s);
338 if (ct->type == TYPEOTHER) {
339 ct->xtype = m_strdup(s);
342 if (ct->subtype == NULL) {
343 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
344 * field, so we can attempt to convert the type to BODY here.
346 if (ct->type == TYPETEXT)
347 ct->subtype = m_strdup("plain");
348 else if (ct->type == TYPEAUDIO)
349 ct->subtype = m_strdup("basic");
350 else if (ct->type == TYPEMESSAGE)
351 ct->subtype = m_strdup("rfc822");
352 else if (ct->type == TYPEOTHER) {
353 char buffer[SHORT_STRING];
355 ct->type = TYPEAPPLICATION;
356 snprintf (buffer, sizeof (buffer), "x-%s", s);
357 ct->subtype = m_strdup(buffer);
360 ct->subtype = m_strdup("x-unknown");
363 /* Default character set for text types. */
364 if (ct->type == TYPETEXT) {
365 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
366 mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
368 mutt_get_first_charset (AssumedCharset),
374 static void parse_content_disposition (char *s, BODY * ct)
378 if (!ascii_strncasecmp ("inline", s, 6))
379 ct->disposition = DISPINLINE;
380 else if (!ascii_strncasecmp ("form-data", s, 9))
381 ct->disposition = DISPFORMDATA;
383 ct->disposition = DISPATTACH;
385 /* Check to see if a default filename was given */
386 if ((s = strchr (s, ';')) != NULL) {
387 s = vskipspaces(s + 1);
388 if ((s = mutt_get_parameter("filename",
389 (parms = parse_parameters (s)))) != 0)
390 m_strreplace(&ct->filename, s);
391 if ((s = mutt_get_parameter ("name", parms)) != 0)
392 ct->form_name = m_strdup(s);
393 mutt_free_parameter (&parms);
398 * fp stream to read from
400 * digest 1 if reading subparts of a multipart/digest, 0
404 BODY *mutt_read_mime_header (FILE * fp, int digest)
406 BODY *p = mutt_new_body ();
408 char *line = p_new(char, LONG_STRING);
409 size_t linelen = LONG_STRING;
411 p->hdr_offset = ftello (fp);
413 p->encoding = ENC7BIT; /* default from RFC1521 */
414 p->type = digest ? TYPEMESSAGE : TYPETEXT;
415 p->disposition = DISPINLINE;
417 while (*(line = mutt_read_rfc822_line (fp, line, &linelen)) != 0) {
418 /* Find the value of the current header */
419 if ((c = strchr (line, ':'))) {
423 debug_print (1, ("skipping empty header field: %s\n", line));
428 debug_print (1, ("bogus MIME header: %s\n", line));
432 if (!ascii_strncasecmp ("content-", line, 8)) {
433 if (!ascii_strcasecmp ("type", line + 8))
434 mutt_parse_content_type (c, p);
435 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
436 p->encoding = mutt_check_encoding (c);
437 else if (!ascii_strcasecmp ("disposition", line + 8))
438 parse_content_disposition (c, p);
439 else if (!ascii_strcasecmp ("description", line + 8)) {
440 m_strreplace(&p->description, c);
441 rfc2047_decode (&p->description);
445 p->offset = ftello (fp); /* Mark the start of the real data */
446 if (p->type == TYPETEXT && !p->subtype)
447 p->subtype = m_strdup("plain");
448 else if (p->type == TYPEMESSAGE && !p->subtype)
449 p->subtype = m_strdup("rfc822");
456 void mutt_parse_part (FILE * fp, BODY * b)
462 bound = mutt_get_parameter ("boundary", b->parameter);
463 fseeko (fp, b->offset, SEEK_SET);
464 b->parts = mutt_parse_multipart (fp, bound,
465 b->offset + b->length,
466 ascii_strcasecmp ("digest",
472 fseeko (fp, b->offset, SEEK_SET);
473 if (mutt_is_message_type (b->type, b->subtype))
474 b->parts = mutt_parse_messageRFC822 (fp, b);
475 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
476 b->parts = mutt_read_mime_header (fp, 0);
486 /* try to recover from parsing error */
489 m_strreplace(&b->subtype, "plain");
493 /* parse a MESSAGE/RFC822 body
496 * fp stream to read from
498 * parent structure which contains info about the message/rfc822
501 * NOTE: this assumes that `parent->length' has been set!
504 BODY *mutt_parse_messageRFC822 (FILE * fp, BODY * parent)
508 parent->hdr = mutt_new_header ();
509 parent->hdr->offset = ftello (fp);
510 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
511 msg = parent->hdr->content;
513 /* ignore the length given in the content-length since it could be wrong
514 and we already have the info to calculate the correct length */
515 /* if (msg->length == -1) */
516 msg->length = parent->length - (msg->offset - parent->offset);
518 /* if body of this message is empty, we can end up with a negative length */
522 mutt_parse_part (fp, msg);
526 /* parse a multipart structure
529 * fp stream to read from
531 * boundary body separator
533 * end_off length of the multipart body (used when the final
534 * boundary is missing to avoid reading too far)
536 * digest 1 if reading a multipart/digest, 0 otherwise
539 BODY *mutt_parse_multipart (FILE * fp, const char *boundary, off_t end_off,
542 int blen, len, crlf = 0;
543 char buffer[LONG_STRING];
544 BODY *head = 0, *last = 0, *new = 0;
546 int final = 0; /* did we see the ending boundary? */
549 mutt_error _("multipart message has no boundary parameter!");
554 blen = m_strlen(boundary);
555 while (ftello (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL) {
556 len = m_strlen(buffer);
558 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
560 if (buffer[0] == '-' && buffer[1] == '-' &&
561 m_strncmp(buffer + 2, boundary, blen) == 0) {
563 last->length = ftello (fp) - last->offset - len - 1 - crlf;
564 if (last->parts && last->parts->length == 0)
565 last->parts->length =
566 ftello (fp) - last->parts->offset - len - 1 - crlf;
567 /* if the body is empty, we can end up with a -1 length */
568 if (last->length < 0)
572 /* Remove any trailing whitespace, up to the length of the boundary */
573 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
576 /* Check for the end boundary */
577 if (m_strcmp(buffer + blen + 2, "--") == 0) {
579 break; /* done parsing */
581 else if (buffer[2 + blen] == 0) {
582 new = mutt_read_mime_header (fp, digest);
585 * Consistency checking - catch
586 * bad attachment end boundaries
589 if (new->offset > end_off) {
590 mutt_free_body (&new);
603 /* in case of missing end boundary, set the length to something reasonable */
604 if (last && last->length == 0 && !final)
605 last->length = end_off - last->offset;
607 /* parse recursive MIME parts */
608 for (last = head; last; last = last->next)
609 mutt_parse_part (fp, last);
614 static const char *uncomment_timezone (char *buf, size_t buflen,
621 return tz; /* no need to do anything */
622 tz = vskipspaces(tz + 1);
623 if ((p = strpbrk (tz, " )")) == NULL)
626 if (len > buflen - 1)
628 memcpy (buf, tz, len);
635 unsigned char zhours;
636 unsigned char zminutes;
637 unsigned char zoccident; /* west of UTC? */
640 "aat", 1, 0, 1}, /* Atlantic Africa Time */
642 "adt", 4, 0, 0}, /* Arabia DST */
644 "ast", 3, 0, 0}, /* Arabia */
645 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
647 "bst", 1, 0, 0}, /* British DST */
649 "cat", 1, 0, 0}, /* Central Africa */
652 "cest", 2, 0, 0}, /* Central Europe DST */
654 "cet", 1, 0, 0}, /* Central Europe */
657 /*{ "cst", 8, 0, 0 }, *//* China */
658 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
660 "eat", 3, 0, 0}, /* East Africa */
663 "eest", 3, 0, 0}, /* Eastern Europe DST */
665 "eet", 2, 0, 0}, /* Eastern Europe */
667 "egst", 0, 0, 0}, /* Eastern Greenland DST */
669 "egt", 1, 0, 1}, /* Eastern Greenland */
673 "gst", 4, 0, 0}, /* Presian Gulf */
675 "hkt", 8, 0, 0}, /* Hong Kong */
677 "ict", 7, 0, 0}, /* Indochina */
679 "idt", 3, 0, 0}, /* Israel DST */
681 "ist", 2, 0, 0}, /* Israel */
682 /*{ "ist", 5, 30, 0 }, *//* India */
684 "jst", 9, 0, 0}, /* Japan */
686 "kst", 9, 0, 0}, /* Korea */
689 "met", 1, 0, 0}, /* this is now officially CET */
691 "msd", 4, 0, 0}, /* Moscow DST */
693 "msk", 3, 0, 0}, /* Moscow */
696 "nzdt", 13, 0, 0}, /* New Zealand DST */
698 "nzst", 12, 0, 0}, /* New Zealand */
702 "sat", 2, 0, 0}, /* South Africa */
704 "smt", 4, 0, 0}, /* Seychelles */
706 "sst", 11, 0, 1}, /* Samoa */
707 /*{ "sst", 8, 0, 0 }, *//* Singapore */
710 "wat", 0, 0, 0}, /* West Africa */
712 "west", 1, 0, 0}, /* Western Europe DST */
714 "wet", 0, 0, 0}, /* Western Europe */
716 "wgst", 2, 0, 1}, /* Western Greenland DST */
718 "wgt", 3, 0, 1}, /* Western Greenland */
720 "wst", 8, 0, 0}, /* Western Australia */
723 /* parses a date string in RFC822 format:
725 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
727 * This routine assumes that `h' has been initialized to 0. the `timezone'
728 * field is optional, defaulting to +0000 if missing.
730 time_t mutt_parse_date (const char *s, HEADER * h)
742 char tzstr[SHORT_STRING];
743 char scratch[SHORT_STRING];
745 /* Don't modify our argument. Fixed-size buffer is ok here since
746 * the date format imposes a natural limit.
749 m_strcpy(scratch, sizeof(scratch), s);
751 /* kill the day of the week, if it exists. */
752 if ((t = strchr (scratch, ',')))
760 while ((t = strtok (t, " \t")) != NULL) {
762 case 0: /* day of the month */
763 if (!isdigit ((unsigned char) *t))
765 tm.tm_mday = atoi (t);
770 case 1: /* month of the year */
771 if ((i = mutt_check_month (t)) < 0)
777 tm.tm_year = atoi (t);
780 else if (tm.tm_year >= 1900)
784 case 3: /* time of day */
785 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3);
786 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
789 debug_print (1, ("could not process time format: %s\n", t));
797 case 4: /* timezone */
798 /* sometimes we see things like (MST) or (-0700) so attempt to
799 * compensate by uncommenting the string if non-RFC822 compliant
801 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
803 if (*ptz == '+' || *ptz == '-') {
804 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
805 && isdigit ((unsigned char) ptz[1])
806 && isdigit ((unsigned char) ptz[2])
807 && isdigit ((unsigned char) ptz[3])
808 && isdigit ((unsigned char) ptz[4])) {
809 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
810 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
819 tz = bsearch (ptz, TimeZones, sizeof TimeZones / sizeof (struct tz_t),
820 sizeof (struct tz_t),
821 (int (*)(const void *, const void *)) ascii_strcasecmp
822 /* This is safe to do: A pointer to a struct equals
823 * a pointer to its first element*/ );
827 zminutes = tz->zminutes;
828 zoccident = tz->zoccident;
831 /* ad hoc support for the European MET (now officially CET) TZ */
832 if (ascii_strcasecmp (t, "MET") == 0) {
833 if ((t = strtok (NULL, " \t")) != NULL) {
834 if (!ascii_strcasecmp (t, "DST"))
839 tz_offset = zhours * 3600 + zminutes * 60;
841 tz_offset = -tz_offset;
848 if (count < 4) { /* don't check for missing timezone */
849 debug_print (1, ("error parsing date format, using received time\n"));
855 h->zminutes = zminutes;
856 h->zoccident = zoccident;
859 return (mutt_mktime (&tm, 0) + tz_offset);
862 /* extract the first substring that looks like a message-id */
863 static char *extract_message_id(const char *s)
867 if ((s = strchr(s, '<')) == NULL || (p = strchr(s, '>')) == NULL)
869 return p_dupstr(s, (p - s) + 1);
872 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
878 if (cur->content->type != TYPEMESSAGE
879 && cur->content->type != TYPEMULTIPART)
880 break; /* nothing to do */
882 if (cur->content->parts)
883 break; /* The message was parsed earlier. */
885 if ((msg = mx_open_message (ctx, cur->msgno))) {
886 mutt_parse_part (msg->fp, cur->content);
888 cur->security = crypt_query (cur->content);
890 mx_close_message (&msg);
893 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);
896 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
897 short user_hdrs, short weed, short do_2047,
906 switch (ascii_tolower (line[0])) {
908 if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
909 e->to = rfc822_parse_adrlist (e->to, p);
912 else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
913 e->from = rfc822_parse_adrlist (e->from, p);
919 if (ascii_strcasecmp (line + 1, "cc") == 0) {
920 e->bcc = rfc822_parse_adrlist (e->bcc, p);
926 if (ascii_strcasecmp (line + 1, "c") == 0) {
927 e->cc = rfc822_parse_adrlist (e->cc, p);
930 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
931 if (ascii_strcasecmp (line + 8, "type") == 0) {
933 mutt_parse_content_type (p, hdr->content);
936 else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
938 hdr->content->encoding = mutt_check_encoding (p);
941 else if (ascii_strcasecmp (line + 8, "length") == 0) {
943 if ((hdr->content->length = atoi (p)) < 0)
944 hdr->content->length = -1;
948 else if (ascii_strcasecmp (line + 8, "description") == 0) {
950 m_strreplace(&hdr->content->description, p);
951 rfc2047_decode (&hdr->content->description);
955 else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
957 parse_content_disposition (p, hdr->content);
964 if (!ascii_strcasecmp ("ate", line + 1)) {
965 m_strreplace(&e->date, p);
967 hdr->date_sent = mutt_parse_date (p, hdr);
973 if (!ascii_strcasecmp ("xpires", line + 1) &&
974 hdr && mutt_parse_date (p, NULL) < time (NULL))
979 if (!ascii_strcasecmp ("rom", line + 1)) {
980 e->from = rfc822_parse_adrlist (e->from, p);
981 /* don't leave from info NULL if there's an invalid address (or
982 * whatever) in From: field; mutt would just display it as empty
983 * and mark mail/(esp.) news article as your own. aaargh! this
984 * bothered me for _years_ */
986 e->from = address_new ();
987 e->from->personal = m_strdup(p);
992 else if (!m_strcasecmp(line + 1, "ollowup-to")) {
993 if (!e->followup_to) {
995 e->followup_to = m_strdup(skipspaces(p));
1003 if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
1004 mutt_free_list (&e->in_reply_to);
1005 e->in_reply_to = mutt_parse_references (p, 1);
1011 if (!ascii_strcasecmp (line + 1, "ines")) {
1013 hdr->lines = atoi (p);
1016 * HACK - mutt has, for a very short time, produced negative
1017 * Lines header values. Ignore them.
1025 else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
1026 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1027 if (strncmp (p, "NO", 2)) {
1030 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
1032 if (!(end = strchr (beg, '>')))
1035 /* Take the first mailto URL */
1036 if (url_check_scheme (beg) == U_MAILTO) {
1037 p_delete(&e->list_post);
1038 e->list_post = p_dupstr(beg, end - beg);
1048 if (!ascii_strcasecmp (line + 1, "ime-version")) {
1053 else if (!ascii_strcasecmp (line + 1, "essage-id")) {
1054 /* We add a new "Message-ID:" when building a message */
1055 p_delete(&e->message_id);
1056 e->message_id = extract_message_id (p);
1059 else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
1060 if (!ascii_strcasecmp (line + 5, "reply-to")) {
1061 /* override the Reply-To: field */
1062 address_delete (&e->reply_to);
1063 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1066 else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1067 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1075 if (!m_strcasecmp(line + 1, "ewsgroups")) {
1076 p_delete(&e->newsgroups);
1078 e->newsgroups = m_strdup(skipspaces(p));
1085 /* field `Organization:' saves only for pager! */
1086 if (!m_strcasecmp(line + 1, "rganization")) {
1087 if (!e->organization && m_strcasecmp(p, "unknown"))
1088 e->organization = m_strdup(p);
1093 if (!ascii_strcasecmp (line + 1, "eferences")) {
1094 mutt_free_list (&e->references);
1095 e->references = mutt_parse_references (p, 0);
1098 else if (!ascii_strcasecmp (line + 1, "eply-to")) {
1099 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1102 else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
1103 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1106 else if (!ascii_strcasecmp (line + 1, "eceived")) {
1107 if (hdr && !hdr->received) {
1108 char *d = strchr (p, ';');
1111 hdr->received = mutt_parse_date (d + 1, NULL);
1117 if (!ascii_strcasecmp (line + 1, "ubject")) {
1119 e->subject = m_strdup(p);
1122 else if (!ascii_strcasecmp (line + 1, "ender")) {
1123 e->sender = rfc822_parse_adrlist (e->sender, p);
1126 else if (!ascii_strcasecmp (line + 1, "tatus")) {
1145 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1146 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1147 e->supersedes = m_strdup(p);
1151 if (ascii_strcasecmp (line + 1, "o") == 0) {
1152 e->to = rfc822_parse_adrlist (e->to, p);
1158 if (ascii_strcasecmp (line + 1, "-status") == 0) {
1179 else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1180 e->x_label = m_strdup(p);
1184 else if (!m_strcasecmp(line + 1, "-comment-to")) {
1185 if (!e->x_comment_to)
1186 e->x_comment_to = m_strdup(p);
1189 else if (!m_strcasecmp(line + 1, "ref")) {
1191 e->xref = m_strdup(p);
1200 /* Keep track of the user-defined headers */
1201 if (!matched && user_hdrs) {
1202 /* restore the original line */
1203 line[m_strlen(line)] = ':';
1205 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1206 && !mutt_matches_ignore (line, UnIgnore))
1210 last->next = mutt_new_list ();
1214 last = e->userhdrs = mutt_new_list ();
1215 last->data = m_strdup(line);
1217 rfc2047_decode (&last->data);
1227 /* mutt_read_rfc822_header() -- parses a RFC822 header
1231 * f stream to read from
1233 * hdr header structure of current message (optional).
1235 * user_hdrs If set, store user headers. Used for recall-message and
1238 * weed If this parameter is set and the user has activated the
1239 * $weed option, honor the header weed list for user headers.
1240 * Used for recall-message.
1242 * Returns: newly allocated envelope structure. You should free it by
1243 * mutt_free_envelope() when envelope stay unneeded.
1245 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1248 ENVELOPE *e = mutt_new_envelope ();
1250 char *line = p_new(char, LONG_STRING);
1254 size_t linelen = LONG_STRING;
1255 char buf[LONG_STRING + 1];
1258 if (hdr->content == NULL) {
1259 hdr->content = mutt_new_body ();
1261 /* set the defaults from RFC1521 */
1262 hdr->content->type = TYPETEXT;
1263 hdr->content->subtype = m_strdup("plain");
1264 hdr->content->encoding = ENC7BIT;
1265 hdr->content->length = -1;
1267 /* RFC 2183 says this is arbitrary */
1268 hdr->content->disposition = DISPINLINE;
1272 while ((loc = ftello (f)),
1273 *(line = mutt_read_rfc822_line (f, line, &linelen)) != 0) {
1276 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1277 char return_path[LONG_STRING];
1280 /* some bogus MTAs will quote the original "From " line */
1281 if (m_strncmp(">From ", line, 6) == 0)
1282 continue; /* just ignore */
1283 else if (is_from (line, return_path, sizeof (return_path), &t)) {
1284 /* MH somtimes has the From_ line in the middle of the header! */
1285 if (hdr && !hdr->received)
1286 hdr->received = t - mutt_local_tz (t);
1291 break; /* end of header */
1296 if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1297 if (!rx_list_match (NoSpamList, line)) {
1299 /* if spam tag already exists, figure out how to amend it */
1300 if (e->spam && *buf) {
1301 /* If SpamSep defined, append with separator */
1303 mutt_buffer_addstr (e->spam, SpamSep);
1304 mutt_buffer_addstr (e->spam, buf);
1307 /* else overwrite */
1309 e->spam->dptr = e->spam->data;
1310 *e->spam->dptr = '\0';
1311 mutt_buffer_addstr (e->spam, buf);
1315 /* spam tag is new, and match expr is non-empty; copy */
1316 else if (!e->spam && *buf) {
1317 e->spam = mutt_buffer_from (NULL, buf);
1320 /* match expr is empty; plug in null string if no existing tag */
1321 else if (!e->spam) {
1322 e->spam = mutt_buffer_from (NULL, "");
1325 if (e->spam && e->spam->data)
1326 debug_print (5, ("spam = %s\n", e->spam->data));
1333 continue; /* skip empty header fields */
1336 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1343 hdr->content->hdr_offset = hdr->offset;
1344 hdr->content->offset = ftello (f);
1345 rfc2047_decode_envelope(e);
1346 /* check for missing or invalid date */
1347 if (hdr->date_sent <= 0) {
1348 debug_print (1, ("no date found, using received "
1349 "time from msg separator\n"));
1350 hdr->date_sent = hdr->received;
1357 address_t *mutt_parse_adrlist (address_t * p, const char *s)
1361 /* check for a simple whitespace separated list of addresses */
1362 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1363 char tmp[HUGE_STRING];
1366 m_strcpy(tmp, sizeof(tmp), s);
1368 while ((r = strtok (r, " \t")) != NULL) {
1369 p = rfc822_parse_adrlist (p, r);
1374 p = rfc822_parse_adrlist (p, s);
1380 /* Compares mime types to the ok and except lists */
1381 int count_body_parts_check(LIST **checklist, BODY *b, int dflt) {
1385 /* If list is null, use default behavior. */
1391 for (type = *checklist; type; type = type->next) {
1392 a = (ATTACH_MATCH *)type->data;
1393 debug_print(5, ("cbpc: %s %d/%s ?? %s/%s [%d]... ",
1394 dflt ? "[OK] " : "[EXCL] ",
1395 b->type, b->subtype, a->major, a->minor, a->major_int));
1396 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1397 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1398 debug_print(5, ("yes\n"));
1401 debug_print(5, ("no\n"));
1407 #define AT_COUNT(why) { shallcount = 1; }
1408 #define AT_NOCOUNT(why) { shallcount = 0; }
1410 int count_body_parts (BODY *body, int flags) {
1412 int shallcount, shallrecurse;
1418 for (bp = body; bp != NULL; bp = bp->next) {
1419 /* Initial disposition is to count and not to recurse this part. */
1420 AT_COUNT("default");
1423 debug_print(5, ("bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1424 bp->description ? bp->description : ("none"),
1425 bp->filename ? bp->filename :
1426 bp->d_filename ? bp->d_filename : "(none)",
1427 bp->type, bp->subtype ? bp->subtype : "*"));
1429 if (bp->type == TYPEMESSAGE) {
1432 /* If it's an external body pointer, don't recurse it. */
1433 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1436 /* Don't count containers if they're top-level. */
1437 if (flags & M_PARTS_TOPLEVEL)
1438 AT_NOCOUNT("top-level message/*");
1439 } else if (bp->type == TYPEMULTIPART) {
1440 /* Always recurse multiparts, except multipart/alternative. */
1442 if (!m_strcasecmp(bp->subtype, "alternative"))
1445 /* Don't count containers if they're top-level. */
1446 if (flags & M_PARTS_TOPLEVEL)
1447 AT_NOCOUNT("top-level multipart");
1450 if (bp->disposition == DISPINLINE &&
1451 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1452 AT_NOCOUNT("ignore fundamental inlines");
1454 /* If this body isn't scheduled for enumeration already, don't bother
1455 * profiling it further. */
1458 /* Turn off shallcount if message type is not in ok list,
1459 * or if it is in except list. Check is done separately for
1460 * inlines vs. attachments.
1463 if (bp->disposition == DISPATTACH) {
1464 if (!count_body_parts_check(&AttachAllow, bp, 1))
1465 AT_NOCOUNT("attach not allowed");
1466 if (count_body_parts_check(&AttachExclude, bp, 0))
1467 AT_NOCOUNT("attach excluded");
1469 if (!count_body_parts_check(&InlineAllow, bp, 1))
1470 AT_NOCOUNT("inline not allowed");
1471 if (count_body_parts_check(&InlineExclude, bp, 0))
1472 AT_NOCOUNT("excluded");
1478 bp->attach_qualifies = shallcount ? 1 : 0;
1480 debug_print(5, ("cbp: %p shallcount = %d\n", bp, shallcount));
1483 debug_print(5, ("cbp: %p pre count = %d\n", bp, count));
1484 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1485 count += bp->attach_count;
1486 debug_print(5, ("cbp: %p post count = %d\n", bp, count));
1490 debug_print(5, ("bp: return %d\n", count < 0 ? 0 : count));
1491 return count < 0 ? 0 : count;
1494 int mutt_count_body_parts (HEADER *hdr, int flags) {
1495 if (!option (OPTCOUNTATTACH))
1497 if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1498 return hdr->attach_total;
1500 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1501 hdr->attach_total = count_body_parts(hdr->content, flags | M_PARTS_TOPLEVEL);
1503 hdr->attach_total = 0;
1505 hdr->attach_valid = 1;
1506 return hdr->attach_total;