2006-06-15 21:34:10 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
|
|
|
|
|
|
|
This file is part of OpenGroupware.org.
|
|
|
|
|
|
|
|
OGo is free software; you can redistribute it and/or modify it under
|
|
|
|
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.
|
|
|
|
|
|
|
|
OGo 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 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.
|
|
|
|
*/
|
|
|
|
|
2007-05-22 21:30:36 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
2007-06-29 02:13:03 +02:00
|
|
|
#import <Foundation/NSCalendarDate.h>
|
2007-05-22 21:30:36 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSUserDefaults.h>
|
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
|
|
|
|
#import <NGObjWeb/WOContext.h>
|
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
|
|
|
#import <NGObjWeb/WOResponse.h>
|
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
|
|
|
#import <NGExtensions/NSNull+misc.h>
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
#import <NGExtensions/NSString+Encoding.h>
|
2007-07-10 21:23:39 +02:00
|
|
|
#import <NGExtensions/NSString+misc.h>
|
2007-05-22 21:30:36 +02:00
|
|
|
#import <NGImap4/NGImap4Connection.h>
|
|
|
|
#import <NGImap4/NGImap4Envelope.h>
|
|
|
|
#import <NGImap4/NGImap4EnvelopeAddress.h>
|
|
|
|
#import <NGMail/NGMimeMessageParser.h>
|
|
|
|
|
2007-10-30 20:46:48 +01:00
|
|
|
#import <SoObjects/SOGo/NSArray+Utilities.h>
|
2007-11-13 18:40:36 +01:00
|
|
|
#import <SoObjects/SOGo/NSDictionary+Utilities.h>
|
2007-05-22 21:30:36 +02:00
|
|
|
#import <SoObjects/SOGo/SOGoPermissions.h>
|
|
|
|
#import <SoObjects/SOGo/SOGoUser.h>
|
2007-10-25 17:26:54 +02:00
|
|
|
|
2007-11-06 22:24:13 +01:00
|
|
|
#import "NSString+Mail.h"
|
2007-10-25 17:26:54 +02:00
|
|
|
#import "NSData+Mail.h"
|
2007-05-22 21:30:36 +02:00
|
|
|
#import "SOGoMailFolder.h"
|
|
|
|
#import "SOGoMailAccount.h"
|
2007-11-09 00:18:52 +01:00
|
|
|
#import "SOGoMailAccounts.h"
|
2007-05-22 21:30:36 +02:00
|
|
|
#import "SOGoMailManager.h"
|
|
|
|
#import "SOGoMailBodyPart.h"
|
|
|
|
|
|
|
|
#import "SOGoMailObject.h"
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
@implementation SOGoMailObject
|
|
|
|
|
|
|
|
static NSArray *coreInfoKeys = nil;
|
|
|
|
static NSString *mailETag = nil;
|
|
|
|
static BOOL heavyDebug = NO;
|
|
|
|
static BOOL fetchHeader = YES;
|
|
|
|
static BOOL debugOn = NO;
|
|
|
|
static BOOL debugBodyStructure = NO;
|
|
|
|
static BOOL debugSoParts = NO;
|
|
|
|
|
2007-07-10 16:22:33 +02:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
if ((fetchHeader = ([ud boolForKey: @"SOGoDoNotFetchMailHeader"] ? NO : YES)))
|
2006-06-15 21:34:10 +02:00
|
|
|
NSLog(@"Note: fetching full mail header.");
|
|
|
|
else
|
|
|
|
NSLog(@"Note: not fetching full mail header: 'SOGoDoNotFetchMailHeader'");
|
|
|
|
|
|
|
|
/* Note: see SOGoMailManager.m for allowed IMAP4 keys */
|
|
|
|
/* Note: "BODY" actually returns the structure! */
|
|
|
|
if (fetchHeader) {
|
|
|
|
coreInfoKeys = [[NSArray alloc] initWithObjects:
|
2007-08-01 20:39:38 +02:00
|
|
|
@"FLAGS", @"ENVELOPE", @"BODYSTRUCTURE",
|
2006-06-15 21:34:10 +02:00
|
|
|
@"RFC822.SIZE",
|
|
|
|
@"RFC822.HEADER",
|
|
|
|
// not yet supported: @"INTERNALDATE",
|
|
|
|
nil];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
coreInfoKeys = [[NSArray alloc] initWithObjects:
|
2007-08-01 20:39:38 +02:00
|
|
|
@"FLAGS", @"ENVELOPE", @"BODYSTRUCTURE",
|
2006-06-15 21:34:10 +02:00
|
|
|
@"RFC822.SIZE",
|
|
|
|
// not yet supported: @"INTERNALDATE",
|
|
|
|
nil];
|
|
|
|
}
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
if (![[ud objectForKey: @"SOGoMailDisableETag"] boolValue]) {
|
|
|
|
mailETag = [[NSString alloc] initWithFormat: @"\"imap4url_%d_%d_%03d\"",
|
2006-06-15 21:34:10 +02:00
|
|
|
UIX_MAILER_MAJOR_VERSION,
|
|
|
|
UIX_MAILER_MINOR_VERSION,
|
|
|
|
UIX_MAILER_SUBMINOR_VERSION];
|
|
|
|
NSLog(@"Note(SOGoMailObject): using constant etag for mail parts: '%@'",
|
|
|
|
mailETag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NSLog(@"Note(SOGoMailObject): etag caching disabled!");
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:29:18 +02:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
2007-07-10 21:23:39 +02:00
|
|
|
[headers release];
|
|
|
|
[headerPart release];
|
|
|
|
[coreInfos release];
|
2006-06-15 21:34:10 +02:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IMAP4 */
|
|
|
|
|
2007-07-10 21:23:39 +02:00
|
|
|
- (NSString *) relativeImap4Name
|
|
|
|
{
|
|
|
|
return [nameInContainer stringByDeletingPathExtension];
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
/* hierarchy */
|
|
|
|
|
|
|
|
- (SOGoMailObject *)mailObject {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* part hierarchy */
|
|
|
|
|
2007-08-15 22:26:15 +02:00
|
|
|
- (NSString *) keyExtensionForPart: (id) _partInfo
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSString *mt, *st;
|
|
|
|
|
|
|
|
if (_partInfo == nil)
|
|
|
|
return nil;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
mt = [_partInfo valueForKey: @"type"];
|
|
|
|
st = [[_partInfo valueForKey: @"subtype"] lowercaseString];
|
|
|
|
if ([mt isEqualToString: @"text"]) {
|
|
|
|
if ([st isEqualToString: @"plain"]) return @".txt";
|
|
|
|
if ([st isEqualToString: @"html"]) return @".html";
|
|
|
|
if ([st isEqualToString: @"calendar"]) return @".ics";
|
|
|
|
if ([st isEqualToString: @"x-vcard"]) return @".vcf";
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-08-06 20:14:53 +02:00
|
|
|
else if ([mt isEqualToString: @"image"])
|
2006-06-15 21:34:10 +02:00
|
|
|
return [@"." stringByAppendingString:st];
|
2007-08-06 20:14:53 +02:00
|
|
|
else if ([mt isEqualToString: @"application"]) {
|
|
|
|
if ([st isEqualToString: @"pgp-signature"])
|
2006-06-15 21:34:10 +02:00
|
|
|
return @".asc";
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)relationshipKeysWithParts:(BOOL)_withParts {
|
|
|
|
/* should return non-multipart children */
|
|
|
|
NSMutableArray *ma;
|
|
|
|
NSArray *parts;
|
|
|
|
unsigned i, count;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
parts = [[self bodyStructure] valueForKey: @"parts"];
|
2006-06-15 21:34:10 +02:00
|
|
|
if (![parts isNotNull])
|
|
|
|
return nil;
|
|
|
|
if ((count = [parts count]) == 0)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
for (i = 0, ma = nil; i < count; i++) {
|
|
|
|
NSString *key, *ext;
|
|
|
|
id part;
|
|
|
|
BOOL hasParts;
|
|
|
|
|
|
|
|
part = [parts objectAtIndex:i];
|
2007-08-06 20:14:53 +02:00
|
|
|
hasParts = [part valueForKey: @"parts"] != nil ? YES:NO;
|
2006-06-15 21:34:10 +02:00
|
|
|
if ((hasParts && !_withParts) || (_withParts && !hasParts))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ma == nil)
|
|
|
|
ma = [NSMutableArray arrayWithCapacity:count - i];
|
|
|
|
|
|
|
|
ext = [self keyExtensionForPart:part];
|
2007-08-06 20:14:53 +02:00
|
|
|
key = [[NSString alloc] initWithFormat: @"%d%@", i + 1, ext?ext: @""];
|
2006-06-15 21:34:10 +02:00
|
|
|
[ma addObject:key];
|
|
|
|
[key release];
|
|
|
|
}
|
|
|
|
return ma;
|
|
|
|
}
|
|
|
|
|
2007-10-30 20:46:48 +01:00
|
|
|
- (NSArray *) toOneRelationshipKeys
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [self relationshipKeysWithParts:NO];
|
|
|
|
}
|
2007-10-30 20:46:48 +01:00
|
|
|
|
|
|
|
- (NSArray *) toManyRelationshipKeys
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [self relationshipKeysWithParts:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* message */
|
|
|
|
|
2007-08-15 22:26:15 +02:00
|
|
|
- (id) fetchParts: (NSArray *) _parts
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
// TODO: explain what it does
|
|
|
|
/*
|
|
|
|
Called by -fetchPlainTextParts:
|
|
|
|
*/
|
2007-08-01 20:39:38 +02:00
|
|
|
return [[self imap4Connection] fetchURL: [self imap4URL] parts:_parts];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* core infos */
|
|
|
|
|
|
|
|
- (BOOL)doesMailExist {
|
|
|
|
static NSArray *existsKey = nil;
|
|
|
|
id msgs;
|
|
|
|
|
2007-07-10 21:23:39 +02:00
|
|
|
if (coreInfos != nil) /* if we have coreinfos, we can use them */
|
|
|
|
return [coreInfos isNotNull];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
/* otherwise fetch something really simple */
|
|
|
|
|
|
|
|
if (existsKey == nil) /* we use size, other suggestions? */
|
2007-08-06 20:14:53 +02:00
|
|
|
existsKey = [[NSArray alloc] initWithObjects: @"RFC822.SIZE", nil];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
msgs = [self fetchParts:existsKey]; // returns dict
|
2007-08-06 20:14:53 +02:00
|
|
|
msgs = [msgs valueForKey: @"fetch"];
|
2006-06-15 21:34:10 +02:00
|
|
|
return [msgs count] > 0 ? YES : NO;
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:29:18 +02:00
|
|
|
- (id) fetchCoreInfos
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
id msgs;
|
2007-08-18 22:35:38 +02:00
|
|
|
|
|
|
|
if (!coreInfos)
|
|
|
|
{
|
2007-08-20 22:43:22 +02:00
|
|
|
msgs = [self fetchParts: coreInfoKeys]; // returns dict
|
2007-08-18 22:35:38 +02:00
|
|
|
if (heavyDebug)
|
|
|
|
[self logWithFormat: @"M: %@", msgs];
|
|
|
|
msgs = [msgs valueForKey: @"fetch"];
|
|
|
|
if ([msgs count] > 0)
|
|
|
|
coreInfos = [msgs objectAtIndex: 0];
|
|
|
|
[coreInfos retain];
|
|
|
|
}
|
2007-08-17 04:29:18 +02:00
|
|
|
|
2007-07-10 21:23:39 +02:00
|
|
|
return coreInfos;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 04:29:18 +02:00
|
|
|
- (id) bodyStructure
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
id body;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
body = [[self fetchCoreInfos] valueForKey: @"body"];
|
2006-06-15 21:34:10 +02:00
|
|
|
if (debugBodyStructure)
|
2007-08-06 20:14:53 +02:00
|
|
|
[self logWithFormat: @"BODY: %@", body];
|
2007-08-17 04:29:18 +02:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:29:18 +02:00
|
|
|
- (NGImap4Envelope *) envelope
|
|
|
|
{
|
2007-08-06 20:14:53 +02:00
|
|
|
return [[self fetchCoreInfos] valueForKey: @"envelope"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-06-29 02:13:03 +02:00
|
|
|
|
|
|
|
- (NSString *) subject
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [[self envelope] subject];
|
|
|
|
}
|
2007-06-29 02:13:03 +02:00
|
|
|
|
2007-11-05 17:37:17 +01:00
|
|
|
- (NSString *) decodedSubject
|
|
|
|
{
|
|
|
|
return [[self subject] decodedSubject];
|
|
|
|
}
|
|
|
|
|
2007-06-29 02:13:03 +02:00
|
|
|
- (NSCalendarDate *) date
|
|
|
|
{
|
|
|
|
NSTimeZone *userTZ;
|
|
|
|
NSCalendarDate *date;
|
|
|
|
|
|
|
|
userTZ = [[context activeUser] timeZone];
|
|
|
|
date = [[self envelope] date];
|
|
|
|
[date setTimeZone: userTZ];
|
|
|
|
|
|
|
|
return date;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-06-29 02:13:03 +02:00
|
|
|
|
2007-08-18 22:35:38 +02:00
|
|
|
- (NSArray *) fromEnvelopeAddresses
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [[self envelope] from];
|
|
|
|
}
|
2007-08-18 22:35:38 +02:00
|
|
|
|
|
|
|
- (NSArray *) toEnvelopeAddresses
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [[self envelope] to];
|
|
|
|
}
|
2007-08-18 22:35:38 +02:00
|
|
|
|
|
|
|
- (NSArray *) ccEnvelopeAddresses
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [[self envelope] cc];
|
|
|
|
}
|
|
|
|
|
2007-11-19 01:47:07 +01:00
|
|
|
- (NSArray *) replyToEnvelopeAddresses
|
|
|
|
{
|
|
|
|
return [[self envelope] replyTo];
|
|
|
|
}
|
|
|
|
|
2007-08-18 22:35:38 +02:00
|
|
|
- (NSData *) mailHeaderData
|
|
|
|
{
|
2007-08-06 20:14:53 +02:00
|
|
|
return [[self fetchCoreInfos] valueForKey: @"header"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-08-18 22:35:38 +02:00
|
|
|
|
|
|
|
- (BOOL) hasMailHeaderInCoreInfos
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [[self mailHeaderData] length] > 0 ? YES : NO;
|
|
|
|
}
|
|
|
|
|
2007-08-18 22:35:38 +02:00
|
|
|
- (id) mailHeaderPart
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NGMimeMessageParser *parser;
|
|
|
|
NSData *data;
|
|
|
|
|
2007-07-10 21:23:39 +02:00
|
|
|
if (headerPart != nil)
|
|
|
|
return [headerPart isNotNull] ? headerPart : nil;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
if ([(data = [self mailHeaderData]) length] == 0)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
// TODO: do we need to set some delegate method which stops parsing the body?
|
|
|
|
parser = [[NGMimeMessageParser alloc] init];
|
2007-07-10 21:23:39 +02:00
|
|
|
headerPart = [[parser parsePartFromData:data] retain];
|
2006-06-15 21:34:10 +02:00
|
|
|
[parser release]; parser = nil;
|
|
|
|
|
2007-07-10 21:23:39 +02:00
|
|
|
if (headerPart == nil) {
|
|
|
|
headerPart = [[NSNull null] retain];
|
2006-06-15 21:34:10 +02:00
|
|
|
return nil;
|
|
|
|
}
|
2007-07-10 21:23:39 +02:00
|
|
|
return headerPart;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-07-10 21:23:39 +02:00
|
|
|
|
|
|
|
- (NSDictionary *) mailHeaders
|
|
|
|
{
|
|
|
|
if (!headers)
|
|
|
|
headers = [[[self mailHeaderPart] headers] copy];
|
|
|
|
|
|
|
|
return headers;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (id) lookupInfoForBodyPart: (id) _path
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSEnumerator *pe;
|
|
|
|
NSString *p;
|
|
|
|
id info;
|
|
|
|
|
|
|
|
if (![_path isNotNull])
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
if ((info = [self bodyStructure]) == nil) {
|
2007-08-06 20:14:53 +02:00
|
|
|
[self errorWithFormat: @"got no body part structure!"];
|
2006-06-15 21:34:10 +02:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ensure array argument */
|
|
|
|
|
|
|
|
if ([_path isKindOfClass:[NSString class]]) {
|
|
|
|
if ([_path length] == 0)
|
|
|
|
return info;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
_path = [_path componentsSeparatedByString: @"."];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
For each path component, eg 1,1,3
|
|
|
|
|
|
|
|
Remember that we need special processing for message/rfc822 which maps the
|
|
|
|
namespace of multiparts directly into the main namespace.
|
|
|
|
|
|
|
|
TODO(hh): no I don't remember, please explain in more detail!
|
|
|
|
*/
|
|
|
|
pe = [_path objectEnumerator];
|
|
|
|
while ((p = [pe nextObject]) != nil && [info isNotNull]) {
|
|
|
|
unsigned idx;
|
|
|
|
NSArray *parts;
|
|
|
|
NSString *mt;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
[self debugWithFormat: @"check PATH: %@", p];
|
2006-06-15 21:34:10 +02:00
|
|
|
idx = [p intValue] - 1;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
parts = [info valueForKey: @"parts"];
|
|
|
|
mt = [[info valueForKey: @"type"] lowercaseString];
|
|
|
|
if ([mt isEqualToString: @"message"]) {
|
2006-06-15 21:34:10 +02:00
|
|
|
/* we have special behaviour for message types */
|
|
|
|
id body;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
if ((body = [info valueForKey: @"body"]) != nil) {
|
|
|
|
mt = [body valueForKey: @"type"];
|
|
|
|
if ([mt isEqualToString: @"multipart"])
|
|
|
|
parts = [body valueForKey: @"parts"];
|
2006-06-15 21:34:10 +02:00
|
|
|
else
|
|
|
|
parts = [NSArray arrayWithObject:body];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx >= [parts count]) {
|
|
|
|
[self errorWithFormat:
|
|
|
|
@"body part index out of bounds(idx=%d vs count=%d): %@",
|
|
|
|
(idx + 1), [parts count], info];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
info = [parts objectAtIndex:idx];
|
|
|
|
}
|
|
|
|
return [info isNotNull] ? info : nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* content */
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSData *) content
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSData *content;
|
|
|
|
id result, fullResult;
|
|
|
|
|
2007-10-30 18:07:39 +01:00
|
|
|
fullResult = [self fetchParts: [NSArray arrayWithObject: @"RFC822"]];
|
2006-06-15 21:34:10 +02:00
|
|
|
if (fullResult == nil)
|
|
|
|
return nil;
|
|
|
|
|
2007-10-30 18:07:39 +01:00
|
|
|
if ([fullResult isKindOfClass: [NSException class]])
|
2006-06-15 21:34:10 +02:00
|
|
|
return fullResult;
|
|
|
|
|
|
|
|
/* extract fetch result */
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
result = [fullResult valueForKey: @"fetch"];
|
2006-06-15 21:34:10 +02:00
|
|
|
if (![result isKindOfClass:[NSArray class]]) {
|
|
|
|
[self logWithFormat:
|
|
|
|
@"ERROR: unexpected IMAP4 result (missing 'fetch'): %@",
|
|
|
|
fullResult];
|
|
|
|
return [NSException exceptionWithHTTPStatus:500 /* server error */
|
2007-08-06 20:14:53 +02:00
|
|
|
reason: @"unexpected IMAP4 result"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
if ([result count] == 0)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
result = [result objectAtIndex:0];
|
|
|
|
|
|
|
|
/* extract message */
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
if ((content = [result valueForKey: @"message"]) == nil) {
|
2006-06-15 21:34:10 +02:00
|
|
|
[self logWithFormat:
|
|
|
|
@"ERROR: unexpected IMAP4 result (missing 'message'): %@",
|
|
|
|
result];
|
|
|
|
return [NSException exceptionWithHTTPStatus:500 /* server error */
|
2007-08-06 20:14:53 +02:00
|
|
|
reason: @"unexpected IMAP4 result"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return [[content copy] autorelease];
|
|
|
|
}
|
|
|
|
|
2007-06-01 07:06:29 +02:00
|
|
|
- (NSString *) davContentType
|
|
|
|
{
|
|
|
|
return @"message/rfc822";
|
|
|
|
}
|
|
|
|
|
2007-07-04 17:19:58 +02:00
|
|
|
- (NSString *) contentAsString
|
|
|
|
{
|
2007-10-30 18:07:39 +01:00
|
|
|
id s;
|
2006-06-15 21:34:10 +02:00
|
|
|
NSData *content;
|
2007-10-30 18:07:39 +01:00
|
|
|
|
|
|
|
content = [self content];
|
|
|
|
if (content)
|
|
|
|
{
|
|
|
|
if ([content isKindOfClass: [NSData class]])
|
|
|
|
{
|
2007-11-19 01:47:07 +01:00
|
|
|
#warning we ignore the charset here?
|
2007-10-30 18:07:39 +01:00
|
|
|
s = [[NSString alloc] initWithData: content
|
|
|
|
encoding: NSISOLatin1StringEncoding];
|
|
|
|
if (s)
|
|
|
|
[s autorelease];
|
|
|
|
else
|
|
|
|
[self logWithFormat:
|
|
|
|
@"ERROR: could not convert data of length %d to string",
|
|
|
|
[content length]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s = content;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s = nil;
|
|
|
|
|
|
|
|
return s;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* bulk fetching of plain/text content */
|
|
|
|
|
2007-10-30 20:46:48 +01:00
|
|
|
// - (BOOL) shouldFetchPartOfType: (NSString *) _type
|
|
|
|
// subtype: (NSString *) _subtype
|
|
|
|
// {
|
|
|
|
// /*
|
|
|
|
// This method decides which parts are 'prefetched' for display. Those are
|
|
|
|
// usually text parts (the set is currently hardcoded in this method ...).
|
|
|
|
// */
|
|
|
|
// _type = [_type lowercaseString];
|
|
|
|
// _subtype = [_subtype lowercaseString];
|
|
|
|
|
|
|
|
// return (([_type isEqualToString: @"text"]
|
|
|
|
// && ([_subtype isEqualToString: @"plain"]
|
|
|
|
// || [_subtype isEqualToString: @"html"]
|
|
|
|
// || [_subtype isEqualToString: @"calendar"]))
|
|
|
|
// || ([_type isEqualToString: @"application"]
|
|
|
|
// && ([_subtype isEqualToString: @"pgp-signature"]
|
|
|
|
// || [_subtype hasPrefix: @"x-vnd.kolab."])));
|
|
|
|
// }
|
|
|
|
|
|
|
|
- (void) addRequiredKeysOfStructure: (NSDictionary *) info
|
|
|
|
path: (NSString *) p
|
|
|
|
toArray: (NSMutableArray *) keys
|
|
|
|
acceptedTypes: (NSArray *) types
|
2006-06-15 21:34:10 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is used to collect the set of IMAP4 fetch-keys required to fetch
|
|
|
|
the basic parts of the body structure. That is, to fetch all parts which
|
|
|
|
are displayed 'inline' in a single IMAP4 fetch.
|
|
|
|
|
|
|
|
The method calls itself recursively to walk the body structure.
|
|
|
|
*/
|
2007-08-17 04:29:18 +02:00
|
|
|
NSArray *parts;
|
2006-06-15 21:34:10 +02:00
|
|
|
unsigned i, count;
|
2007-08-17 04:29:18 +02:00
|
|
|
NSString *k;
|
2006-06-15 21:34:10 +02:00
|
|
|
id body;
|
2007-10-30 20:46:48 +01:00
|
|
|
NSString *sp, *mimeType;
|
2007-08-17 04:29:18 +02:00
|
|
|
id childInfo;
|
2007-10-30 20:46:48 +01:00
|
|
|
|
|
|
|
mimeType = [[NSString stringWithFormat: @"%@/%@",
|
|
|
|
[info valueForKey: @"type"],
|
|
|
|
[info valueForKey: @"subtype"]]
|
|
|
|
lowercaseString];
|
|
|
|
if ([types containsObject: mimeType])
|
2007-08-17 04:29:18 +02:00
|
|
|
{
|
2007-10-30 20:46:48 +01:00
|
|
|
if ([p length] > 0)
|
|
|
|
k = [NSString stringWithFormat: @"body[%@]", p];
|
2007-08-17 04:29:18 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
for some reason we need to add ".TEXT" for plain text stuff on root
|
|
|
|
entities?
|
|
|
|
TODO: check with HTML
|
|
|
|
*/
|
|
|
|
k = @"body[text]";
|
|
|
|
}
|
2007-10-30 20:46:48 +01:00
|
|
|
[keys addObject: [NSDictionary dictionaryWithObjectsAndKeys: k, @"key",
|
|
|
|
mimeType, @"mimeType", nil]];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-10-30 20:46:48 +01:00
|
|
|
parts = [info objectForKey: @"parts"];
|
|
|
|
count = [parts count];
|
|
|
|
for (i = 0; i < count; i++)
|
2007-08-17 04:29:18 +02:00
|
|
|
{
|
2007-10-30 20:46:48 +01:00
|
|
|
sp = (([p length] > 0)
|
|
|
|
? [p stringByAppendingFormat: @".%d", i + 1]
|
|
|
|
: [NSString stringWithFormat: @"%d", i + 1]);
|
2007-08-17 04:29:18 +02:00
|
|
|
|
2007-10-30 20:46:48 +01:00
|
|
|
childInfo = [parts objectAtIndex: i];
|
|
|
|
|
|
|
|
[self addRequiredKeysOfStructure: childInfo
|
|
|
|
path: sp toArray: keys
|
|
|
|
acceptedTypes: types];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check body */
|
|
|
|
body = [info objectForKey: @"body"];
|
|
|
|
if (body)
|
|
|
|
{
|
|
|
|
sp = [[body valueForKey: @"type"] lowercaseString];
|
|
|
|
if ([sp isEqualToString: @"multipart"])
|
|
|
|
sp = p;
|
|
|
|
else
|
|
|
|
sp = [p length] > 0 ? [p stringByAppendingString: @".1"] : @"1";
|
|
|
|
[self addRequiredKeysOfStructure: body
|
|
|
|
path: sp toArray: keys
|
|
|
|
acceptedTypes: types];
|
2007-08-17 04:29:18 +02:00
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 04:29:18 +02:00
|
|
|
- (NSArray *) plainTextContentFetchKeys
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
/*
|
|
|
|
The name is not 100% correct. The method returns all body structure fetch
|
|
|
|
keys which are marked by the -shouldFetchPartOfType:subtype: method.
|
|
|
|
*/
|
|
|
|
NSMutableArray *ma;
|
2007-10-30 20:46:48 +01:00
|
|
|
NSArray *types;
|
|
|
|
|
|
|
|
types = [NSArray arrayWithObjects: @"text/plain", @"text/html",
|
2007-11-22 18:32:44 +01:00
|
|
|
@"text/calendar", @"application/ics",
|
|
|
|
@"application/pgp-signature", nil];
|
2007-10-30 20:46:48 +01:00
|
|
|
ma = [NSMutableArray arrayWithCapacity: 4];
|
2007-08-18 22:35:38 +02:00
|
|
|
[self addRequiredKeysOfStructure: [self bodyStructure]
|
2007-10-30 20:46:48 +01:00
|
|
|
path: @"" toArray: ma acceptedTypes: types];
|
2007-08-18 22:35:38 +02:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return ma;
|
|
|
|
}
|
|
|
|
|
2007-08-18 22:35:38 +02:00
|
|
|
- (NSDictionary *) fetchPlainTextParts: (NSArray *) _fetchKeys
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
// TODO: is the name correct or does it also fetch other parts?
|
|
|
|
NSMutableDictionary *flatContents;
|
|
|
|
unsigned i, count;
|
|
|
|
id result;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
[self debugWithFormat: @"fetch keys: %@", _fetchKeys];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-10-30 20:46:48 +01:00
|
|
|
result = [self fetchParts: [_fetchKeys objectsForKey: @"key"]];
|
2007-08-06 20:14:53 +02:00
|
|
|
result = [result valueForKey: @"RawResponse"]; // hackish
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
// Note: -valueForKey: doesn't work!
|
2007-08-06 20:14:53 +02:00
|
|
|
result = [(NSDictionary *)result objectForKey: @"fetch"];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
count = [_fetchKeys count];
|
|
|
|
flatContents = [NSMutableDictionary dictionaryWithCapacity:count];
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
NSString *key;
|
|
|
|
NSData *data;
|
|
|
|
|
2007-10-30 20:46:48 +01:00
|
|
|
key = [[_fetchKeys objectAtIndex:i] objectForKey: @"key"];
|
2006-06-15 21:34:10 +02:00
|
|
|
data = [(NSDictionary *)[(NSDictionary *)result objectForKey:key]
|
2007-08-06 20:14:53 +02:00
|
|
|
objectForKey: @"data"];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
if (![data isNotNull]) {
|
2007-08-06 20:14:53 +02:00
|
|
|
[self errorWithFormat: @"got no data for key: %@", key];
|
2006-06-15 21:34:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
if ([key isEqualToString: @"body[text]"])
|
2006-06-15 21:34:10 +02:00
|
|
|
key = @""; // see key collector for explanation (TODO: where?)
|
2007-08-06 20:14:53 +02:00
|
|
|
else if ([key hasPrefix: @"body["]) {
|
2006-06-15 21:34:10 +02:00
|
|
|
NSRange r;
|
|
|
|
|
|
|
|
key = [key substringFromIndex:5];
|
2007-08-06 20:14:53 +02:00
|
|
|
r = [key rangeOfString: @"]"];
|
2006-06-15 21:34:10 +02:00
|
|
|
if (r.length > 0)
|
|
|
|
key = [key substringToIndex:r.location];
|
|
|
|
}
|
|
|
|
[flatContents setObject:data forKey:key];
|
|
|
|
}
|
|
|
|
return flatContents;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSDictionary *) fetchPlainTextParts
|
|
|
|
{
|
2007-10-30 20:46:48 +01:00
|
|
|
return [self fetchPlainTextParts: [self plainTextContentFetchKeys]];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* convert parts to strings */
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSString *) stringForData: (NSData *) _data
|
|
|
|
partInfo: (NSDictionary *) _info
|
2006-11-21 23:33:43 +01:00
|
|
|
{
|
2007-10-25 17:26:54 +02:00
|
|
|
NSString *charset, *s;
|
2006-11-17 23:07:57 +01:00
|
|
|
NSData *mailData;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-10-25 17:26:54 +02:00
|
|
|
if ([_data isNotNull])
|
2006-11-17 23:07:57 +01:00
|
|
|
{
|
2007-10-25 17:26:54 +02:00
|
|
|
mailData
|
|
|
|
= [_data bodyDataFromEncoding: [_info objectForKey: @"encoding"]];
|
|
|
|
|
|
|
|
charset = [[_info valueForKey: @"parameterList"] valueForKey: @"charset"];
|
|
|
|
if (![charset length])
|
|
|
|
{
|
2007-11-25 18:26:56 +01:00
|
|
|
s = nil;
|
2007-10-25 17:26:54 +02:00
|
|
|
}
|
|
|
|
else
|
2007-11-16 22:15:38 +01:00
|
|
|
{
|
|
|
|
s = [NSString stringWithData: mailData usingEncodingNamed: charset];
|
|
|
|
}
|
2007-11-25 18:26:56 +01:00
|
|
|
|
|
|
|
// If it has failed, we try at least using UTF-8. Normally, this can NOT fail.
|
|
|
|
// Unfortunately, it seems to fail under GNUstep so we try latin1 if that's
|
|
|
|
// the case
|
|
|
|
if (!s)
|
|
|
|
s = [[[NSString alloc] initWithData: mailData encoding: NSUTF8StringEncoding] autorelease];
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
s = [[[NSString alloc] initWithData: mailData encoding: NSISOLatin1StringEncoding] autorelease];
|
2006-11-17 23:07:57 +01:00
|
|
|
}
|
|
|
|
else
|
2007-10-25 17:26:54 +02:00
|
|
|
s = nil;
|
2006-11-17 23:07:57 +01:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSDictionary *) stringifyTextParts: (NSDictionary *) _datas
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSMutableDictionary *md;
|
2007-10-30 20:46:48 +01:00
|
|
|
NSDictionary *info;
|
2006-06-15 21:34:10 +02:00
|
|
|
NSEnumerator *keys;
|
2007-10-30 20:46:48 +01:00
|
|
|
NSString *key, *s;
|
|
|
|
|
|
|
|
md = [NSMutableDictionary dictionaryWithCapacity:4];
|
2006-06-15 21:34:10 +02:00
|
|
|
keys = [_datas keyEnumerator];
|
2007-10-30 20:46:48 +01:00
|
|
|
while ((key = [keys nextObject]))
|
|
|
|
{
|
|
|
|
info = [self lookupInfoForBodyPart: key];
|
|
|
|
s = [self stringForData: [_datas objectForKey:key] partInfo: info];
|
|
|
|
if (s)
|
|
|
|
[md setObject: s forKey: key];
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return md;
|
|
|
|
}
|
2007-08-22 17:14:36 +02:00
|
|
|
|
|
|
|
- (NSDictionary *) fetchPlainTextStrings: (NSArray *) _fetchKeys
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
/*
|
|
|
|
The fetched parts are NSData objects, this method converts them into
|
|
|
|
NSString objects based on the information inside the bodystructure.
|
|
|
|
|
|
|
|
The fetch-keys are body fetch-keys like: body[text] or body[1.2.3].
|
|
|
|
The keys in the result dictionary are "" for 'text' and 1.2.3 for parts.
|
|
|
|
*/
|
|
|
|
NSDictionary *datas;
|
|
|
|
|
|
|
|
if ((datas = [self fetchPlainTextParts:_fetchKeys]) == nil)
|
|
|
|
return nil;
|
|
|
|
if ([datas isKindOfClass:[NSException class]])
|
|
|
|
return datas;
|
|
|
|
|
|
|
|
return [self stringifyTextParts:datas];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
|
2007-06-01 22:57:20 +02:00
|
|
|
- (NSException *) addFlags: (id) _flags
|
|
|
|
{
|
2007-08-01 20:39:38 +02:00
|
|
|
return [[self imap4Connection] addFlags:_flags toURL: [self imap4URL]];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-06-01 22:57:20 +02:00
|
|
|
|
|
|
|
- (NSException *) removeFlags: (id) _flags
|
|
|
|
{
|
2007-08-01 20:39:38 +02:00
|
|
|
return [[self imap4Connection] removeFlags:_flags toURL: [self imap4URL]];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* permissions */
|
|
|
|
|
2007-05-22 21:16:58 +02:00
|
|
|
- (BOOL) isDeletionAllowed
|
|
|
|
{
|
2007-05-22 21:30:36 +02:00
|
|
|
NSArray *parentAcl;
|
2007-05-22 21:16:58 +02:00
|
|
|
NSString *login;
|
|
|
|
|
|
|
|
login = [[context activeUser] login];
|
|
|
|
parentAcl = [[self container] aclsForUser: login];
|
|
|
|
|
2007-06-01 22:57:20 +02:00
|
|
|
return [parentAcl containsObject: SOGoRole_ObjectEraser];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* name lookup */
|
|
|
|
|
2007-06-13 00:09:32 +02:00
|
|
|
- (id) lookupImap4BodyPartKey: (NSString *) _key
|
|
|
|
inContext: (id) _ctx
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
// TODO: we might want to check for existence prior controller creation
|
|
|
|
Class clazz;
|
2007-11-10 01:02:30 +01:00
|
|
|
NSArray *parts;
|
|
|
|
int part;
|
|
|
|
NSDictionary *partDesc;
|
|
|
|
NSString *mimeType;
|
|
|
|
|
|
|
|
parts = [[self bodyStructure] objectForKey: @"parts"];
|
2007-11-19 01:47:07 +01:00
|
|
|
|
|
|
|
/* We don't have parts here but we're trying to download the message's
|
|
|
|
content that could be an image/jpeg, as an example */
|
2007-11-21 16:04:24 +01:00
|
|
|
if ([parts count] == 0 && ![_key intValue])
|
2007-11-21 16:27:52 +01:00
|
|
|
{
|
|
|
|
partDesc = [self bodyStructure];
|
|
|
|
_key = @"1";
|
|
|
|
}
|
2007-11-21 16:04:24 +01:00
|
|
|
else
|
2007-11-19 01:47:07 +01:00
|
|
|
{
|
2007-11-21 16:04:24 +01:00
|
|
|
part = [_key intValue] - 1;
|
|
|
|
if (part > -1 && part < [parts count])
|
|
|
|
partDesc = [parts objectAtIndex: part];
|
|
|
|
else
|
|
|
|
partDesc = nil;
|
2007-11-19 01:47:07 +01:00
|
|
|
}
|
|
|
|
|
2007-11-21 16:04:24 +01:00
|
|
|
if (partDesc)
|
2007-11-10 01:02:30 +01:00
|
|
|
{
|
|
|
|
mimeType = [[partDesc keysWithFormat: @"%{type}/%{subtype}"] lowercaseString];
|
|
|
|
clazz = [SOGoMailBodyPart bodyPartClassForMimeType: mimeType
|
|
|
|
inContext: _ctx];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
clazz = Nil;
|
2007-11-21 16:04:24 +01:00
|
|
|
|
2007-06-13 00:09:32 +02:00
|
|
|
return [clazz objectWithName:_key inContainer: self];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (id) lookupName: (NSString *) _key
|
|
|
|
inContext: (id) _ctx
|
|
|
|
acquire: (BOOL) _flag
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
id obj;
|
|
|
|
|
|
|
|
/* first check attributes directly bound to the application */
|
|
|
|
if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]) != nil)
|
|
|
|
return obj;
|
|
|
|
|
|
|
|
/* lookup body part */
|
|
|
|
|
|
|
|
if ([self isBodyPartKey:_key inContext:_ctx]) {
|
|
|
|
if ((obj = [self lookupImap4BodyPartKey:_key inContext:_ctx]) != nil) {
|
2007-11-10 01:02:30 +01:00
|
|
|
if (debugSoParts)
|
2007-08-06 20:14:53 +02:00
|
|
|
[self logWithFormat: @"mail looked up part %@: %@", _key, obj];
|
2006-06-15 21:34:10 +02:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return 404 to stop acquisition */
|
|
|
|
return [NSException exceptionWithHTTPStatus:404 /* Not Found */
|
2007-08-06 20:14:53 +02:00
|
|
|
reason: @"Did not find mail method or part-reference!"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* WebDAV */
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (BOOL) davIsCollection
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
/* while a mail has child objects, it should appear as a file in WebDAV */
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (id) davContentLength
|
|
|
|
{
|
2007-08-06 20:14:53 +02:00
|
|
|
return [[self fetchCoreInfos] valueForKey: @"size"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSDate *) davCreationDate
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
// TODO: use INTERNALDATE once NGImap4 supports that
|
|
|
|
return nil;
|
|
|
|
}
|
2007-08-22 17:14:36 +02:00
|
|
|
|
|
|
|
- (NSDate *) davLastModified
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [self davCreationDate];
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSException *) davMoveToTargetObject: (id) _target
|
|
|
|
newName: (NSString *) _name
|
|
|
|
inContext: (id)_ctx
|
2006-06-15 21:34:10 +02:00
|
|
|
{
|
2007-08-06 20:14:53 +02:00
|
|
|
[self logWithFormat: @"TODO: should move mail as '%@' to: %@",
|
2006-06-15 21:34:10 +02:00
|
|
|
_name, _target];
|
2007-08-22 17:14:36 +02:00
|
|
|
return [NSException exceptionWithHTTPStatus: 501 /* Not Implemented */
|
2007-08-06 20:14:53 +02:00
|
|
|
reason: @"not implemented"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSException *) davCopyToTargetObject: (id) _target
|
|
|
|
newName: (NSString *) _name
|
|
|
|
inContext: (id)_ctx
|
2006-06-15 21:34:10 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Note: this is special because we create SOGoMailObject's even if they do
|
|
|
|
not exist (for performance reasons).
|
|
|
|
|
|
|
|
Also: we cannot really take a target resource, the ID will be assigned by
|
|
|
|
the IMAP4 server.
|
|
|
|
We even cannot return a 'location' header instead because IMAP4
|
|
|
|
doesn't tell us the new ID.
|
|
|
|
*/
|
|
|
|
NSURL *destImap4URL;
|
|
|
|
|
|
|
|
destImap4URL = ([_name length] == 0)
|
|
|
|
? [[_target container] imap4URL]
|
|
|
|
: [_target imap4URL];
|
|
|
|
|
|
|
|
return [[self mailManager] copyMailURL:[self imap4URL]
|
|
|
|
toFolderURL:destImap4URL
|
|
|
|
password:[self imap4Password]];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* actions */
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (id) GETAction: (id) _ctx
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSException *error;
|
|
|
|
WOResponse *r;
|
|
|
|
NSData *content;
|
|
|
|
|
|
|
|
if ((error = [self matchesRequestConditionInContext:_ctx]) != nil) {
|
|
|
|
/* check whether the mail still exists */
|
|
|
|
if (![self doesMailExist]) {
|
|
|
|
return [NSException exceptionWithHTTPStatus:404 /* Not Found */
|
2007-08-06 20:14:53 +02:00
|
|
|
reason: @"mail was deleted"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
return error; /* return 304 or 416 */
|
|
|
|
}
|
|
|
|
|
|
|
|
content = [self content];
|
|
|
|
if ([content isKindOfClass:[NSException class]])
|
|
|
|
return content;
|
|
|
|
if (content == nil) {
|
|
|
|
return [NSException exceptionWithHTTPStatus:404 /* Not Found */
|
2007-08-06 20:14:53 +02:00
|
|
|
reason: @"did not find IMAP4 message"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
r = [(WOContext *)_ctx response];
|
2007-08-06 20:14:53 +02:00
|
|
|
[r setHeader: @"message/rfc822" forKey: @"content-type"];
|
2006-06-15 21:34:10 +02:00
|
|
|
[r setContent:content];
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* operations */
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSException *) trashInContext: (id) _ctx
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
/*
|
|
|
|
Trashing is three actions:
|
|
|
|
a) copy to trash folder
|
|
|
|
b) mark mail as deleted
|
|
|
|
c) expunge folder
|
|
|
|
|
|
|
|
In case b) or c) fails, we can't do anything because IMAP4 doesn't tell us
|
|
|
|
the ID used in the trash folder.
|
|
|
|
*/
|
|
|
|
SOGoMailFolder *trashFolder;
|
|
|
|
NSException *error;
|
|
|
|
|
|
|
|
// TODO: check for safe HTTP method
|
|
|
|
|
|
|
|
trashFolder = [[self mailAccountFolder] trashFolderInContext:_ctx];
|
|
|
|
if ([trashFolder isKindOfClass:[NSException class]])
|
|
|
|
return (NSException *)trashFolder;
|
|
|
|
if (![trashFolder isNotNull]) {
|
|
|
|
return [NSException exceptionWithHTTPStatus:500 /* Server Error */
|
2007-08-06 20:14:53 +02:00
|
|
|
reason: @"Did not find Trash folder!"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
[trashFolder flushMailCaches];
|
|
|
|
|
|
|
|
/* a) copy */
|
|
|
|
|
|
|
|
error = [self davCopyToTargetObject:trashFolder
|
2007-08-06 20:14:53 +02:00
|
|
|
newName: @"fakeNewUnusedByIMAP4" /* autoassigned */
|
2006-06-15 21:34:10 +02:00
|
|
|
inContext:_ctx];
|
|
|
|
if (error != nil) return error;
|
|
|
|
|
|
|
|
/* b) mark deleted */
|
|
|
|
|
2007-08-17 04:29:18 +02:00
|
|
|
error = [[self imap4Connection] markURLDeleted: [self imap4URL]];
|
2006-06-15 21:34:10 +02:00
|
|
|
if (error != nil) return error;
|
2007-10-26 16:26:01 +02:00
|
|
|
|
|
|
|
[container markForExpunge];
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
[self flushMailCaches];
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2007-10-18 00:29:51 +02:00
|
|
|
- (NSException *) copyToFolderNamed: (NSString *) folderName
|
2006-11-10 23:53:57 +01:00
|
|
|
inContext: (id)_ctx
|
|
|
|
{
|
2007-11-19 16:02:25 +01:00
|
|
|
SOGoMailFolder *destFolder;
|
2006-11-10 23:53:57 +01:00
|
|
|
NSEnumerator *folders;
|
|
|
|
NSString *currentFolderName, *reason;
|
|
|
|
|
|
|
|
// TODO: check for safe HTTP method
|
|
|
|
|
2007-11-19 16:02:25 +01:00
|
|
|
destFolder = (SOGoMailFolder *) [self mailAccountsFolder];
|
2006-11-10 23:53:57 +01:00
|
|
|
folders = [[folderName componentsSeparatedByString: @"/"] objectEnumerator];
|
|
|
|
currentFolderName = [folders nextObject];
|
|
|
|
currentFolderName = [folders nextObject];
|
|
|
|
|
|
|
|
while (currentFolderName)
|
|
|
|
{
|
|
|
|
destFolder = [destFolder lookupName: currentFolderName
|
|
|
|
inContext: _ctx
|
|
|
|
acquire: NO];
|
|
|
|
if ([destFolder isKindOfClass: [NSException class]])
|
|
|
|
return (NSException *) destFolder;
|
|
|
|
currentFolderName = [folders nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!([destFolder isKindOfClass: [SOGoMailFolder class]]
|
|
|
|
&& [destFolder isNotNull]))
|
|
|
|
{
|
|
|
|
reason = [NSString stringWithFormat: @"Did not find folder name '%@'!",
|
|
|
|
folderName];
|
|
|
|
return [NSException exceptionWithHTTPStatus:500 /* Server Error */
|
|
|
|
reason: reason];
|
|
|
|
}
|
|
|
|
[destFolder flushMailCaches];
|
|
|
|
|
|
|
|
/* a) copy */
|
|
|
|
|
2007-10-18 00:29:51 +02:00
|
|
|
return [self davCopyToTargetObject: destFolder
|
|
|
|
newName: @"fakeNewUnusedByIMAP4" /* autoassigned */
|
|
|
|
inContext:_ctx];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) moveToFolderNamed: (NSString *) folderName
|
|
|
|
inContext: (id)_ctx
|
|
|
|
{
|
|
|
|
NSException *error;
|
|
|
|
|
|
|
|
if (![self copyToFolderNamed: folderName
|
|
|
|
inContext: _ctx])
|
|
|
|
{
|
|
|
|
/* b) mark deleted */
|
2006-11-10 23:53:57 +01:00
|
|
|
|
2007-10-18 00:29:51 +02:00
|
|
|
error = [[self imap4Connection] markURLDeleted: [self imap4URL]];
|
|
|
|
if (error != nil) return error;
|
2006-11-10 23:53:57 +01:00
|
|
|
|
2007-10-18 00:29:51 +02:00
|
|
|
[self flushMailCaches];
|
|
|
|
}
|
2006-11-10 23:53:57 +01:00
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSException *) delete
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
/*
|
|
|
|
Note: delete is different to DELETEAction: for mails! The 'delete' runs
|
|
|
|
either flags a message as deleted or moves it to the Trash while
|
|
|
|
the DELETEAction: really deletes a message (by flagging it as
|
|
|
|
deleted _AND_ performing an expunge).
|
|
|
|
*/
|
|
|
|
// TODO: copy to Trash folder
|
|
|
|
NSException *error;
|
|
|
|
|
|
|
|
// TODO: check for safe HTTP method
|
|
|
|
|
|
|
|
error = [[self imap4Connection] markURLDeleted:[self imap4URL]];
|
|
|
|
return error;
|
|
|
|
}
|
2007-08-22 17:14:36 +02:00
|
|
|
|
|
|
|
- (id) DELETEAction: (id) _ctx
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSException *error;
|
|
|
|
|
|
|
|
// TODO: ensure safe HTTP method
|
|
|
|
|
|
|
|
error = [[self imap4Connection] markURLDeleted:[self imap4URL]];
|
|
|
|
if (error != nil) return error;
|
|
|
|
|
|
|
|
error = [[self imap4Connection] expungeAtURL:[[self container] imap4URL]];
|
|
|
|
if (error != nil) return error; // TODO: unflag as deleted?
|
|
|
|
|
|
|
|
return [NSNumber numberWithBool:YES]; /* delete was successful */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* some mail classification */
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (BOOL) isKolabObject
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSDictionary *h;
|
|
|
|
|
|
|
|
if ((h = [self mailHeaders]) != nil)
|
2007-08-06 20:14:53 +02:00
|
|
|
return [[h objectForKey: @"x-kolab-type"] isNotEmpty];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
// TODO: we could check the body structure?
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (BOOL) isMailingListMail
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSDictionary *h;
|
|
|
|
|
|
|
|
if ((h = [self mailHeaders]) == nil)
|
|
|
|
return NO;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
return [[h objectForKey: @"list-id"] isNotEmpty];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (BOOL) isVirusScanned
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSDictionary *h;
|
|
|
|
|
|
|
|
if ((h = [self mailHeaders]) == nil)
|
|
|
|
return NO;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
if (![[h objectForKey: @"x-virus-status"] isNotEmpty]) return NO;
|
|
|
|
if (![[h objectForKey: @"x-virus-scanned"] isNotEmpty]) return NO;
|
2006-06-15 21:34:10 +02:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSString *) scanListHeaderValue: (id) _value
|
|
|
|
forFieldWithPrefix: (NSString *) _prefix
|
2006-06-15 21:34:10 +02:00
|
|
|
{
|
|
|
|
/* Note: not very tolerant on embedded commands and <> */
|
|
|
|
// TODO: does not really belong here, should be a header-field-parser
|
|
|
|
NSRange r;
|
|
|
|
|
|
|
|
if (![_value isNotEmpty])
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
if ([_value isKindOfClass:[NSArray class]]) {
|
|
|
|
NSEnumerator *e;
|
|
|
|
id value;
|
|
|
|
|
|
|
|
e = [_value objectEnumerator];
|
|
|
|
while ((value = [e nextObject]) != nil) {
|
|
|
|
value = [self scanListHeaderValue:value forFieldWithPrefix:_prefix];
|
|
|
|
if (value != nil) return value;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (![_value isKindOfClass:[NSString class]])
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
/* check for commas in string values */
|
2007-08-06 20:14:53 +02:00
|
|
|
r = [_value rangeOfString: @","];
|
2006-06-15 21:34:10 +02:00
|
|
|
if (r.length > 0) {
|
2007-08-06 20:14:53 +02:00
|
|
|
return [self scanListHeaderValue:[_value componentsSeparatedByString: @","]
|
2006-06-15 21:34:10 +02:00
|
|
|
forFieldWithPrefix:_prefix];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* value qualifies */
|
|
|
|
if (![(NSString *)_value hasPrefix:_prefix])
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
/* unquote */
|
|
|
|
if ([_value characterAtIndex:0] == '<') {
|
2007-08-06 20:14:53 +02:00
|
|
|
r = [_value rangeOfString: @">"];
|
2006-06-15 21:34:10 +02:00
|
|
|
_value = (r.length == 0)
|
|
|
|
? [_value substringFromIndex:1]
|
|
|
|
: [_value substringWithRange:NSMakeRange(1, r.location - 2)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSString *) mailingListArchiveURL
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [self scanListHeaderValue:
|
2007-08-06 20:14:53 +02:00
|
|
|
[[self mailHeaders] objectForKey: @"list-archive"]
|
|
|
|
forFieldWithPrefix: @"<http://"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-08-22 17:14:36 +02:00
|
|
|
|
|
|
|
- (NSString *) mailingListSubscribeURL
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [self scanListHeaderValue:
|
2007-08-06 20:14:53 +02:00
|
|
|
[[self mailHeaders] objectForKey: @"list-subscribe"]
|
|
|
|
forFieldWithPrefix: @"<http://"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-08-22 17:14:36 +02:00
|
|
|
|
|
|
|
- (NSString *) mailingListUnsubscribeURL
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [self scanListHeaderValue:
|
2007-08-06 20:14:53 +02:00
|
|
|
[[self mailHeaders] objectForKey: @"list-unsubscribe"]
|
|
|
|
forFieldWithPrefix: @"<http://"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* etag support */
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (id) davEntityTag
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
/*
|
|
|
|
Note: There is one thing which *can* change for an existing message,
|
|
|
|
those are the IMAP4 flags (and annotations, which we do not use).
|
|
|
|
Since we don't render the flags, it should be OK, if this changes
|
|
|
|
we must embed the flagging into the etag.
|
|
|
|
*/
|
|
|
|
return mailETag;
|
|
|
|
}
|
2007-08-22 17:14:36 +02:00
|
|
|
|
|
|
|
- (int) zlGenerationCount
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return 0; /* mails never change */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Outlook mail tagging */
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (NSString *) outlookMessageClass
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSString *type;
|
|
|
|
|
2007-08-06 20:14:53 +02:00
|
|
|
if ((type = [[self mailHeaders] objectForKey: @"x-kolab-type"]) != nil) {
|
|
|
|
if ([type isEqualToString: @"application/x-vnd.kolab.contact"])
|
2006-06-15 21:34:10 +02:00
|
|
|
return @"IPM.Contact";
|
2007-08-06 20:14:53 +02:00
|
|
|
if ([type isEqualToString: @"application/x-vnd.kolab.task"])
|
2006-06-15 21:34:10 +02:00
|
|
|
return @"IPM.Task";
|
2007-08-06 20:14:53 +02:00
|
|
|
if ([type isEqualToString: @"application/x-vnd.kolab.event"])
|
2006-06-15 21:34:10 +02:00
|
|
|
return @"IPM.Appointment";
|
2007-08-06 20:14:53 +02:00
|
|
|
if ([type isEqualToString: @"application/x-vnd.kolab.note"])
|
2006-06-15 21:34:10 +02:00
|
|
|
return @"IPM.Note";
|
2007-08-06 20:14:53 +02:00
|
|
|
if ([type isEqualToString: @"application/x-vnd.kolab.journal"])
|
2006-06-15 21:34:10 +02:00
|
|
|
return @"IPM.Journal";
|
|
|
|
}
|
|
|
|
|
|
|
|
return @"IPM.Message"; /* email, default class */
|
|
|
|
}
|
|
|
|
|
2007-06-13 00:09:32 +02:00
|
|
|
- (NSArray *) aclsForUser: (NSString *) uid
|
|
|
|
{
|
|
|
|
return [container aclsForUser: uid];
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
/* debugging */
|
|
|
|
|
2007-08-22 17:14:36 +02:00
|
|
|
- (BOOL) isDebuggingEnabled
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return debugOn;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end /* SOGoMailObject */
|