2006-06-15 21:34:10 +02:00
|
|
|
/*
|
2014-03-28 19:18:48 +01:00
|
|
|
Copyright (C) 2007-2014 Inverse inc.
|
2006-06-15 21:34:10 +02:00
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
|
|
|
|
2014-03-28 19:18:48 +01:00
|
|
|
This file is part of SOGo.
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2014-03-28 19:18:48 +01:00
|
|
|
SOGo is free software; you can redistribute it and/or modify it under
|
2006-06-15 21:34:10 +02:00
|
|
|
the terms of the GNU Lesser General Public License as published by the
|
|
|
|
Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
later version.
|
|
|
|
|
2014-03-28 19:18:48 +01:00
|
|
|
SOGo is distributed in the hope that it will be useful, but WITHOUT ANY
|
2006-06-15 21:34:10 +02:00
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with OGo; see the file COPYING. If not, write to the
|
|
|
|
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2010-01-15 00:19:19 +01:00
|
|
|
#import <Foundation/NSArray.h>
|
2008-02-08 16:35:11 +01:00
|
|
|
#import <Foundation/NSCharacterSet.h>
|
|
|
|
#import <Foundation/NSString.h>
|
2009-09-29 22:13:36 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-02-08 16:35:11 +01:00
|
|
|
|
2007-07-10 20:22:59 +02:00
|
|
|
#import <NGObjWeb/SoObject+SoDAV.h>
|
2012-10-22 16:09:13 +02:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
2007-07-10 20:22:59 +02:00
|
|
|
#import <NGExtensions/NSNull+misc.h>
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
#import <NGExtensions/NSString+misc.h>
|
|
|
|
#import <NGExtensions/NSURL+misc.h>
|
2012-10-22 16:09:13 +02:00
|
|
|
#import <NGImap4/NGImap4Connection.h>
|
2015-11-03 20:59:40 +01:00
|
|
|
#import <NGImap4/NGImap4Client.h>
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2010-10-13 15:21:16 +02:00
|
|
|
#import <SOGo/SOGoCache.h>
|
2012-10-22 16:09:13 +02:00
|
|
|
#import <SOGo/SOGoUser.h>
|
2015-11-03 20:59:40 +01:00
|
|
|
#import <SOGo/WORequest+SOGo.h>
|
2010-10-13 15:21:16 +02:00
|
|
|
|
2010-08-06 16:25:59 +02:00
|
|
|
#import "SOGoMailAccount.h"
|
2007-07-10 20:22:59 +02:00
|
|
|
#import "SOGoMailManager.h"
|
|
|
|
|
|
|
|
#import "SOGoMailBaseObject.h"
|
|
|
|
|
|
|
|
@implementation SOGoMailBaseObject
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2016-01-26 21:04:22 +01:00
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
|
|
|
|
// With this to YES, imap4Connection will raise exception if a working
|
|
|
|
// connection cannot be provided
|
|
|
|
imap4ExceptionsEnabled = [ud boolForKey:@"SoIMAP4ExceptionsEnabled"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-07-10 20:22:59 +02:00
|
|
|
- (id) initWithImap4URL: (NSURL *) _url
|
|
|
|
inContainer: (id) _container
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSString *n;
|
2016-01-26 21:04:22 +01:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
n = [[_url path] lastPathComponent];
|
2007-07-10 20:22:59 +02:00
|
|
|
if ((self = [self initWithName: n inContainer:_container]))
|
|
|
|
{
|
|
|
|
imap4URL = [_url retain];
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-07-10 20:22:59 +02:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[imap4URL release];
|
2008-09-02 04:57:12 +02:00
|
|
|
[imap4 release];
|
2006-06-15 21:34:10 +02:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2008-03-04 21:57:36 +01:00
|
|
|
- (BOOL) isFolderish
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
/* hierarchy */
|
|
|
|
|
2007-07-10 20:22:59 +02:00
|
|
|
- (SOGoMailAccount *) mailAccountFolder
|
|
|
|
{
|
|
|
|
SOGoMailAccount *folder;
|
|
|
|
|
2010-10-13 15:21:16 +02:00
|
|
|
if ([container respondsToSelector: _cmd])
|
2007-07-10 20:22:59 +02:00
|
|
|
folder = [container mailAccountFolder];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self warnWithFormat: @"weird container of mailfolder: %@",
|
|
|
|
container];
|
|
|
|
folder = nil;
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-07-10 20:22:59 +02:00
|
|
|
return folder;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-07-10 20:22:59 +02:00
|
|
|
- (SOGoMailAccounts *) mailAccountsFolder
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
id o;
|
2007-07-10 20:22:59 +02:00
|
|
|
SOGoMailAccounts *folder;
|
|
|
|
Class folderClass;
|
|
|
|
|
|
|
|
folder = nil;
|
|
|
|
|
|
|
|
folderClass = NSClassFromString (@"SOGoMailAccounts");
|
|
|
|
o = container;
|
|
|
|
while (!folder && [o isNotNull])
|
|
|
|
if ([o isKindOfClass: folderClass])
|
|
|
|
folder = o;
|
|
|
|
else
|
|
|
|
o = [o container];
|
|
|
|
|
|
|
|
return o;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-15 22:19:39 +02:00
|
|
|
- (BOOL) isInDraftsFolder
|
|
|
|
{
|
|
|
|
return [container isInDraftsFolder];
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
/* IMAP4 */
|
|
|
|
|
2007-07-10 20:22:59 +02:00
|
|
|
- (NGImap4ConnectionManager *) mailManager
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [NGImap4ConnectionManager defaultConnectionManager];
|
|
|
|
}
|
2007-07-10 20:22:59 +02:00
|
|
|
|
2010-10-13 15:21:16 +02:00
|
|
|
- (NGImap4Connection *) _createIMAP4Connection
|
2007-07-10 20:22:59 +02:00
|
|
|
{
|
2010-01-28 22:42:03 +01:00
|
|
|
NGImap4ConnectionManager *manager;
|
2010-10-13 15:21:16 +02:00
|
|
|
NGImap4Connection *newConnection;
|
2016-12-05 20:51:33 +01:00
|
|
|
NSString *password;
|
|
|
|
NSHost *host;
|
2010-01-28 22:42:03 +01:00
|
|
|
|
2010-10-13 15:21:16 +02:00
|
|
|
[self imap4URL];
|
2016-12-05 20:51:33 +01:00
|
|
|
|
|
|
|
// We first check if we're trying to establish an IMAP connection to localhost
|
|
|
|
// for an account number greater than 0 (default account). We prevent that
|
|
|
|
// for security reasons if admins use an IMAP trust.
|
|
|
|
host = [NSHost hostWithName: [[self imap4URL] host]];
|
|
|
|
if (![[[self mailAccountFolder] nameInContainer] isEqualToString: @"0"] &&
|
|
|
|
[[host address] isEqualToString: @"127.0.0.1"])
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"Trying to use localhost for additional IMAP account - aborting."];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2010-10-13 15:21:16 +02:00
|
|
|
manager = [self mailManager];
|
|
|
|
password = [self imap4PasswordRenewed: NO];
|
|
|
|
if (password)
|
2007-07-10 20:22:59 +02:00
|
|
|
{
|
2010-10-13 15:21:16 +02:00
|
|
|
newConnection = [manager connectionForURL: imap4URL
|
|
|
|
password: password];
|
|
|
|
if (!newConnection)
|
2010-01-28 22:42:03 +01:00
|
|
|
{
|
2010-10-13 15:21:16 +02:00
|
|
|
[self logWithFormat: @"renewing imap4 password"];
|
|
|
|
password = [self imap4PasswordRenewed: YES];
|
|
|
|
if (password)
|
|
|
|
newConnection = [manager connectionForURL: imap4URL
|
|
|
|
password: password];
|
2010-01-28 22:42:03 +01:00
|
|
|
}
|
2010-10-13 15:21:16 +02:00
|
|
|
}
|
2010-11-01 15:05:11 +01:00
|
|
|
else
|
|
|
|
newConnection = nil;
|
|
|
|
|
2010-10-13 15:21:16 +02:00
|
|
|
if (!newConnection)
|
|
|
|
{
|
|
|
|
newConnection = (NGImap4Connection *) [NSNull null];
|
2016-01-26 21:04:22 +01:00
|
|
|
if (imap4ExceptionsEnabled)
|
|
|
|
[NSException raise: @"IOException" format: @"IMAP connection failed"];
|
|
|
|
else
|
|
|
|
[self errorWithFormat:@"Could not connect IMAP4"];
|
2010-10-13 15:21:16 +02:00
|
|
|
}
|
2013-07-18 17:46:49 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// If the server has the ID capability (RFC 2971), we set the x-originating-ip
|
|
|
|
// accordingly for the IMAP connection.
|
|
|
|
NSString *remoteHost;
|
|
|
|
|
|
|
|
remoteHost = [[context request] headerForKey: @"x-webobjects-remote-host"];
|
|
|
|
|
|
|
|
if (remoteHost)
|
|
|
|
{
|
|
|
|
if ([[[[newConnection client] capability] objectForKey: @"capability"] containsObject: @"id"])
|
|
|
|
{
|
|
|
|
[[newConnection client] processCommand: [NSString stringWithFormat: @"ID (\"x-originating-ip\" \"%@\")", remoteHost]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-13 15:21:16 +02:00
|
|
|
|
|
|
|
return newConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NGImap4Connection *) imap4Connection
|
|
|
|
{
|
2011-09-15 02:59:36 +02:00
|
|
|
NSString *cacheKey, *login;
|
|
|
|
|
2010-10-13 15:21:16 +02:00
|
|
|
SOGoCache *sogoCache;
|
2011-09-15 02:59:36 +02:00
|
|
|
|
2010-10-13 15:21:16 +02:00
|
|
|
|
|
|
|
if (!imap4)
|
|
|
|
{
|
|
|
|
sogoCache = [SOGoCache sharedCache];
|
2011-09-14 20:33:44 +02:00
|
|
|
// The cacheKey *MUST* be prefixed by the username here as
|
|
|
|
// the cache is shared across OpenChange users and not necessarily
|
|
|
|
// flushed between requests. This could lead us to using the wrong
|
|
|
|
// IMAP connection.
|
2011-09-15 02:59:36 +02:00
|
|
|
//
|
|
|
|
// We also have a HACK in case self's context doesn't exist, we
|
|
|
|
// use the container's one.
|
|
|
|
//
|
|
|
|
login = [[[self context] activeUser] login];
|
|
|
|
|
|
|
|
if (!login)
|
|
|
|
login = [[[[self container] context] activeUser] login];
|
|
|
|
|
2011-09-14 20:33:44 +02:00
|
|
|
cacheKey = [NSString stringWithFormat: @"%@+%@",
|
2011-09-15 02:59:36 +02:00
|
|
|
login,
|
2011-09-14 20:33:44 +02:00
|
|
|
[[self mailAccountFolder] nameInContainer]];
|
2010-10-13 15:21:16 +02:00
|
|
|
imap4 = [sogoCache imap4ConnectionForKey: cacheKey];
|
2010-01-28 22:42:03 +01:00
|
|
|
if (!imap4)
|
|
|
|
{
|
2010-10-13 15:21:16 +02:00
|
|
|
imap4 = [self _createIMAP4Connection];
|
2016-12-05 20:51:33 +01:00
|
|
|
[sogoCache registerIMAP4Connection: imap4
|
|
|
|
forKey: cacheKey];
|
2010-01-28 22:42:03 +01:00
|
|
|
}
|
|
|
|
[imap4 retain];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-07-10 20:22:59 +02:00
|
|
|
|
2016-01-26 21:04:22 +01:00
|
|
|
// Connection broken, try to reconnect
|
|
|
|
if (![imap4 isKindOfClass: [NSNull class]] && ![[[imap4 client] isConnected] boolValue])
|
|
|
|
{
|
|
|
|
[self warnWithFormat: @"IMAP connection is broken, trying to reconnect..."];
|
|
|
|
[[imap4 client] reconnect];
|
|
|
|
|
|
|
|
// Still broken, give up
|
|
|
|
if (![[[imap4 client] isConnected] boolValue])
|
|
|
|
{
|
|
|
|
if (imap4ExceptionsEnabled)
|
|
|
|
[NSException raise: @"IOException" format: @"IMAP connection failed"];
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"Could not get a valid IMAP connection"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-28 22:42:03 +01:00
|
|
|
return [imap4 isKindOfClass: [NSNull class]] ? nil : imap4;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-07-10 20:22:59 +02:00
|
|
|
- (NSString *) relativeImap4Name
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return nil;
|
|
|
|
}
|
2007-07-10 20:22:59 +02:00
|
|
|
|
|
|
|
- (NSURL *) baseImap4URL
|
|
|
|
{
|
|
|
|
NSURL *url;
|
|
|
|
|
2008-02-07 21:21:13 +01:00
|
|
|
if ([container respondsToSelector: @selector(imap4URL)])
|
2007-07-10 20:22:59 +02:00
|
|
|
url = [container imap4URL];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self warnWithFormat:@"container does not implement -imap4URL!"];
|
|
|
|
url = nil;
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-07-10 20:22:59 +02:00
|
|
|
return url;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-07-10 20:22:59 +02:00
|
|
|
|
2007-07-10 21:23:39 +02:00
|
|
|
- (NSMutableString *) imap4URLString
|
|
|
|
{
|
|
|
|
NSMutableString *urlString;
|
2007-08-01 20:46:43 +02:00
|
|
|
NSString *imap4Name;
|
2007-07-10 21:23:39 +02:00
|
|
|
|
|
|
|
urlString = [container imap4URLString];
|
2007-08-01 20:46:43 +02:00
|
|
|
imap4Name = [[self relativeImap4Name] stringByEscapingURL];
|
2007-08-17 04:24:10 +02:00
|
|
|
[urlString appendFormat: @"%@", imap4Name];
|
2007-07-10 21:23:39 +02:00
|
|
|
|
|
|
|
return urlString;
|
|
|
|
}
|
|
|
|
|
2007-07-10 20:22:59 +02:00
|
|
|
- (NSURL *) imap4URL
|
|
|
|
{
|
2010-08-06 16:25:59 +02:00
|
|
|
SOGoMailAccount *account;
|
|
|
|
NSString *urlString;
|
|
|
|
|
2007-07-10 21:23:39 +02:00
|
|
|
/* this could probably be handled better from NSURL but it's buggy in
|
|
|
|
GNUstep */
|
2007-07-10 20:22:59 +02:00
|
|
|
if (!imap4URL)
|
2010-08-06 16:25:59 +02:00
|
|
|
{
|
|
|
|
account = [self mailAccountFolder];
|
|
|
|
if ([[account encryption] isEqualToString: @"tls"])
|
|
|
|
urlString = [NSString stringWithFormat: @"%@?tls=YES",
|
|
|
|
[self imap4URLString]];
|
|
|
|
else
|
2010-09-02 18:47:06 +02:00
|
|
|
urlString = [self imap4URLString];
|
2010-08-06 16:25:59 +02:00
|
|
|
imap4URL = [[NSURL alloc] initWithString: urlString];
|
|
|
|
}
|
2007-07-10 20:22:59 +02:00
|
|
|
|
|
|
|
return imap4URL;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2010-01-28 22:42:03 +01:00
|
|
|
- (NSString *) imap4PasswordRenewed: (BOOL) renewed
|
2007-07-10 20:22:59 +02:00
|
|
|
{
|
2010-08-06 16:25:59 +02:00
|
|
|
return [[self mailAccountFolder] imap4PasswordRenewed: renewed];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-10-31 16:55:47 +01:00
|
|
|
- (NSMutableString *) traversalFromMailAccount
|
|
|
|
{
|
|
|
|
NSMutableString *currentTraversal;
|
|
|
|
|
|
|
|
currentTraversal = [container traversalFromMailAccount];
|
2007-11-01 13:48:19 +01:00
|
|
|
if ([container isKindOfClass: [SOGoMailAccount class]])
|
|
|
|
[currentTraversal appendString: [self relativeImap4Name]];
|
|
|
|
else
|
|
|
|
[currentTraversal appendFormat: @"/%@", [self relativeImap4Name]];
|
2007-10-31 16:55:47 +01:00
|
|
|
|
|
|
|
return currentTraversal;
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
- (void)flushMailCaches {
|
|
|
|
[[self mailManager] flushCachesForURL:[self imap4URL]];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IMAP4 names */
|
|
|
|
|
2008-02-08 16:35:11 +01:00
|
|
|
#warning we could improve this by simply testing if the reference is the filename of an attachment or if the body part mentionned actually exists in the list of body parts. Another way is to use a prefix such as "attachment-*" to declare attachments.
|
|
|
|
- (BOOL) isBodyPartKey: (NSString *) key
|
|
|
|
{
|
|
|
|
NSString *trimmedKey;
|
|
|
|
|
|
|
|
trimmedKey = [key stringByTrimmingCharactersInSet:
|
|
|
|
[NSCharacterSet decimalDigitCharacterSet]];
|
|
|
|
|
|
|
|
return (![trimmedKey length]);
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-04-25 00:43:33 +02:00
|
|
|
- (NSArray *) aclsForUser: (NSString *) uid
|
|
|
|
{
|
|
|
|
return nil;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-04-25 00:43:33 +02:00
|
|
|
|
2009-09-29 22:13:36 +02:00
|
|
|
- (int) IMAP4IDFromAppendResult: (NSDictionary *) result
|
|
|
|
{
|
|
|
|
NSDictionary *results;
|
|
|
|
NSString *flag, *newIdString;
|
|
|
|
|
|
|
|
results = [[result objectForKey: @"RawResponse"]
|
|
|
|
objectForKey: @"ResponseResult"];
|
|
|
|
flag = [results objectForKey: @"flag"];
|
|
|
|
newIdString = [[flag componentsSeparatedByString: @" "] objectAtIndex: 2];
|
|
|
|
|
|
|
|
return [newIdString intValue];
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
@end /* SOGoMailBaseObject */
|