2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "NGMimeHeaderFieldParser.h"
23 #include "NGMimeHeaderFields.h"
24 #include "NGMimeUtilities.h"
27 @implementation NGMimeRFC822DateHeaderFieldParser
29 static Class CalDateClass = Nil;
30 static NSTimeZone *gmt = nil;
31 static NSTimeZone *gmt01 = nil;
32 static NSTimeZone *gmt02 = nil;
33 static NSTimeZone *gmt03 = nil;
34 static NSTimeZone *gmt04 = nil;
35 static NSTimeZone *gmt05 = nil;
36 static NSTimeZone *gmt06 = nil;
37 static NSTimeZone *gmt07 = nil;
38 static NSTimeZone *gmt08 = nil;
39 static NSTimeZone *gmt09 = nil;
40 static NSTimeZone *gmt10 = nil;
41 static NSTimeZone *gmt11 = nil;
42 static NSTimeZone *gmt12 = nil;
43 static NSTimeZone *gmt0530 = nil;
44 static NSTimeZone *gmtM01 = nil;
45 static NSTimeZone *gmtM02 = nil;
46 static NSTimeZone *gmtM03 = nil;
47 static NSTimeZone *gmtM04 = nil;
48 static NSTimeZone *gmtM05 = nil;
49 static NSTimeZone *gmtM06 = nil;
50 static NSTimeZone *gmtM07 = nil;
51 static NSTimeZone *gmtM08 = nil;
52 static NSTimeZone *gmtM09 = nil;
53 static NSTimeZone *gmtM10 = nil;
54 static NSTimeZone *gmtM11 = nil;
55 static NSTimeZone *gmtM12 = nil;
56 static NSTimeZone *gmtM13 = nil;
57 static NSTimeZone *gmtM14 = nil;
58 static NSTimeZone *met = nil;
64 static BOOL didInit = NO;
69 CalDateClass = [NSCalendarDate class];
71 /* timezones which were actually used in a maillist mailbox */
72 TzClass = [NSTimeZone class];
73 gmt = [[TzClass timeZoneWithName:@"GMT"] retain];
74 met = [[TzClass timeZoneWithName:@"MET"] retain];
75 gmt01 = [[TzClass timeZoneForSecondsFromGMT: 1 * (60 * 60)] retain];
76 gmt02 = [[TzClass timeZoneForSecondsFromGMT: 2 * (60 * 60)] retain];
77 gmt03 = [[TzClass timeZoneForSecondsFromGMT: 3 * (60 * 60)] retain];
78 gmt04 = [[TzClass timeZoneForSecondsFromGMT: 4 * (60 * 60)] retain];
79 gmt05 = [[TzClass timeZoneForSecondsFromGMT: 5 * (60 * 60)] retain];
80 gmt06 = [[TzClass timeZoneForSecondsFromGMT: 6 * (60 * 60)] retain];
81 gmt07 = [[TzClass timeZoneForSecondsFromGMT: 7 * (60 * 60)] retain];
82 gmt08 = [[TzClass timeZoneForSecondsFromGMT: 8 * (60 * 60)] retain];
83 gmt09 = [[TzClass timeZoneForSecondsFromGMT: 9 * (60 * 60)] retain];
84 gmt10 = [[TzClass timeZoneForSecondsFromGMT: 10 * (60 * 60)] retain];
85 gmt11 = [[TzClass timeZoneForSecondsFromGMT: 11 * (60 * 60)] retain];
86 gmt12 = [[TzClass timeZoneForSecondsFromGMT: 12 * (60 * 60)] retain];
87 gmtM01 = [[TzClass timeZoneForSecondsFromGMT: -1 * (60 * 60)] retain];
88 gmtM02 = [[TzClass timeZoneForSecondsFromGMT: -2 * (60 * 60)] retain];
89 gmtM03 = [[TzClass timeZoneForSecondsFromGMT: -3 * (60 * 60)] retain];
90 gmtM04 = [[TzClass timeZoneForSecondsFromGMT: -4 * (60 * 60)] retain];
91 gmtM05 = [[TzClass timeZoneForSecondsFromGMT: -5 * (60 * 60)] retain];
92 gmtM06 = [[TzClass timeZoneForSecondsFromGMT: -6 * (60 * 60)] retain];
93 gmtM07 = [[TzClass timeZoneForSecondsFromGMT: -7 * (60 * 60)] retain];
94 gmtM08 = [[TzClass timeZoneForSecondsFromGMT: -8 * (60 * 60)] retain];
95 gmtM09 = [[TzClass timeZoneForSecondsFromGMT: -9 * (60 * 60)] retain];
96 gmtM10 = [[TzClass timeZoneForSecondsFromGMT:-10 * (60 * 60)] retain];
97 gmtM11 = [[TzClass timeZoneForSecondsFromGMT:-11 * (60 * 60)] retain];
98 gmtM12 = [[TzClass timeZoneForSecondsFromGMT:-12 * (60 * 60)] retain];
99 gmtM13 = [[TzClass timeZoneForSecondsFromGMT:-13 * (60 * 60)] retain];
100 gmtM14 = [[TzClass timeZoneForSecondsFromGMT:-14 * (60 * 60)] retain];
102 gmt0530 = [[TzClass timeZoneForSecondsFromGMT:5 * (60*60) + (30*60)] retain];
106 All the date formats are more or less the same. If they start with a char
107 those can be skipped to the first digit (since it is the weekday name that
108 is unnecessary for date construction).
110 TODO: use an own parser for that.
113 static int parseMonthOfYear(unsigned char *s, unsigned int len) {
115 This one is *extremely* forgiving, it only checks what is
116 necessary for the set below. This should work for both, English
119 English: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
120 J F M A M J J A S O N D
123 NSLog(@"RFC822 Parser: cannot process month name: '%s'", s);
126 switch (toupper(*s)) {
127 case 'A': // April, August
128 if (toupper(s[1]) == 'P') return 4; // Apr
130 case 'D': return 12; // Dec
131 case 'F': return 2; // Feb
132 case 'J': // Jan, Jun, Jul
133 if (toupper(s[1]) == 'A') return 1; // Jan
134 if (toupper(s[2]) == 'N') return 6; // Jun
136 case 'M': // Mar, May
137 if (toupper(s[2]) == 'Y' || toupper(s[2]) == 'I') // May or Mai (German ;-)
140 case 'N': return 11; // Nov
141 case 'O': return 10; // Oct
142 case 'S': return 9; // Sep
144 NSLog(@"RFC822 Parser: cannot process month name: '%s'", s);
149 static NSTimeZone *parseTimeZone(unsigned char *s, unsigned int len) {
151 WARNING: failed to parse RFC822 timezone: '+0530' \
152 (value='Tue, 13 Jul 2004 21:39:28 +0530')
153 TODO: this is because libFoundation doesn't accept 'GMT+0530' as input.
162 if (*s == '+' || *s == '-') {
164 if (p[1] == '0' && p[2] == '0') // '+00' or '-00'
167 if (p[1] == '0' && p[2] == '1') // '+01'
169 if (p[1] == '0' && p[2] == '2') // '+02'
174 if (p[3] == '0' && p[4] == '0' && p[1] == '0') { // '?0x00'
175 if (p[2] == '0') // '+0000'
179 if (p[2] == '1') return gmt01; // '+0100'
180 if (p[2] == '2') return gmt02; // '+0200'
181 if (p[2] == '3') return gmt03; // '+0300'
182 if (p[2] == '4') return gmt04; // '+0400'
183 if (p[2] == '5') return gmt05; // '+0500'
184 if (p[2] == '6') return gmt06; // '+0600'
185 if (p[2] == '7') return gmt07; // '+0700'
186 if (p[2] == '8') return gmt08; // '+0800'
187 if (p[2] == '9') return gmt09; // '+0900'
189 else if (*s == '-') {
190 if (p[2] == '1') return gmtM01; // '-0100'
191 if (p[2] == '2') return gmtM02; // '-0200'
192 if (p[2] == '3') return gmtM03; // '-0300'
193 if (p[2] == '4') return gmtM04; // '-0400'
194 if (p[2] == '5') return gmtM05; // '-0500'
195 if (p[2] == '6') return gmtM06; // '-0600'
196 if (p[2] == '7') return gmtM07; // '-0700'
197 if (p[2] == '8') return gmtM08; // '-0800'
198 if (p[2] == '9') return gmtM09; // '-0900'
201 else if (p[3] == '0' && p[4] == '0' && p[1] == '1') { // "?1x00"
203 if (p[2] == '0') return gmt10; // '+1000'
204 if (p[2] == '1') return gmt11; // '+1100'
205 if (p[2] == '2') return gmt12; // '+1200'
207 else if (*s == '-') {
208 if (p[2] == '0') return gmtM10; // '-1000'
209 if (p[2] == '1') return gmtM11; // '-1100'
210 if (p[2] == '2') return gmtM12; // '-1200'
211 if (p[2] == '3') return gmtM13; // '-1300'
212 if (p[2] == '4') return gmtM14; // '-1400'
216 /* special case for GMT+0530 */
217 if (strncmp((char *)s, "+0530", 5) == 0)
222 "MultiMail" submits timezones like this:
223 "Tue, 9 Mar 2004 9:43:00 -05-500",
224 don't know what the "-500" trailer is supposed to mean? Apparently
225 Thunderbird just uses the "-05", so do we.
228 if (isdigit(p[1]) && isdigit(p[2]) && (p[3] == '-'||p[3] == '+')) {
229 unsigned char tmp[8];
231 strncpy((char *)tmp, p, 3);
235 return parseTimeZone(tmp, 5);
239 else if (*s == '0') {
240 if (len == 2) { // '00'
241 if (p[1] == '0') return gmt;
242 if (p[1] == '1') return gmt01;
243 if (p[1] == '2') return gmt02;
246 if (p[2] == '0' && p[3] == '0') { // '0x00'
247 if (p[1] == '0') return gmt;
248 if (p[1] == '1') return gmt01;
249 if (p[1] == '2') return gmt02;
254 if (strcasecmp((char *)s, "GMT") == 0) return gmt;
255 if (strcasecmp((char *)s, "MET") == 0) return met;
259 ts = [[NSString alloc] initWithCString:(char *)s length:len];
264 buf[0] = 'G'; buf[1] = 'M'; buf[2] = 'T';
265 if (*s == '+' || *s == '-') {
266 strcpy(&(buf[3]), (char *)s);
270 strcpy(&(buf[4]), (char *)s);
272 ts = [[NSString alloc] initWithCString:buf];
275 NSLog(@"%s: RFC822 TZ Parser: expensive: '%@'", __PRETTY_FUNCTION__, ts);
277 tz = [NSTimeZone timeZoneWithAbbreviation:ts];
282 - (id)parseValue:(id)_data ofHeaderField:(NSString *)_field {
284 NSCalendarDate *date = nil;
285 unsigned char buf[256];
286 unsigned char *bytes = buf, *pe;
288 NSTimeZone *tz = nil;
289 char dayOfMonth, monthOfYear, hour, minute, second;
293 if ((length = [_data cStringLength]) > 254) {
295 @"header field value to large for date parsing: '%@'(%i)",
300 [_data getCString:(char *)buf maxLength:length];
303 /* remove leading chars (skip to first digit, the day of the month) */
304 while (length > 0 && (!isdigit(*bytes))) {
310 NSLog(@"WARNING(%s): empty value for header field %@ ..",
311 __PRETTY_FUNCTION__, _field);
312 return [CalDateClass date];
315 // TODO: should be a category on NSCalendarDate
316 // TODO: optimize much further!
317 // first part: '16 Jun 2002'
318 // snd part: '12:28[:11]'
319 // trd part: 'GMT' '+0000' '(MET)' '(+0200)'
321 /* defaults for early aborts */
326 /* parse day of month */
328 for (pe = bytes; isdigit(*pe); pe++)
330 if (*pe == 0) goto failed;
332 dayOfMonth = atoi((char *)bytes);
335 /* parse month-abbrev (should be English, could be other langs) */
337 while (!isalpha(*bytes)) { /* go to first char */
338 if (*bytes == '\0') goto failed;
341 for (pe = bytes; isalpha(*pe); pe++) /* find end of string */
343 if (*pe == 0) goto failed;
345 if ((monthOfYear = parseMonthOfYear(bytes, (pe - bytes))) == 0) {
346 [self logWithFormat:@"WARNING(%s): cannot parse month in date: %@",
347 __PRETTY_FUNCTION__, _data];
353 while (!isdigit(*bytes)) { /* go to first digit */
354 if (*bytes == '\0') goto failed;
357 for (pe = bytes; isdigit(*pe); pe++) /* find end of number */
359 if (*pe == 0) goto failed;
361 year = atoi((char *)bytes);
363 if (year >= 70 && year < 135) // Y2K
365 else if (year >= 0 && year < 70) // Y2K
368 #if LIB_FOUNDATION_LIBRARY
370 NSLog(@"ERROR(%s): got invalid year in date header %d: '%s'",
371 __PRETTY_FUNCTION__, year, buf);
372 year = 2000; /* no choice is good ..., maybe return nil? */
378 while (!isdigit(*bytes)) { /* go to first digit */
379 if (*bytes == '\0') goto failed;
382 for (pe = bytes; isdigit(*pe); pe++) /* find end of number */
386 hour = bytes != pe ? atoi((char *)bytes) : 0;
387 if (flag) goto finished; // this is: '12\0'
392 while (!isdigit(*bytes)) { /* go to first digit */
393 if (*bytes == '\0') goto finished; // this is: '12 \0'
396 for (pe = bytes; isdigit(*pe); pe++) /* find end of number */
400 minute = bytes != pe ? atoi((char *)bytes) : 0;
401 if (flag) goto finished; // this is: '12:23\0'
404 /* parse second - if available '13:13:23' vs '12:23\0' or '12:12 (MET)' */
406 while (isspace(*bytes)) /* skip spaces */
408 if (*bytes == 0) goto finished; // this is: '12:23 \0'
409 if (isdigit(*bytes) || *bytes == ':') {
411 while (!isdigit(*bytes)) { /* go to first digit, skip the ':' */
412 if (*bytes == '\0') goto finished;
416 for (pe = bytes; isdigit(*pe); pe++) /* find end of number */
420 second = bytes != pe ? atoi((char *)bytes) : 0;
421 if (flag) goto finished; // this is: '12:23:12\0'
425 /* parse timezone: 'GMT' '+0000' '(MET)' '(+0200)' */
426 // TODO: do we need to parse: "-0700 (PDT)" as "PDT"?
428 while (isspace(*bytes) || *bytes == '(') /* skip spaces */
430 if (*bytes == 0) goto finished; // this is: '12:23:12 \0' or '12:12 ('
432 for (pe = bytes; isalnum(*pe) || *pe == '-' || *pe == '+'; pe++)
435 if ((tz = parseTimeZone(bytes, (pe - bytes))) == nil) {
437 @"WARNING: failed to parse RFC822 timezone: '%s' (value='%@')",
442 /* construct and return */
444 date = [CalDateClass dateWithYear:year month:monthOfYear day:dayOfMonth
445 hour:hour minute:minute second:second
447 if (date == nil) goto failed;
450 printf("parsed '%s' to date: %s\n",
451 [_data cString], [[date description] cString]);
452 //[self logWithFormat:@"parsed '%@' to date: %@", _data, date];
457 // TODO: 'Sun, May 18 2003 14:20:55 -0700' - why does this fail?
458 [self logWithFormat:@"WARNING: failed to parse RFC822 date field: '%@'",
463 @end /* NGMimeRFC822DateHeaderFieldParser */