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 ssize_t mutt_read_rfc822_line(FILE *f, char **line, ssize_t *n)
63 /* end of file or end of headers */
64 if (!fgets(p + pos, *n - pos, f) || (ISSPACE(*p) && pos == 0)) {
69 pos += m_strlen(p + pos);
70 if (p[pos - 1] == '\n') {
73 /* remove trailing spaces. safe: p[0] is not a space */
76 } while (ISSPACE(p[pos]));
78 /* check to see if the next line is a continuation line */
80 if (c != ' ' && c != '\t') {
81 /* next line is a separate header field or EOH */
86 /* eat tabs and spaces from the beginning of the continuation line */
89 } while (c == ' ' || c == '\t');
92 /* string is still terminated because we removed at least one
93 whitespace char above */
97 if (*n < pos + STRING) {
105 /* TODO: Make that a string list somehow */
106 LIST *mutt_parse_references(char *s, int in_reply_to)
112 /* some mail clients add other garbage besides message-ids, so do a quick
113 * check to make sure this looks like a valid message-id
114 * some idiotic clients also break their message-ids between lines, deal
115 * with that too (give up if it's more than two lines, though)
118 for (s = strtok(s, " \t;"); s; s = strtok(NULL, " \t;")) {
123 if (s[n - 1] != '>') {
130 ssize_t m = m_strlen(s);
132 if (s[m - 1] != '>') {
135 new = p_new(char, n + m + 1);
141 /* make sure that this really does look like a message-id.
142 * it should have exactly one @, and if we're looking at
143 * an in-reply-to header, make sure that the part before
144 * the @ has more than eight characters or it's probably
148 char *at = strchr(new, '@');
151 if (!at || strchr(at + 1, '@') || (in_reply_to && at - new <= 8)) {
156 tmp = p_new(LIST, 1);
166 int mutt_check_encoding(const char *s)
168 #define COMPARE(tok, value) \
169 if (!ascii_strncasecmp(tok, s, sizeof(tok) - 1)) { \
172 COMPARE("7bit", ENC7BIT);
173 COMPARE("8bit", ENC8BIT);
174 COMPARE("binary", ENCBINARY);
175 COMPARE("quoted-printable", ENCQUOTEDPRINTABLE);
176 COMPARE("base64", ENCBASE64);
177 COMPARE("x-uuencode", ENCUUENCODED);
183 int mutt_check_mime_type(const char *s)
185 #define COMPARE(tok, value) \
186 if (!ascii_strncasecmp(tok, s, sizeof(tok) - 1)) { \
189 COMPARE("text", TYPETEXT);
190 COMPARE("multipart", TYPEMULTIPART);
191 COMPARE("application", TYPEAPPLICATION);
192 COMPARE("message", TYPEMESSAGE);
193 COMPARE("image", TYPEIMAGE);
194 COMPARE("audio", TYPEAUDIO);
195 COMPARE("video", TYPEVIDEO);
196 COMPARE("model", TYPEMODEL);
197 COMPARE("*", TYPEANY);
198 COMPARE(".*", TYPEANY);
204 static PARAMETER *parse_parameters(const char *s)
206 PARAMETER *res = NULL;
207 PARAMETER **list = &res;
215 if (*s == '=') /* parameters are fucked up, go away */
218 p = strpbrk(s, "=;");
223 /* if we hit a ; now the parameter has no value, just skip it */
229 new = parameter_new();
230 new->attribute = p_dupstr(s, i);
232 while (--i >= 0 && ISSPACE(new->attribute[i])) {
233 new->attribute[i] = '\0';
235 s = skipspaces(p + 1); /* skip over the = */
238 char buffer[LONG_STRING];
242 for (i = 0; *s && i < ssizeof(buffer) - 1; i++, s++) {
243 if (!option(OPTSTRICTMIME)) {
244 /* As iso-2022-* has a characer of '"' with non-ascii state,
246 if (*s == 0x1b && i < ssizeof(buffer) - 2) {
247 state_ascii = s[1] == '(' && (s[2] == 'B' || s[2] == 'J');
250 if (state_ascii && *s == '"')
260 new->value = p_dupstr(buffer, i);
262 for (p = s; *p && *p != ' ' && *p != ';'; p++);
263 new->value = p_dupstr(s, p - s);
269 s = strchr(s, ';'); /* Find the next parameter */
271 break; /* no more parameters */
274 rfc2231_decode_parameters(&res);
278 void mutt_parse_content_type(char *s, BODY *ct)
283 p_delete(&ct->subtype);
284 parameter_delete(&ct->parameter);
286 /* First extract any existing parameters */
287 if ((pc = strchr(s, ';')) != NULL) {
289 ct->parameter = parse_parameters(vskipspaces(pc));
291 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
292 * but if a filename has already been set in the content-disposition,
293 * let that take precedence, and don't set it here */
294 pc = mutt_get_parameter("name", ct->parameter);
295 if (pc && !ct->filename)
296 ct->filename = m_strdup(pc);
299 /* Now get the subtype */
300 if ((subtype = strchr (s, '/'))) {
302 for (pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++);
303 ct->subtype = p_dupstr(subtype, pc - subtype);
306 /* Finally, get the major type */
307 ct->type = mutt_check_mime_type(s);
309 if (ct->type == TYPEOTHER) {
310 ct->xtype = m_strdup(s);
314 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
315 * field, so we can attempt to convert the type to BODY here.
318 char buffer[SHORT_STRING];
321 ct->subtype = m_strdup("plain");
325 ct->subtype = m_strdup("basic");
329 ct->subtype = m_strdup("rfc822");
333 ct->type = TYPEAPPLICATION;
334 snprintf(buffer, sizeof(buffer), "x-%s", s);
335 ct->subtype = m_strdup(buffer);
339 ct->subtype = m_strdup("x-unknown");
344 /* Default character set for text types. */
345 if (ct->type == TYPETEXT) {
346 pc = mutt_get_parameter("charset", ct->parameter);
348 mutt_set_parameter("charset",
349 option(OPTSTRICTMIME) ? "us-ascii" :
350 mutt_get_first_charset(AssumedCharset),
356 static void parse_content_disposition(char *s, BODY *ct)
358 if (!ascii_strncasecmp(s, "inline", 6)) {
359 ct->disposition = DISPINLINE;
360 } else if (!ascii_strncasecmp(s, "form-data", 9)) {
361 ct->disposition = DISPFORMDATA;
363 ct->disposition = DISPATTACH;
366 /* Check to see if a default filename was given */
367 if ((s = strchr (s, ';'))) {
368 PARAMETER *parms = parse_parameters(vskipspaces(s));
370 if ((s = mutt_get_parameter("filename", parms)))
371 m_strreplace(&ct->filename, s);
372 if ((s = mutt_get_parameter ("name", parms)))
373 ct->form_name = m_strdup(s);
375 parameter_delete(&parms);
380 * fp stream to read from
382 * digest 1 if reading subparts of a multipart/digest, 0
385 BODY *mutt_read_mime_header(FILE *fp, int digest)
387 BODY *body = mutt_new_body ();
388 char *line = p_new(char, LONG_STRING);
389 ssize_t linelen = LONG_STRING;
392 body->hdr_offset = ftello(fp);
393 body->encoding = ENC7BIT; /* default from RFC1521 */
394 body->disposition = DISPINLINE;
395 body->type = digest ? TYPEMESSAGE : TYPETEXT;
397 while (mutt_read_rfc822_line(fp, &line, &linelen)) {
398 /* Find the value of the current header */
399 if ((p = strchr(line, ':'))) {
405 debug_print (1, ("bogus MIME header: %s\n", line));
409 if (!ascii_strncasecmp(line, "content-", 8)) {
410 if (!ascii_strcasecmp("type", line + 8))
411 mutt_parse_content_type (p, body);
412 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
413 body->encoding = mutt_check_encoding (p);
414 else if (!ascii_strcasecmp ("disposition", line + 8))
415 parse_content_disposition(p, body);
416 else if (!ascii_strcasecmp ("description", line + 8)) {
417 m_strreplace(&body->description, p);
418 rfc2047_decode(&body->description);
423 body->offset = ftello(fp); /* Mark the start of the real data */
424 if (!body->subtype) {
425 if (body->type == TYPETEXT)
426 body->subtype = m_strdup("plain");
427 if (body->type == TYPEMESSAGE)
428 body->subtype = m_strdup("rfc822");
435 void mutt_parse_part(FILE *fp, BODY *b)
441 bound = mutt_get_parameter("boundary", b->parameter);
442 fseeko(fp, b->offset, SEEK_SET);
443 b->parts = mutt_parse_multipart(fp, bound, b->offset + b->length,
444 !ascii_strcasecmp("digest", b->subtype));
449 fseeko(fp, b->offset, SEEK_SET);
451 if (mutt_is_message_type(b->type, b->subtype)) {
452 b->parts = mutt_parse_messageRFC822(fp, b);
454 if (!ascii_strcasecmp(b->subtype, "external-body") == 0) {
455 b->parts = mutt_read_mime_header(fp, 0);
466 /* try to recover from parsing error */
469 m_strreplace(&b->subtype, "plain");
473 /* parse a MESSAGE/RFC822 body
476 * fp stream to read from
478 * parent structure which contains info about the message/rfc822
481 * NOTE: this assumes that `parent->length' has been set!
483 BODY *mutt_parse_messageRFC822(FILE * fp, BODY * parent)
487 parent->hdr = header_new();
488 parent->hdr->offset = ftello(fp);
489 parent->hdr->env = mutt_read_rfc822_header(fp, parent->hdr, 0, 0);
491 msg = parent->hdr->content;
493 /* ignore the length given in the content-length since it could be wrong
494 and we already have the info to calculate the correct length */
495 /* if (msg->length == -1) */
496 /* if body of this message is empty, we can end up with a negative length */
497 msg->length = MAX(0, parent->length - (msg->offset - parent->offset));
499 mutt_parse_part(fp, msg);
504 /* parse a multipart structure
507 * fp stream to read from
509 * bound body separator
511 * end_off length of the multipart body (used when the final
512 * boundary is missing to avoid reading too far)
514 * digest 1 if reading a multipart/digest, 0 otherwise
518 mutt_parse_multipart(FILE *fp, const char *bound, off_t end_off, int digest)
520 char buffer[LONG_STRING];
523 int blen = m_strlen(bound);
524 int final = 0; /* did we see the ending boundary? */
527 mutt_error _("multipart message has no boundary parameter!");
531 while (ftello(fp) < end_off && fgets(buffer, sizeof(buffer), fp)) {
534 len = m_strlen(buffer);
535 crlf = len > 1 && buffer[len - 2] == '\r';
537 if (buffer[0] == '-' && buffer[1] == '-'
538 && !m_strncmp(buffer + 2, bound, blen))
543 /* if the body is empty, we can end up with a -1 length */
544 b->length = MAX(0, ftello(fp) - b->offset - len - 1 - crlf);
545 if (b->parts && b->parts->length == 0) {
546 b->parts->length = ftello(fp) - b->parts->offset
551 /* Remove any trailing whitespace, up to the length of the boundary */
552 for (i = len - 1; ISSPACE(buffer[i]) && i >= blen + 2; i--)
555 /* Check for the end boundary */
556 final = buffer[blen + 3] == '-' && buffer[blen + 4] == '-';
560 if (buffer[2 + blen] == '\0') {
561 BODY *new = mutt_read_mime_header(fp, digest);
564 * Consistency checking - catch
565 * bad attachment end boundaries
568 if (new->offset > end_off) {
569 mutt_free_body(&new);
574 last = &(*last)->next;
580 /* in case of missing end boundary, set the length to something reasonable */
581 if (*last && (*last)->length == 0 && !final)
582 (*last)->length = end_off - (*last)->offset;
584 /* parse recursive MIME parts */
587 for (b = head; b; b = b->next)
588 mutt_parse_part(fp, b);
595 uncomment_timezone(char *buf, size_t buflen, const char *tz)
600 return tz; /* no need to do anything */
602 tz = vskipspaces(tz + 1);
603 p = strpbrk(tz, " )");
607 m_strncpy(buf, buflen, tz, p - tz);
613 unsigned char zhours;
614 unsigned char zminutes;
615 unsigned char zoccident; /* west of UTC? */
617 {"aat", 1, 0, 1}, /* Atlantic Africa Time */
618 {"adt", 4, 0, 0}, /* Arabia DST */
619 {"ast", 3, 0, 0}, /* Arabia */
620 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
621 {"bst", 1, 0, 0}, /* British DST */
622 {"cat", 1, 0, 0}, /* Central Africa */
624 {"cest", 2, 0, 0}, /* Central Europe DST */
625 {"cet", 1, 0, 0}, /* Central Europe */
627 /*{ "cst", 8, 0, 0 }, *//* China */
628 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
629 {"eat", 3, 0, 0}, /* East Africa */
631 {"eest", 3, 0, 0}, /* Eastern Europe DST */
632 {"eet", 2, 0, 0}, /* Eastern Europe */
633 {"egst", 0, 0, 0}, /* Eastern Greenland DST */
634 {"egt", 1, 0, 1}, /* Eastern Greenland */
637 {"gst", 4, 0, 0}, /* Presian Gulf */
638 {"hkt", 8, 0, 0}, /* Hong Kong */
639 {"ict", 7, 0, 0}, /* Indochina */
640 {"idt", 3, 0, 0}, /* Israel DST */
641 {"ist", 2, 0, 0}, /* Israel */
642 /*{ "ist", 5, 30, 0 }, *//* India */
643 {"jst", 9, 0, 0}, /* Japan */
644 {"kst", 9, 0, 0}, /* Korea */
646 {"met", 1, 0, 0}, /* this is now officially CET */
647 {"msd", 4, 0, 0}, /* Moscow DST */
648 {"msk", 3, 0, 0}, /* Moscow */
650 {"nzdt", 13, 0, 0}, /* New Zealand DST */
651 {"nzst", 12, 0, 0}, /* New Zealand */
654 {"sat", 2, 0, 0}, /* South Africa */
655 {"smt", 4, 0, 0}, /* Seychelles */
656 {"sst", 11, 0, 1}, /* Samoa */
657 /*{ "sst", 8, 0, 0 }, *//* Singapore */
659 {"wat", 0, 0, 0}, /* West Africa */
660 {"west", 1, 0, 0}, /* Western Europe DST */
661 {"wet", 0, 0, 0}, /* Western Europe */
662 {"wgst", 2, 0, 1}, /* Western Greenland DST */
663 {"wgt", 3, 0, 1}, /* Western Greenland */
664 {"wst", 8, 0, 0}, /* Western Australia */
667 /* parses a date string in RFC822 format:
669 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
671 * This routine assumes that `h' has been initialized to 0. the `timezone'
672 * field is optional, defaulting to +0000 if missing.
674 time_t mutt_parse_date(const char *s, HEADER *h)
680 int zhours = 0, zminutes = 0, zoccident = 0;
682 char tzstr[SHORT_STRING];
683 char scratch[SHORT_STRING];
685 /* Don't modify our argument. Fixed-size buffer is ok here since
686 the date format imposes a natural limit. */
688 m_strcpy(scratch, sizeof(scratch), s);
690 /* kill the day of the week, if it exists. */
691 p = strchr(scratch, ',');
692 p = vskipspaces(p ? p + 1 : scratch);
696 while ((p = strtok (p, " \t")) != NULL) {
698 case 0: /* day of the month */
699 if (!isdigit((unsigned char)*p))
701 tm.tm_mday = atoi(p);
706 case 1: /* month of the year */
707 tm.tm_mon = mutt_check_month(p);
713 tm.tm_year = atoi(p);
716 else if (tm.tm_year >= 1900)
720 case 3: /* time of day */
721 tm.tm_hour = strtol(p, &p, 10);
724 tm.tm_min = strtol(p, &p, 10);
726 tm.tm_sec = strtol(p, &p, 10);
732 case 4: /* timezone */
733 /* sometimes we see things like (MST) or (-0700) so attempt to
734 * compensate by uncommenting the string if non-RFC822 compliant
736 ptz = uncomment_timezone(tzstr, sizeof(tzstr), p);
738 if (*ptz == '+' || *ptz == '-') {
739 if (isdigit((unsigned char)ptz[1])
740 && isdigit((unsigned char)ptz[2])
741 && isdigit((unsigned char)ptz[3])
742 && isdigit((unsigned char)ptz[4]))
744 zoccident = ptz[0] == '-';
745 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
746 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
751 /* This is safe to do: A pointer to a struct equals a pointer to its
753 tz = bsearch(ptz, TimeZones, countof(TimeZones), sizeof(TimeZones[0]),
754 (int (*)(const void *, const void *))ascii_strcasecmp);
758 zminutes = tz->zminutes;
759 zoccident = tz->zoccident;
762 /* ad hoc support for the European MET (now officially CET) TZ */
763 if (ascii_strcasecmp(p, "MET") == 0) {
764 if ((p = strtok (NULL, " \t")) && !ascii_strcasecmp(p, "DST")) {
770 tz_offset = zhours * 3600 + zminutes * 60;
772 tz_offset = -tz_offset;
779 if (count < 4) { /* don't check for missing timezone */
780 debug_print (1, ("error parsing date format, using received time\n"));
786 h->zminutes = zminutes;
787 h->zoccident = zoccident;
790 return mutt_mktime(&tm, 0) + tz_offset;
793 /*** XXX: MC READ MARK ***/
795 /* extract the first substring that looks like a message-id */
796 static char *extract_message_id(const char *s)
800 if ((s = strchr(s, '<')) == NULL || (p = strchr(s, '>')) == NULL)
802 return p_dupstr(s, (p - s) + 1);
805 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
811 if (cur->content->type != TYPEMESSAGE
812 && cur->content->type != TYPEMULTIPART)
813 break; /* nothing to do */
815 if (cur->content->parts)
816 break; /* The message was parsed earlier. */
818 if ((msg = mx_open_message (ctx, cur->msgno))) {
819 mutt_parse_part (msg->fp, cur->content);
821 cur->security = crypt_query (cur->content);
823 mx_close_message (&msg);
826 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);
829 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
830 short user_hdrs, short weed, short do_2047,
839 switch (ascii_tolower (line[0])) {
841 if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
842 e->to = rfc822_parse_adrlist (e->to, p);
845 else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
846 e->from = rfc822_parse_adrlist (e->from, p);
852 if (ascii_strcasecmp (line + 1, "cc") == 0) {
853 e->bcc = rfc822_parse_adrlist (e->bcc, p);
859 if (ascii_strcasecmp (line + 1, "c") == 0) {
860 e->cc = rfc822_parse_adrlist (e->cc, p);
863 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
864 if (ascii_strcasecmp (line + 8, "type") == 0) {
866 mutt_parse_content_type (p, hdr->content);
869 else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
871 hdr->content->encoding = mutt_check_encoding (p);
874 else if (ascii_strcasecmp (line + 8, "length") == 0) {
876 if ((hdr->content->length = atoi (p)) < 0)
877 hdr->content->length = -1;
881 else if (ascii_strcasecmp (line + 8, "description") == 0) {
883 m_strreplace(&hdr->content->description, p);
884 rfc2047_decode (&hdr->content->description);
888 else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
890 parse_content_disposition (p, hdr->content);
897 if (!ascii_strcasecmp ("ate", line + 1)) {
898 m_strreplace(&e->date, p);
900 hdr->date_sent = mutt_parse_date (p, hdr);
906 if (!ascii_strcasecmp ("xpires", line + 1) &&
907 hdr && mutt_parse_date (p, NULL) < time (NULL))
912 if (!ascii_strcasecmp ("rom", line + 1)) {
913 e->from = rfc822_parse_adrlist (e->from, p);
914 /* don't leave from info NULL if there's an invalid address (or
915 * whatever) in From: field; mutt would just display it as empty
916 * and mark mail/(esp.) news article as your own. aaargh! this
917 * bothered me for _years_ */
919 e->from = address_new ();
920 e->from->personal = m_strdup(p);
925 else if (!m_strcasecmp(line + 1, "ollowup-to")) {
926 if (!e->followup_to) {
928 e->followup_to = m_strdup(skipspaces(p));
936 if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
937 mutt_free_list (&e->in_reply_to);
938 e->in_reply_to = mutt_parse_references (p, 1);
944 if (!ascii_strcasecmp (line + 1, "ines")) {
946 hdr->lines = atoi (p);
949 * HACK - mutt has, for a very short time, produced negative
950 * Lines header values. Ignore them.
958 else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
959 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
960 if (strncmp (p, "NO", 2)) {
963 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
965 if (!(end = strchr (beg, '>')))
968 /* Take the first mailto URL */
969 if (url_check_scheme (beg) == U_MAILTO) {
970 p_delete(&e->list_post);
971 e->list_post = p_dupstr(beg, end - beg);
981 if (!ascii_strcasecmp (line + 1, "ime-version")) {
986 else if (!ascii_strcasecmp (line + 1, "essage-id")) {
987 /* We add a new "Message-ID:" when building a message */
988 p_delete(&e->message_id);
989 e->message_id = extract_message_id (p);
992 else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
993 if (!ascii_strcasecmp (line + 5, "reply-to")) {
994 /* override the Reply-To: field */
995 address_delete (&e->reply_to);
996 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
999 else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1000 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1008 if (!m_strcasecmp(line + 1, "ewsgroups")) {
1009 p_delete(&e->newsgroups);
1011 e->newsgroups = m_strdup(skipspaces(p));
1018 /* field `Organization:' saves only for pager! */
1019 if (!m_strcasecmp(line + 1, "rganization")) {
1020 if (!e->organization && m_strcasecmp(p, "unknown"))
1021 e->organization = m_strdup(p);
1026 if (!ascii_strcasecmp (line + 1, "eferences")) {
1027 mutt_free_list (&e->references);
1028 e->references = mutt_parse_references (p, 0);
1031 else if (!ascii_strcasecmp (line + 1, "eply-to")) {
1032 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1035 else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
1036 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1039 else if (!ascii_strcasecmp (line + 1, "eceived")) {
1040 if (hdr && !hdr->received) {
1041 char *d = strchr (p, ';');
1044 hdr->received = mutt_parse_date (d + 1, NULL);
1050 if (!ascii_strcasecmp (line + 1, "ubject")) {
1052 e->subject = m_strdup(p);
1055 else if (!ascii_strcasecmp (line + 1, "ender")) {
1056 e->sender = rfc822_parse_adrlist (e->sender, p);
1059 else if (!ascii_strcasecmp (line + 1, "tatus")) {
1078 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1079 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1080 e->supersedes = m_strdup(p);
1084 if (ascii_strcasecmp (line + 1, "o") == 0) {
1085 e->to = rfc822_parse_adrlist (e->to, p);
1091 if (ascii_strcasecmp (line + 1, "-status") == 0) {
1112 else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1113 e->x_label = m_strdup(p);
1117 else if (!m_strcasecmp(line + 1, "-comment-to")) {
1118 if (!e->x_comment_to)
1119 e->x_comment_to = m_strdup(p);
1122 else if (!m_strcasecmp(line + 1, "ref")) {
1124 e->xref = m_strdup(p);
1133 /* Keep track of the user-defined headers */
1134 if (!matched && user_hdrs) {
1135 /* restore the original line */
1136 line[m_strlen(line)] = ':';
1138 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1139 && !mutt_matches_ignore (line, UnIgnore))
1143 last->next = mutt_new_list ();
1147 last = e->userhdrs = mutt_new_list ();
1148 last->data = m_strdup(line);
1150 rfc2047_decode (&last->data);
1160 /* mutt_read_rfc822_header() -- parses a RFC822 header
1164 * f stream to read from
1166 * hdr header structure of current message (optional).
1168 * user_hdrs If set, store user headers. Used for recall-message and
1171 * weed If this parameter is set and the user has activated the
1172 * $weed option, honor the header weed list for user headers.
1173 * Used for recall-message.
1175 * Returns: newly allocated envelope structure. You should free it by
1176 * envelope_delete() when envelope stay unneeded.
1178 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1181 ENVELOPE *e = envelope_new();
1183 char *line = p_new(char, LONG_STRING);
1187 ssize_t linelen = LONG_STRING;
1188 char buf[LONG_STRING + 1];
1191 if (hdr->content == NULL) {
1192 hdr->content = mutt_new_body ();
1194 /* set the defaults from RFC1521 */
1195 hdr->content->type = TYPETEXT;
1196 hdr->content->subtype = m_strdup("plain");
1197 hdr->content->encoding = ENC7BIT;
1198 hdr->content->length = -1;
1200 /* RFC 2183 says this is arbitrary */
1201 hdr->content->disposition = DISPINLINE;
1205 while ((loc = ftello (f)),
1206 mutt_read_rfc822_line (f, &line, &linelen))
1210 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1211 char return_path[LONG_STRING];
1214 /* some bogus MTAs will quote the original "From " line */
1215 if (m_strncmp(">From ", line, 6) == 0)
1216 continue; /* just ignore */
1217 else if (is_from (line, return_path, sizeof (return_path), &t)) {
1218 /* MH somtimes has the From_ line in the middle of the header! */
1219 if (hdr && !hdr->received)
1220 hdr->received = t - mutt_local_tz (t);
1225 break; /* end of header */
1230 if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1231 if (!rx_list_match (NoSpamList, line)) {
1233 /* if spam tag already exists, figure out how to amend it */
1234 if (e->spam && *buf) {
1235 /* If SpamSep defined, append with separator */
1237 mutt_buffer_addstr (e->spam, SpamSep);
1238 mutt_buffer_addstr (e->spam, buf);
1241 /* else overwrite */
1243 e->spam->dptr = e->spam->data;
1244 *e->spam->dptr = '\0';
1245 mutt_buffer_addstr (e->spam, buf);
1249 /* spam tag is new, and match expr is non-empty; copy */
1250 else if (!e->spam && *buf) {
1251 e->spam = mutt_buffer_from (NULL, buf);
1254 /* match expr is empty; plug in null string if no existing tag */
1255 else if (!e->spam) {
1256 e->spam = mutt_buffer_from (NULL, "");
1259 if (e->spam && e->spam->data)
1260 debug_print (5, ("spam = %s\n", e->spam->data));
1267 continue; /* skip empty header fields */
1270 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1277 hdr->content->hdr_offset = hdr->offset;
1278 hdr->content->offset = ftello (f);
1279 rfc2047_decode_envelope(e);
1280 /* check for missing or invalid date */
1281 if (hdr->date_sent <= 0) {
1282 debug_print (1, ("no date found, using received "
1283 "time from msg separator\n"));
1284 hdr->date_sent = hdr->received;
1291 address_t *mutt_parse_adrlist (address_t * p, const char *s)
1295 /* check for a simple whitespace separated list of addresses */
1296 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1297 char tmp[HUGE_STRING];
1300 m_strcpy(tmp, sizeof(tmp), s);
1302 while ((r = strtok (r, " \t")) != NULL) {
1303 p = rfc822_parse_adrlist (p, r);
1308 p = rfc822_parse_adrlist (p, s);
1314 /* Compares mime types to the ok and except lists */
1315 int count_body_parts_check(LIST **checklist, BODY *b, int dflt) {
1319 /* If list is null, use default behavior. */
1325 for (type = *checklist; type; type = type->next) {
1326 a = (ATTACH_MATCH *)type->data;
1327 debug_print(5, ("cbpc: %s %d/%s ?? %s/%s [%d]... ",
1328 dflt ? "[OK] " : "[EXCL] ",
1329 b->type, b->subtype, a->major, a->minor, a->major_int));
1330 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1331 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1332 debug_print(5, ("yes\n"));
1335 debug_print(5, ("no\n"));
1341 #define AT_COUNT(why) { shallcount = 1; }
1342 #define AT_NOCOUNT(why) { shallcount = 0; }
1344 int count_body_parts (BODY *body, int flags) {
1346 int shallcount, shallrecurse;
1352 for (bp = body; bp != NULL; bp = bp->next) {
1353 /* Initial disposition is to count and not to recurse this part. */
1354 AT_COUNT("default");
1357 debug_print(5, ("bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1358 bp->description ? bp->description : ("none"),
1359 bp->filename ? bp->filename :
1360 bp->d_filename ? bp->d_filename : "(none)",
1361 bp->type, bp->subtype ? bp->subtype : "*"));
1363 if (bp->type == TYPEMESSAGE) {
1366 /* If it's an external body pointer, don't recurse it. */
1367 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1370 /* Don't count containers if they're top-level. */
1371 if (flags & M_PARTS_TOPLEVEL)
1372 AT_NOCOUNT("top-level message/*");
1373 } else if (bp->type == TYPEMULTIPART) {
1374 /* Always recurse multiparts, except multipart/alternative. */
1376 if (!m_strcasecmp(bp->subtype, "alternative"))
1379 /* Don't count containers if they're top-level. */
1380 if (flags & M_PARTS_TOPLEVEL)
1381 AT_NOCOUNT("top-level multipart");
1384 if (bp->disposition == DISPINLINE &&
1385 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1386 AT_NOCOUNT("ignore fundamental inlines");
1388 /* If this body isn't scheduled for enumeration already, don't bother
1389 * profiling it further. */
1392 /* Turn off shallcount if message type is not in ok list,
1393 * or if it is in except list. Check is done separately for
1394 * inlines vs. attachments.
1397 if (bp->disposition == DISPATTACH) {
1398 if (!count_body_parts_check(&AttachAllow, bp, 1))
1399 AT_NOCOUNT("attach not allowed");
1400 if (count_body_parts_check(&AttachExclude, bp, 0))
1401 AT_NOCOUNT("attach excluded");
1403 if (!count_body_parts_check(&InlineAllow, bp, 1))
1404 AT_NOCOUNT("inline not allowed");
1405 if (count_body_parts_check(&InlineExclude, bp, 0))
1406 AT_NOCOUNT("excluded");
1412 bp->attach_qualifies = shallcount ? 1 : 0;
1414 debug_print(5, ("cbp: %p shallcount = %d\n", bp, shallcount));
1417 debug_print(5, ("cbp: %p pre count = %d\n", bp, count));
1418 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1419 count += bp->attach_count;
1420 debug_print(5, ("cbp: %p post count = %d\n", bp, count));
1424 debug_print(5, ("bp: return %d\n", count < 0 ? 0 : count));
1425 return count < 0 ? 0 : count;
1428 int mutt_count_body_parts (HEADER *hdr, int flags) {
1429 if (!option (OPTCOUNTATTACH))
1431 if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1432 return hdr->attach_total;
1434 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1435 hdr->attach_total = count_body_parts(hdr->content, flags | M_PARTS_TOPLEVEL);
1437 hdr->attach_total = 0;
1439 hdr->attach_valid = 1;
1440 return hdr->attach_total;