2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
21 * Copyright notice from original mutt:
22 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
24 * This file is part of mutt-ng, see http://www.muttng.org/.
25 * It's licensed under the GNU General Public License,
26 * please see the file GPL in the top level source directory.
35 #include <lib-lib/mem.h>
36 #include <lib-lib/str.h>
37 #include <lib-lib/ascii.h>
38 #include <lib-lib/macros.h>
39 #include <lib-lib/buffer.h>
40 #include <lib-lib/date.h>
42 #include <lib-crypt/crypt.h>
44 #include "recvattach.h"
48 #include "lib/debug.h"
52 /* Reads an arbitrarily long header field, and looks ahead for continuation
53 * lines. ``line'' must point to a dynamically allocated string; it is
54 * increased if more space is required to fit the whole line.
56 ssize_t mutt_read_rfc822_line(FILE *f, char **line, ssize_t *n)
63 /* end of file or end of headers */
64 if (!fgets(p + pos, *n - pos, f) || (ISSPACE(*p) && pos == 0)) {
69 pos += m_strlen(p + pos);
70 if (p[pos - 1] == '\n') {
73 /* remove trailing spaces. safe: p[0] is not a space */
76 } while (ISSPACE(p[pos]));
78 /* check to see if the next line is a continuation line */
80 if (c != ' ' && c != '\t') {
81 /* next line is a separate header field or EOH */
86 /* eat tabs and spaces from the beginning of the continuation line */
89 } while (c == ' ' || c == '\t');
92 /* string is still terminated because we removed at least one
93 whitespace char above */
97 if (*n < pos + STRING) {
105 LIST *mutt_parse_references (char *s, int in_reply_to)
107 LIST *t, *lst = NULL;
109 char *o = NULL, *new, *at;
111 while ((s = strtok (s, " \t;")) != NULL) {
113 * some mail clients add other garbage besides message-ids, so do a quick
114 * check to make sure this looks like a valid message-id
115 * some idiotic clients also break their message-ids between lines, deal
116 * with that too (give up if it's more than two lines, though)
123 if (s[n - 1] != '>') {
133 if (s[m - 1] == '>') {
134 new = p_new(char, n + m + 1);
135 strcpy (new, o); /* __STRCPY_CHECKED__ */
136 strcpy (new + n, s); /* __STRCPY_CHECKED__ */
140 /* make sure that this really does look like a message-id.
141 * it should have exactly one @, and if we're looking at
142 * an in-reply-to header, make sure that the part before
143 * the @ has more than eight characters or it's probably
146 if (!(at = strchr (new, '@')) || strchr (at + 1, '@')
147 || (in_reply_to && at - new <= 8))
163 int mutt_check_encoding (const char *c)
165 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit") - 1) == 0)
167 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit") - 1) == 0)
169 else if (ascii_strncasecmp ("binary", c, sizeof ("binary") - 1) == 0)
172 if (ascii_strncasecmp
173 ("quoted-printable", c, sizeof ("quoted-printable") - 1) == 0)
174 return (ENCQUOTEDPRINTABLE);
175 else if (ascii_strncasecmp ("base64", c, sizeof ("base64") - 1) == 0)
177 else if (ascii_strncasecmp ("x-uuencode", c, sizeof ("x-uuencode") - 1) == 0)
178 return (ENCUUENCODED);
183 static PARAMETER *parse_parameters (const char *s)
185 PARAMETER *head = 0, *cur = 0, *new;
186 char buffer[LONG_STRING];
190 debug_print (2, ("`%s'\n", s));
193 if ((p = strpbrk (s, "=;")) == NULL) {
194 debug_print (1, ("malformed parameter: %s\n", s));
198 /* if we hit a ; now the parameter has no value, just skip it */
202 new = mutt_new_parameter ();
204 new->attribute = p_dupstr(s, i);
206 /* remove whitespace from the end of the attribute name */
207 while (ISSPACE (new->attribute[--i]))
208 new->attribute[i] = 0;
210 s = vskipspaces(p + 1); /* skip over the = */
216 for (i = 0; *s && i < sizeof (buffer) - 1; i++, s++) {
217 if (!option (OPTSTRICTMIME)) {
218 /* As iso-2022-* has a characer of '"' with non-ascii state,
220 if (*s == 0x1b && i < sizeof (buffer) - 2) {
221 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
227 if (state_ascii && *s == '"')
230 /* Quote the next character */
240 s++; /* skip over the " */
243 for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1;
249 new->value = m_strdup(buffer);
251 debug_print (2, ("`%s' = `%s'\n", new->attribute ? new->attribute : "",
252 new->value ? new->value : ""));
254 /* Add this parameter to the list */
263 debug_print (1, ("parameter with no value: %s\n", s));
267 /* Find the next parameter */
268 if (*s != ';' && (s = strchr (s, ';')) == NULL)
269 break; /* no more parameters */
272 /* Move past any leading whitespace */
273 s = vskipspaces(s + 1);
275 while (*s == ';'); /* skip empty parameters */
280 rfc2231_decode_parameters (&head);
284 int mutt_check_mime_type (const char *s)
286 if (ascii_strcasecmp ("text", s) == 0)
288 else if (ascii_strcasecmp ("multipart", s) == 0)
289 return TYPEMULTIPART;
290 else if (ascii_strcasecmp ("application", s) == 0)
291 return TYPEAPPLICATION;
292 else if (ascii_strcasecmp ("message", s) == 0)
294 else if (ascii_strcasecmp ("image", s) == 0)
296 else if (ascii_strcasecmp ("audio", s) == 0)
298 else if (ascii_strcasecmp ("video", s) == 0)
300 else if (ascii_strcasecmp ("model", s) == 0)
302 else if (ascii_strcasecmp ("*", s) == 0)
304 else if (ascii_strcasecmp (".*", s) == 0)
310 void mutt_parse_content_type (char *s, BODY * ct)
315 p_delete(&ct->subtype);
316 mutt_free_parameter (&ct->parameter);
318 /* First extract any existing parameters */
319 if ((pc = strchr (s, ';')) != NULL) {
321 while (*pc && ISSPACE (*pc))
323 ct->parameter = parse_parameters (pc);
325 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
326 * but if a filename has already been set in the content-disposition,
327 * let that take precedence, and don't set it here */
328 if ((pc = mutt_get_parameter ("name", ct->parameter)) != 0
330 ct->filename = m_strdup(pc);
333 /* Now get the subtype */
334 if ((subtype = strchr (s, '/'))) {
336 for (pc = subtype; *pc && !ISSPACE (*pc) && *pc != ';'; pc++);
338 ct->subtype = m_strdup(subtype);
341 /* Finally, get the major type */
342 ct->type = mutt_check_mime_type (s);
344 if (ct->type == TYPEOTHER) {
345 ct->xtype = m_strdup(s);
348 if (ct->subtype == NULL) {
349 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
350 * field, so we can attempt to convert the type to BODY here.
352 if (ct->type == TYPETEXT)
353 ct->subtype = m_strdup("plain");
354 else if (ct->type == TYPEAUDIO)
355 ct->subtype = m_strdup("basic");
356 else if (ct->type == TYPEMESSAGE)
357 ct->subtype = m_strdup("rfc822");
358 else if (ct->type == TYPEOTHER) {
359 char buffer[SHORT_STRING];
361 ct->type = TYPEAPPLICATION;
362 snprintf (buffer, sizeof (buffer), "x-%s", s);
363 ct->subtype = m_strdup(buffer);
366 ct->subtype = m_strdup("x-unknown");
369 /* Default character set for text types. */
370 if (ct->type == TYPETEXT) {
371 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
372 mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
374 mutt_get_first_charset (AssumedCharset),
380 static void parse_content_disposition (char *s, BODY * ct)
384 if (!ascii_strncasecmp ("inline", s, 6))
385 ct->disposition = DISPINLINE;
386 else if (!ascii_strncasecmp ("form-data", s, 9))
387 ct->disposition = DISPFORMDATA;
389 ct->disposition = DISPATTACH;
391 /* Check to see if a default filename was given */
392 if ((s = strchr (s, ';')) != NULL) {
393 s = vskipspaces(s + 1);
394 if ((s = mutt_get_parameter("filename",
395 (parms = parse_parameters (s)))) != 0)
396 m_strreplace(&ct->filename, s);
397 if ((s = mutt_get_parameter ("name", parms)) != 0)
398 ct->form_name = m_strdup(s);
399 mutt_free_parameter (&parms);
404 * fp stream to read from
406 * digest 1 if reading subparts of a multipart/digest, 0
410 BODY *mutt_read_mime_header (FILE * fp, int digest)
412 BODY *p = mutt_new_body ();
414 char *line = p_new(char, LONG_STRING);
415 ssize_t linelen = LONG_STRING;
417 p->hdr_offset = ftello (fp);
419 p->encoding = ENC7BIT; /* default from RFC1521 */
420 p->type = digest ? TYPEMESSAGE : TYPETEXT;
421 p->disposition = DISPINLINE;
423 while (mutt_read_rfc822_line(fp, &line, &linelen)) {
424 /* Find the value of the current header */
425 if ((c = strchr (line, ':'))) {
429 debug_print (1, ("skipping empty header field: %s\n", line));
434 debug_print (1, ("bogus MIME header: %s\n", line));
438 if (!ascii_strncasecmp ("content-", line, 8)) {
439 if (!ascii_strcasecmp ("type", line + 8))
440 mutt_parse_content_type (c, p);
441 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
442 p->encoding = mutt_check_encoding (c);
443 else if (!ascii_strcasecmp ("disposition", line + 8))
444 parse_content_disposition (c, p);
445 else if (!ascii_strcasecmp ("description", line + 8)) {
446 m_strreplace(&p->description, c);
447 rfc2047_decode (&p->description);
451 p->offset = ftello (fp); /* Mark the start of the real data */
452 if (p->type == TYPETEXT && !p->subtype)
453 p->subtype = m_strdup("plain");
454 else if (p->type == TYPEMESSAGE && !p->subtype)
455 p->subtype = m_strdup("rfc822");
462 void mutt_parse_part (FILE * fp, BODY * b)
468 bound = mutt_get_parameter ("boundary", b->parameter);
469 fseeko (fp, b->offset, SEEK_SET);
470 b->parts = mutt_parse_multipart (fp, bound,
471 b->offset + b->length,
472 ascii_strcasecmp ("digest",
478 fseeko (fp, b->offset, SEEK_SET);
479 if (mutt_is_message_type (b->type, b->subtype))
480 b->parts = mutt_parse_messageRFC822 (fp, b);
481 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
482 b->parts = mutt_read_mime_header (fp, 0);
492 /* try to recover from parsing error */
495 m_strreplace(&b->subtype, "plain");
499 /* parse a MESSAGE/RFC822 body
502 * fp stream to read from
504 * parent structure which contains info about the message/rfc822
507 * NOTE: this assumes that `parent->length' has been set!
510 BODY *mutt_parse_messageRFC822 (FILE * fp, BODY * parent)
514 parent->hdr = mutt_new_header ();
515 parent->hdr->offset = ftello (fp);
516 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
517 msg = parent->hdr->content;
519 /* ignore the length given in the content-length since it could be wrong
520 and we already have the info to calculate the correct length */
521 /* if (msg->length == -1) */
522 msg->length = parent->length - (msg->offset - parent->offset);
524 /* if body of this message is empty, we can end up with a negative length */
528 mutt_parse_part (fp, msg);
532 /* parse a multipart structure
535 * fp stream to read from
537 * boundary body separator
539 * end_off length of the multipart body (used when the final
540 * boundary is missing to avoid reading too far)
542 * digest 1 if reading a multipart/digest, 0 otherwise
545 BODY *mutt_parse_multipart (FILE * fp, const char *boundary, off_t end_off,
548 int blen, len, crlf = 0;
549 char buffer[LONG_STRING];
550 BODY *head = 0, *last = 0, *new = 0;
552 int final = 0; /* did we see the ending boundary? */
555 mutt_error _("multipart message has no boundary parameter!");
560 blen = m_strlen(boundary);
561 while (ftello (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL) {
562 len = m_strlen(buffer);
564 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
566 if (buffer[0] == '-' && buffer[1] == '-' &&
567 m_strncmp(buffer + 2, boundary, blen) == 0) {
569 last->length = ftello (fp) - last->offset - len - 1 - crlf;
570 if (last->parts && last->parts->length == 0)
571 last->parts->length =
572 ftello (fp) - last->parts->offset - len - 1 - crlf;
573 /* if the body is empty, we can end up with a -1 length */
574 if (last->length < 0)
578 /* Remove any trailing whitespace, up to the length of the boundary */
579 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
582 /* Check for the end boundary */
583 if (m_strcmp(buffer + blen + 2, "--") == 0) {
585 break; /* done parsing */
587 else if (buffer[2 + blen] == 0) {
588 new = mutt_read_mime_header (fp, digest);
591 * Consistency checking - catch
592 * bad attachment end boundaries
595 if (new->offset > end_off) {
596 mutt_free_body (&new);
609 /* in case of missing end boundary, set the length to something reasonable */
610 if (last && last->length == 0 && !final)
611 last->length = end_off - last->offset;
613 /* parse recursive MIME parts */
614 for (last = head; last; last = last->next)
615 mutt_parse_part (fp, last);
620 static const char *uncomment_timezone (char *buf, size_t buflen,
627 return tz; /* no need to do anything */
628 tz = vskipspaces(tz + 1);
629 if ((p = strpbrk (tz, " )")) == NULL)
632 if (len > buflen - 1)
634 memcpy (buf, tz, len);
641 unsigned char zhours;
642 unsigned char zminutes;
643 unsigned char zoccident; /* west of UTC? */
646 "aat", 1, 0, 1}, /* Atlantic Africa Time */
648 "adt", 4, 0, 0}, /* Arabia DST */
650 "ast", 3, 0, 0}, /* Arabia */
651 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
653 "bst", 1, 0, 0}, /* British DST */
655 "cat", 1, 0, 0}, /* Central Africa */
658 "cest", 2, 0, 0}, /* Central Europe DST */
660 "cet", 1, 0, 0}, /* Central Europe */
663 /*{ "cst", 8, 0, 0 }, *//* China */
664 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
666 "eat", 3, 0, 0}, /* East Africa */
669 "eest", 3, 0, 0}, /* Eastern Europe DST */
671 "eet", 2, 0, 0}, /* Eastern Europe */
673 "egst", 0, 0, 0}, /* Eastern Greenland DST */
675 "egt", 1, 0, 1}, /* Eastern Greenland */
679 "gst", 4, 0, 0}, /* Presian Gulf */
681 "hkt", 8, 0, 0}, /* Hong Kong */
683 "ict", 7, 0, 0}, /* Indochina */
685 "idt", 3, 0, 0}, /* Israel DST */
687 "ist", 2, 0, 0}, /* Israel */
688 /*{ "ist", 5, 30, 0 }, *//* India */
690 "jst", 9, 0, 0}, /* Japan */
692 "kst", 9, 0, 0}, /* Korea */
695 "met", 1, 0, 0}, /* this is now officially CET */
697 "msd", 4, 0, 0}, /* Moscow DST */
699 "msk", 3, 0, 0}, /* Moscow */
702 "nzdt", 13, 0, 0}, /* New Zealand DST */
704 "nzst", 12, 0, 0}, /* New Zealand */
708 "sat", 2, 0, 0}, /* South Africa */
710 "smt", 4, 0, 0}, /* Seychelles */
712 "sst", 11, 0, 1}, /* Samoa */
713 /*{ "sst", 8, 0, 0 }, *//* Singapore */
716 "wat", 0, 0, 0}, /* West Africa */
718 "west", 1, 0, 0}, /* Western Europe DST */
720 "wet", 0, 0, 0}, /* Western Europe */
722 "wgst", 2, 0, 1}, /* Western Greenland DST */
724 "wgt", 3, 0, 1}, /* Western Greenland */
726 "wst", 8, 0, 0}, /* Western Australia */
729 /* parses a date string in RFC822 format:
731 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
733 * This routine assumes that `h' has been initialized to 0. the `timezone'
734 * field is optional, defaulting to +0000 if missing.
736 time_t mutt_parse_date (const char *s, HEADER * h)
748 char tzstr[SHORT_STRING];
749 char scratch[SHORT_STRING];
751 /* Don't modify our argument. Fixed-size buffer is ok here since
752 * the date format imposes a natural limit.
755 m_strcpy(scratch, sizeof(scratch), s);
757 /* kill the day of the week, if it exists. */
758 if ((t = strchr (scratch, ',')))
766 while ((t = strtok (t, " \t")) != NULL) {
768 case 0: /* day of the month */
769 if (!isdigit ((unsigned char) *t))
771 tm.tm_mday = atoi (t);
776 case 1: /* month of the year */
777 if ((i = mutt_check_month (t)) < 0)
783 tm.tm_year = atoi (t);
786 else if (tm.tm_year >= 1900)
790 case 3: /* time of day */
791 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3);
792 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
795 debug_print (1, ("could not process time format: %s\n", t));
803 case 4: /* timezone */
804 /* sometimes we see things like (MST) or (-0700) so attempt to
805 * compensate by uncommenting the string if non-RFC822 compliant
807 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
809 if (*ptz == '+' || *ptz == '-') {
810 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
811 && isdigit ((unsigned char) ptz[1])
812 && isdigit ((unsigned char) ptz[2])
813 && isdigit ((unsigned char) ptz[3])
814 && isdigit ((unsigned char) ptz[4])) {
815 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
816 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
825 tz = bsearch (ptz, TimeZones, sizeof TimeZones / sizeof (struct tz_t),
826 sizeof (struct tz_t),
827 (int (*)(const void *, const void *)) ascii_strcasecmp
828 /* This is safe to do: A pointer to a struct equals
829 * a pointer to its first element*/ );
833 zminutes = tz->zminutes;
834 zoccident = tz->zoccident;
837 /* ad hoc support for the European MET (now officially CET) TZ */
838 if (ascii_strcasecmp (t, "MET") == 0) {
839 if ((t = strtok (NULL, " \t")) != NULL) {
840 if (!ascii_strcasecmp (t, "DST"))
845 tz_offset = zhours * 3600 + zminutes * 60;
847 tz_offset = -tz_offset;
854 if (count < 4) { /* don't check for missing timezone */
855 debug_print (1, ("error parsing date format, using received time\n"));
861 h->zminutes = zminutes;
862 h->zoccident = zoccident;
865 return (mutt_mktime (&tm, 0) + tz_offset);
868 /* extract the first substring that looks like a message-id */
869 static char *extract_message_id(const char *s)
873 if ((s = strchr(s, '<')) == NULL || (p = strchr(s, '>')) == NULL)
875 return p_dupstr(s, (p - s) + 1);
878 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
884 if (cur->content->type != TYPEMESSAGE
885 && cur->content->type != TYPEMULTIPART)
886 break; /* nothing to do */
888 if (cur->content->parts)
889 break; /* The message was parsed earlier. */
891 if ((msg = mx_open_message (ctx, cur->msgno))) {
892 mutt_parse_part (msg->fp, cur->content);
894 cur->security = crypt_query (cur->content);
896 mx_close_message (&msg);
899 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);
902 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
903 short user_hdrs, short weed, short do_2047,
912 switch (ascii_tolower (line[0])) {
914 if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
915 e->to = rfc822_parse_adrlist (e->to, p);
918 else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
919 e->from = rfc822_parse_adrlist (e->from, p);
925 if (ascii_strcasecmp (line + 1, "cc") == 0) {
926 e->bcc = rfc822_parse_adrlist (e->bcc, p);
932 if (ascii_strcasecmp (line + 1, "c") == 0) {
933 e->cc = rfc822_parse_adrlist (e->cc, p);
936 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
937 if (ascii_strcasecmp (line + 8, "type") == 0) {
939 mutt_parse_content_type (p, hdr->content);
942 else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
944 hdr->content->encoding = mutt_check_encoding (p);
947 else if (ascii_strcasecmp (line + 8, "length") == 0) {
949 if ((hdr->content->length = atoi (p)) < 0)
950 hdr->content->length = -1;
954 else if (ascii_strcasecmp (line + 8, "description") == 0) {
956 m_strreplace(&hdr->content->description, p);
957 rfc2047_decode (&hdr->content->description);
961 else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
963 parse_content_disposition (p, hdr->content);
970 if (!ascii_strcasecmp ("ate", line + 1)) {
971 m_strreplace(&e->date, p);
973 hdr->date_sent = mutt_parse_date (p, hdr);
979 if (!ascii_strcasecmp ("xpires", line + 1) &&
980 hdr && mutt_parse_date (p, NULL) < time (NULL))
985 if (!ascii_strcasecmp ("rom", line + 1)) {
986 e->from = rfc822_parse_adrlist (e->from, p);
987 /* don't leave from info NULL if there's an invalid address (or
988 * whatever) in From: field; mutt would just display it as empty
989 * and mark mail/(esp.) news article as your own. aaargh! this
990 * bothered me for _years_ */
992 e->from = address_new ();
993 e->from->personal = m_strdup(p);
998 else if (!m_strcasecmp(line + 1, "ollowup-to")) {
999 if (!e->followup_to) {
1001 e->followup_to = m_strdup(skipspaces(p));
1009 if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
1010 mutt_free_list (&e->in_reply_to);
1011 e->in_reply_to = mutt_parse_references (p, 1);
1017 if (!ascii_strcasecmp (line + 1, "ines")) {
1019 hdr->lines = atoi (p);
1022 * HACK - mutt has, for a very short time, produced negative
1023 * Lines header values. Ignore them.
1031 else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
1032 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1033 if (strncmp (p, "NO", 2)) {
1036 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
1038 if (!(end = strchr (beg, '>')))
1041 /* Take the first mailto URL */
1042 if (url_check_scheme (beg) == U_MAILTO) {
1043 p_delete(&e->list_post);
1044 e->list_post = p_dupstr(beg, end - beg);
1054 if (!ascii_strcasecmp (line + 1, "ime-version")) {
1059 else if (!ascii_strcasecmp (line + 1, "essage-id")) {
1060 /* We add a new "Message-ID:" when building a message */
1061 p_delete(&e->message_id);
1062 e->message_id = extract_message_id (p);
1065 else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
1066 if (!ascii_strcasecmp (line + 5, "reply-to")) {
1067 /* override the Reply-To: field */
1068 address_delete (&e->reply_to);
1069 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1072 else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1073 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1081 if (!m_strcasecmp(line + 1, "ewsgroups")) {
1082 p_delete(&e->newsgroups);
1084 e->newsgroups = m_strdup(skipspaces(p));
1091 /* field `Organization:' saves only for pager! */
1092 if (!m_strcasecmp(line + 1, "rganization")) {
1093 if (!e->organization && m_strcasecmp(p, "unknown"))
1094 e->organization = m_strdup(p);
1099 if (!ascii_strcasecmp (line + 1, "eferences")) {
1100 mutt_free_list (&e->references);
1101 e->references = mutt_parse_references (p, 0);
1104 else if (!ascii_strcasecmp (line + 1, "eply-to")) {
1105 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1108 else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
1109 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1112 else if (!ascii_strcasecmp (line + 1, "eceived")) {
1113 if (hdr && !hdr->received) {
1114 char *d = strchr (p, ';');
1117 hdr->received = mutt_parse_date (d + 1, NULL);
1123 if (!ascii_strcasecmp (line + 1, "ubject")) {
1125 e->subject = m_strdup(p);
1128 else if (!ascii_strcasecmp (line + 1, "ender")) {
1129 e->sender = rfc822_parse_adrlist (e->sender, p);
1132 else if (!ascii_strcasecmp (line + 1, "tatus")) {
1151 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1152 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1153 e->supersedes = m_strdup(p);
1157 if (ascii_strcasecmp (line + 1, "o") == 0) {
1158 e->to = rfc822_parse_adrlist (e->to, p);
1164 if (ascii_strcasecmp (line + 1, "-status") == 0) {
1185 else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1186 e->x_label = m_strdup(p);
1190 else if (!m_strcasecmp(line + 1, "-comment-to")) {
1191 if (!e->x_comment_to)
1192 e->x_comment_to = m_strdup(p);
1195 else if (!m_strcasecmp(line + 1, "ref")) {
1197 e->xref = m_strdup(p);
1206 /* Keep track of the user-defined headers */
1207 if (!matched && user_hdrs) {
1208 /* restore the original line */
1209 line[m_strlen(line)] = ':';
1211 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1212 && !mutt_matches_ignore (line, UnIgnore))
1216 last->next = mutt_new_list ();
1220 last = e->userhdrs = mutt_new_list ();
1221 last->data = m_strdup(line);
1223 rfc2047_decode (&last->data);
1233 /* mutt_read_rfc822_header() -- parses a RFC822 header
1237 * f stream to read from
1239 * hdr header structure of current message (optional).
1241 * user_hdrs If set, store user headers. Used for recall-message and
1244 * weed If this parameter is set and the user has activated the
1245 * $weed option, honor the header weed list for user headers.
1246 * Used for recall-message.
1248 * Returns: newly allocated envelope structure. You should free it by
1249 * mutt_free_envelope() when envelope stay unneeded.
1251 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1254 ENVELOPE *e = mutt_new_envelope ();
1256 char *line = p_new(char, LONG_STRING);
1260 ssize_t linelen = LONG_STRING;
1261 char buf[LONG_STRING + 1];
1264 if (hdr->content == NULL) {
1265 hdr->content = mutt_new_body ();
1267 /* set the defaults from RFC1521 */
1268 hdr->content->type = TYPETEXT;
1269 hdr->content->subtype = m_strdup("plain");
1270 hdr->content->encoding = ENC7BIT;
1271 hdr->content->length = -1;
1273 /* RFC 2183 says this is arbitrary */
1274 hdr->content->disposition = DISPINLINE;
1278 while ((loc = ftello (f)),
1279 mutt_read_rfc822_line (f, &line, &linelen))
1283 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1284 char return_path[LONG_STRING];
1287 /* some bogus MTAs will quote the original "From " line */
1288 if (m_strncmp(">From ", line, 6) == 0)
1289 continue; /* just ignore */
1290 else if (is_from (line, return_path, sizeof (return_path), &t)) {
1291 /* MH somtimes has the From_ line in the middle of the header! */
1292 if (hdr && !hdr->received)
1293 hdr->received = t - mutt_local_tz (t);
1298 break; /* end of header */
1303 if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1304 if (!rx_list_match (NoSpamList, line)) {
1306 /* if spam tag already exists, figure out how to amend it */
1307 if (e->spam && *buf) {
1308 /* If SpamSep defined, append with separator */
1310 mutt_buffer_addstr (e->spam, SpamSep);
1311 mutt_buffer_addstr (e->spam, buf);
1314 /* else overwrite */
1316 e->spam->dptr = e->spam->data;
1317 *e->spam->dptr = '\0';
1318 mutt_buffer_addstr (e->spam, buf);
1322 /* spam tag is new, and match expr is non-empty; copy */
1323 else if (!e->spam && *buf) {
1324 e->spam = mutt_buffer_from (NULL, buf);
1327 /* match expr is empty; plug in null string if no existing tag */
1328 else if (!e->spam) {
1329 e->spam = mutt_buffer_from (NULL, "");
1332 if (e->spam && e->spam->data)
1333 debug_print (5, ("spam = %s\n", e->spam->data));
1340 continue; /* skip empty header fields */
1343 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1350 hdr->content->hdr_offset = hdr->offset;
1351 hdr->content->offset = ftello (f);
1352 rfc2047_decode_envelope(e);
1353 /* check for missing or invalid date */
1354 if (hdr->date_sent <= 0) {
1355 debug_print (1, ("no date found, using received "
1356 "time from msg separator\n"));
1357 hdr->date_sent = hdr->received;
1364 address_t *mutt_parse_adrlist (address_t * p, const char *s)
1368 /* check for a simple whitespace separated list of addresses */
1369 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1370 char tmp[HUGE_STRING];
1373 m_strcpy(tmp, sizeof(tmp), s);
1375 while ((r = strtok (r, " \t")) != NULL) {
1376 p = rfc822_parse_adrlist (p, r);
1381 p = rfc822_parse_adrlist (p, s);
1387 /* Compares mime types to the ok and except lists */
1388 int count_body_parts_check(LIST **checklist, BODY *b, int dflt) {
1392 /* If list is null, use default behavior. */
1398 for (type = *checklist; type; type = type->next) {
1399 a = (ATTACH_MATCH *)type->data;
1400 debug_print(5, ("cbpc: %s %d/%s ?? %s/%s [%d]... ",
1401 dflt ? "[OK] " : "[EXCL] ",
1402 b->type, b->subtype, a->major, a->minor, a->major_int));
1403 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1404 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1405 debug_print(5, ("yes\n"));
1408 debug_print(5, ("no\n"));
1414 #define AT_COUNT(why) { shallcount = 1; }
1415 #define AT_NOCOUNT(why) { shallcount = 0; }
1417 int count_body_parts (BODY *body, int flags) {
1419 int shallcount, shallrecurse;
1425 for (bp = body; bp != NULL; bp = bp->next) {
1426 /* Initial disposition is to count and not to recurse this part. */
1427 AT_COUNT("default");
1430 debug_print(5, ("bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1431 bp->description ? bp->description : ("none"),
1432 bp->filename ? bp->filename :
1433 bp->d_filename ? bp->d_filename : "(none)",
1434 bp->type, bp->subtype ? bp->subtype : "*"));
1436 if (bp->type == TYPEMESSAGE) {
1439 /* If it's an external body pointer, don't recurse it. */
1440 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1443 /* Don't count containers if they're top-level. */
1444 if (flags & M_PARTS_TOPLEVEL)
1445 AT_NOCOUNT("top-level message/*");
1446 } else if (bp->type == TYPEMULTIPART) {
1447 /* Always recurse multiparts, except multipart/alternative. */
1449 if (!m_strcasecmp(bp->subtype, "alternative"))
1452 /* Don't count containers if they're top-level. */
1453 if (flags & M_PARTS_TOPLEVEL)
1454 AT_NOCOUNT("top-level multipart");
1457 if (bp->disposition == DISPINLINE &&
1458 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1459 AT_NOCOUNT("ignore fundamental inlines");
1461 /* If this body isn't scheduled for enumeration already, don't bother
1462 * profiling it further. */
1465 /* Turn off shallcount if message type is not in ok list,
1466 * or if it is in except list. Check is done separately for
1467 * inlines vs. attachments.
1470 if (bp->disposition == DISPATTACH) {
1471 if (!count_body_parts_check(&AttachAllow, bp, 1))
1472 AT_NOCOUNT("attach not allowed");
1473 if (count_body_parts_check(&AttachExclude, bp, 0))
1474 AT_NOCOUNT("attach excluded");
1476 if (!count_body_parts_check(&InlineAllow, bp, 1))
1477 AT_NOCOUNT("inline not allowed");
1478 if (count_body_parts_check(&InlineExclude, bp, 0))
1479 AT_NOCOUNT("excluded");
1485 bp->attach_qualifies = shallcount ? 1 : 0;
1487 debug_print(5, ("cbp: %p shallcount = %d\n", bp, shallcount));
1490 debug_print(5, ("cbp: %p pre count = %d\n", bp, count));
1491 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1492 count += bp->attach_count;
1493 debug_print(5, ("cbp: %p post count = %d\n", bp, count));
1497 debug_print(5, ("bp: return %d\n", count < 0 ? 0 : count));
1498 return count < 0 ? 0 : count;
1501 int mutt_count_body_parts (HEADER *hdr, int flags) {
1502 if (!option (OPTCOUNTATTACH))
1504 if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1505 return hdr->attach_total;
1507 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1508 hdr->attach_total = count_body_parts(hdr->content, flags | M_PARTS_TOPLEVEL);
1510 hdr->attach_total = 0;
1512 hdr->attach_valid = 1;
1513 return hdr->attach_total;