-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathSOGoDraftObject.m
More file actions
2514 lines (2121 loc) · 72.9 KB
/
SOGoDraftObject.m
File metadata and controls
2514 lines (2121 loc) · 72.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2007-2021 Inverse inc.
This file is part of SOGo.
SOGo is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
SOGo is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with OGo; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pkcs7.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#endif
#import <Foundation/NSURL.h>
#import <Foundation/NSValue.h>
#import <NGObjWeb/NSException+HTTP.h>
#import <NGObjWeb/SoObject+SoDAV.h>
#import <NGObjWeb/WOContext+SoObjects.h>
#import <NGObjWeb/WORequest+So.h>
#import <NGExtensions/NGBase64Coding.h>
#import <NGExtensions/NSFileManager+Extensions.h>
#import <NGExtensions/NGHashMap.h>
#import <NGExtensions/NSNull+misc.h>
#import <NGExtensions/NSObject+Logs.h>
#import <NGExtensions/NSString+misc.h>
#import <NGImap4/NGImap4Connection.h>
#import <NGImap4/NGImap4Client.h>
#import <NGImap4/NGImap4Envelope.h>
#import <NGImap4/NGImap4EnvelopeAddress.h>
#import <NGMail/NGMailAddress.h>
#import <NGMail/NGMailAddressParser.h>
#import <NGMail/NGMimeMessage.h>
#import <NGMail/NGMimeMessageGenerator.h>
#import <NGMail/NGMimeMessageParser.h>
#import <NGMime/NGMimeBodyPart.h>
#import <NGMime/NGMimeFileData.h>
#import <NGMime/NGMimeMultipartBody.h>
#import <NGMime/NGMimeType.h>
#import <NGMime/NGMimeHeaderFields.h>
#import <SOGo/NSArray+Utilities.h>
#import <SOGo/NSCalendarDate+SOGo.h>
#import <SOGo/NSDictionary+Utilities.h>
#import <SOGo/NSString+Utilities.h>
#import <SOGo/SOGoBuild.h>
#import <SOGo/SOGoDomainDefaults.h>
#import <SOGo/SOGoMailer.h>
#import <SOGo/SOGoUser.h>
#import <SOGo/SOGoUserFolder.h>
#import <SOGo/SOGoUserDefaults.h>
#import <SOGo/SOGoSystemDefaults.h>
#import <SOGo/NGMimeFileData+SOGo.h>
#import <NGCards/NGVCard.h>
#import <Contacts/SOGoContactFolder.h>
#import <Contacts/SOGoContactFolders.h>
#import <Contacts/SOGoContactGCSEntry.h>
#import "NSData+Mail.h"
#import "NSData+SMIME.h"
#import "NSString+Mail.h"
#import "SOGoDraftsFolder.h"
#import "SOGoMailAccount.h"
#import "SOGoMailObject+Draft.h"
#import "SOGoSentFolder.h"
#import "SOGoDraftObject.h"
static NSString *contentTypeValue = @"text/plain; charset=utf-8";
static NSString *htmlContentTypeValue = @"text/html; charset=utf-8";
static NSString *headerKeys[] = {@"subject", @"to", @"cc", @"bcc",
@"from", @"replyTo", @"message-id",
nil};
#warning -[NGImap4Connection postData:flags:toFolderURL:] should be enhanced \
to return at least the new uid
@interface NGImap4Connection (SOGoHiddenMethods)
- (NSString *) imap4FolderNameForURL: (NSURL *) url;
@end
//
//
//
@implementation SOGoDraftObject
static NGMimeType *MultiMixedType = nil;
static NGMimeType *MultiAlternativeType = nil;
static NGMimeType *MultiRelatedType = nil;
static NSString *userAgent = nil;
+ (void) initialize
{
MultiMixedType = [NGMimeType mimeType: @"multipart" subType: @"mixed"];
[MultiMixedType retain];
MultiAlternativeType = [NGMimeType mimeType: @"multipart" subType: @"alternative"];
[MultiAlternativeType retain];
MultiRelatedType = [NGMimeType mimeType: @"multipart" subType: @"related"];
[MultiRelatedType retain];
userAgent = [NSString stringWithFormat: @"SOGoMail %@",
SOGoVersion];
[userAgent retain];
}
- (id) init
{
if ((self = [super init]))
{
sourceIMAP4ID = -1;
IMAP4ID = -1;
headers = [[NSMutableDictionary alloc] init];
certificates = [[NSMutableDictionary alloc] init];
text = @"";
path = nil;
sourceURL = nil;
sourceFlag = nil;
inReplyTo = nil;
references = nil;
isHTML = NO;
sign = NO;
encrypt = NO;
tmpFiles = [[NSMutableArray alloc] init];
}
return self;
}
- (void) dealloc
{
[headers release];
[certificates release];
[text release];
[path release];
[sourceURL release];
[sourceFlag release];
[inReplyTo release];
[references release];
[tmpFiles release];
[super dealloc];
}
/* draft folder functionality */
- (NSString *) userSpoolFolderPath
{
return [[self container] userSpoolFolderPath];
}
/* draft object functionality */
- (NSString *) draftFolderPath
{
if (!path)
{
path = [[self userSpoolFolderPath] stringByAppendingPathComponent: nameInContainer];
[path retain];
}
return path;
}
- (BOOL) _ensureDraftFolderPath
{
NSFileManager *fm;
fm = [NSFileManager defaultManager];
return ([fm createDirectoriesAtPath: [container userSpoolFolderPath]
attributes: nil]
&& [fm createDirectoriesAtPath: [self draftFolderPath]
attributes:nil]);
}
- (NSString *) infoPath
{
return [[self draftFolderPath]
stringByAppendingPathComponent: @".info.plist"];
}
/* contents */
- (void) setHeaders: (NSDictionary *) newHeaders
{
id headerValue;
unsigned int count;
NSString *messageID, *priority, *pureSender, *replyTo, *receipt;
for (count = 0; count < 8; count++)
{
headerValue = [newHeaders objectForKey: headerKeys[count]];
if (headerValue)
[headers setObject: headerValue
forKey: headerKeys[count]];
else if ([headers objectForKey: headerKeys[count]])
[headers removeObjectForKey: headerKeys[count]];
}
messageID = [headers objectForKey: @"message-id"];
if (!messageID)
{
messageID = [NSString generateMessageID: [[newHeaders objectForKey: @"from"] pureEMailAddress]];
[headers setObject: messageID forKey: @"message-id"];
}
priority = [newHeaders objectForKey: @"X-Priority"];
if (priority)
{
// newHeaders come from MIME message; convert X-Priority to Web representation
[headers setObject: priority forKey: @"X-Priority"];
[headers removeObjectForKey: @"priority"];
if ([priority isEqualToString: @"1 (Highest)"])
{
[headers setObject: @"HIGHEST" forKey: @"priority"];
}
else if ([priority isEqualToString: @"2 (High)"])
{
[headers setObject: @"HIGH" forKey: @"priority"];
}
else if ([priority isEqualToString: @"4 (Low)"])
{
[headers setObject: @"LOW" forKey: @"priority"];
}
else if ([priority isEqualToString: @"5 (Lowest)"])
{
[headers setObject: @"LOWEST" forKey: @"priority"];
}
}
else
{
// newHeaders come from Web form; convert priority to MIME header representation
priority = [newHeaders objectForKey: @"priority"];
if ([priority intValue] == 1)
{
[headers setObject: @"1 (Highest)" forKey: @"X-Priority"];
}
else if ([priority intValue] == 2)
{
[headers setObject: @"2 (High)" forKey: @"X-Priority"];
}
else if ([priority intValue] == 4)
{
[headers setObject: @"4 (Low)" forKey: @"X-Priority"];
}
else if ([priority intValue] == 5)
{
[headers setObject: @"5 (Lowest)" forKey: @"X-Priority"];
}
else
{
[headers removeObjectForKey: @"X-Priority"];
}
if (priority)
{
[headers setObject: priority forKey: @"priority"];
}
}
replyTo = [headers objectForKey: @"replyTo"];
if ([replyTo length] > 0)
{
[headers setObject: replyTo forKey: @"reply-to"];
}
[headers removeObjectForKey: @"replyTo"];
receipt = [newHeaders objectForKey: @"Disposition-Notification-To"];
if ([receipt length] > 0)
{
[headers setObject: @"true" forKey: @"receipt"];
[headers setObject: receipt forKey: @"Disposition-Notification-To"];
[headers setObject: receipt forKey: @"Return-Receipt-To"];
}
else
{
receipt = [newHeaders objectForKey: @"receipt"];
if ([receipt boolValue])
{
[headers setObject: receipt forKey: @"receipt"];
pureSender = [[newHeaders objectForKey: @"from"] pureEMailAddress];
if (pureSender)
{
[headers setObject: pureSender forKey: @"Disposition-Notification-To"];
[headers setObject: pureSender forKey: @"Return-Receipt-To"];
}
}
else
{
[headers removeObjectForKey: @"receipt"];
[headers removeObjectForKey: @"Disposition-Notification-To"];
[headers removeObjectForKey: @"Return-Receipt-To"];
}
}
}
- (NSDictionary *) headers
{
return headers;
}
- (void) setText: (NSString *) newText
{
ASSIGN (text, [newText stringWithoutHTMLInjection: NO]);
}
- (NSString *) text
{
return text;
}
- (void) setIsHTML: (BOOL) aBool
{
isHTML = aBool;
}
- (BOOL) isHTML
{
return isHTML;
}
- (void) setSign: (BOOL) aBool
{
sign = aBool;
}
- (BOOL) sign
{
return sign;
}
- (void) setEncrypt: (BOOL) aBool
{
encrypt = aBool;
}
- (BOOL) encrypt
{
return encrypt;
}
- (NSString *) inReplyTo
{
return inReplyTo;
}
- (void) setInReplyTo: (NSString *) newInReplyTo
{
ASSIGN (inReplyTo, newInReplyTo);
}
- (NSString *) references
{
return references;
}
- (void) setReferences: (NSString *) newReferences
{
ASSIGN (references, newReferences);
}
- (void) setSourceURL: (NSString *) newSourceURL
{
ASSIGN (sourceURL, newSourceURL);
}
- (void) setSourceFlag: (NSString *) newSourceFlag
{
ASSIGN (sourceFlag, newSourceFlag);
}
- (void) setSourceFolder: (NSString *) newSourceFolder
{
ASSIGN (sourceFolder, newSourceFolder);
}
- (void) setSourceFolderWithMailObject: (SOGoMailObject *) sourceMail
{
NSMutableArray *paths;
id parent;
parent = [sourceMail container];
paths = [NSMutableArray arrayWithCapacity: 1];
while (parent && ![parent isKindOfClass: [SOGoMailAccount class]])
{
[paths insertObject: [parent nameInContainer] atIndex: 0];
parent = [parent container];
}
if (parent)
[paths insertObject: [NSString stringWithFormat: @"/%@", [parent nameInContainer]]
atIndex: 0];
[self setSourceFolder: [paths componentsJoinedByString: @"/"]];
}
//
//
//
- (NSString *) sourceFolder
{
return sourceFolder;
}
//
// Store the message definition in a plist file (.info.plist) in the spool directory
//
- (NSException *) storeInfo
{
NSMutableDictionary *infos;
NSException *error;
if ([self _ensureDraftFolderPath])
{
infos = [NSMutableDictionary dictionary];
[infos setObject: headers forKey: @"headers"];
if (text)
[infos setObject: text forKey: @"text"];
[infos setObject: [NSNumber numberWithBool: isHTML]
forKey: @"isHTML"];
if (inReplyTo)
[infos setObject: inReplyTo forKey: @"inReplyTo"];
if (references)
[infos setObject: references forKey: @"references"];
if (sourceIMAP4ID > -1)
[infos setObject: [NSString stringWithFormat: @"%i", sourceIMAP4ID]
forKey: @"sourceIMAP4ID"];
if (IMAP4ID > -1)
[infos setObject: [NSString stringWithFormat: @"%i", IMAP4ID]
forKey: @"IMAP4ID"];
if (sourceURL && sourceFlag && sourceFolder)
{
[infos setObject: sourceURL forKey: @"sourceURL"];
[infos setObject: sourceFlag forKey: @"sourceFlag"];
[infos setObject: sourceFolder forKey: @"sourceFolder"];
}
if ([infos writeToFile: [self infoPath] atomically: YES])
error = nil;
else
{
[self errorWithFormat: @"could not write info: '%@'",
[self infoPath]];
error = [NSException exceptionWithHTTPStatus:500 /* server error */
reason: @"could not write draft info!"];
}
}
else
{
[self errorWithFormat: @"could not create folder for draft: '%@'",
[self draftFolderPath]];
error = [NSException exceptionWithHTTPStatus:500 /* server error */
reason: @"could not create folder for draft!"];
}
return error;
}
//
//
//
- (void) _loadInfosFromDictionary: (NSDictionary *) infoDict
{
id value;
value = [infoDict objectForKey: @"headers"];
if (value)
[self setHeaders: value];
value = [infoDict objectForKey: @"text"];
if ([value length] > 0)
[self setText: value];
isHTML = [[infoDict objectForKey: @"isHTML"] boolValue];
value = [infoDict objectForKey: @"sourceIMAP4ID"];
if (value)
[self setSourceIMAP4ID: [value intValue]];
value = [infoDict objectForKey: @"IMAP4ID"];
if (value)
[self setIMAP4ID: [value intValue]];
value = [infoDict objectForKey: @"sourceURL"];
if (value)
[self setSourceURL: value];
value = [infoDict objectForKey: @"sourceFlag"];
if (value)
[self setSourceFlag: value];
value = [infoDict objectForKey: @"sourceFolder"];
if (value)
[self setSourceFolder: value];
value = [infoDict objectForKey: @"inReplyTo"];
if (value)
[self setInReplyTo: value];
value = [infoDict objectForKey: @"references"];
if (value)
[self setReferences: value];
}
//
//
//
- (NSString *) relativeImap4Name
{
return [NSString stringWithFormat: @"%d", IMAP4ID];
}
//
//
//
- (void) fetchInfo
{
NSString *p;
NSDictionary *infos;
NSFileManager *fm;
p = [self infoPath];
fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath: p])
{
infos = [NSDictionary dictionaryWithContentsOfFile: p];
if (infos)
[self _loadInfosFromDictionary: infos];
// else
// [self errorWithFormat: @"draft info dictionary broken at path: %@", p];
}
else
[self debugWithFormat: @"Note: info object does not yet exist: %@", p];
}
//
//
//
- (void) setSourceIMAP4ID: (int) newSourceIMAP4ID
{
sourceIMAP4ID = newSourceIMAP4ID;
}
//
//
//
- (int) sourceIMAP4ID
{
return sourceIMAP4ID;
}
//
//
//
- (void) setIMAP4ID: (int) newIMAP4ID
{
IMAP4ID = newIMAP4ID;
}
//
//
//
- (int) IMAP4ID
{
return IMAP4ID;
}
//
//
//
- (NSException *) save
{
NGImap4Client *client;
NSException *error;
NSData *message;
NSString *folder;
id result;
error = nil;
message = [self mimeMessageForRecipient: nil extractingImages: NO];
if (!message)
{
error = [NSException exceptionWithHTTPStatus: 500 /* Server Error */
reason: @"Message is too big"];
return error;
}
client = [[self imap4Connection] client];
if (![imap4 doesMailboxExistAtURL: [container imap4URL]])
{
[[self imap4Connection] createMailbox: [[self imap4Connection] imap4FolderNameForURL: [container imap4URL]]
atURL: [[self mailAccountFolder] imap4URL]];
[imap4 flushFolderHierarchyCache];
}
folder = [imap4 imap4FolderNameForURL: [container imap4URL]];
result = [client append: message toFolder: folder
withFlags: [NSArray arrayWithObjects: @"draft", nil]];
if ([[result objectForKey: @"result"] boolValue])
{
if (IMAP4ID > -1)
error = [imap4 markURLDeleted: [self imap4URL]];
[self setIMAP4ID: [self IMAP4IDFromAppendResult: result]];
if (imap4URL)
{
// Invalidate the IMAP message URL since the message ID has changed
[imap4URL release];
imap4URL = nil;
}
}
else
error = [NSException exceptionWithHTTPStatus: 500 /* Server Error */
reason: [result objectForKey: @"reason"]];
return error;
}
//
//
//
- (void) _addEMailsOfAddresses: (NSArray *) _addrs
toArray: (NSMutableArray *) _ma
{
NSEnumerator *addresses;
NGImap4EnvelopeAddress *currentAddress;
addresses = [_addrs objectEnumerator];
while ((currentAddress = [addresses nextObject]))
if ([currentAddress email])
[_ma addObject: [currentAddress email]];
}
//
//
//
- (void) _addRecipients: (NSArray *) recipients
toArray: (NSMutableArray *) array
{
NSEnumerator *addresses;
NGImap4EnvelopeAddress *currentAddress;
addresses = [recipients objectEnumerator];
while ((currentAddress = [addresses nextObject]))
if ([currentAddress baseEMail])
[array addObject: [currentAddress baseEMail]];
}
//
//
//
- (void) _purgeRecipients: (NSArray *) recipients
fromAddresses: (NSMutableArray *) addresses
{
NSEnumerator *allRecipients;
NSString *currentRecipient;
NGImap4EnvelopeAddress *currentAddress;
int count, max;
max = [addresses count];
allRecipients = [recipients objectEnumerator];
while (max > 0
&& ((currentRecipient = [allRecipients nextObject])))
for (count = max - 1; count >= 0; count--)
{
currentAddress = [addresses objectAtIndex: count];
if (![currentAddress baseEMail] ||
[currentRecipient
caseInsensitiveCompare: [currentAddress baseEMail]]
== NSOrderedSame)
{
[addresses removeObjectAtIndex: count];
max--;
}
}
}
//
//
//
- (NSString *) _emailFromIdentity: (NSDictionary *) identity
{
NSString *fullName, *format;
fullName = [identity objectForKey: @"fullName"];
if ([fullName length])
format = @"%{fullName} <%{email}>";
else
format = @"%{email}";
return [identity keysWithFormat: format];
}
- (void) _fillInFromAddress: (NSMutableDictionary *) _info
fromSentMailbox: (BOOL) _fromSentMailbox
envelope: (NGImap4Envelope *) _envelope
{
NSDictionary *identity;
NSMutableArray *addrs;
NSString *email;
SOGoMailAccount *account;
int i;
identity = nil;
account = [[self container] mailAccountFolder];
if (![account forceDefaultIdentity])
{
/* Pick the first email matching one of the account's identities */
addrs = [NSMutableArray array];
if (_fromSentMailbox)
[self _addRecipients: [_envelope from] toArray: addrs];
else
{
[self _addRecipients: [_envelope to] toArray: addrs];
[self _addRecipients: [_envelope cc] toArray: addrs];
[self _addRecipients: [_envelope bcc] toArray: addrs];
}
if ([addrs count])
{
for (i = 0; !identity && i < [addrs count]; i++)
{
email = [addrs objectAtIndex: i];
identity = [[[self container] mailAccountFolder] identityForEmail: email];
}
if (identity)
{
[_info setObject: [self _emailFromIdentity: identity] forKey: @"from"];
}
}
}
if (!identity)
{
identity = [account defaultIdentity];
if (identity)
[_info setObject: [self _emailFromIdentity: identity] forKey: @"from"];
}
}
//
//
//
- (void) _fillInReplyAddresses: (NSMutableDictionary *) _info
replyToAll: (BOOL) _replyToAll
fromSentMailbox: (BOOL) _fromSentMailbox
envelope: (NGImap4Envelope *) _envelope
{
/*
The rules as implemented by Thunderbird:
- if there is a 'reply-to' header, only include that (as TO)
- if we reply to all, all non-from addresses are added as CC
- the from is always the lone TO (except for reply-to)
Note: we cannot check reply-to, because Cyrus even sets a reply-to in the
envelope if none is contained in the message itself! (bug or
feature?)
*/
NSMutableArray *to, *addrs, *allRecipients;
NSArray *envelopeAddresses;
allRecipients = [NSMutableArray array];
//
// When we do a Reply-To or a Reply-To-All, we strip our own addresses
// from the list of recipients so we don't reply to ourself! We check
// which addresses we should use - that is the ones for the current
// user if we're dealing with the default "SOGo mail account" or
// the ones specified in the auxiliary IMAP accounts
//
if ([[[self->container mailAccountFolder] nameInContainer] intValue] == 0)
{
NSArray *userEmails;
userEmails = [[context activeUser] allEmails];
[allRecipients addObjectsFromArray: userEmails];
}
else
{
NSArray *identities;
NSString *email;
int i;
identities = [[[self container] mailAccountFolder] identities];
for (i = 0; i < [identities count]; i++)
{
email = [[identities objectAtIndex: i] objectForKey: @"email"];
if (email)
[allRecipients addObject: email];
}
}
to = [NSMutableArray arrayWithCapacity: 2];
addrs = [NSMutableArray array];
envelopeAddresses = [_envelope replyTo];
if (_fromSentMailbox)
[addrs setArray: [_envelope to]];
else if ([envelopeAddresses count])
[addrs setArray: envelopeAddresses];
else
[addrs setArray: [_envelope from]];
[self _purgeRecipients: allRecipients fromAddresses: addrs]; // addrs contain the recipient addresses without any of the sender's addresses
[self _addEMailsOfAddresses: addrs toArray: to];
[self _addRecipients: addrs toArray: allRecipients];
[_info setObject: to forKey: @"to"];
/* If "to" is empty, we add at least ourself as a recipient!
This is for emails in the "Sent" folder that we reply to... */
if (![to count])
{
if ([[_envelope replyTo] count])
[self _addEMailsOfAddresses: [_envelope replyTo] toArray: to];
else
[self _addEMailsOfAddresses: [_envelope from] toArray: to];
}
/* Pick the first email matching one of the account's identities */
[self _fillInFromAddress: _info
fromSentMailbox: _fromSentMailbox
envelope: _envelope];
/* If we have no To but we have Cc recipients, let's move the Cc
to the To bucket... */
if ([[_info objectForKey: @"to"] count] == 0 && [_info objectForKey: @"cc"])
{
id o;
o = [_info objectForKey: @"cc"];
[_info setObject: o forKey: @"to"];
[_info removeObjectForKey: @"cc"];
}
/* CC processing if we reply-to-all: - we add all 'to', 'cc' and 'bcc' fields */
if (_replyToAll)
{
to = [NSMutableArray array];
[addrs setArray: [_envelope to]];
[self _purgeRecipients: allRecipients
fromAddresses: addrs];
[self _addEMailsOfAddresses: addrs toArray: to];
[self _addRecipients: addrs toArray: allRecipients];
[addrs setArray: [_envelope cc]];
[self _purgeRecipients: allRecipients
fromAddresses: addrs];
[self _addEMailsOfAddresses: addrs toArray: to];
[self _addRecipients: addrs toArray: allRecipients];
[_info setObject: to forKey: @"cc"];
if ([[_envelope bcc] count])
{
to = [NSMutableArray array];
[addrs setArray: [_envelope bcc]];
[self _purgeRecipients: allRecipients
fromAddresses: addrs];
[self _addEMailsOfAddresses: addrs toArray: to];
[_info setObject: to forKey: @"bcc"];
}
}
}
//
//
//
- (void) _fetchAttachmentsFromMail: (SOGoMailObject *) sourceMail onlyImages: (BOOL) onlyImages
{
NSMutableDictionary *currentInfo;
NSArray *attachments;
unsigned int max, count;
attachments = [sourceMail fetchFileAttachments];
max = [attachments count];
for (count = 0; count < max; count++)
{
currentInfo = [attachments objectAtIndex: count];
if (!onlyImages
|| (onlyImages
&& [[NGMimeBodyPart imageMimeTypes] containsObject: [currentInfo objectForKey: @"mimetype"]]
&& [currentInfo objectForKey: @"bodyId"]
&& [[currentInfo objectForKey: @"bodyId"] length] > 0)) {
[self saveAttachment: [currentInfo objectForKey: @"body"]
withMetadata: currentInfo];
}
}
}
//
//
//
- (void) _fileAttachmentsFromPart: (id) thePart onlyImages: (BOOL) onlyImages
{
// Small hack to avoid SOPE's stupid behavior to wrap a multipart
// object in a NGMimeBodyPart.
if ([thePart isKindOfClass: [NGMimeBodyPart class]] &&
[[[thePart contentType] type] isEqualToString: @"multipart"])
thePart = [thePart body];
if ([thePart isKindOfClass: [NGMimeBodyPart class]])
{
NSString *filename, *mimeType, *bodyId;
id body;
if (onlyImages && ![thePart isImage]) {
return;
}
mimeType = [[thePart contentType] stringValue];
bodyId = [[thePart contentId] stringValue];
body = [thePart body];
filename = [(NGMimeContentDispositionHeaderField *)[thePart headerForKey: @"content-disposition"] filename];
if (!filename)
filename = [mimeType asPreferredFilenameUsingPath: nil];
if (filename)
{
NSMutableDictionary *currentInfo;
currentInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
filename, @"filename",
mimeType, @"mimetype",
bodyId, @"bodyId",
nil];
[self saveAttachment: body
withMetadata: currentInfo];
}
}
else if ([thePart isKindOfClass: [NGMimeMultipartBody class]])
{
NSArray *parts;
int i;
parts = [thePart parts];
for (i = 0; i < [parts count]; i++)
{
[self _fileAttachmentsFromPart: [parts objectAtIndex: i] onlyImages: onlyImages];
}
}
}
//
//
//
- (void) _fetchAttachmentsFromEncryptedMail: (SOGoMailObject *) sourceMail onlyImages: (BOOL) onlyImages
{
NSData *certificate;
certificate = [[self mailAccountFolder] certificate];
// If we got a user certificate, let's use it. Otherwise we fallback we
// don't try to get any attachments from the encrypted content
if (certificate)
{
NGMimeMessage *m;
m = [[sourceMail content] messageFromEncryptedDataAndCertificate: certificate];
[self _fileAttachmentsFromPart: [m body] onlyImages: onlyImages];
}
}
//
//
//
- (void) _fetchAttachmentsFromOpaqueSignedMail: (SOGoMailObject *) sourceMail onlyImages: (BOOL) onlyImages
{
NGMimeMessage *m;
m = [[sourceMail content] messageFromOpaqueSignedData];
[self _fileAttachmentsFromPart: [m body] onlyImages: onlyImages];
}
//
//
//
- (void) fetchMailForEditing: (SOGoMailObject *) sourceMail
{