@interface AgenorUserManager (PrivateAPI)
- (NGLdapConnection *)ldapConnection;
+
+- (void)_cacheCN:(NSString *)_cn forUID:(NSString *)_uid;
+- (NSString *)_cachedCNForUID:(NSString *)_uid;
+- (void)_cacheServer:(NSString *)_server forUID:(NSString *)_uid;
+- (NSString *)_cachedServerForUID:(NSString *)_uid;
+
@end
return ldapConnection;
}
+- (void)_cacheCN:(NSString *)_cn forUID:(NSString *)_uid {
+}
+
+- (NSString *)_cachedCNForUID:(NSString *)_uid {
+ return nil;
+}
+
+- (void)_cacheServer:(NSString *)_server forUID:(NSString *)_uid {
+}
+
+- (NSString *)_cachedServerForUID:(NSString *)_uid {
+ return nil;
+}
+
+
- (NSString *)getUIDForEmail:(NSString *)_email {
NSRange r;
NSString *domain;
return nil;
r = [_email rangeOfString:@"@"];
- domain = [_email substringFromIndex:r.location + 1];
+ if(r.length == 0)
+ return nil;
+ domain = [_email substringFromIndex:NSMaxRange(r)];
if(![domain isEqualToString:@"equipement.gouv.fr"])
return _email;
return [_email substringToIndex:r.location];
NSEnumerator *resultEnum;
NGLdapEntry *entry;
NGLdapAttribute *cnAttr;
+ NSString *cn;
if(!cnAttrs) {
cnAttrs = [[NSArray alloc] initWithObjects:@"cn", nil];
}
+
+ if((cn = [self _cachedCNForUID:_uid]))
+ return cn;
+
q = [EOQualifier qualifierWithQualifierFormat:@"uid = %@", _uid];
conn = [self ldapConnection];
entry = [resultEnum nextObject];
if(!entry) {
if(debugOn) {
- NSLog(@"Didn't find LDAP entry for uid '%@'!", _uid);
+ NSLog(@"%s Didn't find LDAP entry for uid '%@'!",
+ __PRETTY_FUNCTION__,
+ _uid);
}
return nil;
}
cnAttr = [entry attributeWithName:@"cn"];
if(!cnAttr && debugOn) {
- NSLog(@"LDAP entry for uid '%@' has no common name?", _uid);
+ NSLog(@"%s LDAP entry for uid '%@' has no common name?",
+ __PRETTY_FUNCTION__,
+ _uid);
}
- return [cnAttr stringValueAtIndex:0];
+ cn = [cnAttr stringValueAtIndex:0];
+ [self _cacheCN:cn forUID:_uid];
+ return cn;
}
else {
NSString *s;
}
}
-- (NSString *)getIMAPServerForUID:(NSString *)_uid {
+- (NSString *)getIMAPAccountStringForUID:(NSString *)_uid {
+ NSString *server;
+
+ server = [self getServerForUID:_uid];
+ if(!server)
+ return nil;
+ return [NSString stringWithFormat:@"%@@%@", _uid, server];
+}
+
+- (NSString *)getServerForUID:(NSString *)_uid {
/*
First of all : for a particular user IMAP and SMTP are served on the same
host.
values, then the IMAP/SMTP server name is to be found in the
mineqMelServeurPrincipal attribute of the user.
*/
- return @"mail.opengroupware.org";
+ if(useLDAP) {
+ static NSArray *attrs = nil;
+ NGLdapConnection *conn;
+ EOQualifier *q;
+ NSEnumerator *resultEnum;
+ NGLdapEntry *entry;
+ NGLdapAttribute *attr;
+ NSMutableArray *serverCandidates;
+ NSString *server;
+
+ if(!attrs) {
+ attrs = [[NSArray alloc] initWithObjects:@"mineqMelRoutage",
+ @"mineqMelServeurPrincipal",
+ nil];
+ }
+
+ if((server = [self _cachedServerForUID:_uid]))
+ return server;
+
+ q = [EOQualifier qualifierWithQualifierFormat:@"uid = %@", _uid];
+
+ conn = [self ldapConnection];
+ resultEnum = [conn deepSearchAtBaseDN:ldapBaseDN
+ qualifier:q
+ attributes:attrs];
+ /* we just expect one entry, thus drop the rest */
+ entry = [resultEnum nextObject];
+ if(!entry) {
+ if(debugOn) {
+ NSLog(@"%s Didn't find LDAP entry for uid '%@'!",
+ __PRETTY_FUNCTION__,
+ _uid);
+ }
+ return nil;
+ }
+ attr = [entry attributeWithName:@"mineqMelRoutage"];
+ if(attr) {
+ unsigned i, count;
+
+ count = [attr count];
+ serverCandidates = [NSMutableArray arrayWithCapacity:count];
+ for(i = 0; i < count; i++) {
+ NSRange r;
+ NSString *route;
+
+ route = [attr stringValueAtIndex:i];
+ r = [route rangeOfString:@".melanie2.i2" options:NSBackwardsSearch];
+ if(r.length > 0) {
+ unsigned length;
+
+ /* be clever */
+ length = [route length];
+ r = NSMakeRange(0, length - r.length);
+ r = [route rangeOfString:@"@" options:NSBackwardsSearch range:r];
+ if(r.length > 0) {
+ unsigned start;
+ NSRange serverNameRange;
+
+ start = NSMaxRange(r);
+ serverNameRange = NSMakeRange(start, length - start);
+ r = NSMakeRange(0, length - start);
+ r = [route rangeOfString:@"%" options:NSBackwardsSearch range:r];
+ if(r.length > 0) {
+ NSString *serverName;
+
+ serverName = [route substringWithRange:serverNameRange];
+ [serverCandidates addObject:serverName];
+ }
+ }
+ }
+ }
+ }
+ else {
+ if(debugOn) {
+ NSLog(@"%s LDAP entry for uid '%@' has no mineqMelRoutage entry?",
+ __PRETTY_FUNCTION__,
+ _uid);
+ }
+ serverCandidates = nil;
+ }
+ if([serverCandidates count] == 1) {
+ server = [serverCandidates objectAtIndex:0];
+ }
+
+ /* last resort */
+ if(!server) {
+ attr = [entry attributeWithName:@"mineqMelServeurPrincipal"];
+ if([attr count] > 0)
+ server = [attr stringValueAtIndex:0];
+ }
+
+ if(!server) {
+ if(debugOn) {
+ NSLog(@"%s no chance of getting at server info for user '%@', "
+ @"tried everything. Sorry.",
+ __PRETTY_FUNCTION__,
+ _uid);
+ }
+ return nil;
+ }
+
+ [self _cacheServer:server forUID:_uid];
+ return server;
+ }
+ else {
+ return @"mail.opengroupware.org";
+ }
}
@end