Debian Codegraph @

Framework/Pantomime/CWParser.m

raw
1 /*
2 ** CWParser.m
3 **
4 ** Copyright (c) 2001-2007 Ludovic Marcotte
5 ** Copyright (C) 2013-2020 The GNUstep Team
6 **
7 ** Author: Ludovic Marcotte <[email protected]>
8 ** Riccardo Mottola <[email protected]>
9 ** Sebastian Reitenbach
10 ** German Arias
11 **
12 ** This library is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU Lesser General Public
14 ** License as published by the Free Software Foundation; either
15 ** version 2.1 of the License, or (at your option) any later version.
16 **
17 ** This library is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** Lesser General Public License for more details.
21 **
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #import <Pantomime/CWParser.h>
27
28 #import <Pantomime/CWConstants.h>
29 #import <Pantomime/CWFlags.h>
30 #import <Pantomime/CWInternetAddress.h>
31 #import <Pantomime/CWMessage.h>
32 #import <Pantomime/CWMIMEUtility.h>
33 #import <Pantomime/NSData+Extensions.h>
34 #import <Pantomime/NSString+Extensions.h>
35
36 #include <stdlib.h>
37 #include <string.h> // For NULL on OS X
38 #include <ctype.h>
39 #include <stdio.h>
40 //#include <Pantomime/elm_defs.h>
41
42 #import <Foundation/NSBundle.h>
43 #import <Foundation/NSTimeZone.h>
44 #import <Foundation/NSString.h>
45 #import <Foundation/NSURL.h>
46
47 //
48 //
49 //
50 static char *month_name[12] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
51
52 static struct _timezone {
53 char *name; /* time zone name */
54 int offset; /* offset, in minutes, EAST of GMT */
55 } timezone_info[] = {
56
57 /* the following are from RFC-822 */
58 { "ut", 0 },
59 { "gmt", 0 },
60 { "est", -5*3600 }, { "edt", -4*3600 }, /* USA eastern standard */
61 { "cst", -6*3600 }, { "cdt", -5*3600 }, /* USA central standard */
62 { "mst", -7*3600 }, { "mdt", -6*3600 }, /* USA mountain standard */
63 { "pst", -8*3600 }, { "pdt", -7*3600 }, /* USA pacific standard */
64 { "z", 0 }, /* zulu time (the rest of the military codes are bogus) */
65
66 /* popular European timezones */
67 { "wet", 0*3600 }, /* western european */
68 { "met", 1*3600 }, /* middle european */
69 { "eet", 2*3600 }, /* eastern european */
70 { "bst", 1*3600 }, /* ??? british summer time */
71
72 /* Canadian timezones */
73 { "ast", -4*3600 }, { "adt", -3*3600 }, /* atlantic */
74 { "nst", -3*1800 },{ "ndt", -2*1800 }, /* newfoundland */
75 { "yst", -9*3600 }, { "ydt", -8*3600 }, /* yukon */
76 { "hst", -10*3600 }, /* hawaii (not really canada) */
77
78 /* Asian timezones */
79 { "jst", 9*3600 }, /* japan */
80 { "sst", 8*3600 }, /* singapore */
81
82 /* South-Pacific timezones */
83 { "nzst", 12*3600 }, { "nzdt", 13*3600 }, /* new zealand */
84 { "wst", 8*3600 }, { "wdt", 9*3600 }, /* western australia */
85
86 /*
87 * Daylight savings modifiers. These are not real timezones.
88 * They are used for things like "met dst". The "met" timezone
89 * is 1*3600, and applying the "dst" modifier makes it 2*3600.
90 */
91 { "dst", 1*3600 },
92 { "dt", 1*3600 },
93 { "st", 1*3600 }
94 };
95
96 //
97 //
98 //
99 int next_word(unsigned char *buf, NSUInteger start, NSUInteger len, unsigned char *word)
100 {
101 unsigned char *p;
102 NSUInteger i;
103
104 for (p = buf+start, i = start; (isspace(*p) || *p == ','); ++p, ++i);
105
106 if (start >= len) return -1;
107
108 while (i < len && !(isspace(*p) || *p == ','))
109 {
110 *word++ = *p++;
111 i++;
112 }
113
114 *word = '\0';
115
116 return (int)((NSUInteger)(p-buf)-start);
117 }
118
119 //
120 // private methods
121 //
122 @interface CWParser (Private)
123 + (id) _parameterValueUsingLine: (NSData *) theLine
124 range: (NSRange) theRange
125 decode: (BOOL) theBOOL
126 charset: (NSString *) theCharset;
127 @end
128
129
130 //
131 //
132 //
133 @implementation CWParser
134
135 + (void) parseContentDescription: (NSData *) theLine
136 inPart: (CWPart *) thePart
137 {
138 NSData *aData;
139
140 aData = [[theLine subdataFromIndex: 20] dataByTrimmingWhiteSpaces];
141
142 if (aData && [aData length])
143 {
144 [thePart setContentDescription: [[aData dataFromQuotedData] asciiString] ];
145 }
146 }
147
148
149 //
150 //
151 //
152 + (void) parseContentDisposition: (NSData *) theLine
153 inPart: (CWPart *) thePart
154 {
155 if ([theLine length] > 21)
156 {
157 NSData *aData;
158 NSRange aRange;
159
160 aData = [theLine subdataFromIndex: 21];
161 aRange = [aData rangeOfCString: ";"];
162
163 if (aRange.length > 0)
164 {
165 NSRange filenameRange;
166
167 // We set the content disposition to this part
168 [thePart setContentDisposition: ([[[aData subdataWithRange: NSMakeRange(0, aRange.location)] asciiString] caseInsensitiveCompare: @"inline"] == NSOrderedSame ? PantomimeInlineDisposition : PantomimeAttachmentDisposition)];
169
170 // We now decode our filename
171 filenameRange = [aData rangeOfCString: "filename"];
172
173 if (filenameRange.length > 0)
174 {
175 [thePart setFilename: [CWParser _parameterValueUsingLine: aData range: filenameRange decode: YES charset: [thePart defaultCharset]]];
176 }
177 }
178 else
179 {
180 [thePart setContentDisposition: ([[[aData dataByTrimmingWhiteSpaces] asciiString] caseInsensitiveCompare: @"inline"] == NSOrderedSame ? PantomimeInlineDisposition : PantomimeAttachmentDisposition)];
181 }
182 }
183 else
184 {
185 [thePart setContentDisposition: PantomimeAttachmentDisposition];
186 }
187 }
188
189
190 //
191 //
192 //
193 + (void) parseContentID: (NSData *) theLine
194 inPart: (CWPart *) thePart
195 {
196 if ([theLine length] > 12)
197 {
198 NSData *aData;
199
200 aData = [theLine subdataFromIndex: 12];
201
202 if ([aData hasCPrefix: "<"] && [aData hasCSuffix: ">"])
203 {
204 [thePart setContentID: [[aData subdataWithRange: NSMakeRange(1, [aData length]-2)] asciiString]];
205 }
206 else
207 {
208 [thePart setContentID: [aData asciiString]];
209 }
210 }
211 else
212 {
213 [thePart setContentID: @""];
214 }
215 }
216
217
218 //
219 //
220 //
221 + (void) parseContentTransferEncoding: (NSData *) theLine
222 inPart: (CWPart *) thePart
223 {
224 if ([theLine length] > 26)
225 {
226 NSData *aData;
227
228 aData = [[theLine subdataFromIndex: 26] dataByTrimmingWhiteSpaces];
229
230 if ([aData caseInsensitiveCCompare: "quoted-printable"] == NSOrderedSame)
231 {
232 [thePart setContentTransferEncoding: PantomimeEncodingQuotedPrintable];
233 }
234 else if ([aData caseInsensitiveCCompare: "base64"] == NSOrderedSame)
235 {
236 [thePart setContentTransferEncoding: PantomimeEncodingBase64];
237 }
238 else if ([aData caseInsensitiveCCompare: "8bit"] == NSOrderedSame)
239 {
240 [thePart setContentTransferEncoding: PantomimeEncoding8bit];
241 }
242 else if ([aData caseInsensitiveCCompare: "binary"] == NSOrderedSame)
243 {
244 [thePart setContentTransferEncoding: PantomimeEncodingBinary];
245 }
246 else
247 {
248 [thePart setContentTransferEncoding: PantomimeEncodingNone];
249 }
250 }
251 else
252 {
253 [thePart setContentTransferEncoding: PantomimeEncodingNone];
254 }
255 }
256
257
258 //
259 //
260 //
261 + (void) parseContentType: (NSData *) theLine
262 inPart: (CWPart *) thePart
263 {
264 NSRange aRange;
265 NSData *aData;
266 NSUInteger x;
267
268 if ([theLine length] <= 14)
269 {
270 [thePart setContentType: @"text/plain"];
271 return;
272 }
273
274 aData = [[theLine subdataFromIndex: 13] dataByTrimmingWhiteSpaces];
275 if (aData == nil || [aData length] == 0)
276 {
277 [thePart setContentType: @"text/plain"];
278 return;
279 }
280
281 // We first skip the parameters, if we need to
282 x = [aData indexOfCharacter: ';'];
283 if (x != NSNotFound && x > 0)
284 {
285 aData = [aData subdataToIndex: x];
286 }
287
288 // We see if there's a subtype specified for text, if none was specified, we append "/plain"
289 x = [aData indexOfCharacter: '/'];
290
291 if (x == NSNotFound && [aData hasCaseInsensitiveCPrefix: "text"])
292 {
293 [thePart setContentType: [[[aData asciiString] stringByAppendingString: @"/plain"] lowercaseString]];
294 }
295 else
296 {
297 [thePart setContentType: [[aData asciiString] lowercaseString]];
298 }
299
300 //
301 // We decode our boundary (if we need to)
302 //
303 aRange = [theLine rangeOfCString: "boundary" options: NSCaseInsensitiveSearch];
304
305 if (aRange.length > 0)
306 {
307 [thePart setBoundary: [CWParser _parameterValueUsingLine: theLine range: aRange decode: NO charset: nil]];
308 }
309
310 //
311 // We decode our charset (if we need to)
312 //
313 aRange = [theLine rangeOfCString: "charset" options: NSCaseInsensitiveSearch];
314
315 if (aRange.length > 0)
316 {
317 [thePart setCharset: [[CWParser _parameterValueUsingLine: theLine range: aRange decode: NO charset: nil] asciiString]];
318 }
319
320 //
321 // We decode our format (if we need to). See RFC2646.
322 //
323 aRange = [theLine rangeOfCString: "format" options: NSCaseInsensitiveSearch];
324
325 if (aRange.length > 0)
326 {
327 NSData *aFormat;
328
329 aFormat = [CWParser _parameterValueUsingLine: theLine range: aRange decode: NO charset: nil];
330
331 if ([aFormat caseInsensitiveCCompare: "flowed"] == NSOrderedSame)
332 {
333 [thePart setFormat: PantomimeFormatFlowed];
334 }
335 else
336 {
337 [thePart setFormat: PantomimeFormatUnknown];
338 }
339 }
340 else
341 {
342 [thePart setFormat: PantomimeFormatUnknown];
343 }
344
345 //
346 // We decode the parameter "name" iif the thePart is an instance of Part
347 //
348 if ([thePart isKindOfClass: [CWPart class]])
349 {
350 aRange = [theLine rangeOfCString: "name" options: NSCaseInsensitiveSearch];
351
352 if (aRange.length > 0)
353 {
354 [thePart setFilename: [CWParser _parameterValueUsingLine: theLine range: aRange decode: YES charset: [thePart defaultCharset]]];
355 }
356 }
357 }
358
359
360 //
361 //
362 //
363 + (void) parseDate: (NSData *) theLine
364 inMessage: (CWMessage *) theMessage
365 {
366 if ([theLine length] > 6)
367 {
368 NSData *aData;
369 NSUInteger tot;
370 int month, day, year, hours, mins, secs, tz;
371 int len, s;
372 unsigned i, j;
373 unsigned char *bytes, *word;
374
375 aData = [theLine subdataFromIndex: 6];
376 word = malloc(256);
377
378 bytes = (unsigned char*)[aData bytes];
379 tot = [aData length];
380 i = 0;
381 s = 0;
382 tz = 0;
383
384 // date-time = [ day-of-week "," ] date FWS time [CFWS]
385 // day-of-week = ([FWS] day-name) / obs-day-of-week
386 // day-name = "Mon" / "Tue" / "Wed" / "Thu" /
387 // "Fri" / "Sat" / "Sun"
388 // date = day month year
389 // year = 4*DIGIT / obs-year
390 // month = (FWS month-name FWS) / obs-month
391 // month-name = "Jan" / "Feb" / "Mar" / "Apr" /
392 // "May" / "Jun" / "Jul" / "Aug" /
393 // "Sep" / "Oct" / "Nov" / "Dec"
394 //
395 // day = ([FWS] 1*2DIGIT) / obs-day
396 // time = time-of-day FWS zone
397 // time-of-day = hour ":" minute [ ":" second ]
398 // hour = 2DIGIT / obs-hour
399 // minute = 2DIGIT / obs-minute
400 // second = 2DIGIT / obs-second
401 // zone = (( "+" / "-" ) 4DIGIT) / obs-zone
402 //
403 // We need to handle RFC2822 and UNIX time:
404 //
405 // Date: Wed, 02 Jan 2002 09:07:19 -0700
406 // Date: 02 Jan 2002 19:57:49 +0000
407 //
408 // And broken dates such as:
409 //
410 // Date: Thu, 03 Jan 2002 16:40:30 GMT
411 // Date: Wed, 2 Jan 2002 08:56:18 -0700 (MST)
412 // Date: Wed, 9 Jan 2002 10:04:23 -0500 (Eastern Standard Time)
413 // Date: 11-Jan-02
414 // Date: Tue, 15 Jan 2002 15:45:53 -0801
415 // Date: Thu, 17 Jan 2002 11:54:11 -0900<br>
416 //
417 //while (i < tot && isspace(*bytes))
418 // {
419 // i++; bytes++;
420 // }
421
422 len = next_word(bytes, i, tot, word); if (len <= 0) { free(word); return; }
423
424 if (isalpha(*word))
425 {
426 //NSLog(@"UNIX DATE");
427
428 // We skip the first word, no need for it.
429 i += (unsigned)len+1; len = next_word(bytes, i, tot, word); if (len <= 0) { free(word); return; }
430 }
431
432
433 month = -1;
434
435 // We got a RFC 822 date. The syntax is:
436 // day month year hh:mm:ss zone
437 // For example: 03 Apr 2003 17:27:06 +0200
438 //NSLog(@"RFC-822 time");
439 day = atoi((const char*)word);
440
441 //printf("len = %d |%s| day = %d\n", len, word, day);
442
443 // We get the month name and we convert it.
444 i += (unsigned)len+1; len = next_word(bytes, i, tot, word); if (len <= 0) { free(word); return; }
445
446 for (j = 0; j < 12; j++)
447 {
448 if (strncasecmp((const char*)word, month_name[j], 3) == 0)
449 {
450 month = (int)j+1;
451 }
452 }
453
454 if (month < 0) { free(word); return; }
455
456 //printf("len = %d |%s| month = %d\n", len, word, month);
457
458 // We get the year.
459 i += (unsigned)len+1; len = next_word(bytes, i, tot, word); if (len <= 0) { free(word); return; }
460 year = atoi((const char*)word);
461
462 if (year < 70) year += 2000;
463 if (year < 100) year += 1900;
464
465 //printf("len = %d |%s| year = %d\n", len, word, year);
466
467 // We parse the time using the hh:mm:ss format.
468 i += (unsigned)len+1; len = next_word(bytes, i, tot, word); if (len <= 0) { free(word); return; }
469 sscanf((const char*)word, "%d:%d:%d", &hours, &mins, &secs);
470 //printf("len = %d |%s| %d:%d:%d\n", len, word, hours, mins, secs);
471
472 // We parse the timezone.
473 i += (unsigned)len+1; len = next_word(bytes, i, tot, word);
474
475 if (len <= 0)
476 {
477 tz = 0;
478 }
479 else
480 {
481 unsigned char *p;
482
483 p = word;
484
485 if (*p == '-' || *p == '+')
486 {
487 s = (*p == '-' ? -1 : 1);
488 p++;
489 }
490
491 len = (int)strlen((const char*)p);
492
493 if (isdigit(*p))
494 {
495 if (len == 2)
496 {
497 tz = (*(p)-48)*36000+*((p+1)-48)*3600;
498 }
499 else
500 {
501 tz = (*(p)-48)*36000+(*(p+1)-48)*3600+(*(p+2)-48)*10+(*(p+3)-48);
502 }
503 }
504 else
505 {
506 for (j = 0; j < sizeof(timezone_info)/sizeof(timezone_info[0]); j++)
507 {
508 if (strncasecmp((const char*)p, timezone_info[j].name, len) == 0)
509 {
510 tz = timezone_info[j].offset;
511 }
512 }
513 }
514 tz = s*tz;
515 }
516
517 [theMessage setReceivedDate: [NSCalendarDate dateWithYear: year
518 month: (NSUInteger) month
519 day: (NSUInteger)day
520 hour: (NSUInteger) hours
521 minute: (NSUInteger) mins
522 second: (NSUInteger) secs
523 timeZone: [NSTimeZone timeZoneForSecondsFromGMT: tz]]];
524 free(word);
525 }
526 }
527
528 //
529 //
530 //
531 + (NSData *) parseDestination: (NSData *) theLine
532 forType: (PantomimeRecipientType) theType
533 inMessage: (CWMessage *) theMessage
534 quick: (BOOL) theBOOL
535 {
536 CWInternetAddress *anInternetAddress;
537 NSData *aData;
538
539 NSUInteger i, len, x;
540 unsigned char *bytes;
541 BOOL b;
542
543 if (theBOOL)
544 {
545 aData = theLine;
546 }
547 else
548 {
549 switch (theType)
550 {
551 case PantomimeBccRecipient:
552 len = 5;
553 break;
554
555 case PantomimeCcRecipient:
556 case PantomimeToRecipient:
557 len = 4;
558 break;
559
560 case PantomimeResentBccRecipient:
561 len = 12;
562 break;
563
564 case PantomimeResentCcRecipient:
565 case PantomimeResentToRecipient:
566 len = 11;
567 break;
568 default:
569 NSDebugLog(@"CWParser parseDestination: forType: unknown type!");
570 len = 0;
571 break;
572 }
573
574 // We skip over emtpy headers.
575 if (len >= [theLine length]) return [NSData data];
576
577 aData = [theLine subdataFromIndex: len];
578 }
579
580 bytes = (unsigned char*)[aData bytes];
581 len = [aData length];
582 b = NO; x = 0;
583
584 for (i = 0; i < len; i++)
585 {
586 if (*bytes == '"')
587 {
588 b = !b;
589 }
590
591 if (*bytes == ',' || i == len-1)
592 {
593 NSData *headerData;
594 NSRange headerRange;
595
596 if (b)
597 {
598 bytes++;
599 continue;
600 }
601
602 // We strip the trailing comma for all but the last entries.
603 if (i == len-1)
604 headerRange = NSMakeRange(x, i-x+1);
605 else
606 headerRange = NSMakeRange(x, i-x);
607
608 headerData = [aData subdataWithRange: headerRange];
609 headerData = [headerData dataByTrimmingWhiteSpaces];
610 anInternetAddress = [[CWInternetAddress alloc]
611 initWithString: [CWMIMEUtility decodeHeader: headerData
612 charset: [theMessage defaultCharset]]];
613
614 if (anInternetAddress != nil)
615 {
616 [anInternetAddress setType: theType];
617 [theMessage addRecipient: anInternetAddress];
618 RELEASE(anInternetAddress);
619 }
620 x = i+1;
621 }
622
623 bytes++;
624 }
625
626 return aData;
627 }
628
629 //
630 //
631 //
632 + (NSData *) parseFrom: (NSData *) theLine
633 inMessage: (CWMessage *) theMessage
634 quick: (BOOL) theBOOL;
635 {
636 CWInternetAddress *anInternetAddress;
637 NSData *aData;
638
639 if (!theBOOL && !([theLine length] > 6))
640 {
641 return [NSData data];
642 }
643
644 if (theBOOL)
645 {
646 aData = theLine;
647 }
648 else
649 {
650 aData = [theLine subdataFromIndex: 6];
651 }
652
653 anInternetAddress = [[CWInternetAddress alloc] initWithString: [CWMIMEUtility decodeHeader: aData charset: [theMessage defaultCharset]]];
654
655 [theMessage setFrom: anInternetAddress];
656 RELEASE(anInternetAddress);
657
658 return aData;
659 }
660
661
662 //
663 // This method is used to parse the In-Reply-To: header value.
664 //
665 + (NSData *) parseInReplyTo: (NSData *) theLine
666 inMessage: (CWMessage *) theMessage
667 quick: (BOOL) theBOOL
668 {
669 NSData *aData;
670 NSUInteger x, y;
671
672 if (theBOOL)
673 {
674 aData = theLine;
675 }
676 else if ([theLine length] > 13)
677 {
678 aData = [theLine subdataFromIndex: 13];
679 }
680 else
681 {
682 return [NSData data];
683 }
684
685 // We check for lame headers like that:
686 //
687 // In-Reply-To: <[email protected]> (Markus Deuling's message of "Tue, 05 Dec 2006 08:02:23 +0100")
688 // In-Reply-To: <[email protected]>; from [email protected] on Wed, Mar 26, 2003 at 11:23:37AM -0800
689 //
690 x = [aData indexOfCharacter: ';'];
691 y = [aData indexOfCharacter: ' '];
692
693 if (x != NSNotFound && x > 0 && x < y)
694 {
695 aData = [aData subdataToIndex: x];
696 }
697 else if (y != NSNotFound && y > 0)
698 {
699 aData = [aData subdataToIndex: y];
700 }
701
702 [theMessage setInReplyTo: [aData asciiString]];
703
704 return aData;
705 }
706
707
708 //
709 //
710 //
711 + (NSData *) parseMessageID: (NSData *) theLine
712 inMessage: (CWMessage *) theMessage
713 quick: (BOOL) theBOOL
714 {
715 NSData *aData;
716
717 if (!theBOOL && !([theLine length] > 12))
718 {
719 return [NSData data];
720 }
721
722 if (theBOOL) aData = theLine;
723 else aData = [theLine subdataFromIndex: 12];
724
725 [theMessage setMessageID: [[aData dataByTrimmingWhiteSpaces] asciiString]];
726 return aData;
727 }
728
729
730 //
731 //
732 //
733 + (void) parseMIMEVersion: (NSData *) theLine
734 inMessage: (CWMessage *) theMessage
735 {
736 if ([theLine length] > 14)
737 {
738 [theMessage setMIMEVersion: [[theLine subdataFromIndex: 14] asciiString]];
739 }
740 }
741
742
743 //
744 //
745 //
746 + (NSData *) parseReferences: (NSData *) theLine
747 inMessage: (CWMessage *) theMessage
748 quick: (BOOL) theBOOL
749 {
750 NSData *aData;
751
752 aData = nil;
753
754 if (theBOOL)
755 {
756 aData = theLine;
757 }
758 else if ([theLine length] > 12)
759 {
760 aData = [theLine subdataFromIndex: 12];
761 }
762
763 if (aData && [aData length])
764 {
765 NSMutableArray *aMutableArray;
766 NSArray *allReferences;
767 NSData *aReference;
768 NSString *aString;
769 NSUInteger i, count;
770
771 allReferences = [aData componentsSeparatedByCString: " "];
772 count = [allReferences count];
773
774 aMutableArray = [[NSMutableArray alloc] initWithCapacity: count];
775
776 for (i = 0; i < count; i++)
777 {
778 aReference = [allReferences objectAtIndex: i];
779 aString = [aReference asciiString];
780
781 // We protect ourself against values that could hold 8-bit characters.
782 if (aString)
783 {
784 [aMutableArray addObject: aString];
785 }
786 }
787
788 [theMessage setReferences: aMutableArray];
789 RELEASE(aMutableArray);
790
791 return aData;
792 }
793
794 return [NSData data];
795 }
796
797
798 //
799 //
800 //
801 + (void) parseReplyTo: (NSData *) theLine
802 inMessage: (CWMessage *) theMessage
803 {
804 if ([theLine length] > 10)
805 {
806 CWInternetAddress *anInternetAddress;
807 NSMutableArray *aMutableArray;
808 NSData *aData;
809
810 unsigned char *bytes;
811 NSUInteger i, len;
812 int s_len, x, y;
813 BOOL b;
814
815 aMutableArray = [[NSMutableArray alloc] init];
816 aData = [theLine subdataFromIndex: 10];
817 bytes = (unsigned char*)[aData bytes];
818 len = [aData length];
819 b = NO; x = 0;
820
821 for (i = 0; i < len; i++)
822 {
823 if (*bytes == '"')
824 {
825 b = !b;
826 }
827
828 if (*bytes == ',' || i == len-1)
829 {
830 if (b)
831 {
832 bytes++;
833 continue;
834 }
835
836 y = i;
837
838 // We strip the trailing comma for all but the last entries.
839 s_len = y-x;
840 if (i == len-1) s_len++;
841
842 anInternetAddress = [[CWInternetAddress alloc]
843 initWithString: [CWMIMEUtility decodeHeader: [[aData subdataWithRange: NSMakeRange(x, s_len)] dataByTrimmingWhiteSpaces]
844 charset: [theMessage defaultCharset]]];
845 if (anInternetAddress != nil)
846 {
847 [aMutableArray addObject: anInternetAddress];
848 RELEASE(anInternetAddress);
849 }
850 x = y+1;
851 }
852
853 bytes++;
854 }
855
856 if ([aMutableArray count])
857 {
858 [theMessage setReplyTo: aMutableArray];
859 }
860
861 RELEASE(aMutableArray);
862 }
863 }
864
865
866 //
867 //
868 //
869 + (void) parseResentFrom: (NSData *) theLine
870 inMessage: (CWMessage *) theMessage
871 {
872 if ([theLine length] > 13)
873 {
874 CWInternetAddress *anInternetAddress;
875
876 anInternetAddress = [[CWInternetAddress alloc] initWithString: [CWMIMEUtility decodeHeader:
877 [theLine subdataFromIndex: 13]
878 charset: [theMessage defaultCharset]]];
879
880 [theMessage setResentFrom: anInternetAddress];
881 RELEASE(anInternetAddress);
882 }
883 }
884
885
886 //
887 //
888 //
889 + (void) parseStatus: (NSData *) theLine
890 inMessage: (CWMessage *) theMessage
891 {
892 if ([theLine length] > 8)
893 {
894 NSData *aData;
895
896 aData = [theLine subdataFromIndex: 8];
897 [[theMessage flags] addFlagsFromData: aData format: PantomimeFormatMbox];
898 [theMessage addHeader: @"Status" withValue: [aData asciiString]];
899 }
900 }
901
902
903 //
904 //
905 //
906 + (void) parseXStatus: (NSData *) theLine
907 inMessage: (CWMessage *) theMessage
908 {
909 if ([theLine length] > 10)
910 {
911 NSData *aData;
912
913 aData = [theLine subdataFromIndex: 10];
914 [[theMessage flags] addFlagsFromData: aData format: PantomimeFormatMbox];
915 [theMessage addHeader: @"X-Status" withValue: [aData asciiString]];
916 }
917 }
918
919
920 //
921 //
922 //
923 + (NSData *) parseSubject: (NSData *) theLine
924 inMessage: (CWMessage *) theMessage
925 quick: (BOOL) theBOOL
926 {
927 NSData *aData;
928
929 if (theBOOL)
930 {
931 aData = theLine;
932 }
933 else if ([theLine length] > 9)
934 {
935 aData = [[theLine subdataFromIndex: 8] dataByTrimmingWhiteSpaces];
936 }
937 else
938 {
939 return [NSData data];
940 }
941
942 [theMessage setSubject: [CWMIMEUtility decodeHeader: aData charset: [theMessage defaultCharset]]];
943
944 return aData;
945 }
946
947
948 //
949 //
950 //
951 + (void) parseUnknownHeader: (NSData *) theLine
952 inMessage: (CWMessage *) theMessage
953 {
954 NSData *aName, *aValue;
955 NSRange range;
956
957 range = [theLine rangeOfCString: ":"];
958
959 if (range.location != NSNotFound)
960 {
961 aName = [theLine subdataWithRange: NSMakeRange(0, range.location)];
962
963 // we keep only the headers that have a value
964 if (([theLine length]-range.location-1) > 0)
965 {
966 aValue = [theLine subdataWithRange: NSMakeRange(range.location + 2, [theLine length]-range.location-2)];
967
968 [theMessage addHeader: [aName asciiString] withValue: [aValue asciiString]];
969 }
970 }
971 }
972
973
974 //
975 //
976 //
977 + (void) parseOrganization: (NSData *) theLine
978 inMessage: (CWMessage *) theMessage
979 {
980 NSString *organization;
981
982 if ([theLine length] > 14)
983 {
984 organization = [CWMIMEUtility decodeHeader: [[theLine subdataFromIndex: 13] dataByTrimmingWhiteSpaces]
985 charset: [theMessage defaultCharset]];
986 }
987 else
988 {
989 organization = @"";
990 }
991
992 [theMessage setOrganization: organization];
993 }
994
995 @end
996
997
998 //
999 // private methods
1000 //
1001 @implementation CWParser (Private)
1002
1003 + (id) _parameterValueUsingLine: (NSData *) theLine
1004 range: (NSRange) theRange
1005 decode: (BOOL) theBOOL
1006 charset: (NSString *) theCharset
1007 {
1008 NSMutableData *aMutableData;
1009 NSRange r1, r2;
1010
1011 int value_start, value_end, parameters_count,len;
1012 BOOL is_rfc2231, has_charset;
1013
1014 is_rfc2231 = has_charset = NO;
1015 len = [theLine length];
1016
1017 //
1018 // Look for the first occurrence of '=' before the end of the line within
1019 // our range. That marke the beginning of the value. If we don't find one,
1020 // we set the beggining right after the end of the key tag
1021 //
1022 r1 = [theLine rangeOfCString: "="
1023 options: 0
1024 range: NSMakeRange(NSMaxRange(theRange), len-NSMaxRange(theRange))];
1025
1026 if (r1.length > 0)
1027 {
1028 // If "=" was found, but after ";", something is very broken
1029 // and we just return nil. That can happen if we have a Content-Type like:
1030 //
1031 // Content-Type: text/x-patch; name=mpg321-format-string.diff; charset=ISO-8859-1
1032 //
1033 // "format" is part of the _name_ parameter. It has nothing to do with format=flowed.
1034 //
1035 #warning FIXME - consider format= when passing the parameter range
1036 #if 0
1037 if (r1.location > value_end)
1038 {
1039 return nil;
1040 }
1041 #endif
1042
1043 value_start = r1.location+r1.length;
1044 }
1045 else
1046 {
1047 value_start = theRange.location+theRange.length;
1048 }
1049
1050 // The parameter can be quoted or not like this (for example, with a charset):
1051 // charset="us-ascii"
1052 // charset = "us-ascii"
1053 // charset=us-ascii
1054 // charset = us-ascii
1055 // It can be terminated by ';' or end of line.
1056
1057 // Look for the first occurrence of ';'.
1058 // That marks the end of this key value pair.
1059 // If we don't find one, we set it to the end of the line.
1060 r1 = [theLine rangeOfCString: ";"
1061 options: 0
1062 range: NSMakeRange(NSMaxRange(theRange), len-NSMaxRange(theRange))];
1063
1064 if (r1.length > 0)
1065 {
1066 value_end = r1.location-1;
1067 }
1068 else
1069 {
1070 value_end = len-1;
1071 }
1072
1073 // We now have a range that should contain our value.
1074 // Build a NSRange out of it.
1075 if (value_end - value_start + 1 > 0)
1076 {
1077 r1 = NSMakeRange(value_start, value_end-value_start+1);
1078 }
1079 else
1080 {
1081 r1 = NSMakeRange(value_start, 0);
1082 }
1083
1084 aMutableData = AUTORELEASE([[NSMutableData alloc] initWithData: [[[theLine subdataWithRange: r1] dataByTrimmingWhiteSpaces] dataFromQuotedData]]);
1085
1086 //
1087 // VERY IMPORTANT:
1088 //
1089 // We check if something was encoded using RFC2231. We need to adjust
1090 // value_end if we find a multi-line parameter. We also proceed
1091 // with data substitution while we loop for parameter values unfolding.
1092 //
1093 if ([theLine characterAtIndex: NSMaxRange(theRange)] == '*')
1094 {
1095 is_rfc2231 = YES;
1096
1097 // We consider parameter value continuations (Section 3. of the RFC)
1098 // in order the set the appropriate end boundary.
1099 if ([theLine characterAtIndex: NSMaxRange(theRange)+1] == '0')
1100 {
1101 // int end;
1102
1103 // We check if we have a charset, in case of a multiline value.
1104 if ([theLine characterAtIndex: NSMaxRange(theRange)+2] == '*')
1105 {
1106 has_charset = YES;
1107 }
1108
1109 r1.location = theRange.location;
1110 r1.length = theRange.length;
1111 parameters_count = 1;
1112
1113 while (YES)
1114 {
1115 // end = NSMaxRange(r1);
1116 r1 = [theLine rangeOfCString: [[NSString stringWithFormat: @"%s*%i",
1117 [[theLine subdataWithRange: theRange] cString],
1118 parameters_count] UTF8String]
1119 options: 0
1120 range: NSMakeRange(NSMaxRange(r1), len-NSMaxRange(r1))];
1121 parameters_count++;
1122
1123 if (r1.location != NSNotFound)
1124 {
1125 value_start = NSMaxRange(r1);
1126
1127 // now we may need to skip *=, when data is encoded like
1128 // filename*1*=%56%67;
1129 // else it is like
1130 // filename*1="xxxxx";
1131 if ([theLine characterAtIndex: value_start] == '*')
1132 value_start += 2;
1133 else
1134 value_start += 1;
1135
1136
1137 r2 = [theLine rangeOfCString: ";" options: 0 range: NSMakeRange(NSMaxRange(r1), len-NSMaxRange(r1))];
1138
1139 if (r2.length > 0)
1140 {
1141 value_end = r2.location;
1142 }
1143 else
1144 {
1145 value_end = len;
1146 }
1147
1148 [aMutableData appendData: [[[theLine subdataWithRange: NSMakeRange(value_start,value_end-value_start)]
1149 dataFromSemicolonTerminatedData] dataFromQuotedData]];
1150 }
1151 else
1152 {
1153 break;
1154 }
1155 }
1156 }
1157 else if ([theLine characterAtIndex: NSMaxRange(theRange)+1] == '=')
1158 {
1159 has_charset = YES;
1160 }
1161 }
1162
1163 if (is_rfc2231)
1164 {
1165 if (has_charset)
1166 {
1167 BOOL lang = NO;
1168 NSData *aCharset;
1169
1170 aCharset = nil;
1171 r1 = [aMutableData rangeOfCString: "'"];
1172
1173 if (r1.location != NSNotFound)
1174 {
1175 lang = YES;
1176 // We check for a language
1177 r2 = [aMutableData rangeOfCString: "'" options: 0
1178 range: NSMakeRange(NSMaxRange(r1),
1179 [aMutableData length]-NSMaxRange(r1))];
1180
1181 if (r2.length && r2.location > r1.location+1)
1182 {
1183 NSLog(@"WE'VE GOT A LANGUAGE!");
1184 }
1185
1186 aCharset = [aMutableData subdataToIndex: r1.location];
1187
1188 // We strip the charset and the language information from our data
1189 [aMutableData replaceBytesInRange: NSMakeRange(0, NSMaxRange(r2))
1190 withBytes: NULL
1191 length: 0];
1192 }
1193
1194 if (theBOOL)
1195 {
1196 NSString *aString;
1197
1198 aString = AUTORELEASE([[NSString alloc] initWithData: aMutableData encoding: NSASCIIStringEncoding]);
1199
1200 if (lang)
1201 {
1202 return [aString stringByReplacingPercentEscapesUsingEncoding:
1203 [NSString encodingForCharset: aCharset]];
1204 }
1205 else
1206 {
1207 return aString;
1208 }
1209 }
1210 }
1211 else
1212 {
1213 NSString *aString;
1214
1215 aString = AUTORELEASE([[NSString alloc] initWithData: aMutableData encoding: NSASCIIStringEncoding]);
1216
1217 return aString;
1218 }
1219 }
1220 else
1221 {
1222 if (theBOOL)
1223 {
1224 return [CWMIMEUtility decodeHeader: aMutableData charset: theCharset];
1225 }
1226 }
1227
1228 return aMutableData;
1229 }
1230
1231 @end