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 "recvattach.h"
45 #include "lib/debug.h"
49 /* Reads an arbitrarily long header field, and looks ahead for continuation
50 * lines. ``line'' must point to a dynamically allocated string; it is
51 * increased if more space is required to fit the whole line.
53 ssize_t mutt_read_rfc822_line(FILE *f, char **line, ssize_t *n)
60 /* end of file or end of headers */
61 if (!fgets(p + pos, *n - pos, f) || (ISSPACE(*p) && pos == 0)) {
66 pos += m_strlen(p + pos);
67 if (p[pos - 1] == '\n') {
70 /* remove trailing spaces. safe: p[0] is not a space */
73 } while (ISSPACE(p[pos]));
75 /* check to see if the next line is a continuation line */
77 if (c != ' ' && c != '\t') {
78 /* next line is a separate header field or EOH */
83 /* eat tabs and spaces from the beginning of the continuation line */
86 } while (c == ' ' || c == '\t');
89 /* string is still terminated because we removed at least one
90 whitespace char above */
94 if (*n < pos + STRING) {
102 /* TODO: Make that a string list somehow */
103 LIST *mutt_parse_references(char *s, int in_reply_to)
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)
115 for (s = strtok(s, " \t;"); s; s = strtok(NULL, " \t;")) {
120 if (s[n - 1] != '>') {
127 ssize_t m = m_strlen(s);
129 if (s[m - 1] != '>') {
132 new = p_new(char, n + m + 1);
138 /* make sure that this really does look like a message-id.
139 * it should have exactly one @, and if we're looking at
140 * an in-reply-to header, make sure that the part before
141 * the @ has more than eight characters or it's probably
145 char *at = strchr(new, '@');
148 if (!at || strchr(at + 1, '@') || (in_reply_to && at - new <= 8)) {
153 tmp = p_new(LIST, 1);
163 int mutt_check_encoding(const char *s)
165 #define COMPARE(tok, value) \
166 if (!ascii_strncasecmp(tok, s, sizeof(tok) - 1)) { \
169 COMPARE("7bit", ENC7BIT);
170 COMPARE("8bit", ENC8BIT);
171 COMPARE("binary", ENCBINARY);
172 COMPARE("quoted-printable", ENCQUOTEDPRINTABLE);
173 COMPARE("base64", ENCBASE64);
174 COMPARE("x-uuencode", ENCUUENCODED);
180 int mutt_check_mime_type(const char *s)
182 #define COMPARE(tok, value) \
183 if (!ascii_strncasecmp(tok, s, sizeof(tok) - 1)) { \
186 COMPARE("text", TYPETEXT);
187 COMPARE("multipart", TYPEMULTIPART);
188 COMPARE("application", TYPEAPPLICATION);
189 COMPARE("message", TYPEMESSAGE);
190 COMPARE("image", TYPEIMAGE);
191 COMPARE("audio", TYPEAUDIO);
192 COMPARE("video", TYPEVIDEO);
193 COMPARE("model", TYPEMODEL);
194 COMPARE("*", TYPEANY);
195 COMPARE(".*", TYPEANY);
201 static PARAMETER *parse_parameters(const char *s)
203 PARAMETER *res = NULL;
204 PARAMETER **list = &res;
212 if (*s == '=') /* parameters are fucked up, go away */
215 p = strpbrk(s, "=;");
220 /* if we hit a ; now the parameter has no value, just skip it */
226 new = parameter_new();
227 new->attribute = p_dupstr(s, i);
229 while (--i >= 0 && ISSPACE(new->attribute[i])) {
230 new->attribute[i] = '\0';
232 s = skipspaces(p + 1); /* skip over the = */
235 char buffer[LONG_STRING];
239 for (i = 0; *s && i < ssizeof(buffer) - 1; i++, s++) {
240 if (!option(OPTSTRICTMIME)) {
241 /* As iso-2022-* has a characer of '"' with non-ascii state,
243 if (*s == 0x1b && i < ssizeof(buffer) - 2) {
244 state_ascii = s[1] == '(' && (s[2] == 'B' || s[2] == 'J');
247 if (state_ascii && *s == '"')
257 new->value = p_dupstr(buffer, i);
259 for (p = s; *p && *p != ' ' && *p != ';'; p++);
260 new->value = p_dupstr(s, p - s);
266 s = strchr(s, ';'); /* Find the next parameter */
268 break; /* no more parameters */
271 rfc2231_decode_parameters(&res);
275 void mutt_parse_content_type(char *s, BODY *ct)
280 p_delete(&ct->subtype);
281 parameter_delete(&ct->parameter);
283 /* First extract any existing parameters */
284 if ((pc = strchr(s, ';')) != NULL) {
286 ct->parameter = parse_parameters(vskipspaces(pc));
288 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
289 * but if a filename has already been set in the content-disposition,
290 * let that take precedence, and don't set it here */
291 pc = mutt_get_parameter("name", ct->parameter);
292 if (pc && !ct->filename)
293 ct->filename = m_strdup(pc);
296 /* Now get the subtype */
297 if ((subtype = strchr (s, '/'))) {
299 for (pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++);
300 ct->subtype = p_dupstr(subtype, pc - subtype);
303 /* Finally, get the major type */
304 ct->type = mutt_check_mime_type(s);
306 if (ct->type == TYPEOTHER) {
307 ct->xtype = m_strdup(s);
311 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
312 * field, so we can attempt to convert the type to BODY here.
315 char buffer[SHORT_STRING];
318 ct->subtype = m_strdup("plain");
322 ct->subtype = m_strdup("basic");
326 ct->subtype = m_strdup("rfc822");
330 ct->type = TYPEAPPLICATION;
331 snprintf(buffer, sizeof(buffer), "x-%s", s);
332 ct->subtype = m_strdup(buffer);
336 ct->subtype = m_strdup("x-unknown");
341 /* Default character set for text types. */
342 if (ct->type == TYPETEXT) {
343 pc = mutt_get_parameter("charset", ct->parameter);
345 mutt_set_parameter("charset",
346 option(OPTSTRICTMIME) ? "us-ascii" :
347 mutt_get_first_charset(AssumedCharset),
353 static void parse_content_disposition(char *s, BODY *ct)
355 if (!ascii_strncasecmp(s, "inline", 6)) {
356 ct->disposition = DISPINLINE;
357 } else if (!ascii_strncasecmp(s, "form-data", 9)) {
358 ct->disposition = DISPFORMDATA;
360 ct->disposition = DISPATTACH;
363 /* Check to see if a default filename was given */
364 if ((s = strchr (s, ';'))) {
365 PARAMETER *parms = parse_parameters(vskipspaces(s));
367 if ((s = mutt_get_parameter("filename", parms)))
368 m_strreplace(&ct->filename, s);
369 if ((s = mutt_get_parameter ("name", parms)))
370 ct->form_name = m_strdup(s);
372 parameter_delete(&parms);
377 * fp stream to read from
379 * digest 1 if reading subparts of a multipart/digest, 0
382 BODY *mutt_read_mime_header(FILE *fp, int digest)
384 BODY *body = mutt_new_body ();
385 char *line = p_new(char, LONG_STRING);
386 ssize_t linelen = LONG_STRING;
389 body->hdr_offset = ftello(fp);
390 body->encoding = ENC7BIT; /* default from RFC1521 */
391 body->disposition = DISPINLINE;
392 body->type = digest ? TYPEMESSAGE : TYPETEXT;
394 while (mutt_read_rfc822_line(fp, &line, &linelen)) {
395 /* Find the value of the current header */
396 if ((p = strchr(line, ':'))) {
402 debug_print (1, ("bogus MIME header: %s\n", line));
406 if (!ascii_strncasecmp(line, "content-", 8)) {
407 if (!ascii_strcasecmp("type", line + 8))
408 mutt_parse_content_type (p, body);
409 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
410 body->encoding = mutt_check_encoding (p);
411 else if (!ascii_strcasecmp ("disposition", line + 8))
412 parse_content_disposition(p, body);
413 else if (!ascii_strcasecmp ("description", line + 8)) {
414 m_strreplace(&body->description, p);
415 rfc2047_decode(&body->description);
420 body->offset = ftello(fp); /* Mark the start of the real data */
421 if (!body->subtype) {
422 if (body->type == TYPETEXT)
423 body->subtype = m_strdup("plain");
424 if (body->type == TYPEMESSAGE)
425 body->subtype = m_strdup("rfc822");
432 void mutt_parse_part(FILE *fp, BODY *b)
438 bound = mutt_get_parameter("boundary", b->parameter);
439 fseeko(fp, b->offset, SEEK_SET);
440 b->parts = mutt_parse_multipart(fp, bound, b->offset + b->length,
441 !ascii_strcasecmp("digest", b->subtype));
446 fseeko(fp, b->offset, SEEK_SET);
448 if (mutt_is_message_type(b->type, b->subtype)) {
449 b->parts = mutt_parse_messageRFC822(fp, b);
451 if (!ascii_strcasecmp(b->subtype, "external-body") == 0) {
452 b->parts = mutt_read_mime_header(fp, 0);
463 /* try to recover from parsing error */
466 m_strreplace(&b->subtype, "plain");
470 /* parse a MESSAGE/RFC822 body
473 * fp stream to read from
475 * parent structure which contains info about the message/rfc822
478 * NOTE: this assumes that `parent->length' has been set!
480 BODY *mutt_parse_messageRFC822(FILE * fp, BODY * parent)
484 parent->hdr = header_new();
485 parent->hdr->offset = ftello(fp);
486 parent->hdr->env = mutt_read_rfc822_header(fp, parent->hdr, 0, 0);
488 msg = parent->hdr->content;
490 /* ignore the length given in the content-length since it could be wrong
491 and we already have the info to calculate the correct length */
492 /* if (msg->length == -1) */
493 /* if body of this message is empty, we can end up with a negative length */
494 msg->length = MAX(0, parent->length - (msg->offset - parent->offset));
496 mutt_parse_part(fp, msg);
501 /* parse a multipart structure
504 * fp stream to read from
506 * bound body separator
508 * end_off length of the multipart body (used when the final
509 * boundary is missing to avoid reading too far)
511 * digest 1 if reading a multipart/digest, 0 otherwise
515 mutt_parse_multipart(FILE *fp, const char *bound, off_t end_off, int digest)
517 char buffer[LONG_STRING];
520 int blen = m_strlen(bound);
521 int final = 0; /* did we see the ending boundary? */
524 mutt_error _("multipart message has no boundary parameter!");
528 while (ftello(fp) < end_off && fgets(buffer, sizeof(buffer), fp)) {
531 len = m_strlen(buffer);
532 crlf = len > 1 && buffer[len - 2] == '\r';
534 if (buffer[0] == '-' && buffer[1] == '-'
535 && !m_strncmp(buffer + 2, bound, blen))
540 /* if the body is empty, we can end up with a -1 length */
541 b->length = MAX(0, ftello(fp) - b->offset - len - 1 - crlf);
542 if (b->parts && b->parts->length == 0) {
543 b->parts->length = ftello(fp) - b->parts->offset
548 /* Remove any trailing whitespace, up to the length of the boundary */
549 for (i = len - 1; ISSPACE(buffer[i]) && i >= blen + 2; i--)
552 /* Check for the end boundary */
553 final = buffer[blen + 3] == '-' && buffer[blen + 4] == '-';
557 if (buffer[2 + blen] == '\0') {
558 BODY *new = mutt_read_mime_header(fp, digest);
561 * Consistency checking - catch
562 * bad attachment end boundaries
565 if (new->offset > end_off) {
566 mutt_free_body(&new);
571 last = &(*last)->next;
577 /* in case of missing end boundary, set the length to something reasonable */
578 if (*last && (*last)->length == 0 && !final)
579 (*last)->length = end_off - (*last)->offset;
581 /* parse recursive MIME parts */
584 for (b = head; b; b = b->next)
585 mutt_parse_part(fp, b);
592 uncomment_timezone(char *buf, size_t buflen, const char *tz)
597 return tz; /* no need to do anything */
599 tz = vskipspaces(tz + 1);
600 p = strpbrk(tz, " )");
604 m_strncpy(buf, buflen, tz, p - tz);
610 unsigned char zhours;
611 unsigned char zminutes;
612 unsigned char zoccident; /* west of UTC? */
614 {"aat", 1, 0, 1}, /* Atlantic Africa Time */
615 {"adt", 4, 0, 0}, /* Arabia DST */
616 {"ast", 3, 0, 0}, /* Arabia */
617 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
618 {"bst", 1, 0, 0}, /* British DST */
619 {"cat", 1, 0, 0}, /* Central Africa */
621 {"cest", 2, 0, 0}, /* Central Europe DST */
622 {"cet", 1, 0, 0}, /* Central Europe */
624 /*{ "cst", 8, 0, 0 }, *//* China */
625 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
626 {"eat", 3, 0, 0}, /* East Africa */
628 {"eest", 3, 0, 0}, /* Eastern Europe DST */
629 {"eet", 2, 0, 0}, /* Eastern Europe */
630 {"egst", 0, 0, 0}, /* Eastern Greenland DST */
631 {"egt", 1, 0, 1}, /* Eastern Greenland */
634 {"gst", 4, 0, 0}, /* Presian Gulf */
635 {"hkt", 8, 0, 0}, /* Hong Kong */
636 {"ict", 7, 0, 0}, /* Indochina */
637 {"idt", 3, 0, 0}, /* Israel DST */
638 {"ist", 2, 0, 0}, /* Israel */
639 /*{ "ist", 5, 30, 0 }, *//* India */
640 {"jst", 9, 0, 0}, /* Japan */
641 {"kst", 9, 0, 0}, /* Korea */
643 {"met", 1, 0, 0}, /* this is now officially CET */
644 {"msd", 4, 0, 0}, /* Moscow DST */
645 {"msk", 3, 0, 0}, /* Moscow */
647 {"nzdt", 13, 0, 0}, /* New Zealand DST */
648 {"nzst", 12, 0, 0}, /* New Zealand */
651 {"sat", 2, 0, 0}, /* South Africa */
652 {"smt", 4, 0, 0}, /* Seychelles */
653 {"sst", 11, 0, 1}, /* Samoa */
654 /*{ "sst", 8, 0, 0 }, *//* Singapore */
656 {"wat", 0, 0, 0}, /* West Africa */
657 {"west", 1, 0, 0}, /* Western Europe DST */
658 {"wet", 0, 0, 0}, /* Western Europe */
659 {"wgst", 2, 0, 1}, /* Western Greenland DST */
660 {"wgt", 3, 0, 1}, /* Western Greenland */
661 {"wst", 8, 0, 0}, /* Western Australia */
664 /* parses a date string in RFC822 format:
666 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
668 * This routine assumes that `h' has been initialized to 0. the `timezone'
669 * field is optional, defaulting to +0000 if missing.
671 time_t mutt_parse_date(const char *s, HEADER *h)
673 int zhours = 0, zminutes = 0, zoccident = 0;
674 char scratch[SHORT_STRING];
679 /* Don't modify our argument. Fixed-size buffer is ok here since
680 the date format imposes a natural limit. */
682 m_strcpy(scratch, sizeof(scratch), s);
684 /* kill the day of the week, if it exists. */
685 p = strchr(scratch, ',');
686 p = vskipspaces(p ? p + 1 : scratch);
690 while ((p = strtok (p, " \t")) != NULL) {
691 char tzstr[SHORT_STRING];
695 case 0: /* day of the month */
696 if (!isdigit((unsigned char)*p))
698 tm.tm_mday = atoi(p);
703 case 1: /* month of the year */
704 tm.tm_mon = mutt_check_month(p);
710 tm.tm_year = atoi(p);
713 else if (tm.tm_year >= 1900)
717 case 3: /* time of day */
718 tm.tm_hour = strtol(p, &p, 10);
721 tm.tm_min = strtol(p, &p, 10);
723 tm.tm_sec = strtol(p, &p, 10);
729 case 4: /* timezone */
730 /* sometimes we see things like (MST) or (-0700) so attempt to
731 * compensate by uncommenting the string if non-RFC822 compliant
733 ptz = uncomment_timezone(tzstr, sizeof(tzstr), p);
735 if (*ptz == '+' || *ptz == '-') {
736 if (isdigit((unsigned char)ptz[1])
737 && isdigit((unsigned char)ptz[2])
738 && isdigit((unsigned char)ptz[3])
739 && isdigit((unsigned char)ptz[4]))
741 zoccident = ptz[0] == '-';
742 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
743 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
748 /* This is safe to do: A pointer to a struct equals a pointer to its
750 tz = bsearch(ptz, TimeZones, countof(TimeZones), sizeof(TimeZones[0]),
751 (int (*)(const void *, const void *))ascii_strcasecmp);
755 zminutes = tz->zminutes;
756 zoccident = tz->zoccident;
759 /* ad hoc support for the European MET (now officially CET) TZ */
760 if (ascii_strcasecmp(p, "MET") == 0) {
761 if ((p = strtok (NULL, " \t")) && !ascii_strcasecmp(p, "DST")) {
772 if (count < 4) { /* don't check for missing timezone */
773 debug_print (1, ("error parsing date format, using received time\n"));
779 h->zminutes = zminutes;
780 h->zoccident = zoccident;
783 return mutt_mktime(&tm, 0) + (zoccident ? 1 : -1) * (zhours * 3600 + zminutes * 60);
786 #include "rfc822hdrs.h"
788 LIST **mutt_parse_rfc822_line(ENVELOPE *e, HEADER *hdr, char *line, char *p,
789 short weed, short do_2047, LIST **user_hdrs)
791 switch (rfc822_well_known(line)) {
792 case HDR_APPARENTLY_FROM:
793 e->from = rfc822_parse_adrlist (e->from, p);
796 case HDR_APPARENTLY_TO:
797 e->to = rfc822_parse_adrlist (e->to, p);
801 e->bcc = rfc822_parse_adrlist (e->bcc, p);
805 e->cc = rfc822_parse_adrlist (e->cc, p);
808 case HDR_CONTENT_DESCRIPTION:
810 m_strreplace(&hdr->content->description, p);
811 rfc2047_decode(&hdr->content->description);
815 case HDR_CONTENT_DISPOSITION:
817 parse_content_disposition(p, hdr->content);
820 case HDR_CONTENT_LENGTH:
822 if ((hdr->content->length = atoi(p)) < 0)
823 hdr->content->length = -1;
827 case HDR_CONTENT_TRANSFER_ENCODING:
829 hdr->content->encoding = mutt_check_encoding(p);
832 case HDR_CONTENT_TYPE:
834 mutt_parse_content_type (p, hdr->content);
838 m_strreplace(&e->date, p);
840 hdr->date_sent = mutt_parse_date (p, hdr);
844 if (hdr && mutt_parse_date (p, NULL) < time (NULL))
849 case HDR_FOLLOWUP_TO:
850 if (!e->followup_to) {
852 e->followup_to = m_strdup(skipspaces(p));
858 e->from = rfc822_parse_adrlist(e->from, p);
859 /* don't leave from info NULL if there's an invalid address (or
860 * whatever) in From: field; mutt would just display it as empty
861 * and mark mail/(esp.) news article as your own. aaargh! this
862 * bothered me for _years_ */
864 e->from = address_new();
865 e->from->personal = m_strdup(p);
869 case HDR_IN_REPLY_TO:
870 mutt_free_list(&e->in_reply_to);
871 e->in_reply_to = mutt_parse_references(p, 1);
876 /* HACK - mutt has, for a very short time, produced negative
877 Lines header values. Ignore them. */
878 hdr->lines = MAX(0, atoi(p));
883 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
884 if (strncmp(p, "NO", 2)) {
887 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
889 if (!(end = strchr (beg, '>')))
892 /* Take the first mailto URL */
893 if (url_check_scheme (beg) == U_MAILTO) {
894 p_delete(&e->list_post);
895 e->list_post = p_dupstr(beg, end - beg);
902 case HDR_MAIL_FOLLOWUP_TO:
903 e->mail_followup_to = rfc822_parse_adrlist(e->mail_followup_to, p);
906 case HDR_MAIL_REPLY_TO:
907 address_delete (&e->reply_to);
908 e->reply_to = rfc822_parse_adrlist(e->reply_to, p);
913 const char *beg, *end;
915 /* We add a new "Message-ID:" when building a message */
916 p_delete(&e->message_id);
918 if ((beg = strchr(p, '<')) && (end = strchr(beg, '>')))
919 e->message_id = p_dupstr(beg, (end - beg) + 1);
923 case HDR_MIME_VERSION:
930 p_delete(&e->newsgroups);
932 e->newsgroups = m_strdup(skipspaces(p));
936 case HDR_ORGANIZATION:
937 if (!e->organization && m_strcasecmp(p, "unknown"))
938 e->organization = m_strdup(p);
942 if (hdr && !hdr->received) {
943 char *d = strchr(p, ';');
945 hdr->received = mutt_parse_date(d + 1, NULL);
950 mutt_free_list(&e->references);
951 e->references = mutt_parse_references(p, 0);
955 e->reply_to = rfc822_parse_adrlist(e->reply_to, p);
958 case HDR_RETURN_PATH:
959 e->return_path = rfc822_parse_adrlist(e->return_path, p);
963 e->sender = rfc822_parse_adrlist (e->sender, p);
987 e->subject = m_strdup(p);
993 e->supersedes = m_strdup(p);
997 e->to = rfc822_parse_adrlist(e->to, p);
1001 case HDR_X_COMMENT_TO:
1002 if (!e->x_comment_to)
1003 e->x_comment_to = m_strdup(p);
1008 e->x_label = m_strdup(p);
1014 e->xref = m_strdup(p);
1043 /* restore the original line */
1044 line[m_strlen(line)] = ':';
1046 if (weed && option(OPTWEED) && mutt_matches_ignore(line, Ignore)
1047 && !mutt_matches_ignore(line, UnIgnore)) {
1051 *user_hdrs = mutt_new_list();
1052 (*user_hdrs)->data = m_strdup(line);
1054 rfc2047_decode(&(*user_hdrs)->data);
1055 (*user_hdrs)->next = mutt_new_list();
1056 return &(*user_hdrs)->next;
1062 /* mutt_read_rfc822_header() -- parses a RFC822 header
1066 * f stream to read from
1068 * hdr header structure of current message (optional).
1070 * user_hdrs If set, store user headers. Used for recall-message and
1073 * weed If this parameter is set and the user has activated the
1074 * $weed option, honor the header weed list for user headers.
1075 * Used for recall-message.
1077 * Returns: newly allocated envelope structure. You should free it by
1078 * envelope_delete() when envelope stay unneeded.
1081 mutt_read_rfc822_header(FILE *f, HEADER *hdr, short user_hdrs, short weed)
1083 ENVELOPE *e = envelope_new();
1084 LIST **last = user_hdrs ? &e->userhdrs : NULL;
1086 char *line = p_new(char, LONG_STRING);
1087 ssize_t linelen = LONG_STRING;
1090 if (hdr && !hdr->content) {
1091 hdr->content = mutt_new_body ();
1093 /* set the defaults from RFC1521 */
1094 hdr->content->type = TYPETEXT;
1095 hdr->content->subtype = m_strdup("plain");
1096 hdr->content->encoding = ENC7BIT;
1097 hdr->content->length = -1;
1099 /* RFC 2183 says this is arbitrary */
1100 hdr->content->disposition = DISPINLINE;
1103 while ((loc = ftello(f)),
1104 mutt_read_rfc822_line(f, &line, &linelen))
1106 char buf[LONG_STRING + 1] = "";
1109 p = strpbrk(line, ": \t");
1110 if (!p || *p != ':') {
1111 char return_path[LONG_STRING];
1114 /* some bogus MTAs will quote the original "From " line */
1115 if (!m_strncmp(">From ", line, 6))
1116 continue; /* just ignore */
1118 if (is_from(line, return_path, sizeof(return_path), &t)) {
1119 /* MH somtimes has the From_ line in the middle of the header! */
1120 if (hdr && !hdr->received)
1121 hdr->received = t - mutt_local_tz(t);
1126 break; /* end of header */
1129 if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf))) {
1130 if (!rx_list_match(NoSpamList, line)) {
1131 /* if spam tag already exists, figure out how to amend it */
1132 if (e->spam && *buf) {
1134 /* If SpamSep defined, append with separator */
1135 mutt_buffer_addstr(e->spam, SpamSep);
1136 mutt_buffer_addstr(e->spam, buf);
1138 /* else overwrite */
1139 mutt_buffer_reset(e->spam);
1140 mutt_buffer_addstr(e->spam, buf);
1143 else if (!e->spam && *buf) {
1144 /* spam tag is new, and match expr is non-empty; copy */
1145 e->spam = mutt_buffer_from(NULL, buf);
1147 else if (!e->spam) {
1148 /* match expr is empty; plug in null string if no existing tag */
1149 e->spam = mutt_buffer_from(NULL, "");
1157 continue; /* skip empty header fields */
1159 last = mutt_parse_rfc822_line(e, hdr, line, p, weed, 1, last);
1165 hdr->content->hdr_offset = hdr->offset;
1166 hdr->content->offset = ftello(f);
1167 rfc2047_decode_envelope(e);
1168 /* check for missing or invalid date */
1169 if (hdr->date_sent <= 0) {
1170 debug_print(1, ("no date found, using received "
1171 "time from msg separator\n"));
1172 hdr->date_sent = hdr->received;
1179 /* Compares mime types to the ok and except lists */
1180 static int count_body_parts_check(LIST **checklist, BODY *b)
1184 for (type = *checklist; type; type = type->next) {
1185 ATTACH_MATCH *a = (ATTACH_MATCH *)type->data;
1187 if ((a->major_int == TYPEANY || a->major_int == b->type)
1188 && !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1196 /* -------------------- XXX: MC READ MARK ------------- */
1198 #define AT_COUNT(why) { shallcount = 1; }
1199 #define AT_NOCOUNT(why) { shallcount = 0; }
1201 int count_body_parts (BODY *body, int flags) {
1203 int shallcount, shallrecurse;
1209 for (bp = body; bp != NULL; bp = bp->next) {
1210 /* Initial disposition is to count and not to recurse this part. */
1211 AT_COUNT("default");
1214 debug_print(5, ("bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1215 bp->description ? bp->description : ("none"),
1216 bp->filename ? bp->filename :
1217 bp->d_filename ? bp->d_filename : "(none)",
1218 bp->type, bp->subtype ? bp->subtype : "*"));
1220 if (bp->type == TYPEMESSAGE) {
1223 /* If it's an external body pointer, don't recurse it. */
1224 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1227 /* Don't count containers if they're top-level. */
1228 if (flags & M_PARTS_TOPLEVEL)
1229 AT_NOCOUNT("top-level message/*");
1230 } else if (bp->type == TYPEMULTIPART) {
1231 /* Always recurse multiparts, except multipart/alternative. */
1233 if (!m_strcasecmp(bp->subtype, "alternative"))
1236 /* Don't count containers if they're top-level. */
1237 if (flags & M_PARTS_TOPLEVEL)
1238 AT_NOCOUNT("top-level multipart");
1241 if (bp->disposition == DISPINLINE &&
1242 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1243 AT_NOCOUNT("ignore fundamental inlines");
1245 /* If this body isn't scheduled for enumeration already, don't bother
1246 * profiling it further. */
1249 /* Turn off shallcount if message type is not in ok list,
1250 * or if it is in except list. Check is done separately for
1251 * inlines vs. attachments.
1254 if (bp->disposition == DISPATTACH) {
1255 if (!count_body_parts_check(&AttachAllow, bp))
1256 AT_NOCOUNT("attach not allowed");
1257 if (count_body_parts_check(&AttachExclude, bp))
1258 AT_NOCOUNT("attach excluded");
1260 if (!count_body_parts_check(&InlineAllow, bp))
1261 AT_NOCOUNT("inline not allowed");
1262 if (count_body_parts_check(&InlineExclude, bp))
1263 AT_NOCOUNT("excluded");
1269 bp->attach_qualifies = shallcount ? 1 : 0;
1271 debug_print(5, ("cbp: %p shallcount = %d\n", bp, shallcount));
1274 debug_print(5, ("cbp: %p pre count = %d\n", bp, count));
1275 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1276 count += bp->attach_count;
1277 debug_print(5, ("cbp: %p post count = %d\n", bp, count));
1281 debug_print(5, ("bp: return %d\n", count < 0 ? 0 : count));
1282 return count < 0 ? 0 : count;
1285 int mutt_count_body_parts (HEADER *hdr, int flags) {
1286 if (!option (OPTCOUNTATTACH))
1288 if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1289 return hdr->attach_total;
1291 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1292 hdr->attach_total = count_body_parts(hdr->content, flags | M_PARTS_TOPLEVEL);
1294 hdr->attach_total = 0;
1296 hdr->attach_valid = 1;
1297 return hdr->attach_total;