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
|
/* MAPIStoreUserContext.m - this file is part of SOGo
*
* 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
* the Free Software Foundation; either version 3, or (at your option)
* 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.
*/
#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSMapTable.h>
#import <Foundation/NSPropertyList.h>
#import <Foundation/NSThread.h>
#import <Foundation/NSTimeZone.h>
#import <Foundation/NSURL.h>
#import <NGObjWeb/WOContext.h>
#import <NGObjWeb/WOContext+SoObjects.h>
#import <NGImap4/NGImap4Connection.h>
#import <GDLContentStore/GCSChannelManager.h>
#import <SOGo/SOGoDomainDefaults.h>
#import <SOGo/SOGoUser.h>
#import <SOGo/SOGoUserFolder.h>
#import <SOGo/NSString+Utilities.h>
#import <Mailer/SOGoMailAccount.h>
#import <Mailer/SOGoMailAccounts.h>
#import <SOGo/GCSSpecialQueries+SOGoCacheObject.h>
#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 indexing_context *) indexing;
{
id userContext;
userContext = [contextsTable objectForKey: username];
if (!userContext)
{
userContext = [[self alloc] initWithUsername: username
andTDBIndexing: indexing];
[userContext autorelease];
[contextsTable setObject: userContext forKey: username];
}
return userContext;
}
- (id) init
{
if ((self = [super init]))
{
username = nil;
sogoUser = nil;
userFolder = nil;
containersBag = [NSMutableArray new];
rootFolders = nil;
mapping = nil;
userDbTableExists = NO;
folderTableURL = nil;
authenticator = nil;
woContext = [WOContext contextWithRequest: nil];
[woContext retain];
}
return self;
}
- (NSString *) _readPasswordFile: (NSString *) path
{
NSString *password;
NSData *content;
password = nil;
content = [NSData dataWithContentsOfFile: path];
if (content)
{
password = [[NSString alloc] initWithData: content
encoding: NSUTF8StringEncoding];
[password autorelease];
password = [password stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString: @"\r\n"]];
}
return password;
}
- (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;
}
- (id) initWithUsername: (NSString *) newUsername
andTDBIndexing: (struct indexing_context *) indexing
{
NSString *userPassword;
if ((self = [self init]))
{
/* "username" will be retained by table */
username = newUsername;
if (indexing)
ASSIGN (mapping, [MAPIStoreMapping mappingForUsername: username
withIndexing: indexing]);
authenticator = [MAPIStoreAuthenticator new];
[authenticator setUsername: username];
/* TODO: very hackish (IMAP access) */
userPassword = [self _readUserPassword: newUsername];
if ([userPassword length] == 0)
userPassword = username;
[authenticator setPassword: userPassword];
}
return self;
}
- (void) dealloc
{
[userFolder release];
[containersBag release];
[rootFolders release];
[woContext release];
[authenticator release];
[mapping release];
[folderTableURL release];
[sogoUser release];
[contextsTable removeObjectForKey: username];
[super dealloc];
}
- (NSString *) username
{
return username;
}
- (SOGoUser *) sogoUser
{
if (!sogoUser)
ASSIGN (sogoUser, [SOGoUser userWithLogin: username]);
return sogoUser;
}
- (NSTimeZone *) timeZone
{
if (!timeZone)
{
SOGoUser *user;
user = [self sogoUser];
timeZone = [[user userDefaults] timeZone];
[timeZone retain];
}
return timeZone;
}
- (SOGoUserFolder *) userFolder
{
if (!userFolder)
{
userFolder = [SOGoUserFolder objectWithName: username
inContainer: MAPIApp];
[userFolder retain];
}
return userFolder;
}
- (NSDictionary *) rootFolders
{
SOGoMailAccounts *accountsFolder;
id currentFolder;
NGImap4Connection *connection;
NSDictionary *hierarchy;
NSArray *flags;
if (!rootFolders)
{
rootFolders = [NSMutableDictionary new];
[self userFolder];
[woContext setClientObject: userFolder];
/* 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 */
accountsFolder = [userFolder lookupName: @"Mail"
inContext: woContext
acquire: NO];
[containersBag addObject: accountsFolder];
[woContext setClientObject: accountsFolder];
currentFolder = [accountsFolder lookupName: @"0"
inContext: woContext
acquire: NO];
[rootFolders setObject: currentFolder
forKey: @"mail"];
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"];
}
return rootFolders;
}
- (BOOL) inboxHasNoInferiors
{
[self rootFolders];
return inboxHasNoInferiors;
}
- (MAPIStoreMapping *) mapping
{
return mapping;
}
/* 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. We strip the '-' character in case we have
this in the domain part - like foo@bar-zot.com */
ocFSTableName = [NSString stringWithFormat: @"sogo_cache_folder_%@",
[[[user loginInDomain] asCSSIdentifier]
stringByReplacingOccurrencesOfString: @"-"
withString: @"_"]];
[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 createSOGoCacheGCSFolderTableWithName: tableName];
if ([channel evaluateExpressionX: query])
[NSException raise: @"MAPIStoreIOException"
format: @"could not create special table '%@'", tableName];
}
else
[channel cancelFetch];
[cm releaseChannel: channel];
}
/* SOGo context objects */
- (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
|