Debian Codegraph @

Framework/Pantomime/CWMessage.m

raw
1 /*
2 ** CWMessage.m
3 **
4 ** Copyright (c) 2001-2007 Ludovic Marcotte
5 ** Copyright (C) 2016-2020 Riccardo Mottola
6 **
7 ** Author: Ludovic Marcotte <[email protected]>
8 ** Riccardo Mottola
9 ** Sebastian Reitenbach
10 **
11 ** This library is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU Lesser General Public
13 ** License as published by the Free Software Foundation; either
14 ** version 2.1 of the License, or (at your option) any later version.
15 **
16 ** This library is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** Lesser General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #import <Pantomime/CWMessage.h>
26
27 #import <Pantomime/CWFlags.h>
28 #import <Pantomime/CWFolder.h>
29 #import <Pantomime/CWInternetAddress.h>
30 #import <Pantomime/CWMIMEMultipart.h>
31 #import <Pantomime/CWMIMEUtility.h>
32 #import <Pantomime/CWRegEx.h>
33 #import <Pantomime/NSData+Extensions.h>
34 #import <Pantomime/NSString+Extensions.h>
35 #import <Pantomime/CWParser.h>
36
37 #import <Foundation/NSAutoreleasePool.h>
38 #import <Foundation/NSBundle.h>
39 #import <Foundation/NSEnumerator.h>
40 #import <Foundation/NSTimeZone.h>
41 #import <Foundation/NSUserDefaults.h>
42
43 #import <Pantomime/CWIMAPCacheManager.h>
44
45 #include <stdlib.h>
46 #include <string.h>
47 #include <time.h>
48
49 #define CHECK_RANGE(r,len) (r.location < len && (r.length < len-r.location))
50 #define LF "\n"
51
52 static int currentMessageVersion = 2;
53
54 static CWRegEx *atLeastOneSpaceRegex = nil;
55 static CWRegEx *suffixSubjTrailerRegex = nil;
56 static CWRegEx *prefixSubjLeaderRegex = nil;
57 static CWRegEx *prefixSubjBlobRegex = nil;
58 static CWRegEx *prefixSubjFwdHdrAndSuffixSubjFwdTrlRegex = nil;
59
60 //
61 // Private methods
62 //
63 @interface CWMessage (Private)
64
65 - (NSString *) _computeBaseSubject;
66 - (void) _extractText: (NSMutableData *) theMutableData
67 part: (id) thePart
68 quote: (BOOL *) theBOOL;
69 - (NSData *) _formatRecipientsWithType: (PantomimeRecipientType) theType;
70
71 @end
72
73
74 //
75 //
76 //
77 @implementation CWMessage
78
79 //
80 // We setup regexes used in _computeBaseSubject
81 //
82 + (void) initialize
83 {
84 if (atLeastOneSpaceRegex == nil)
85 {
86 NSString *blobChar = @"[^][]";
87 NSString *subjTrailer = @"(\\(fwd\\))| ";
88 NSString *subjFwdHdr = @"\\[fwd:";
89 NSString *subjFwdTrl = @"\\]";
90 NSString *subjBlob = [NSString stringWithFormat:@"\\[(%@)*\\] *", blobChar];
91 NSString *subjReFwd = [NSString stringWithFormat:@"((re)|(fwd?)) *(%@)?:", subjBlob];
92 NSString *subjLeader = [NSString stringWithFormat:@"((%@)*%@)| ", subjBlob, subjReFwd];
93
94 atLeastOneSpaceRegex = [[CWRegEx alloc] initWithPattern: @"[[:space:]]+"
95 flags: REG_EXTENDED|REG_ICASE];
96 suffixSubjTrailerRegex = [[CWRegEx alloc] initWithPattern: [NSString stringWithFormat:@"(%@)*$", subjTrailer]
97 flags: REG_EXTENDED|REG_ICASE];
98 prefixSubjLeaderRegex = [[CWRegEx alloc] initWithPattern: [NSString stringWithFormat:@"^(%@)", subjLeader]
99 flags: REG_EXTENDED|REG_ICASE];
100 prefixSubjBlobRegex = [[CWRegEx alloc] initWithPattern: [NSString stringWithFormat:@"^(%@)", subjBlob]
101 flags: REG_EXTENDED|REG_ICASE];
102 prefixSubjFwdHdrAndSuffixSubjFwdTrlRegex = [[CWRegEx alloc] initWithPattern: [NSString stringWithFormat:@"^(%@)(.*)(%@)$", subjFwdHdr, subjFwdTrl]
103 flags: REG_EXTENDED|REG_ICASE];
104
105 [CWMessage setVersion: currentMessageVersion];
106 }
107 }
108
109
110 //
111 //
112 //
113 - (id) init
114 {
115 self = [super init];
116 if (self)
117 {
118 _recipients = [[NSMutableArray alloc] init];
119 _flags = [[CWFlags alloc] init];
120 _initialized = NO;
121 _references = nil;
122 _folder = nil;
123
124 // By default, we want the subclass's rawSource method to be called so we set our
125 // rawSource ivar to nil. If it's not nil (ONLY set in initWithData) it'll be returned,
126 // for performances improvements.
127 _rawSource = nil;
128
129 // We initialize our dictionary holding all extra properties a message might have
130 _properties = [[NSMutableDictionary alloc] init];
131 }
132 return self;
133 }
134
135
136 //
137 //
138 //
139 - (id) initWithData: (NSData *) theData
140 {
141 self = [super initWithData: theData];
142
143 // Part: -initWithData could fail. We return nil if it does.
144 if (self)
145 {
146 //
147 // We can tell now that this message is fully initialized
148 // NOTE: We must NOT call [self setInitialized: YES] since
149 // it will call the method from the subclass and may do
150 // extremely weird things.
151 _initialized = YES;
152
153 // We set our rawSource ivar for performance reasons
154 [self setRawSource: theData];
155 }
156 return self;
157 }
158
159
160
161 //
162 //
163 //
164 - (id) initWithHeadersFromData: (NSData *) theHeaders
165 {
166 self = [self init];
167 {
168 [self setHeadersFromData: theHeaders];
169 }
170 return self;
171 }
172
173
174 //
175 //
176 //
177 - (id) initWithHeaders: (NSDictionary *) theHeaders
178 {
179 self = [self init];
180 if (self)
181 {
182 [self setHeaders: theHeaders];
183 }
184 return self;
185 }
186
187
188 //
189 //
190 //
191 - (void) dealloc
192 {
193 RELEASE(_recipients);
194 RELEASE(_properties);
195 RELEASE(_references);
196 RELEASE(_rawSource);
197 RELEASE(_flags);
198
199 [super dealloc];
200 }
201
202
203 //
204 // NSCoding protocol
205 //
206 - (void) encodeWithCoder: (NSCoder *) theCoder
207 {
208 // Must also encode Part's superclass
209 [super encodeWithCoder: theCoder];
210
211 [theCoder encodeObject: [self receivedDate]]; // Date
212 [theCoder encodeObject: [self from]]; // From
213 [theCoder encodeObject: _recipients]; // To and Cc (Bcc, at worst)
214 [theCoder encodeObject: [self subject]]; // Subject
215 [theCoder encodeObject: [self messageID]]; // Message-ID
216 [theCoder encodeObject: [self MIMEVersion]]; // MIME-Version
217 [theCoder encodeObject: _references]; // References
218 [theCoder encodeObject: [self inReplyTo]]; // In-Reply-To
219
220 [theCoder encodeObject: [NSNumber numberWithUnsignedInt: _message_number]]; // Message number
221 [theCoder encodeObject: _flags]; // Message flags
222 }
223
224
225 //
226 //
227 //
228 - (id) initWithCoder: (NSCoder *) theCoder
229 {
230 // Must also decode Part's superclass
231 self = [super initWithCoder: theCoder];
232 if (self)
233 {
234 _properties = [[NSMutableDictionary alloc] init];
235 _recipients = [[NSMutableArray alloc] init];
236
237 [self setReceivedDate: [theCoder decodeObject]]; // Date
238 [self setFrom: [theCoder decodeObject]]; // From
239 [self setRecipients: [theCoder decodeObject]]; // To and Cc (Bcc, at worst)
240 [self setSubject: [theCoder decodeObject]]; // Subject
241 [self setMessageID: [theCoder decodeObject]]; // Message-ID
242 [self setMIMEVersion: [theCoder decodeObject]]; // MIME-Version
243 [self setReferences: [theCoder decodeObject]]; // References
244
245 [self setInReplyTo: [theCoder decodeObject]]; // In-Reply-To
246 [self setMessageNumber: [[theCoder decodeObject] unsignedIntValue]]; // Message number
247
248 // We decode our flags. We must not simply call [self setFlags: [theCoder decodeObject]]
249 // since IMAPMessage is re-implementing flags and that would cause problems when
250 // unarchiving IMAP caches.
251 _flags = [[CWFlags alloc] init];
252 [_flags replaceWithFlags: [theCoder decodeObject]];
253
254 // It's very important to set the "initialized" ivar to NO since we didn't serialize the content.
255 // or our message.
256 _initialized = NO;
257
258 // We initialize the rest of our ivars
259 _rawSource = nil;
260 _folder = nil;
261 }
262 return self;
263 }
264
265
266 //
267 // NSCopying protocol (FIXME)
268 //
269 - (id) copyWithZone: (NSZone *) zone
270 {
271 return RETAIN(self);
272 }
273
274
275 //
276 //
277 //
278 - (CWInternetAddress *) from
279 {
280 return [_headers objectForKey: @"From"];
281 }
282
283
284 //
285 //
286 //
287 - (void) setFrom: (CWInternetAddress *) theInternetAddress
288 {
289 if (theInternetAddress)
290 {
291 [_headers setObject: theInternetAddress forKey: @"From"];
292 }
293 }
294
295
296 //
297 //
298 //
299 - (unsigned int) messageNumber
300 {
301 return _message_number;
302 }
303
304
305 //
306 //
307 //
308 - (void) setMessageNumber: (unsigned int) theMessageNumber
309 {
310 _message_number = theMessageNumber;
311 }
312
313
314 //
315 //
316 //
317 - (NSString *) messageID
318 {
319 NSString *aString;
320
321 aString = [_headers objectForKey: @"Message-ID"];
322
323 if (!aString)
324 {
325 aString = [[CWMIMEUtility globallyUniqueID] asciiString];
326 [self setMessageID: aString];
327 }
328
329 return aString;
330 }
331
332
333 //
334 //
335 //
336 - (void) setMessageID: (NSString *) theMessageID
337 {
338 if (theMessageID)
339 {
340 [_headers setObject: theMessageID forKey: @"Message-ID"];
341 }
342 }
343
344
345 //
346 //
347 //
348 - (NSString *) inReplyTo
349 {
350 return [_headers objectForKey: @"In-Reply-To"];
351 }
352
353
354 //
355 //
356 //
357 - (void) setInReplyTo: (NSString *) theInReplyTo
358 {
359 if (theInReplyTo)
360 {
361 [_headers setObject: theInReplyTo forKey: @"In-Reply-To"];
362 }
363 }
364
365
366 //
367 //
368 //
369 - (NSCalendarDate *) receivedDate
370 {
371 return [_headers objectForKey: @"Date"];
372 }
373
374
375 //
376 //
377 //
378 - (void) setReceivedDate: (NSCalendarDate*) theDate
379 {
380 if (theDate)
381 {
382 [_headers setObject: theDate forKey: @"Date"];
383 }
384 }
385
386
387 //
388 //
389 //
390 - (void) addRecipient: (CWInternetAddress *) theAddress
391 {
392 if (theAddress)
393 {
394 [_recipients addObject: theAddress];
395 }
396 }
397
398
399 //
400 //
401 //
402 - (void) removeRecipient: (CWInternetAddress *) theAddress
403 {
404 if (theAddress)
405 {
406 [_recipients removeObject: theAddress];
407 }
408 }
409
410
411 //
412 //
413 //
414 - (NSArray *) recipients
415 {
416 return _recipients;
417 }
418
419
420 //
421 //
422 //
423 - (void) setRecipients: (NSArray *) theRecipients
424 {
425 [_recipients removeAllObjects];
426
427 if (theRecipients)
428 {
429 [_recipients addObjectsFromArray: theRecipients];
430 }
431 }
432
433
434 //
435 //
436 //
437 - (NSUInteger) recipientsCount
438 {
439 return [_recipients count];
440 }
441
442
443 //
444 //
445 //
446 - (void) removeAllRecipients
447 {
448 [_recipients removeAllObjects];
449 }
450
451
452 //
453 //
454 //
455 - (NSArray *) replyTo
456 {
457 return [_headers objectForKey: @"Reply-To"];
458 }
459
460
461 //
462 //
463 //
464 - (void) setReplyTo: (NSArray *) theAddressList
465 {
466 if (theAddressList && [theAddressList count])
467 {
468 [_headers setObject: theAddressList forKey: @"Reply-To"];
469 }
470 else
471 {
472 [_headers removeObjectForKey: @"Reply-To"];
473 }
474 }
475
476
477 //
478 //
479 //
480 - (NSString *) subject
481 {
482 return [_headers objectForKey: @"Subject"];
483 }
484
485
486 //
487 //
488 //
489 - (void) setSubject: (NSString *) theSubject
490 {
491 if (theSubject)
492 {
493 [_headers setObject: theSubject forKey: @"Subject"];
494
495 // We invalidate our previous base subject.
496 [self setBaseSubject: nil];
497 }
498 }
499
500
501 //
502 //
503 //
504 - (NSString *) baseSubject
505 {
506 NSString *baseSubject;
507
508 baseSubject = [self propertyForKey: @"baseSubject"];
509
510 if (!baseSubject)
511 {
512 baseSubject = [self _computeBaseSubject];
513 [self setBaseSubject: baseSubject];
514 }
515
516 return baseSubject;
517 }
518
519
520 //
521 //
522 //
523 - (void) setBaseSubject: (NSString *) theBaseSubject
524 {
525 [self setProperty: theBaseSubject forKey: @"baseSubject"];
526 }
527
528
529 //
530 //
531 //
532 - (BOOL) isInitialized
533 {
534 return _initialized;
535 }
536
537
538 //
539 //
540 //
541 - (void) setInitialized: (BOOL) theBOOL
542 {
543 _initialized = theBOOL;
544 }
545
546
547 //
548 //
549 //
550 - (NSString *) organization
551 {
552 return [_headers objectForKey: @"Organization"];
553 }
554
555
556 //
557 //
558 //
559 - (void) setOrganization: (NSString *) theOrganization
560 {
561 [_headers setObject: theOrganization forKey: @"Organization"];
562 }
563
564
565 //
566 //
567 //
568 - (id) propertyForKey: (id) theKey
569 {
570 return [_properties objectForKey: theKey];
571 }
572
573
574 //
575 //
576 //
577 - (void) setProperty: (id) theProperty
578 forKey: (id) theKey
579 {
580 if (theProperty)
581 {
582 [_properties setObject: theProperty forKey: theKey];
583 }
584 else
585 {
586 [_properties removeObjectForKey: theKey];
587 }
588 }
589
590
591 //
592 //
593 //
594 - (NSArray *) allReferences
595 {
596 return _references;
597 }
598
599
600 //
601 //
602 //
603 - (void) setReferences: (NSArray *) theReferences
604 {
605 ASSIGN(_references, theReferences);
606 }
607
608
609 //
610 //
611 //
612 - (CWFlags *) flags
613 {
614 return _flags;
615 }
616
617
618 //
619 //
620 //
621 - (void) setFlags: (CWFlags *) theFlags
622 {
623 ASSIGN(_flags, theFlags);
624 }
625
626
627 //
628 //
629 //
630 - (NSString *) MIMEVersion
631 {
632 return [_headers objectForKey: @"MIME-Version"];
633 }
634
635
636 //
637 //
638 //
639 - (void) setMIMEVersion: (NSString *) theMIMEVersion
640 {
641 if (theMIMEVersion)
642 {
643 [_headers setObject: theMIMEVersion forKey: @"MIME-Version"];
644 }
645 }
646
647
648 //
649 //
650 //
651 - (CWMessage *) reply: (PantomimeReplyMode) theMode
652 {
653 CWInternetAddress *anInternetAddress;
654 NSMutableData *aMutableData;
655 CWMessage *theMessage;
656 BOOL needsToQuote;
657
658 theMessage = [[CWMessage alloc] init];
659 [theMessage setContentType: @"text/plain"];
660 [theMessage setCharset: @"utf-8"];
661
662 // We set the subject of our message
663 if (![self subject])
664 {
665 [theMessage setSubject: _(@"Re: your mail")];
666 }
667 else if ([[[self subject] stringByTrimmingWhiteSpaces] hasREPrefix])
668 {
669 [theMessage setSubject: [self subject]];
670 }
671 else
672 {
673 [theMessage setSubject: [NSString stringWithFormat: _(@"Re: %@"), [[self subject] stringByTrimmingWhiteSpaces]]];
674 }
675
676 // If Reply-To is defined, we use it. Otherwise, we use From:
677 if ([self replyTo] == nil)
678 {
679 anInternetAddress = [self from];
680 [anInternetAddress setType: PantomimeToRecipient];
681 [theMessage addRecipient: anInternetAddress];
682 }
683 else
684 {
685 NSUInteger i;
686
687 for (i = 0; i < [[self replyTo] count]; i++)
688 {
689 anInternetAddress = [[self replyTo] objectAtIndex: i];
690 [anInternetAddress setType: PantomimeToRecipient];
691 [theMessage addRecipient: anInternetAddress];
692 }
693 }
694
695
696
697 // We add our In-Reply-To header
698 if ([self messageID])
699 {
700 [theMessage setInReplyTo: [self messageID]];
701 }
702
703 // If we reply to all, we add the other recipients
704 if ((theMode&PantomimeReplyAllMode) == PantomimeReplyAllMode)
705 {
706 NSEnumerator *anEnumerator;
707
708 anEnumerator = [_recipients objectEnumerator];
709
710 while ((anInternetAddress = [anEnumerator nextObject]))
711 {
712 [anInternetAddress setType: PantomimeCcRecipient];
713 [theMessage addRecipient: anInternetAddress];
714 }
715 }
716
717 // If it's a "simple" reply, we don't compute a content.
718 if ((theMode&PantomimeSimpleReplyMode) == PantomimeSimpleReplyMode)
719 {
720 [theMessage setContent: [NSData data]];
721 return AUTORELEASE(theMessage);
722 }
723
724
725 // We finally work on the content of the message
726 aMutableData = [[NSMutableData alloc] init];
727 needsToQuote = NO;
728
729 [self _extractText: aMutableData part: self quote: &needsToQuote];
730
731 //
732 // It was impossible for use to find a text/plain part. Let's
733 // inform our user that we can't do anything with this message.
734 //
735 if (![aMutableData length])
736 {
737 [aMutableData appendData: [@"\t[NON-Text Body part not included]" dataUsingEncoding: NSUTF8StringEncoding]];
738 needsToQuote = NO;
739 }
740 else
741 {
742 // We remove the signature
743 // RFC 3676 specifies sig-dashes as followed by space and new line
744 NSRange aRange;
745
746 aRange = [aMutableData rangeOfCString: "\n-- \n" options: NSBackwardsSearch];
747
748 if (aRange.location != NSNotFound)
749 {
750 [aMutableData replaceBytesInRange: NSMakeRange(aRange.location, [aMutableData length]-aRange.location)
751 withBytes: NULL
752 length: 0];
753 }
754 }
755
756
757 // We now have our content as string, let's 'quote' it
758 if (needsToQuote)
759 {
760 NSData *aData;
761
762 aData = [aMutableData unwrapWithLimit: 78];
763 [aMutableData setData: [aData quoteWithLevel: 1 wrappingLimit: 80]];
764 }
765
766 [aMutableData insertCString: [[NSString stringWithFormat: @"%@ wrote:\n\n", [[self from] stringValue]] UTF8String]
767 atIndex: 0];
768
769
770 // We verify if we have a Date value. We might receive messages w/o this field
771 // (Yes, I know, it's borken but it's happening).
772 if ([self receivedDate])
773 {
774 [aMutableData insertCString: [[NSString stringWithFormat: @"On %@ ", [[self receivedDate] description]] UTF8String]
775 atIndex: 0];
776 }
777
778 [theMessage setContent: aMutableData];
779 RELEASE(aMutableData);
780
781 return AUTORELEASE(theMessage);
782 }
783
784
785 //
786 //
787 //
788 - (CWMessage *) forward: (PantomimeForwardMode) theMode;
789 {
790 CWMessage *theMessage;
791
792 theMessage = [[CWMessage alloc] init];
793
794 // We set the subject of our message
795 if ([self subject])
796 {
797 [theMessage setSubject: [NSString stringWithFormat: @"%@ (fwd)", [self subject]]];
798 }
799 else
800 {
801 [theMessage setSubject: @"Forwarded mail..."];
802 }
803
804 if (theMode == PantomimeAttachmentForwardMode)
805 {
806 CWMIMEMultipart *aMimeMultipart;
807 CWPart *aPart;
808
809 aMimeMultipart = [[CWMIMEMultipart alloc] init];
810
811 aPart = [[CWPart alloc] init];
812 [aMimeMultipart addPart: aPart];
813 RELEASE(aPart);
814
815 aPart = [[CWPart alloc] init];
816 [aPart setContentType: @"message/rfc822"];
817 [aPart setContentDisposition: PantomimeAttachmentDisposition];
818 [aPart setSize: [self size]];
819 [aPart setContent: self];
820 [aMimeMultipart addPart: aPart];
821 RELEASE(aPart);
822
823 [theMessage setContentType: @"multipart/mixed"];
824 [theMessage setContent: aMimeMultipart];
825 RELEASE(aMimeMultipart);
826 }
827 else
828 {
829 NSMutableData *aMutableData;
830
831 // We create our generic forward message header
832 aMutableData = AUTORELEASE([[NSMutableData alloc] init]);
833 [aMutableData appendCString: "---------- Forwarded message ----------"];
834
835 // We verify if we have a Date value. We might receive messages w/o this field
836 // (Yes, I know, it's borken but it's happening).
837 if ([self receivedDate])
838 {
839 [aMutableData appendCString: "\nDate: "];
840 [aMutableData appendData: [[[self receivedDate] description] dataUsingEncoding: NSASCIIStringEncoding]];
841 }
842
843 [aMutableData appendCString: "\nFrom: "];
844 [aMutableData appendData: [[[self from] stringValue] dataUsingEncoding: [NSString encodingForPart: self]]];
845
846 if ([self subject])
847 {
848 [aMutableData appendCString: "\nSubject: "];
849 }
850
851 [aMutableData appendData: [[NSString stringWithFormat: @"%@\n\n", [self subject]]
852 dataUsingEncoding: [NSString encodingForPart: self]]];
853
854 //
855 // If our Content-Type is text/plain, we represent it as a it is, otherwise,
856 // we currently create a new body part representing the forwarded message.
857 // We assume that no type means raw text
858 //
859 if ([self isMIMEType: @"text" subType: @"*"] || [self contentType] == nil)
860 {
861 // We set the content of our message
862 [aMutableData appendData: [CWMIMEUtility plainTextContentFromPart: self]];
863
864 // We set the Content-Transfer-Encoding and the Charset to the previous one
865 [theMessage setContentTransferEncoding: [self contentTransferEncoding]];
866 [theMessage setCharset: [self charset]];
867
868
869 [theMessage setContentType: @"text/plain"];
870 [theMessage setContent: aMutableData];
871 [theMessage setSize: [aMutableData length]];
872 }
873 //
874 // If our Content-Type is a message/rfc822 or any other type like
875 // application/*, audio/*, image/* or video/*
876 //
877 else if ([self isMIMEType: @"application" subType: @"*"] ||
878 [self isMIMEType: @"audio" subType: @"*"] ||
879 [self isMIMEType: @"image" subType: @"*"] ||
880 [self isMIMEType: @"message" subType: @"*"] ||
881 [self isMIMEType: @"video" subType: @"*"])
882 {
883 CWMIMEMultipart *aMimeMultipart;
884 CWPart *aPart;
885
886 aMimeMultipart = [[CWMIMEMultipart alloc] init];
887
888 // We add our text/plain part.
889 aPart = [[CWPart alloc] init];
890 [aPart setContentType: @"text/plain"];
891 [aPart setContent: aMutableData];
892 [aPart setContentDisposition: PantomimeInlineDisposition];
893 [aPart setSize: [aMutableData length]];
894 [aMimeMultipart addPart: aPart];
895 RELEASE(aPart);
896
897 // We add our content as an attachment
898 aPart = [[CWPart alloc] init];
899 [aPart setContentType: [self contentType]];
900 [aPart setContent: [self content]];
901 [aPart setContentTransferEncoding: [self contentTransferEncoding]];
902 [aPart setContentDisposition: PantomimeAttachmentDisposition];
903 [aPart setCharset: [self charset]];
904 [aPart setFilename: [self filename]];
905 [aPart setSize: [self size]];
906 [aMimeMultipart addPart: aPart];
907 RELEASE(aPart);
908
909 [theMessage setContentType: @"multipart/mixed"];
910 [theMessage setContent: aMimeMultipart];
911 RELEASE(aMimeMultipart);
912 }
913 //
914 // We have a multipart object. We must treat multipart/alternative
915 // parts differently since we don't want to include multipart parts in the forward.
916 //
917 else if ([self isMIMEType: @"multipart" subType: @"*"])
918 {
919 //
920 // If we have multipart/alternative part, we only keep one part from it.
921 //
922 if ([self isMIMEType: @"multipart" subType: @"alternative"])
923 {
924 CWMIMEMultipart *aMimeMultipart;
925 CWPart *aPart;
926 NSUInteger i;
927
928 aMimeMultipart = (CWMIMEMultipart *)[self content];
929 aPart = nil;
930
931 // We search for our text/plain part
932 for (i = 0; i < [aMimeMultipart count]; i++)
933 {
934 aPart = [aMimeMultipart partAtIndex: i];
935
936 if ([aPart isMIMEType: @"text" subType: @"plain"])
937 {
938 break;
939 }
940 else
941 {
942 aPart = nil;
943 }
944 }
945
946 // We found one
947 if (aPart)
948 {
949 // We set the content of our message
950 [aMutableData appendData: (NSData *)[aPart content]];
951
952 // We set the Content-Transfer-Encoding and the Charset to our text part
953 [theMessage setContentTransferEncoding: [aPart contentTransferEncoding]];
954 [theMessage setCharset: [aPart charset]];
955
956 [theMessage setContentType: @"text/plain"];
957 [theMessage setContent: aMutableData];
958 [theMessage setSize: [aMutableData length]];
959 }
960 // We haven't found one! Inform the user that it happened.
961 else
962 {
963 [aMutableData appendCString: "No text/plain part from this multipart/alternative part has been found"];
964 [aMutableData appendCString: "\nNo parts have been included as attachments with this mail during the forward operation."];
965 [aMutableData appendCString: "\n\nPlease report this as a bug."];
966
967 [theMessage setContent: aMutableData];
968 [theMessage setSize: [aMutableData length]];
969 }
970 }
971 //
972 // We surely have a multipart/mixed or multipart/related.
973 // We search for a text/plain part inside our multipart object.
974 // We 'keep' the other parts in a separate new multipart object too
975 // that will become our new content.
976 //
977 else
978 {
979 CWMIMEMultipart *aMimeMultipart, *newMimeMultipart;
980 CWPart *aPart;
981 BOOL hasFoundTextPlain = NO;
982 NSUInteger i;
983
984 // We get our current mutipart object
985 aMimeMultipart = (CWMIMEMultipart *)[self content];
986
987 // We create our new multipart object for holding all our parts.
988 newMimeMultipart = [[CWMIMEMultipart alloc] init];
989
990 for (i = 0; i < [aMimeMultipart count]; i++)
991 {
992 aPart = [aMimeMultipart partAtIndex: i];
993
994 if ([aPart isMIMEType: @"text" subType: @"plain"] && !hasFoundTextPlain)
995 {
996 CWPart *newPart;
997
998 newPart = [[CWPart alloc] init];
999
1000 // We set the content of our new part
1001 [aMutableData appendData: (NSData *)[aPart content]];
1002 [newPart setContentType: @"text/plain"];
1003 [newPart setContent: aMutableData];
1004 [newPart setSize: [aMutableData length]];
1005
1006 // We set the Content-Transfer-Encoding and the Charset to the previous one
1007 [newPart setContentTransferEncoding: [aPart contentTransferEncoding]];
1008 [newPart setCharset: [aPart charset]];
1009
1010 // We finally add our new part to our MIME multipart object
1011 [newMimeMultipart addPart: newPart];
1012 RELEASE(newPart);
1013
1014 hasFoundTextPlain = YES;
1015 }
1016 // We set the Content-Disposition to "attachment"
1017 // all the time.
1018 else
1019 {
1020 [aPart setContentDisposition: PantomimeAttachmentDisposition];
1021 [newMimeMultipart addPart: aPart];
1022 }
1023 }
1024
1025 [theMessage setContentType: @"multipart/mixed"];
1026 [theMessage setContent: newMimeMultipart];
1027 RELEASE(newMimeMultipart);
1028 }
1029 }
1030 //
1031 // We got an unknown part. Let's inform the user about this situation.
1032 //
1033 else
1034 {
1035 // We set the content of our message
1036 [aMutableData appendCString: "The original message contained an unknown part that was not included in this forwarded message."];
1037 [aMutableData appendCString: "\n\nPlease report this as a bug."];
1038
1039 [theMessage setContentType: @"text/plain"];
1040 [theMessage setContent: aMutableData];
1041 [theMessage setSize: [aMutableData length]];
1042 }
1043 }
1044
1045 return AUTORELEASE(theMessage);
1046 }
1047
1048
1049 //
1050 //
1051 //
1052 - (NSData *) dataValue
1053 {
1054 NSMutableData *aMutableData;
1055 NSDictionary *aLocale;
1056
1057 NSEnumerator *allHeaderKeyEnumerator;
1058 NSString *aKey;
1059
1060 NSCalendarDate *aCalendarDate;
1061 NSData *aData;
1062
1063
1064 // We get our locale in English
1065 #ifndef MACOSX
1066 aLocale = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle bundleForLibrary: @"gnustep-base"]
1067 pathForResource: @"English"
1068 ofType: nil
1069 inDirectory: @"Languages"]];
1070 #else
1071 aLocale = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle bundleForClass: [NSObject class]]
1072 pathForResource: @"English"
1073 ofType: nil
1074 inDirectory: @"Languages"] ];
1075 #endif
1076
1077 // We initialize our mutable data object holding the raw data of the
1078 // new message.
1079 aMutableData = [[NSMutableData alloc] init];
1080
1081 #ifndef MACOSX
1082 if ([[NSUserDefaults standardUserDefaults] objectForKey: @"Local Time Zone"])
1083 {
1084 aCalendarDate = [[NSDate date] dateWithCalendarFormat: @"%a, %d %b %Y %H:%M:%S %z"
1085 timeZone: [NSTimeZone systemTimeZone]];
1086 }
1087 else
1088 {
1089 tzset();
1090 aCalendarDate = [[NSDate date] dateWithCalendarFormat: @"%a, %d %b %Y %H:%M:%S %z"
1091 timeZone: [NSTimeZone timeZoneWithAbbreviation:
1092 [NSString stringWithCString: tzname[1]]]];
1093 }
1094 #else
1095 aCalendarDate = [[NSDate date] dateWithCalendarFormat: @"%a, %d %b %Y %H:%M:%S %z"
1096 timeZone: [NSTimeZone systemTimeZone]];
1097 #endif
1098 [aMutableData appendCFormat: @"Date: %@%s", [aCalendarDate descriptionWithLocale: aLocale], LF];
1099
1100 // We set the subject, if we have one!
1101 if ([[[self subject] stringByTrimmingWhiteSpaces] length] > 0)
1102 {
1103 [aMutableData appendCString: "Subject: "];
1104 [aMutableData appendData: [CWMIMEUtility encodeWordUsingQuotedPrintable: [self subject]
1105 prefixLength: 8]];
1106 [aMutableData appendCString: LF];
1107 }
1108
1109 // We set our Message-ID
1110 [aMutableData appendCFormat: @"Message-ID: <%@>%s", [self messageID], LF];
1111
1112 // We set our MIME-Version header
1113 [aMutableData appendCFormat: @"MIME-Version: 1.0 (Generated by Pantomime %@)%s", PANTOMIME_VERSION, LF];
1114
1115 // We encode our From: field
1116 [aMutableData appendCFormat: @"From: "];
1117 [aMutableData appendData: [[self from] dataValue]];
1118 [aMutableData appendCFormat: @"%s", LF];
1119
1120 // We encode our To field
1121 aData = [self _formatRecipientsWithType: PantomimeToRecipient];
1122
1123 if (aData)
1124 {
1125 [aMutableData appendCString: "To: "];
1126 [aMutableData appendData: aData];
1127 [aMutableData appendCString: LF];
1128 }
1129
1130 // We encode our Cc field
1131 aData = [self _formatRecipientsWithType: PantomimeCcRecipient];
1132
1133 if (aData)
1134 {
1135 [aMutableData appendCString: "Cc: "];
1136 [aMutableData appendData: aData];
1137 [aMutableData appendCString: LF];
1138 }
1139
1140 // We encode our Bcc field
1141 aData = [self _formatRecipientsWithType: PantomimeBccRecipient];
1142
1143 if (aData)
1144 {
1145 [aMutableData appendCString: "Bcc: "];
1146 [aMutableData appendData: aData];
1147 [aMutableData appendCString: LF];
1148 }
1149
1150 // We set the Reply-To address in case we need to
1151 if ([self replyTo])
1152 {
1153 NSUInteger i, count;
1154
1155 [aMutableData appendCFormat: @"Reply-To: "];
1156
1157 count = [[self replyTo] count];
1158 for (i = 0; i < count; i++)
1159 {
1160 [aMutableData appendData: [[[self replyTo] objectAtIndex: i] dataValue]];
1161
1162 if (i < count-1) [aMutableData appendCString: ", "];
1163 }
1164
1165 [aMutableData appendCString: LF];
1166 }
1167
1168 // We set the Organization header value if we need to
1169 if ([self organization])
1170 {
1171 [aMutableData appendCString: "Organization: "];
1172 [aMutableData appendData: [CWMIMEUtility encodeWordUsingQuotedPrintable: [self organization]
1173 prefixLength: 13]];
1174 [aMutableData appendCString: LF];
1175 }
1176
1177 // We set the In-Reply-To header if we need to
1178 if ([self headerValueForName: @"In-Reply-To"])
1179 {
1180 [aMutableData appendCFormat: @"In-Reply-To: %@%s", [self inReplyTo], LF];
1181 }
1182
1183
1184 // We now set all X-* headers
1185 allHeaderKeyEnumerator = [_headers keyEnumerator];
1186
1187 while ((aKey = [allHeaderKeyEnumerator nextObject]))
1188 {
1189 if ([aKey hasPrefix: @"X-"] || ([aKey caseInsensitiveCompare: @"User-Agent"] == NSOrderedSame))
1190 {
1191 [aMutableData appendCFormat: @"%@: %@%s", aKey, [self headerValueForName: aKey], LF];
1192 }
1193 }
1194
1195 //
1196 // We add our message header/body separator
1197 //
1198 [aMutableData appendData: [super dataValue]];
1199
1200 return AUTORELEASE(aMutableData);
1201 }
1202
1203 //
1204 //
1205 //
1206 - (void) addHeader: (NSString *) theName
1207 withValue: (NSString *) theValue
1208 {
1209 if (theName && theValue)
1210 {
1211 NSString *aString;
1212
1213 if ((aString = [_headers objectForKey: theName]))
1214 {
1215 aString = [NSString stringWithFormat: @"%@ %@", aString, theValue];
1216 }
1217 else
1218 {
1219 aString = theValue;
1220 }
1221
1222 [_headers setObject: aString forKey: theName];
1223 }
1224 }
1225
1226 //
1227 //
1228 //
1229 - (CWFolder *) folder
1230 {
1231 return _folder;
1232 }
1233
1234 - (void) setFolder: (CWFolder *) theFolder
1235 {
1236 _folder = theFolder;
1237 }
1238
1239 //
1240 // This method is used to optain tha raw source of a message.
1241 // It's returned as a NSData object. The returned data should
1242 // be the message like it's achived in a mbox format and it could
1243 // easily be decoded with Message: -initWithData.
1244 //
1245 // All subclasses of Message MUST implement this method.
1246 //
1247 - (NSData *) rawSource
1248 {
1249 if (!_rawSource)
1250 {
1251 [self subclassResponsibility: _cmd];
1252 return nil;
1253 }
1254
1255 return _rawSource;
1256 }
1257
1258
1259 //
1260 //
1261 //
1262 - (void) setRawSource: (NSData *) theRawSource
1263 {
1264 ASSIGN(_rawSource, theRawSource);
1265 }
1266
1267
1268 //
1269 //
1270 //
1271 - (NSCalendarDate *) resentDate
1272 {
1273 return [_headers objectForKey: @"Resent-Date"];
1274 }
1275
1276
1277 //
1278 //
1279 //
1280 - (void) setResentDate: (NSCalendarDate *) theResentDate
1281 {
1282 [_headers setObject: theResentDate forKey: @"Resent-Date"];
1283 }
1284
1285
1286 //
1287 //
1288 //
1289 - (CWInternetAddress *) resentFrom
1290 {
1291 return [_headers objectForKey: @"Resent-From"];
1292 }
1293
1294
1295 //
1296 //
1297 //
1298 - (void) setResentFrom: (CWInternetAddress *) theInternetAddress
1299 {
1300 [_headers setObject: theInternetAddress forKey: @"Resent-From"];
1301 }
1302
1303
1304 //
1305 //
1306 //
1307 - (NSString *) resentMessageID
1308 {
1309 return [_headers objectForKey: @"Resent-Message-ID"];
1310 }
1311
1312
1313 //
1314 //
1315 //
1316 - (void) setResentMessageID: (NSString *) theResentMessageID
1317 {
1318 [_headers setObject: theResentMessageID forKey: @"Resent-Message-ID"];
1319 }
1320
1321
1322 //
1323 //
1324 //
1325 - (NSString *) resentSubject
1326 {
1327 return [_headers objectForKey: @"Resent-Subject"];
1328 }
1329
1330
1331 //
1332 //
1333 //
1334 - (void) setResentSubject: (NSString *) theResentSubject
1335 {
1336 [_headers setObject: theResentSubject forKey: @"Resent-Subject"];
1337 }
1338
1339
1340 //
1341 //
1342 //
1343 - (void) addHeadersFromData: (NSData *) theHeaders record: (cache_record *) theRecord
1344 {
1345 NSArray *allLines;
1346 NSData *aData;
1347 NSUInteger i, count;
1348
1349 [super setHeadersFromData: theHeaders];
1350
1351 // We MUST be sure to unfold all headers properly before
1352 // decoding the headers
1353 theHeaders = [theHeaders unfoldLines];
1354
1355 allLines = [theHeaders componentsSeparatedByCString: "\n"];
1356 count = [allLines count];
1357
1358 for (i = 0; i < count; i++)
1359 {
1360 NSData *aLine = [allLines objectAtIndex: i];
1361
1362 // We stop if we found the header separator. (\n\n) since someone could
1363 // have called this method with the entire rawsource of a message.
1364 if ([aLine length] == 0)
1365 {
1366 break;
1367 }
1368
1369 if ([aLine hasCaseInsensitiveCPrefix: "Bcc"])
1370 {
1371 [CWParser parseDestination: aLine
1372 forType: PantomimeBccRecipient
1373 inMessage: self
1374 quick: NO];
1375 }
1376 else if ([aLine hasCaseInsensitiveCPrefix: "Cc"])
1377 {
1378 aData = [CWParser parseDestination: aLine
1379 forType: PantomimeCcRecipient
1380 inMessage: self
1381 quick: NO];
1382 if (theRecord) theRecord->cc = aData;
1383 }
1384 else if ([aLine hasCaseInsensitiveCPrefix: "Date"])
1385 {
1386 [CWParser parseDate: aLine inMessage: self];
1387 if (theRecord && [self receivedDate]) theRecord->date = [[self receivedDate] timeIntervalSince1970];
1388 }
1389 else if ([aLine hasCaseInsensitiveCPrefix: "From"] &&
1390 ![aLine hasCaseInsensitiveCPrefix: "From "])
1391 {
1392 aData = [CWParser parseFrom: aLine inMessage: self quick: NO];
1393 if (theRecord) theRecord->from = aData;
1394 }
1395 else if ([aLine hasCaseInsensitiveCPrefix: "In-Reply-To"])
1396 {
1397 aData = [CWParser parseInReplyTo: aLine inMessage: self quick: NO];
1398 if (theRecord) theRecord->in_reply_to = aData;
1399 }
1400 else if ([aLine hasCaseInsensitiveCPrefix: "Message-ID"])
1401 {
1402 aData = [CWParser parseMessageID: aLine inMessage: self quick: NO];
1403 if (theRecord) theRecord->message_id = aData;
1404 }
1405 else if ([aLine hasCaseInsensitiveCPrefix: "MIME-Version"])
1406 {
1407 [CWParser parseMIMEVersion: aLine inMessage: self];
1408 }
1409 else if ([aLine hasCaseInsensitiveCPrefix: "Organization"])
1410 {
1411 [CWParser parseOrganization: aLine inMessage: self];
1412 }
1413 else if ([aLine hasCaseInsensitiveCPrefix: "References"])
1414 {
1415 aData = [CWParser parseReferences: aLine inMessage: self quick: NO];
1416 if (theRecord) theRecord->references = aData;
1417 }
1418 else if ([aLine hasCaseInsensitiveCPrefix: "Reply-To"])
1419 {
1420 [CWParser parseReplyTo: aLine inMessage: self];
1421 }
1422 else if ([aLine hasCaseInsensitiveCPrefix: "Resent-From"])
1423 {
1424 [CWParser parseResentFrom: aLine inMessage: self];
1425 }
1426 else if ([aLine hasCaseInsensitiveCPrefix: "Resent-Bcc"])
1427 {
1428 [CWParser parseDestination: aLine
1429 forType: PantomimeResentBccRecipient
1430 inMessage: self
1431 quick: NO];
1432 }
1433 else if ([aLine hasCaseInsensitiveCPrefix: "Resent-Cc"])
1434 {
1435 [CWParser parseDestination: aLine
1436 forType: PantomimeResentCcRecipient
1437 inMessage: self
1438 quick: NO];
1439 }
1440 else if ([aLine hasCaseInsensitiveCPrefix: "Resent-To"])
1441 {
1442 [CWParser parseDestination: aLine
1443 forType: PantomimeResentToRecipient
1444 inMessage: self
1445 quick: NO];
1446 }
1447 else if ([aLine hasCaseInsensitiveCPrefix: "Status"])
1448 {
1449 [CWParser parseStatus: aLine inMessage: self];
1450 }
1451 else if ([aLine hasCaseInsensitiveCPrefix: "To"])
1452 {
1453 aData = [CWParser parseDestination: aLine
1454 forType: PantomimeToRecipient
1455 inMessage: self
1456 quick: NO];
1457 if (theRecord) theRecord->to = aData;
1458 }
1459 else if ([aLine hasCaseInsensitiveCPrefix: "X-Status"])
1460 {
1461 [CWParser parseXStatus: aLine inMessage: self];
1462 }
1463 else if ([aLine hasCaseInsensitiveCPrefix: "Subject"])
1464 {
1465 aData = [CWParser parseSubject: aLine inMessage: self quick: NO];
1466 if (theRecord) theRecord->subject = aData;
1467 }
1468 else
1469 {
1470 // We MUST NOT parse the headers that we already parsed in
1471 // Part as "unknown".
1472 if (![aLine hasCaseInsensitiveCPrefix: "Content-Description"] &&
1473 ![aLine hasCaseInsensitiveCPrefix: "Content-Disposition"] &&
1474 ![aLine hasCaseInsensitiveCPrefix: "Content-ID"] &&
1475 ![aLine hasCaseInsensitiveCPrefix: "Content-Length"] &&
1476 ![aLine hasCaseInsensitiveCPrefix: "Content-Transfer-Encoding"] &&
1477 ![aLine hasCaseInsensitiveCPrefix: "Content-Type"])
1478 {
1479 [CWParser parseUnknownHeader: aLine inMessage: self];
1480 }
1481 }
1482 }
1483 }
1484
1485 //
1486 //
1487 //
1488 - (void) setHeadersFromData: (NSData *) theHeaders
1489 {
1490 [self setHeadersFromData: theHeaders record: NULL];
1491 }
1492
1493 //
1494 //
1495 //
1496 - (void) setHeadersFromData: (NSData *) theHeaders record: (cache_record *) theRecord
1497 {
1498 if (!theHeaders || [theHeaders length] == 0)
1499 {
1500 return;
1501 }
1502
1503 [_recipients removeAllObjects];
1504 [_headers removeAllObjects];
1505 [self addHeadersFromData: theHeaders record: theRecord];
1506 }
1507
1508 @end
1509
1510
1511 //
1512 // Message's sorting category
1513 //
1514 @implementation CWMessage (Comparing)
1515
1516 - (NSComparisonResult) compareAccordingToNumber: (CWMessage *) aMessage
1517 {
1518 unsigned int num;
1519 num = [aMessage messageNumber];
1520 if (_message_number < num)
1521 {
1522 return NSOrderedAscending;
1523 }
1524 else if (_message_number > num)
1525 {
1526 return NSOrderedDescending;
1527 }
1528 else
1529 {
1530 return NSOrderedSame;
1531 }
1532 }
1533
1534 - (NSComparisonResult) reverseCompareAccordingToNumber: (CWMessage *) aMessage
1535 {
1536 unsigned int num;
1537 num = [aMessage messageNumber];
1538 if (num < _message_number)
1539 {
1540 return NSOrderedAscending;
1541 }
1542 else if (num > _message_number)
1543 {
1544 return NSOrderedDescending;
1545 }
1546 else
1547 {
1548 return NSOrderedSame;
1549 }
1550 }
1551
1552 - (NSComparisonResult) compareAccordingToDate: (CWMessage *) aMessage
1553 {
1554 NSDate *date1 = [self receivedDate];
1555 NSDate *date2 = [aMessage receivedDate];
1556 NSTimeInterval timeInterval;
1557
1558 if (date1 == nil || date2 == nil)
1559 {
1560 return [self compareAccordingToNumber: aMessage];
1561 }
1562
1563 timeInterval = [date1 timeIntervalSinceDate: date2];
1564
1565 if (timeInterval < 0)
1566 {
1567 return NSOrderedAscending;
1568 }
1569 else if (timeInterval > 0)
1570 {
1571 return NSOrderedDescending;
1572 }
1573 else
1574 {
1575 return [self compareAccordingToNumber: aMessage];
1576 }
1577 }
1578
1579 - (NSComparisonResult) reverseCompareAccordingToDate: (CWMessage *) aMessage
1580 {
1581 NSDate *date2 = [self receivedDate];
1582 NSDate *date1 = [aMessage receivedDate];
1583 NSTimeInterval timeInterval;
1584
1585 if (date1 == nil || date2 == nil)
1586 {
1587 return [self reverseCompareAccordingToNumber: aMessage];
1588 }
1589
1590 timeInterval = [date1 timeIntervalSinceDate: date2];
1591
1592 if (timeInterval < 0)
1593 {
1594 return NSOrderedAscending;
1595 }
1596 else if (timeInterval > 0)
1597 {
1598 return NSOrderedDescending;
1599 }
1600 else
1601 {
1602 return [self reverseCompareAccordingToNumber: aMessage];
1603 }
1604 }
1605
1606 - (NSComparisonResult) compareAccordingToSender: (CWMessage *) aMessage
1607 {
1608 CWInternetAddress *from1, *from2;
1609 NSString *fromString1, *fromString2;
1610 NSString *tempString;
1611 NSComparisonResult result;
1612
1613 from1 = [self from];
1614 from2 = [aMessage from];
1615
1616 tempString = [from1 personal];
1617 if (tempString == nil || [tempString length] == 0)
1618 {
1619 fromString1 = [from1 address];
1620 if (fromString1 == nil)
1621 fromString1 = @"";
1622 }
1623 else
1624 {
1625 fromString1 = tempString;
1626 }
1627
1628
1629 tempString = [from2 personal];
1630 if (tempString == nil || [tempString length] == 0)
1631 {
1632 fromString2 = [from2 address];
1633 if (fromString2 == nil)
1634 fromString2 = @"";
1635 }
1636 else
1637 {
1638 fromString2 = tempString;
1639 }
1640
1641 result = [fromString1 caseInsensitiveCompare: fromString2];
1642 if (result == NSOrderedSame)
1643 {
1644 return [self compareAccordingToNumber: aMessage];
1645 }
1646 else
1647 {
1648 return result;
1649 }
1650 }
1651
1652 - (NSComparisonResult) reverseCompareAccordingToSender: (CWMessage *) aMessage
1653 {
1654 CWInternetAddress *from1, *from2;
1655 NSString *fromString1, *fromString2;
1656 NSString *tempString;
1657 NSComparisonResult result;
1658
1659 from2 = [self from];
1660 from1 = [aMessage from];
1661
1662 tempString = [from1 personal];
1663 if (tempString == nil || [tempString length] == 0)
1664 {
1665 fromString1 = [from1 address];
1666 if (fromString1 == nil)
1667 fromString1 = @"";
1668 }
1669 else
1670 {
1671 fromString1 = tempString;
1672 }
1673
1674
1675 tempString = [from2 personal];
1676 if (tempString == nil || [tempString length] == 0)
1677 {
1678 fromString2 = [from2 address];
1679 if (fromString2 == nil)
1680 fromString2 = @"";
1681 }
1682 else
1683 {
1684 fromString2 = tempString;
1685 }
1686
1687
1688 result = [fromString1 caseInsensitiveCompare: fromString2];
1689
1690 if (result == NSOrderedSame)
1691 {
1692 return [self reverseCompareAccordingToNumber: aMessage];
1693 }
1694 else
1695 {
1696 return result;
1697 }
1698 }
1699
1700 - (NSComparisonResult) compareAccordingToSubject: (CWMessage *) aMessage
1701 {
1702 NSString *subject1 = [self baseSubject];
1703 NSString *subject2 = [aMessage baseSubject];
1704 NSComparisonResult result;
1705
1706 if (subject1 == nil)
1707 subject1 = @"";
1708 if (subject2 == nil)
1709 subject2 = @"";
1710
1711 result = [subject1 caseInsensitiveCompare: subject2];
1712
1713 if (result == NSOrderedSame)
1714 {
1715 return [self compareAccordingToNumber: aMessage];
1716 }
1717 else
1718 {
1719 return result;
1720 }
1721 }
1722
1723 - (NSComparisonResult) reverseCompareAccordingToSubject: (CWMessage *) aMessage
1724 {
1725 NSString *subject2 = [self baseSubject];
1726 NSString *subject1 = [aMessage baseSubject];
1727 NSComparisonResult result;
1728
1729 if (subject1 == nil)
1730 subject1 = @"";
1731 if (subject2 == nil)
1732 subject2 = @"";
1733
1734 result = [subject1 caseInsensitiveCompare: subject2];
1735
1736 if (result == NSOrderedSame)
1737 {
1738 return [self compareAccordingToNumber: aMessage];
1739 }
1740 else
1741 {
1742 return result;
1743 }
1744 }
1745
1746 - (NSComparisonResult) compareAccordingToSize: (CWMessage *) aMessage
1747 {
1748 unsigned long size1 = [self size];
1749 unsigned long size2 = [aMessage size];
1750
1751 if (size1 < size2)
1752 {
1753 return NSOrderedAscending;
1754 }
1755 else if (size1 > size2)
1756 {
1757 return NSOrderedDescending;
1758 }
1759 else
1760 {
1761 return [self compareAccordingToNumber: aMessage];
1762 }
1763 }
1764
1765 - (NSComparisonResult) reverseCompareAccordingToSize: (CWMessage *) aMessage
1766 {
1767 unsigned long size1 = [aMessage size];
1768 unsigned long size2 = [self size];
1769
1770 if (size1 < size2)
1771 {
1772 return NSOrderedAscending;
1773 }
1774 else if (size1 > size2)
1775 {
1776 return NSOrderedDescending;
1777 }
1778 else
1779 {
1780 return [self reverseCompareAccordingToNumber: aMessage];
1781 }
1782 }
1783
1784 @end
1785
1786
1787 //
1788 // Private methods
1789 //
1790 @implementation CWMessage (Private)
1791
1792 //
1793 // Intended to be compatible for use with the draft specification for
1794 // sorting by subject contained in
1795 // INTERNET MESSAGE ACCESS PROTOCOL - SORT and THREAD EXTENSIONS
1796 // draft document, May 2003
1797 //
1798 // At the time of this writing, it can be found at
1799 // http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-13.txt
1800 //
1801 - (NSString *) _computeBaseSubject
1802 {
1803 NSMutableString *baseSubject;
1804 NSArray *theMatches;
1805 NSString *aSubject;
1806 NSRange aRange;
1807
1808 BOOL b1, b2;
1809 NSUInteger i;
1810
1811 aSubject = [self subject];
1812
1813 if (!aSubject)
1814 {
1815 return nil;
1816 }
1817
1818 baseSubject = [NSMutableString stringWithString: aSubject];
1819
1820 //
1821 // (1) Convert any RFC 2047 encoded-words in the subject to
1822 // UTF-8 as described in "internationalization
1823 // considerations." Convert all tabs and continuations to
1824 // space. Convert all multiple spaces to a single space.
1825 //
1826 theMatches = [atLeastOneSpaceRegex matchString: baseSubject];
1827
1828 for (i = [theMatches count]; i > 0; i--)
1829 {
1830 aRange = [[theMatches objectAtIndex:i-1] rangeValue];
1831
1832 if (CHECK_RANGE(aRange, [baseSubject length]))
1833 {
1834 [baseSubject replaceCharactersInRange: aRange withString:@" "];
1835 }
1836 }
1837 do
1838 {
1839 b1 = NO;
1840
1841 //
1842 // (2) Remove all trailing text of the subject that matches
1843 // the subj-trailer ABNF, repeat until no more matches are
1844 // possible.
1845 //
1846 theMatches = [suffixSubjTrailerRegex matchString: baseSubject];
1847
1848 if ([theMatches count] > 0)
1849 {
1850 aRange = [[theMatches objectAtIndex:0] rangeValue];
1851
1852 if (CHECK_RANGE(aRange,[baseSubject length]))
1853 {
1854 [baseSubject deleteCharactersInRange: [[theMatches objectAtIndex:0] rangeValue]];
1855 }
1856 }
1857 do
1858 {
1859 b2 = NO;
1860 //
1861 // (3) Remove all prefix text of the subject that matches the
1862 // subj-leader ABNF.
1863 //
1864 theMatches = [prefixSubjLeaderRegex matchString: baseSubject];
1865
1866 if ([theMatches count] > 0)
1867 {
1868 aRange = [[theMatches objectAtIndex:0] rangeValue];
1869
1870 if (CHECK_RANGE(aRange, [baseSubject length]))
1871 {
1872 [baseSubject deleteCharactersInRange: [[theMatches objectAtIndex:0] rangeValue]];
1873 b2 = YES;
1874 }
1875 }
1876 //
1877 // (4) If there is prefix text of the subject that matches the
1878 // subj-blob ABNF, and removing that prefix leaves a non-empty
1879 // subj-base, then remove the prefix text.
1880 //
1881 theMatches = [prefixSubjBlobRegex matchString: baseSubject];
1882
1883 if ([theMatches count] > 0)
1884 {
1885 aRange = [[theMatches objectAtIndex:0] rangeValue];
1886 if (CHECK_RANGE(aRange, [baseSubject length]))
1887 {
1888 [baseSubject deleteCharactersInRange:[[theMatches objectAtIndex:0] rangeValue]];
1889 b2 = YES;
1890 }
1891 }
1892 //
1893 // (5) Repeat (3) and (4) until no matches remain.
1894 //
1895 } while (b2);
1896 //
1897 // (6) If the resulting text begins with the subj-fwd-hdr ABNF
1898 // and ends with the subj-fwd-trl ABNF, remove the
1899 // subj-fwd-hdr and subj-fwd-trl and repeat from step (2).
1900 //
1901 theMatches = [prefixSubjFwdHdrAndSuffixSubjFwdTrlRegex matchString:baseSubject];
1902
1903 if ([theMatches count] > 0)
1904 {
1905 [baseSubject deleteCharactersInRange:NSMakeRange(0,5)];
1906 [baseSubject deleteCharactersInRange:NSMakeRange([baseSubject length] - 1,1)];
1907 b1 = YES;
1908 }
1909 } while (b1);
1910 //
1911 // (7) The resulting text is the "base subject" used in the SORT.
1912 //
1913 return baseSubject;
1914 }
1915
1916
1917 //
1918 //
1919 //
1920 - (void) _extractText: (NSMutableData *) theMutableData
1921 part: (id) thePart
1922 quote: (BOOL *) theBOOL
1923 {
1924 //
1925 // We now get the right text part of the message.
1926 // We assume that no type means raw text (assuming also not mutlipart)
1927 //
1928 //
1929 if ([thePart isMIMEType: @"text" subType: @"*"] || [thePart contentType] == nil)
1930 {
1931 NSData *d;
1932
1933 d = [[NSString stringWithData: [CWMIMEUtility plainTextContentFromPart: thePart] charset: [[thePart charset] dataUsingEncoding: NSASCIIStringEncoding]]
1934 dataUsingEncoding: NSUTF8StringEncoding];
1935 [theMutableData appendData: d];
1936 *theBOOL = YES;
1937 }
1938 //
1939 // If our message only contains the following part types, we cannot
1940 // represent those in a reply.
1941 //
1942 else if ([thePart isMIMEType: @"application" subType: @"*"] ||
1943 [thePart isMIMEType: @"audio" subType: @"*"] ||
1944 [thePart isMIMEType: @"image" subType: @"*"] ||
1945 [thePart isMIMEType: @"message" subType: @"*"] ||
1946 [thePart isMIMEType: @"video" subType: @"*"])
1947 {
1948 [theMutableData appendData: [@"\t[NON-Text Body part not included]" dataUsingEncoding: NSUTF8StringEncoding]];
1949 }
1950 //
1951 // We have a multipart type. It can be:
1952 //
1953 // multipart/appledouble, multipart/alternative, multipart/related,
1954 // multipart/mixed or even multipart/report.
1955 //
1956 // We must search for a text part to use in our reply.
1957 //
1958 else if ([thePart isMIMEType: @"multipart" subType: @"*"])
1959 {
1960 CWMIMEMultipart *aMimeMultipart;
1961 CWPart *aPart;
1962 NSUInteger i;
1963
1964 aMimeMultipart = (CWMIMEMultipart *)[thePart content];
1965
1966 for (i = 0; i < [aMimeMultipart count]; i++)
1967 {
1968 aPart = [aMimeMultipart partAtIndex: i];
1969
1970 //
1971 // We do a full verification on the Content-Type since we might
1972 // have a text/x-{something} like text/x-vcard.
1973 //
1974 if ([aPart isMIMEType: @"text" subType: @"plain"] ||
1975 [aPart isMIMEType: @"text" subType: @"enriched"] ||
1976 [aPart isMIMEType: @"text" subType: @"html"])
1977 {
1978 [theMutableData appendData: [[NSString stringWithData: [CWMIMEUtility plainTextContentFromPart: aPart]
1979 charset: [[aPart charset] dataUsingEncoding: NSASCIIStringEncoding]]
1980 dataUsingEncoding: NSUTF8StringEncoding]];
1981
1982 // If our original Content-Type is multipart/alternative, no need to
1983 // consider to the other text/* parts. Otherwise, we just append
1984 // all text/* parts.
1985 if ([thePart isMIMEType: @"multipart" subType: @"alternative"])
1986 {
1987 break;
1988 }
1989 }
1990 //
1991 // If we got any other kind of multipart parts, we loop inside of it in order to
1992 // extract all text parts.
1993 //
1994 else if ([aPart isMIMEType: @"multipart" subType: @"*"])
1995 {
1996 [self _extractText: theMutableData part: aPart quote: theBOOL];
1997 }
1998 } // for ( ... )
1999
2000 *theBOOL = YES;
2001 } // else if ([thePart isMIMEType: @"multipart" subType: @"*"])
2002 }
2003
2004
2005 //
2006 //
2007 //
2008 - (NSData *) _formatRecipientsWithType: (PantomimeRecipientType) theType
2009 {
2010 NSMutableData *aMutableData;
2011 NSUInteger i;
2012
2013 aMutableData = [[NSMutableData alloc] init];
2014
2015 for (i = 0; i < [_recipients count]; i++)
2016 {
2017 CWInternetAddress *anInternetAddress;
2018
2019 anInternetAddress = [_recipients objectAtIndex: i];
2020
2021 if ([anInternetAddress type] == theType)
2022 {
2023 [aMutableData appendData: [anInternetAddress dataValue]];
2024 [aMutableData appendCString: ", "];
2025 }
2026 }
2027
2028 if ([aMutableData length] > 0)
2029 {
2030 [aMutableData setLength: [aMutableData length]-2];
2031 return AUTORELEASE(aMutableData);
2032 }
2033
2034 RELEASE(aMutableData);
2035 return nil;
2036 }
2037
2038 @end
2039
2040