/
SOGoUser.m
1166 lines (985 loc) · 33 KB
/
SOGoUser.m
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) 2006-2020 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 SOGo; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#import <NGObjWeb/WOApplication.h>
#import <NGObjWeb/WOContext.h>
#import <NGObjWeb/WORequest.h>
#import <NGExtensions/NSCalendarDate+misc.h>
#import <NGExtensions/NSNull+misc.h>
#import <NGExtensions/NSObject+Logs.h>
#import <Appointments/SOGoAppointmentFolders.h>
#import <Contacts/SOGoContactFolders.h>
#import "NSArray+Utilities.h"
#import "SOGoCache.h"
#import "SOGoDateFormatter.h"
#import "SOGoPermissions.h"
#import "SOGoSystemDefaults.h"
#import "SOGoUserFolder.h"
#import "SOGoUserManager.h"
#import "SOGoUserSettings.h"
#import "WOResourceManager+SOGo.h"
#if defined(MFA_CONFIG)
#include <liboath/oath.h>
#endif
#import "SOGoUser.h"
@implementation SoUser (SOGoExtension)
- (SOGoDomainDefaults *) userDefaults
{
return [SOGoSystemDefaults sharedSystemDefaults];
}
- (SOGoDomainDefaults *) domainDefaults
{
return [SOGoSystemDefaults sharedSystemDefaults];
}
@end
@implementation SOGoUser
// + (NSString *) language
// {
// NSArray *bLanguages;
// WOContext *context;
// NSString *lng;
// context = [[WOApplication application] context];
// bLanguages = [[context request] browserLanguages];
// if ([bLanguages count] > 0)
// lng = [bLanguages objectAtIndex: 0];
// if (![lng length])
// lng = defaultLanguage;
// return lng;
// }
+ (SOGoUser *) userWithLogin: (NSString *) newLogin
{
return [self userWithLogin: newLogin roles: nil];
}
+ (SOGoUser *) userWithLogin: (NSString *) newLogin
roles: (NSArray *) newRoles
{
return [self userWithLogin: newLogin roles: newRoles trust: NO];
}
+ (SOGoUser *) userWithLogin: (NSString *) newLogin
roles: (NSArray *) newRoles
trust: (BOOL) b
{
SOGoCache *cache;
SOGoUser *user;
cache = [SOGoCache sharedCache];
user = [cache userNamed: newLogin];
if (!user)
{
user = [[self alloc] initWithLogin: newLogin roles: newRoles trust: b];
if (user)
{
[cache registerUser: user withName: newLogin];
[user release];
}
}
if (newRoles)
[user setPrimaryRoles: newRoles];
return user;
}
/**
* Return a new instance for the login name, which can be appended by a
* domain name. The domain is extracted only if the system defaults
* SOGoEnableDomainBasedUID is enabled.
*
* @param newLogin a login name optionally follow by @domain
* @param newRoles
* @param b is set to YES if newLogin can be trust
* @see loginInDomain
* @see [SOGoSession decodeValue:usingKey:login:domain:password:]
*/
- (id) initWithLogin: (NSString *) newLogin
roles: (NSArray *) newRoles
trust: (BOOL) b
{
SOGoUserManager *um;
SOGoSystemDefaults *sd;
NSDictionary *contactInfos;
NSString *realUID, *uid, *domain;
NSRange r;
_defaults = nil;
_settings = nil;
uid = nil;
realUID = nil;
domain = nil;
if ([newLogin isEqualToString: @"anonymous"]
|| [newLogin isEqualToString: @"freebusy"])
realUID = newLogin;
else
{
sd = [SOGoSystemDefaults sharedSystemDefaults];
if ([sd enableDomainBasedUID] || [[sd loginDomains] count] > 0)
{
r = [newLogin rangeOfString: @"@" options: NSBackwardsSearch];
if (r.location != NSNotFound)
{
// The domain is probably appended to the username;
// make sure it is defined as a domain in the configuration.
domain = [newLogin substringFromIndex: (r.location + r.length)];
if ([[SOGoUserManager sharedUserManager] isDomainDefined: domain] &&
![sd enableDomainBasedUID])
newLogin = [newLogin substringToIndex: r.location];
if (domain != nil && ![sd enableDomainBasedUID])
// Login domains are enabled (SOGoLoginDomains) but not
// domain-based UID (SOGoEnableDomainBasedUID).
// Drop the domain from the login name.
domain = nil;
}
}
newLogin = [newLogin stringByReplacingString: @"%40"
withString: @"@"];
if (b)
realUID = newLogin;
else
{
um = [SOGoUserManager sharedUserManager];
contactInfos = [um contactInfosForUserWithUIDorEmail: newLogin
inDomain: domain];
realUID = [contactInfos objectForKey: @"c_uid"];
if (domain == nil && [sd enableDomainBasedUID])
domain = [contactInfos objectForKey: @"c_domain"];
}
if ([realUID length] && [domain length])
{
// When the user is associated to a domain, the [SOGoUser login]
// method returns the combination login@domain while
// [SOGoUser loginInDomain] only returns the login.
r = [realUID rangeOfString: domain options: NSBackwardsSearch|NSCaseInsensitiveSearch];
// Do NOT strip @domain.com if SOGoEnableDomainBasedUID is enabled since
// the real login most likely is the email address.
if (r.location != NSNotFound && ![sd enableDomainBasedUID])
uid = [realUID substringToIndex: r.location-1];
// If we don't have the domain in the UID but SOGoEnableDomainBasedUID is
// enabled, let's add it internally so so it becomes unique across
// all potential domains.
else if (r.location == NSNotFound && [sd enableDomainBasedUID])
{
uid = [NSString stringWithString: realUID];
realUID = [NSString stringWithFormat: @"%@@%@", realUID, domain];
}
// We found the domain and SOGoEnableDomainBasedUID is enabled,
// we keep realUID.. This would happen for example if the user
// authenticates with foo@bar.com and the UIDFieldName is also foo@bar.com
else if ([sd enableDomainBasedUID])
uid = [NSString stringWithString: realUID];
}
}
if ([realUID length])
{
if ((self = [super initWithLogin: realUID roles: newRoles]))
{
allEmails = nil;
currentPassword = nil;
cn = nil;
ASSIGN (loginInDomain, (uid ? uid : realUID));
_defaults = nil;
_domainDefaults = nil;
_settings = nil;
mailAccounts = nil;
}
}
else
{
[self release];
self = nil;
}
return self;
}
- (void) dealloc
{
[_defaults release];
[_domainDefaults release];
[_settings release];
[allEmails release];
[mailAccounts release];
[currentPassword release];
[cn release];
[loginInDomain release];
[super dealloc];
}
- (void) setPrimaryRoles: (NSArray *) newRoles
{
ASSIGN (roles, newRoles);
}
- (void) setCurrentPassword: (NSString *) newPassword
{
ASSIGN (currentPassword, newPassword);
}
- (NSString *) currentPassword
{
return currentPassword;
}
- (NSString *) loginInDomain
{
return loginInDomain;
}
- (id) _fetchFieldForUser: (NSString *) field
{
NSDictionary *contactInfos;
SOGoUserManager *um;
um = [SOGoUserManager sharedUserManager];
contactInfos = [um contactInfosForUserWithUIDorEmail: login];
return [contactInfos objectForKey: field];
}
- (void) _fetchAllEmails
{
allEmails = [self _fetchFieldForUser: @"emails"];
[allEmails retain];
}
- (void) _fetchCN
{
cn = [[self _fetchFieldForUser: @"cn"] stringByTrimmingSpaces];
[cn retain];
}
/* properties */
- (NSString *) domain
{
return [self _fetchFieldForUser: @"c_domain"];
}
- (id <SOGoSource>) authenticationSource
{
NSString *sourceID;
SOGoUserManager *um;
sourceID = [self _fetchFieldForUser: @"SOGoSource"];
um = [SOGoUserManager sharedUserManager];
return [um sourceWithID: sourceID];
}
- (NSArray *) allEmails
{
if (!allEmails)
[self _fetchAllEmails];
return allEmails;
}
//
// We always return the last object among our list of email addresses. This value
// is always added in SOGoUserManager: -_fillContactMailRecords:
//
- (NSString *) systemEmail
{
if (!allEmails)
[self _fetchAllEmails];
return [allEmails objectAtIndex: 0];
}
- (BOOL) hasEmail: (NSString *) email
{
if (!allEmails)
[self _fetchAllEmails];
return [allEmails containsCaseInsensitiveString: email];
}
- (NSString *) cn
{
if (!cn)
[self _fetchCN];
return cn;
}
- (NSMutableDictionary *) defaultIdentity
{
NSDictionary *defaultAccount, *currentIdentity;
NSMutableDictionary *defaultIdentity;
NSArray *identities;
NSString *defaultEmail;
unsigned int count, max;
defaultEmail = [NSString stringWithFormat: @"%@@%@", [self loginInDomain], [self domain]];
defaultAccount = [[self mailAccounts] objectAtIndex: 0];
defaultIdentity = nil;
identities = [defaultAccount objectForKey: @"identities"];
max = [identities count];
for (count = 0; count < max; count++)
{
currentIdentity = [identities objectAtIndex: count];
if ([[currentIdentity objectForKey: @"isDefault"] boolValue])
{
defaultIdentity = [NSMutableDictionary dictionaryWithDictionary: currentIdentity];
break;
}
else if ([[currentIdentity objectForKey: @"email"] caseInsensitiveCompare: defaultEmail] == NSOrderedSame)
{
defaultIdentity = [NSMutableDictionary dictionaryWithDictionary: currentIdentity];
}
}
return defaultIdentity; // can be nil
}
- (SOGoDateFormatter *) dateFormatterInContext: (WOContext *) context
{
SOGoDateFormatter *dateFormatter;
NSString *format;
SOGoUserDefaults *ud;
NSDictionary *locale;
WOResourceManager *resMgr;
dateFormatter = [SOGoDateFormatter new];
[dateFormatter autorelease];
ud = [self userDefaults];
resMgr = [[WOApplication application] resourceManager];
locale = [resMgr localeForLanguageNamed: [ud language]];
[dateFormatter setLocale: locale];
format = [ud shortDateFormat];
if (format)
[dateFormatter setShortDateFormat: format];
format = [ud longDateFormat];
if (format)
[dateFormatter setLongDateFormat: format];
format = [ud timeFormat];
if (format)
[dateFormatter setTimeFormat: format];
return dateFormatter;
}
- (NSDictionary *) currentDay
{
NSCalendarDate *now;
NSDictionary *description, *abbr;
NSDictionary *locale;
SOGoUserDefaults *ud;
WOResourceManager *resMgr;
NSUInteger seconds;
now = [NSCalendarDate calendarDate];
ud = [self userDefaults];
resMgr = [[WOApplication application] resourceManager];
locale = [resMgr localeForLanguageNamed: [ud language]];
[now setTimeZone: [ud timeZone]];
seconds = [now hourOfDay]*3600 + [now minuteOfHour]*60 + [now secondOfMinute];
abbr = [NSDictionary dictionaryWithObjectsAndKeys:
[now descriptionWithCalendarFormat: @"%a" locale: locale], @"weekday",
[now descriptionWithCalendarFormat: @"%b" locale: locale], @"month",
nil];
description = [NSDictionary dictionaryWithObjectsAndKeys:
[now descriptionWithCalendarFormat: @"%A" locale: locale], @"weekday",
[now descriptionWithCalendarFormat: @"%B" locale: locale], @"month",
[now descriptionWithCalendarFormat: @"%d" locale: locale], @"day",
[now descriptionWithCalendarFormat: @"%Y" locale: locale], @"year",
abbr, @"abbr",
[NSNumber numberWithInt: (24*3600 - seconds)], @"secondsBeforeTomorrow",
nil];
return description;
}
- (SOGoUserDefaults *) userDefaults
{
if (!_defaults)
{
_defaults = [SOGoUserDefaults defaultsForUser: login
inDomain: [self domain]];
[_defaults retain];
}
//else
// NSLog(@"User defaults cache hit for %@", login);
return _defaults;
}
- (SOGoDomainDefaults *) domainDefaults
{
NSString *domain;
if (!_domainDefaults)
{
domain = [self domain];
if ([domain length])
{
_domainDefaults = [SOGoDomainDefaults defaultsForDomain: domain];
if (!_domainDefaults)
{
//[self errorWithFormat: @"domain '%@' does not exist!", domain];
_domainDefaults = [SOGoSystemDefaults sharedSystemDefaults];
}
}
else
_domainDefaults = [SOGoSystemDefaults sharedSystemDefaults];
[_domainDefaults retain];
}
//else
// NSLog(@"User defaults cache hit for %@", login);
return _domainDefaults;
}
- (SOGoUserSettings *) userSettings
{
if (!_settings)
{
_settings = [SOGoUserSettings settingsForUser: login];
[_settings retain];
}
return _settings;
}
- (NSCalendarDate *) firstDayOfWeekForDate: (NSCalendarDate *) date
{
int offset;
NSCalendarDate *firstDay;
offset = [[self userDefaults] firstDayOfWeek] - [date dayOfWeek];
if (offset > 0)
offset -= 7;
firstDay = [date addTimeInterval: offset * 86400];
return firstDay;
}
- (unsigned int) dayOfWeekForDate: (NSCalendarDate *) date
{
unsigned int offset, baseDayOfWeek, dayOfWeek;
offset = [[self userDefaults] firstDayOfWeek];
baseDayOfWeek = [date dayOfWeek];
if (offset > baseDayOfWeek)
baseDayOfWeek += 7;
dayOfWeek = baseDayOfWeek - offset;
return dayOfWeek;
}
- (NSCalendarDate *) firstWeekOfYearForDate: (NSCalendarDate *) date
{
NSString *firstWeekRule;
NSCalendarDate *januaryFirst, *firstWeek;
unsigned int dayOfWeek;
firstWeekRule = [[self userDefaults] firstWeekOfYear];
januaryFirst = [NSCalendarDate dateWithYear: [date yearOfCommonEra]
month: 1 day: 1 hour: 0 minute: 0 second: 0
timeZone: [date timeZone]];
if ([firstWeekRule isEqualToString: SOGoWeekStartFirst4DayWeek])
{
dayOfWeek = [self dayOfWeekForDate: januaryFirst];
if (dayOfWeek < 4)
firstWeek = [self firstDayOfWeekForDate: januaryFirst];
else
firstWeek = [self firstDayOfWeekForDate: [januaryFirst
dateByAddingYears: 0
months: 0
days: 7]];
}
else if ([firstWeekRule isEqualToString: SOGoWeekStartFirstFullWeek])
{
dayOfWeek = [self dayOfWeekForDate: januaryFirst];
if (dayOfWeek == 0)
firstWeek = [self firstDayOfWeekForDate: januaryFirst];
else
firstWeek = [self firstDayOfWeekForDate: [januaryFirst
dateByAddingYears: 0
months: 0
days: 7]];
}
else
firstWeek = [self firstDayOfWeekForDate: januaryFirst];
return firstWeek;
}
- (unsigned int) weekNumberForDate: (NSCalendarDate *) date
{
NSCalendarDate *firstWeek, *previousWeek;
unsigned int weekNumber;
firstWeek = [self firstWeekOfYearForDate: date];
if ([firstWeek earlierDate: date] == firstWeek)
{
weekNumber = ([date timeIntervalSinceDate: firstWeek] / (86400 * 7) + 1);
}
else
{
// Date is within the last week of the previous year;
// Compute the previous week number to find the week number of the requested date.
// The number will either be 52 or 53.
previousWeek = [date dateByAddingYears: 0
months: 0
days: -7];
firstWeek = [self firstWeekOfYearForDate: previousWeek];
weekNumber = ([previousWeek timeIntervalSinceDate: firstWeek] / (86400 * 7) + 1);
weekNumber += 1;
}
return weekNumber;
}
/* mail */
- (BOOL) _migrateFolderWithPurpose: (NSString *) purpose
withName: (NSString *) folderName
{
NSString *methodName;
SEL methodSel;
BOOL rc;
[self userDefaults];
methodName = [NSString stringWithFormat: @"set%@FolderName:", purpose];
methodSel = NSSelectorFromString (methodName);
if ([_defaults respondsToSelector: methodSel])
{
[_defaults performSelector: methodSel withObject: folderName];
rc = YES;
}
else
{
[self errorWithFormat: @"method '%@' not available with user defaults"
@" object, folder migration fails", methodName];
rc = NO;
}
return rc;
}
- (void) _migrateFolderSettings
{
NSMutableDictionary *mailSettings;
NSString *folderName, *key;
BOOL migrated;
NSString **purpose;
NSString *purposes[] = { @"Drafts", @"Sent", @"Trash", nil };
[self userSettings];
mailSettings = [_settings objectForKey: @"Mail"];
if (mailSettings)
{
migrated = NO;
purpose = purposes;
while (*purpose)
{
key = [NSString stringWithFormat: @"%@Folder", *purpose];
folderName = [mailSettings objectForKey: key];
if ([folderName length]
&& [self _migrateFolderWithPurpose: *purpose
withName: folderName])
{
migrated = YES;
[mailSettings removeObjectForKey: key];
folderName = nil;
}
purpose++;
}
if (migrated)
{
[_settings synchronize];
[self userDefaults];
[_defaults synchronize];
}
}
}
- (void) _appendSystemMailAccountWithDelegatedIdentities: (BOOL) appendDeletegatedIdentities
{
NSString *fullName, *imapLogin, *imapServer, *cImapServer,
*encryption, *scheme, *action, *query, *customEmail, *sieveServer;
NSMutableDictionary *mailAccount, *identity, *mailboxes, *receipts, *security, *mailSettings;
NSNumber *port;
NSMutableArray *identities, *mails;
NSArray *delegators, *delegates;
NSURL *url, *cUrl;
unsigned int count, max; //, default_identity;
NSInteger defaultPort;
NSUInteger index;
BOOL hasDefaultIdentity;
[self userDefaults]; // set _defaults
[self userSettings]; // set _settings
mailSettings = [_settings objectForKey: @"Mail"];
mailAccount = [NSMutableDictionary new];
// 1. login
imapLogin = [[SOGoUserManager sharedUserManager]
getExternalLoginForUID: [self loginInDomain]
inDomain: [self domain]];
[mailAccount setObject: imapLogin forKey: @"userName"];
// 2. server
// imapServer might have the following format
// localhost
// localhost:143
// imap://localhost
// imap://localhost:143
// imaps://localhost:993
// imaps://localhost:143/?tls=YES
// imaps://localhost/?tls=YES
cImapServer = [self _fetchFieldForUser: @"c_imaphostname"];
imapServer = [[self domainDefaults] imapServer];
cUrl = [NSURL URLWithString: (cImapServer ? cImapServer : @"")];
url = [NSURL URLWithString: imapServer];
if([cUrl host])
imapServer = [cUrl host];
else
if(cImapServer)
imapServer = cImapServer;
else
if([url host])
imapServer = [url host];
[mailAccount setObject: imapServer forKey: @"serverName"];
// 3. port & encryption
scheme = [cUrl scheme] ? [cUrl scheme] : [url scheme];
query = [cUrl query] ? [cUrl query] : [url query];
if (scheme
&& [scheme caseInsensitiveCompare: @"imaps"] == NSOrderedSame)
{
if (query && [query caseInsensitiveCompare: @"tls=YES"] == NSOrderedSame)
{
defaultPort = 143;
encryption = @"tls";
}
else
{
encryption = @"ssl";
defaultPort = 993;
}
}
else
{
if (query && [query caseInsensitiveCompare: @"tls=YES"] == NSOrderedSame)
encryption = @"tls";
else
encryption = @"none";
defaultPort = 143;
}
port = [cUrl port] ? [cUrl port] : [url port];
if ([port intValue] == 0) /* port is nil or intValue == 0 */
port = [NSNumber numberWithInt: defaultPort];
[mailAccount setObject: port forKey: @"port"];
[mailAccount setObject: encryption forKey: @"encryption"];
// 4. Sieve server
sieveServer = [self _fetchFieldForUser: @"c_sievehostname"];
if (sieveServer)
{
[mailAccount setObject: sieveServer forKey: @"sieveServerName"];
}
// 5. Identities
identities = [NSMutableArray new];
[identities addObjectsFromArray: [_defaults mailIdentities]];
mails = [NSMutableArray arrayWithArray: [self allEmails]];
[mailAccount setObject: [mails objectAtIndex: 0] forKey: @"name"];
max = [identities count];
hasDefaultIdentity = NO;
fullName = [self cn];
if ([fullName length] == 0)
fullName = login;
// Sanitize identities
for (count = 0; count < max; count++)
{
identity = [NSMutableDictionary dictionaryWithDictionary: [identities objectAtIndex: count]];
customEmail = [identity objectForKey: @"email"];
if ([customEmail length])
{
if (![[self domainDefaults] mailCustomFromEnabled])
{
// No custom from -- enforce a valid email
index = [mails indexOfObject: customEmail];
if (index == NSNotFound)
{
[identity setObject: [self systemEmail] forKey: @"email"];
}
}
}
else
{
// Email must be defined
[identity setObject: [self systemEmail] forKey: @"email"];
}
if (![[self domainDefaults] mailCustomFromEnabled])
{
// No custom from -- enforce a valid fullname and remove reply-to
[identity setObject: fullName forKey: @"fullName"];
[identity removeObjectForKey: @"replyTo"];
}
if (!appendDeletegatedIdentities)
{
[identity setObject: [NSNumber numberWithBool: YES] forKey: @"isReadOnly"];
}
if ([[identity objectForKey: @"isDefault"] boolValue])
{
if (hasDefaultIdentity || !appendDeletegatedIdentities)
[identity removeObjectForKey: @"isDefault"]; // only one possible default identity
else
hasDefaultIdentity = YES;
}
[identities replaceObjectAtIndex: count withObject: identity];
}
if (![identities count])
{
// Create an identity for each email address
max = [mails count];
for (count = 0; count < max; count++)
{
identity = [NSMutableDictionary dictionaryWithObjectsAndKeys:
fullName, @"fullName",
[mails objectAtIndex: count], @"email", nil];
if (appendDeletegatedIdentities)
{
if (count == 0)
{
// First identity uses the system email -- mark it as the default
[identity setObject: [NSNumber numberWithBool: YES] forKey: @"isDefault"];
hasDefaultIdentity = YES;
}
}
else
{
[identity setObject: [NSNumber numberWithBool: YES] forKey: @"isReadOnly"];
}
[identities addObject: identity];
}
}
/* identities from delegators */
if (appendDeletegatedIdentities)
{
delegators = [mailSettings objectForKey: @"DelegateFrom"];
if (delegators)
{
BOOL dirty;
NSDictionary *delegatorAccount, *delegatorSettings;
NSMutableArray *validDelegators;
NSString *delegatorLogin;
SOGoUser *delegatorUser;
dirty = NO;
validDelegators = [NSMutableArray array];
max = [delegators count];
for (count = 0; count < max; count++)
{
// 1. Verify if delegator is valid
delegatorLogin = [delegators objectAtIndex: count];
delegatorUser = [SOGoUser userWithLogin: delegatorLogin];
if (delegatorUser)
{
// 2. Verify if delegator still delegates to user
delegatorSettings = [[delegatorUser userSettings] objectForKey: @"Mail"];
delegates = [delegatorSettings objectForKey: @"DelegateTo"];
if ([delegates containsObject: [self login]])
{
[validDelegators addObject: delegatorLogin];
delegatorAccount = [[delegatorUser mailAccountsWithDelegatedIdentities: NO] objectAtIndex: 0];
[identities addObjectsFromArray: [delegatorAccount objectForKey: @"identities"]];
}
else
dirty = YES;
}
else
dirty = YES;
}
if (dirty)
{
[mailSettings setObject: validDelegators
forKey: @"DelegateFrom"];
[_settings synchronize];
}
}
}
[mailAccount setObject: identities forKey: @"identities"];
[identities release];
// 6. Receipts
if ([_defaults allowUserReceipt])
{
receipts = [NSMutableDictionary new];
[receipts setObject: @"allow" forKey: @"receiptAction"];
action = [_defaults userReceiptNonRecipientAction];
if (action)
[receipts setObject: action forKey: @"receiptNonRecipientAction"];
action = [_defaults userReceiptOutsideDomainAction];
if (action)
[receipts setObject: action forKey: @"receiptOutsideDomainAction"];
action = [_defaults userReceiptAnyAction];
if (action)
[receipts setObject: action forKey: @"receiptAnyAction"];
[mailAccount setObject: receipts forKey: @"receipts"];
[receipts release];
}
// 7. Mailboxes
mailboxes = [NSMutableDictionary new];
[self _migrateFolderSettings];
[mailboxes setObject: [_defaults draftsFolderName]
forKey: @"Drafts"];
[mailboxes setObject: [_defaults sentFolderName]
forKey: @"Sent"];
[mailboxes setObject: [_defaults trashFolderName]
forKey: @"Trash"];
[mailboxes setObject: [_defaults junkFolderName]
forKey: @"Junk"];
[mailAccount setObject: mailboxes forKey: @"specialMailboxes"];
[mailboxes release];
// 8. Delegates
delegates = [mailSettings objectForKey: @"DelegateTo"];
if (!delegates)
delegates = [NSArray array];
else
{
NSMutableArray *allDelegates;
SOGoUser *delegate;
allDelegates = [NSMutableArray array];
for (count = 0; count < [delegates count]; count++)
{
delegate = [SOGoUser userWithLogin: [delegates objectAtIndex: count]];
[allDelegates addObject: [NSDictionary dictionaryWithObjectsAndKeys: [delegates objectAtIndex: count], @"uid",
[delegate cn], @"cn",
[delegate systemEmail], @"c_email", nil]];
}
delegates = allDelegates;
}
[mailAccount setObject: delegates forKey: @"delegates"];
// 9. Security
if ([[_defaults mailCertificate] length])
{
security = [NSMutableDictionary new];
[security setObject: [NSNumber numberWithBool: YES] forKey: @"hasCertificate"];
if ([_defaults mailCertificateAlwaysSign])
[security setObject: [NSNumber numberWithBool: YES] forKey: @"alwaysSign"];
if ([_defaults mailCertificateAlwaysEncrypt])
[security setObject: [NSNumber numberWithBool: YES] forKey: @"alwaysEncrypt"];
[mailAccount setObject: security forKey: @"security"];
[security release];
}
[mailAccounts addObject: mailAccount];
[mailAccount release];
}
- (NSArray *) mailAccounts
{
return [self mailAccountsWithDelegatedIdentities: YES];
}
- (NSArray *) mailAccountsWithDelegatedIdentities: (BOOL) appendDeletegatedIdentities
{
NSArray *auxAccounts;
if (!mailAccounts)
{
mailAccounts = [NSMutableArray new];
[self _appendSystemMailAccountWithDelegatedIdentities: appendDeletegatedIdentities];
if ([[self domainDefaults] mailAuxiliaryUserAccountsEnabled])
{
auxAccounts = [[self userDefaults] auxiliaryMailAccounts];
if (auxAccounts)
[mailAccounts addObjectsFromArray: auxAccounts];
}
}
return mailAccounts;
}
- (NSDictionary *) accountWithName: (NSString *) accountName;
{
NSEnumerator *accounts;
NSDictionary *mailAccount, *currentAccount;
mailAccount = nil;
accounts = [[self mailAccounts] objectEnumerator];
while (!mailAccount
&& ((currentAccount = [accounts nextObject])))
if ([[currentAccount objectForKey: @"name"]
isEqualToString: accountName])
mailAccount = currentAccount;
return mailAccount;
}
- (NSArray *) allIdentities
{
NSArray *identities;
identities = [[self mailAccounts] objectsForKey: @"identities"
notFoundMarker: nil];
return [identities flattenedArray];
}
- (NSDictionary *) primaryIdentity
{
NSArray *identities;
NSDictionary *defaultIdentity, *defaultAccount;
defaultIdentity = [self defaultIdentity];
if (!defaultIdentity && [[self mailAccounts] count])
{