2012-01-30 21:17:21 +01:00
|
|
|
/* MAPIStoreUserContext.m - this file is part of SOGo
|
2012-01-29 20:40:44 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Inverse inc
|
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2012-06-29 19:59:38 +02:00
|
|
|
* the Free Software Foundation; either version 3, or (at your option)
|
2012-01-29 20:40:44 +01:00
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2012-07-24 22:45:03 +02:00
|
|
|
#import <Foundation/NSData.h>
|
2012-01-30 21:17:21 +01:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2012-01-29 20:40:44 +01:00
|
|
|
#import <Foundation/NSMapTable.h>
|
2012-07-24 22:45:03 +02:00
|
|
|
#import <Foundation/NSPropertyList.h>
|
2012-01-29 20:40:44 +01:00
|
|
|
#import <Foundation/NSThread.h>
|
2012-08-09 21:42:38 +02:00
|
|
|
#import <Foundation/NSTimeZone.h>
|
2012-06-29 19:59:38 +02:00
|
|
|
#import <Foundation/NSURL.h>
|
2012-01-29 20:40:44 +01:00
|
|
|
|
|
|
|
#import <NGObjWeb/WOContext.h>
|
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
|
|
|
|
|
|
|
#import <NGImap4/NGImap4Connection.h>
|
|
|
|
|
2012-06-29 19:59:38 +02:00
|
|
|
#import <GDLContentStore/GCSChannelManager.h>
|
|
|
|
#import <SOGo/SOGoDomainDefaults.h>
|
2012-01-29 20:40:44 +01:00
|
|
|
#import <SOGo/SOGoUser.h>
|
|
|
|
#import <SOGo/SOGoUserFolder.h>
|
|
|
|
#import <Mailer/SOGoMailAccount.h>
|
|
|
|
#import <Mailer/SOGoMailAccounts.h>
|
|
|
|
|
2012-06-29 19:59:38 +02:00
|
|
|
#import "GCSSpecialQueries+OpenChange.h"
|
2012-01-29 20:40:44 +01:00
|
|
|
#import "MAPIApplication.h"
|
|
|
|
#import "MAPIStoreAuthenticator.h"
|
|
|
|
#import "MAPIStoreMapping.h"
|
|
|
|
|
|
|
|
#import "MAPIStoreUserContext.h"
|
|
|
|
|
|
|
|
static NSMapTable *contextsTable = nil;
|
|
|
|
|
|
|
|
@implementation MAPIStoreUserContext
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
contextsTable = [NSMapTable mapTableWithStrongToWeakObjects];
|
|
|
|
[contextsTable retain];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) userContextWithUsername: (NSString *) username
|
|
|
|
andTDBIndexing: (struct tdb_wrap *) indexingTdb;
|
|
|
|
{
|
|
|
|
id userContext;
|
|
|
|
|
|
|
|
userContext = [contextsTable objectForKey: username];
|
|
|
|
if (!userContext)
|
|
|
|
{
|
|
|
|
userContext = [[self alloc] initWithUsername: username
|
|
|
|
andTDBIndexing: indexingTdb];
|
|
|
|
[userContext autorelease];
|
2012-02-02 02:50:45 +01:00
|
|
|
[contextsTable setObject: userContext forKey: username];
|
2012-01-29 20:40:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return userContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
username = nil;
|
|
|
|
sogoUser = nil;
|
|
|
|
|
|
|
|
userFolder = nil;
|
|
|
|
containersBag = [NSMutableArray new];
|
2012-01-30 21:17:21 +01:00
|
|
|
rootFolders = nil;
|
2012-01-29 20:40:44 +01:00
|
|
|
|
|
|
|
mapping = nil;
|
|
|
|
|
2012-06-29 19:59:38 +02:00
|
|
|
userDbTableExists = NO;
|
|
|
|
folderTableURL = nil;
|
|
|
|
|
2012-01-29 20:40:44 +01:00
|
|
|
authenticator = nil;
|
|
|
|
woContext = [WOContext contextWithRequest: nil];
|
|
|
|
[woContext retain];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2012-07-24 22:45:03 +02:00
|
|
|
- (NSString *) _readUserPassword: (NSString *) newUsername
|
|
|
|
{
|
2012-08-02 15:18:09 +02:00
|
|
|
NSString *password, *path;
|
2012-07-24 22:45:03 +02:00
|
|
|
NSData *content;
|
2012-08-02 15:18:09 +02:00
|
|
|
|
2012-07-24 22:45:03 +02:00
|
|
|
password = nil;
|
2012-08-02 15:18:09 +02:00
|
|
|
|
|
|
|
path = [NSString stringWithFormat: SAMBA_PRIVATE_DIR
|
|
|
|
@"/mapistore/%@/password", newUsername];
|
|
|
|
|
|
|
|
content = [NSData dataWithContentsOfFile: path];
|
2012-07-24 22:45:03 +02:00
|
|
|
|
|
|
|
if (content)
|
|
|
|
{
|
2012-08-02 15:18:09 +02:00
|
|
|
password = [[NSString alloc] initWithData: content
|
|
|
|
encoding: NSUTF8StringEncoding];
|
|
|
|
[password autorelease];
|
2012-07-24 22:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
2012-01-29 20:40:44 +01:00
|
|
|
- (id) initWithUsername: (NSString *) newUsername
|
|
|
|
andTDBIndexing: (struct tdb_wrap *) indexingTdb
|
|
|
|
{
|
2012-07-24 22:45:03 +02:00
|
|
|
NSString *userPassword;
|
|
|
|
|
2012-01-29 20:40:44 +01:00
|
|
|
if ((self = [self init]))
|
|
|
|
{
|
|
|
|
/* "username" will be retained by table */
|
|
|
|
username = newUsername;
|
|
|
|
if (indexingTdb)
|
|
|
|
ASSIGN (mapping, [MAPIStoreMapping mappingForUsername: username
|
|
|
|
withIndexing: indexingTdb]);
|
|
|
|
|
|
|
|
authenticator = [MAPIStoreAuthenticator new];
|
|
|
|
[authenticator setUsername: username];
|
|
|
|
/* TODO: very hackish (IMAP access) */
|
2012-07-24 22:45:03 +02:00
|
|
|
userPassword = [self _readUserPassword: newUsername];
|
|
|
|
if ([userPassword length] == 0)
|
|
|
|
userPassword = username;
|
|
|
|
[authenticator setPassword: userPassword];
|
2012-01-29 20:40:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[userFolder release];
|
|
|
|
[containersBag release];
|
2012-01-30 21:17:21 +01:00
|
|
|
[rootFolders release];
|
2012-01-29 20:40:44 +01:00
|
|
|
|
|
|
|
[authenticator release];
|
|
|
|
[mapping release];
|
|
|
|
|
2012-06-29 19:59:38 +02:00
|
|
|
[folderTableURL release];
|
|
|
|
|
2012-01-29 20:40:44 +01:00
|
|
|
[sogoUser release];
|
|
|
|
|
2012-01-30 21:17:21 +01:00
|
|
|
[contextsTable removeObjectForKey: username];
|
|
|
|
|
2012-01-29 20:40:44 +01:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) username
|
|
|
|
{
|
|
|
|
return username;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (SOGoUser *) sogoUser
|
|
|
|
{
|
|
|
|
if (!sogoUser)
|
|
|
|
ASSIGN (sogoUser, [SOGoUser userWithLogin: username]);
|
|
|
|
|
|
|
|
return sogoUser;
|
|
|
|
}
|
|
|
|
|
2012-03-30 17:33:47 +02:00
|
|
|
- (NSTimeZone *) timeZone
|
|
|
|
{
|
|
|
|
if (!timeZone)
|
|
|
|
{
|
|
|
|
SOGoUser *user;
|
|
|
|
|
|
|
|
user = [self sogoUser];
|
|
|
|
timeZone = [[user userDefaults] timeZone];
|
|
|
|
[timeZone retain];
|
|
|
|
}
|
|
|
|
|
|
|
|
return timeZone;
|
|
|
|
}
|
|
|
|
|
2012-01-29 20:40:44 +01:00
|
|
|
- (SOGoUserFolder *) userFolder
|
|
|
|
{
|
|
|
|
if (!userFolder)
|
|
|
|
{
|
|
|
|
userFolder = [SOGoUserFolder objectWithName: username
|
|
|
|
inContainer: MAPIApp];
|
|
|
|
[userFolder retain];
|
|
|
|
}
|
|
|
|
|
|
|
|
return userFolder;
|
|
|
|
}
|
|
|
|
|
2012-01-30 21:17:21 +01:00
|
|
|
- (NSDictionary *) rootFolders
|
2012-01-29 20:40:44 +01:00
|
|
|
{
|
|
|
|
SOGoMailAccounts *accountsFolder;
|
2012-01-30 21:17:21 +01:00
|
|
|
id currentFolder;
|
2012-09-26 17:50:28 +02:00
|
|
|
NGImap4Connection *connection;
|
|
|
|
NSDictionary *hierarchy;
|
|
|
|
NSArray *flags;
|
2012-01-29 20:40:44 +01:00
|
|
|
|
2012-01-30 21:17:21 +01:00
|
|
|
if (!rootFolders)
|
2012-01-29 20:40:44 +01:00
|
|
|
{
|
2012-01-30 21:17:21 +01:00
|
|
|
rootFolders = [NSMutableDictionary new];
|
2012-01-29 20:40:44 +01:00
|
|
|
[self userFolder];
|
|
|
|
[woContext setClientObject: userFolder];
|
|
|
|
|
2012-01-30 21:17:21 +01:00
|
|
|
/* Calendar */
|
|
|
|
currentFolder = [userFolder lookupName: @"Calendar"
|
|
|
|
inContext: woContext
|
|
|
|
acquire: NO];
|
|
|
|
[rootFolders setObject: currentFolder
|
|
|
|
forKey: @"calendar"];
|
|
|
|
[rootFolders setObject: currentFolder
|
|
|
|
forKey: @"tasks"];
|
|
|
|
|
|
|
|
/* Contacts */
|
|
|
|
currentFolder = [userFolder lookupName: @"Contacts"
|
|
|
|
inContext: woContext
|
|
|
|
acquire: NO];
|
|
|
|
[rootFolders setObject: currentFolder
|
|
|
|
forKey: @"contacts"];
|
|
|
|
|
|
|
|
/* Mail */
|
2012-01-29 20:40:44 +01:00
|
|
|
accountsFolder = [userFolder lookupName: @"Mail"
|
|
|
|
inContext: woContext
|
|
|
|
acquire: NO];
|
|
|
|
[containersBag addObject: accountsFolder];
|
|
|
|
[woContext setClientObject: accountsFolder];
|
2012-01-30 21:17:21 +01:00
|
|
|
currentFolder = [accountsFolder lookupName: @"0"
|
2012-09-26 17:50:28 +02:00
|
|
|
inContext: woContext
|
|
|
|
acquire: NO];
|
|
|
|
|
2012-01-30 21:17:21 +01:00
|
|
|
[rootFolders setObject: currentFolder
|
|
|
|
forKey: @"mail"];
|
2012-09-26 17:50:28 +02:00
|
|
|
connection = [currentFolder imap4Connection];
|
|
|
|
[connection enableExtensions: [NSArray arrayWithObject: @"QRESYNC"]];
|
|
|
|
|
|
|
|
/* ensure the folder cache is filled */
|
|
|
|
[currentFolder toManyRelationshipKeysWithNamespaces: YES];
|
|
|
|
hierarchy = [connection
|
|
|
|
cachedHierarchyResultsForURL: [currentFolder imap4URL]];
|
|
|
|
flags = [[hierarchy objectForKey: @"list"] objectForKey: @"/INBOX"];
|
|
|
|
inboxHasNoInferiors = [flags containsObject: @"noinferiors"];
|
2012-01-29 20:40:44 +01:00
|
|
|
}
|
|
|
|
|
2012-01-30 21:17:21 +01:00
|
|
|
return rootFolders;
|
2012-01-29 20:40:44 +01:00
|
|
|
}
|
|
|
|
|
2012-09-26 17:50:28 +02:00
|
|
|
- (BOOL) inboxHasNoInferiors
|
|
|
|
{
|
|
|
|
[self rootFolders];
|
|
|
|
|
|
|
|
return inboxHasNoInferiors;
|
|
|
|
}
|
|
|
|
|
2012-01-29 20:40:44 +01:00
|
|
|
- (MAPIStoreMapping *) mapping
|
|
|
|
{
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
2012-06-29 19:59:38 +02:00
|
|
|
|
|
|
|
/* OpenChange db table */
|
|
|
|
|
|
|
|
- (NSURL *) folderTableURL
|
|
|
|
{
|
|
|
|
NSString *urlString, *ocFSTableName;
|
|
|
|
NSMutableArray *parts;
|
|
|
|
SOGoUser *user;
|
|
|
|
|
|
|
|
if (!folderTableURL)
|
|
|
|
{
|
|
|
|
user = [self sogoUser];
|
|
|
|
urlString = [[user domainDefaults] folderInfoURL];
|
|
|
|
parts = [[urlString componentsSeparatedByString: @"/"]
|
|
|
|
mutableCopy];
|
|
|
|
[parts autorelease];
|
|
|
|
if ([parts count] == 5)
|
|
|
|
{
|
|
|
|
/* If "OCSFolderInfoURL" is properly configured, we must have 5
|
|
|
|
parts in this url. */
|
|
|
|
ocFSTableName = [NSString stringWithFormat: @"socfs_%@", username];
|
|
|
|
[parts replaceObjectAtIndex: 4 withObject: ocFSTableName];
|
|
|
|
folderTableURL
|
|
|
|
= [NSURL URLWithString: [parts componentsJoinedByString: @"/"]];
|
|
|
|
[folderTableURL retain];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[NSException raise: @"MAPIStoreIOException"
|
|
|
|
format: @"'OCSFolderInfoURL' is not set"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return folderTableURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) ensureFolderTableExists
|
|
|
|
{
|
|
|
|
GCSChannelManager *cm;
|
|
|
|
EOAdaptorChannel *channel;
|
|
|
|
NSString *tableName, *query;
|
|
|
|
GCSSpecialQueries *queries;
|
|
|
|
|
|
|
|
[self folderTableURL];
|
|
|
|
|
|
|
|
cm = [GCSChannelManager defaultChannelManager];
|
|
|
|
channel = [cm acquireOpenChannelForURL: folderTableURL];
|
|
|
|
|
|
|
|
/* FIXME: make use of [EOChannelAdaptor describeTableNames] instead */
|
|
|
|
tableName = [[folderTableURL path] lastPathComponent];
|
|
|
|
if ([channel evaluateExpressionX:
|
|
|
|
[NSString stringWithFormat: @"SELECT count(*) FROM %@",
|
|
|
|
tableName]])
|
|
|
|
{
|
|
|
|
queries = [channel specialQueries];
|
|
|
|
query = [queries createOpenChangeFSTableWithName: tableName];
|
|
|
|
if ([channel evaluateExpressionX: query])
|
|
|
|
[NSException raise: @"MAPIStoreIOException"
|
|
|
|
format: @"could not create special table '%@'", tableName];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[channel cancelFetch];
|
|
|
|
|
|
|
|
|
|
|
|
[cm releaseChannel: channel];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SOGo context objects */
|
2012-01-29 20:40:44 +01:00
|
|
|
- (WOContext *) woContext
|
|
|
|
{
|
|
|
|
return woContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (MAPIStoreAuthenticator *) authenticator
|
|
|
|
{
|
|
|
|
return authenticator;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) activateWithUser: (SOGoUser *) activeUser;
|
|
|
|
{
|
|
|
|
NSMutableDictionary *info;
|
|
|
|
|
|
|
|
[MAPIApp setUserContext: self];
|
|
|
|
[woContext setActiveUser: activeUser];
|
|
|
|
info = [[NSThread currentThread] threadDictionary];
|
|
|
|
[info setObject: woContext forKey: @"WOContext"];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|