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>
41 #include <lib-lib/url.h>
43 #include "recvattach.h"
48 /* Reads an arbitrarily long header field, and looks ahead for continuation
49 * lines. ``line'' must point to a dynamically allocated string; it is
50 * increased if more space is required to fit the whole line.
52 ssize_t mutt_read_rfc822_line(FILE *f, char **line, ssize_t *n)
59 /* end of file or end of headers */
60 if (!fgets(p + pos, *n - pos, f) || (ISSPACE(*p) && pos == 0)) {
65 pos += m_strlen(p + pos);
66 if (p[pos - 1] == '\n') {
69 /* remove trailing spaces. safe: p[0] is not a space */
72 } while (ISSPACE(p[pos]));
74 /* check to see if the next line is a continuation line */
76 if (c != ' ' && c != '\t') {
77 /* next line is a separate header field or EOH */
82 /* eat tabs and spaces from the beginning of the continuation line */
85 } while (c == ' ' || c == '\t');
88 /* string is still terminated because we removed at least one
89 whitespace char above */
93 if (*n < pos + STRING) {
101 /* TODO: Make that a string list somehow */
102 string_list_t *mutt_parse_references(char *s, int in_reply_to)
104 string_list_t *lst = NULL;
108 /* some mail clients add other garbage besides message-ids, so do a quick
109 * check to make sure this looks like a valid message-id
110 * some idiotic clients also break their message-ids between lines, deal
111 * with that too (give up if it's more than two lines, though)
114 for (s = strtok(s, " \t;"); s; s = strtok(NULL, " \t;")) {
119 if (s[n - 1] != '>') {
126 ssize_t m = m_strlen(s);
128 if (s[m - 1] != '>') {
131 new = p_new(char, n + m + 1);
137 /* make sure that this really does look like a message-id.
138 * it should have exactly one @, and if we're looking at
139 * an in-reply-to header, make sure that the part before
140 * the @ has more than eight characters or it's probably
144 char *at = strchr(new, '@');
147 if (!at || strchr(at + 1, '@') || (in_reply_to && at - new <= 8)) {
152 tmp = p_new(string_list_t, 1);
162 int mutt_check_encoding(const char *s)
164 int tok = mime_which_token(s, -1);
172 case MIME_QUOTED_PRINTABLE:
173 return ENCQUOTEDPRINTABLE;
176 case MIME_X_UUENCODE:
183 int mutt_check_mime_type(const char *s)
187 if (!m_strcmp(s, "*") || !m_strcmp(s, ".*"))
190 tok = mime_which_token(s, -1);
192 case MIME_TEXT: return TYPETEXT;
193 case MIME_MULTIPART: return TYPEMULTIPART;
194 case MIME_APPLICATION: return TYPEAPPLICATION;
195 case MIME_MESSAGE: return TYPEMESSAGE;
196 case MIME_IMAGE: return TYPEIMAGE;
197 case MIME_AUDIO: return TYPEAUDIO;
198 case MIME_VIDEO: return TYPEVIDEO;
199 case MIME_MODEL: return TYPEMODEL;
200 default: return TYPEOTHER;
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_list_wipe(&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_list_wipe(&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, ':'))) {
408 switch (mime_which_token(line, -1)) {
409 case MIME_CONTENT_TYPE:
410 mutt_parse_content_type (p, body);
413 case MIME_CONTENT_TRANSFER_ENCODING:
414 body->encoding = mutt_check_encoding (p);
417 case MIME_CONTENT_DISPOSITION:
418 parse_content_disposition(p, body);
421 case MIME_CONTENT_DESCRIPTION:
422 m_strreplace(&body->description, p);
423 rfc2047_decode(&body->description);
430 body->offset = ftello(fp); /* Mark the start of the real data */
431 if (!body->subtype) {
432 if (body->type == TYPETEXT)
433 body->subtype = m_strdup("plain");
434 if (body->type == TYPEMESSAGE)
435 body->subtype = m_strdup("rfc822");
442 void mutt_parse_part(FILE *fp, BODY *b)
448 bound = mutt_get_parameter("boundary", b->parameter);
449 fseeko(fp, b->offset, SEEK_SET);
450 b->parts = mutt_parse_multipart(fp, bound, b->offset + b->length,
451 mime_which_token(b->subtype, -1) == MIME_DIGEST);
456 fseeko(fp, b->offset, SEEK_SET);
458 if (mutt_is_message_type(b->type, b->subtype)) {
459 b->parts = mutt_parse_messageRFC822(fp, b);
461 if (mime_which_token(b->subtype, -1) == MIME_EXTERNAL_BODY) {
462 b->parts = mutt_read_mime_header(fp, 0);
473 /* try to recover from parsing error */
476 m_strreplace(&b->subtype, "plain");
480 /* parse a MESSAGE/RFC822 body
483 * fp stream to read from
485 * parent structure which contains info about the message/rfc822
488 * NOTE: this assumes that `parent->length' has been set!
490 BODY *mutt_parse_messageRFC822(FILE * fp, BODY * parent)
494 parent->hdr = header_new();
495 parent->hdr->offset = ftello(fp);
496 parent->hdr->env = mutt_read_rfc822_header(fp, parent->hdr, 0, 0);
498 msg = parent->hdr->content;
500 /* ignore the length given in the content-length since it could be wrong
501 and we already have the info to calculate the correct length */
502 /* if (msg->length == -1) */
503 /* if body of this message is empty, we can end up with a negative length */
504 msg->length = MAX(0, parent->length - (msg->offset - parent->offset));
506 mutt_parse_part(fp, msg);
511 /* parse a multipart structure
514 * fp stream to read from
516 * bound body separator
518 * end_off length of the multipart body (used when the final
519 * boundary is missing to avoid reading too far)
521 * digest 1 if reading a multipart/digest, 0 otherwise
525 mutt_parse_multipart(FILE *fp, const char *bound, off_t end_off, int digest)
527 char buffer[LONG_STRING];
530 int blen = m_strlen(bound);
531 int final = 0; /* did we see the ending boundary? */
534 mutt_error _("multipart message has no boundary parameter!");
538 while (ftello(fp) < end_off && fgets(buffer, sizeof(buffer), fp)) {
541 len = m_strlen(buffer);
542 crlf = len > 1 && buffer[len - 2] == '\r';
544 if (buffer[0] == '-' && buffer[1] == '-'
545 && !m_strncmp(buffer + 2, bound, blen))
550 /* if the body is empty, we can end up with a -1 length */
551 b->length = MAX(0, ftello(fp) - b->offset - len - 1 - crlf);
552 if (b->parts && b->parts->length == 0) {
553 b->parts->length = ftello(fp) - b->parts->offset
558 /* Remove any trailing whitespace, up to the length of the boundary */
559 for (i = len - 1; ISSPACE(buffer[i]) && i >= blen + 2; i--)
562 /* Check for the end boundary */
563 final = buffer[blen + 3] == '-' && buffer[blen + 4] == '-';
567 if (buffer[2 + blen] == '\0') {
568 BODY *new = mutt_read_mime_header(fp, digest);
571 * Consistency checking - catch
572 * bad attachment end boundaries
575 if (new->offset > end_off) {
576 mutt_free_body(&new);
581 last = &(*last)->next;
587 /* in case of missing end boundary, set the length to something reasonable */
588 if (*last && (*last)->length == 0 && !final)
589 (*last)->length = end_off - (*last)->offset;
591 /* parse recursive MIME parts */
594 for (b = head; b; b = b->next)
595 mutt_parse_part(fp, b);
602 uncomment_timezone(char *buf, size_t buflen, const char *tz)
607 return tz; /* no need to do anything */
609 tz = vskipspaces(tz + 1);
610 p = strpbrk(tz, " )");
614 m_strncpy(buf, buflen, tz, p - tz);
618 /* parses a date string in RFC822 format:
620 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
622 * This routine assumes that `h' has been initialized to 0. the `timezone'
623 * field is optional, defaulting to +0000 if missing.
625 time_t mutt_parse_date(const char *s, HEADER *h)
627 int zhours = 0, zminutes = 0, zoccident = 0;
628 char scratch[SHORT_STRING];
633 /* Don't modify our argument. Fixed-size buffer is ok here since
634 the date format imposes a natural limit. */
636 m_strcpy(scratch, sizeof(scratch), s);
638 /* kill the day of the week, if it exists. */
639 p = strchr(scratch, ',');
640 p = vskipspaces(p ? p + 1 : scratch);
644 while ((p = strtok (p, " \t")) != NULL) {
645 char tzstr[SHORT_STRING];
649 case 0: /* day of the month */
650 if (!isdigit((unsigned char)*p))
652 tm.tm_mday = atoi(p);
657 case 1: /* month of the year */
658 tm.tm_mon = mutt_check_month(p);
664 tm.tm_year = atoi(p);
667 else if (tm.tm_year >= 1900)
671 case 3: /* time of day */
672 tm.tm_hour = strtol(p, &p, 10);
675 tm.tm_min = strtol(p, &p, 10);
677 tm.tm_sec = strtol(p, &p, 10);
683 case 4: /* timezone */
684 /* sometimes we see things like (MST) or (-0700) so attempt to
685 * compensate by uncommenting the string if non-RFC822 compliant
687 ptz = uncomment_timezone(tzstr, sizeof(tzstr), p);
689 if (*ptz == '+' || *ptz == '-') {
690 if (isdigit((unsigned char)ptz[1])
691 && isdigit((unsigned char)ptz[2])
692 && isdigit((unsigned char)ptz[3])
693 && isdigit((unsigned char)ptz[4]))
695 zoccident = ptz[0] == '-';
696 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
697 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
706 if (count < 4) { /* don't check for missing timezone */
712 h->zminutes = zminutes;
713 h->zoccident = zoccident;
716 return mutt_mktime(&tm, 0) + (zoccident ? 1 : -1) * (zhours * 3600 + zminutes * 60);
719 string_list_t **mutt_parse_rfc822_line(ENVELOPE *e, HEADER *hdr, char *line, char *p,
720 short weed, short do_2047, string_list_t **user_hdrs)
722 switch (mime_which_token(line, -1)) {
723 case MIME_APPARENTLY_FROM:
724 e->from = rfc822_parse_adrlist (e->from, p);
727 case MIME_APPARENTLY_TO:
728 e->to = rfc822_parse_adrlist (e->to, p);
732 e->bcc = rfc822_parse_adrlist (e->bcc, p);
736 e->cc = rfc822_parse_adrlist (e->cc, p);
739 case MIME_CONTENT_DESCRIPTION:
741 m_strreplace(&hdr->content->description, p);
742 rfc2047_decode(&hdr->content->description);
746 case MIME_CONTENT_DISPOSITION:
748 parse_content_disposition(p, hdr->content);
751 case MIME_CONTENT_LENGTH:
753 if ((hdr->content->length = atoi(p)) < 0)
754 hdr->content->length = -1;
758 case MIME_CONTENT_TRANSFER_ENCODING:
760 hdr->content->encoding = mutt_check_encoding(p);
763 case MIME_CONTENT_TYPE:
765 mutt_parse_content_type (p, hdr->content);
769 m_strreplace(&e->date, p);
771 hdr->date_sent = mutt_parse_date (p, hdr);
775 if (hdr && mutt_parse_date (p, NULL) < time (NULL))
780 case MIME_FOLLOWUP_TO:
781 if (!e->followup_to) {
783 e->followup_to = m_strdup(skipspaces(p));
789 e->from = rfc822_parse_adrlist(e->from, p);
790 /* don't leave from info NULL if there's an invalid address (or
791 * whatever) in From: field; mutt would just display it as empty
792 * and mark mail/(esp.) news article as your own. aaargh! this
793 * bothered me for _years_ */
795 e->from = address_new();
796 e->from->personal = m_strdup(p);
800 case MIME_IN_REPLY_TO:
801 string_list_wipe(&e->in_reply_to);
802 e->in_reply_to = mutt_parse_references(p, 1);
807 /* HACK - mutt has, for a very short time, produced negative
808 Lines header values. Ignore them. */
809 hdr->lines = MAX(0, atoi(p));
814 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
815 if (strncmp(p, "NO", 2)) {
818 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
820 if (!(end = strchr (beg, '>')))
823 /* Take the first mailto URL */
824 if (url_check_scheme (beg) == U_MAILTO) {
825 p_delete(&e->list_post);
826 e->list_post = p_dupstr(beg, end - beg);
833 case MIME_MAIL_FOLLOWUP_TO:
834 e->mail_followup_to = rfc822_parse_adrlist(e->mail_followup_to, p);
837 case MIME_MAIL_REPLY_TO:
838 address_list_wipe(&e->reply_to);
839 e->reply_to = rfc822_parse_adrlist(e->reply_to, p);
842 case MIME_MESSAGE_ID:
844 const char *beg, *end;
846 /* We add a new "Message-ID:" when building a message */
847 p_delete(&e->message_id);
849 if ((beg = strchr(p, '<')) && (end = strchr(beg, '>')))
850 e->message_id = p_dupstr(beg, (end - beg) + 1);
854 case MIME_MIME_VERSION:
860 case MIME_NEWSGROUPS:
861 p_delete(&e->newsgroups);
863 e->newsgroups = m_strdup(skipspaces(p));
867 case MIME_ORGANIZATION:
868 if (!e->organization && mime_which_token(p, -1) == MIME_UNKNOWN)
869 e->organization = m_strdup(p);
873 if (hdr && !hdr->received) {
874 char *d = strchr(p, ';');
876 hdr->received = mutt_parse_date(d + 1, NULL);
880 case MIME_REFERENCES:
881 string_list_wipe(&e->references);
882 e->references = mutt_parse_references(p, 0);
886 e->reply_to = rfc822_parse_adrlist(e->reply_to, p);
889 case MIME_RETURN_PATH:
890 e->return_path = rfc822_parse_adrlist(e->return_path, p);
894 e->sender = rfc822_parse_adrlist (e->sender, p);
918 e->subject = m_strdup(p);
921 case MIME_SUPERCEDES:
922 case MIME_SUPERSEDES:
924 e->supersedes = m_strdup(p);
928 e->to = rfc822_parse_adrlist(e->to, p);
932 case MIME_X_COMMENT_TO:
933 if (!e->x_comment_to)
934 e->x_comment_to = m_strdup(p);
939 e->x_label = m_strdup(p);
945 e->xref = m_strdup(p);
974 /* restore the original line */
975 line[m_strlen(line)] = ':';
977 if (weed && option(OPTWEED) && mutt_matches_ignore(line, Ignore)
978 && !mutt_matches_ignore(line, UnIgnore)) {
982 *user_hdrs = string_item_new();
983 (*user_hdrs)->data = m_strdup(line);
985 rfc2047_decode(&(*user_hdrs)->data);
986 return &(*user_hdrs)->next;
992 /* mutt_read_rfc822_header() -- parses a RFC822 header
996 * f stream to read from
998 * hdr header structure of current message (optional).
1000 * user_hdrs If set, store user headers. Used for recall-message and
1003 * weed If this parameter is set and the user has activated the
1004 * $weed option, honor the header weed list for user headers.
1005 * Used for recall-message.
1007 * Returns: newly allocated envelope structure. You should free it by
1008 * envelope_delete() when envelope stay unneeded.
1011 mutt_read_rfc822_header(FILE *f, HEADER *hdr, short user_hdrs, short weed)
1013 ENVELOPE *e = envelope_new();
1014 string_list_t **last = user_hdrs ? &e->userhdrs : NULL;
1016 char *line = p_new(char, LONG_STRING);
1017 ssize_t linelen = LONG_STRING;
1020 if (hdr && !hdr->content) {
1021 hdr->content = mutt_new_body ();
1023 /* set the defaults from RFC1521 */
1024 hdr->content->type = TYPETEXT;
1025 hdr->content->subtype = m_strdup("plain");
1026 hdr->content->encoding = ENC7BIT;
1027 hdr->content->length = -1;
1029 /* RFC 2183 says this is arbitrary */
1030 hdr->content->disposition = DISPINLINE;
1033 while ((loc = ftello(f)),
1034 mutt_read_rfc822_line(f, &line, &linelen))
1036 char buf[LONG_STRING + 1] = "";
1039 p = strpbrk(line, ": \t");
1040 if (!p || *p != ':') {
1041 char return_path[LONG_STRING];
1044 /* some bogus MTAs will quote the original "From " line */
1045 if (!m_strncmp(">From ", line, 6))
1046 continue; /* just ignore */
1048 if (is_from(line, return_path, sizeof(return_path), &t)) {
1049 /* MH somtimes has the From_ line in the middle of the header! */
1050 if (hdr && !hdr->received)
1051 hdr->received = t - mutt_local_tz(t);
1056 break; /* end of header */
1059 if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf))) {
1060 if (!rx_list_match(NoSpamList, line)) {
1061 /* if spam tag already exists, figure out how to amend it */
1062 if (e->spam && *buf) {
1064 /* If SpamSep defined, append with separator */
1065 mutt_buffer_addstr(e->spam, SpamSep);
1066 mutt_buffer_addstr(e->spam, buf);
1068 /* else overwrite */
1069 mutt_buffer_reset(e->spam);
1070 mutt_buffer_addstr(e->spam, buf);
1073 else if (!e->spam && *buf) {
1074 /* spam tag is new, and match expr is non-empty; copy */
1075 e->spam = mutt_buffer_from(NULL, buf);
1077 else if (!e->spam) {
1078 /* match expr is empty; plug in null string if no existing tag */
1079 e->spam = mutt_buffer_from(NULL, "");
1087 continue; /* skip empty header fields */
1089 last = mutt_parse_rfc822_line(e, hdr, line, p, weed, 1, last);
1095 hdr->content->hdr_offset = hdr->offset;
1096 hdr->content->offset = ftello(f);
1097 rfc2047_decode_envelope(e);
1098 /* check for missing or invalid date */
1099 if (hdr->date_sent <= 0) {
1100 hdr->date_sent = hdr->received;
1107 /* Compares mime types to the ok and except lists */
1108 static int count_body_parts_check(string_list_t **checklist, BODY *b)
1110 string_list_t *type;
1112 for (type = *checklist; type; type = type->next) {
1113 ATTACH_MATCH *a = (ATTACH_MATCH *)type->data;
1115 if ((a->major_int == TYPEANY || a->major_int == b->type)
1116 && !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1124 static int count_body_parts (BODY *body, int flags)
1132 for (bp = body; bp != NULL; bp = bp->next) {
1133 /* Initial disposition is to count and not to recurse this part. */
1134 int shallcount, shallrecurse, iscontainer;
1135 int tok = mime_which_token(bp->subtype, -1);
1137 iscontainer = bp->type == TYPEMESSAGE || bp->type == TYPEMULTIPART;
1139 /* don't recurse in external bodies or multipart/alternatives */
1140 shallrecurse = (bp->type == TYPEMESSAGE && tok != MIME_EXTERNAL_BODY)
1141 || (bp->type == TYPEMULTIPART && tok != MIME_ALTERNATIVE);
1143 /* Don't count top level containers and fundamental inlines */
1144 shallcount = !(iscontainer && (flags & M_PARTS_TOPLEVEL))
1145 && !(!iscontainer && bp->disposition == DISPINLINE && bp == body);
1148 /* Turn off shallcount if message type is not in ok list,
1149 * or if it is in except list. Check is done separately for
1150 * inlines vs. attachments.
1153 if (bp->disposition == DISPATTACH) {
1154 if (!count_body_parts_check(&AttachAllow, bp))
1156 if (count_body_parts_check(&AttachExclude, bp))
1159 if (!count_body_parts_check(&InlineAllow, bp))
1161 if (count_body_parts_check(&InlineExclude, bp))
1166 bp->attach_qualifies = shallcount;
1167 count += shallcount;
1170 bp->attach_count = count_body_parts(bp->parts,
1171 flags & ~M_PARTS_TOPLEVEL);
1172 count += bp->attach_count;
1179 int mutt_count_body_parts(HEADER *hdr, int flags)
1181 if (!option(OPTCOUNTATTACH))
1184 if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1185 return hdr->attach_total;
1187 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1188 hdr->attach_total = count_body_parts(hdr->content,
1189 flags | M_PARTS_TOPLEVEL);
1191 hdr->attach_total = 0;
1193 hdr->attach_valid = 1;
1194 return hdr->attach_total;