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