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.
|
|
|
|
*/
|
|
|
|
|
2013-02-20 16:50:58 +01:00
|
|
|
#import <Foundation/NSCharacterSet.h>
|
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>
|
2012-10-22 21:17:47 +02:00
|
|
|
#import <SOGo/NSString+Utilities.h>
|
2015-07-14 16:30:51 +02:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2012-01-29 20:40:44 +01:00
|
|
|
#import <Mailer/SOGoMailAccount.h>
|
|
|
|
#import <Mailer/SOGoMailAccounts.h>
|
|
|
|
|
2014-05-14 03:06:59 +02:00
|
|
|
#import <SOGo/GCSSpecialQueries+SOGoCacheObject.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
|
2013-11-08 17:02:50 +01:00
|
|
|
andTDBIndexing: (struct indexing_context *) indexing;
|
2012-01-29 20:40:44 +01:00
|
|
|
{
|
|
|
|
id userContext;
|
|
|
|
|
|
|
|
userContext = [contextsTable objectForKey: username];
|
2014-08-28 04:14:06 +02:00
|
|
|
if (!userContext)
|
2012-01-29 20:40:44 +01:00
|
|
|
{
|
|
|
|
userContext = [[self alloc] initWithUsername: username
|
2013-11-08 17:02:50 +01:00
|
|
|
andTDBIndexing: indexing];
|
2012-01-29 20:40:44 +01:00
|
|
|
[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;
|
|
|
|
}
|
|
|
|
|
2014-03-17 21:08:12 +01:00
|
|
|
- (NSString *) _readPasswordFile: (NSString *) path
|
2012-07-24 22:45:03 +02:00
|
|
|
{
|
2014-03-17 21:08:12 +01:00
|
|
|
NSString *password;
|
2012-07-24 22:45:03 +02:00
|
|
|
NSData *content;
|
2012-08-02 15:18:09 +02:00
|
|
|
|
2014-03-17 21:08:12 +01:00
|
|
|
password = nil;
|
2012-08-02 15:18:09 +02:00
|
|
|
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
|
2013-02-20 16:50:58 +01:00
|
|
|
encoding: NSUTF8StringEncoding];
|
2012-08-02 15:18:09 +02:00
|
|
|
[password autorelease];
|
2013-02-20 16:50:58 +01:00
|
|
|
password = [password stringByTrimmingCharactersInSet:
|
|
|
|
[NSCharacterSet characterSetWithCharactersInString: @"\r\n"]];
|
2012-07-24 22:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
2014-03-17 21:08:12 +01:00
|
|
|
- (NSString *) _readUserPassword: (NSString *) newUsername
|
|
|
|
{
|
|
|
|
NSString *password, *path;
|
|
|
|
|
|
|
|
path = [NSString stringWithFormat: SAMBA_PRIVATE_DIR
|
|
|
|
@"/mapistore/%@/password", newUsername];
|
|
|
|
|
|
|
|
password = [self _readPasswordFile: path];
|
|
|
|
if (password == nil)
|
|
|
|
{
|
|
|
|
// Try to get master password
|
|
|
|
path = [NSString stringWithFormat: SAMBA_PRIVATE_DIR @"/mapistore/master.password"];
|
|
|
|
password = [self _readPasswordFile: path];
|
|
|
|
}
|
|
|
|
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
2012-01-29 20:40:44 +01:00
|
|
|
- (id) initWithUsername: (NSString *) newUsername
|
2013-11-08 17:02:50 +01:00
|
|
|
andTDBIndexing: (struct indexing_context *) indexing
|
2012-01-29 20:40:44 +01:00
|
|
|
{
|
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;
|
2013-11-08 17:02:50 +01:00
|
|
|
if (indexing)
|
2012-01-29 20:40:44 +01:00
|
|
|
ASSIGN (mapping, [MAPIStoreMapping mappingForUsername: username
|
2013-11-08 17:02:50 +01:00
|
|
|
withIndexing: indexing]);
|
2012-01-29 20:40:44 +01:00
|
|
|
|
|
|
|
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];
|
2015-07-14 16:47:38 +02:00
|
|
|
// Activate the profile on initialization
|
|
|
|
[self activate];
|
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
|
|
|
|
2012-10-07 06:17:06 +02:00
|
|
|
[woContext 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
|
2015-07-14 17:44:17 +02:00
|
|
|
inContainer: nil];
|
2012-01-29 20:40:44 +01:00
|
|
|
[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];
|
2015-07-14 17:44:17 +02:00
|
|
|
[self activate];
|
|
|
|
[self userFolder]; // force lazy initialization
|
2012-01-29 20:40:44 +01:00
|
|
|
[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];
|
2015-07-14 17:44:17 +02:00
|
|
|
|
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];
|
2015-07-14 17:44:17 +02:00
|
|
|
hierarchy = [connection cachedHierarchyResultsForURL: [currentFolder imap4URL]];
|
2012-09-26 17:50:28 +02:00
|
|
|
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
|
2014-06-10 14:46:35 +02:00
|
|
|
parts in this url. We strip the '-' character in case we have
|
|
|
|
this in the domain part - like foo@bar-zot.com */
|
2014-05-14 14:56:35 +02:00
|
|
|
ocFSTableName = [NSString stringWithFormat: @"sogo_cache_folder_%@",
|
2015-04-14 13:08:21 +02:00
|
|
|
[[[user login] asCSSIdentifier]
|
|
|
|
stringByReplacingOccurrencesOfString: @"-"
|
|
|
|
withString: @"_"]];
|
2012-06-29 19:59:38 +02:00
|
|
|
[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];
|
2015-07-14 17:44:17 +02:00
|
|
|
|
2012-06-29 19:59:38 +02:00
|
|
|
/* FIXME: make use of [EOChannelAdaptor describeTableNames] instead */
|
|
|
|
tableName = [[folderTableURL path] lastPathComponent];
|
|
|
|
if ([channel evaluateExpressionX:
|
|
|
|
[NSString stringWithFormat: @"SELECT count(*) FROM %@",
|
|
|
|
tableName]])
|
|
|
|
{
|
|
|
|
queries = [channel specialQueries];
|
2014-05-14 15:56:10 +02:00
|
|
|
query = [queries createSOGoCacheGCSFolderTableWithName: tableName];
|
2012-06-29 19:59:38 +02:00
|
|
|
if ([channel evaluateExpressionX: query])
|
|
|
|
[NSException raise: @"MAPIStoreIOException"
|
|
|
|
format: @"could not create special table '%@'", tableName];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[channel cancelFetch];
|
|
|
|
|
|
|
|
|
2015-07-14 17:44:17 +02:00
|
|
|
[cm releaseChannel: channel];
|
2012-06-29 19:59:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SOGo context objects */
|
2012-01-29 20:40:44 +01:00
|
|
|
- (WOContext *) woContext
|
|
|
|
{
|
|
|
|
return woContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (MAPIStoreAuthenticator *) authenticator
|
|
|
|
{
|
|
|
|
return authenticator;
|
|
|
|
}
|
|
|
|
|
2015-07-14 16:30:51 +02:00
|
|
|
- (void) activate
|
|
|
|
{
|
|
|
|
[self activateWithUser: [self sogoUser]];
|
|
|
|
}
|
|
|
|
|
2012-01-29 20:40:44 +01:00
|
|
|
- (void) activateWithUser: (SOGoUser *) activeUser;
|
|
|
|
{
|
|
|
|
NSMutableDictionary *info;
|
|
|
|
|
|
|
|
[MAPIApp setUserContext: self];
|
|
|
|
[woContext setActiveUser: activeUser];
|
|
|
|
info = [[NSThread currentThread] threadDictionary];
|
|
|
|
[info setObject: woContext forKey: @"WOContext"];
|
|
|
|
}
|
|
|
|
|
2015-07-14 16:30:51 +02:00
|
|
|
- (void) deactivate
|
|
|
|
{
|
|
|
|
NSMutableDictionary *info;
|
|
|
|
|
|
|
|
if (self == [MAPIApp userContext])
|
|
|
|
[MAPIApp setUserContext: nil];
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"Error: Tried to deactivate an user context "
|
|
|
|
@"not enabled (%@ vs %@)",
|
|
|
|
[self username], [[MAPIApp userContext] username]];
|
|
|
|
|
|
|
|
info = [[NSThread currentThread] threadDictionary];
|
|
|
|
if (woContext == [info objectForKey: @"WOContext"])
|
|
|
|
[info removeObjectForKey: @"WOContext"];
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"Error: Tried to deactivate a WOContext "
|
|
|
|
@"not enabled (%@ vs %@)",
|
|
|
|
woContext, [info objectForKey: @"WOContext"]];
|
|
|
|
}
|
|
|
|
|
2012-01-29 20:40:44 +01:00
|
|
|
@end
|