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) ==
160 return (ENCUUENCODED);
161 #ifdef SUN_ATTACHMENT
162 else if (ascii_strncasecmp ("uuencode", c, sizeof ("uuencode") - 1) == 0)
163 return (ENCUUENCODED);
169 static PARAMETER *parse_parameters (const char *s)
171 PARAMETER *head = 0, *cur = 0, *new;
172 char buffer[LONG_STRING];
176 debug_print (2, ("`%s'\n", s));
179 if ((p = strpbrk (s, "=;")) == NULL) {
180 debug_print (1, ("malformed parameter: %s\n", s));
184 /* if we hit a ; now the parameter has no value, just skip it */
188 new = mutt_new_parameter ();
190 new->attribute = p_dupstr(s, i);
192 /* remove whitespace from the end of the attribute name */
193 while (ISSPACE (new->attribute[--i]))
194 new->attribute[i] = 0;
196 s = vskipspaces(p + 1); /* skip over the = */
202 for (i = 0; *s && i < sizeof (buffer) - 1; i++, s++) {
203 if (!option (OPTSTRICTMIME)) {
204 /* As iso-2022-* has a characer of '"' with non-ascii state,
206 if (*s == 0x1b && i < sizeof (buffer) - 2) {
207 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
213 if (state_ascii && *s == '"')
216 /* Quote the next character */
226 s++; /* skip over the " */
229 for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1;
235 new->value = m_strdup(buffer);
237 debug_print (2, ("`%s' = `%s'\n", new->attribute ? new->attribute : "",
238 new->value ? new->value : ""));
240 /* Add this parameter to the list */
249 debug_print (1, ("parameter with no value: %s\n", s));
253 /* Find the next parameter */
254 if (*s != ';' && (s = strchr (s, ';')) == NULL)
255 break; /* no more parameters */
258 /* Move past any leading whitespace */
259 s = vskipspaces(s + 1);
261 while (*s == ';'); /* skip empty parameters */
266 rfc2231_decode_parameters (&head);
270 int mutt_check_mime_type (const char *s)
272 if (ascii_strcasecmp ("text", s) == 0)
274 else if (ascii_strcasecmp ("multipart", s) == 0)
275 return TYPEMULTIPART;
276 #ifdef SUN_ATTACHMENT
277 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
278 return TYPEMULTIPART;
280 else if (ascii_strcasecmp ("application", s) == 0)
281 return TYPEAPPLICATION;
282 else if (ascii_strcasecmp ("message", s) == 0)
284 else if (ascii_strcasecmp ("image", s) == 0)
286 else if (ascii_strcasecmp ("audio", s) == 0)
288 else if (ascii_strcasecmp ("video", s) == 0)
290 else if (ascii_strcasecmp ("model", s) == 0)
292 else if (ascii_strcasecmp ("*", s) == 0)
294 else if (ascii_strcasecmp (".*", s) == 0)
300 void mutt_parse_content_type (char *s, BODY * ct)
305 p_delete(&ct->subtype);
306 mutt_free_parameter (&ct->parameter);
308 /* First extract any existing parameters */
309 if ((pc = strchr (s, ';')) != NULL) {
311 while (*pc && ISSPACE (*pc))
313 ct->parameter = parse_parameters (pc);
315 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
316 * but if a filename has already been set in the content-disposition,
317 * let that take precedence, and don't set it here */
318 if ((pc = mutt_get_parameter ("name", ct->parameter)) != 0
320 ct->filename = m_strdup(pc);
322 #ifdef SUN_ATTACHMENT
323 /* this is deep and utter perversion */
324 if ((pc = mutt_get_parameter ("conversions", ct->parameter)) != 0)
325 ct->encoding = mutt_check_encoding (pc);
330 /* Now get the subtype */
331 if ((subtype = strchr (s, '/'))) {
333 for (pc = subtype; *pc && !ISSPACE (*pc) && *pc != ';'; pc++);
335 ct->subtype = m_strdup(subtype);
338 /* Finally, get the major type */
339 ct->type = mutt_check_mime_type (s);
341 #ifdef SUN_ATTACHMENT
342 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
343 ct->subtype = m_strdup("x-sun-attachment");
346 if (ct->type == TYPEOTHER) {
347 ct->xtype = m_strdup(s);
350 if (ct->subtype == NULL) {
351 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
352 * field, so we can attempt to convert the type to BODY here.
354 if (ct->type == TYPETEXT)
355 ct->subtype = m_strdup("plain");
356 else if (ct->type == TYPEAUDIO)
357 ct->subtype = m_strdup("basic");
358 else if (ct->type == TYPEMESSAGE)
359 ct->subtype = m_strdup("rfc822");
360 else if (ct->type == TYPEOTHER) {
361 char buffer[SHORT_STRING];
363 ct->type = TYPEAPPLICATION;
364 snprintf (buffer, sizeof (buffer), "x-%s", s);
365 ct->subtype = m_strdup(buffer);
368 ct->subtype = m_strdup("x-unknown");
371 /* Default character set for text types. */
372 if (ct->type == TYPETEXT) {
373 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
374 mutt_set_parameter ("charset", option (OPTSTRICTMIME) ? "us-ascii" :
376 mutt_get_first_charset (AssumedCharset),
382 static void parse_content_disposition (char *s, BODY * ct)
386 if (!ascii_strncasecmp ("inline", s, 6))
387 ct->disposition = DISPINLINE;
388 else if (!ascii_strncasecmp ("form-data", s, 9))
389 ct->disposition = DISPFORMDATA;
391 ct->disposition = DISPATTACH;
393 /* Check to see if a default filename was given */
394 if ((s = strchr (s, ';')) != NULL) {
395 s = vskipspaces(s + 1);
396 if ((s = mutt_get_parameter("filename",
397 (parms = parse_parameters (s)))) != 0)
398 m_strreplace(&ct->filename, s);
399 if ((s = mutt_get_parameter ("name", parms)) != 0)
400 ct->form_name = m_strdup(s);
401 mutt_free_parameter (&parms);
406 * fp stream to read from
408 * digest 1 if reading subparts of a multipart/digest, 0
412 BODY *mutt_read_mime_header (FILE * fp, int digest)
414 BODY *p = mutt_new_body ();
416 char *line = p_new(char, LONG_STRING);
417 size_t linelen = LONG_STRING;
419 p->hdr_offset = ftello (fp);
421 p->encoding = ENC7BIT; /* default from RFC1521 */
422 p->type = digest ? TYPEMESSAGE : TYPETEXT;
423 p->disposition = DISPINLINE;
425 while (*(line = mutt_read_rfc822_line (fp, line, &linelen)) != 0) {
426 /* Find the value of the current header */
427 if ((c = strchr (line, ':'))) {
431 debug_print (1, ("skipping empty header field: %s\n", line));
436 debug_print (1, ("bogus MIME header: %s\n", line));
440 if (!ascii_strncasecmp ("content-", line, 8)) {
441 if (!ascii_strcasecmp ("type", line + 8))
442 mutt_parse_content_type (c, p);
443 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
444 p->encoding = mutt_check_encoding (c);
445 else if (!ascii_strcasecmp ("disposition", line + 8))
446 parse_content_disposition (c, p);
447 else if (!ascii_strcasecmp ("description", line + 8)) {
448 m_strreplace(&p->description, c);
449 rfc2047_decode (&p->description);
452 #ifdef SUN_ATTACHMENT
453 else if (!ascii_strncasecmp ("x-sun-", line, 6)) {
454 if (!ascii_strcasecmp ("data-type", line + 6))
455 mutt_parse_content_type (c, p);
456 else if (!ascii_strcasecmp ("encoding-info", line + 6))
457 p->encoding = mutt_check_encoding (c);
458 else if (!ascii_strcasecmp ("content-lines", line + 6))
459 mutt_set_parameter ("content-lines", c, &(p->parameter));
460 else if (!ascii_strcasecmp ("data-description", line + 6)) {
461 m_strreplace(&p->description, c);
462 rfc2047_decode (&p->description);
467 p->offset = ftello (fp); /* Mark the start of the real data */
468 if (p->type == TYPETEXT && !p->subtype)
469 p->subtype = m_strdup("plain");
470 else if (p->type == TYPEMESSAGE && !p->subtype)
471 p->subtype = m_strdup("rfc822");
478 void mutt_parse_part (FILE * fp, BODY * b)
484 #ifdef SUN_ATTACHMENT
485 if (!ascii_strcasecmp (b->subtype, "x-sun-attachment"))
489 bound = mutt_get_parameter ("boundary", b->parameter);
491 fseeko (fp, b->offset, SEEK_SET);
492 b->parts = mutt_parse_multipart (fp, bound,
493 b->offset + b->length,
494 ascii_strcasecmp ("digest",
500 fseeko (fp, b->offset, SEEK_SET);
501 if (mutt_is_message_type (b->type, b->subtype))
502 b->parts = mutt_parse_messageRFC822 (fp, b);
503 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
504 b->parts = mutt_read_mime_header (fp, 0);
514 /* try to recover from parsing error */
517 m_strreplace(&b->subtype, "plain");
521 /* parse a MESSAGE/RFC822 body
524 * fp stream to read from
526 * parent structure which contains info about the message/rfc822
529 * NOTE: this assumes that `parent->length' has been set!
532 BODY *mutt_parse_messageRFC822 (FILE * fp, BODY * parent)
536 parent->hdr = mutt_new_header ();
537 parent->hdr->offset = ftello (fp);
538 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
539 msg = parent->hdr->content;
541 /* ignore the length given in the content-length since it could be wrong
542 and we already have the info to calculate the correct length */
543 /* if (msg->length == -1) */
544 msg->length = parent->length - (msg->offset - parent->offset);
546 /* if body of this message is empty, we can end up with a negative length */
550 mutt_parse_part (fp, msg);
554 /* parse a multipart structure
557 * fp stream to read from
559 * boundary body separator
561 * end_off length of the multipart body (used when the final
562 * boundary is missing to avoid reading too far)
564 * digest 1 if reading a multipart/digest, 0 otherwise
567 BODY *mutt_parse_multipart (FILE * fp, const char *boundary, off_t end_off,
570 #ifdef SUN_ATTACHMENT
573 int blen, len, crlf = 0;
574 char buffer[LONG_STRING];
575 BODY *head = 0, *last = 0, *new = 0;
577 int final = 0; /* did we see the ending boundary? */
580 mutt_error _("multipart message has no boundary parameter!");
585 blen = m_strlen(boundary);
586 while (ftello (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL) {
587 len = m_strlen(buffer);
589 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
591 if (buffer[0] == '-' && buffer[1] == '-' &&
592 m_strncmp(buffer + 2, boundary, blen) == 0) {
594 last->length = ftello (fp) - last->offset - len - 1 - crlf;
595 if (last->parts && last->parts->length == 0)
596 last->parts->length =
597 ftello (fp) - last->parts->offset - len - 1 - crlf;
598 /* if the body is empty, we can end up with a -1 length */
599 if (last->length < 0)
603 /* Remove any trailing whitespace, up to the length of the boundary */
604 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
607 /* Check for the end boundary */
608 if (m_strcmp(buffer + blen + 2, "--") == 0) {
610 break; /* done parsing */
612 else if (buffer[2 + blen] == 0) {
613 new = mutt_read_mime_header (fp, digest);
615 #ifdef SUN_ATTACHMENT
616 if (mutt_get_parameter ("content-lines", new->parameter)) {
618 atoi (mutt_get_parameter ("content-lines", new->parameter));
620 if (ftello (fp) >= end_off
621 || fgets (buffer, LONG_STRING, fp) == NULL)
627 * Consistency checking - catch
628 * bad attachment end boundaries
631 if (new->offset > end_off) {
632 mutt_free_body (&new);
645 /* in case of missing end boundary, set the length to something reasonable */
646 if (last && last->length == 0 && !final)
647 last->length = end_off - last->offset;
649 /* parse recursive MIME parts */
650 for (last = head; last; last = last->next)
651 mutt_parse_part (fp, last);
656 static const char *uncomment_timezone (char *buf, size_t buflen,
663 return tz; /* no need to do anything */
664 tz = vskipspaces(tz + 1);
665 if ((p = strpbrk (tz, " )")) == NULL)
668 if (len > buflen - 1)
670 memcpy (buf, tz, len);
677 unsigned char zhours;
678 unsigned char zminutes;
679 unsigned char zoccident; /* west of UTC? */
682 "aat", 1, 0, 1}, /* Atlantic Africa Time */
684 "adt", 4, 0, 0}, /* Arabia DST */
686 "ast", 3, 0, 0}, /* Arabia */
687 /*{ "ast", 4, 0, 1 }, *//* Atlantic */
689 "bst", 1, 0, 0}, /* British DST */
691 "cat", 1, 0, 0}, /* Central Africa */
694 "cest", 2, 0, 0}, /* Central Europe DST */
696 "cet", 1, 0, 0}, /* Central Europe */
699 /*{ "cst", 8, 0, 0 }, *//* China */
700 /*{ "cst", 9, 30, 0 }, *//* Australian Central Standard Time */
702 "eat", 3, 0, 0}, /* East Africa */
705 "eest", 3, 0, 0}, /* Eastern Europe DST */
707 "eet", 2, 0, 0}, /* Eastern Europe */
709 "egst", 0, 0, 0}, /* Eastern Greenland DST */
711 "egt", 1, 0, 1}, /* Eastern Greenland */
715 "gst", 4, 0, 0}, /* Presian Gulf */
717 "hkt", 8, 0, 0}, /* Hong Kong */
719 "ict", 7, 0, 0}, /* Indochina */
721 "idt", 3, 0, 0}, /* Israel DST */
723 "ist", 2, 0, 0}, /* Israel */
724 /*{ "ist", 5, 30, 0 }, *//* India */
726 "jst", 9, 0, 0}, /* Japan */
728 "kst", 9, 0, 0}, /* Korea */
731 "met", 1, 0, 0}, /* this is now officially CET */
733 "msd", 4, 0, 0}, /* Moscow DST */
735 "msk", 3, 0, 0}, /* Moscow */
738 "nzdt", 13, 0, 0}, /* New Zealand DST */
740 "nzst", 12, 0, 0}, /* New Zealand */
744 "sat", 2, 0, 0}, /* South Africa */
746 "smt", 4, 0, 0}, /* Seychelles */
748 "sst", 11, 0, 1}, /* Samoa */
749 /*{ "sst", 8, 0, 0 }, *//* Singapore */
752 "wat", 0, 0, 0}, /* West Africa */
754 "west", 1, 0, 0}, /* Western Europe DST */
756 "wet", 0, 0, 0}, /* Western Europe */
758 "wgst", 2, 0, 1}, /* Western Greenland DST */
760 "wgt", 3, 0, 1}, /* Western Greenland */
762 "wst", 8, 0, 0}, /* Western Australia */
765 /* parses a date string in RFC822 format:
767 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
769 * This routine assumes that `h' has been initialized to 0. the `timezone'
770 * field is optional, defaulting to +0000 if missing.
772 time_t mutt_parse_date (const char *s, HEADER * h)
784 char tzstr[SHORT_STRING];
785 char scratch[SHORT_STRING];
787 /* Don't modify our argument. Fixed-size buffer is ok here since
788 * the date format imposes a natural limit.
791 m_strcpy(scratch, sizeof(scratch), s);
793 /* kill the day of the week, if it exists. */
794 if ((t = strchr (scratch, ',')))
802 while ((t = strtok (t, " \t")) != NULL) {
804 case 0: /* day of the month */
805 if (!isdigit ((unsigned char) *t))
807 tm.tm_mday = atoi (t);
812 case 1: /* month of the year */
813 if ((i = mutt_check_month (t)) < 0)
819 tm.tm_year = atoi (t);
822 else if (tm.tm_year >= 1900)
826 case 3: /* time of day */
827 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3);
828 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
831 debug_print (1, ("could not process time format: %s\n", t));
839 case 4: /* timezone */
840 /* sometimes we see things like (MST) or (-0700) so attempt to
841 * compensate by uncommenting the string if non-RFC822 compliant
843 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
845 if (*ptz == '+' || *ptz == '-') {
846 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
847 && isdigit ((unsigned char) ptz[1])
848 && isdigit ((unsigned char) ptz[2])
849 && isdigit ((unsigned char) ptz[3])
850 && isdigit ((unsigned char) ptz[4])) {
851 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
852 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
861 tz = bsearch (ptz, TimeZones, sizeof TimeZones / sizeof (struct tz_t),
862 sizeof (struct tz_t),
863 (int (*)(const void *, const void *)) ascii_strcasecmp
864 /* This is safe to do: A pointer to a struct equals
865 * a pointer to its first element*/ );
869 zminutes = tz->zminutes;
870 zoccident = tz->zoccident;
873 /* ad hoc support for the European MET (now officially CET) TZ */
874 if (ascii_strcasecmp (t, "MET") == 0) {
875 if ((t = strtok (NULL, " \t")) != NULL) {
876 if (!ascii_strcasecmp (t, "DST"))
881 tz_offset = zhours * 3600 + zminutes * 60;
883 tz_offset = -tz_offset;
890 if (count < 4) { /* don't check for missing timezone */
891 debug_print (1, ("error parsing date format, using received time\n"));
897 h->zminutes = zminutes;
898 h->zoccident = zoccident;
901 return (mutt_mktime (&tm, 0) + tz_offset);
904 /* extract the first substring that looks like a message-id */
905 static char *extract_message_id(const char *s)
909 if ((s = strchr(s, '<')) == NULL || (p = strchr(s, '>')) == NULL)
911 return p_dupstr(s, (p - s) + 1);
914 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
920 if (cur->content->type != TYPEMESSAGE
921 && cur->content->type != TYPEMULTIPART)
922 break; /* nothing to do */
924 if (cur->content->parts)
925 break; /* The message was parsed earlier. */
927 if ((msg = mx_open_message (ctx, cur->msgno))) {
928 mutt_parse_part (msg->fp, cur->content);
931 cur->security = crypt_query (cur->content);
933 mx_close_message (&msg);
936 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);
939 int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p,
940 short user_hdrs, short weed, short do_2047,
949 switch (ascii_tolower (line[0])) {
951 if (ascii_strcasecmp (line + 1, "pparently-to") == 0) {
952 e->to = rfc822_parse_adrlist (e->to, p);
955 else if (ascii_strcasecmp (line + 1, "pparently-from") == 0) {
956 e->from = rfc822_parse_adrlist (e->from, p);
962 if (ascii_strcasecmp (line + 1, "cc") == 0) {
963 e->bcc = rfc822_parse_adrlist (e->bcc, p);
969 if (ascii_strcasecmp (line + 1, "c") == 0) {
970 e->cc = rfc822_parse_adrlist (e->cc, p);
973 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0) {
974 if (ascii_strcasecmp (line + 8, "type") == 0) {
976 mutt_parse_content_type (p, hdr->content);
979 else if (ascii_strcasecmp (line + 8, "transfer-encoding") == 0) {
981 hdr->content->encoding = mutt_check_encoding (p);
984 else if (ascii_strcasecmp (line + 8, "length") == 0) {
986 if ((hdr->content->length = atoi (p)) < 0)
987 hdr->content->length = -1;
991 else if (ascii_strcasecmp (line + 8, "description") == 0) {
993 m_strreplace(&hdr->content->description, p);
994 rfc2047_decode (&hdr->content->description);
998 else if (ascii_strcasecmp (line + 8, "disposition") == 0) {
1000 parse_content_disposition (p, hdr->content);
1007 if (!ascii_strcasecmp ("ate", line + 1)) {
1008 m_strreplace(&e->date, p);
1010 hdr->date_sent = mutt_parse_date (p, hdr);
1016 if (!ascii_strcasecmp ("xpires", line + 1) &&
1017 hdr && mutt_parse_date (p, NULL) < time (NULL))
1022 if (!ascii_strcasecmp ("rom", line + 1)) {
1023 e->from = rfc822_parse_adrlist (e->from, p);
1024 /* don't leave from info NULL if there's an invalid address (or
1025 * whatever) in From: field; mutt would just display it as empty
1026 * and mark mail/(esp.) news article as your own. aaargh! this
1027 * bothered me for _years_ */
1029 e->from = address_new ();
1030 e->from->personal = m_strdup(p);
1035 else if (!m_strcasecmp(line + 1, "ollowup-to")) {
1036 if (!e->followup_to) {
1037 str_skip_trailws (p);
1038 e->followup_to = m_strdup(vskipspaces(p));
1046 if (!ascii_strcasecmp (line + 1, "n-reply-to")) {
1047 mutt_free_list (&e->in_reply_to);
1048 e->in_reply_to = mutt_parse_references (p, 1);
1054 if (!ascii_strcasecmp (line + 1, "ines")) {
1056 hdr->lines = atoi (p);
1059 * HACK - mutt has, for a very short time, produced negative
1060 * Lines header values. Ignore them.
1068 else if (!ascii_strcasecmp (line + 1, "ist-Post")) {
1069 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1070 if (strncmp (p, "NO", 2)) {
1073 for (beg = strchr (p, '<'); beg; beg = strchr (end, ',')) {
1075 if (!(end = strchr (beg, '>')))
1078 /* Take the first mailto URL */
1079 if (url_check_scheme (beg) == U_MAILTO) {
1080 p_delete(&e->list_post);
1081 e->list_post = str_substrdup (beg, end);
1091 if (!ascii_strcasecmp (line + 1, "ime-version")) {
1096 else if (!ascii_strcasecmp (line + 1, "essage-id")) {
1097 /* We add a new "Message-ID:" when building a message */
1098 p_delete(&e->message_id);
1099 e->message_id = extract_message_id (p);
1102 else if (!ascii_strncasecmp (line + 1, "ail-", 4)) {
1103 if (!ascii_strcasecmp (line + 5, "reply-to")) {
1104 /* override the Reply-To: field */
1105 address_delete (&e->reply_to);
1106 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1109 else if (!ascii_strcasecmp (line + 5, "followup-to")) {
1110 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1118 if (!m_strcasecmp(line + 1, "ewsgroups")) {
1119 p_delete(&e->newsgroups);
1120 str_skip_trailws (p);
1121 e->newsgroups = m_strdup(vskipspaces(p));
1128 /* field `Organization:' saves only for pager! */
1129 if (!m_strcasecmp(line + 1, "rganization")) {
1130 if (!e->organization && m_strcasecmp(p, "unknown"))
1131 e->organization = m_strdup(p);
1136 if (!ascii_strcasecmp (line + 1, "eferences")) {
1137 mutt_free_list (&e->references);
1138 e->references = mutt_parse_references (p, 0);
1141 else if (!ascii_strcasecmp (line + 1, "eply-to")) {
1142 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1145 else if (!ascii_strcasecmp (line + 1, "eturn-path")) {
1146 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1149 else if (!ascii_strcasecmp (line + 1, "eceived")) {
1150 if (hdr && !hdr->received) {
1151 char *d = strchr (p, ';');
1154 hdr->received = mutt_parse_date (d + 1, NULL);
1160 if (!ascii_strcasecmp (line + 1, "ubject")) {
1162 e->subject = m_strdup(p);
1165 else if (!ascii_strcasecmp (line + 1, "ender")) {
1166 e->sender = rfc822_parse_adrlist (e->sender, p);
1169 else if (!ascii_strcasecmp (line + 1, "tatus")) {
1188 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1189 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1190 e->supersedes = m_strdup(p);
1194 if (ascii_strcasecmp (line + 1, "o") == 0) {
1195 e->to = rfc822_parse_adrlist (e->to, p);
1201 if (ascii_strcasecmp (line + 1, "-status") == 0) {
1222 else if (ascii_strcasecmp (line + 1, "-label") == 0) {
1223 e->x_label = m_strdup(p);
1227 else if (!m_strcasecmp(line + 1, "-comment-to")) {
1228 if (!e->x_comment_to)
1229 e->x_comment_to = m_strdup(p);
1232 else if (!m_strcasecmp(line + 1, "ref")) {
1234 e->xref = m_strdup(p);
1243 /* Keep track of the user-defined headers */
1244 if (!matched && user_hdrs) {
1245 /* restore the original line */
1246 line[m_strlen(line)] = ':';
1248 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1249 && !mutt_matches_ignore (line, UnIgnore))
1253 last->next = mutt_new_list ();
1257 last = e->userhdrs = mutt_new_list ();
1258 last->data = m_strdup(line);
1260 rfc2047_decode (&last->data);
1270 /* mutt_read_rfc822_header() -- parses a RFC822 header
1274 * f stream to read from
1276 * hdr header structure of current message (optional).
1278 * user_hdrs If set, store user headers. Used for recall-message and
1281 * weed If this parameter is set and the user has activated the
1282 * $weed option, honor the header weed list for user headers.
1283 * Used for recall-message.
1285 * Returns: newly allocated envelope structure. You should free it by
1286 * mutt_free_envelope() when envelope stay unneeded.
1288 ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs,
1291 ENVELOPE *e = mutt_new_envelope ();
1293 char *line = p_new(char, LONG_STRING);
1297 size_t linelen = LONG_STRING;
1298 char buf[LONG_STRING + 1];
1301 if (hdr->content == NULL) {
1302 hdr->content = mutt_new_body ();
1304 /* set the defaults from RFC1521 */
1305 hdr->content->type = TYPETEXT;
1306 hdr->content->subtype = m_strdup("plain");
1307 hdr->content->encoding = ENC7BIT;
1308 hdr->content->length = -1;
1310 /* RFC 2183 says this is arbitrary */
1311 hdr->content->disposition = DISPINLINE;
1315 while ((loc = ftello (f)),
1316 *(line = mutt_read_rfc822_line (f, line, &linelen)) != 0) {
1319 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') {
1320 char return_path[LONG_STRING];
1323 /* some bogus MTAs will quote the original "From " line */
1324 if (m_strncmp(">From ", line, 6) == 0)
1325 continue; /* just ignore */
1326 else if (is_from (line, return_path, sizeof (return_path), &t)) {
1327 /* MH somtimes has the From_ line in the middle of the header! */
1328 if (hdr && !hdr->received)
1329 hdr->received = t - mutt_local_tz (t);
1334 break; /* end of header */
1339 if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) {
1340 if (!rx_list_match (NoSpamList, line)) {
1342 /* if spam tag already exists, figure out how to amend it */
1343 if (e->spam && *buf) {
1344 /* If SpamSep defined, append with separator */
1346 mutt_buffer_addstr (e->spam, SpamSep);
1347 mutt_buffer_addstr (e->spam, buf);
1350 /* else overwrite */
1352 e->spam->dptr = e->spam->data;
1353 *e->spam->dptr = '\0';
1354 mutt_buffer_addstr (e->spam, buf);
1358 /* spam tag is new, and match expr is non-empty; copy */
1359 else if (!e->spam && *buf) {
1360 e->spam = mutt_buffer_from (NULL, buf);
1363 /* match expr is empty; plug in null string if no existing tag */
1364 else if (!e->spam) {
1365 e->spam = mutt_buffer_from (NULL, "");
1368 if (e->spam && e->spam->data)
1369 debug_print (5, ("spam = %s\n", e->spam->data));
1376 continue; /* skip empty header fields */
1379 mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1386 hdr->content->hdr_offset = hdr->offset;
1387 hdr->content->offset = ftello (f);
1388 rfc2047_decode_envelope (e);
1389 /* check for missing or invalid date */
1390 if (hdr->date_sent <= 0) {
1391 debug_print (1, ("no date found, using received "
1392 "time from msg separator\n"));
1393 hdr->date_sent = hdr->received;
1400 address_t *mutt_parse_adrlist (address_t * p, const char *s)
1404 /* check for a simple whitespace separated list of addresses */
1405 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) {
1406 char tmp[HUGE_STRING];
1409 m_strcpy(tmp, sizeof(tmp), s);
1411 while ((r = strtok (r, " \t")) != NULL) {
1412 p = rfc822_parse_adrlist (p, r);
1417 p = rfc822_parse_adrlist (p, s);
1423 /* Compares mime types to the ok and except lists */
1424 int count_body_parts_check(LIST **checklist, BODY *b, int dflt) {
1428 /* If list is null, use default behavior. */
1434 for (type = *checklist; type; type = type->next) {
1435 a = (ATTACH_MATCH *)type->data;
1436 debug_print(5, ("cbpc: %s %d/%s ?? %s/%s [%d]... ",
1437 dflt ? "[OK] " : "[EXCL] ",
1438 b->type, b->subtype, a->major, a->minor, a->major_int));
1439 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1440 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) {
1441 debug_print(5, ("yes\n"));
1444 debug_print(5, ("no\n"));
1450 #define AT_COUNT(why) { shallcount = 1; }
1451 #define AT_NOCOUNT(why) { shallcount = 0; }
1453 int count_body_parts (BODY *body, int flags) {
1455 int shallcount, shallrecurse;
1461 for (bp = body; bp != NULL; bp = bp->next) {
1462 /* Initial disposition is to count and not to recurse this part. */
1463 AT_COUNT("default");
1466 debug_print(5, ("bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1467 bp->description ? bp->description : ("none"),
1468 bp->filename ? bp->filename :
1469 bp->d_filename ? bp->d_filename : "(none)",
1470 bp->type, bp->subtype ? bp->subtype : "*"));
1472 if (bp->type == TYPEMESSAGE) {
1475 /* If it's an external body pointer, don't recurse it. */
1476 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1479 /* Don't count containers if they're top-level. */
1480 if (flags & M_PARTS_TOPLEVEL)
1481 AT_NOCOUNT("top-level message/*");
1482 } else if (bp->type == TYPEMULTIPART) {
1483 /* Always recurse multiparts, except multipart/alternative. */
1485 if (!m_strcasecmp(bp->subtype, "alternative"))
1488 /* Don't count containers if they're top-level. */
1489 if (flags & M_PARTS_TOPLEVEL)
1490 AT_NOCOUNT("top-level multipart");
1493 if (bp->disposition == DISPINLINE &&
1494 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1495 AT_NOCOUNT("ignore fundamental inlines");
1497 /* If this body isn't scheduled for enumeration already, don't bother
1498 * profiling it further. */
1501 /* Turn off shallcount if message type is not in ok list,
1502 * or if it is in except list. Check is done separately for
1503 * inlines vs. attachments.
1506 if (bp->disposition == DISPATTACH) {
1507 if (!count_body_parts_check(&AttachAllow, bp, 1))
1508 AT_NOCOUNT("attach not allowed");
1509 if (count_body_parts_check(&AttachExclude, bp, 0))
1510 AT_NOCOUNT("attach excluded");
1512 if (!count_body_parts_check(&InlineAllow, bp, 1))
1513 AT_NOCOUNT("inline not allowed");
1514 if (count_body_parts_check(&InlineExclude, bp, 0))
1515 AT_NOCOUNT("excluded");
1521 bp->attach_qualifies = shallcount ? 1 : 0;
1523 debug_print(5, ("cbp: %p shallcount = %d\n", bp, shallcount));
1526 debug_print(5, ("cbp: %p pre count = %d\n", bp, count));
1527 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1528 count += bp->attach_count;
1529 debug_print(5, ("cbp: %p post count = %d\n", bp, count));
1533 debug_print(5, ("bp: return %d\n", count < 0 ? 0 : count));
1534 return count < 0 ? 0 : count;
1537 int mutt_count_body_parts (HEADER *hdr, int flags) {
1538 if (!option (OPTCOUNTATTACH))
1540 if (hdr->attach_valid && !(flags & M_PARTS_RECOUNT))
1541 return hdr->attach_total;
1543 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1544 hdr->attach_total = count_body_parts(hdr->content, flags | M_PARTS_TOPLEVEL);
1546 hdr->attach_total = 0;
1548 hdr->attach_valid = 1;
1549 return hdr->attach_total;