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.
35 #include <lib-lib/mem.h>
36 #include <lib-lib/str.h>
37 #include <lib-lib/ascii.h>
38 #include <lib-lib/macros.h>
39 #include <lib-lib/buffer.h>
40 #include <lib-lib/date.h>
42 #include <lib-crypt/crypt.h>
44 #include "recvattach.h"
48 #include "lib/debug.h"
52 /* Reads an arbitrarily long header field, and looks ahead for continuation
53 * lines. ``line'' must point to a dynamically allocated string; it is
54 * increased if more space is required to fit the whole line.
56 char *mutt_read_rfc822_line (FILE * f, char *line, size_t * linelen)
63 if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */
64 (ISSPACE (*line) && !offset)) { /* end of headers */
69 buf += m_strlen(buf) - 1;
71 /* we did get a full line. remove trailing space */
72 while (ISSPACE (*buf))
73 *buf-- = 0; /* we cannot come beyond line's beginning because
74 * it begins with a non-space */
76 /* check to see if the next line is a continuation line */
77 if ((ch = fgetc (f)) != ' ' && ch != '\t') {
79 return (line); /* next line is a separate header field or EOH */
82 /* eat tabs and spaces from the beginning of the continuation line */
83 while ((ch = fgetc (f)) == ' ' || ch == '\t');
85 *++buf = ' '; /* string is still terminated because we removed
86 at least one whitespace char above */
91 if (*linelen < offset + STRING) {
94 p_realloc(&line, *linelen);
101 LIST *mutt_parse_references (char *s, int in_reply_to)
103 LIST *t, *lst = NULL;
105 char *o = NULL, *new, *at;
107 while ((s = strtok (s, " \t;")) != NULL) {
109 * some mail clients add other garbage besides message-ids, so do a quick
110 * check to make sure this looks like a valid message-id
111 * some idiotic clients also break their message-ids between lines, deal
112 * with that too (give up if it's more than two lines, though)
119 if (s[n - 1] != '>') {
129 if (s[m - 1] == '>') {
130 new = p_new(char, n + m + 1);
131 strcpy (new, o); /* __STRCPY_CHECKED__ */
132 strcpy (new + n, s); /* __STRCPY_CHECKED__ */
136 /* make sure that this really does look like a message-id.
137 * it should have exactly one @, and if we're looking at
138 * an in-reply-to header, make sure that the part before
139 * the @ has more than eight characters or it's probably
142 if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
143 || (in_reply_to && at - new <= 8))
159 int mutt_check_encoding (const char *c)
161 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit") - 1) == 0)
163 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit") - 1) == 0)
165 else if (ascii_strncasecmp ("binary", c, sizeof ("binary") - 1) == 0)
168 if (ascii_strncasecmp
169 ("quoted-printable", c, sizeof ("quoted-printable") - 1) == 0)
170 return (ENCQUOTEDPRINTABLE);
171 else if (ascii_strncasecmp ("base64", c, sizeof ("base64") - 1) == 0)
173 else if (ascii_strncasecmp ("x-uuencode", c, sizeof ("x-uuencode") - 1) == 0)
174 return (ENCUUENCODED);
179 static PARAMETER *parse_parameters (const char *s)
181 PARAMETER *head = 0, *cur = 0, *new;
182 char buffer[LONG_STRING];
186 debug_print (2, ("`%s'\n", s));
189 if ((p = strpbrk (s, "=;")) == NULL) {
190 debug_print (1, ("malformed parameter: %s\n", s));
194 /* if we hit a ; now the parameter has no value, just skip it */
198 new = mutt_new_parameter ();
200 new->attribute = p_dupstr(s, i);
202 /* remove whitespace from the end of the attribute name */
203 while (ISSPACE (new->attribute[--i]))
204 new->attribute[i] = 0;
206 s = vskipspaces(p + 1); /* skip over the = */
212 for (i = 0; *s && i < sizeof (buffer) - 1; i++, s++) {
213 if (!option (OPTSTRICTMIME)) {
214 /* As iso-2022-* has a characer of '"' with non-ascii state,
216 if (*s == 0x1b && i < sizeof (buffer) - 2) {
217 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
223 if (state_ascii && *s == '"')
226 /* Quote the next character */
236 s++; /* skip over the " */
239 for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1;
245 new->value = m_strdup(buffer);
247 debug_print (2, ("`%s' = `%s'\n", new->attribute ? new->attribute : "",
248 new->value ? new->value : ""));
250 /* Add this parameter to the list */
259 debug_print (1, ("parameter with no value: %s\n", s));
263 /* Find the next parameter */
264 if (*s != ';' && (s = strchr (s, ';')) == NULL)
265 break; /* no more parameters */
268 /* Move past any leading whitespace */
269 s = vskipspaces(s + 1);
271 while (*s == ';'); /* skip empty parameters */
276 rfc2231_decode_parameters (&head);
280 int mutt_check_mime_type (const char *s)
282 if (ascii_strcasecmp ("text", s) == 0)
284 else if (ascii_strcasecmp ("multipart", s) == 0)
285 return TYPEMULTIPART;
286 else if (ascii_strcasecmp ("application", s) == 0)
287 return TYPEAPPLICATION;
288 else if (ascii_strcasecmp ("message", s) == 0)
290 else if (ascii_strcasecmp ("image", s) == 0)
292 else if (ascii_strcasecmp ("audio", s) == 0)
294 else if (ascii_strcasecmp ("video", s) == 0)
296 else if (ascii_strcasecmp ("model", s) == 0)
298 else if (ascii_strcasecmp ("*", s) == 0)
300 else if (ascii_strcasecmp (".*", s) == 0)
306 void mutt_parse_content_type (char *s, BODY * ct)
311 p_delete(&ct->subtype);
312 mutt_free_parameter (&ct->parameter);
314 /* First extract any existing parameters */
315 if ((pc = strchr (s, ';')) != NULL) {
317 while (*pc && ISSPACE (*pc))
319 ct->parameter = parse_parameters (pc);
321 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
322 * but if a filename has already been set in the content-disposition,
323 * let that take precedence, and don't set it here */
324 if ((pc = mutt_get_parameter ("name", ct->parameter)) != 0
326 ct->filename = m_strdup(pc);
329 /* Now get the subtype */
330 if ((subtype = strchr (s, '/'))) {
332 for (pc = subtype; *pc && !ISSPACE (*pc) && *pc != ';'; pc++);
334 ct->subtype = m_strdup(subtype);
337 /* Finally, get the major type */
338 ct->type = mutt_check_mime_type (s);
340 if (ct->type == TYPEOTHER) {
341 ct->xtype = m_strdup(s);
344 if (ct->subtype == NULL) {
345 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
346 * field, so we can attempt to convert the type to BODY here.
348 if (ct->type == TYPETEXT)
349 ct->subtype = m_strdup("plain");
350 else if (ct->type == TYPEAUDIO)
351 ct->subtype = m_strdup("basic");
352 else if (ct->type == TYPEMESSAGE)
353 ct->subtype = m_strdup("rfc822");
354 else if (ct->type == TYPEOTHER) {
355 char buffer[SHORT_STRING];
357 ct->type = TYPEAPPLICATION;
358 snprintf (buffer, sizeof (buffer), "x-%s", s);
359 ct->subtype = m_strdup(buffer);
362 ct->subtype = m_strdup("x-unknown");
365 /* Default character set for text types. */
366 if (ct->type == TYPETEXT) {
367 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
368 mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
370 mutt_get_first_charset (AssumedCharset),
376 static void parse_content_disposition (char *s, BODY * ct)
380 if (!ascii_strncasecmp ("inline", s, 6))
381 ct->disposition = DISPINLINE;
382 else if (!ascii_strncasecmp ("form-data", s, 9))
383 ct->disposition = DISPFORMDATA;
385 ct->disposition = DISPATTACH;
387 /* Check to see if a default filename was given */
388 if ((s = strchr (s, ';')) != NULL) {
389 s = vskipspaces(s + 1);
390 if ((s = mutt_get_parameter("filename",
391 (parms = parse_parameters (s)))) != 0)
392 m_strreplace(&ct->filename, s);
393 if ((s = mutt_get_parameter ("name", parms)) != 0)
394 ct->form_name = m_strdup(s);
395 mutt_free_parameter (&parms);
400 * fp stream to read from
402 * digest 1 if reading subparts of a multipart/digest, 0
406 BODY *mutt_read_mime_header (FILE * fp, int digest)
408 BODY *p = mutt_new_body ();
410 char *line = p_new(char, LONG_STRING);
411 size_t linelen = LONG_STRING;
413 p->hdr_offset = ftello (fp);
415 p->encoding = ENC7BIT; /* default from RFC1521 */
416 p->type = digest ? TYPEMESSAGE : TYPETEXT;
417 p->disposition = DISPINLINE;
419 while (*(line = mutt_read_rfc822_line (fp, line, &linelen)) != 0) {
420 /* Find the value of the current header */
421 if ((c = strchr (line, ':'))) {
425 debug_print (1, ("skipping empty header field: %s\n", line));
430 debug_print (1, ("bogus MIME header: %s\n", line));
434 if (!ascii_strncasecmp ("content-", line, 8)) {
435 if (!ascii_strcasecmp ("type", line + 8))
436 mutt_parse_content_type (c, p);
437 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
438 p->encoding = mutt_check_encoding (c);
439 else if (!ascii_strcasecmp ("disposition", line + 8))
440 parse_content_disposition (c, p);
441 else if (!ascii_strcasecmp ("description", line + 8)) {
442 m_strreplace(&p->description, c);
443 rfc2047_decode (&p->description);
447 p->offset = ftello (fp); /* Mark the start of the real data */
448 if (p->type == TYPETEXT && !p->subtype)
449 p->subtype = m_strdup("plain");
450 else if (p->type == TYPEMESSAGE && !p->subtype)
451 p->subtype = m_strdup("rfc822");
458 void mutt_parse_part (FILE * fp, BODY * b)
464 bound = mutt_get_parameter ("boundary", b->parameter);
465 fseeko (fp, b->offset, SEEK_SET);
466 b->parts = mutt_parse_multipart (fp, bound,
467 b->offset + b->length,
468 ascii_strcasecmp ("digest",
474 fseeko (fp, b->offset, SEEK_SET);
475 if (mutt_is_message_type (b->type, b->subtype))
476 b->parts = mutt_parse_messageRFC822 (fp, b);
477 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
478 b->parts = mutt_read_mime_header (fp, 0);
488 /* try to recover from parsing error */
491 m_strreplace(&b->subtype, "plain");
495 /* parse a MESSAGE/RFC822 body
498 * fp stream to read from
500 * parent structure which contains info about the message/rfc822
503 * NOTE: this assumes that `parent->length' has been set!
506 BODY *mutt_parse_messageRFC822 (FILE * fp, BODY * parent)
510 parent->hdr = mutt_new_header ();
511 parent->hdr->offset = ftello (fp);
512 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
513 msg = parent->hdr->content;
515 /* ignore the length given in the content-length since it could be wrong
516 and we already have the info to calculate the correct length */
517 /* if (msg->length == -1) */
518 msg->length = parent->length - (msg->offset - parent->offset);
520 /* if body of this message is empty, we can end up with a negative length */
524 mutt_parse_part (fp, msg);
528 /* parse a multipart structure
531 * fp stream to read from
533 * boundary body separator
535 * end_off length of the multipart body (used when the final
536 * boundary is missing to avoid reading too far)
538 * digest 1 if reading a multipart/digest, 0 otherwise
541 BODY *mutt_parse_multipart (FILE * fp, const char *boundary, off_t end_off,
544 int blen, len, crlf = 0;
545 char buffer[LONG_STRING];
546 BODY *head = 0, *last = 0, *new = 0;
548 int final = 0; /* did we see the ending boundary? */
551 mutt_error _("multipart message has no boundary parameter!");
556 blen = m_strlen(boundary);
557 while (ftello (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL) {
558 len = m_strlen(buffer);
560 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
562 if (buffer[0] == '-' && buffer[1] == '-' &&
563 m_strncmp(buffer + 2, boundary, blen) == 0) {
565 last->length = ftello (fp) - last->offset - len - 1 - crlf;
566 if (last->parts && last->parts->length == 0)
567 last->parts->length =
568 ftello (fp) - last->parts->offset - len - 1 - crlf;
569 /* if the body is empty, we can end up with a -1 length */
570 if (last->length < 0)
574 /* Remove any trailing whitespace, up to the length of the boundary */
575 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
578 /* Check for the end boundary */
579 if (m_strcmp(buffer + blen + 2, "--") == 0) {
581 break; /* done parsing */
583 else if (buffer[2 + blen] == 0) {
584 new = mutt_read_mime_header (fp, digest);
587 * Consistency checking - catch
588 * bad attachment end boundaries
591 if (new->offset > end_off) {
592 mutt_free_body (&new);
605 /* in case of missing end boundary, set the length to something reasonable */
606 if (last && last->length == 0 && !final)
607 last->length = end_off - last->offset;
609 /* parse recursive MIME parts */
610 for (last = head; last; last = last->next)
611 mutt_parse_part (fp, last);
616 static const char *uncomment_timezone (char *buf, size_t buflen,
623 return tz; /* no need to do anything */
624 tz = vskipspaces(tz + 1);
625 if ((p = strpbrk (tz, " )")) == NULL)
628 if (len > buflen - 1)
630 memcpy (buf, tz, len);
637 unsigned char zhours;
638 unsigned char zminutes;
639 unsigned char zoccident; /* west of UTC? */
642 "aat", 1, 0, 1}, /* Atlantic Africa Time */
644 "adt", 4, 0, 0}, /* Arabia DST */
646 "ast", 3, 0, 0}, /* Arabia */
647 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
649 "bst", 1, 0, 0}, /* British DST */
651 "cat", 1, 0, 0}, /* Central Africa */
654 "cest", 2, 0, 0}, /* Central Europe DST */
656 "cet", 1, 0, 0}, /* Central Europe */
659 /*{ "cst", 8, 0, 0 }, *//* China */
660 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
662 "eat", 3, 0, 0}, /* East Africa */
665 "eest", 3, 0, 0}, /* Eastern Europe DST */
667 "eet", 2, 0, 0}, /* Eastern Europe */
669 "egst", 0, 0, 0}, /* Eastern Greenland DST */
671 "egt", 1, 0, 1}, /* Eastern Greenland */
675 "gst", 4, 0, 0}, /* Presian Gulf */
677 "hkt", 8, 0, 0}, /* Hong Kong */
679 "ict", 7, 0, 0}, /* Indochina */
681 "idt", 3, 0, 0}, /* Israel DST */
683 "ist", 2, 0, 0}, /* Israel */
684 /*{ "ist", 5, 30, 0 }, *//* India */
686 "jst", 9, 0, 0}, /* Japan */
688 "kst", 9, 0, 0}, /* Korea */
691 "met", 1, 0, 0}, /* this is now officially CET */
693 "msd", 4, 0, 0}, /* Moscow DST */
695 "msk", 3, 0, 0}, /* Moscow */
698 "nzdt", 13, 0, 0}, /* New Zealand DST */
700 "nzst", 12, 0, 0}, /* New Zealand */
704 "sat", 2, 0, 0}, /* South Africa */
706 "smt", 4, 0, 0}, /* Seychelles */
708 "sst", 11, 0, 1}, /* Samoa */
709 /*{ "sst", 8, 0, 0 }, *//* Singapore */
712 "wat", 0, 0, 0}, /* West Africa */
714 "west", 1, 0, 0}, /* Western Europe DST */
716 "wet", 0, 0, 0}, /* Western Europe */
718 "wgst", 2, 0, 1}, /* Western Greenland DST */
720 "wgt", 3, 0, 1}, /* Western Greenland */
722 "wst", 8, 0, 0}, /* Western Australia */
725 /* parses a date string in RFC822 format:
727 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
729 * This routine assumes that `h' has been initialized to 0. the `timezone'
730 * field is optional, defaulting to +0000 if missing.
732 time_t mutt_parse_date (const char *s, HEADER * h)
744 char tzstr[SHORT_STRING];
745 char scratch[SHORT_STRING];
747 /* Don't modify our argument. Fixed-size buffer is ok here since
748 * the date format imposes a natural limit.
751 m_strcpy(scratch, sizeof(scratch), s);
753 /* kill the day of the week, if it exists. */
754 if ((t = strchr (scratch, ',')))
762 while ((t = strtok (t, " \t")) != NULL) {
764 case 0: /* day of the month */
765 if (!isdigit ((unsigned char) *t))
767 tm.tm_mday = atoi (t);
772 case 1: /* month of the year */
773 if ((i = mutt_check_month (t)) < 0)
779 tm.tm_year = atoi (t);
782 else if (tm.tm_year >= 1900)
786 case 3: /* time of day */
787 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3);
788 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
791 debug_print (1, ("could not process time format: %s\n", t));
799 case 4: /* timezone */
800 /* sometimes we see things like (MST) or (-0700) so attempt to
801 * compensate by uncommenting the string if non-RFC822 compliant
803 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
805 if (*ptz == '+' || *ptz == '-') {
806 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
807 && isdigit ((unsigned char) ptz[1])
808 && isdigit ((unsigned char) ptz[2])
809 && isdigit ((unsigned char) ptz[3])
810 && isdigit ((unsigned char) ptz[4])) {
811 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
812 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
821 tz = bsearch (ptz, TimeZones, sizeof TimeZones / sizeof (struct tz_t),
822 sizeof (struct tz_t),
823 (int (*)(const void *, const void *)) ascii_strcasecmp
824 /* This is safe to do: A pointer to a struct equals
825 * a pointer to its first element*/ );
829 zminutes = tz->zminutes;
830 zoccident = tz->zoccident;
833 /* ad hoc support for the European MET (now officially CET) TZ */
834 if (ascii_strcasecmp (t, "MET") == 0) {
835 if ((t = strtok (NULL, " \t")) != NULL) {
836 if (!ascii_strcasecmp (t, "DST"))
841 tz_offset = zhours * 3600 + zminutes * 60;
843 tz_offset = -tz_offset;
850 if (count < 4) { /* don't check for missing timezone */
851 debug_print (1, ("error parsing date format, using received time\n"));
857 h->zminutes = zminutes;
858 h->zoccident = zoccident;
861 return (mutt_mktime (&tm, 0) + tz_offset);
864 /* extract the first substring that looks like a message-id */
865 static char *extract_message_id(const char *s)
869 if ((s = strchr(s, '<')) == NULL || (p = strchr(s, '>')) == NULL)
871 return p_dupstr(s, (p - s) + 1);
874 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
880 if (cur->content->type != TYPEMESSAGE
881 && cur->content->type != TYPEMULTIPART)
882 break; /* nothing to do */
884 if (cur->content->parts)
885 break; /* The message was parsed earlier. */
887 if ((msg = mx_open_message (ctx, cur->msgno))) {
888 mutt_parse_part (msg->fp, cur->content);
890 cur->security = crypt_query (cur->content);
892 mx_close_message (&msg);
895 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);
898 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
899 short user_hdrs, short weed, short do_2047,
908 switch (ascii_tolower (line[0])) {
910 if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
911 e->to = rfc822_parse_adrlist (e->to, p);
914 else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
915 e->from = rfc822_parse_adrlist (e->from, p);
921 if (ascii_strcasecmp (line + 1, "cc") == 0) {
922 e->bcc = rfc822_parse_adrlist (e->bcc, p);
928 if (ascii_strcasecmp (line + 1, "c") == 0) {
929 e->cc = rfc822_parse_adrlist (e->cc, p);
932 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
933 if (ascii_strcasecmp (line + 8, "type") == 0) {
935 mutt_parse_content_type (p, hdr->content);
938 else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
940 hdr->content->encoding = mutt_check_encoding (p);
943 else if (ascii_strcasecmp (line + 8, "length") == 0) {
945 if ((hdr->content->length = atoi (p)) < 0)
946 hdr->content->length = -1;
950 else if (ascii_strcasecmp (line + 8, "description") == 0) {
952 m_strreplace(&hdr->content->description, p);
953 rfc2047_decode (&hdr->content->description);
957 else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
959 parse_content_disposition (p, hdr->content);
966 if (!ascii_strcasecmp ("ate", line + 1)) {
967 m_strreplace(&e->date, p);
969 hdr->date_sent = mutt_parse_date (p, hdr);
975 if (!ascii_strcasecmp ("xpires", line + 1) &&
976 hdr && mutt_parse_date (p, NULL) < time (NULL))
981 if (!ascii_strcasecmp ("rom", line + 1)) {
982 e->from = rfc822_parse_adrlist (e->from, p);
983 /* don't leave from info NULL if there's an invalid address (or
984 * whatever) in From: field; mutt would just display it as empty
985 * and mark mail/(esp.) news article as your own. aaargh! this
986 * bothered me for _years_ */
988 e->from = address_new ();
989 e->from->personal = m_strdup(p);
994 else if (!m_strcasecmp(line + 1, "ollowup-to")) {
995 if (!e->followup_to) {
997 e->followup_to = m_strdup(skipspaces(p));
1005 if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
1006 mutt_free_list (&e->in_reply_to);
1007 e->in_reply_to = mutt_parse_references (p, 1);
1013 if (!ascii_strcasecmp (line + 1, "ines")) {
1015 hdr->lines = atoi (p);
1018 * HACK - mutt has, for a very short time, produced negative
1019 * Lines header values. Ignore them.
1027 else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
1028 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1029 if (strncmp (p, "NO", 2)) {
1032 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
1034 if (!(end = strchr (beg, '>')))
1037 /* Take the first mailto URL */
1038 if (url_check_scheme (beg) == U_MAILTO) {
1039 p_delete(&e->list_post);
1040 e->list_post = p_dupstr(beg, end - beg);
1050 if (!ascii_strcasecmp (line + 1, "ime-version")) {
1055 else if (!ascii_strcasecmp (line + 1, "essage-id")) {
1056 /* We add a new "Message-ID:" when building a message */
1057 p_delete(&e->message_id);
1058 e->message_id = extract_message_id (p);
1061 else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
1062 if (!ascii_strcasecmp (line + 5, "reply-to")) {
1063 /* override the Reply-To: field */
1064 address_delete (&e->reply_to);
1065 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1068 else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1069 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1077 if (!m_strcasecmp(line + 1, "ewsgroups")) {
1078 p_delete(&e->newsgroups);
1080 e->newsgroups = m_strdup(skipspaces(p));
1087 /* field `Organization:' saves only for pager! */
1088 if (!m_strcasecmp(line + 1, "rganization")) {
1089 if (!e->organization && m_strcasecmp(p, "unknown"))
1090 e->organization = m_strdup(p);
1095 if (!ascii_strcasecmp (line + 1, "eferences")) {
1096 mutt_free_list (&e->references);
1097 e->references = mutt_parse_references (p, 0);
1100 else if (!ascii_strcasecmp (line + 1, "eply-to")) {
1101 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1104 else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
1105 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1108 else if (!ascii_strcasecmp (line + 1, "eceived")) {
1109 if (hdr && !hdr->received) {
1110 char *d = strchr (p, ';');
1113 hdr->received = mutt_parse_date (d + 1, NULL);
1119 if (!ascii_strcasecmp (line + 1, "ubject")) {
1121 e->subject = m_strdup(p);
1124 else if (!ascii_strcasecmp (line + 1, "ender")) {
1125 e->sender = rfc822_parse_adrlist (e->sender, p);
1128 else if (!ascii_strcasecmp (line + 1, "tatus")) {
1147 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1148 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1149 e->supersedes = m_strdup(p);
1153 if (ascii_strcasecmp (line + 1, "o") == 0) {
1154 e->to = rfc822_parse_adrlist (e->to, p);
1160 if (ascii_strcasecmp (line + 1, "-status") == 0) {
1181 else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1182 e->x_label = m_strdup(p);
1186 else if (!m_strcasecmp(line + 1, "-comment-to")) {
1187 if (!e->x_comment_to)
1188 e->x_comment_to = m_strdup(p);
1191 else if (!m_strcasecmp(line + 1, "ref")) {
1193 e->xref = m_strdup(p);
1202 /* Keep track of the user-defined headers */
1203 if (!matched && user_hdrs) {
1204 /* restore the original line */
1205 line[m_strlen(line)] = ':';
1207 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1208 && !mutt_matches_ignore (line, UnIgnore))
1212 last->next = mutt_new_list ();
1216 last = e->userhdrs = mutt_new_list ();
1217 last->data = m_strdup(line);
1219 rfc2047_decode (&last->data);
1229 /* mutt_read_rfc822_header() -- parses a RFC822 header
1233 * f stream to read from
1235 * hdr header structure of current message (optional).
1237 * user_hdrs If set, store user headers. Used for recall-message and
1240 * weed If this parameter is set and the user has activated the
1241 * $weed option, honor the header weed list for user headers.
1242 * Used for recall-message.
1244 * Returns: newly allocated envelope structure. You should free it by
1245 * mutt_free_envelope() when envelope stay unneeded.
1247 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1250 ENVELOPE *e = mutt_new_envelope ();
1252 char *line = p_new(char, LONG_STRING);
1256 size_t linelen = LONG_STRING;
1257 char buf[LONG_STRING + 1];
1260 if (hdr->content == NULL) {
1261 hdr->content = mutt_new_body ();
1263 /* set the defaults from RFC1521 */
1264 hdr->content->type = TYPETEXT;
1265 hdr->content->subtype = m_strdup("plain");
1266 hdr->content->encoding = ENC7BIT;
1267 hdr->content->length = -1;
1269 /* RFC 2183 says this is arbitrary */
1270 hdr->content->disposition = DISPINLINE;
1274 while ((loc = ftello (f)),
1275 *(line = mutt_read_rfc822_line (f, line, &linelen)) != 0) {
1278 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1279 char return_path[LONG_STRING];
1282 /* some bogus MTAs will quote the original "From " line */
1283 if (m_strncmp(">From ", line, 6) == 0)
1284 continue; /* just ignore */
1285 else if (is_from (line, return_path, sizeof (return_path), &t)) {
1286 /* MH somtimes has the From_ line in the middle of the header! */
1287 if (hdr && !hdr->received)
1288 hdr->received = t - mutt_local_tz (t);
1293 break; /* end of header */
1298 if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1299 if (!rx_list_match (NoSpamList, line)) {
1301 /* if spam tag already exists, figure out how to amend it */
1302 if (e->spam && *buf) {
1303 /* If SpamSep defined, append with separator */
1305 mutt_buffer_addstr (e->spam, SpamSep);
1306 mutt_buffer_addstr (e->spam, buf);
1309 /* else overwrite */
1311 e->spam->dptr = e->spam->data;
1312 *e->spam->dptr = '\0';
1313 mutt_buffer_addstr (e->spam, buf);
1317 /* spam tag is new, and match expr is non-empty; copy */
1318 else if (!e->spam && *buf) {
1319 e->spam = mutt_buffer_from (NULL, buf);
1322 /* match expr is empty; plug in null string if no existing tag */
1323 else if (!e->spam) {
1324 e->spam = mutt_buffer_from (NULL, "");
1327 if (e->spam && e->spam->data)
1328 debug_print (5, ("spam = %s\n", e->spam->data));
1335 continue; /* skip empty header fields */
1338 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1345 hdr->content->hdr_offset = hdr->offset;
1346 hdr->content->offset = ftello (f);
1347 rfc2047_decode_envelope(e);
1348 /* check for missing or invalid date */
1349 if (hdr->date_sent <= 0) {
1350 debug_print (1, ("no date found, using received "
1351 "time from msg separator\n"));
1352 hdr->date_sent = hdr->received;
1359 address_t *mutt_parse_adrlist (address_t * p, const char *s)
1363 /* check for a simple whitespace separated list of addresses */
1364 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1365 char tmp[HUGE_STRING];
1368 m_strcpy(tmp, sizeof(tmp), s);
1370 while ((r = strtok (r, " \t")) != NULL) {
1371 p = rfc822_parse_adrlist (p, r);
1376 p = rfc822_parse_adrlist (p, s);
1382 /* Compares mime types to the ok and except lists */
1383 int count_body_parts_check(LIST **checklist, BODY *b, int dflt) {
1387 /* If list is null, use default behavior. */
1393 for (type = *checklist; type; type = type->next) {
1394 a = (ATTACH_MATCH *)type->data;
1395 debug_print(5, ("cbpc: %s %d/%s ?? %s/%s [%d]... ",
1396 dflt ? "[OK] " : "[EXCL] ",
1397 b->type, b->subtype, a->major, a->minor, a->major_int));
1398 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1399 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1400 debug_print(5, ("yes\n"));
1403 debug_print(5, ("no\n"));
1409 #define AT_COUNT(why) { shallcount = 1; }
1410 #define AT_NOCOUNT(why) { shallcount = 0; }
1412 int count_body_parts (BODY *body, int flags) {
1414 int shallcount, shallrecurse;
1420 for (bp = body; bp != NULL; bp = bp->next) {
1421 /* Initial disposition is to count and not to recurse this part. */
1422 AT_COUNT("default");
1425 debug_print(5, ("bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1426 bp->description ? bp->description : ("none"),
1427 bp->filename ? bp->filename :
1428 bp->d_filename ? bp->d_filename : "(none)",
1429 bp->type, bp->subtype ? bp->subtype : "*"));
1431 if (bp->type == TYPEMESSAGE) {
1434 /* If it's an external body pointer, don't recurse it. */
1435 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1438 /* Don't count containers if they're top-level. */
1439 if (flags & M_PARTS_TOPLEVEL)
1440 AT_NOCOUNT("top-level message/*");
1441 } else if (bp->type == TYPEMULTIPART) {
1442 /* Always recurse multiparts, except multipart/alternative. */
1444 if (!m_strcasecmp(bp->subtype, "alternative"))
1447 /* Don't count containers if they're top-level. */
1448 if (flags & M_PARTS_TOPLEVEL)
1449 AT_NOCOUNT("top-level multipart");
1452 if (bp->disposition == DISPINLINE &&
1453 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1454 AT_NOCOUNT("ignore fundamental inlines");
1456 /* If this body isn't scheduled for enumeration already, don't bother
1457 * profiling it further. */
1460 /* Turn off shallcount if message type is not in ok list,
1461 * or if it is in except list. Check is done separately for
1462 * inlines vs. attachments.
1465 if (bp->disposition == DISPATTACH) {
1466 if (!count_body_parts_check(&AttachAllow, bp, 1))
1467 AT_NOCOUNT("attach not allowed");
1468 if (count_body_parts_check(&AttachExclude, bp, 0))
1469 AT_NOCOUNT("attach excluded");
1471 if (!count_body_parts_check(&InlineAllow, bp, 1))
1472 AT_NOCOUNT("inline not allowed");
1473 if (count_body_parts_check(&InlineExclude, bp, 0))
1474 AT_NOCOUNT("excluded");
1480 bp->attach_qualifies = shallcount ? 1 : 0;
1482 debug_print(5, ("cbp: %p shallcount = %d\n", bp, shallcount));
1485 debug_print(5, ("cbp: %p pre count = %d\n", bp, count));
1486 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1487 count += bp->attach_count;
1488 debug_print(5, ("cbp: %p post count = %d\n", bp, count));
1492 debug_print(5, ("bp: return %d\n", count < 0 ? 0 : count));
1493 return count < 0 ? 0 : count;
1496 int mutt_count_body_parts (HEADER *hdr, int flags) {
1497 if (!option (OPTCOUNTATTACH))
1499 if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1500 return hdr->attach_total;
1502 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1503 hdr->attach_total = count_body_parts(hdr->content, flags | M_PARTS_TOPLEVEL);
1505 hdr->attach_total = 0;
1507 hdr->attach_valid = 1;
1508 return hdr->attach_total;