#include "SOGoMailAccounts.h"
#include "common.h"
#include <NGObjWeb/SoObject+SoDAV.h>
+#include <SOGoLogic/AgenorUserManager.h>
@implementation SOGoMailAccounts
/* listing the available mailboxes */
- (NSArray *)toManyRelationshipKeys {
- // TODO: hardcoded, need to implement the Agenor Anais LDAP query?
- return [NSArray arrayWithObjects:
- @"agenortest@mail.opengroupware.org",
- nil];
+ static AgenorUserManager *um = nil;
+ NSString *uid, *server, *account;
+
+ if(!um)
+ um = [AgenorUserManager sharedUserManager];
+ uid = [[self container] davDisplayName];
+ server = [um getIMAPServerForUID:uid];
+ if(!server)
+ return nil;
+ account = [NSString stringWithFormat:@"%@@%@",
+ uid,
+ server];
+ return [NSArray arrayWithObject:account];
}
/* name lookup */
// $Id$
-#import "AgenorUserManager.h"
+#include "AgenorUserManager.h"
+#include <NGLdap/NGLdap.h>
+
+
+@interface AgenorUserManager (PrivateAPI)
+- (NGLdapConnection *)ldapConnection;
+@end
@implementation AgenorUserManager
+static BOOL debugOn = NO;
+static BOOL useLDAP = NO;
+static NSString *ldapHost = nil;
+static NSString *ldapBaseDN = nil;
+
++ (void)initialize {
+ static BOOL didInit = NO;
+ NSUserDefaults *ud;
+
+ if(didInit)
+ return;
+ didInit = YES;
+ ud = [NSUserDefaults standardUserDefaults];
+ debugOn = [ud boolForKey:@"SOGoUserManagerDebugEnabled"];
+ useLDAP = [ud boolForKey:@"SOGoUserManagerUsesLDAP"];
+ if(useLDAP) {
+ ldapHost = [[ud stringForKey:@"SOGoLDAPHost"] retain];
+ ldapBaseDN = [[ud stringForKey:@"SOGoLDAPBaseDN"] retain];
+ }
+}
+
+ (id)sharedUserManager {
static id mgr = nil;
if(mgr == nil) {
[super dealloc];
}
+- (NGLdapConnection *)ldapConnection {
+ static NGLdapConnection *ldapConnection = nil;
+ if(!ldapConnection) {
+ ldapConnection = [[NGLdapConnection alloc] initWithHostName:ldapHost];
+#if 0
+ [ldapConnection setUseCache:YES];
+#endif
+ }
+ return ldapConnection;
+}
- (NSString *)getUIDForEmail:(NSString *)_email {
NSRange r;
}
- (NSString *)getCNForUID:(NSString *)_uid {
-#warning !! IMPLEMENT ME!!
- NSString *s;
- NSRange r;
-
- s = _uid;
- if ([s length] < 10)
+ if(useLDAP) {
+ static NSArray *cnAttrs = nil;
+ NGLdapConnection *conn;
+ EOQualifier *q;
+ NSEnumerator *resultEnum;
+ NGLdapEntry *entry;
+ NGLdapAttribute *cnAttr;
+
+ if(!cnAttrs) {
+ cnAttrs = [[NSArray alloc] initWithObjects:@"cn", nil];
+ }
+ q = [EOQualifier qualifierWithQualifierFormat:@"uid = %@", _uid];
+
+ conn = [self ldapConnection];
+ resultEnum = [conn deepSearchAtBaseDN:ldapBaseDN
+ qualifier:q
+ attributes:cnAttrs];
+ entry = [resultEnum nextObject];
+ if(!entry) {
+ if(debugOn) {
+ NSLog(@"Didn't find LDAP entry for uid '%@'!", _uid);
+ }
+ return nil;
+ }
+ cnAttr = [entry attributeWithName:@"cn"];
+ if(!cnAttr && debugOn) {
+ NSLog(@"LDAP entry for uid '%@' has no common name?", _uid);
+ }
+ return [cnAttr stringValueAtIndex:0];
+ }
+ else {
+ NSString *s;
+ NSRange r;
+
+ s = _uid;
+ if ([s length] < 10)
return s;
-
- // TODO: algorithm might be inappropriate, depends on the actual UID
- r = [s rangeOfString:@"."];
- if (r.length == 0)
+
+ // TODO: algorithm might be inappropriate, depends on the actual UID
+ r = [s rangeOfString:@"."];
+ if (r.length == 0)
return s;
-
- return [s substringToIndex:r.location];
+
+ return [s substringToIndex:r.location];
+ }
+}
+
+- (NSString *)getIMAPServerForUID:(NSString *)_uid {
+ /*
+ First of all : for a particular user IMAP and SMTP are served on the same
+ host.
+
+ The name of the machine is determined by applying a regex on every values of
+ the mineqMelRoutage LDAP attribute.
+ The regex is : .*%.*@(.*\.melanie2\.i2$)
+ It extracts the substring that follows '@', ends with 'melanie2', on
+ adresses which have a '%' before the '@'
+
+ Example: helge.hesse%opengroupware.org@servername1.melanie2.i2
+ -> servername1.melanie2.i2
+
+ If only one server name is found by applying the regex on every value of the
+ attribute, then this name is the IMAP/SMTP server for that user.
+ Note that this is the case when we got a unique (well formed) value for the
+ attribute.
+ If the regex finds more than one servername when applied to the differents
+ values, then the IMAP/SMTP server name is to be found in the
+ mineqMelServeurPrincipal attribute of the user.
+ */
+ return @"mail.opengroupware.org";
}
@end