2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/ascii.h>
17 #include <lib-lib/macros.h>
18 #include <lib-lib/buffer.h>
20 #include <lib-mime/mime.h>
24 #include "recvattach.h"
26 #include "mutt_crypt.h"
30 #include "lib/debug.h"
37 /* Reads an arbitrarily long header field, and looks ahead for continuation
38 * lines. ``line'' must point to a dynamically allocated string; it is
39 * increased if more space is required to fit the whole line.
41 char *mutt_read_rfc822_line (FILE * f, char *line, size_t * linelen)
48 if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */
49 (ISSPACE (*line) && !offset)) { /* end of headers */
54 buf += m_strlen(buf) - 1;
56 /* we did get a full line. remove trailing space */
57 while (ISSPACE (*buf))
58 *buf-- = 0; /* we cannot come beyond line's beginning because
59 * it begins with a non-space */
61 /* check to see if the next line is a continuation line */
62 if ((ch = fgetc (f)) != ' ' && ch != '\t') {
64 return (line); /* next line is a separate header field or EOH */
67 /* eat tabs and spaces from the beginning of the continuation line */
68 while ((ch = fgetc (f)) == ' ' || ch == '\t');
70 *++buf = ' '; /* string is still terminated because we removed
71 at least one whitespace char above */
76 if (*linelen < offset + STRING) {
79 p_realloc(&line, *linelen);
86 LIST *mutt_parse_references (char *s, int in_reply_to)
90 char *o = NULL, *new, *at;
92 while ((s = strtok (s, " \t;")) != NULL) {
94 * some mail clients add other garbage besides message-ids, so do a quick
95 * check to make sure this looks like a valid message-id
96 * some idiotic clients also break their message-ids between lines, deal
97 * with that too (give up if it's more than two lines, though)
104 if (s[n - 1] != '>') {
114 if (s[m - 1] == '>') {
115 new = p_new(char, n + m + 1);
116 strcpy (new, o); /* __STRCPY_CHECKED__ */
117 strcpy (new + n, s); /* __STRCPY_CHECKED__ */
121 /* make sure that this really does look like a message-id.
122 * it should have exactly one @, and if we're looking at
123 * an in-reply-to header, make sure that the part before
124 * the @ has more than eight characters or it's probably
127 if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
128 || (in_reply_to && at - new <= 8))
144 int mutt_check_encoding (const char *c)
146 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit") - 1) == 0)
148 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit") - 1) == 0)
150 else if (ascii_strncasecmp ("binary", c, sizeof ("binary") - 1) == 0)
153 if (ascii_strncasecmp
154 ("quoted-printable", c, sizeof ("quoted-printable") - 1) == 0)
155 return (ENCQUOTEDPRINTABLE);
156 else if (ascii_strncasecmp ("base64", c, sizeof ("base64") - 1) == 0)
158 else if (ascii_strncasecmp ("x-uuencode", c, sizeof ("x-uuencode") - 1) == 0)
159 return (ENCUUENCODED);
164 static PARAMETER *parse_parameters (const char *s)
166 PARAMETER *head = 0, *cur = 0, *new;
167 char buffer[LONG_STRING];
171 debug_print (2, ("`%s'\n", s));
174 if ((p = strpbrk (s, "=;")) == NULL) {
175 debug_print (1, ("malformed parameter: %s\n", s));
179 /* if we hit a ; now the parameter has no value, just skip it */
183 new = mutt_new_parameter ();
185 new->attribute = p_dupstr(s, i);
187 /* remove whitespace from the end of the attribute name */
188 while (ISSPACE (new->attribute[--i]))
189 new->attribute[i] = 0;
191 s = vskipspaces(p + 1); /* skip over the = */
197 for (i = 0; *s && i < sizeof (buffer) - 1; i++, s++) {
198 if (!option (OPTSTRICTMIME)) {
199 /* As iso-2022-* has a characer of '"' with non-ascii state,
201 if (*s == 0x1b && i < sizeof (buffer) - 2) {
202 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
208 if (state_ascii && *s == '"')
211 /* Quote the next character */
221 s++; /* skip over the " */
224 for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1;
230 new->value = m_strdup(buffer);
232 debug_print (2, ("`%s' = `%s'\n", new->attribute ? new->attribute : "",
233 new->value ? new->value : ""));
235 /* Add this parameter to the list */
244 debug_print (1, ("parameter with no value: %s\n", s));
248 /* Find the next parameter */
249 if (*s != ';' && (s = strchr (s, ';')) == NULL)
250 break; /* no more parameters */
253 /* Move past any leading whitespace */
254 s = vskipspaces(s + 1);
256 while (*s == ';'); /* skip empty parameters */
261 rfc2231_decode_parameters (&head);
265 int mutt_check_mime_type (const char *s)
267 if (ascii_strcasecmp ("text", s) == 0)
269 else if (ascii_strcasecmp ("multipart", s) == 0)
270 return TYPEMULTIPART;
271 else if (ascii_strcasecmp ("application", s) == 0)
272 return TYPEAPPLICATION;
273 else if (ascii_strcasecmp ("message", s) == 0)
275 else if (ascii_strcasecmp ("image", s) == 0)
277 else if (ascii_strcasecmp ("audio", s) == 0)
279 else if (ascii_strcasecmp ("video", s) == 0)
281 else if (ascii_strcasecmp ("model", s) == 0)
283 else if (ascii_strcasecmp ("*", s) == 0)
285 else if (ascii_strcasecmp (".*", s) == 0)
291 void mutt_parse_content_type (char *s, BODY * ct)
296 p_delete(&ct->subtype);
297 mutt_free_parameter (&ct->parameter);
299 /* First extract any existing parameters */
300 if ((pc = strchr (s, ';')) != NULL) {
302 while (*pc && ISSPACE (*pc))
304 ct->parameter = parse_parameters (pc);
306 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
307 * but if a filename has already been set in the content-disposition,
308 * let that take precedence, and don't set it here */
309 if ((pc = mutt_get_parameter ("name", ct->parameter)) != 0
311 ct->filename = m_strdup(pc);
314 /* Now get the subtype */
315 if ((subtype = strchr (s, '/'))) {
317 for (pc = subtype; *pc && !ISSPACE (*pc) && *pc != ';'; pc++);
319 ct->subtype = m_strdup(subtype);
322 /* Finally, get the major type */
323 ct->type = mutt_check_mime_type (s);
325 if (ct->type == TYPEOTHER) {
326 ct->xtype = m_strdup(s);
329 if (ct->subtype == NULL) {
330 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
331 * field, so we can attempt to convert the type to BODY here.
333 if (ct->type == TYPETEXT)
334 ct->subtype = m_strdup("plain");
335 else if (ct->type == TYPEAUDIO)
336 ct->subtype = m_strdup("basic");
337 else if (ct->type == TYPEMESSAGE)
338 ct->subtype = m_strdup("rfc822");
339 else if (ct->type == TYPEOTHER) {
340 char buffer[SHORT_STRING];
342 ct->type = TYPEAPPLICATION;
343 snprintf (buffer, sizeof (buffer), "x-%s", s);
344 ct->subtype = m_strdup(buffer);
347 ct->subtype = m_strdup("x-unknown");
350 /* Default character set for text types. */
351 if (ct->type == TYPETEXT) {
352 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
353 mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
355 mutt_get_first_charset (AssumedCharset),
361 static void parse_content_disposition (char *s, BODY * ct)
365 if (!ascii_strncasecmp ("inline", s, 6))
366 ct->disposition = DISPINLINE;
367 else if (!ascii_strncasecmp ("form-data", s, 9))
368 ct->disposition = DISPFORMDATA;
370 ct->disposition = DISPATTACH;
372 /* Check to see if a default filename was given */
373 if ((s = strchr (s, ';')) != NULL) {
374 s = vskipspaces(s + 1);
375 if ((s = mutt_get_parameter("filename",
376 (parms = parse_parameters (s)))) != 0)
377 m_strreplace(&ct->filename, s);
378 if ((s = mutt_get_parameter ("name", parms)) != 0)
379 ct->form_name = m_strdup(s);
380 mutt_free_parameter (&parms);
385 * fp stream to read from
387 * digest 1 if reading subparts of a multipart/digest, 0
391 BODY *mutt_read_mime_header (FILE * fp, int digest)
393 BODY *p = mutt_new_body ();
395 char *line = p_new(char, LONG_STRING);
396 size_t linelen = LONG_STRING;
398 p->hdr_offset = ftello (fp);
400 p->encoding = ENC7BIT; /* default from RFC1521 */
401 p->type = digest ? TYPEMESSAGE : TYPETEXT;
402 p->disposition = DISPINLINE;
404 while (*(line = mutt_read_rfc822_line (fp, line, &linelen)) != 0) {
405 /* Find the value of the current header */
406 if ((c = strchr (line, ':'))) {
410 debug_print (1, ("skipping empty header field: %s\n", line));
415 debug_print (1, ("bogus MIME header: %s\n", line));
419 if (!ascii_strncasecmp ("content-", line, 8)) {
420 if (!ascii_strcasecmp ("type", line + 8))
421 mutt_parse_content_type (c, p);
422 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
423 p->encoding = mutt_check_encoding (c);
424 else if (!ascii_strcasecmp ("disposition", line + 8))
425 parse_content_disposition (c, p);
426 else if (!ascii_strcasecmp ("description", line + 8)) {
427 m_strreplace(&p->description, c);
428 rfc2047_decode (&p->description);
432 p->offset = ftello (fp); /* Mark the start of the real data */
433 if (p->type == TYPETEXT && !p->subtype)
434 p->subtype = m_strdup("plain");
435 else if (p->type == TYPEMESSAGE && !p->subtype)
436 p->subtype = m_strdup("rfc822");
443 void mutt_parse_part (FILE * fp, BODY * b)
449 bound = mutt_get_parameter ("boundary", b->parameter);
450 fseeko (fp, b->offset, SEEK_SET);
451 b->parts = mutt_parse_multipart (fp, bound,
452 b->offset + b->length,
453 ascii_strcasecmp ("digest",
459 fseeko (fp, b->offset, SEEK_SET);
460 if (mutt_is_message_type (b->type, b->subtype))
461 b->parts = mutt_parse_messageRFC822 (fp, b);
462 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
463 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!
491 BODY *mutt_parse_messageRFC822 (FILE * fp, BODY * parent)
495 parent->hdr = mutt_new_header ();
496 parent->hdr->offset = ftello (fp);
497 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 msg->length = parent->length - (msg->offset - parent->offset);
505 /* if body of this message is empty, we can end up with a negative length */
509 mutt_parse_part (fp, msg);
513 /* parse a multipart structure
516 * fp stream to read from
518 * boundary 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
526 BODY *mutt_parse_multipart (FILE * fp, const char *boundary, off_t end_off,
529 int blen, len, crlf = 0;
530 char buffer[LONG_STRING];
531 BODY *head = 0, *last = 0, *new = 0;
533 int final = 0; /* did we see the ending boundary? */
536 mutt_error _("multipart message has no boundary parameter!");
541 blen = m_strlen(boundary);
542 while (ftello (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL) {
543 len = m_strlen(buffer);
545 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
547 if (buffer[0] == '-' && buffer[1] == '-' &&
548 m_strncmp(buffer + 2, boundary, blen) == 0) {
550 last->length = ftello (fp) - last->offset - len - 1 - crlf;
551 if (last->parts && last->parts->length == 0)
552 last->parts->length =
553 ftello (fp) - last->parts->offset - len - 1 - crlf;
554 /* if the body is empty, we can end up with a -1 length */
555 if (last->length < 0)
559 /* Remove any trailing whitespace, up to the length of the boundary */
560 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
563 /* Check for the end boundary */
564 if (m_strcmp(buffer + blen + 2, "--") == 0) {
566 break; /* done parsing */
568 else if (buffer[2 + blen] == 0) {
569 new = mutt_read_mime_header (fp, digest);
572 * Consistency checking - catch
573 * bad attachment end boundaries
576 if (new->offset > end_off) {
577 mutt_free_body (&new);
590 /* in case of missing end boundary, set the length to something reasonable */
591 if (last && last->length == 0 && !final)
592 last->length = end_off - last->offset;
594 /* parse recursive MIME parts */
595 for (last = head; last; last = last->next)
596 mutt_parse_part (fp, last);
601 static const char *uncomment_timezone (char *buf, size_t buflen,
608 return tz; /* no need to do anything */
609 tz = vskipspaces(tz + 1);
610 if ((p = strpbrk (tz, " )")) == NULL)
613 if (len > buflen - 1)
615 memcpy (buf, tz, len);
622 unsigned char zhours;
623 unsigned char zminutes;
624 unsigned char zoccident; /* west of UTC? */
627 "aat", 1, 0, 1}, /* Atlantic Africa Time */
629 "adt", 4, 0, 0}, /* Arabia DST */
631 "ast", 3, 0, 0}, /* Arabia */
632 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
634 "bst", 1, 0, 0}, /* British DST */
636 "cat", 1, 0, 0}, /* Central Africa */
639 "cest", 2, 0, 0}, /* Central Europe DST */
641 "cet", 1, 0, 0}, /* Central Europe */
644 /*{ "cst", 8, 0, 0 }, *//* China */
645 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
647 "eat", 3, 0, 0}, /* East Africa */
650 "eest", 3, 0, 0}, /* Eastern Europe DST */
652 "eet", 2, 0, 0}, /* Eastern Europe */
654 "egst", 0, 0, 0}, /* Eastern Greenland DST */
656 "egt", 1, 0, 1}, /* Eastern Greenland */
660 "gst", 4, 0, 0}, /* Presian Gulf */
662 "hkt", 8, 0, 0}, /* Hong Kong */
664 "ict", 7, 0, 0}, /* Indochina */
666 "idt", 3, 0, 0}, /* Israel DST */
668 "ist", 2, 0, 0}, /* Israel */
669 /*{ "ist", 5, 30, 0 }, *//* India */
671 "jst", 9, 0, 0}, /* Japan */
673 "kst", 9, 0, 0}, /* Korea */
676 "met", 1, 0, 0}, /* this is now officially CET */
678 "msd", 4, 0, 0}, /* Moscow DST */
680 "msk", 3, 0, 0}, /* Moscow */
683 "nzdt", 13, 0, 0}, /* New Zealand DST */
685 "nzst", 12, 0, 0}, /* New Zealand */
689 "sat", 2, 0, 0}, /* South Africa */
691 "smt", 4, 0, 0}, /* Seychelles */
693 "sst", 11, 0, 1}, /* Samoa */
694 /*{ "sst", 8, 0, 0 }, *//* Singapore */
697 "wat", 0, 0, 0}, /* West Africa */
699 "west", 1, 0, 0}, /* Western Europe DST */
701 "wet", 0, 0, 0}, /* Western Europe */
703 "wgst", 2, 0, 1}, /* Western Greenland DST */
705 "wgt", 3, 0, 1}, /* Western Greenland */
707 "wst", 8, 0, 0}, /* Western Australia */
710 /* parses a date string in RFC822 format:
712 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
714 * This routine assumes that `h' has been initialized to 0. the `timezone'
715 * field is optional, defaulting to +0000 if missing.
717 time_t mutt_parse_date (const char *s, HEADER * h)
729 char tzstr[SHORT_STRING];
730 char scratch[SHORT_STRING];
732 /* Don't modify our argument. Fixed-size buffer is ok here since
733 * the date format imposes a natural limit.
736 m_strcpy(scratch, sizeof(scratch), s);
738 /* kill the day of the week, if it exists. */
739 if ((t = strchr (scratch, ',')))
747 while ((t = strtok (t, " \t")) != NULL) {
749 case 0: /* day of the month */
750 if (!isdigit ((unsigned char) *t))
752 tm.tm_mday = atoi (t);
757 case 1: /* month of the year */
758 if ((i = mutt_check_month (t)) < 0)
764 tm.tm_year = atoi (t);
767 else if (tm.tm_year >= 1900)
771 case 3: /* time of day */
772 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3);
773 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
776 debug_print (1, ("could not process time format: %s\n", t));
784 case 4: /* timezone */
785 /* sometimes we see things like (MST) or (-0700) so attempt to
786 * compensate by uncommenting the string if non-RFC822 compliant
788 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
790 if (*ptz == '+' || *ptz == '-') {
791 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
792 && isdigit ((unsigned char) ptz[1])
793 && isdigit ((unsigned char) ptz[2])
794 && isdigit ((unsigned char) ptz[3])
795 && isdigit ((unsigned char) ptz[4])) {
796 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
797 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
806 tz = bsearch (ptz, TimeZones, sizeof TimeZones / sizeof (struct tz_t),
807 sizeof (struct tz_t),
808 (int (*)(const void *, const void *)) ascii_strcasecmp
809 /* This is safe to do: A pointer to a struct equals
810 * a pointer to its first element*/ );
814 zminutes = tz->zminutes;
815 zoccident = tz->zoccident;
818 /* ad hoc support for the European MET (now officially CET) TZ */
819 if (ascii_strcasecmp (t, "MET") == 0) {
820 if ((t = strtok (NULL, " \t")) != NULL) {
821 if (!ascii_strcasecmp (t, "DST"))
826 tz_offset = zhours * 3600 + zminutes * 60;
828 tz_offset = -tz_offset;
835 if (count < 4) { /* don't check for missing timezone */
836 debug_print (1, ("error parsing date format, using received time\n"));
842 h->zminutes = zminutes;
843 h->zoccident = zoccident;
846 return (mutt_mktime (&tm, 0) + tz_offset);
849 /* extract the first substring that looks like a message-id */
850 static char *extract_message_id(const char *s)
854 if ((s = strchr(s, '<')) == NULL || (p = strchr(s, '>')) == NULL)
856 return p_dupstr(s, (p - s) + 1);
859 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
865 if (cur->content->type != TYPEMESSAGE
866 && cur->content->type != TYPEMULTIPART)
867 break; /* nothing to do */
869 if (cur->content->parts)
870 break; /* The message was parsed earlier. */
872 if ((msg = mx_open_message (ctx, cur->msgno))) {
873 mutt_parse_part (msg->fp, cur->content);
875 cur->security = crypt_query (cur->content);
877 mx_close_message (&msg);
880 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);
883 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
884 short user_hdrs, short weed, short do_2047,
893 switch (ascii_tolower (line[0])) {
895 if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
896 e->to = rfc822_parse_adrlist (e->to, p);
899 else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
900 e->from = rfc822_parse_adrlist (e->from, p);
906 if (ascii_strcasecmp (line + 1, "cc") == 0) {
907 e->bcc = rfc822_parse_adrlist (e->bcc, p);
913 if (ascii_strcasecmp (line + 1, "c") == 0) {
914 e->cc = rfc822_parse_adrlist (e->cc, p);
917 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
918 if (ascii_strcasecmp (line + 8, "type") == 0) {
920 mutt_parse_content_type (p, hdr->content);
923 else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
925 hdr->content->encoding = mutt_check_encoding (p);
928 else if (ascii_strcasecmp (line + 8, "length") == 0) {
930 if ((hdr->content->length = atoi (p)) < 0)
931 hdr->content->length = -1;
935 else if (ascii_strcasecmp (line + 8, "description") == 0) {
937 m_strreplace(&hdr->content->description, p);
938 rfc2047_decode (&hdr->content->description);
942 else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
944 parse_content_disposition (p, hdr->content);
951 if (!ascii_strcasecmp ("ate", line + 1)) {
952 m_strreplace(&e->date, p);
954 hdr->date_sent = mutt_parse_date (p, hdr);
960 if (!ascii_strcasecmp ("xpires", line + 1) &&
961 hdr && mutt_parse_date (p, NULL) < time (NULL))
966 if (!ascii_strcasecmp ("rom", line + 1)) {
967 e->from = rfc822_parse_adrlist (e->from, p);
968 /* don't leave from info NULL if there's an invalid address (or
969 * whatever) in From: field; mutt would just display it as empty
970 * and mark mail/(esp.) news article as your own. aaargh! this
971 * bothered me for _years_ */
973 e->from = address_new ();
974 e->from->personal = m_strdup(p);
979 else if (!m_strcasecmp(line + 1, "ollowup-to")) {
980 if (!e->followup_to) {
982 e->followup_to = m_strdup(skipspaces(p));
990 if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
991 mutt_free_list (&e->in_reply_to);
992 e->in_reply_to = mutt_parse_references (p, 1);
998 if (!ascii_strcasecmp (line + 1, "ines")) {
1000 hdr->lines = atoi (p);
1003 * HACK - mutt has, for a very short time, produced negative
1004 * Lines header values. Ignore them.
1012 else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
1013 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1014 if (strncmp (p, "NO", 2)) {
1017 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
1019 if (!(end = strchr (beg, '>')))
1022 /* Take the first mailto URL */
1023 if (url_check_scheme (beg) == U_MAILTO) {
1024 p_delete(&e->list_post);
1025 e->list_post = p_dupstr(beg, end - beg);
1035 if (!ascii_strcasecmp (line + 1, "ime-version")) {
1040 else if (!ascii_strcasecmp (line + 1, "essage-id")) {
1041 /* We add a new "Message-ID:" when building a message */
1042 p_delete(&e->message_id);
1043 e->message_id = extract_message_id (p);
1046 else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
1047 if (!ascii_strcasecmp (line + 5, "reply-to")) {
1048 /* override the Reply-To: field */
1049 address_delete (&e->reply_to);
1050 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1053 else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1054 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1062 if (!m_strcasecmp(line + 1, "ewsgroups")) {
1063 p_delete(&e->newsgroups);
1065 e->newsgroups = m_strdup(skipspaces(p));
1072 /* field `Organization:' saves only for pager! */
1073 if (!m_strcasecmp(line + 1, "rganization")) {
1074 if (!e->organization && m_strcasecmp(p, "unknown"))
1075 e->organization = m_strdup(p);
1080 if (!ascii_strcasecmp (line + 1, "eferences")) {
1081 mutt_free_list (&e->references);
1082 e->references = mutt_parse_references (p, 0);
1085 else if (!ascii_strcasecmp (line + 1, "eply-to")) {
1086 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1089 else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
1090 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1093 else if (!ascii_strcasecmp (line + 1, "eceived")) {
1094 if (hdr && !hdr->received) {
1095 char *d = strchr (p, ';');
1098 hdr->received = mutt_parse_date (d + 1, NULL);
1104 if (!ascii_strcasecmp (line + 1, "ubject")) {
1106 e->subject = m_strdup(p);
1109 else if (!ascii_strcasecmp (line + 1, "ender")) {
1110 e->sender = rfc822_parse_adrlist (e->sender, p);
1113 else if (!ascii_strcasecmp (line + 1, "tatus")) {
1132 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1133 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1134 e->supersedes = m_strdup(p);
1138 if (ascii_strcasecmp (line + 1, "o") == 0) {
1139 e->to = rfc822_parse_adrlist (e->to, p);
1145 if (ascii_strcasecmp (line + 1, "-status") == 0) {
1166 else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1167 e->x_label = m_strdup(p);
1171 else if (!m_strcasecmp(line + 1, "-comment-to")) {
1172 if (!e->x_comment_to)
1173 e->x_comment_to = m_strdup(p);
1176 else if (!m_strcasecmp(line + 1, "ref")) {
1178 e->xref = m_strdup(p);
1187 /* Keep track of the user-defined headers */
1188 if (!matched && user_hdrs) {
1189 /* restore the original line */
1190 line[m_strlen(line)] = ':';
1192 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1193 && !mutt_matches_ignore (line, UnIgnore))
1197 last->next = mutt_new_list ();
1201 last = e->userhdrs = mutt_new_list ();
1202 last->data = m_strdup(line);
1204 rfc2047_decode (&last->data);
1214 /* mutt_read_rfc822_header() -- parses a RFC822 header
1218 * f stream to read from
1220 * hdr header structure of current message (optional).
1222 * user_hdrs If set, store user headers. Used for recall-message and
1225 * weed If this parameter is set and the user has activated the
1226 * $weed option, honor the header weed list for user headers.
1227 * Used for recall-message.
1229 * Returns: newly allocated envelope structure. You should free it by
1230 * mutt_free_envelope() when envelope stay unneeded.
1232 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1235 ENVELOPE *e = mutt_new_envelope ();
1237 char *line = p_new(char, LONG_STRING);
1241 size_t linelen = LONG_STRING;
1242 char buf[LONG_STRING + 1];
1245 if (hdr->content == NULL) {
1246 hdr->content = mutt_new_body ();
1248 /* set the defaults from RFC1521 */
1249 hdr->content->type = TYPETEXT;
1250 hdr->content->subtype = m_strdup("plain");
1251 hdr->content->encoding = ENC7BIT;
1252 hdr->content->length = -1;
1254 /* RFC 2183 says this is arbitrary */
1255 hdr->content->disposition = DISPINLINE;
1259 while ((loc = ftello (f)),
1260 *(line = mutt_read_rfc822_line (f, line, &linelen)) != 0) {
1263 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1264 char return_path[LONG_STRING];
1267 /* some bogus MTAs will quote the original "From " line */
1268 if (m_strncmp(">From ", line, 6) == 0)
1269 continue; /* just ignore */
1270 else if (is_from (line, return_path, sizeof (return_path), &t)) {
1271 /* MH somtimes has the From_ line in the middle of the header! */
1272 if (hdr && !hdr->received)
1273 hdr->received = t - mutt_local_tz (t);
1278 break; /* end of header */
1283 if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1284 if (!rx_list_match (NoSpamList, line)) {
1286 /* if spam tag already exists, figure out how to amend it */
1287 if (e->spam && *buf) {
1288 /* If SpamSep defined, append with separator */
1290 mutt_buffer_addstr (e->spam, SpamSep);
1291 mutt_buffer_addstr (e->spam, buf);
1294 /* else overwrite */
1296 e->spam->dptr = e->spam->data;
1297 *e->spam->dptr = '\0';
1298 mutt_buffer_addstr (e->spam, buf);
1302 /* spam tag is new, and match expr is non-empty; copy */
1303 else if (!e->spam && *buf) {
1304 e->spam = mutt_buffer_from (NULL, buf);
1307 /* match expr is empty; plug in null string if no existing tag */
1308 else if (!e->spam) {
1309 e->spam = mutt_buffer_from (NULL, "");
1312 if (e->spam && e->spam->data)
1313 debug_print (5, ("spam = %s\n", e->spam->data));
1320 continue; /* skip empty header fields */
1323 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1330 hdr->content->hdr_offset = hdr->offset;
1331 hdr->content->offset = ftello (f);
1332 rfc2047_decode_envelope(e);
1333 /* check for missing or invalid date */
1334 if (hdr->date_sent <= 0) {
1335 debug_print (1, ("no date found, using received "
1336 "time from msg separator\n"));
1337 hdr->date_sent = hdr->received;
1344 address_t *mutt_parse_adrlist (address_t * p, const char *s)
1348 /* check for a simple whitespace separated list of addresses */
1349 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1350 char tmp[HUGE_STRING];
1353 m_strcpy(tmp, sizeof(tmp), s);
1355 while ((r = strtok (r, " \t")) != NULL) {
1356 p = rfc822_parse_adrlist (p, r);
1361 p = rfc822_parse_adrlist (p, s);
1367 /* Compares mime types to the ok and except lists */
1368 int count_body_parts_check(LIST **checklist, BODY *b, int dflt) {
1372 /* If list is null, use default behavior. */
1378 for (type = *checklist; type; type = type->next) {
1379 a = (ATTACH_MATCH *)type->data;
1380 debug_print(5, ("cbpc: %s %d/%s ?? %s/%s [%d]... ",
1381 dflt ? "[OK] " : "[EXCL] ",
1382 b->type, b->subtype, a->major, a->minor, a->major_int));
1383 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1384 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1385 debug_print(5, ("yes\n"));
1388 debug_print(5, ("no\n"));
1394 #define AT_COUNT(why) { shallcount = 1; }
1395 #define AT_NOCOUNT(why) { shallcount = 0; }
1397 int count_body_parts (BODY *body, int flags) {
1399 int shallcount, shallrecurse;
1405 for (bp = body; bp != NULL; bp = bp->next) {
1406 /* Initial disposition is to count and not to recurse this part. */
1407 AT_COUNT("default");
1410 debug_print(5, ("bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1411 bp->description ? bp->description : ("none"),
1412 bp->filename ? bp->filename :
1413 bp->d_filename ? bp->d_filename : "(none)",
1414 bp->type, bp->subtype ? bp->subtype : "*"));
1416 if (bp->type == TYPEMESSAGE) {
1419 /* If it's an external body pointer, don't recurse it. */
1420 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1423 /* Don't count containers if they're top-level. */
1424 if (flags & M_PARTS_TOPLEVEL)
1425 AT_NOCOUNT("top-level message/*");
1426 } else if (bp->type == TYPEMULTIPART) {
1427 /* Always recurse multiparts, except multipart/alternative. */
1429 if (!m_strcasecmp(bp->subtype, "alternative"))
1432 /* Don't count containers if they're top-level. */
1433 if (flags & M_PARTS_TOPLEVEL)
1434 AT_NOCOUNT("top-level multipart");
1437 if (bp->disposition == DISPINLINE &&
1438 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1439 AT_NOCOUNT("ignore fundamental inlines");
1441 /* If this body isn't scheduled for enumeration already, don't bother
1442 * profiling it further. */
1445 /* Turn off shallcount if message type is not in ok list,
1446 * or if it is in except list. Check is done separately for
1447 * inlines vs. attachments.
1450 if (bp->disposition == DISPATTACH) {
1451 if (!count_body_parts_check(&AttachAllow, bp, 1))
1452 AT_NOCOUNT("attach not allowed");
1453 if (count_body_parts_check(&AttachExclude, bp, 0))
1454 AT_NOCOUNT("attach excluded");
1456 if (!count_body_parts_check(&InlineAllow, bp, 1))
1457 AT_NOCOUNT("inline not allowed");
1458 if (count_body_parts_check(&InlineExclude, bp, 0))
1459 AT_NOCOUNT("excluded");
1465 bp->attach_qualifies = shallcount ? 1 : 0;
1467 debug_print(5, ("cbp: %p shallcount = %d\n", bp, shallcount));
1470 debug_print(5, ("cbp: %p pre count = %d\n", bp, count));
1471 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1472 count += bp->attach_count;
1473 debug_print(5, ("cbp: %p post count = %d\n", bp, count));
1477 debug_print(5, ("bp: return %d\n", count < 0 ? 0 : count));
1478 return count < 0 ? 0 : count;
1481 int mutt_count_body_parts (HEADER *hdr, int flags) {
1482 if (!option (OPTCOUNTATTACH))
1484 if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1485 return hdr->attach_total;
1487 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1488 hdr->attach_total = count_body_parts(hdr->content, flags | M_PARTS_TOPLEVEL);
1490 hdr->attach_total = 0;
1492 hdr->attach_valid = 1;
1493 return hdr->attach_total;