2006-06-15 21:34:10 +02:00
|
|
|
/*
|
2014-01-07 16:28:53 +01:00
|
|
|
Copyright (C) 2007-2014 Inverse inc.
|
2006-06-15 21:34:10 +02:00
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
|
|
|
|
2009-04-01 15:49:24 +02:00
|
|
|
This file is part of SOGo.
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2009-04-01 15:49:24 +02: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.
|
|
|
|
|
2009-04-01 15:49:24 +02: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.
|
|
|
|
*/
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
#import <Foundation/NSURL.h>
|
2007-06-14 23:23:14 +02:00
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
|
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
|
|
|
#import <NGObjWeb/SoObject+SoDAV.h>
|
2007-08-22 17:13:06 +02:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
2007-06-14 23:23:14 +02:00
|
|
|
#import <NGObjWeb/WORequest+So.h>
|
|
|
|
#import <NGExtensions/NGBase64Coding.h>
|
|
|
|
#import <NGExtensions/NSFileManager+Extensions.h>
|
|
|
|
#import <NGExtensions/NGHashMap.h>
|
|
|
|
#import <NGExtensions/NSNull+misc.h>
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2007-08-17 04:11:34 +02:00
|
|
|
#import <NGExtensions/NSString+misc.h>
|
|
|
|
#import <NGImap4/NGImap4Connection.h>
|
|
|
|
#import <NGImap4/NGImap4Client.h>
|
2007-06-14 23:23:14 +02:00
|
|
|
#import <NGImap4/NGImap4Envelope.h>
|
|
|
|
#import <NGImap4/NGImap4EnvelopeAddress.h>
|
2014-09-17 17:51:46 +02:00
|
|
|
#import <NGMail/NGMailAddress.h>
|
2014-03-24 18:41:57 +01:00
|
|
|
#import <NGMail/NGMailAddressParser.h>
|
2007-06-14 23:23:14 +02:00
|
|
|
#import <NGMail/NGMimeMessage.h>
|
|
|
|
#import <NGMail/NGMimeMessageGenerator.h>
|
|
|
|
#import <NGMime/NGMimeBodyPart.h>
|
|
|
|
#import <NGMime/NGMimeFileData.h>
|
|
|
|
#import <NGMime/NGMimeMultipartBody.h>
|
|
|
|
#import <NGMime/NGMimeType.h>
|
2007-11-15 23:39:17 +01:00
|
|
|
#import <NGMime/NGMimeHeaderFields.h>
|
2007-06-14 23:23:14 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
#import <SOGo/NSArray+Utilities.h>
|
|
|
|
#import <SOGo/NSCalendarDate+SOGo.h>
|
|
|
|
#import <SOGo/NSString+Utilities.h>
|
2012-12-21 19:55:03 +01:00
|
|
|
#import <SOGo/SOGoBuild.h>
|
2009-11-29 05:19:32 +01:00
|
|
|
#import <SOGo/SOGoDomainDefaults.h>
|
|
|
|
#import <SOGo/SOGoMailer.h>
|
|
|
|
#import <SOGo/SOGoUser.h>
|
2015-08-03 18:16:16 +02:00
|
|
|
#import <SOGo/SOGoUserFolder.h>
|
2007-10-25 17:25:39 +02:00
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
#import <NGCards/NGVCard.h>
|
|
|
|
|
|
|
|
#import <Contacts/SOGoContactFolders.h>
|
|
|
|
#import <Contacts/SOGoContactGCSEntry.h>
|
|
|
|
|
2007-10-25 17:25:39 +02:00
|
|
|
#import "NSData+Mail.h"
|
2008-08-18 21:01:02 +02:00
|
|
|
#import "NSString+Mail.h"
|
2007-08-17 04:11:34 +02:00
|
|
|
#import "SOGoMailAccount.h"
|
|
|
|
#import "SOGoMailObject+Draft.h"
|
2010-01-06 01:20:19 +01:00
|
|
|
#import "SOGoSentFolder.h"
|
2007-06-14 23:23:14 +02:00
|
|
|
|
|
|
|
#import "SOGoDraftObject.h"
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2016-02-17 22:30:38 +01:00
|
|
|
|
2007-05-15 05:23:15 +02:00
|
|
|
static NSString *contentTypeValue = @"text/plain; charset=utf-8";
|
2009-06-23 19:53:18 +02:00
|
|
|
static NSString *htmlContentTypeValue = @"text/html; charset=utf-8";
|
2007-08-17 04:11:34 +02:00
|
|
|
static NSString *headerKeys[] = {@"subject", @"to", @"cc", @"bcc",
|
2007-11-01 14:53:56 +01:00
|
|
|
@"from", @"replyTo", @"message-id",
|
2007-08-28 23:43:06 +02:00
|
|
|
nil};
|
2007-05-11 19:58:29 +02:00
|
|
|
|
2010-01-22 17:33:35 +01:00
|
|
|
#warning -[NGImap4Connection postData:flags:toFolderURL:] should be enhanced \
|
|
|
|
to return at least the new uid
|
|
|
|
@interface NGImap4Connection (SOGoHiddenMethods)
|
|
|
|
|
|
|
|
- (NSString *) imap4FolderNameForURL: (NSURL *) url;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2011-02-24 00:06:55 +01:00
|
|
|
//
|
|
|
|
// Useful extension that comes from Pantomime which is also
|
|
|
|
// released under the LGPL. We should eventually merge
|
|
|
|
// this with the same category found in SOPE's NGSmtpClient.m
|
|
|
|
// or simply drop sope-mime in favor of Pantomime
|
|
|
|
//
|
|
|
|
@interface NSMutableData (DataCleanupExtension)
|
|
|
|
|
|
|
|
- (NSRange) rangeOfCString: (const char *) theCString;
|
|
|
|
- (NSRange) rangeOfCString: (const char *) theCString
|
|
|
|
options: (unsigned int) theOptions
|
|
|
|
range: (NSRange) theRange;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSMutableData (DataCleanupExtension)
|
|
|
|
|
|
|
|
- (NSRange) rangeOfCString: (const char *) theCString
|
|
|
|
{
|
|
|
|
return [self rangeOfCString: theCString
|
|
|
|
options: 0
|
|
|
|
range: NSMakeRange(0,[self length])];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSRange) rangeOfCString: (const char *) theCString
|
|
|
|
options: (unsigned int) theOptions
|
|
|
|
range: (NSRange) theRange
|
|
|
|
{
|
|
|
|
const char *b, *bytes;
|
|
|
|
int i, len, slen;
|
|
|
|
|
|
|
|
if (!theCString)
|
|
|
|
{
|
|
|
|
return NSMakeRange(NSNotFound,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = [self bytes];
|
|
|
|
len = [self length];
|
|
|
|
slen = strlen(theCString);
|
|
|
|
|
|
|
|
b = bytes;
|
|
|
|
|
|
|
|
if (len > theRange.location + theRange.length)
|
|
|
|
{
|
|
|
|
len = theRange.location + theRange.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (theOptions == NSCaseInsensitiveSearch)
|
|
|
|
{
|
|
|
|
i = theRange.location;
|
|
|
|
b += i;
|
|
|
|
|
|
|
|
for (; i <= len-slen; i++, b++)
|
|
|
|
{
|
|
|
|
if (!strncasecmp(theCString,b,slen))
|
|
|
|
{
|
|
|
|
return NSMakeRange(i,slen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i = theRange.location;
|
|
|
|
b += i;
|
|
|
|
|
|
|
|
for (; i <= len-slen; i++, b++)
|
|
|
|
{
|
|
|
|
if (!memcmp(theCString,b,slen))
|
|
|
|
{
|
|
|
|
return NSMakeRange(i,slen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSMakeRange(NSNotFound,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2006-06-15 21:34:10 +02:00
|
|
|
@implementation SOGoDraftObject
|
|
|
|
|
|
|
|
static NGMimeType *MultiMixedType = nil;
|
2013-09-19 22:14:08 +02:00
|
|
|
static NGMimeType *MultiAlternativeType = nil;
|
2015-10-23 20:34:19 +02:00
|
|
|
static NGMimeType *MultiRelatedType = nil;
|
2010-03-31 18:00:03 +02:00
|
|
|
static NSString *userAgent = nil;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
MultiMixedType = [NGMimeType mimeType: @"multipart" subType: @"mixed"];
|
|
|
|
[MultiMixedType retain];
|
2013-09-19 22:14:08 +02:00
|
|
|
|
|
|
|
MultiAlternativeType = [NGMimeType mimeType: @"multipart" subType: @"alternative"];
|
|
|
|
[MultiAlternativeType retain];
|
|
|
|
|
2015-10-23 20:34:19 +02:00
|
|
|
MultiRelatedType = [NGMimeType mimeType: @"multipart" subType: @"related"];
|
|
|
|
[MultiRelatedType retain];
|
|
|
|
|
2012-12-21 19:55:03 +01:00
|
|
|
userAgent = [NSString stringWithFormat: @"SOGoMail %@",
|
|
|
|
SOGoVersion];
|
2010-03-31 18:00:03 +02:00
|
|
|
[userAgent retain];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2014-07-09 17:17:36 +02:00
|
|
|
sourceIMAP4ID = -1;
|
2007-08-17 04:11:34 +02:00
|
|
|
IMAP4ID = -1;
|
|
|
|
headers = [NSMutableDictionary new];
|
|
|
|
text = @"";
|
2008-08-29 02:31:27 +02:00
|
|
|
path = nil;
|
2007-08-17 04:11:34 +02:00
|
|
|
sourceURL = nil;
|
|
|
|
sourceFlag = nil;
|
2007-08-28 23:43:06 +02:00
|
|
|
inReplyTo = nil;
|
2010-11-23 14:53:54 +01:00
|
|
|
isHTML = NO;
|
2007-08-17 04:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[headers release];
|
|
|
|
[text release];
|
2007-05-11 19:58:29 +02:00
|
|
|
[path release];
|
2007-08-17 04:11:34 +02:00
|
|
|
[sourceURL release];
|
|
|
|
[sourceFlag release];
|
2007-08-28 23:43:06 +02:00
|
|
|
[inReplyTo release];
|
2006-06-15 21:34:10 +02:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draft folder functionality */
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSString *) userSpoolFolderPath
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [[self container] userSpoolFolderPath];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draft object functionality */
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSString *) draftFolderPath
|
|
|
|
{
|
|
|
|
if (!path)
|
|
|
|
{
|
2014-07-09 17:17:36 +02:00
|
|
|
path = [[self userSpoolFolderPath] stringByAppendingPathComponent: nameInContainer];
|
2007-08-17 04:11:34 +02:00
|
|
|
[path retain];
|
|
|
|
}
|
|
|
|
|
2007-05-11 19:58:29 +02:00
|
|
|
return path;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
|
|
|
|
- (BOOL) _ensureDraftFolderPath
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSFileManager *fm;
|
2007-08-17 04:11:34 +02:00
|
|
|
|
|
|
|
fm = [NSFileManager defaultManager];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
return ([fm createDirectoriesAtPath: [container userSpoolFolderPath]
|
|
|
|
attributes: nil]
|
|
|
|
&& [fm createDirectoriesAtPath: [self draftFolderPath]
|
|
|
|
attributes:nil]);
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSString *) infoPath
|
|
|
|
{
|
|
|
|
return [[self draftFolderPath]
|
|
|
|
stringByAppendingPathComponent: @".info.plist"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* contents */
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (void) setHeaders: (NSDictionary *) newHeaders
|
|
|
|
{
|
|
|
|
id headerValue;
|
|
|
|
unsigned int count;
|
2014-05-28 17:19:31 +02:00
|
|
|
NSString *messageID, *priority, *pureSender, *replyTo, *receipt;
|
2007-08-17 04:11:34 +02:00
|
|
|
|
2007-11-01 14:53:56 +01:00
|
|
|
for (count = 0; count < 8; count++)
|
2007-08-17 04:11:34 +02:00
|
|
|
{
|
|
|
|
headerValue = [newHeaders objectForKey: headerKeys[count]];
|
|
|
|
if (headerValue)
|
|
|
|
[headers setObject: headerValue
|
|
|
|
forKey: headerKeys[count]];
|
2007-11-01 21:20:57 +01:00
|
|
|
else if ([headers objectForKey: headerKeys[count]])
|
2007-08-17 04:11:34 +02:00
|
|
|
[headers removeObjectForKey: headerKeys[count]];
|
|
|
|
}
|
2007-11-01 14:53:56 +01:00
|
|
|
|
|
|
|
messageID = [headers objectForKey: @"message-id"];
|
|
|
|
if (!messageID)
|
|
|
|
{
|
2014-02-07 16:51:42 +01:00
|
|
|
messageID = [NSString generateMessageID];
|
2007-11-01 14:53:56 +01:00
|
|
|
[headers setObject: messageID forKey: @"message-id"];
|
|
|
|
}
|
2008-09-28 16:02:40 +02:00
|
|
|
|
2014-05-28 17:19:31 +02:00
|
|
|
priority = [newHeaders objectForKey: @"X-Priority"];
|
|
|
|
if (priority)
|
2008-09-28 16:02:40 +02:00
|
|
|
{
|
2014-05-28 17:19:31 +02:00
|
|
|
// newHeaders come from MIME message; convert X-Priority to Web representation
|
|
|
|
[headers setObject: priority forKey: @"X-Priority"];
|
|
|
|
[headers removeObjectForKey: @"priority"];
|
|
|
|
if ([priority isEqualToString: @"1 (Highest)"])
|
|
|
|
{
|
|
|
|
[headers setObject: @"HIGHEST" forKey: @"priority"];
|
|
|
|
}
|
|
|
|
else if ([priority isEqualToString: @"2 (High)"])
|
|
|
|
{
|
|
|
|
[headers setObject: @"HIGH" forKey: @"priority"];
|
|
|
|
}
|
|
|
|
else if ([priority isEqualToString: @"4 (Low)"])
|
|
|
|
{
|
|
|
|
[headers setObject: @"LOW" forKey: @"priority"];
|
|
|
|
}
|
|
|
|
else if ([priority isEqualToString: @"5 (Lowest)"])
|
|
|
|
{
|
|
|
|
[headers setObject: @"LOWEST" forKey: @"priority"];
|
|
|
|
}
|
2008-09-28 16:02:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-28 17:19:31 +02:00
|
|
|
// newHeaders come from Web form; convert priority to MIME header representation
|
|
|
|
priority = [newHeaders objectForKey: @"priority"];
|
2016-02-25 17:52:12 +01:00
|
|
|
if (!priority || ![priority length] || [priority intValue] == 3)
|
2014-05-28 17:19:31 +02:00
|
|
|
{
|
|
|
|
[headers removeObjectForKey: @"X-Priority"];
|
|
|
|
}
|
2015-07-07 14:35:53 +02:00
|
|
|
else if ([priority intValue] == 1)
|
2014-05-28 17:19:31 +02:00
|
|
|
{
|
|
|
|
[headers setObject: @"1 (Highest)" forKey: @"X-Priority"];
|
|
|
|
}
|
2015-07-07 14:35:53 +02:00
|
|
|
else if ([priority intValue] == 2)
|
2014-05-28 17:19:31 +02:00
|
|
|
{
|
|
|
|
[headers setObject: @"2 (High)" forKey: @"X-Priority"];
|
|
|
|
}
|
2015-07-07 14:35:53 +02:00
|
|
|
else if ([priority intValue] == 4)
|
2014-05-28 17:19:31 +02:00
|
|
|
{
|
|
|
|
[headers setObject: @"4 (Low)" forKey: @"X-Priority"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[headers setObject: @"5 (Lowest)" forKey: @"X-Priority"];
|
|
|
|
}
|
|
|
|
if (priority)
|
|
|
|
{
|
|
|
|
[headers setObject: priority forKey: @"priority"];
|
|
|
|
}
|
2008-09-28 16:02:40 +02:00
|
|
|
}
|
2010-08-31 15:49:31 +02:00
|
|
|
|
2012-04-20 15:27:21 +02:00
|
|
|
replyTo = [headers objectForKey: @"replyTo"];
|
|
|
|
if ([replyTo length] > 0)
|
|
|
|
{
|
|
|
|
[headers setObject: replyTo forKey: @"reply-to"];
|
|
|
|
}
|
2012-11-12 21:32:03 +01:00
|
|
|
[headers removeObjectForKey: @"replyTo"];
|
2012-04-20 15:27:21 +02:00
|
|
|
|
2014-05-28 17:19:31 +02:00
|
|
|
receipt = [newHeaders objectForKey: @"Disposition-Notification-To"];
|
|
|
|
if ([receipt length] > 0)
|
2010-08-31 15:49:31 +02:00
|
|
|
{
|
2014-05-28 17:19:31 +02:00
|
|
|
[headers setObject: @"true" forKey: @"receipt"];
|
|
|
|
[headers setObject: receipt forKey: @"Disposition-Notification-To"];
|
2010-08-31 15:49:31 +02:00
|
|
|
}
|
|
|
|
else
|
2014-05-28 17:19:31 +02:00
|
|
|
{
|
|
|
|
receipt = [newHeaders objectForKey: @"receipt"];
|
2015-07-07 14:35:53 +02:00
|
|
|
if ([receipt boolValue])
|
2014-05-28 17:19:31 +02:00
|
|
|
{
|
|
|
|
[headers setObject: receipt forKey: @"receipt"];
|
|
|
|
pureSender = [[newHeaders objectForKey: @"from"] pureEMailAddress];
|
|
|
|
if (pureSender)
|
|
|
|
{
|
|
|
|
[headers setObject: pureSender forKey: @"Disposition-Notification-To"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[headers removeObjectForKey: @"receipt"];
|
|
|
|
[headers removeObjectForKey: @"Disposition-Notification-To"];
|
|
|
|
}
|
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *) headers
|
|
|
|
{
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setText: (NSString *) newText
|
|
|
|
{
|
|
|
|
ASSIGN (text, newText);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) text
|
|
|
|
{
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2010-11-23 14:53:54 +01:00
|
|
|
- (void) setIsHTML: (BOOL) aBool
|
|
|
|
{
|
|
|
|
isHTML = aBool;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isHTML
|
|
|
|
{
|
|
|
|
return isHTML;
|
|
|
|
}
|
|
|
|
|
2014-05-30 19:53:20 +02:00
|
|
|
- (NSString *) inReplyTo
|
|
|
|
{
|
|
|
|
return inReplyTo;
|
|
|
|
}
|
|
|
|
|
2007-08-28 23:43:06 +02:00
|
|
|
- (void) setInReplyTo: (NSString *) newInReplyTo
|
|
|
|
{
|
|
|
|
ASSIGN (inReplyTo, newInReplyTo);
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (void) setSourceURL: (NSString *) newSourceURL
|
|
|
|
{
|
|
|
|
ASSIGN (sourceURL, newSourceURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setSourceFlag: (NSString *) newSourceFlag
|
|
|
|
{
|
|
|
|
ASSIGN (sourceFlag, newSourceFlag);
|
|
|
|
}
|
|
|
|
|
2010-06-25 21:58:30 +02:00
|
|
|
- (void) setSourceFolder: (NSString *) newSourceFolder
|
|
|
|
{
|
|
|
|
ASSIGN (sourceFolder, newSourceFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setSourceFolderWithMailObject: (SOGoMailObject *) sourceMail
|
|
|
|
{
|
|
|
|
NSMutableArray *paths;
|
|
|
|
id parent;
|
|
|
|
|
|
|
|
parent = [sourceMail container];
|
|
|
|
paths = [NSMutableArray arrayWithCapacity: 1];
|
|
|
|
while (parent && ![parent isKindOfClass: [SOGoMailAccount class]])
|
|
|
|
{
|
|
|
|
[paths insertObject: [parent nameInContainer] atIndex: 0];
|
|
|
|
parent = [parent container];
|
|
|
|
}
|
|
|
|
if (parent)
|
|
|
|
[paths insertObject: [NSString stringWithFormat: @"/%@", [parent nameInContainer]]
|
|
|
|
atIndex: 0];
|
|
|
|
|
|
|
|
[self setSourceFolder: [paths componentsJoinedByString: @"/"]];
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2010-06-25 21:58:30 +02:00
|
|
|
- (NSString *) sourceFolder
|
|
|
|
{
|
|
|
|
return sourceFolder;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
2014-01-08 20:40:39 +01:00
|
|
|
// Store the message definition in a plist file (.info.plist) in the spool directory
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSException *) storeInfo
|
|
|
|
{
|
|
|
|
NSMutableDictionary *infos;
|
|
|
|
NSException *error;
|
|
|
|
|
|
|
|
if ([self _ensureDraftFolderPath])
|
|
|
|
{
|
2009-10-13 16:10:17 +02:00
|
|
|
infos = [NSMutableDictionary dictionary];
|
2007-08-17 04:11:34 +02:00
|
|
|
[infos setObject: headers forKey: @"headers"];
|
|
|
|
if (text)
|
|
|
|
[infos setObject: text forKey: @"text"];
|
2010-11-23 14:53:54 +01:00
|
|
|
[infos setObject: [NSNumber numberWithBool: isHTML]
|
|
|
|
forKey: @"isHTML"];
|
2007-08-28 23:43:06 +02:00
|
|
|
if (inReplyTo)
|
|
|
|
[infos setObject: inReplyTo forKey: @"inReplyTo"];
|
2014-07-09 17:17:36 +02:00
|
|
|
if (sourceIMAP4ID > -1)
|
|
|
|
[infos setObject: [NSString stringWithFormat: @"%i", sourceIMAP4ID]
|
|
|
|
forKey: @"sourceIMAP4ID"];
|
2007-08-17 04:11:34 +02:00
|
|
|
if (IMAP4ID > -1)
|
2010-06-25 21:58:30 +02:00
|
|
|
[infos setObject: [NSString stringWithFormat: @"%i", IMAP4ID]
|
|
|
|
forKey: @"IMAP4ID"];
|
|
|
|
if (sourceURL && sourceFlag && sourceFolder)
|
2007-08-17 04:11:34 +02:00
|
|
|
{
|
|
|
|
[infos setObject: sourceURL forKey: @"sourceURL"];
|
|
|
|
[infos setObject: sourceFlag forKey: @"sourceFlag"];
|
2010-06-25 21:58:30 +02:00
|
|
|
[infos setObject: sourceFolder forKey: @"sourceFolder"];
|
2007-08-17 04:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ([infos writeToFile: [self infoPath] atomically:YES])
|
|
|
|
error = nil;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"could not write info: '%@'",
|
|
|
|
[self infoPath]];
|
|
|
|
error = [NSException exceptionWithHTTPStatus:500 /* server error */
|
|
|
|
reason: @"could not write draft info!"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"could not create folder for draft: '%@'",
|
2006-06-15 21:34:10 +02:00
|
|
|
[self draftFolderPath]];
|
2007-08-17 04:11:34 +02:00
|
|
|
error = [NSException exceptionWithHTTPStatus:500 /* server error */
|
|
|
|
reason: @"could not create folder for draft!"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (void) _loadInfosFromDictionary: (NSDictionary *) infoDict
|
|
|
|
{
|
|
|
|
id value;
|
|
|
|
|
|
|
|
value = [infoDict objectForKey: @"headers"];
|
|
|
|
if (value)
|
|
|
|
[self setHeaders: value];
|
|
|
|
|
|
|
|
value = [infoDict objectForKey: @"text"];
|
|
|
|
if ([value length] > 0)
|
|
|
|
[self setText: value];
|
2010-11-23 14:53:54 +01:00
|
|
|
isHTML = [[infoDict objectForKey: @"isHTML"] boolValue];
|
2007-08-17 04:11:34 +02:00
|
|
|
|
2014-07-09 17:17:36 +02:00
|
|
|
value = [infoDict objectForKey: @"sourceIMAP4ID"];
|
|
|
|
if (value)
|
|
|
|
[self setSourceIMAP4ID: [value intValue]];
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
value = [infoDict objectForKey: @"IMAP4ID"];
|
|
|
|
if (value)
|
|
|
|
[self setIMAP4ID: [value intValue]];
|
|
|
|
|
|
|
|
value = [infoDict objectForKey: @"sourceURL"];
|
|
|
|
if (value)
|
|
|
|
[self setSourceURL: value];
|
|
|
|
value = [infoDict objectForKey: @"sourceFlag"];
|
|
|
|
if (value)
|
|
|
|
[self setSourceFlag: value];
|
2010-06-25 21:58:30 +02:00
|
|
|
value = [infoDict objectForKey: @"sourceFolder"];
|
|
|
|
if (value)
|
|
|
|
[self setSourceFolder: value];
|
2007-08-28 23:43:06 +02:00
|
|
|
|
|
|
|
value = [infoDict objectForKey: @"inReplyTo"];
|
|
|
|
if (value)
|
|
|
|
[self setInReplyTo: value];
|
2007-08-17 04:11:34 +02:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSString *) relativeImap4Name
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"%d", IMAP4ID];
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (void) fetchInfo
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSString *p;
|
2007-08-17 04:11:34 +02:00
|
|
|
NSDictionary *infos;
|
|
|
|
NSFileManager *fm;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
p = [self infoPath];
|
2007-08-17 04:11:34 +02:00
|
|
|
|
|
|
|
fm = [NSFileManager defaultManager];
|
|
|
|
if ([fm fileExistsAtPath: p])
|
|
|
|
{
|
|
|
|
infos = [NSDictionary dictionaryWithContentsOfFile: p];
|
|
|
|
if (infos)
|
|
|
|
[self _loadInfosFromDictionary: infos];
|
|
|
|
// else
|
|
|
|
// [self errorWithFormat: @"draft info dictionary broken at path: %@", p];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self debugWithFormat: @"Note: info object does not yet exist: %@", p];
|
|
|
|
}
|
|
|
|
|
2014-07-09 17:17:36 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
- (void) setSourceIMAP4ID: (int) newSourceIMAP4ID
|
|
|
|
{
|
|
|
|
sourceIMAP4ID = newSourceIMAP4ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
- (int) sourceIMAP4ID
|
|
|
|
{
|
|
|
|
return sourceIMAP4ID;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (void) setIMAP4ID: (int) newIMAP4ID
|
|
|
|
{
|
|
|
|
IMAP4ID = newIMAP4ID;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (int) IMAP4ID
|
|
|
|
{
|
|
|
|
return IMAP4ID;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSException *) save
|
|
|
|
{
|
|
|
|
NGImap4Client *client;
|
|
|
|
NSException *error;
|
|
|
|
NSData *message;
|
|
|
|
NSString *folder;
|
|
|
|
id result;
|
|
|
|
|
|
|
|
error = nil;
|
|
|
|
message = [self mimeMessageAsData];
|
|
|
|
|
|
|
|
client = [[self imap4Connection] client];
|
2009-03-13 20:48:15 +01:00
|
|
|
|
|
|
|
if (![imap4 doesMailboxExistAtURL: [container imap4URL]])
|
|
|
|
{
|
2011-01-27 15:57:04 +01:00
|
|
|
[[self imap4Connection] createMailbox: [[self imap4Connection] imap4FolderNameForURL: [container imap4URL]]
|
|
|
|
atURL: [[self mailAccountFolder] imap4URL]];
|
2009-03-13 20:48:15 +01:00
|
|
|
[imap4 flushFolderHierarchyCache];
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
folder = [imap4 imap4FolderNameForURL: [container imap4URL]];
|
2013-12-18 20:12:29 +01:00
|
|
|
result = [client append: message toFolder: folder
|
2014-12-11 19:31:32 +01:00
|
|
|
withFlags: [NSArray arrayWithObjects: @"draft", nil]];
|
2007-08-17 04:11:34 +02:00
|
|
|
if ([[result objectForKey: @"result"] boolValue])
|
|
|
|
{
|
|
|
|
if (IMAP4ID > -1)
|
|
|
|
error = [imap4 markURLDeleted: [self imap4URL]];
|
2014-07-09 17:17:36 +02:00
|
|
|
[self setIMAP4ID: [self IMAP4IDFromAppendResult: result]];
|
2013-12-18 20:12:29 +01:00
|
|
|
if (imap4URL)
|
|
|
|
{
|
|
|
|
// Invalidate the IMAP message URL since the message ID has changed
|
|
|
|
[imap4URL release];
|
|
|
|
imap4URL = nil;
|
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
}
|
|
|
|
else
|
2013-12-18 20:12:29 +01:00
|
|
|
error = [NSException exceptionWithHTTPStatus: 500 /* Server Error */
|
|
|
|
reason: [result objectForKey: @"reason"]];
|
2007-08-17 04:11:34 +02:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (void) _addEMailsOfAddresses: (NSArray *) _addrs
|
|
|
|
toArray: (NSMutableArray *) _ma
|
|
|
|
{
|
2007-11-12 18:08:12 +01:00
|
|
|
NSEnumerator *addresses;
|
|
|
|
NGImap4EnvelopeAddress *currentAddress;
|
2007-08-17 04:11:34 +02:00
|
|
|
|
2007-11-12 18:08:12 +01:00
|
|
|
addresses = [_addrs objectEnumerator];
|
|
|
|
while ((currentAddress = [addresses nextObject]))
|
2008-08-29 19:33:19 +02:00
|
|
|
if ([currentAddress email])
|
|
|
|
[_ma addObject: [currentAddress email]];
|
2007-11-12 18:08:12 +01:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-11-12 18:08:12 +01:00
|
|
|
- (void) _addRecipients: (NSArray *) recipients
|
|
|
|
toArray: (NSMutableArray *) array
|
|
|
|
{
|
|
|
|
NSEnumerator *addresses;
|
|
|
|
NGImap4EnvelopeAddress *currentAddress;
|
|
|
|
|
|
|
|
addresses = [recipients objectEnumerator];
|
|
|
|
while ((currentAddress = [addresses nextObject]))
|
2008-08-29 19:33:19 +02:00
|
|
|
if ([currentAddress baseEMail])
|
|
|
|
[array addObject: [currentAddress baseEMail]];
|
2007-11-12 18:08:12 +01:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-11-12 18:08:12 +01:00
|
|
|
- (void) _purgeRecipients: (NSArray *) recipients
|
|
|
|
fromAddresses: (NSMutableArray *) addresses
|
|
|
|
{
|
|
|
|
NSEnumerator *allRecipients;
|
|
|
|
NSString *currentRecipient;
|
|
|
|
NGImap4EnvelopeAddress *currentAddress;
|
|
|
|
int count, max;
|
|
|
|
|
|
|
|
max = [addresses count];
|
|
|
|
|
|
|
|
allRecipients = [recipients objectEnumerator];
|
|
|
|
while (max > 0
|
|
|
|
&& ((currentRecipient = [allRecipients nextObject])))
|
|
|
|
for (count = max - 1; count >= 0; count--)
|
|
|
|
{
|
|
|
|
currentAddress = [addresses objectAtIndex: count];
|
2015-10-23 18:44:43 +02:00
|
|
|
if (![currentAddress baseEMail] ||
|
|
|
|
[currentRecipient
|
2010-06-16 19:50:37 +02:00
|
|
|
caseInsensitiveCompare: [currentAddress baseEMail]]
|
|
|
|
== NSOrderedSame)
|
2007-11-12 18:08:12 +01:00
|
|
|
{
|
|
|
|
[addresses removeObjectAtIndex: count];
|
|
|
|
max--;
|
|
|
|
}
|
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (void) _fillInReplyAddresses: (NSMutableDictionary *) _info
|
|
|
|
replyToAll: (BOOL) _replyToAll
|
|
|
|
envelope: (NGImap4Envelope *) _envelope
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
The rules as implemented by Thunderbird:
|
|
|
|
- if there is a 'reply-to' header, only include that (as TO)
|
|
|
|
- if we reply to all, all non-from addresses are added as CC
|
|
|
|
- the from is always the lone TO (except for reply-to)
|
|
|
|
|
|
|
|
Note: we cannot check reply-to, because Cyrus even sets a reply-to in the
|
|
|
|
envelope if none is contained in the message itself! (bug or
|
|
|
|
feature?)
|
|
|
|
|
|
|
|
TODO: what about sender (RFC 822 3.6.2)
|
|
|
|
*/
|
2007-11-12 18:08:12 +01:00
|
|
|
NSMutableArray *to, *addrs, *allRecipients;
|
2013-11-19 17:39:45 +01:00
|
|
|
NSArray *envelopeAddresses;
|
2007-08-17 04:11:34 +02:00
|
|
|
|
2009-10-13 16:10:17 +02:00
|
|
|
allRecipients = [NSMutableArray array];
|
2013-11-19 17:39:45 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// When we do a Reply-To or a Reply-To-All, we strip our own addresses
|
|
|
|
// from the list of recipients so we don't reply to ourself! We check
|
|
|
|
// which addresses we should use - that is the ones for the current
|
|
|
|
// user if we're dealing with the default "SOGo mail account" or
|
|
|
|
// the ones specified in the auxiliary IMAP accounts
|
|
|
|
//
|
|
|
|
if ([[[self->container mailAccountFolder] nameInContainer] intValue] == 0)
|
|
|
|
{
|
|
|
|
NSArray *userEmails;
|
|
|
|
|
|
|
|
userEmails = [[context activeUser] allEmails];
|
|
|
|
[allRecipients addObjectsFromArray: userEmails];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSArray *identities;
|
|
|
|
NSString *email;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
identities = [[[self container] mailAccountFolder] identities];
|
|
|
|
|
|
|
|
for (i = 0; i < [identities count]; i++)
|
|
|
|
{
|
|
|
|
email = [[identities objectAtIndex: i] objectForKey: @"email"];
|
|
|
|
|
|
|
|
if (email)
|
|
|
|
[allRecipients addObject: email];
|
|
|
|
}
|
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
|
2007-11-12 18:08:12 +01:00
|
|
|
to = [NSMutableArray arrayWithCapacity: 2];
|
|
|
|
|
2009-10-13 16:10:17 +02:00
|
|
|
addrs = [NSMutableArray array];
|
2007-11-12 18:08:12 +01:00
|
|
|
envelopeAddresses = [_envelope replyTo];
|
|
|
|
if ([envelopeAddresses count])
|
|
|
|
[addrs setArray: envelopeAddresses];
|
|
|
|
else
|
|
|
|
[addrs setArray: [_envelope from]];
|
|
|
|
|
2013-11-19 17:39:45 +01:00
|
|
|
[self _purgeRecipients: allRecipients fromAddresses: addrs];
|
|
|
|
[self _addEMailsOfAddresses: addrs toArray: to];
|
|
|
|
[self _addRecipients: addrs toArray: allRecipients];
|
|
|
|
[_info setObject: to forKey: @"to"];
|
2007-08-17 04:11:34 +02:00
|
|
|
|
2007-11-26 15:23:35 +01:00
|
|
|
/* If "to" is empty, we add at least ourself as a recipient!
|
|
|
|
This is for emails in the "Sent" folder that we reply to... */
|
2007-11-26 02:28:40 +01:00
|
|
|
if (![to count])
|
|
|
|
{
|
|
|
|
if ([[_envelope replyTo] count])
|
2013-11-19 17:39:45 +01:00
|
|
|
[self _addEMailsOfAddresses: [_envelope replyTo] toArray: to];
|
2007-11-26 02:28:40 +01:00
|
|
|
else
|
2013-11-19 17:39:45 +01:00
|
|
|
[self _addEMailsOfAddresses: [_envelope from] toArray: to];
|
2007-11-26 02:28:40 +01:00
|
|
|
}
|
|
|
|
|
2007-12-03 15:13:10 +01:00
|
|
|
/* If we have no To but we have Cc recipients, let's move the Cc
|
|
|
|
to the To bucket... */
|
|
|
|
if ([[_info objectForKey: @"to"] count] == 0 && [_info objectForKey: @"cc"])
|
|
|
|
{
|
|
|
|
id o;
|
|
|
|
|
|
|
|
o = [_info objectForKey: @"cc"];
|
|
|
|
[_info setObject: o forKey: @"to"];
|
|
|
|
[_info removeObjectForKey: @"cc"];
|
|
|
|
}
|
|
|
|
|
2013-11-19 17:39:45 +01:00
|
|
|
/* CC processing if we reply-to-all: - we add all 'to' and 'cc' fields */
|
2007-12-05 22:02:57 +01:00
|
|
|
if (_replyToAll)
|
|
|
|
{
|
|
|
|
to = [NSMutableArray new];
|
|
|
|
|
|
|
|
[addrs setArray: [_envelope to]];
|
|
|
|
[self _purgeRecipients: allRecipients
|
|
|
|
fromAddresses: addrs];
|
|
|
|
[self _addEMailsOfAddresses: addrs toArray: to];
|
|
|
|
[self _addRecipients: addrs toArray: allRecipients];
|
|
|
|
|
|
|
|
[addrs setArray: [_envelope cc]];
|
|
|
|
[self _purgeRecipients: allRecipients
|
|
|
|
fromAddresses: addrs];
|
|
|
|
[self _addEMailsOfAddresses: addrs toArray: to];
|
|
|
|
|
|
|
|
[_info setObject: to forKey: @"cc"];
|
|
|
|
|
|
|
|
[to release];
|
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-10-25 17:25:39 +02:00
|
|
|
- (NSArray *) _attachmentBodiesFromPaths: (NSArray *) paths
|
|
|
|
fromResponseFetch: (NSDictionary *) fetch;
|
|
|
|
{
|
|
|
|
NSEnumerator *attachmentKeys;
|
|
|
|
NSMutableArray *bodies;
|
|
|
|
NSString *currentKey;
|
|
|
|
NSDictionary *body;
|
|
|
|
|
|
|
|
bodies = [NSMutableArray array];
|
|
|
|
|
|
|
|
attachmentKeys = [paths objectEnumerator];
|
|
|
|
while ((currentKey = [attachmentKeys nextObject]))
|
|
|
|
{
|
|
|
|
body = [fetch objectForKey: [currentKey lowercaseString]];
|
2016-03-18 17:54:00 +01:00
|
|
|
if (body)
|
|
|
|
[bodies addObject: [body objectForKey: @"data"]];
|
|
|
|
else
|
|
|
|
[bodies addObject: [NSData data]];
|
2007-10-25 17:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return bodies;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2014-01-08 20:40:39 +01:00
|
|
|
- (void) _fetchAttachmentsFromMail: (SOGoMailObject *) sourceMail
|
2007-08-18 22:33:23 +02:00
|
|
|
{
|
|
|
|
unsigned int count, max;
|
2014-01-08 20:40:39 +01:00
|
|
|
NSArray *parts, *paths, *bodies;
|
2007-10-25 17:25:39 +02:00
|
|
|
NSData *body;
|
|
|
|
NSDictionary *currentInfo;
|
|
|
|
NGHashMap *response;
|
2007-08-18 22:33:23 +02:00
|
|
|
|
2014-01-08 20:40:39 +01:00
|
|
|
parts = [sourceMail fetchFileAttachmentKeys];
|
2007-08-18 22:33:23 +02:00
|
|
|
max = [parts count];
|
|
|
|
if (max > 0)
|
|
|
|
{
|
|
|
|
paths = [parts keysWithFormat: @"BODY[%{path}]"];
|
2007-10-25 17:25:39 +02:00
|
|
|
response = [[sourceMail fetchParts: paths] objectForKey: @"RawResponse"];
|
|
|
|
bodies = [self _attachmentBodiesFromPaths: paths
|
|
|
|
fromResponseFetch: [response objectForKey: @"fetch"]];
|
2007-08-18 22:33:23 +02:00
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
2007-10-25 17:25:39 +02:00
|
|
|
currentInfo = [parts objectAtIndex: count];
|
|
|
|
body = [[bodies objectAtIndex: count]
|
|
|
|
bodyDataFromEncoding: [currentInfo
|
|
|
|
objectForKey: @"encoding"]];
|
|
|
|
[self saveAttachment: body withMetadata: currentInfo];
|
2007-08-18 22:33:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-18 22:33:23 +02:00
|
|
|
- (void) fetchMailForEditing: (SOGoMailObject *) sourceMail
|
|
|
|
{
|
2007-11-01 14:53:56 +01:00
|
|
|
NSString *subject, *msgid;
|
2007-08-18 22:33:23 +02:00
|
|
|
NSMutableDictionary *info;
|
2014-05-28 17:19:31 +02:00
|
|
|
NSDictionary *h;
|
2007-08-18 22:33:23 +02:00
|
|
|
NSMutableArray *addresses;
|
|
|
|
NGImap4Envelope *sourceEnvelope;
|
2014-05-28 17:19:31 +02:00
|
|
|
id priority, receipt;
|
2007-08-18 22:33:23 +02:00
|
|
|
|
|
|
|
[sourceMail fetchCoreInfos];
|
|
|
|
|
2014-01-08 20:40:39 +01:00
|
|
|
[self _fetchAttachmentsFromMail: sourceMail];
|
2007-08-18 22:33:23 +02:00
|
|
|
info = [NSMutableDictionary dictionaryWithCapacity: 16];
|
|
|
|
subject = [sourceMail subject];
|
|
|
|
if ([subject length] > 0)
|
|
|
|
[info setObject: subject forKey: @"subject"];
|
|
|
|
|
|
|
|
sourceEnvelope = [sourceMail envelope];
|
2007-11-01 14:53:56 +01:00
|
|
|
msgid = [sourceEnvelope messageID];
|
|
|
|
if ([msgid length] > 0)
|
|
|
|
[info setObject: msgid forKey: @"message-id"];
|
|
|
|
|
2007-08-18 22:33:23 +02:00
|
|
|
addresses = [NSMutableArray array];
|
|
|
|
[self _addEMailsOfAddresses: [sourceEnvelope to] toArray: addresses];
|
|
|
|
[info setObject: addresses forKey: @"to"];
|
|
|
|
addresses = [NSMutableArray array];
|
|
|
|
[self _addEMailsOfAddresses: [sourceEnvelope cc] toArray: addresses];
|
|
|
|
if ([addresses count] > 0)
|
|
|
|
[info setObject: addresses forKey: @"cc"];
|
|
|
|
addresses = [NSMutableArray array];
|
|
|
|
[self _addEMailsOfAddresses: [sourceEnvelope bcc] toArray: addresses];
|
|
|
|
if ([addresses count] > 0)
|
|
|
|
[info setObject: addresses forKey: @"bcc"];
|
|
|
|
addresses = [NSMutableArray array];
|
|
|
|
[self _addEMailsOfAddresses: [sourceEnvelope replyTo] toArray: addresses];
|
|
|
|
if ([addresses count] > 0)
|
|
|
|
[info setObject: addresses forKey: @"replyTo"];
|
2014-05-28 17:19:31 +02:00
|
|
|
|
|
|
|
h = [sourceMail mailHeaders];
|
|
|
|
priority = [h objectForKey: @"x-priority"];
|
|
|
|
if ([priority isNotEmpty] && [priority isKindOfClass: [NSString class]])
|
|
|
|
[info setObject: (NSString*)priority forKey: @"X-Priority"];
|
|
|
|
receipt = [h objectForKey: @"disposition-notification-to"];
|
|
|
|
if ([receipt isNotEmpty] && [receipt isKindOfClass: [NSString class]])
|
|
|
|
[info setObject: (NSString*)receipt forKey: @"Disposition-Notification-To"];
|
|
|
|
|
2007-08-18 22:33:23 +02:00
|
|
|
[self setHeaders: info];
|
|
|
|
|
|
|
|
[self setText: [sourceMail contentForEditing]];
|
2010-06-25 21:58:30 +02:00
|
|
|
[self setIMAP4ID: [[sourceMail nameInContainer] intValue]];
|
2007-08-18 22:33:23 +02:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (void) fetchMailForReplying: (SOGoMailObject *) sourceMail
|
|
|
|
toAll: (BOOL) toAll
|
|
|
|
{
|
2014-01-08 20:40:39 +01:00
|
|
|
NSString *msgID;
|
2007-08-17 04:11:34 +02:00
|
|
|
NSMutableDictionary *info;
|
2007-08-28 23:43:06 +02:00
|
|
|
NGImap4Envelope *sourceEnvelope;
|
2007-08-17 04:11:34 +02:00
|
|
|
|
|
|
|
[sourceMail fetchCoreInfos];
|
|
|
|
|
|
|
|
info = [NSMutableDictionary dictionaryWithCapacity: 16];
|
|
|
|
[info setObject: [sourceMail subjectForReply] forKey: @"subject"];
|
2007-08-28 23:43:06 +02:00
|
|
|
|
|
|
|
sourceEnvelope = [sourceMail envelope];
|
2007-08-17 04:11:34 +02:00
|
|
|
[self _fillInReplyAddresses: info replyToAll: toAll
|
2014-06-05 18:14:34 +02:00
|
|
|
envelope: sourceEnvelope];
|
2007-08-28 23:43:06 +02:00
|
|
|
msgID = [sourceEnvelope messageID];
|
|
|
|
if ([msgID length] > 0)
|
|
|
|
[self setInReplyTo: msgID];
|
2014-01-08 20:40:39 +01:00
|
|
|
[self setText: [sourceMail contentForReply]];
|
2007-08-17 04:11:34 +02:00
|
|
|
[self setHeaders: info];
|
|
|
|
[self setSourceURL: [sourceMail imap4URLString]];
|
|
|
|
[self setSourceFlag: @"Answered"];
|
2014-07-09 17:17:36 +02:00
|
|
|
[self setSourceIMAP4ID: [[sourceMail nameInContainer] intValue]];
|
2010-06-25 21:58:30 +02:00
|
|
|
[self setSourceFolderWithMailObject: sourceMail];
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
[self storeInfo];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) fetchMailForForwarding: (SOGoMailObject *) sourceMail
|
|
|
|
{
|
|
|
|
NSDictionary *info, *attachment;
|
2012-11-21 02:29:27 +01:00
|
|
|
NSString *signature, *nl;
|
2009-11-29 05:19:32 +01:00
|
|
|
SOGoUserDefaults *ud;
|
2007-08-17 04:11:34 +02:00
|
|
|
|
|
|
|
[sourceMail fetchCoreInfos];
|
2007-11-15 23:39:17 +01:00
|
|
|
|
|
|
|
if ([sourceMail subjectForForward])
|
|
|
|
{
|
|
|
|
info = [NSDictionary dictionaryWithObject: [sourceMail subjectForForward]
|
|
|
|
forKey: @"subject"];
|
|
|
|
[self setHeaders: info];
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
[self setSourceURL: [sourceMail imap4URLString]];
|
|
|
|
[self setSourceFlag: @"$Forwarded"];
|
2014-07-09 17:17:36 +02:00
|
|
|
[self setSourceIMAP4ID: [[sourceMail nameInContainer] intValue]];
|
2010-06-25 21:58:30 +02:00
|
|
|
[self setSourceFolderWithMailObject: sourceMail];
|
2007-08-17 04:11:34 +02:00
|
|
|
|
|
|
|
/* attach message */
|
2009-11-29 05:19:32 +01:00
|
|
|
ud = [[context activeUser] userDefaults];
|
2014-01-23 20:34:14 +01:00
|
|
|
if ([[ud mailMessageForwarding] isEqualToString: @"inline"])
|
2007-11-01 21:20:57 +01:00
|
|
|
{
|
2014-01-23 20:34:14 +01:00
|
|
|
[self setText: [sourceMail contentForInlineForward]];
|
2014-01-08 20:40:39 +01:00
|
|
|
[self _fetchAttachmentsFromMail: sourceMail];
|
2007-11-01 21:20:57 +01:00
|
|
|
}
|
2007-08-22 17:13:06 +02:00
|
|
|
else
|
|
|
|
{
|
2012-11-21 02:29:27 +01:00
|
|
|
// TODO: use subject for filename?
|
|
|
|
// error = [newDraft saveAttachment:content withName:@"forward.eml"];
|
2010-08-06 16:25:59 +02:00
|
|
|
signature = [[self mailAccountFolder] signature];
|
2007-12-17 21:39:17 +01:00
|
|
|
if ([signature length])
|
2012-11-21 02:29:27 +01:00
|
|
|
{
|
|
|
|
nl = (isHTML ? @"<br/>" : @"\n");
|
2014-01-23 20:34:14 +01:00
|
|
|
[self setText: [NSString stringWithFormat: @"%@%@-- %@%@", nl, nl, nl, signature]];
|
2012-11-21 02:29:27 +01:00
|
|
|
}
|
2007-08-22 17:13:06 +02:00
|
|
|
attachment = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[sourceMail filenameForForward], @"filename",
|
2007-10-25 17:25:39 +02:00
|
|
|
@"message/rfc822", @"mimetype",
|
2007-08-22 17:13:06 +02:00
|
|
|
nil];
|
|
|
|
[self saveAttachment: [sourceMail content]
|
2014-06-05 18:14:34 +02:00
|
|
|
withMetadata: attachment];
|
2007-08-22 17:13:06 +02:00
|
|
|
}
|
2007-12-17 21:39:17 +01:00
|
|
|
|
2014-01-23 20:39:03 +01:00
|
|
|
// Save the message to the IMAP store so the user can eventually view the attached file(s)
|
|
|
|
// from the Web interface
|
|
|
|
[self save];
|
2015-01-06 17:59:35 +01:00
|
|
|
|
|
|
|
[self storeInfo];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* accessors */
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSString *) sender
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
id tmp;
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
if ((tmp = [headers objectForKey: @"from"]) == nil)
|
2006-06-15 21:34:10 +02:00
|
|
|
return nil;
|
|
|
|
if ([tmp isKindOfClass:[NSArray class]])
|
2007-08-17 04:11:34 +02:00
|
|
|
return [tmp count] > 0 ? [tmp objectAtIndex: 0] : nil;
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* attachments */
|
|
|
|
|
2014-01-08 20:40:39 +01:00
|
|
|
//
|
|
|
|
// Return the attributes (name, size and mime body part) of the files found in the draft folder
|
|
|
|
// on the local filesystem
|
|
|
|
//
|
2013-12-18 20:12:29 +01:00
|
|
|
- (NSArray *) fetchAttachmentAttrs
|
2007-08-17 04:11:34 +02:00
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSMutableArray *ma;
|
2007-08-17 04:11:34 +02:00
|
|
|
NSFileManager *fm;
|
|
|
|
NSArray *files;
|
|
|
|
NSString *filename;
|
2013-12-18 20:12:29 +01:00
|
|
|
NSDictionary *fileAttrs;
|
|
|
|
NGMimeBodyPart *bodyPart;
|
|
|
|
unsigned count, max;
|
2007-08-17 04:11:34 +02:00
|
|
|
|
|
|
|
fm = [NSFileManager defaultManager];
|
|
|
|
files = [fm directoryContentsAtPath: [self draftFolderPath]];
|
|
|
|
|
|
|
|
max = [files count];
|
|
|
|
ma = [NSMutableArray arrayWithCapacity: max];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
filename = [files objectAtIndex: count];
|
|
|
|
if (![filename hasPrefix: @"."])
|
2013-12-18 20:12:29 +01:00
|
|
|
{
|
|
|
|
fileAttrs = [fm fileAttributesAtPath: [self pathToAttachmentWithName: filename] traverseLink: YES];
|
|
|
|
bodyPart = [self bodyPartForAttachmentWithName: filename];
|
2014-01-08 20:40:39 +01:00
|
|
|
[ma addObject: [NSDictionary dictionaryWithObjectsAndKeys: filename, @"filename",
|
2013-12-18 20:12:29 +01:00
|
|
|
[fileAttrs objectForKey: @"NSFileSize"], @"size",
|
|
|
|
bodyPart, @"part", nil]];
|
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return ma;
|
|
|
|
}
|
|
|
|
|
2008-03-28 19:16:55 +01:00
|
|
|
- (BOOL) isValidAttachmentName: (NSString *) filename
|
2007-08-17 04:11:34 +02:00
|
|
|
{
|
2008-09-10 21:50:27 +02:00
|
|
|
return (!([filename rangeOfString: @"/"].length
|
|
|
|
|| [filename isEqualToString: @"."]
|
|
|
|
|| [filename isEqualToString: @".."]));
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSString *) pathToAttachmentWithName: (NSString *) _name
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
if ([_name length] == 0)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
return [[self draftFolderPath] stringByAppendingPathComponent:_name];
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSException *) invalidAttachmentNameError: (NSString *) _name
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return [NSException exceptionWithHTTPStatus:400 /* Bad Request */
|
2007-08-17 04:11:34 +02:00
|
|
|
reason: @"Invalid attachment name!"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-07-27 21:58:30 +02:00
|
|
|
- (NSException *) saveAttachment: (NSData *) _attach
|
|
|
|
withMetadata: (NSDictionary *) metadata
|
|
|
|
{
|
2016-01-18 15:26:07 +01:00
|
|
|
NSString *p, *pmime, *name, *mimeType;
|
2007-09-29 02:23:23 +02:00
|
|
|
NSRange r;
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
if (![_attach isNotNull]) {
|
|
|
|
return [NSException exceptionWithHTTPStatus:400 /* Bad Request */
|
2007-08-17 04:11:34 +02:00
|
|
|
reason: @"Missing attachment content!"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (![self _ensureDraftFolderPath]) {
|
|
|
|
return [NSException exceptionWithHTTPStatus:500 /* Server Error */
|
2007-08-17 04:11:34 +02:00
|
|
|
reason: @"Could not create folder for draft!"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-09-29 02:23:23 +02:00
|
|
|
|
2007-07-27 21:58:30 +02:00
|
|
|
name = [metadata objectForKey: @"filename"];
|
2007-09-29 02:23:23 +02:00
|
|
|
r = [name rangeOfString: @"\\"
|
|
|
|
options: NSBackwardsSearch];
|
|
|
|
if (r.length > 0)
|
|
|
|
name = [name substringFromIndex: r.location + 1];
|
|
|
|
|
2007-07-27 21:58:30 +02:00
|
|
|
if (![self isValidAttachmentName: name])
|
|
|
|
return [self invalidAttachmentNameError: name];
|
|
|
|
|
|
|
|
p = [self pathToAttachmentWithName: name];
|
|
|
|
if (![_attach writeToFile: p atomically: YES])
|
|
|
|
{
|
|
|
|
return [NSException exceptionWithHTTPStatus:500 /* Server Error */
|
2007-08-17 04:11:34 +02:00
|
|
|
reason: @"Could not write attachment to draft!"];
|
2007-07-27 21:58:30 +02:00
|
|
|
}
|
|
|
|
|
2007-10-25 17:25:39 +02:00
|
|
|
mimeType = [metadata objectForKey: @"mimetype"];
|
2007-07-27 21:58:30 +02:00
|
|
|
if ([mimeType length] > 0)
|
|
|
|
{
|
2016-01-18 15:26:07 +01:00
|
|
|
pmime = [self pathToAttachmentWithName: [NSString stringWithFormat: @".%@.mime", name]];
|
|
|
|
if (![[mimeType dataUsingEncoding: NSUTF8StringEncoding] writeToFile: pmime atomically: YES])
|
|
|
|
{
|
2016-02-17 22:32:08 +01:00
|
|
|
[[NSFileManager defaultManager] removeItemAtPath: p error: NULL];
|
2016-01-18 15:26:07 +01:00
|
|
|
return [NSException exceptionWithHTTPStatus: 500 /* Server Error */
|
|
|
|
reason: @"Could not write attachment to draft!"];
|
|
|
|
}
|
2007-07-27 21:58:30 +02:00
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
return nil; /* everything OK */
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSException *) deleteAttachmentWithName: (NSString *) _name
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSFileManager *fm;
|
|
|
|
NSString *p;
|
2007-08-17 04:11:34 +02:00
|
|
|
NSException *error;
|
|
|
|
|
|
|
|
error = nil;
|
|
|
|
|
|
|
|
if ([self isValidAttachmentName:_name])
|
|
|
|
{
|
|
|
|
fm = [NSFileManager defaultManager];
|
|
|
|
p = [self pathToAttachmentWithName:_name];
|
|
|
|
if ([fm fileExistsAtPath: p])
|
|
|
|
if (![fm removeFileAtPath: p handler: nil])
|
|
|
|
error
|
|
|
|
= [NSException exceptionWithHTTPStatus: 500 /* Server Error */
|
|
|
|
reason: @"Could not delete attachment from draft!"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error = [self invalidAttachmentNameError:_name];
|
|
|
|
|
|
|
|
return error;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2013-09-19 22:14:08 +02:00
|
|
|
//
|
|
|
|
// Only called when converting text/html to text/plain parts
|
|
|
|
//
|
|
|
|
- (NGMimeBodyPart *) plainTextBodyPartForText
|
|
|
|
{
|
|
|
|
NGMutableHashMap *map;
|
|
|
|
NGMimeBodyPart *bodyPart;
|
|
|
|
NSString *plainText;
|
|
|
|
|
|
|
|
/* prepare header of body part */
|
|
|
|
map = [[[NGMutableHashMap alloc] initWithCapacity: 1] autorelease];
|
|
|
|
|
|
|
|
[map setObject: contentTypeValue forKey: @"content-type"];
|
|
|
|
|
|
|
|
/* prepare body content */
|
|
|
|
bodyPart = [[[NGMimeBodyPart alloc] initWithHeader:map] autorelease];
|
|
|
|
|
|
|
|
plainText = [text htmlToText];
|
|
|
|
[bodyPart setBody: plainText];
|
|
|
|
|
|
|
|
return bodyPart;
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2013-09-19 22:14:08 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NGMimeBodyPart *) bodyPartForText
|
2006-11-07 21:15:20 +01:00
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
/*
|
|
|
|
This add the text typed by the user (the primary plain/text part).
|
|
|
|
*/
|
|
|
|
NGMutableHashMap *map;
|
|
|
|
NGMimeBodyPart *bodyPart;
|
|
|
|
|
|
|
|
/* prepare header of body part */
|
2007-08-17 04:11:34 +02:00
|
|
|
map = [[[NGMutableHashMap alloc] initWithCapacity: 1] autorelease];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
// TODO: set charset in header!
|
2007-08-17 04:11:34 +02:00
|
|
|
if (text)
|
2010-11-23 14:53:54 +01:00
|
|
|
[map setObject: (isHTML ? htmlContentTypeValue : contentTypeValue)
|
|
|
|
forKey: @"content-type"];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
/* prepare body content */
|
|
|
|
bodyPart = [[[NGMimeBodyPart alloc] initWithHeader:map] autorelease];
|
2007-08-17 04:11:34 +02:00
|
|
|
[bodyPart setBody: text];
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return bodyPart;
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NGMimeMessage *) mimeMessageForContentWithHeaderMap: (NGMutableHashMap *) map
|
2006-11-07 21:15:20 +01:00
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NGMimeMessage *message;
|
2013-09-19 22:14:08 +02:00
|
|
|
id body;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2013-09-19 22:14:08 +02:00
|
|
|
message = [[[NGMimeMessage alloc] initWithHeader:map] autorelease];
|
|
|
|
|
|
|
|
if (!isHTML)
|
2007-08-17 04:11:34 +02:00
|
|
|
{
|
2014-12-19 15:01:39 +01:00
|
|
|
[message setHeader: contentTypeValue forKey: @"content-type"];
|
2013-09-19 22:14:08 +02:00
|
|
|
body = text;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
else
|
2013-09-19 22:14:08 +02:00
|
|
|
{
|
|
|
|
body = [[[NGMimeMultipartBody alloc] initWithPart: message] autorelease];
|
2014-12-19 15:01:39 +01:00
|
|
|
[message setHeader: MultiAlternativeType forKey: @"content-type"];
|
2007-08-17 04:11:34 +02:00
|
|
|
|
2013-11-25 15:52:18 +01:00
|
|
|
// Get the text part from it and add it
|
|
|
|
[body addBodyPart: [self plainTextBodyPartForText]];
|
|
|
|
|
2013-09-19 22:14:08 +02:00
|
|
|
// Add the HTML part
|
|
|
|
[body addBodyPart: [self bodyPartForText]];
|
|
|
|
}
|
|
|
|
|
|
|
|
[message setBody: body];
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSString *) mimeTypeForExtension: (NSString *) _ext
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
// TODO: make configurable
|
|
|
|
// TODO: use /etc/mime-types
|
2007-08-17 04:11:34 +02:00
|
|
|
if ([_ext isEqualToString: @"txt"]) return @"text/plain";
|
|
|
|
if ([_ext isEqualToString: @"html"]) return @"text/html";
|
|
|
|
if ([_ext isEqualToString: @"htm"]) return @"text/html";
|
|
|
|
if ([_ext isEqualToString: @"gif"]) return @"image/gif";
|
|
|
|
if ([_ext isEqualToString: @"jpg"]) return @"image/jpeg";
|
|
|
|
if ([_ext isEqualToString: @"jpeg"]) return @"image/jpeg";
|
2009-09-29 22:13:36 +02:00
|
|
|
if ([_ext isEqualToString: @"eml"]) return @"message/rfc822";
|
2006-06-15 21:34:10 +02:00
|
|
|
return @"application/octet-stream";
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSString *) contentTypeForAttachmentWithName: (NSString *) _name
|
|
|
|
{
|
2007-07-27 21:58:30 +02:00
|
|
|
NSString *s, *p;
|
|
|
|
NSData *mimeData;
|
|
|
|
|
|
|
|
p = [self pathToAttachmentWithName:
|
|
|
|
[NSString stringWithFormat: @".%@.mime", _name]];
|
|
|
|
mimeData = [NSData dataWithContentsOfFile: p];
|
|
|
|
if (mimeData)
|
|
|
|
{
|
|
|
|
s = [[NSString alloc] initWithData: mimeData
|
|
|
|
encoding: NSUTF8StringEncoding];
|
|
|
|
[s autorelease];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s = [self mimeTypeForExtension:[_name pathExtension]];
|
|
|
|
if ([_name length] > 0)
|
2007-08-17 04:11:34 +02:00
|
|
|
s = [s stringByAppendingFormat: @"; name=\"%@\"", _name];
|
2007-07-27 21:58:30 +02:00
|
|
|
}
|
2006-11-07 21:15:20 +01:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return s;
|
|
|
|
}
|
2007-07-27 21:58:30 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSString *) contentDispositionForAttachmentWithName: (NSString *) _name
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSString *type;
|
|
|
|
NSString *cdtype;
|
|
|
|
NSString *cd;
|
2009-11-29 05:19:32 +01:00
|
|
|
SOGoDomainDefaults *dd;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
type = [self contentTypeForAttachmentWithName:_name];
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
if ([type hasPrefix: @"text/"])
|
2009-11-29 05:19:32 +01:00
|
|
|
{
|
|
|
|
dd = [[context activeUser] domainDefaults];
|
|
|
|
cdtype = [dd mailAttachTextDocumentsInline] ? @"inline" : @"attachment";
|
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
else if ([type hasPrefix: @"image/"] || [type hasPrefix: @"message"])
|
2006-06-15 21:34:10 +02:00
|
|
|
cdtype = @"inline";
|
|
|
|
else
|
|
|
|
cdtype = @"attachment";
|
2007-11-15 23:39:17 +01:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
cd = [cdtype stringByAppendingString: @"; filename=\""];
|
2007-08-18 22:33:23 +02:00
|
|
|
cd = [cd stringByAppendingString: _name];
|
2007-08-17 04:11:34 +02:00
|
|
|
cd = [cd stringByAppendingString: @"\""];
|
2007-08-18 22:33:23 +02:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
// TODO: add size parameter (useful addition, RFC 2183)
|
|
|
|
return cd;
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NGMimeBodyPart *) bodyPartForAttachmentWithName: (NSString *) _name
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSFileManager *fm;
|
|
|
|
NGMutableHashMap *map;
|
|
|
|
NGMimeBodyPart *bodyPart;
|
|
|
|
NSString *s;
|
|
|
|
NSData *content;
|
2012-05-18 17:02:42 +02:00
|
|
|
BOOL attachAsString, attachAsRFC822;
|
2006-06-15 21:34:10 +02:00
|
|
|
NSString *p;
|
|
|
|
id body;
|
|
|
|
|
|
|
|
if (_name == nil) return nil;
|
|
|
|
|
|
|
|
/* check attachment */
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
fm = [NSFileManager defaultManager];
|
2014-01-08 20:40:39 +01:00
|
|
|
p = [self pathToAttachmentWithName: _name];
|
|
|
|
if (![fm isReadableFileAtPath: p]) {
|
2007-08-17 04:11:34 +02:00
|
|
|
[self errorWithFormat: @"did not find attachment: '%@'", _name];
|
2006-06-15 21:34:10 +02:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
attachAsString = NO;
|
2012-05-18 17:02:42 +02:00
|
|
|
attachAsRFC822 = NO;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
/* prepare header of body part */
|
|
|
|
|
2014-01-08 20:40:39 +01:00
|
|
|
map = [[[NGMutableHashMap alloc] initWithCapacity: 4] autorelease];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
if ((s = [self contentTypeForAttachmentWithName:_name]) != nil) {
|
2014-01-08 20:40:39 +01:00
|
|
|
[map setObject: s forKey: @"content-type"];
|
2011-04-12 15:57:41 +02:00
|
|
|
if ([s hasPrefix: @"text/plain"] || [s hasPrefix: @"text/html"])
|
2006-06-15 21:34:10 +02:00
|
|
|
attachAsString = YES;
|
2012-05-18 17:02:42 +02:00
|
|
|
else if ([s hasPrefix: @"message/rfc822"])
|
|
|
|
attachAsRFC822 = YES;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2014-01-08 20:40:39 +01:00
|
|
|
if ((s = [self contentDispositionForAttachmentWithName: _name]))
|
2007-11-15 23:39:17 +01:00
|
|
|
{
|
|
|
|
NGMimeContentDispositionHeaderField *o;
|
|
|
|
|
|
|
|
o = [[NGMimeContentDispositionHeaderField alloc] initWithString: s];
|
2014-01-08 20:40:39 +01:00
|
|
|
[map setObject: o forKey: @"content-disposition"];
|
2007-11-15 23:39:17 +01:00
|
|
|
[o release];
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
/* prepare body content */
|
|
|
|
|
|
|
|
if (attachAsString) { // TODO: is this really necessary?
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
content = [[NSData alloc] initWithContentsOfMappedFile:p];
|
|
|
|
|
2012-04-25 22:56:14 +02:00
|
|
|
s = [[NSString alloc] initWithData: content
|
|
|
|
encoding: [NSString defaultCStringEncoding]];
|
2006-06-15 21:34:10 +02:00
|
|
|
if (s != nil) {
|
|
|
|
body = s;
|
|
|
|
[content release]; content = nil;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
[self warnWithFormat:
|
|
|
|
@"could not get text attachment as string: '%@'", _name];
|
|
|
|
body = content;
|
|
|
|
content = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
Note: in OGo this is done in LSWImapMailEditor.m:2477. Apparently
|
|
|
|
NGMimeFileData objects are not processed by the MIME generator!
|
|
|
|
*/
|
|
|
|
content = [[NSData alloc] initWithContentsOfMappedFile:p];
|
2012-05-01 20:38:40 +02:00
|
|
|
[content autorelease];
|
2012-04-25 22:56:14 +02:00
|
|
|
|
2012-05-18 17:02:42 +02:00
|
|
|
if (attachAsRFC822)
|
|
|
|
{
|
|
|
|
[map setObject: @"8bit" forKey: @"content-transfer-encoding"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
content = [content dataByEncodingBase64];
|
|
|
|
[map setObject: @"base64" forKey: @"content-transfer-encoding"];
|
|
|
|
}
|
|
|
|
[map setObject:[NSNumber numberWithInt:[content length]]
|
|
|
|
forKey: @"content-length"];
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
/* Note: the -init method will create a temporary file! */
|
2012-05-01 16:47:20 +02:00
|
|
|
body = [[NGMimeFileData alloc] initWithBytes:[content bytes]
|
|
|
|
length:[content length]];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bodyPart = [[[NGMimeBodyPart alloc] initWithHeader:map] autorelease];
|
|
|
|
[bodyPart setBody:body];
|
|
|
|
|
|
|
|
[body release]; body = nil;
|
|
|
|
return bodyPart;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSArray *) bodyPartsForAllAttachments
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
/* returns nil on error */
|
2013-12-18 20:12:29 +01:00
|
|
|
NSArray *attrs;
|
2006-06-15 21:34:10 +02:00
|
|
|
unsigned i, count;
|
2007-08-17 04:11:34 +02:00
|
|
|
NGMimeBodyPart *bodyPart;
|
|
|
|
NSMutableArray *bodyParts;
|
|
|
|
|
2013-12-18 20:12:29 +01:00
|
|
|
attrs = [self fetchAttachmentAttrs];
|
|
|
|
count = [attrs count];
|
2007-08-17 04:11:34 +02:00
|
|
|
bodyParts = [NSMutableArray arrayWithCapacity: count];
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2014-01-08 20:40:39 +01:00
|
|
|
bodyPart = [self bodyPartForAttachmentWithName: [[attrs objectAtIndex: i] objectForKey: @"filename"]];
|
2007-08-17 04:11:34 +02:00
|
|
|
[bodyParts addObject: bodyPart];
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return bodyParts;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2013-09-19 22:14:08 +02:00
|
|
|
- (NGMimeBodyPart *) mimeMultipartAlternative
|
|
|
|
{
|
|
|
|
NGMimeMultipartBody *textParts;
|
|
|
|
NGMutableHashMap *header;
|
|
|
|
NGMimeBodyPart *part;
|
|
|
|
|
|
|
|
header = [NGMutableHashMap hashMap];
|
|
|
|
[header addObject: MultiAlternativeType forKey: @"content-type"];
|
|
|
|
|
|
|
|
part = [NGMimeBodyPart bodyPartWithHeader: header];
|
|
|
|
|
|
|
|
textParts = [[NGMimeMultipartBody alloc] initWithPart: part];
|
|
|
|
|
2013-11-25 15:52:18 +01:00
|
|
|
// Get the text part from it and add it
|
|
|
|
[textParts addBodyPart: [self plainTextBodyPartForText]];
|
|
|
|
|
2013-09-19 22:14:08 +02:00
|
|
|
// Add the HTML part
|
|
|
|
[textParts addBodyPart: [self bodyPartForText]];
|
|
|
|
|
|
|
|
[part setBody: textParts];
|
|
|
|
RELEASE(textParts);
|
|
|
|
|
|
|
|
return part;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NGMimeMessage *) mimeMultiPartMessageWithHeaderMap: (NGMutableHashMap *) map
|
|
|
|
andBodyParts: (NSArray *) _bodyParts
|
2006-06-15 21:34:10 +02:00
|
|
|
{
|
|
|
|
NGMimeMessage *message;
|
|
|
|
NGMimeMultipartBody *mBody;
|
|
|
|
NSEnumerator *e;
|
2013-09-19 22:14:08 +02:00
|
|
|
id part;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
[map addObject: MultiMixedType forKey: @"content-type"];
|
|
|
|
|
|
|
|
message = [[NGMimeMessage alloc] initWithHeader: map];
|
|
|
|
[message autorelease];
|
|
|
|
mBody = [[NGMimeMultipartBody alloc] initWithPart: message];
|
2013-09-19 22:14:08 +02:00
|
|
|
|
|
|
|
if (!isHTML)
|
|
|
|
{
|
|
|
|
part = [self bodyPartForText];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
part = [self mimeMultipartAlternative];
|
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
|
|
|
|
[mBody addBodyPart: part];
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
e = [_bodyParts objectEnumerator];
|
2007-08-17 04:11:34 +02:00
|
|
|
part = [e nextObject];
|
|
|
|
while (part)
|
|
|
|
{
|
|
|
|
[mBody addBodyPart: part];
|
|
|
|
part = [e nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
[message setBody: mBody];
|
|
|
|
[mBody release];
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (void) _addHeaders: (NSDictionary *) _h
|
|
|
|
toHeaderMap: (NGMutableHashMap *) _map
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSEnumerator *names;
|
|
|
|
NSString *name;
|
|
|
|
|
|
|
|
if ([_h count] == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
names = [_h keyEnumerator];
|
|
|
|
while ((name = [names nextObject]) != nil) {
|
|
|
|
id value;
|
|
|
|
|
|
|
|
value = [_h objectForKey:name];
|
|
|
|
[_map addObject:value forKey:name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (BOOL) isEmptyValue: (id) _value
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
if (![_value isNotNull])
|
|
|
|
return YES;
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
if ([_value isKindOfClass: [NSArray class]])
|
2006-06-15 21:34:10 +02:00
|
|
|
return [_value count] == 0 ? YES : NO;
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
if ([_value isKindOfClass: [NSString class]])
|
2006-06-15 21:34:10 +02:00
|
|
|
return [_value length] == 0 ? YES : NO;
|
2007-08-17 04:11:34 +02:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2010-08-26 17:30:51 +02:00
|
|
|
- (NSString *) _quoteSpecials: (NSString *) address
|
2008-08-18 21:01:02 +02:00
|
|
|
{
|
2010-08-26 17:30:51 +02:00
|
|
|
NSString *result, *part, *s2;
|
|
|
|
int i, len;
|
2008-08-18 21:01:02 +02:00
|
|
|
|
|
|
|
// We want to correctly send mails to recipients such as :
|
|
|
|
// foo.bar
|
|
|
|
// foo (bar) <foo@zot.com>
|
|
|
|
// bar, foo <foo@zot.com>
|
2010-08-26 17:30:51 +02:00
|
|
|
if ([address indexOf: '('] >= 0 || [address indexOf: ')'] >= 0
|
|
|
|
|| [address indexOf: '<'] >= 0 || [address indexOf: '>'] >= 0
|
|
|
|
|| [address indexOf: '@'] >= 0 || [address indexOf: ','] >= 0
|
|
|
|
|| [address indexOf: ';'] >= 0 || [address indexOf: ':'] >= 0
|
|
|
|
|| [address indexOf: '\\'] >= 0 || [address indexOf: '"'] >= 0
|
|
|
|
|| [address indexOf: '.'] >= 0
|
|
|
|
|| [address indexOf: '['] >= 0 || [address indexOf: ']'] >= 0)
|
2008-08-18 21:01:02 +02:00
|
|
|
{
|
2010-08-26 17:30:51 +02:00
|
|
|
// We search for the first instance of < from the end
|
|
|
|
// and we quote what was before if we need to
|
|
|
|
len = [address length];
|
|
|
|
i = -1;
|
|
|
|
while (len--)
|
|
|
|
if ([address characterAtIndex: len] == '<')
|
|
|
|
{
|
|
|
|
i = len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
part = [address substringToIndex: i - 1];
|
|
|
|
s2 = [[part stringByReplacingString: @"\\" withString: @"\\\\"]
|
|
|
|
stringByReplacingString: @"\"" withString: @"\\\""];
|
|
|
|
result = [NSString stringWithFormat: @"\"%@\" %@", s2, [address substringFromIndex: i]];
|
|
|
|
}
|
2008-08-18 21:01:02 +02:00
|
|
|
else
|
2010-08-26 17:30:51 +02:00
|
|
|
{
|
|
|
|
s2 = [[address stringByReplacingString: @"\\" withString: @"\\\\"]
|
|
|
|
stringByReplacingString: @"\"" withString: @"\\\""];
|
|
|
|
result = [NSString stringWithFormat: @"\"%@\"", s2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = address;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) _quoteSpecialsInArray: (NSArray *) addresses
|
|
|
|
{
|
|
|
|
NSMutableArray *result;
|
|
|
|
NSString *address;
|
|
|
|
int count, max;
|
|
|
|
|
|
|
|
max = [addresses count];
|
|
|
|
result = [NSMutableArray arrayWithCapacity: max];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
address = [self _quoteSpecials: [addresses objectAtIndex: count]];
|
|
|
|
[result addObject: address];
|
2008-08-18 21:01:02 +02:00
|
|
|
}
|
|
|
|
|
2010-08-26 17:30:51 +02:00
|
|
|
return result;
|
2008-08-18 21:01:02 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NGMutableHashMap *) mimeHeaderMapWithHeaders: (NSDictionary *) _headers
|
2010-03-23 00:06:42 +01:00
|
|
|
excluding: (NSArray *) _exclude
|
2007-08-17 04:11:34 +02:00
|
|
|
{
|
2010-03-23 00:06:42 +01:00
|
|
|
NSString *s, *dateString;
|
2006-06-15 21:34:10 +02:00
|
|
|
NGMutableHashMap *map;
|
2014-12-06 03:56:54 +01:00
|
|
|
id emails, from, replyTo;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
map = [[[NGMutableHashMap alloc] initWithCapacity:16] autorelease];
|
|
|
|
|
|
|
|
/* add recipients */
|
2014-12-06 03:56:54 +01:00
|
|
|
if ((emails = [headers objectForKey: @"to"]) != nil && [emails isKindOfClass: [NSArray class]])
|
2010-08-26 17:30:51 +02:00
|
|
|
[map setObjects: [self _quoteSpecialsInArray: emails] forKey: @"to"];
|
2014-12-06 03:56:54 +01:00
|
|
|
if ((emails = [headers objectForKey: @"cc"]) != nil && [emails isKindOfClass: [NSArray class]])
|
2010-08-26 17:30:51 +02:00
|
|
|
[map setObjects: [self _quoteSpecialsInArray: emails] forKey: @"cc"];
|
2014-12-06 03:56:54 +01:00
|
|
|
if ((emails = [headers objectForKey: @"bcc"]) != nil && [emails isKindOfClass: [NSArray class]])
|
2010-08-26 17:30:51 +02:00
|
|
|
[map setObjects: [self _quoteSpecialsInArray: emails] forKey: @"bcc"];
|
2007-08-18 22:33:23 +02:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
/* add senders */
|
2007-08-18 22:33:23 +02:00
|
|
|
from = [headers objectForKey: @"from"];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
if (![self isEmptyValue:from]) {
|
|
|
|
if ([from isKindOfClass:[NSArray class]])
|
2010-08-26 17:30:51 +02:00
|
|
|
[map setObjects: [self _quoteSpecialsInArray: from] forKey: @"from"];
|
2006-06-15 21:34:10 +02:00
|
|
|
else
|
2010-08-26 17:30:51 +02:00
|
|
|
[map setObject: [self _quoteSpecials: from] forKey: @"from"];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2010-08-26 17:30:51 +02:00
|
|
|
|
2012-04-25 22:56:14 +02:00
|
|
|
if ((replyTo = [headers objectForKey: @"reply-to"]))
|
2012-04-20 15:27:21 +02:00
|
|
|
[map setObject: replyTo forKey: @"reply-to"];
|
|
|
|
|
2007-08-28 23:43:06 +02:00
|
|
|
if (inReplyTo)
|
|
|
|
[map setObject: inReplyTo forKey: @"in-reply-to"];
|
|
|
|
|
2007-12-14 21:02:51 +01:00
|
|
|
/* add subject */
|
2007-08-17 04:11:34 +02:00
|
|
|
if ([(s = [headers objectForKey: @"subject"]) length] > 0)
|
2007-05-15 05:23:15 +02:00
|
|
|
[map setObject: [s asQPSubjectString: @"utf-8"]
|
2014-05-30 14:41:42 +02:00
|
|
|
forKey: @"subject"];
|
|
|
|
|
|
|
|
if ([(s = [headers objectForKey: @"message-id"]) length] > 0)
|
|
|
|
[map setObject: s
|
|
|
|
forKey: @"message-id"];
|
2007-11-01 14:53:56 +01:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
/* add standard headers */
|
2007-04-25 16:28:00 +02:00
|
|
|
dateString = [[NSCalendarDate date] rfc822DateString];
|
2007-08-17 04:11:34 +02:00
|
|
|
[map addObject: dateString forKey: @"date"];
|
|
|
|
[map addObject: @"1.0" forKey: @"MIME-Version"];
|
2007-11-13 23:38:05 +01:00
|
|
|
[map addObject: userAgent forKey: @"User-Agent"];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
/* add custom headers */
|
2010-10-28 15:52:02 +02:00
|
|
|
if ([(s = [[context request] headerForKey:@"x-webobjects-remote-host"]) length] > 0 &&
|
|
|
|
[s compare: @"localhost"] != NSOrderedSame)
|
|
|
|
[map addObject: s
|
|
|
|
forKey: @"X-Forward"];
|
2008-09-28 16:02:40 +02:00
|
|
|
if ([(s = [headers objectForKey: @"X-Priority"]) length] > 0)
|
|
|
|
[map setObject: s
|
|
|
|
forKey: @"X-Priority"];
|
2010-08-31 15:49:31 +02:00
|
|
|
if ([(s = [headers objectForKey: @"Disposition-Notification-To"]) length] > 0)
|
|
|
|
[map setObject: s
|
|
|
|
forKey: @"Disposition-Notification-To"];
|
2008-09-28 16:02:40 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
[self _addHeaders: _headers toHeaderMap: map];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2010-03-23 00:06:42 +01:00
|
|
|
// We remove what we have to...
|
|
|
|
if (_exclude)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < [_exclude count]; i++)
|
|
|
|
[map removeAllObjectsForKey: [_exclude objectAtIndex: i]];
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NGMimeMessage *) mimeMessageWithHeaders: (NSDictionary *) _headers
|
2010-03-23 00:06:42 +01:00
|
|
|
excluding: (NSArray *) _exclude
|
2014-01-07 16:28:53 +01:00
|
|
|
extractingImages: (BOOL) _extractImages
|
2007-08-17 04:11:34 +02:00
|
|
|
{
|
2013-11-20 14:56:29 +01:00
|
|
|
NSMutableArray *bodyParts;
|
|
|
|
NGMimeMessage *message;
|
|
|
|
NGMutableHashMap *map;
|
|
|
|
NSString *newText;
|
2015-10-23 20:34:19 +02:00
|
|
|
BOOL has_inline_images;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
message = nil;
|
2015-10-23 20:34:19 +02:00
|
|
|
has_inline_images = NO;
|
2013-11-20 14:56:29 +01:00
|
|
|
bodyParts = [NSMutableArray array];
|
|
|
|
|
2014-01-07 16:28:53 +01:00
|
|
|
if (_extractImages)
|
|
|
|
{
|
|
|
|
newText = [text htmlByExtractingImages: bodyParts];
|
|
|
|
if ([bodyParts count])
|
2015-10-23 20:34:19 +02:00
|
|
|
{
|
|
|
|
[self setText: newText];
|
|
|
|
has_inline_images = YES;
|
|
|
|
}
|
2014-01-07 16:28:53 +01:00
|
|
|
}
|
2013-11-20 14:56:29 +01:00
|
|
|
|
2010-03-23 00:06:42 +01:00
|
|
|
map = [self mimeHeaderMapWithHeaders: _headers
|
2014-01-07 16:28:53 +01:00
|
|
|
excluding: _exclude];
|
2007-08-17 04:11:34 +02:00
|
|
|
if (map)
|
|
|
|
{
|
2010-11-25 19:35:20 +01:00
|
|
|
//[self debugWithFormat: @"MIME Envelope: %@", map];
|
2013-11-20 14:56:29 +01:00
|
|
|
|
|
|
|
[bodyParts addObjectsFromArray: [self bodyPartsForAllAttachments]];
|
|
|
|
|
|
|
|
//[self debugWithFormat: @"attachments: %@", bodyParts];
|
|
|
|
|
|
|
|
if ([bodyParts count] == 0)
|
|
|
|
/* no attachments */
|
|
|
|
message = [self mimeMessageForContentWithHeaderMap: map];
|
2007-08-17 04:11:34 +02:00
|
|
|
else
|
2015-10-23 20:34:19 +02:00
|
|
|
{
|
|
|
|
// attachments, create multipart/mixed or multipart/related if
|
|
|
|
// we have inline image to avoid Thunderbird bug #61815 (https://bugzilla.mozilla.org/show_bug.cgi?id=61815)
|
|
|
|
if (has_inline_images)
|
|
|
|
{
|
|
|
|
[map removeAllObjectsForKey: @"content-type"];
|
|
|
|
[map addObject: MultiRelatedType forKey: @"content-type"];
|
|
|
|
}
|
|
|
|
|
|
|
|
message = [self mimeMultiPartMessageWithHeaderMap: map
|
|
|
|
andBodyParts: bodyParts];
|
|
|
|
|
|
|
|
//[self debugWithFormat: @"message: %@", message];
|
|
|
|
}
|
2007-08-17 04:11:34 +02:00
|
|
|
}
|
2013-11-20 14:56:29 +01:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
return message;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
2014-01-07 16:28:53 +01:00
|
|
|
// Return a NGMimeMessage object with inline HTML images (<img src=data>) extracted as attachments (<img src=cid>).
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NGMimeMessage *) mimeMessage
|
|
|
|
{
|
2014-01-07 16:28:53 +01:00
|
|
|
return [self mimeMessageWithHeaders: nil excluding: nil extractingImages: YES];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
2014-01-07 16:28:53 +01:00
|
|
|
// Return a NSData object of the message with no alteration.
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSData *) mimeMessageAsData
|
2007-05-09 21:16:17 +02:00
|
|
|
{
|
2007-08-17 04:11:34 +02:00
|
|
|
NGMimeMessageGenerator *generator;
|
|
|
|
NSData *message;
|
2007-08-09 22:58:17 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
generator = [NGMimeMessageGenerator new];
|
2014-01-07 16:28:53 +01:00
|
|
|
message = [generator generateMimeFromPart: [self mimeMessageWithHeaders: nil excluding: nil extractingImages: NO]];
|
2007-08-17 04:11:34 +02:00
|
|
|
[generator release];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
return message;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSArray *) allRecipients
|
|
|
|
{
|
|
|
|
NSMutableArray *allRecipients;
|
|
|
|
NSArray *recipients;
|
|
|
|
NSString *fieldNames[] = {@"to", @"cc", @"bcc"};
|
|
|
|
unsigned int count;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
allRecipients = [NSMutableArray arrayWithCapacity: 16];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
for (count = 0; count < 3; count++)
|
|
|
|
{
|
|
|
|
recipients = [headers objectForKey: fieldNames[count]];
|
|
|
|
if ([recipients count] > 0)
|
2014-03-24 18:41:57 +01:00
|
|
|
[allRecipients addObjectsFromArray: recipients];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
return allRecipients;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2008-05-22 17:46:49 +02:00
|
|
|
- (NSArray *) allBareRecipients
|
|
|
|
{
|
|
|
|
NSMutableArray *bareRecipients;
|
|
|
|
NSEnumerator *allRecipients;
|
|
|
|
NSString *recipient;
|
|
|
|
|
|
|
|
bareRecipients = [NSMutableArray array];
|
|
|
|
|
|
|
|
allRecipients = [[self allRecipients] objectEnumerator];
|
|
|
|
while ((recipient = [allRecipients nextObject]))
|
|
|
|
[bareRecipients addObject: [recipient pureEMailAddress]];
|
|
|
|
|
|
|
|
return bareRecipients;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2016-02-17 22:30:38 +01:00
|
|
|
/*
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSException *) sendMail
|
2011-09-26 17:49:11 +02:00
|
|
|
{
|
2014-03-24 18:41:57 +01:00
|
|
|
SOGoUserDefaults *ud;
|
2014-09-17 17:51:46 +02:00
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
ud = [[context activeUser] userDefaults];
|
2014-06-26 22:08:47 +02:00
|
|
|
|
2014-03-25 19:53:42 +01:00
|
|
|
if ([ud mailAddOutgoingAddresses])
|
2014-03-24 18:41:57 +01:00
|
|
|
{
|
2014-09-17 17:51:46 +02:00
|
|
|
NSString *recipient, *emailAddress, *addressBook, *uid;
|
|
|
|
NSArray *matchingContacts, *recipients;
|
|
|
|
SOGoContactFolders *contactFolders;
|
|
|
|
SOGoContactGCSEntry *newContact;
|
|
|
|
NGMailAddress *parsedRecipient;
|
|
|
|
NGMailAddressParser *parser;
|
|
|
|
SOGoContactFolder *folder;
|
|
|
|
NGVCard *card;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// Get all the addressbooks
|
|
|
|
contactFolders = [[[context activeUser] homeFolderInContext: context]
|
|
|
|
lookupName: @"Contacts"
|
|
|
|
inContext: context
|
|
|
|
acquire: NO];
|
|
|
|
// Get all the recipients from the current email
|
|
|
|
recipients = [self allRecipients];
|
|
|
|
for (i = 0; i < [recipients count]; i++)
|
|
|
|
{
|
|
|
|
// The address contains a string. ex: "John Doe <sogo1@exemple.com>"
|
|
|
|
recipient = [recipients objectAtIndex: i];
|
|
|
|
parser = [NGMailAddressParser mailAddressParserWithString: recipient];
|
|
|
|
parsedRecipient = [parser parse];
|
|
|
|
emailAddress = [parsedRecipient address];
|
2014-06-26 22:08:47 +02:00
|
|
|
|
2014-09-17 17:51:46 +02:00
|
|
|
matchingContacts = [contactFolders allContactsFromFilter: emailAddress
|
|
|
|
excludeGroups: YES
|
|
|
|
excludeLists: YES];
|
|
|
|
}
|
|
|
|
// If we don't get any results from the autocompletion code, we add it..
|
|
|
|
if ([matchingContacts count] == 0)
|
|
|
|
{
|
|
|
|
// Get the selected addressbook from the user preferences where the new address will be added
|
|
|
|
addressBook = [ud selectedAddressBook];
|
|
|
|
folder = [contactFolders lookupName: addressBook inContext: context acquire: NO];
|
|
|
|
uid = [folder globallyUniqueObjectId];
|
2014-03-24 18:41:57 +01:00
|
|
|
|
2014-09-17 17:51:46 +02:00
|
|
|
if (folder && uid)
|
|
|
|
{
|
|
|
|
card = [NGVCard cardWithUid: uid];
|
|
|
|
[card addEmail: emailAddress types: nil];
|
|
|
|
[card setFn: [parsedRecipient displayName]];
|
|
|
|
|
|
|
|
newContact = [SOGoContactGCSEntry objectWithName: uid
|
|
|
|
inContainer: folder];
|
|
|
|
[newContact setIsNew: YES];
|
|
|
|
[newContact saveComponent: card];
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 18:41:57 +01:00
|
|
|
}
|
2011-09-26 17:49:11 +02:00
|
|
|
return [self sendMailAndCopyToSent: YES];
|
|
|
|
}
|
2016-02-17 22:30:38 +01:00
|
|
|
*/
|
2011-09-26 17:49:11 +02:00
|
|
|
|
2013-11-20 14:56:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2011-09-26 17:49:11 +02:00
|
|
|
- (NSException *) sendMailAndCopyToSent: (BOOL) copyToSent
|
2007-08-17 04:11:34 +02:00
|
|
|
{
|
2011-02-24 00:06:55 +01:00
|
|
|
NSMutableData *cleaned_message;
|
2007-08-17 04:11:34 +02:00
|
|
|
SOGoMailFolder *sentFolder;
|
2009-11-29 05:19:32 +01:00
|
|
|
SOGoDomainDefaults *dd;
|
2011-02-24 00:06:55 +01:00
|
|
|
NSURL *sourceIMAP4URL;
|
|
|
|
NSException *error;
|
|
|
|
NSData *message;
|
|
|
|
NSRange r1, r2;
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
/* send mail */
|
2010-03-23 00:06:42 +01:00
|
|
|
|
2011-09-26 17:49:11 +02:00
|
|
|
// We strip the BCC fields prior sending any mails
|
|
|
|
NGMimeMessageGenerator *generator;
|
|
|
|
|
|
|
|
generator = [[[NGMimeMessageGenerator alloc] init] autorelease];
|
2014-01-07 16:28:53 +01:00
|
|
|
message = [generator generateMimeFromPart: [self mimeMessage]];
|
|
|
|
|
2011-09-26 17:49:11 +02:00
|
|
|
//
|
|
|
|
// We now look for the Bcc: header. If it is present, we remove it.
|
|
|
|
// Some servers, like qmail, do not remove it automatically.
|
|
|
|
//
|
2011-02-24 00:06:55 +01:00
|
|
|
#warning FIXME - we should fix the case issue when we switch to Pantomime
|
2011-09-26 17:49:11 +02:00
|
|
|
cleaned_message = [NSMutableData dataWithData: message];
|
|
|
|
r1 = [cleaned_message rangeOfCString: "\r\n\r\n"];
|
|
|
|
r1 = [cleaned_message rangeOfCString: "\r\nbcc: "
|
|
|
|
options: 0
|
|
|
|
range: NSMakeRange(0,r1.location-1)];
|
2010-03-23 00:06:42 +01:00
|
|
|
|
2011-09-26 17:49:11 +02:00
|
|
|
if (r1.location != NSNotFound)
|
|
|
|
{
|
|
|
|
// We search for the first \r\n AFTER the Bcc: header and
|
|
|
|
// replace the whole thing with \r\n.
|
|
|
|
r2 = [cleaned_message rangeOfCString: "\r\n"
|
|
|
|
options: 0
|
|
|
|
range: NSMakeRange(NSMaxRange(r1)+1,[cleaned_message length]-NSMaxRange(r1)-1)];
|
|
|
|
[cleaned_message replaceBytesInRange: NSMakeRange(r1.location, NSMaxRange(r2)-r1.location)
|
|
|
|
withBytes: "\r\n"
|
|
|
|
length: 2];
|
|
|
|
}
|
2011-02-24 00:06:55 +01:00
|
|
|
|
2011-09-26 17:49:11 +02:00
|
|
|
dd = [[context activeUser] domainDefaults];
|
|
|
|
error = [[SOGoMailer mailerWithDomainDefaults: dd]
|
2012-10-16 22:56:48 +02:00
|
|
|
sendMailData: cleaned_message
|
|
|
|
toRecipients: [self allBareRecipients]
|
|
|
|
sender: [self sender]
|
|
|
|
withAuthenticator: [self authenticatorInContext: context]
|
|
|
|
inContext: context];
|
2011-09-26 17:49:11 +02:00
|
|
|
if (!error && copyToSent)
|
|
|
|
{
|
|
|
|
sentFolder = [[self mailAccountFolder] sentFolderInContext: context];
|
|
|
|
if ([sentFolder isKindOfClass: [NSException class]])
|
|
|
|
error = (NSException *) sentFolder;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error = [sentFolder postData: message flags: @"seen"];
|
|
|
|
if (!error)
|
|
|
|
{
|
|
|
|
[self imap4Connection];
|
|
|
|
if (IMAP4ID > -1)
|
|
|
|
[imap4 markURLDeleted: [self imap4URL]];
|
|
|
|
if (sourceURL && sourceFlag)
|
|
|
|
{
|
|
|
|
sourceIMAP4URL = [NSURL URLWithString: sourceURL];
|
|
|
|
[imap4 addFlags: sourceFlag toURL: sourceIMAP4URL];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2011-09-26 17:49:11 +02:00
|
|
|
if (!error && ![dd mailKeepDraftsAfterSend])
|
|
|
|
[self delete];
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
return error;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-08-18 22:33:23 +02:00
|
|
|
- (NSException *) delete
|
|
|
|
{
|
|
|
|
NSException *error;
|
|
|
|
|
|
|
|
if ([[NSFileManager defaultManager]
|
|
|
|
removeFileAtPath: [self draftFolderPath]
|
|
|
|
handler: nil])
|
|
|
|
error = nil;
|
|
|
|
else
|
|
|
|
error = [NSException exceptionWithHTTPStatus: 500 /* server error */
|
|
|
|
reason: @"could not delete draft"];
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
/* operations */
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
- (NSString *) contentAsString
|
|
|
|
{
|
|
|
|
NSString *str;
|
|
|
|
NSData *message;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
message = [self mimeMessageAsData];
|
|
|
|
if (message)
|
|
|
|
{
|
|
|
|
str = [[NSString alloc] initWithData: message
|
|
|
|
encoding: NSUTF8StringEncoding];
|
|
|
|
if (!str)
|
|
|
|
[self errorWithFormat: @"could not load draft as UTF-8 (data size=%d)",
|
|
|
|
[message length]];
|
|
|
|
else
|
|
|
|
[str autorelease];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"message data is empty"];
|
|
|
|
str = nil;
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-17 04:11:34 +02:00
|
|
|
return str;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end /* SOGoDraftObject */
|