fix regressions
[apps/madmutt.git] / from.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
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.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <lib-lib/mem.h>
15
16 #include "mutt.h"
17
18 #include "lib/mem.h"
19 #include "lib/intl.h"
20 #include "lib/str.h"
21 #include "lib/debug.h"
22
23 #include <ctype.h>
24 #include <string.h>
25
26 static const char *next_word (const char *s)
27 {
28   while (*s && !ISSPACE (*s))
29     s++;
30   SKIPWS (s);
31   return s;
32 }
33
34 int mutt_check_month (const char *s)
35 {
36   int i;
37
38   for (i = 0; i < 12; i++)
39     if (str_ncasecmp (s, Months[i], 3) == 0)
40       return (i);
41   return (-1);                  /* error */
42 }
43
44 static int is_day_name (const char *s)
45 {
46   int i;
47
48   if ((str_len (s) < 3) || !*(s + 3) || !ISSPACE (*(s + 3)))
49     return 0;
50   for (i = 0; i < 7; i++)
51     if (str_ncasecmp (s, Weekdays[i], 3) == 0)
52       return 1;
53   return 0;
54 }
55
56 /*
57  * A valid message separator looks like:
58  *
59  * From [ <return-path> ] <weekday> <month> <day> <time> [ <timezone> ] <year>
60  */
61
62 int is_from (const char *s, char *path, size_t pathlen, time_t * tp)
63 {
64   struct tm tm;
65   int yr;
66
67   if (path)
68     *path = 0;
69
70   if (str_ncmp ("From ", s, 5) != 0)
71     return 0;
72
73   s = next_word (s);            /* skip over the From part. */
74   if (!*s)
75     return 0;
76
77   debug_print (3, ("parsing: %s\n", s));
78
79   if (!is_day_name (s)) {
80     const char *p;
81     size_t len;
82     short q = 0;
83
84     for (p = s; *p && (q || !ISSPACE (*p)); p++) {
85       if (*p == '\\') {
86         if (*++p == '\0')
87           return 0;
88       }
89       else if (*p == '"') {
90         q = !q;
91       }
92     }
93
94     if (q || !*p)
95       return 0;
96
97     if (path) {
98       len = (size_t) (p - s);
99       if (len + 1 > pathlen)
100         len = pathlen - 1;
101       memcpy (path, s, len);
102       path[len] = 0;
103       debug_print (3, ("got return path: %s\n", path));
104     }
105
106     s = p + 1;
107     SKIPWS (s);
108     if (!*s)
109       return 0;
110
111     if (!is_day_name (s)) {
112       debug_print (1, ("expected weekday, got: %s\n", s));
113       return 0;
114     }
115   }
116
117   s = next_word (s);
118   if (!*s)
119     return 0;
120
121   /* do a quick check to make sure that this isn't really the day of the week.
122    * this could happen when receiving mail from a local user whose login name
123    * is the same as a three-letter abbreviation of the day of the week.
124    */
125   if (is_day_name (s)) {
126     s = next_word (s);
127     if (!*s)
128       return 0;
129   }
130
131   /* now we should be on the month. */
132   if ((tm.tm_mon = mutt_check_month (s)) < 0)
133     return 0;
134
135   /* day */
136   s = next_word (s);
137   if (!*s)
138     return 0;
139   if (sscanf (s, "%d", &tm.tm_mday) != 1)
140     return 0;
141
142   /* time */
143   s = next_word (s);
144   if (!*s)
145     return 0;
146
147   /* Accept either HH:MM or HH:MM:SS */
148   if (sscanf (s, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 3);
149   else if (sscanf (s, "%d:%d", &tm.tm_hour, &tm.tm_min) == 2)
150     tm.tm_sec = 0;
151   else
152     return 0;
153
154   s = next_word (s);
155   if (!*s)
156     return 0;
157
158   /* timezone? */
159   if (isalpha ((unsigned char) *s) || *s == '+' || *s == '-') {
160     s = next_word (s);
161     if (!*s)
162       return 0;
163
164     /*
165      * some places have two timezone fields after the time, e.g.
166      *      From xxxx@yyyyyyy.fr Wed Aug  2 00:39:12 MET DST 1995
167      */
168     if (isalpha ((unsigned char) *s)) {
169       s = next_word (s);
170       if (!*s)
171         return 0;
172     }
173   }
174
175   /* year */
176   if (sscanf (s, "%d", &yr) != 1)
177     return 0;
178   tm.tm_year = yr > 1900 ? yr - 1900 : (yr < 70 ? yr + 100 : yr);
179
180   debug_print (3, ("month=%d, day=%d, hr=%d, min=%d, sec=%d, yr=%d.\n",
181            tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
182            tm.tm_year));
183
184   tm.tm_isdst = -1;
185
186   if (tp)
187     *tp = mutt_mktime (&tm, 0);
188   return 1;
189 }