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/debug.h>
42 #include <lib-lib/url.h>
44 #include "recvattach.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 string_list_t *mutt_parse_references(char *s, int in_reply_to)
105 string_list_t *lst = 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)
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(string_list_t, 1);
163 int mutt_check_encoding(const char *s)
165 int tok = mime_which_token(s, -1);
173 case MIME_QUOTED_PRINTABLE:
174 return ENCQUOTEDPRINTABLE;
177 case MIME_X_UUENCODE:
184 int mutt_check_mime_type(const char *s)
188 if (!m_strcmp(s, "*") || !m_strcmp(s, ".*"))
191 tok = mime_which_token(s, -1);
193 case MIME_TEXT: return TYPETEXT;
194 case MIME_MULTIPART: return TYPEMULTIPART;
195 case MIME_APPLICATION: return TYPEAPPLICATION;
196 case MIME_MESSAGE: return TYPEMESSAGE;
197 case MIME_IMAGE: return TYPEIMAGE;
198 case MIME_AUDIO: return TYPEAUDIO;
199 case MIME_VIDEO: return TYPEVIDEO;
200 case MIME_MODEL: return TYPEMODEL;
201 default: return TYPEOTHER;
205 static PARAMETER *parse_parameters(const char *s)
207 PARAMETER *res = NULL;
208 PARAMETER **list = &res;
216 if (*s == '=') /* parameters are fucked up, go away */
219 p = strpbrk(s, "=;");
224 /* if we hit a ; now the parameter has no value, just skip it */
230 new = parameter_new();
231 new->attribute = p_dupstr(s, i);
233 while (--i >= 0 && ISSPACE(new->attribute[i])) {
234 new->attribute[i] = '\0';
236 s = skipspaces(p + 1); /* skip over the = */
239 char buffer[LONG_STRING];
243 for (i = 0; *s && i < ssizeof(buffer) - 1; i++, s++) {
244 if (!option(OPTSTRICTMIME)) {
245 /* As iso-2022-* has a characer of '"' with non-ascii state,
247 if (*s == 0x1b && i < ssizeof(buffer) - 2) {
248 state_ascii = s[1] == '(' && (s[2] == 'B' || s[2] == 'J');
251 if (state_ascii && *s == '"')
261 new->value = p_dupstr(buffer, i);
263 for (p = s; *p && *p != ' ' && *p != ';'; p++);
264 new->value = p_dupstr(s, p - s);
270 s = strchr(s, ';'); /* Find the next parameter */
272 break; /* no more parameters */
275 rfc2231_decode_parameters(&res);
279 void mutt_parse_content_type(char *s, BODY *ct)
284 p_delete(&ct->subtype);
285 parameter_list_wipe(&ct->parameter);
287 /* First extract any existing parameters */
288 if ((pc = strchr(s, ';')) != NULL) {
290 ct->parameter = parse_parameters(vskipspaces(pc));
292 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
293 * but if a filename has already been set in the content-disposition,
294 * let that take precedence, and don't set it here */
295 pc = mutt_get_parameter("name", ct->parameter);
296 if (pc && !ct->filename)
297 ct->filename = m_strdup(pc);
300 /* Now get the subtype */
301 if ((subtype = strchr (s, '/'))) {
303 for (pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++);
304 ct->subtype = p_dupstr(subtype, pc - subtype);
307 /* Finally, get the major type */
308 ct->type = mutt_check_mime_type(s);
310 if (ct->type == TYPEOTHER) {
311 ct->xtype = m_strdup(s);
315 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
316 * field, so we can attempt to convert the type to BODY here.
319 char buffer[SHORT_STRING];
322 ct->subtype = m_strdup("plain");
326 ct->subtype = m_strdup("basic");
330 ct->subtype = m_strdup("rfc822");
334 ct->type = TYPEAPPLICATION;
335 snprintf(buffer, sizeof(buffer), "x-%s", s);
336 ct->subtype = m_strdup(buffer);
340 ct->subtype = m_strdup("x-unknown");
345 /* Default character set for text types. */
346 if (ct->type == TYPETEXT) {
347 pc = mutt_get_parameter("charset", ct->parameter);
349 mutt_set_parameter("charset",
350 option(OPTSTRICTMIME) ? "us-ascii" :
351 mutt_get_first_charset(AssumedCharset),
357 static void parse_content_disposition(char *s, BODY *ct)
359 if (!ascii_strncasecmp(s, "inline", 6)) {
360 ct->disposition = DISPINLINE;
361 } else if (!ascii_strncasecmp(s, "form-data", 9)) {
362 ct->disposition = DISPFORMDATA;
364 ct->disposition = DISPATTACH;
367 /* Check to see if a default filename was given */
368 if ((s = strchr (s, ';'))) {
369 PARAMETER *parms = parse_parameters(vskipspaces(s));
371 if ((s = mutt_get_parameter("filename", parms)))
372 m_strreplace(&ct->filename, s);
373 if ((s = mutt_get_parameter ("name", parms)))
374 ct->form_name = m_strdup(s);
376 parameter_list_wipe(&parms);
381 * fp stream to read from
383 * digest 1 if reading subparts of a multipart/digest, 0
386 BODY *mutt_read_mime_header(FILE *fp, int digest)
388 BODY *body = mutt_new_body ();
389 char *line = p_new(char, LONG_STRING);
390 ssize_t linelen = LONG_STRING;
393 body->hdr_offset = ftello(fp);
394 body->encoding = ENC7BIT; /* default from RFC1521 */
395 body->disposition = DISPINLINE;
396 body->type = digest ? TYPEMESSAGE : TYPETEXT;
398 while (mutt_read_rfc822_line(fp, &line, &linelen)) {
399 /* Find the value of the current header */
400 if ((p = strchr(line, ':'))) {
406 debug_print (1, ("bogus MIME header: %s\n", line));
410 switch (mime_which_token(line, -1)) {
411 case MIME_CONTENT_TYPE:
412 mutt_parse_content_type (p, body);
415 case MIME_CONTENT_TRANSFER_ENCODING:
416 body->encoding = mutt_check_encoding (p);
419 case MIME_CONTENT_DISPOSITION:
420 parse_content_disposition(p, body);
423 case MIME_CONTENT_DESCRIPTION:
424 m_strreplace(&body->description, p);
425 rfc2047_decode(&body->description);
432 body->offset = ftello(fp); /* Mark the start of the real data */
433 if (!body->subtype) {
434 if (body->type == TYPETEXT)
435 body->subtype = m_strdup("plain");
436 if (body->type == TYPEMESSAGE)
437 body->subtype = m_strdup("rfc822");
444 void mutt_parse_part(FILE *fp, BODY *b)
450 bound = mutt_get_parameter("boundary", b->parameter);
451 fseeko(fp, b->offset, SEEK_SET);
452 b->parts = mutt_parse_multipart(fp, bound, b->offset + b->length,
453 mime_which_token(b->subtype, -1) == MIME_DIGEST);
458 fseeko(fp, b->offset, SEEK_SET);
460 if (mutt_is_message_type(b->type, b->subtype)) {
461 b->parts = mutt_parse_messageRFC822(fp, b);
463 if (mime_which_token(b->subtype, -1) == MIME_EXTERNAL_BODY) {
464 b->parts = mutt_read_mime_header(fp, 0);
475 /* try to recover from parsing error */
478 m_strreplace(&b->subtype, "plain");
482 /* parse a MESSAGE/RFC822 body
485 * fp stream to read from
487 * parent structure which contains info about the message/rfc822
490 * NOTE: this assumes that `parent->length' has been set!
492 BODY *mutt_parse_messageRFC822(FILE * fp, BODY * parent)
496 parent->hdr = header_new();
497 parent->hdr->offset = ftello(fp);
498 parent->hdr->env = mutt_read_rfc822_header(fp, parent->hdr, 0, 0);
500 msg = parent->hdr->content;
502 /* ignore the length given in the content-length since it could be wrong
503 and we already have the info to calculate the correct length */
504 /* if (msg->length == -1) */
505 /* if body of this message is empty, we can end up with a negative length */
506 msg->length = MAX(0, parent->length - (msg->offset - parent->offset));
508 mutt_parse_part(fp, msg);
513 /* parse a multipart structure
516 * fp stream to read from
518 * bound body separator
520 * end_off length of the multipart body (used when the final
521 * boundary is missing to avoid reading too far)
523 * digest 1 if reading a multipart/digest, 0 otherwise
527 mutt_parse_multipart(FILE *fp, const char *bound, off_t end_off, int digest)
529 char buffer[LONG_STRING];
532 int blen = m_strlen(bound);
533 int final = 0; /* did we see the ending boundary? */
536 mutt_error _("multipart message has no boundary parameter!");
540 while (ftello(fp) < end_off && fgets(buffer, sizeof(buffer), fp)) {
543 len = m_strlen(buffer);
544 crlf = len > 1 && buffer[len - 2] == '\r';
546 if (buffer[0] == '-' && buffer[1] == '-'
547 && !m_strncmp(buffer + 2, bound, blen))
552 /* if the body is empty, we can end up with a -1 length */
553 b->length = MAX(0, ftello(fp) - b->offset - len - 1 - crlf);
554 if (b->parts && b->parts->length == 0) {
555 b->parts->length = ftello(fp) - b->parts->offset
560 /* Remove any trailing whitespace, up to the length of the boundary */
561 for (i = len - 1; ISSPACE(buffer[i]) && i >= blen + 2; i--)
564 /* Check for the end boundary */
565 final = buffer[blen + 3] == '-' && buffer[blen + 4] == '-';
569 if (buffer[2 + blen] == '\0') {
570 BODY *new = mutt_read_mime_header(fp, digest);
573 * Consistency checking - catch
574 * bad attachment end boundaries
577 if (new->offset > end_off) {
578 mutt_free_body(&new);
583 last = &(*last)->next;
589 /* in case of missing end boundary, set the length to something reasonable */
590 if (*last && (*last)->length == 0 && !final)
591 (*last)->length = end_off - (*last)->offset;
593 /* parse recursive MIME parts */
596 for (b = head; b; b = b->next)
597 mutt_parse_part(fp, b);
604 uncomment_timezone(char *buf, size_t buflen, const char *tz)
609 return tz; /* no need to do anything */
611 tz = vskipspaces(tz + 1);
612 p = strpbrk(tz, " )");
616 m_strncpy(buf, buflen, tz, p - tz);
622 unsigned char zhours;
623 unsigned char zminutes;
624 unsigned char zoccident; /* west of UTC? */
626 {"aat", 1, 0, 1}, /* Atlantic Africa Time */
627 {"adt", 4, 0, 0}, /* Arabia DST */
628 {"ast", 3, 0, 0}, /* Arabia */
629 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
630 {"bst", 1, 0, 0}, /* British DST */
631 {"cat", 1, 0, 0}, /* Central Africa */
633 {"cest", 2, 0, 0}, /* Central Europe DST */
634 {"cet", 1, 0, 0}, /* Central Europe */
636 /*{ "cst", 8, 0, 0 }, *//* China */
637 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
638 {"eat", 3, 0, 0}, /* East Africa */
640 {"eest", 3, 0, 0}, /* Eastern Europe DST */
641 {"eet", 2, 0, 0}, /* Eastern Europe */
642 {"egst", 0, 0, 0}, /* Eastern Greenland DST */
643 {"egt", 1, 0, 1}, /* Eastern Greenland */
646 {"gst", 4, 0, 0}, /* Presian Gulf */
647 {"hkt", 8, 0, 0}, /* Hong Kong */
648 {"ict", 7, 0, 0}, /* Indochina */
649 {"idt", 3, 0, 0}, /* Israel DST */
650 {"ist", 2, 0, 0}, /* Israel */
651 /*{ "ist", 5, 30, 0 }, *//* India */
652 {"jst", 9, 0, 0}, /* Japan */
653 {"kst", 9, 0, 0}, /* Korea */
655 {"met", 1, 0, 0}, /* this is now officially CET */
656 {"msd", 4, 0, 0}, /* Moscow DST */
657 {"msk", 3, 0, 0}, /* Moscow */
659 {"nzdt", 13, 0, 0}, /* New Zealand DST */
660 {"nzst", 12, 0, 0}, /* New Zealand */
663 {"sat", 2, 0, 0}, /* South Africa */
664 {"smt", 4, 0, 0}, /* Seychelles */
665 {"sst", 11, 0, 1}, /* Samoa */
666 /*{ "sst", 8, 0, 0 }, *//* Singapore */
668 {"wat", 0, 0, 0}, /* West Africa */
669 {"west", 1, 0, 0}, /* Western Europe DST */
670 {"wet", 0, 0, 0}, /* Western Europe */
671 {"wgst", 2, 0, 1}, /* Western Greenland DST */
672 {"wgt", 3, 0, 1}, /* Western Greenland */
673 {"wst", 8, 0, 0}, /* Western Australia */
676 /* parses a date string in RFC822 format:
678 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
680 * This routine assumes that `h' has been initialized to 0. the `timezone'
681 * field is optional, defaulting to +0000 if missing.
683 time_t mutt_parse_date(const char *s, HEADER *h)
685 int zhours = 0, zminutes = 0, zoccident = 0;
686 char scratch[SHORT_STRING];
691 /* Don't modify our argument. Fixed-size buffer is ok here since
692 the date format imposes a natural limit. */
694 m_strcpy(scratch, sizeof(scratch), s);
696 /* kill the day of the week, if it exists. */
697 p = strchr(scratch, ',');
698 p = vskipspaces(p ? p + 1 : scratch);
702 while ((p = strtok (p, " \t")) != NULL) {
703 char tzstr[SHORT_STRING];
707 case 0: /* day of the month */
708 if (!isdigit((unsigned char)*p))
710 tm.tm_mday = atoi(p);
715 case 1: /* month of the year */
716 tm.tm_mon = mutt_check_month(p);
722 tm.tm_year = atoi(p);
725 else if (tm.tm_year >= 1900)
729 case 3: /* time of day */
730 tm.tm_hour = strtol(p, &p, 10);
733 tm.tm_min = strtol(p, &p, 10);
735 tm.tm_sec = strtol(p, &p, 10);
741 case 4: /* timezone */
742 /* sometimes we see things like (MST) or (-0700) so attempt to
743 * compensate by uncommenting the string if non-RFC822 compliant
745 ptz = uncomment_timezone(tzstr, sizeof(tzstr), p);
747 if (*ptz == '+' || *ptz == '-') {
748 if (isdigit((unsigned char)ptz[1])
749 && isdigit((unsigned char)ptz[2])
750 && isdigit((unsigned char)ptz[3])
751 && isdigit((unsigned char)ptz[4]))
753 zoccident = ptz[0] == '-';
754 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
755 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
760 /* This is safe to do: A pointer to a struct equals a pointer to its
762 tz = bsearch(ptz, TimeZones, countof(TimeZones), sizeof(TimeZones[0]),
763 (int (*)(const void *, const void *))ascii_strcasecmp);
767 zminutes = tz->zminutes;
768 zoccident = tz->zoccident;
771 /* ad hoc support for the European MET (now officially CET) TZ */
772 if (ascii_strcasecmp(p, "MET") == 0) {
773 if ((p = strtok (NULL, " \t")) && !ascii_strcasecmp(p, "DST")) {
784 if (count < 4) { /* don't check for missing timezone */
785 debug_print (1, ("error parsing date format, using received time\n"));
791 h->zminutes = zminutes;
792 h->zoccident = zoccident;
795 return mutt_mktime(&tm, 0) + (zoccident ? 1 : -1) * (zhours * 3600 + zminutes * 60);
798 string_list_t **mutt_parse_rfc822_line(ENVELOPE *e, HEADER *hdr, char *line, char *p,
799 short weed, short do_2047, string_list_t **user_hdrs)
801 switch (mime_which_token(line, -1)) {
802 case MIME_APPARENTLY_FROM:
803 e->from = rfc822_parse_adrlist (e->from, p);
806 case MIME_APPARENTLY_TO:
807 e->to = rfc822_parse_adrlist (e->to, p);
811 e->bcc = rfc822_parse_adrlist (e->bcc, p);
815 e->cc = rfc822_parse_adrlist (e->cc, p);
818 case MIME_CONTENT_DESCRIPTION:
820 m_strreplace(&hdr->content->description, p);
821 rfc2047_decode(&hdr->content->description);
825 case MIME_CONTENT_DISPOSITION:
827 parse_content_disposition(p, hdr->content);
830 case MIME_CONTENT_LENGTH:
832 if ((hdr->content->length = atoi(p)) < 0)
833 hdr->content->length = -1;
837 case MIME_CONTENT_TRANSFER_ENCODING:
839 hdr->content->encoding = mutt_check_encoding(p);
842 case MIME_CONTENT_TYPE:
844 mutt_parse_content_type (p, hdr->content);
848 m_strreplace(&e->date, p);
850 hdr->date_sent = mutt_parse_date (p, hdr);
854 if (hdr && mutt_parse_date (p, NULL) < time (NULL))
859 case MIME_FOLLOWUP_TO:
860 if (!e->followup_to) {
862 e->followup_to = m_strdup(skipspaces(p));
868 e->from = rfc822_parse_adrlist(e->from, p);
869 /* don't leave from info NULL if there's an invalid address (or
870 * whatever) in From: field; mutt would just display it as empty
871 * and mark mail/(esp.) news article as your own. aaargh! this
872 * bothered me for _years_ */
874 e->from = address_new();
875 e->from->personal = m_strdup(p);
879 case MIME_IN_REPLY_TO:
880 string_list_wipe(&e->in_reply_to);
881 e->in_reply_to = mutt_parse_references(p, 1);
886 /* HACK - mutt has, for a very short time, produced negative
887 Lines header values. Ignore them. */
888 hdr->lines = MAX(0, atoi(p));
893 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
894 if (strncmp(p, "NO", 2)) {
897 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
899 if (!(end = strchr (beg, '>')))
902 /* Take the first mailto URL */
903 if (url_check_scheme (beg) == U_MAILTO) {
904 p_delete(&e->list_post);
905 e->list_post = p_dupstr(beg, end - beg);
912 case MIME_MAIL_FOLLOWUP_TO:
913 e->mail_followup_to = rfc822_parse_adrlist(e->mail_followup_to, p);
916 case MIME_MAIL_REPLY_TO:
917 address_list_wipe(&e->reply_to);
918 e->reply_to = rfc822_parse_adrlist(e->reply_to, p);
921 case MIME_MESSAGE_ID:
923 const char *beg, *end;
925 /* We add a new "Message-ID:" when building a message */
926 p_delete(&e->message_id);
928 if ((beg = strchr(p, '<')) && (end = strchr(beg, '>')))
929 e->message_id = p_dupstr(beg, (end - beg) + 1);
933 case MIME_MIME_VERSION:
939 case MIME_NEWSGROUPS:
940 p_delete(&e->newsgroups);
942 e->newsgroups = m_strdup(skipspaces(p));
946 case MIME_ORGANIZATION:
947 if (!e->organization && mime_which_token(p, -1) == MIME_UNKNOWN)
948 e->organization = m_strdup(p);
952 if (hdr && !hdr->received) {
953 char *d = strchr(p, ';');
955 hdr->received = mutt_parse_date(d + 1, NULL);
959 case MIME_REFERENCES:
960 string_list_wipe(&e->references);
961 e->references = mutt_parse_references(p, 0);
965 e->reply_to = rfc822_parse_adrlist(e->reply_to, p);
968 case MIME_RETURN_PATH:
969 e->return_path = rfc822_parse_adrlist(e->return_path, p);
973 e->sender = rfc822_parse_adrlist (e->sender, p);
997 e->subject = m_strdup(p);
1000 case MIME_SUPERCEDES:
1001 case MIME_SUPERSEDES:
1003 e->supersedes = m_strdup(p);
1007 e->to = rfc822_parse_adrlist(e->to, p);
1011 case MIME_X_COMMENT_TO:
1012 if (!e->x_comment_to)
1013 e->x_comment_to = m_strdup(p);
1018 e->x_label = m_strdup(p);
1024 e->xref = m_strdup(p);
1053 /* restore the original line */
1054 line[m_strlen(line)] = ':';
1056 if (weed && option(OPTWEED) && mutt_matches_ignore(line, Ignore)
1057 && !mutt_matches_ignore(line, UnIgnore)) {
1061 *user_hdrs = string_item_new();
1062 (*user_hdrs)->data = m_strdup(line);
1064 rfc2047_decode(&(*user_hdrs)->data);
1065 return &(*user_hdrs)->next;
1071 /* mutt_read_rfc822_header() -- parses a RFC822 header
1075 * f stream to read from
1077 * hdr header structure of current message (optional).
1079 * user_hdrs If set, store user headers. Used for recall-message and
1082 * weed If this parameter is set and the user has activated the
1083 * $weed option, honor the header weed list for user headers.
1084 * Used for recall-message.
1086 * Returns: newly allocated envelope structure. You should free it by
1087 * envelope_delete() when envelope stay unneeded.
1090 mutt_read_rfc822_header(FILE *f, HEADER *hdr, short user_hdrs, short weed)
1092 ENVELOPE *e = envelope_new();
1093 string_list_t **last = user_hdrs ? &e->userhdrs : NULL;
1095 char *line = p_new(char, LONG_STRING);
1096 ssize_t linelen = LONG_STRING;
1099 if (hdr && !hdr->content) {
1100 hdr->content = mutt_new_body ();
1102 /* set the defaults from RFC1521 */
1103 hdr->content->type = TYPETEXT;
1104 hdr->content->subtype = m_strdup("plain");
1105 hdr->content->encoding = ENC7BIT;
1106 hdr->content->length = -1;
1108 /* RFC 2183 says this is arbitrary */
1109 hdr->content->disposition = DISPINLINE;
1112 while ((loc = ftello(f)),
1113 mutt_read_rfc822_line(f, &line, &linelen))
1115 char buf[LONG_STRING + 1] = "";
1118 p = strpbrk(line, ": \t");
1119 if (!p || *p != ':') {
1120 char return_path[LONG_STRING];
1123 /* some bogus MTAs will quote the original "From " line */
1124 if (!m_strncmp(">From ", line, 6))
1125 continue; /* just ignore */
1127 if (is_from(line, return_path, sizeof(return_path), &t)) {
1128 /* MH somtimes has the From_ line in the middle of the header! */
1129 if (hdr && !hdr->received)
1130 hdr->received = t - mutt_local_tz(t);
1135 break; /* end of header */
1138 if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf))) {
1139 if (!rx_list_match(NoSpamList, line)) {
1140 /* if spam tag already exists, figure out how to amend it */
1141 if (e->spam && *buf) {
1143 /* If SpamSep defined, append with separator */
1144 mutt_buffer_addstr(e->spam, SpamSep);
1145 mutt_buffer_addstr(e->spam, buf);
1147 /* else overwrite */
1148 mutt_buffer_reset(e->spam);
1149 mutt_buffer_addstr(e->spam, buf);
1152 else if (!e->spam && *buf) {
1153 /* spam tag is new, and match expr is non-empty; copy */
1154 e->spam = mutt_buffer_from(NULL, buf);
1156 else if (!e->spam) {
1157 /* match expr is empty; plug in null string if no existing tag */
1158 e->spam = mutt_buffer_from(NULL, "");
1166 continue; /* skip empty header fields */
1168 last = mutt_parse_rfc822_line(e, hdr, line, p, weed, 1, last);
1174 hdr->content->hdr_offset = hdr->offset;
1175 hdr->content->offset = ftello(f);
1176 rfc2047_decode_envelope(e);
1177 /* check for missing or invalid date */
1178 if (hdr->date_sent <= 0) {
1179 debug_print(1, ("no date found, using received "
1180 "time from msg separator\n"));
1181 hdr->date_sent = hdr->received;
1188 /* Compares mime types to the ok and except lists */
1189 static int count_body_parts_check(string_list_t **checklist, BODY *b)
1191 string_list_t *type;
1193 for (type = *checklist; type; type = type->next) {
1194 ATTACH_MATCH *a = (ATTACH_MATCH *)type->data;
1196 if ((a->major_int == TYPEANY || a->major_int == b->type)
1197 && !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1205 static int count_body_parts (BODY *body, int flags)
1213 for (bp = body; bp != NULL; bp = bp->next) {
1214 /* Initial disposition is to count and not to recurse this part. */
1215 int shallcount, shallrecurse, iscontainer;
1216 int tok = mime_which_token(bp->subtype, -1);
1218 iscontainer = bp->type == TYPEMESSAGE || bp->type == TYPEMULTIPART;
1220 /* don't recurse in external bodies or multipart/alternatives */
1221 shallrecurse = (bp->type == TYPEMESSAGE && tok != MIME_EXTERNAL_BODY)
1222 || (bp->type == TYPEMULTIPART && tok != MIME_ALTERNATIVE);
1224 /* Don't count top level containers and fundamental inlines */
1225 shallcount = !(iscontainer && (flags & M_PARTS_TOPLEVEL))
1226 && !(!iscontainer && bp->disposition == DISPINLINE && bp == body);
1229 /* Turn off shallcount if message type is not in ok list,
1230 * or if it is in except list. Check is done separately for
1231 * inlines vs. attachments.
1234 if (bp->disposition == DISPATTACH) {
1235 if (!count_body_parts_check(&AttachAllow, bp))
1237 if (count_body_parts_check(&AttachExclude, bp))
1240 if (!count_body_parts_check(&InlineAllow, bp))
1242 if (count_body_parts_check(&InlineExclude, bp))
1247 bp->attach_qualifies = shallcount;
1248 count += shallcount;
1251 bp->attach_count = count_body_parts(bp->parts,
1252 flags & ~M_PARTS_TOPLEVEL);
1253 count += bp->attach_count;
1260 int mutt_count_body_parts(HEADER *hdr, int flags)
1262 if (!option(OPTCOUNTATTACH))
1265 if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1266 return hdr->attach_total;
1268 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1269 hdr->attach_total = count_body_parts(hdr->content,
1270 flags | M_PARTS_TOPLEVEL);
1272 hdr->attach_total = 0;
1274 hdr->attach_valid = 1;
1275 return hdr->attach_total;