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