1 /*
2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #include "mutt_regex.h"
25 #include "mailbox.h"
26 #include "mime.h"
27 #include "rfc2047.h"
28 #include "rfc2231.h"
29 #include "mutt_crypt.h"
30 #include "url.h"
31
32 #include <string.h>
33 #include <ctype.h>
34 #include <sys/stat.h>
35 #include <stdlib.h>
36
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.
40 */
mutt_read_rfc822_line(FILE * f,char * line,size_t * linelen)41 char *mutt_read_rfc822_line (FILE *f, char *line, size_t *linelen)
42 {
43 char *buf = line;
44 int ch;
45 size_t offset = 0;
46
47 FOREVER
48 {
49 if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */
50 (ISSPACE (*line) && !offset)) /* end of headers */
51 {
52 *line = 0;
53 return (line);
54 }
55
56 buf += strlen (buf) - 1;
57 if (*buf == '\n')
58 {
59 /* we did get a full line. remove trailing space */
60 while (ISSPACE (*buf))
61 *buf-- = 0; /* we cannot come beyond line's beginning because
62 * it begins with a non-space */
63
64 /* check to see if the next line is a continuation line */
65 if ((ch = fgetc (f)) != ' ' && ch != '\t')
66 {
67 ungetc (ch, f);
68 return (line); /* next line is a separate header field or EOH */
69 }
70
71 /* eat tabs and spaces from the beginning of the continuation line */
72 while ((ch = fgetc (f)) == ' ' || ch == '\t')
73 ;
74 ungetc (ch, f);
75 *++buf = ' '; /* string is still terminated because we removed
76 at least one whitespace char above */
77 }
78
79 buf++;
80 offset = buf - line;
81 if (*linelen < offset + STRING)
82 {
83 /* grow the buffer */
84 *linelen += STRING;
85 safe_realloc (&line, *linelen);
86 buf = line + offset;
87 }
88 }
89 /* not reached */
90 }
91
mutt_parse_references(char * s,int in_reply_to)92 static LIST *mutt_parse_references (char *s, int in_reply_to)
93 {
94 LIST *t, *lst = NULL;
95 char *m;
96 const char *sp;
97
98 m = mutt_extract_message_id (s, &sp);
99 while (m)
100 {
101 t = safe_malloc (sizeof (LIST));
102 t->data = m;
103 t->next = lst;
104 lst = t;
105
106 m = mutt_extract_message_id (NULL, &sp);
107 }
108
109 return lst;
110 }
111
mutt_check_encoding(const char * c)112 int mutt_check_encoding (const char *c)
113 {
114 if (ascii_strncasecmp ("7bit", c, sizeof ("7bit")-1) == 0)
115 return (ENC7BIT);
116 else if (ascii_strncasecmp ("8bit", c, sizeof ("8bit")-1) == 0)
117 return (ENC8BIT);
118 else if (ascii_strncasecmp ("binary", c, sizeof ("binary")-1) == 0)
119 return (ENCBINARY);
120 else if (ascii_strncasecmp ("quoted-printable", c, sizeof ("quoted-printable")-1) == 0)
121 return (ENCQUOTEDPRINTABLE);
122 else if (ascii_strncasecmp ("base64", c, sizeof("base64")-1) == 0)
123 return (ENCBASE64);
124 else if (ascii_strncasecmp ("x-uuencode", c, sizeof("x-uuencode")-1) == 0)
125 return (ENCUUENCODED);
126 #ifdef SUN_ATTACHMENT
127 else if (ascii_strncasecmp ("uuencode", c, sizeof("uuencode")-1) == 0)
128 return (ENCUUENCODED);
129 #endif
130 else
131 return (ENCOTHER);
132 }
133
parse_parameters(const char * s)134 static PARAMETER *parse_parameters (const char *s)
135 {
136 PARAMETER *head = 0, *cur = 0, *new;
137 char buffer[LONG_STRING];
138 const char *p;
139 size_t i;
140
141 dprint (2, (debugfile, "parse_parameters: `%s'\n", s));
142
143 while (*s)
144 {
145 if ((p = strpbrk (s, "=;")) == NULL)
146 {
147 dprint(1, (debugfile, "parse_parameters: malformed parameter: %s\n", s));
148 goto bail;
149 }
150
151 /* if we hit a ; now the parameter has no value, just skip it */
152 if (*p != ';')
153 {
154 i = p - s;
155
156 new = mutt_new_parameter ();
157
158 new->attribute = safe_malloc (i + 1);
159 memcpy (new->attribute, s, i);
160 new->attribute[i] = 0;
161
162 /* remove whitespace from the end of the attribute name */
163 while (ISSPACE (new->attribute[--i]))
164 new->attribute[i] = 0;
165
166 s = p + 1; /* skip over the = */
167 SKIPWS (s);
168
169 if (*s == '"')
170 {
171 int state_ascii = 1;
172 s++;
173 for (i=0; *s && i < sizeof (buffer) - 1; i++, s++)
174 {
175 if (AssumedCharset && *AssumedCharset) {
176 /* As iso-2022-* has a characer of '"' with non-ascii state,
177 * ignore it. */
178 if (*s == 0x1b && i < sizeof (buffer) - 2)
179 {
180 if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J'))
181 state_ascii = 1;
182 else
183 state_ascii = 0;
184 }
185 }
186 if (state_ascii && *s == '"')
187 break;
188 if (*s == '\\')
189 {
190 /* Quote the next character */
191 buffer[i] = s[1];
192 if (!*++s)
193 break;
194 }
195 else
196 buffer[i] = *s;
197 }
198 buffer[i] = 0;
199 if (*s)
200 s++; /* skip over the " */
201 }
202 else
203 {
204 for (i=0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1; i++, s++)
205 buffer[i] = *s;
206 buffer[i] = 0;
207 }
208
209 new->value = safe_strdup (buffer);
210
211 dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n",
212 new->attribute ? new->attribute : "",
213 new->value ? new->value : ""));
214
215 /* Add this parameter to the list */
216 if (head)
217 {
218 cur->next = new;
219 cur = cur->next;
220 }
221 else
222 head = cur = new;
223 }
224 else
225 {
226 dprint (1, (debugfile, "parse_parameters(): parameter with no value: %s\n", s));
227 s = p;
228 }
229
230 /* Find the next parameter */
231 if (*s != ';' && (s = strchr (s, ';')) == NULL)
232 break; /* no more parameters */
233
234 do
235 {
236 s++;
237
238 /* Move past any leading whitespace */
239 SKIPWS (s);
240 }
241 while (*s == ';'); /* skip empty parameters */
242 }
243
244 bail:
245
246 rfc2231_decode_parameters (&head);
247 return (head);
248 }
249
mutt_check_mime_type(const char * s)250 int mutt_check_mime_type (const char *s)
251 {
252 if (ascii_strcasecmp ("text", s) == 0)
253 return TYPETEXT;
254 else if (ascii_strcasecmp ("multipart", s) == 0)
255 return TYPEMULTIPART;
256 #ifdef SUN_ATTACHMENT
257 else if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
258 return TYPEMULTIPART;
259 #endif
260 else if (ascii_strcasecmp ("application", s) == 0)
261 return TYPEAPPLICATION;
262 else if (ascii_strcasecmp ("message", s) == 0)
263 return TYPEMESSAGE;
264 else if (ascii_strcasecmp ("image", s) == 0)
265 return TYPEIMAGE;
266 else if (ascii_strcasecmp ("audio", s) == 0)
267 return TYPEAUDIO;
268 else if (ascii_strcasecmp ("video", s) == 0)
269 return TYPEVIDEO;
270 else if (ascii_strcasecmp ("model", s) == 0)
271 return TYPEMODEL;
272 else if (ascii_strcasecmp ("*", s) == 0)
273 return TYPEANY;
274 else if (ascii_strcasecmp (".*", s) == 0)
275 return TYPEANY;
276 else
277 return TYPEOTHER;
278 }
279
mutt_parse_content_type(char * s,BODY * ct)280 void mutt_parse_content_type (char *s, BODY *ct)
281 {
282 char *pc;
283 char *subtype;
284
285 FREE (&ct->subtype);
286 mutt_free_parameter(&ct->parameter);
287
288 /* First extract any existing parameters */
289 if ((pc = strchr(s, ';')) != NULL)
290 {
291 *pc++ = 0;
292 while (*pc && ISSPACE (*pc))
293 pc++;
294 ct->parameter = parse_parameters(pc);
295
296 /* Some pre-RFC1521 gateways still use the "name=filename" convention,
297 * but if a filename has already been set in the content-disposition,
298 * let that take precedence, and don't set it here */
299 if ((pc = mutt_get_parameter( "name", ct->parameter)) && !ct->filename)
300 ct->filename = safe_strdup(pc);
301
302 #ifdef SUN_ATTACHMENT
303 /* this is deep and utter perversion */
304 if ((pc = mutt_get_parameter ("conversions", ct->parameter)))
305 ct->encoding = mutt_check_encoding (pc);
306 #endif
307
308 }
309
310 /* Now get the subtype */
311 if ((subtype = strchr(s, '/')))
312 {
313 *subtype++ = '\0';
314 for(pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++)
315 ;
316 *pc = '\0';
317 ct->subtype = safe_strdup (subtype);
318 }
319
320 /* Finally, get the major type */
321 ct->type = mutt_check_mime_type (s);
322
323 #ifdef SUN_ATTACHMENT
324 if (ascii_strcasecmp ("x-sun-attachment", s) == 0)
325 ct->subtype = safe_strdup ("x-sun-attachment");
326 #endif
327
328 if (ct->type == TYPEOTHER)
329 {
330 ct->xtype = safe_strdup (s);
331 }
332
333 if (ct->subtype == NULL)
334 {
335 /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type
336 * field, so we can attempt to convert the type to BODY here.
337 */
338 if (ct->type == TYPETEXT)
339 ct->subtype = safe_strdup ("plain");
340 else if (ct->type == TYPEAUDIO)
341 ct->subtype = safe_strdup ("basic");
342 else if (ct->type == TYPEMESSAGE)
343 ct->subtype = safe_strdup ("rfc822");
344 else if (ct->type == TYPEOTHER)
345 {
346 char buffer[SHORT_STRING];
347
348 ct->type = TYPEAPPLICATION;
349 snprintf (buffer, sizeof (buffer), "x-%s", s);
350 ct->subtype = safe_strdup (buffer);
351 }
352 else
353 ct->subtype = safe_strdup ("x-unknown");
354 }
355
356 /* Default character set for text types. */
357 if (ct->type == TYPETEXT)
358 {
359 if (!(pc = mutt_get_parameter ("charset", ct->parameter)))
360 mutt_set_parameter ("charset", (AssumedCharset && *AssumedCharset) ?
361 (const char *) mutt_get_default_charset ()
362 : "us-ascii", &ct->parameter);
363 }
364
365 }
366
parse_content_disposition(char * s,BODY * ct)367 static void parse_content_disposition (char *s, BODY *ct)
368 {
369 PARAMETER *parms;
370
371 if (!ascii_strncasecmp ("inline", s, 6))
372 ct->disposition = DISPINLINE;
373 else if (!ascii_strncasecmp ("form-data", s, 9))
374 ct->disposition = DISPFORMDATA;
375 else
376 ct->disposition = DISPATTACH;
377
378 /* Check to see if a default filename was given */
379 if ((s = strchr (s, ';')) != NULL)
380 {
381 s++;
382 SKIPWS (s);
383 if ((s = mutt_get_parameter ("filename", (parms = parse_parameters (s)))))
384 mutt_str_replace (&ct->filename, s);
385 if ((s = mutt_get_parameter ("name", parms)))
386 ct->form_name = safe_strdup (s);
387 mutt_free_parameter (&parms);
388 }
389 }
390
391 /* args:
392 * fp stream to read from
393 *
394 * digest 1 if reading subparts of a multipart/digest, 0
395 * otherwise
396 */
397
mutt_read_mime_header(FILE * fp,int digest)398 BODY *mutt_read_mime_header (FILE *fp, int digest)
399 {
400 BODY *p = mutt_new_body();
401 char *c;
402 char *line = safe_malloc (LONG_STRING);
403 size_t linelen = LONG_STRING;
404
405 p->hdr_offset = ftello (fp);
406
407 p->encoding = ENC7BIT; /* default from RFC1521 */
408 p->type = digest ? TYPEMESSAGE : TYPETEXT;
409 p->disposition = DISPINLINE;
410
411 while (*(line = mutt_read_rfc822_line (fp, line, &linelen)) != 0)
412 {
413 /* Find the value of the current header */
414 if ((c = strchr (line, ':')))
415 {
416 *c = 0;
417 c++;
418 SKIPWS (c);
419 if (!*c)
420 {
421 dprint (1, (debugfile, "mutt_read_mime_header(): skipping empty header field: %s\n", line));
422 continue;
423 }
424 }
425 else
426 {
427 dprint (1, (debugfile, "read_mime_header: bogus MIME header: %s\n", line));
428 break;
429 }
430
431 if (!ascii_strncasecmp ("content-", line, 8))
432 {
433 if (!ascii_strcasecmp ("type", line + 8))
434 mutt_parse_content_type (c, p);
435 else if (!ascii_strcasecmp ("transfer-encoding", line + 8))
436 p->encoding = mutt_check_encoding (c);
437 else if (!ascii_strcasecmp ("disposition", line + 8))
438 parse_content_disposition (c, p);
439 else if (!ascii_strcasecmp ("description", line + 8))
440 {
441 mutt_str_replace (&p->description, c);
442 rfc2047_decode (&p->description);
443 }
444 }
445 #ifdef SUN_ATTACHMENT
446 else if (!ascii_strncasecmp ("x-sun-", line, 6))
447 {
448 if (!ascii_strcasecmp ("data-type", line + 6))
449 mutt_parse_content_type (c, p);
450 else if (!ascii_strcasecmp ("encoding-info", line + 6))
451 p->encoding = mutt_check_encoding (c);
452 else if (!ascii_strcasecmp ("content-lines", line + 6))
453 mutt_set_parameter ("content-lines", c, &(p->parameter));
454 else if (!ascii_strcasecmp ("data-description", line + 6))
455 {
456 mutt_str_replace (&p->description, c);
457 rfc2047_decode (&p->description);
458 }
459 }
460 #endif
461 }
462 p->offset = ftello (fp); /* Mark the start of the real data */
463 if (p->type == TYPETEXT && !p->subtype)
464 p->subtype = safe_strdup ("plain");
465 else if (p->type == TYPEMESSAGE && !p->subtype)
466 p->subtype = safe_strdup ("rfc822");
467
468 FREE (&line);
469
470 return (p);
471 }
472
mutt_parse_part(FILE * fp,BODY * b)473 void mutt_parse_part (FILE *fp, BODY *b)
474 {
475 char *bound = 0;
476
477 switch (b->type)
478 {
479 case TYPEMULTIPART:
480 #ifdef SUN_ATTACHMENT
481 if ( !ascii_strcasecmp (b->subtype, "x-sun-attachment") )
482 bound = "--------";
483 else
484 #endif
485 bound = mutt_get_parameter ("boundary", b->parameter);
486
487 fseeko (fp, b->offset, SEEK_SET);
488 b->parts = mutt_parse_multipart (fp, bound,
489 b->offset + b->length,
490 ascii_strcasecmp ("digest", b->subtype) == 0);
491 break;
492
493 case TYPEMESSAGE:
494 if (b->subtype)
495 {
496 fseeko (fp, b->offset, SEEK_SET);
497 if (mutt_is_message_type(b->type, b->subtype))
498 b->parts = mutt_parse_messageRFC822 (fp, b);
499 else if (ascii_strcasecmp (b->subtype, "external-body") == 0)
500 b->parts = mutt_read_mime_header (fp, 0);
501 else
502 return;
503 }
504 break;
505
506 default:
507 return;
508 }
509
510 /* try to recover from parsing error */
511 if (!b->parts)
512 {
513 b->type = TYPETEXT;
514 mutt_str_replace (&b->subtype, "plain");
515 }
516 }
517
518 /* parse a MESSAGE/RFC822 body
519 *
520 * args:
521 * fp stream to read from
522 *
523 * parent structure which contains info about the message/rfc822
524 * body part
525 *
526 * NOTE: this assumes that `parent->length' has been set!
527 */
528
mutt_parse_messageRFC822(FILE * fp,BODY * parent)529 BODY *mutt_parse_messageRFC822 (FILE *fp, BODY *parent)
530 {
531 BODY *msg;
532
533 parent->hdr = mutt_new_header ();
534 parent->hdr->offset = ftello (fp);
535 parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0);
536 msg = parent->hdr->content;
537
538 /* ignore the length given in the content-length since it could be wrong
539 and we already have the info to calculate the correct length */
540 /* if (msg->length == -1) */
541 msg->length = parent->length - (msg->offset - parent->offset);
542
543 /* if body of this message is empty, we can end up with a negative length */
544 if (msg->length < 0)
545 msg->length = 0;
546
547 mutt_parse_part(fp, msg);
548 return (msg);
549 }
550
551 /* parse a multipart structure
552 *
553 * args:
554 * fp stream to read from
555 *
556 * boundary body separator
557 *
558 * end_off length of the multipart body (used when the final
559 * boundary is missing to avoid reading too far)
560 *
561 * digest 1 if reading a multipart/digest, 0 otherwise
562 */
563
mutt_parse_multipart(FILE * fp,const char * boundary,LOFF_T end_off,int digest)564 BODY *mutt_parse_multipart (FILE *fp, const char *boundary, LOFF_T end_off, int digest)
565 {
566 #ifdef SUN_ATTACHMENT
567 int lines;
568 #endif
569 int blen, len, crlf = 0;
570 char buffer[LONG_STRING];
571 BODY *head = 0, *last = 0, *new = 0;
572 int i;
573 int final = 0; /* did we see the ending boundary? */
574
575 if (!boundary)
576 {
577 mutt_error _("multipart message has no boundary parameter!");
578 return (NULL);
579 }
580
581 blen = mutt_strlen (boundary);
582 while (ftello (fp) < end_off && fgets (buffer, LONG_STRING, fp) != NULL)
583 {
584 len = mutt_strlen (buffer);
585
586 crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0;
587
588 if (buffer[0] == '-' && buffer[1] == '-' &&
589 mutt_strncmp (buffer + 2, boundary, blen) == 0)
590 {
591 if (last)
592 {
593 last->length = ftello (fp) - last->offset - len - 1 - crlf;
594 if (last->parts && last->parts->length == 0)
595 last->parts->length = ftello (fp) - last->parts->offset - len - 1 - crlf;
596 /* if the body is empty, we can end up with a -1 length */
597 if (last->length < 0)
598 last->length = 0;
599 }
600
601 /* Remove any trailing whitespace, up to the length of the boundary */
602 for (i = len - 1; ISSPACE (buffer[i]) && i >= blen + 2; i--)
603 buffer[i] = 0;
604
605 /* Check for the end boundary */
606 if (mutt_strcmp (buffer + blen + 2, "--") == 0)
607 {
608 final = 1;
609 break; /* done parsing */
610 }
611 else if (buffer[2 + blen] == 0)
612 {
613 new = mutt_read_mime_header (fp, digest);
614
615 #ifdef SUN_ATTACHMENT
616 if (mutt_get_parameter ("content-lines", new->parameter)) {
617 mutt_atoi (mutt_get_parameter ("content-lines", new->parameter), &lines);
618 for ( ; lines; lines-- )
619 if (ftello (fp) >= end_off || fgets (buffer, LONG_STRING, fp) == NULL)
620 break;
621 }
622 #endif
623
624 /*
625 * Consistency checking - catch
626 * bad attachment end boundaries
627 */
628
629 if(new->offset > end_off)
630 {
631 mutt_free_body(&new);
632 break;
633 }
634 if (head)
635 {
636 last->next = new;
637 last = new;
638 }
639 else
640 last = head = new;
641 }
642 }
643 }
644
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;
648
649 /* parse recursive MIME parts */
650 for(last = head; last; last = last->next)
651 mutt_parse_part(fp, last);
652
653 return (head);
654 }
655
uncomment_timezone(char * buf,size_t buflen,const char * tz)656 static const char *uncomment_timezone (char *buf, size_t buflen, const char *tz)
657 {
658 char *p;
659 size_t len;
660
661 if (*tz != '(')
662 return tz; /* no need to do anything */
663 tz++;
664 SKIPWS (tz);
665 if ((p = strpbrk (tz, " )")) == NULL)
666 return tz;
667 len = p - tz;
668 if (len > buflen - 1)
669 len = buflen - 1;
670 memcpy (buf, tz, len);
671 buf[len] = 0;
672 return buf;
673 }
674
675 static struct tz_t
676 {
677 char tzname[5];
678 unsigned char zhours;
679 unsigned char zminutes;
680 unsigned char zoccident; /* west of UTC? */
681 }
682 TimeZones[] =
683 {
684 { "aat", 1, 0, 1 }, /* Atlantic Africa Time */
685 { "adt", 4, 0, 0 }, /* Arabia DST */
686 { "ast", 3, 0, 0 }, /* Arabia */
687 /*{ "ast", 4, 0, 1 },*/ /* Atlantic */
688 { "bst", 1, 0, 0 }, /* British DST */
689 { "cat", 1, 0, 0 }, /* Central Africa */
690 { "cdt", 5, 0, 1 },
691 { "cest", 2, 0, 0 }, /* Central Europe DST */
692 { "cet", 1, 0, 0 }, /* Central Europe */
693 { "cst", 6, 0, 1 },
694 /*{ "cst", 8, 0, 0 },*/ /* China */
695 /*{ "cst", 9, 30, 0 },*/ /* Australian Central Standard Time */
696 { "eat", 3, 0, 0 }, /* East Africa */
697 { "edt", 4, 0, 1 },
698 { "eest", 3, 0, 0 }, /* Eastern Europe DST */
699 { "eet", 2, 0, 0 }, /* Eastern Europe */
700 { "egst", 0, 0, 0 }, /* Eastern Greenland DST */
701 { "egt", 1, 0, 1 }, /* Eastern Greenland */
702 { "est", 5, 0, 1 },
703 { "gmt", 0, 0, 0 },
704 { "gst", 4, 0, 0 }, /* Presian Gulf */
705 { "hkt", 8, 0, 0 }, /* Hong Kong */
706 { "ict", 7, 0, 0 }, /* Indochina */
707 { "idt", 3, 0, 0 }, /* Israel DST */
708 { "ist", 2, 0, 0 }, /* Israel */
709 /*{ "ist", 5, 30, 0 },*/ /* India */
710 { "jst", 9, 0, 0 }, /* Japan */
711 { "kst", 9, 0, 0 }, /* Korea */
712 { "mdt", 6, 0, 1 },
713 { "met", 1, 0, 0 }, /* this is now officially CET */
714 { "msd", 4, 0, 0 }, /* Moscow DST */
715 { "msk", 3, 0, 0 }, /* Moscow */
716 { "mst", 7, 0, 1 },
717 { "nzdt", 13, 0, 0 }, /* New Zealand DST */
718 { "nzst", 12, 0, 0 }, /* New Zealand */
719 { "pdt", 7, 0, 1 },
720 { "pst", 8, 0, 1 },
721 { "sat", 2, 0, 0 }, /* South Africa */
722 { "smt", 4, 0, 0 }, /* Seychelles */
723 { "sst", 11, 0, 1 }, /* Samoa */
724 /*{ "sst", 8, 0, 0 },*/ /* Singapore */
725 { "utc", 0, 0, 0 },
726 { "wat", 0, 0, 0 }, /* West Africa */
727 { "west", 1, 0, 0 }, /* Western Europe DST */
728 { "wet", 0, 0, 0 }, /* Western Europe */
729 { "wgst", 2, 0, 1 }, /* Western Greenland DST */
730 { "wgt", 3, 0, 1 }, /* Western Greenland */
731 { "wst", 8, 0, 0 }, /* Western Australia */
732 };
733
734 /* parses a date string in RFC822 format:
735 *
736 * Date: [ weekday , ] day-of-month month year hour:minute:second timezone
737 *
738 * This routine assumes that `h' has been initialized to 0. the `timezone'
739 * field is optional, defaulting to +0000 if missing.
740 */
mutt_parse_date(const char * s,HEADER * h)741 time_t mutt_parse_date (const char *s, HEADER *h)
742 {
743 int count = 0;
744 char *t;
745 int hour, min, sec;
746 struct tm tm;
747 int i;
748 int tz_offset = 0;
749 int zhours = 0;
750 int zminutes = 0;
751 int zoccident = 0;
752 const char *ptz;
753 char tzstr[SHORT_STRING];
754 char scratch[SHORT_STRING];
755
756 /* Don't modify our argument. Fixed-size buffer is ok here since
757 * the date format imposes a natural limit.
758 */
759
760 strfcpy (scratch, s, sizeof (scratch));
761
762 /* kill the day of the week, if it exists. */
763 if ((t = strchr (scratch, ',')))
764 t++;
765 else
766 t = scratch;
767 SKIPWS (t);
768
769 memset (&tm, 0, sizeof (tm));
770
771 while ((t = strtok (t, " \t")) != NULL)
772 {
773 switch (count)
774 {
775 case 0: /* day of the month */
776 if (mutt_atoi (t, &tm.tm_mday) < 0 || tm.tm_mday < 0)
777 return (-1);
778 if (tm.tm_mday > 31)
779 return (-1);
780 break;
781
782 case 1: /* month of the year */
783 if ((i = mutt_check_month (t)) < 0)
784 return (-1);
785 tm.tm_mon = i;
786 break;
787
788 case 2: /* year */
789 if (mutt_atoi (t, &tm.tm_year) < 0 || tm.tm_year < 0)
790 return (-1);
791 if (tm.tm_year < 50)
792 tm.tm_year += 100;
793 else if (tm.tm_year >= 1900)
794 tm.tm_year -= 1900;
795 break;
796
797 case 3: /* time of day */
798 if (sscanf (t, "%d:%d:%d", &hour, &min, &sec) == 3)
799 ;
800 else if (sscanf (t, "%d:%d", &hour, &min) == 2)
801 sec = 0;
802 else
803 {
804 dprint(1, (debugfile, "parse_date: could not process time format: %s\n", t));
805 return(-1);
806 }
807 tm.tm_hour = hour;
808 tm.tm_min = min;
809 tm.tm_sec = sec;
810 break;
811
812 case 4: /* timezone */
813 /* sometimes we see things like (MST) or (-0700) so attempt to
814 * compensate by uncommenting the string if non-RFC822 compliant
815 */
816 ptz = uncomment_timezone (tzstr, sizeof (tzstr), t);
817
818 if (*ptz == '+' || *ptz == '-')
819 {
820 if (ptz[1] && ptz[2] && ptz[3] && ptz[4]
821 && isdigit ((unsigned char) ptz[1]) && isdigit ((unsigned char) ptz[2])
822 && isdigit ((unsigned char) ptz[3]) && isdigit ((unsigned char) ptz[4]))
823 {
824 zhours = (ptz[1] - '0') * 10 + (ptz[2] - '0');
825 zminutes = (ptz[3] - '0') * 10 + (ptz[4] - '0');
826
827 if (ptz[0] == '-')
828 zoccident = 1;
829 }
830 }
831 else
832 {
833 struct tz_t *tz;
834
835 tz = bsearch (ptz, TimeZones, sizeof TimeZones/sizeof (struct tz_t),
836 sizeof (struct tz_t),
837 (int (*)(const void *, const void *)) ascii_strcasecmp
838 /* This is safe to do: A pointer to a struct equals
839 * a pointer to its first element*/);
840
841 if (tz)
842 {
843 zhours = tz->zhours;
844 zminutes = tz->zminutes;
845 zoccident = tz->zoccident;
846 }
847
848 /* ad hoc support for the European MET (now officially CET) TZ */
849 if (ascii_strcasecmp (t, "MET") == 0)
850 {
851 if ((t = strtok (NULL, " \t")) != NULL)
852 {
853 if (!ascii_strcasecmp (t, "DST"))
854 zhours++;
855 }
856 }
857 }
858 tz_offset = zhours * 3600 + zminutes * 60;
859 if (!zoccident)
860 tz_offset = -tz_offset;
861 break;
862 }
863 count++;
864 t = 0;
865 }
866
867 if (count < 4) /* don't check for missing timezone */
868 {
869 dprint(1,(debugfile, "parse_date(): error parsing date format, using received time\n"));
870 return (-1);
871 }
872
873 if (h)
874 {
875 h->zhours = zhours;
876 h->zminutes = zminutes;
877 h->zoccident = zoccident;
878 }
879
880 return (mutt_mktime (&tm, 0) + tz_offset);
881 }
882
883 /* extract the first substring that looks like a message-id.
884 * call back with NULL for more (like strtok).
885 */
mutt_extract_message_id(const char * s,const char ** saveptr)886 char *mutt_extract_message_id (const char *s, const char **saveptr)
887 {
888 const char *o, *onull, *p;
889 char *ret = NULL;
890
891 if (s)
892 p = s;
893 else if (saveptr)
894 p = *saveptr;
895 else
896 return NULL;
897
898 for (s = NULL, o = NULL, onull = NULL;
899 (p = strpbrk (p, "<> \t;")) != NULL; ++p)
900 {
901 if (*p == '<')
902 {
903 s = p;
904 o = onull = NULL;
905 continue;
906 }
907
908 if (!s)
909 continue;
910
911 if (*p == '>')
912 {
913 size_t olen = onull - o, slen = p - s + 1;
914 ret = safe_malloc (olen + slen + 1);
915 if (o)
916 memcpy (ret, o, olen);
917 memcpy (ret + olen, s, slen);
918 ret[olen + slen] = '\0';
919 if (saveptr)
920 *saveptr = p + 1; /* next call starts after '>' */
921 return ret;
922 }
923
924 /* some idiotic clients break their message-ids between lines */
925 if (s == p)
926 /* step past another whitespace */
927 s = p + 1;
928 else if (o)
929 /* more than two lines, give up */
930 s = o = onull = NULL;
931 else
932 {
933 /* remember the first line, start looking for the second */
934 o = s;
935 onull = p;
936 s = p + 1;
937 }
938 }
939
940 return NULL;
941 }
942
mutt_parse_mime_message(CONTEXT * ctx,HEADER * cur)943 void mutt_parse_mime_message (CONTEXT *ctx, HEADER *cur)
944 {
945 MESSAGE *msg;
946
947 do {
948 if (cur->content->type != TYPEMESSAGE &&
949 cur->content->type != TYPEMULTIPART)
950 break; /* nothing to do */
951
952 if (cur->content->parts)
953 break; /* The message was parsed earlier. */
954
955 if ((msg = mx_open_message (ctx, cur->msgno)))
956 {
957 mutt_parse_part (msg->fp, cur->content);
958
959 if (WithCrypto)
960 cur->security = crypt_query (cur->content);
961
962 mx_close_message (&msg);
963 }
964 } while (0);
965
966 cur->attach_valid = 0;
967 }
968
mutt_parse_rfc822_line(ENVELOPE * e,HEADER * hdr,char * line,char * p,short user_hdrs,short weed,short do_2047,LIST ** lastp)969 int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short user_hdrs, short weed,
970 short do_2047, LIST **lastp)
971 {
972 int matched = 0;
973 LIST *last = NULL;
974
975 if (lastp)
976 last = *lastp;
977
978 switch (ascii_tolower (line[0]))
979 {
980 case 'a':
981 if (ascii_strcasecmp (line+1, "pparently-to") == 0)
982 {
983 e->to = rfc822_parse_adrlist (e->to, p);
984 matched = 1;
985 }
986 else if (ascii_strcasecmp (line+1, "pparently-from") == 0)
987 {
988 e->from = rfc822_parse_adrlist (e->from, p);
989 matched = 1;
990 }
991 break;
992
993 case 'b':
994 if (ascii_strcasecmp (line+1, "cc") == 0)
995 {
996 e->bcc = rfc822_parse_adrlist (e->bcc, p);
997 matched = 1;
998 }
999 break;
1000
1001 case 'c':
1002 if (ascii_strcasecmp (line+1, "c") == 0)
1003 {
1004 e->cc = rfc822_parse_adrlist (e->cc, p);
1005 matched = 1;
1006 }
1007 else if (ascii_strncasecmp (line + 1, "ontent-", 7) == 0)
1008 {
1009 if (ascii_strcasecmp (line+8, "type") == 0)
1010 {
1011 if (hdr)
1012 mutt_parse_content_type (p, hdr->content);
1013 matched = 1;
1014 }
1015 else if (ascii_strcasecmp (line+8, "transfer-encoding") == 0)
1016 {
1017 if (hdr)
1018 hdr->content->encoding = mutt_check_encoding (p);
1019 matched = 1;
1020 }
1021 else if (ascii_strcasecmp (line+8, "length") == 0)
1022 {
1023 if (hdr)
1024 {
1025 if ((hdr->content->length = atol (p)) < 0)
1026 hdr->content->length = -1;
1027 }
1028 matched = 1;
1029 }
1030 else if (ascii_strcasecmp (line+8, "description") == 0)
1031 {
1032 if (hdr)
1033 {
1034 mutt_str_replace (&hdr->content->description, p);
1035 rfc2047_decode (&hdr->content->description);
1036 }
1037 matched = 1;
1038 }
1039 else if (ascii_strcasecmp (line+8, "disposition") == 0)
1040 {
1041 if (hdr)
1042 parse_content_disposition (p, hdr->content);
1043 matched = 1;
1044 }
1045 }
1046 break;
1047
1048 case 'd':
1049 if (!ascii_strcasecmp ("ate", line + 1))
1050 {
1051 mutt_str_replace (&e->date, p);
1052 if (hdr)
1053 hdr->date_sent = mutt_parse_date (p, hdr);
1054 matched = 1;
1055 }
1056 break;
1057
1058 case 'e':
1059 if (!ascii_strcasecmp ("xpires", line + 1) &&
1060 hdr && mutt_parse_date (p, NULL) < time (NULL))
1061 hdr->expired = 1;
1062 break;
1063
1064 case 'f':
1065 if (!ascii_strcasecmp ("rom", line + 1))
1066 {
1067 e->from = rfc822_parse_adrlist (e->from, p);
1068 matched = 1;
1069 }
1070 break;
1071
1072 case 'i':
1073 if (!ascii_strcasecmp (line+1, "n-reply-to"))
1074 {
1075 mutt_free_list (&e->in_reply_to);
1076 e->in_reply_to = mutt_parse_references (p, 1);
1077 matched = 1;
1078 }
1079 break;
1080
1081 case 'l':
1082 if (!ascii_strcasecmp (line + 1, "ines"))
1083 {
1084 if (hdr)
1085 {
1086 /*
1087 * HACK - mutt has, for a very short time, produced negative
1088 * Lines header values. Ignore them.
1089 */
1090 if (mutt_atoi (p, &hdr->lines) < 0 || hdr->lines < 0)
1091 hdr->lines = 0;
1092 }
1093
1094 matched = 1;
1095 }
1096 else if (!ascii_strcasecmp (line + 1, "ist-Post"))
1097 {
1098 /* RFC 2369. FIXME: We should ignore whitespace, but don't. */
1099 if (strncmp (p, "NO", 2))
1100 {
1101 char *beg, *end;
1102 for (beg = strchr (p, '<'); beg; beg = strchr (end, ','))
1103 {
1104 ++beg;
1105 if (!(end = strchr (beg, '>')))
1106 break;
1107
1108 /* Take the first mailto URL */
1109 if (url_check_scheme (beg) == U_MAILTO)
1110 {
1111 FREE (&e->list_post);
1112 e->list_post = mutt_substrdup (beg, end);
1113 break;
1114 }
1115 }
1116 }
1117 matched = 1;
1118 }
1119 break;
1120
1121 case 'm':
1122 if (!ascii_strcasecmp (line + 1, "ime-version"))
1123 {
1124 if (hdr)
1125 hdr->mime = 1;
1126 matched = 1;
1127 }
1128 else if (!ascii_strcasecmp (line + 1, "essage-id"))
1129 {
1130 /* We add a new "Message-ID:" when building a message */
1131 FREE (&e->message_id);
1132 e->message_id = mutt_extract_message_id (p, NULL);
1133 matched = 1;
1134 }
1135 else if (!ascii_strncasecmp (line + 1, "ail-", 4))
1136 {
1137 if (!ascii_strcasecmp (line + 5, "reply-to"))
1138 {
1139 /* override the Reply-To: field */
1140 rfc822_free_address (&e->reply_to);
1141 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1142 matched = 1;
1143 }
1144 else if (!ascii_strcasecmp (line + 5, "followup-to"))
1145 {
1146 e->mail_followup_to = rfc822_parse_adrlist (e->mail_followup_to, p);
1147 matched = 1;
1148 }
1149 }
1150 break;
1151
1152 case 'r':
1153 if (!ascii_strcasecmp (line + 1, "eferences"))
1154 {
1155 mutt_free_list (&e->references);
1156 e->references = mutt_parse_references (p, 0);
1157 matched = 1;
1158 }
1159 else if (!ascii_strcasecmp (line + 1, "eply-to"))
1160 {
1161 e->reply_to = rfc822_parse_adrlist (e->reply_to, p);
1162 matched = 1;
1163 }
1164 else if (!ascii_strcasecmp (line + 1, "eturn-path"))
1165 {
1166 e->return_path = rfc822_parse_adrlist (e->return_path, p);
1167 matched = 1;
1168 }
1169 else if (!ascii_strcasecmp (line + 1, "eceived"))
1170 {
1171 if (hdr && !hdr->received)
1172 {
1173 char *d = strchr (p, ';');
1174
1175 if (d)
1176 hdr->received = mutt_parse_date (d + 1, NULL);
1177 }
1178 }
1179 break;
1180
1181 case 's':
1182 if (!ascii_strcasecmp (line + 1, "ubject"))
1183 {
1184 if (!e->subject)
1185 e->subject = safe_strdup (p);
1186 matched = 1;
1187 }
1188 else if (!ascii_strcasecmp (line + 1, "ender"))
1189 {
1190 e->sender = rfc822_parse_adrlist (e->sender, p);
1191 matched = 1;
1192 }
1193 else if (!ascii_strcasecmp (line + 1, "tatus"))
1194 {
1195 if (hdr)
1196 {
1197 while (*p)
1198 {
1199 switch(*p)
1200 {
1201 case 'r':
1202 hdr->replied = 1;
1203 break;
1204 case 'O':
1205 hdr->old = 1;
1206 break;
1207 case 'R':
1208 hdr->read = 1;
1209 break;
1210 }
1211 p++;
1212 }
1213 }
1214 matched = 1;
1215 }
1216 else if ((!ascii_strcasecmp ("upersedes", line + 1) ||
1217 !ascii_strcasecmp ("upercedes", line + 1)) && hdr)
1218 e->supersedes = safe_strdup (p);
1219 break;
1220
1221 case 't':
1222 if (ascii_strcasecmp (line+1, "o") == 0)
1223 {
1224 e->to = rfc822_parse_adrlist (e->to, p);
1225 matched = 1;
1226 }
1227 break;
1228
1229 case 'x':
1230 if (ascii_strcasecmp (line+1, "-status") == 0)
1231 {
1232 if (hdr)
1233 {
1234 while (*p)
1235 {
1236 switch (*p)
1237 {
1238 case 'A':
1239 hdr->replied = 1;
1240 break;
1241 case 'D':
1242 hdr->deleted = 1;
1243 break;
1244 case 'F':
1245 hdr->flagged = 1;
1246 break;
1247 default:
1248 break;
1249 }
1250 p++;
1251 }
1252 }
1253 matched = 1;
1254 }
1255 else if (ascii_strcasecmp (line+1, "-label") == 0)
1256 {
1257 e->x_label = safe_strdup(p);
1258 matched = 1;
1259 }
1260
1261 default:
1262 break;
1263 }
1264
1265 /* Keep track of the user-defined headers */
1266 if (!matched && user_hdrs)
1267 {
1268 /* restore the original line */
1269 line[strlen (line)] = ':';
1270
1271 if (weed && option (OPTWEED) && mutt_matches_ignore (line, Ignore)
1272 && !mutt_matches_ignore (line, UnIgnore))
1273 goto done;
1274
1275 if (last)
1276 {
1277 last->next = mutt_new_list ();
1278 last = last->next;
1279 }
1280 else
1281 last = e->userhdrs = mutt_new_list ();
1282 last->data = safe_strdup (line);
1283 if (do_2047)
1284 rfc2047_decode (&last->data);
1285 }
1286
1287 done:
1288
1289 *lastp = last;
1290 return matched;
1291 }
1292
1293
1294 /* mutt_read_rfc822_header() -- parses a RFC822 header
1295 *
1296 * Args:
1297 *
1298 * f stream to read from
1299 *
1300 * hdr header structure of current message (optional).
1301 *
1302 * user_hdrs If set, store user headers. Used for recall-message and
1303 * postpone modes.
1304 *
1305 * weed If this parameter is set and the user has activated the
1306 * $weed option, honor the header weed list for user headers.
1307 * Used for recall-message.
1308 *
1309 * Returns: newly allocated envelope structure. You should free it by
1310 * mutt_free_envelope() when envelope stay unneeded.
1311 */
mutt_read_rfc822_header(FILE * f,HEADER * hdr,short user_hdrs,short weed)1312 ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
1313 short weed)
1314 {
1315 ENVELOPE *e = mutt_new_envelope();
1316 LIST *last = NULL;
1317 char *line = safe_malloc (LONG_STRING);
1318 char *p;
1319 LOFF_T loc;
1320 int matched;
1321 size_t linelen = LONG_STRING;
1322 char buf[LONG_STRING+1];
1323
1324 if (hdr)
1325 {
1326 if (hdr->content == NULL)
1327 {
1328 hdr->content = mutt_new_body ();
1329
1330 /* set the defaults from RFC1521 */
1331 hdr->content->type = TYPETEXT;
1332 hdr->content->subtype = safe_strdup ("plain");
1333 hdr->content->encoding = ENC7BIT;
1334 hdr->content->length = -1;
1335
1336 /* RFC 2183 says this is arbitrary */
1337 hdr->content->disposition = DISPINLINE;
1338 }
1339 }
1340
1341 while ((loc = ftello (f)),
1342 *(line = mutt_read_rfc822_line (f, line, &linelen)) != 0)
1343 {
1344 matched = 0;
1345
1346 if ((p = strpbrk (line, ": \t")) == NULL || *p != ':')
1347 {
1348 char return_path[LONG_STRING];
1349 time_t t;
1350
1351 /* some bogus MTAs will quote the original "From " line */
1352 if (mutt_strncmp (">From ", line, 6) == 0)
1353 continue; /* just ignore */
1354 else if (is_from (line, return_path, sizeof (return_path), &t))
1355 {
1356 /* MH somtimes has the From_ line in the middle of the header! */
1357 if (hdr && !hdr->received)
1358 hdr->received = t - mutt_local_tz (t);
1359 continue;
1360 }
1361
1362 fseeko (f, loc, 0);
1363 break; /* end of header */
1364 }
1365
1366 *buf = '\0';
1367
1368 if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf)))
1369 {
1370 if (!mutt_match_rx_list(line, NoSpamList))
1371 {
1372
1373 /* if spam tag already exists, figure out how to amend it */
1374 if (e->spam && *buf)
1375 {
1376 /* If SpamSep defined, append with separator */
1377 if (SpamSep)
1378 {
1379 mutt_buffer_addstr(e->spam, SpamSep);
1380 mutt_buffer_addstr(e->spam, buf);
1381 }
1382
1383 /* else overwrite */
1384 else
1385 {
1386 e->spam->dptr = e->spam->data;
1387 *e->spam->dptr = '\0';
1388 mutt_buffer_addstr(e->spam, buf);
1389 }
1390 }
1391
1392 /* spam tag is new, and match expr is non-empty; copy */
1393 else if (!e->spam && *buf)
1394 {
1395 e->spam = mutt_buffer_from(NULL, buf);
1396 }
1397
1398 /* match expr is empty; plug in null string if no existing tag */
1399 else if (!e->spam)
1400 {
1401 e->spam = mutt_buffer_from(NULL, "");
1402 }
1403
1404 if (e->spam && e->spam->data)
1405 dprint(5, (debugfile, "p822: spam = %s\n", e->spam->data));
1406 }
1407 }
1408
1409 *p = 0;
1410 p++;
1411 SKIPWS (p);
1412 if (!*p)
1413 continue; /* skip empty header fields */
1414
1415 matched = mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last);
1416
1417 }
1418
1419 FREE (&line);
1420
1421 if (hdr)
1422 {
1423 hdr->content->hdr_offset = hdr->offset;
1424 hdr->content->offset = ftello (f);
1425
1426 /* do RFC2047 decoding */
1427 rfc2047_decode_adrlist (e->from);
1428 rfc2047_decode_adrlist (e->to);
1429 rfc2047_decode_adrlist (e->cc);
1430 rfc2047_decode_adrlist (e->bcc);
1431 rfc2047_decode_adrlist (e->reply_to);
1432 rfc2047_decode_adrlist (e->mail_followup_to);
1433 rfc2047_decode_adrlist (e->return_path);
1434 rfc2047_decode_adrlist (e->sender);
1435 rfc2047_decode (&e->x_label);
1436
1437 if (e->subject)
1438 {
1439 regmatch_t pmatch[1];
1440
1441 rfc2047_decode (&e->subject);
1442
1443 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1444 e->real_subj = e->subject + pmatch[0].rm_eo;
1445 else
1446 e->real_subj = e->subject;
1447 if (option (OPTDELETEPREFIX))
1448 {
1449 /* if this option is set, mutt will delete the string as [prefix],
1450 * [prefix:number] and [prefix number] in Subject line.
1451 */
1452 if (regexec (DeleteRegexp.rx, e->real_subj, 1, pmatch, 0) == 0)
1453 {
1454 e->real_subj = e->real_subj + pmatch[0].rm_eo;
1455 if (regexec (ReplyRegexp.rx, e->real_subj, 1, pmatch, 0) == 0)
1456 e->real_subj = e->real_subj + pmatch[0].rm_eo;
1457 }
1458 }
1459 }
1460
1461 /* check for missing or invalid date */
1462 if (hdr->date_sent <= 0)
1463 {
1464 dprint(1,(debugfile,"read_rfc822_header(): no date found, using received time from msg separator\n"));
1465 hdr->date_sent = hdr->received;
1466 }
1467 }
1468
1469 return (e);
1470 }
1471
mutt_parse_adrlist(ADDRESS * p,const char * s)1472 ADDRESS *mutt_parse_adrlist (ADDRESS *p, const char *s)
1473 {
1474 const char *q;
1475
1476 /* check for a simple whitespace separated list of addresses */
1477 if ((q = strpbrk (s, "\"<>():;,\\")) == NULL)
1478 {
1479 char tmp[HUGE_STRING];
1480 char *r;
1481
1482 strfcpy (tmp, s, sizeof (tmp));
1483 r = tmp;
1484 while ((r = strtok (r, " \t")) != NULL)
1485 {
1486 p = rfc822_parse_adrlist (p, r);
1487 r = NULL;
1488 }
1489 }
1490 else
1491 p = rfc822_parse_adrlist (p, s);
1492
1493 return p;
1494 }
1495
1496 /* Compares mime types to the ok and except lists */
count_body_parts_check(LIST ** checklist,BODY * b,int dflt)1497 static int count_body_parts_check(LIST **checklist, BODY *b, int dflt)
1498 {
1499 LIST *type;
1500 ATTACH_MATCH *a;
1501
1502 /* If list is null, use default behavior. */
1503 if (! *checklist)
1504 {
1505 /*return dflt;*/
1506 return 0;
1507 }
1508
1509 for (type = *checklist; type; type = type->next)
1510 {
1511 a = (ATTACH_MATCH *)type->data;
1512 dprint(5, (debugfile, "cbpc: %s %d/%s ?? %s/%s [%d]... ",
1513 dflt ? "[OK] " : "[EXCL] ",
1514 b->type, b->subtype, a->major, a->minor, a->major_int));
1515 if ((a->major_int == TYPEANY || a->major_int == b->type) &&
1516 !regexec(&a->minor_rx, b->subtype, 0, NULL, 0))
1517 {
1518 dprint(5, (debugfile, "yes\n"));
1519 return 1;
1520 }
1521 else
1522 {
1523 dprint(5, (debugfile, "no\n"));
1524 }
1525 }
1526
1527 return 0;
1528 }
1529
1530 #define AT_COUNT(why) { shallcount = 1; }
1531 #define AT_NOCOUNT(why) { shallcount = 0; }
1532
count_body_parts(BODY * body,int flags)1533 static int count_body_parts (BODY *body, int flags)
1534 {
1535 int count = 0;
1536 int shallcount, shallrecurse;
1537 BODY *bp;
1538
1539 if (body == NULL)
1540 return 0;
1541
1542 for (bp = body; bp != NULL; bp = bp->next)
1543 {
1544 /* Initial disposition is to count and not to recurse this part. */
1545 AT_COUNT("default");
1546 shallrecurse = 0;
1547
1548 dprint(5, (debugfile, "bp: desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
1549 bp->description ? bp->description : ("none"),
1550 bp->filename ? bp->filename :
1551 bp->d_filename ? bp->d_filename : "(none)",
1552 bp->type, bp->subtype ? bp->subtype : "*"));
1553
1554 if (bp->type == TYPEMESSAGE)
1555 {
1556 shallrecurse = 1;
1557
1558 /* If it's an external body pointer, don't recurse it. */
1559 if (!ascii_strcasecmp (bp->subtype, "external-body"))
1560 shallrecurse = 0;
1561
1562 /* Don't count containers if they're top-level. */
1563 if (flags & M_PARTS_TOPLEVEL)
1564 AT_NOCOUNT("top-level message/*");
1565 }
1566 else if (bp->type == TYPEMULTIPART)
1567 {
1568 /* Always recurse multiparts, except multipart/alternative. */
1569 shallrecurse = 1;
1570 if (!ascii_strcasecmp(bp->subtype, "alternative"))
1571 shallrecurse = 0;
1572
1573 /* Don't count containers if they're top-level. */
1574 if (flags & M_PARTS_TOPLEVEL)
1575 AT_NOCOUNT("top-level multipart");
1576 }
1577
1578 if (bp->disposition == DISPINLINE &&
1579 bp->type != TYPEMULTIPART && bp->type != TYPEMESSAGE && bp == body)
1580 AT_NOCOUNT("ignore fundamental inlines");
1581
1582 /* If this body isn't scheduled for enumeration already, don't bother
1583 * profiling it further.
1584 */
1585 if (shallcount)
1586 {
1587 /* Turn off shallcount if message type is not in ok list,
1588 * or if it is in except list. Check is done separately for
1589 * inlines vs. attachments.
1590 */
1591
1592 if (bp->disposition == DISPATTACH)
1593 {
1594 if (!count_body_parts_check(&AttachAllow, bp, 1))
1595 AT_NOCOUNT("attach not allowed");
1596 if (count_body_parts_check(&AttachExclude, bp, 0))
1597 AT_NOCOUNT("attach excluded");
1598 }
1599 else
1600 {
1601 if (!count_body_parts_check(&InlineAllow, bp, 1))
1602 AT_NOCOUNT("inline not allowed");
1603 if (count_body_parts_check(&InlineExclude, bp, 0))
1604 AT_NOCOUNT("excluded");
1605 }
1606 }
1607
1608 if (shallcount)
1609 count++;
1610 bp->attach_qualifies = shallcount ? 1 : 0;
1611
1612 dprint(5, (debugfile, "cbp: %p shallcount = %d\n", (void *)bp, shallcount));
1613
1614 if (shallrecurse)
1615 {
1616 dprint(5, (debugfile, "cbp: %p pre count = %d\n", (void *)bp, count));
1617 bp->attach_count = count_body_parts(bp->parts, flags & ~M_PARTS_TOPLEVEL);
1618 count += bp->attach_count;
1619 dprint(5, (debugfile, "cbp: %p post count = %d\n", (void *)bp, count));
1620 }
1621 }
1622
1623 dprint(5, (debugfile, "bp: return %d\n", count < 0 ? 0 : count));
1624 return count < 0 ? 0 : count;
1625 }
1626
mutt_count_body_parts(CONTEXT * ctx,HEADER * hdr)1627 int mutt_count_body_parts (CONTEXT *ctx, HEADER *hdr)
1628 {
1629 short keep_parts = 0;
1630
1631 if (hdr->attach_valid)
1632 return hdr->attach_total;
1633
1634 if (hdr->content->parts)
1635 keep_parts = 1;
1636 else
1637 mutt_parse_mime_message (ctx, hdr);
1638
1639 if (AttachAllow || AttachExclude || InlineAllow || InlineExclude)
1640 hdr->attach_total = count_body_parts(hdr->content, M_PARTS_TOPLEVEL);
1641 else
1642 hdr->attach_total = 0;
1643
1644 hdr->attach_valid = 1;
1645
1646 if (!keep_parts)
1647 mutt_free_body (&hdr->content->parts);
1648
1649 return hdr->attach_total;
1650 }
1651