]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeRFC822DateHeaderFieldParser.m
synced with latest additions and bumped framework versions
[sope] / sope-mime / NGMime / NGMimeRFC822DateHeaderFieldParser.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "NGMimeHeaderFieldParser.h"
23 #include "NGMimeHeaderFields.h"
24 #include "NGMimeUtilities.h"
25 #include "common.h"
26
27 @implementation NGMimeRFC822DateHeaderFieldParser
28
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;
59
60 + (int)version {
61   return 2;
62 }
63 + (void)initialize {
64   static BOOL didInit = NO;
65   Class TzClass;
66   if (didInit) return;
67   didInit = YES;
68   
69   CalDateClass = [NSCalendarDate class];
70   
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];
101   
102   gmt0530 = [[TzClass timeZoneForSecondsFromGMT:5 * (60*60) + (30*60)] retain];
103 }
104
105 /* 
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).
109    
110    TODO: use an own parser for that.
111 */
112
113 static int parseMonthOfYear(unsigned char *s, unsigned int len) {
114   /*
115     This one is *extremely* forgiving, it only checks what is
116     necessary for the set below. This should work for both, English
117     and German.
118     
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
121   */
122   if (len < 3) {
123     NSLog(@"RFC822 Parser: cannot process month name: '%s'", s);
124     return 0;
125   }
126   switch (toupper(*s)) {
127   case 'A': // April, August
128     if (toupper(s[1]) == 'P') return 4; // Apr
129     return 8; // Aug
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
135     return 7; // Jul
136   case 'M': // Mar, May
137     if (toupper(s[2]) == 'Y' || toupper(s[2]) == 'I') // May or Mai (German ;-)
138       return 5;
139     return 3; // Mar
140   case 'N': return 11; // Nov
141   case 'O': return 10; // Oct
142   case 'S': return  9; // Sep
143   default:
144     NSLog(@"RFC822 Parser: cannot process month name: '%s'", s);
145     return 0;
146   }
147 }
148
149 static NSTimeZone *parseTimeZone(unsigned char *s, unsigned int len) {
150   /*
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.
154   */
155   char       *p = (char *)s;
156   NSTimeZone *tz;
157   NSString   *ts;
158   
159   if (len == 0) 
160     return nil;
161   
162   if (*s == '+' || *s == '-') {
163     if (len == 3) {
164       if (p[1] == '0' && p[2] == '0') // '+00' or '-00'
165         return gmt;
166       if (*s == '+') {
167         if (p[1] == '0' && p[2] == '1') // '+01'
168           return gmt01;
169         if (p[1] == '0' && p[2] == '2') // '+02'
170           return gmt02;
171       }
172     }
173     else if (len == 5) {
174       if (p[3] == '0' && p[4] == '0' && p[1] == '0') { // '?0x00'
175         if (p[2] == '0') // '+0000'
176           return gmt;
177         
178         if (*s == '+') {
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'
188         }
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'
199         }
200       }
201       else if (p[3] == '0' && p[4] == '0' && p[1] == '1') { // "?1x00"
202         if (*s == '+') {
203           if (p[2] == '0') return gmt10; // '+1000'
204           if (p[2] == '1') return gmt11; // '+1100'
205           if (p[2] == '2') return gmt12; // '+1200'
206         }
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'
213         }
214       }
215       
216       /* special case for GMT+0530 */
217       if (strncmp((char *)s, "+0530", 5) == 0)
218         return gmt0530;
219     }
220     else if (len == 7) {
221       /*
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.
226       */
227       
228       if (isdigit(p[1]) && isdigit(p[2]) && (p[3] == '-'||p[3] == '+')) {
229         unsigned char tmp[8];
230         
231         strncpy((char *)tmp, p, 3);
232         tmp[3] = '0';
233         tmp[4] = '0';
234         tmp[5] = '\0';
235         return parseTimeZone(tmp, 5);
236       }
237     }
238   }
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;
244     }
245     else if (len == 4) {
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;
250       }
251     }
252   }
253   else if (len == 3) {
254     if (strcasecmp((char *)s, "GMT") == 0) return gmt;
255     if (strcasecmp((char *)s, "MET") == 0) return met;
256   }
257   
258   if (isalpha(*s)) {
259     ts = [[NSString alloc] initWithCString:(char *)s length:len];
260   }
261   else {
262     char buf[len + 5];
263     
264     buf[0] = 'G'; buf[1] = 'M'; buf[2] = 'T';
265     if (*s == '+' || *s == '-') {
266       strcpy(&(buf[3]), (char *)s);
267     }
268     else {
269       buf[3] = '+';
270       strcpy(&(buf[4]), (char *)s);
271     }
272     ts = [[NSString alloc] initWithCString:buf];
273   }
274 #if 1
275   NSLog(@"%s: RFC822 TZ Parser: expensive: '%@'", __PRETTY_FUNCTION__, ts);
276 #endif
277   tz = [NSTimeZone timeZoneWithAbbreviation:ts];
278   [ts release];
279   return tz;
280 }
281
282 - (id)parseValue:(id)_data ofHeaderField:(NSString *)_field {
283   // TODO: use UNICODE
284   NSCalendarDate *date       = nil;
285   unsigned char  buf[256];
286   unsigned char  *bytes = buf, *pe;
287   unsigned       length = 0;
288   NSTimeZone     *tz = nil;
289   char  dayOfMonth, monthOfYear, hour, minute, second;
290   short year;
291   BOOL  flag;
292   
293   if ((length = [_data cStringLength]) > 254) {
294     [self logWithFormat:
295             @"header field value to large for date parsing: '%@'(%i)",
296             _data, length];
297     length = 254;
298   }
299   
300   [_data getCString:(char *)buf maxLength:length];
301   buf[length] = '\0';
302   
303   /* remove leading chars (skip to first digit, the day of the month) */
304   while (length > 0 && (!isdigit(*bytes))) {
305     bytes++;
306     length--;
307   }
308   
309   if (length == 0) {
310     NSLog(@"WARNING(%s): empty value for header field %@ ..",
311           __PRETTY_FUNCTION__, _field);
312     return [CalDateClass date];
313   }
314   
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)'
320
321   /* defaults for early aborts */
322   tz     = gmt;
323   second = 0;
324   minute = 0;
325   
326   /* parse day of month */
327   
328   for (pe = bytes; isdigit(*pe); pe++)
329     ;
330   if (*pe == 0) goto failed;
331   *pe = '\0';
332   dayOfMonth = atoi((char *)bytes);
333   bytes = pe + 1;
334   
335   /* parse month-abbrev (should be English, could be other langs) */
336   
337   while (!isalpha(*bytes)) { /* go to first char */
338     if (*bytes == '\0') goto failed;
339     bytes++;
340   }
341   for (pe = bytes; isalpha(*pe); pe++) /* find end of string */
342     ;
343   if (*pe == 0) goto failed;
344   *pe = '\0';
345   if ((monthOfYear = parseMonthOfYear(bytes, (pe - bytes))) == 0) {
346     [self logWithFormat:@"WARNING(%s): cannot parse month in date: %@",
347             __PRETTY_FUNCTION__, _data];
348   }
349   bytes = pe + 1;
350   
351   /* parse year */
352   
353   while (!isdigit(*bytes)) { /* go to first digit */
354     if (*bytes == '\0') goto failed;
355     bytes++;
356   }
357   for (pe = bytes; isdigit(*pe); pe++) /* find end of number */
358     ;
359   if (*pe == 0) goto failed;
360   *pe = '\0';
361   year = atoi((char *)bytes);
362   bytes = pe + 1;
363   if (year >= 70 && year < 135) // Y2K
364     year += 1900;
365   else if (year >= 0 && year < 70) // Y2K
366     year += 2000;
367   
368 #if LIB_FOUNDATION_LIBRARY
369   if (year > 2030) {
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? */
373   }
374 #endif
375   
376   /* parse hour */
377   
378   while (!isdigit(*bytes)) { /* go to first digit */
379     if (*bytes == '\0') goto failed;
380     bytes++;
381   }
382   for (pe = bytes; isdigit(*pe); pe++) /* find end of number */
383     ;
384   flag = (*pe == 0);
385   *pe = '\0';
386   hour = bytes != pe ? atoi((char *)bytes) : 0;
387   if (flag) goto finished; // this is: '12\0'
388   bytes = pe + 1;
389   
390   /* parse minute */
391   
392   while (!isdigit(*bytes)) { /* go to first digit */
393     if (*bytes == '\0') goto finished; // this is: '12  \0'
394     bytes++;
395   }
396   for (pe = bytes; isdigit(*pe); pe++) /* find end of number */
397     ;
398   flag = (*pe == 0);
399   *pe = '\0';
400   minute = bytes != pe ? atoi((char *)bytes) : 0;
401   if (flag) goto finished; // this is: '12:23\0'
402   bytes = pe + 1;
403   
404   /* parse second - if available '13:13:23' vs '12:23\0' or '12:12 (MET)' */
405   
406   while (isspace(*bytes)) /* skip spaces */
407     bytes++;
408   if (*bytes == 0) goto finished; // this is: '12:23   \0'
409   if (isdigit(*bytes) || *bytes == ':') {
410     /* parse second */
411     while (!isdigit(*bytes)) { /* go to first digit, skip the ':' */
412       if (*bytes == '\0') goto finished;
413       bytes++;
414     }
415     
416     for (pe = bytes; isdigit(*pe); pe++) /* find end of number */
417       ;
418     flag = (*pe == 0);
419     *pe = '\0';
420     second = bytes != pe ? atoi((char *)bytes) : 0;
421     if (flag) goto finished; // this is: '12:23:12\0'
422     bytes = pe + 1;
423   }
424   
425   /* parse timezone: 'GMT' '+0000' '(MET)' '(+0200)' */
426   // TODO: do we need to parse: "-0700 (PDT)" as "PDT"?
427   
428   while (isspace(*bytes) || *bytes == '(') /* skip spaces */
429     bytes++;
430   if (*bytes == 0) goto finished; // this is: '12:23:12 \0' or '12:12 ('
431   
432   for (pe = bytes; isalnum(*pe) || *pe == '-' || *pe == '+'; pe++)
433     ;
434   *pe = '\0';
435   if ((tz = parseTimeZone(bytes, (pe - bytes))) == nil) {
436     [self logWithFormat:
437             @"WARNING: failed to parse RFC822 timezone: '%s' (value='%@')",
438             bytes, _data];
439     tz = gmt;
440   }
441   
442   /* construct and return */
443  finished:  
444   date = [CalDateClass dateWithYear:year month:monthOfYear day:dayOfMonth
445                        hour:hour minute:minute second:second
446                        timeZone:tz];
447   if (date == nil) goto failed;
448
449 #if 0  
450   printf("parsed '%s' to date: %s\n", 
451          [_data cString], [[date description] cString]);
452   //[self logWithFormat:@"parsed '%@' to date: %@", _data, date];
453 #endif
454   return date;
455   
456  failed:
457   // TODO: 'Sun, May 18 2003 14:20:55 -0700' - why does this fail?
458   [self logWithFormat:@"WARNING: failed to parse RFC822 date field: '%@'",
459           _data];
460   return nil;
461 }
462
463 @end /* NGMimeRFC822DateHeaderFieldParser */