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