2007-05-25 23:14:53 +02:00
|
|
|
/* UIxMailAccountActions.m - this file is part of SOGo
|
|
|
|
*
|
2011-04-21 18:02:31 +02:00
|
|
|
* Copyright (C) 2007-2011 Inverse inc.
|
2007-05-25 23:14:53 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
2011-12-29 18:50:48 +01:00
|
|
|
* Ludovic Marcotte <lmarcotte@inverse.ca>
|
2007-05-25 23:14:53 +02:00
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
2011-12-29 18:50:48 +01:00
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
2007-05-25 23:14:53 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
2008-09-18 20:05:27 +02:00
|
|
|
#import <Foundation/NSValue.h>
|
2007-05-25 23:14:53 +02:00
|
|
|
|
2007-11-19 23:44:12 +01:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
2007-08-17 04:30:28 +02:00
|
|
|
#import <NGObjWeb/WORequest.h>
|
2007-05-25 23:14:53 +02:00
|
|
|
#import <NGObjWeb/WOResponse.h>
|
|
|
|
#import <NGImap4/NGImap4Connection.h>
|
2008-09-17 21:44:11 +02:00
|
|
|
#import <NGImap4/NGImap4Client.h>
|
2013-09-24 22:01:23 +02:00
|
|
|
#import <NGImap4/NSString+Imap4.h>
|
2011-04-14 23:16:45 +02:00
|
|
|
#import <NGExtensions/NSString+misc.h>
|
2007-11-19 23:44:12 +01:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
#import <Mailer/SOGoMailAccount.h>
|
|
|
|
#import <Mailer/SOGoDraftObject.h>
|
|
|
|
#import <Mailer/SOGoDraftsFolder.h>
|
|
|
|
#import <SOGo/NSArray+Utilities.h>
|
|
|
|
#import <SOGo/NSObject+Utilities.h>
|
|
|
|
#import <SOGo/NSString+Utilities.h>
|
|
|
|
#import <SOGo/SOGoDomainDefaults.h>
|
|
|
|
#import <SOGo/SOGoUser.h>
|
2011-04-21 18:02:31 +02:00
|
|
|
#import <SOGo/SOGoUserManager.h>
|
2007-08-17 04:30:28 +02:00
|
|
|
|
|
|
|
#import "../Common/WODirectAction+SOGo.h"
|
2007-05-25 23:14:53 +02:00
|
|
|
|
|
|
|
#import "UIxMailAccountActions.h"
|
|
|
|
|
|
|
|
@implementation UIxMailAccountActions
|
|
|
|
|
2007-11-01 13:48:19 +01:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
inboxFolderName = nil;
|
|
|
|
draftsFolderName = nil;
|
|
|
|
sentFolderName = nil;
|
|
|
|
trashFolderName = nil;
|
2011-04-21 18:02:31 +02:00
|
|
|
otherUsersFolderName = nil;
|
|
|
|
sharedFoldersName = nil;
|
2007-11-01 13:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[inboxFolderName release];
|
|
|
|
[draftsFolderName release];
|
|
|
|
[sentFolderName release];
|
|
|
|
[trashFolderName release];
|
2011-04-21 18:02:31 +02:00
|
|
|
[otherUsersFolderName release];
|
|
|
|
[sharedFoldersName release];
|
2007-11-01 13:48:19 +01:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) _folderType: (NSString *) folderName
|
2007-05-25 23:14:53 +02:00
|
|
|
{
|
|
|
|
NSString *folderType;
|
2007-11-01 13:48:19 +01:00
|
|
|
SOGoMailAccount *co;
|
|
|
|
NSArray *specialFolders;
|
2007-05-25 23:14:53 +02:00
|
|
|
|
2007-11-01 13:48:19 +01:00
|
|
|
if (!inboxFolderName)
|
|
|
|
{
|
|
|
|
co = [self clientObject];
|
|
|
|
specialFolders = [[NSArray arrayWithObjects:
|
|
|
|
[co inboxFolderNameInContext: context],
|
|
|
|
[co draftsFolderNameInContext: context],
|
|
|
|
[co sentFolderNameInContext: context],
|
|
|
|
[co trashFolderNameInContext: context],
|
2011-04-21 18:02:31 +02:00
|
|
|
[co otherUsersFolderNameInContext: context],
|
|
|
|
[co sharedFoldersNameInContext: context],
|
2007-11-01 13:48:19 +01:00
|
|
|
nil] stringsWithFormat: @"/%@"];
|
2011-04-21 18:02:31 +02:00
|
|
|
ASSIGN(inboxFolderName, [specialFolders objectAtIndex: 0]);
|
|
|
|
ASSIGN(draftsFolderName, [specialFolders objectAtIndex: 1]);
|
|
|
|
ASSIGN(sentFolderName, [specialFolders objectAtIndex: 2]);
|
|
|
|
ASSIGN(trashFolderName, [specialFolders objectAtIndex: 3]);
|
2011-04-26 15:48:58 +02:00
|
|
|
if ([specialFolders count] > 4)
|
2011-04-26 15:10:40 +02:00
|
|
|
ASSIGN(otherUsersFolderName, [specialFolders objectAtIndex: 4]);
|
2011-04-26 15:48:58 +02:00
|
|
|
if ([specialFolders count] > 5)
|
2011-04-26 15:10:40 +02:00
|
|
|
ASSIGN(sharedFoldersName, [specialFolders objectAtIndex: 5]);
|
2007-11-01 13:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ([folderName isEqualToString: inboxFolderName])
|
2007-05-25 23:14:53 +02:00
|
|
|
folderType = @"inbox";
|
2007-11-01 13:48:19 +01:00
|
|
|
else if ([folderName isEqualToString: draftsFolderName])
|
2007-05-25 23:14:53 +02:00
|
|
|
folderType = @"draft";
|
2007-11-01 13:48:19 +01:00
|
|
|
else if ([folderName isEqualToString: sentFolderName])
|
2007-05-25 23:14:53 +02:00
|
|
|
folderType = @"sent";
|
2007-11-01 13:48:19 +01:00
|
|
|
else if ([folderName isEqualToString: trashFolderName])
|
2007-05-25 23:14:53 +02:00
|
|
|
folderType = @"trash";
|
2013-01-11 20:47:13 +01:00
|
|
|
else if ([folderName hasPrefix: [NSString stringWithFormat: @"%@/", draftsFolderName]])
|
|
|
|
folderType = @"draft/folder";
|
|
|
|
else if ([folderName hasPrefix: [NSString stringWithFormat: @"%@/", sentFolderName]])
|
|
|
|
folderType = @"sent/folder";
|
2007-05-25 23:14:53 +02:00
|
|
|
else
|
|
|
|
folderType = @"folder";
|
|
|
|
|
|
|
|
return folderType;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) _jsonFolders: (NSEnumerator *) rawFolders
|
|
|
|
{
|
2013-09-24 22:01:23 +02:00
|
|
|
NSString *currentFolder, *currentDecodedFolder, *currentDisplayName, *currentFolderType, *login, *fullName;
|
2011-04-21 18:02:31 +02:00
|
|
|
NSMutableArray *pathComponents;
|
|
|
|
SOGoUserManager *userManager;
|
2007-11-01 13:48:19 +01:00
|
|
|
NSDictionary *folderData;
|
2011-04-21 18:02:31 +02:00
|
|
|
NSMutableArray *folders;
|
2011-12-29 18:50:48 +01:00
|
|
|
NSAutoreleasePool *pool;
|
2007-05-25 23:14:53 +02:00
|
|
|
|
|
|
|
folders = [NSMutableArray array];
|
2007-11-01 13:48:19 +01:00
|
|
|
while ((currentFolder = [rawFolders nextObject]))
|
2007-05-25 23:14:53 +02:00
|
|
|
{
|
2011-12-29 18:50:48 +01:00
|
|
|
// Using a local pool to avoid using too many file descriptors. This could
|
|
|
|
// happen with tons of mailboxes under "Other Users" as LDAP connections
|
|
|
|
// are never reused and "autoreleased" at the end. This loop would consume
|
|
|
|
// lots of LDAP connections during its execution.
|
|
|
|
pool = [[NSAutoreleasePool alloc] init];
|
2013-09-24 22:01:23 +02:00
|
|
|
|
|
|
|
currentDecodedFolder = [currentFolder stringByDecodingImap4FolderName];
|
|
|
|
currentFolderType = [self _folderType: currentDecodedFolder];
|
2011-04-21 18:02:31 +02:00
|
|
|
|
|
|
|
// We translate the "Other Users" and "Shared Folders" namespaces.
|
|
|
|
// While we're at it, we also translate the user's mailbox names
|
|
|
|
// to the full name of the person.
|
2013-09-24 22:01:23 +02:00
|
|
|
if (otherUsersFolderName && [currentDecodedFolder hasPrefix: otherUsersFolderName])
|
2011-04-21 18:02:31 +02:00
|
|
|
{
|
2011-05-05 20:34:13 +02:00
|
|
|
// We have a string like /Other Users/lmarcotte/... under Cyrus, but we could
|
|
|
|
// also have something like /shared under Dovecot. So we swap the username only
|
|
|
|
// if we have one, of course.
|
2013-09-24 22:01:23 +02:00
|
|
|
pathComponents = [NSMutableArray arrayWithArray: [currentDecodedFolder pathComponents]];
|
2011-04-21 18:02:31 +02:00
|
|
|
|
2011-05-05 20:34:13 +02:00
|
|
|
if ([pathComponents count] > 2)
|
|
|
|
{
|
|
|
|
login = [pathComponents objectAtIndex: 2];
|
|
|
|
userManager = [SOGoUserManager sharedUserManager];
|
|
|
|
fullName = [userManager getCNForUID: login];
|
|
|
|
[pathComponents removeObjectsInRange: NSMakeRange(0,3)];
|
|
|
|
|
|
|
|
currentDisplayName = [NSString stringWithFormat: @"/%@/%@/%@",
|
|
|
|
[self labelForKey: @"OtherUsersFolderName"],
|
|
|
|
(fullName != nil ? fullName : login),
|
|
|
|
[pathComponents componentsJoinedByString: @"/"]];
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentDisplayName = [NSString stringWithFormat: @"/%@%@", [self labelForKey: @"OtherUsersFolderName"],
|
2013-09-24 22:01:23 +02:00
|
|
|
[currentDecodedFolder substringFromIndex: [otherUsersFolderName length]]];
|
2011-05-05 20:34:13 +02:00
|
|
|
}
|
2011-04-21 18:02:31 +02:00
|
|
|
}
|
2013-09-24 22:01:23 +02:00
|
|
|
else if (sharedFoldersName && [currentDecodedFolder hasPrefix: sharedFoldersName])
|
2011-04-21 18:02:31 +02:00
|
|
|
currentDisplayName = [NSString stringWithFormat: @"/%@%@", [self labelForKey: @"SharedFoldersName"],
|
2013-09-24 22:01:23 +02:00
|
|
|
[currentDecodedFolder substringFromIndex: [sharedFoldersName length]]];
|
2011-04-21 18:02:31 +02:00
|
|
|
else
|
2013-09-24 22:01:23 +02:00
|
|
|
currentDisplayName = currentDecodedFolder;
|
2011-04-21 18:02:31 +02:00
|
|
|
|
2007-11-01 13:48:19 +01:00
|
|
|
folderData = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
currentFolder, @"path",
|
2011-04-21 18:02:31 +02:00
|
|
|
currentFolderType, @"type",
|
|
|
|
currentDisplayName, @"displayName",
|
|
|
|
nil];
|
2007-11-01 13:48:19 +01:00
|
|
|
[folders addObject: folderData];
|
2011-12-29 18:50:48 +01:00
|
|
|
[pool release];
|
2007-05-25 23:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return folders;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) listMailboxesAction
|
|
|
|
{
|
2007-05-28 21:05:00 +02:00
|
|
|
SOGoMailAccount *co;
|
2007-11-01 13:48:19 +01:00
|
|
|
NSEnumerator *rawFolders;
|
|
|
|
NSArray *folders;
|
2008-09-17 21:44:11 +02:00
|
|
|
NSDictionary *data;
|
2007-05-25 23:14:53 +02:00
|
|
|
WOResponse *response;
|
|
|
|
|
2007-05-28 21:05:00 +02:00
|
|
|
co = [self clientObject];
|
2007-11-01 13:48:19 +01:00
|
|
|
|
|
|
|
rawFolders = [[co allFolderPaths] objectEnumerator];
|
|
|
|
folders = [self _jsonFolders: rawFolders];
|
2007-05-25 23:14:53 +02:00
|
|
|
|
2011-07-08 19:38:27 +02:00
|
|
|
data = [NSDictionary dictionaryWithObjectsAndKeys: folders, @"mailboxes", nil];
|
2010-08-12 22:51:10 +02:00
|
|
|
response = [self responseWithStatus: 200
|
|
|
|
andString: [data jsonRepresentation]];
|
|
|
|
[response setHeader: @"application/json"
|
2007-08-09 04:30:41 +02:00
|
|
|
forKey: @"content-type"];
|
2007-05-25 23:14:53 +02:00
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:30:28 +02:00
|
|
|
/* compose */
|
|
|
|
|
|
|
|
- (WOResponse *) composeAction
|
|
|
|
{
|
2012-11-21 14:54:32 +01:00
|
|
|
NSString *urlBase, *url, *value, *signature, *nl;
|
2007-08-17 04:30:28 +02:00
|
|
|
SOGoDraftObject *newDraftMessage;
|
2008-08-26 19:16:16 +02:00
|
|
|
NSMutableDictionary *headers;
|
2012-11-21 14:54:32 +01:00
|
|
|
SOGoDraftsFolder *drafts;
|
|
|
|
id mailTo;
|
2007-11-19 23:44:12 +01:00
|
|
|
BOOL save;
|
2007-08-17 04:30:28 +02:00
|
|
|
|
|
|
|
drafts = [[self clientObject] draftsFolderInContext: context];
|
|
|
|
newDraftMessage = [drafts newDraft];
|
2008-08-26 19:16:16 +02:00
|
|
|
headers = [NSMutableDictionary dictionary];
|
|
|
|
|
2007-11-19 23:44:12 +01:00
|
|
|
save = NO;
|
|
|
|
|
2007-08-21 16:47:26 +02:00
|
|
|
value = [[self request] formValueForKey: @"mailto"];
|
|
|
|
if ([value length] > 0)
|
2007-08-17 04:30:28 +02:00
|
|
|
{
|
2012-02-02 21:46:30 +01:00
|
|
|
mailTo = [[value stringByUnescapingURL] objectFromJSONString];
|
|
|
|
if (mailTo && [mailTo isKindOfClass: [NSArray class]])
|
|
|
|
{
|
|
|
|
[headers setObject: (NSArray *) mailTo forKey: @"to"];
|
|
|
|
save = YES;
|
|
|
|
}
|
2007-11-19 23:44:12 +01:00
|
|
|
}
|
|
|
|
|
2008-08-26 19:16:16 +02:00
|
|
|
value = [[self request] formValueForKey: @"subject"];
|
|
|
|
if ([value length] > 0)
|
|
|
|
{
|
2011-04-14 23:16:45 +02:00
|
|
|
[headers setObject: [value stringByUnescapingURL] forKey: @"subject"];
|
2008-08-26 19:16:16 +02:00
|
|
|
save = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (save)
|
|
|
|
[newDraftMessage setHeaders: headers];
|
|
|
|
|
2010-08-06 16:25:59 +02:00
|
|
|
signature = [[self clientObject] signature];
|
2007-11-19 23:44:12 +01:00
|
|
|
if ([signature length])
|
|
|
|
{
|
2012-11-21 14:54:32 +01:00
|
|
|
nl = ([[[[context activeUser] userDefaults] mailComposeMessageType] isEqualToString: @"html"] ? @"<br/>" : @"\n");
|
|
|
|
|
2007-11-19 23:44:12 +01:00
|
|
|
[newDraftMessage
|
2012-11-21 14:54:32 +01:00
|
|
|
setText: [NSString stringWithFormat: @"%@%@-- %@%@", nl, nl, nl, signature]];
|
2007-11-19 23:44:12 +01:00
|
|
|
save = YES;
|
2007-08-17 04:30:28 +02:00
|
|
|
}
|
2007-11-19 23:44:12 +01:00
|
|
|
if (save)
|
|
|
|
[newDraftMessage storeInfo];
|
2007-08-17 04:30:28 +02:00
|
|
|
|
|
|
|
urlBase = [newDraftMessage baseURLInContext: context];
|
|
|
|
url = [urlBase composeURLWithAction: @"edit"
|
|
|
|
parameters: nil
|
|
|
|
andHash: NO];
|
|
|
|
|
|
|
|
return [self redirectToLocation: url];
|
|
|
|
}
|
|
|
|
|
2010-09-30 20:05:14 +02:00
|
|
|
- (WOResponse *) _performDelegationAction: (SEL) action
|
|
|
|
{
|
|
|
|
SOGoMailAccount *co;
|
|
|
|
WOResponse *response;
|
|
|
|
NSString *uid;
|
|
|
|
|
|
|
|
co = [self clientObject];
|
|
|
|
if ([[co nameInContainer] isEqualToString: @"0"])
|
|
|
|
{
|
|
|
|
uid = [[context request] formValueForKey: @"uid"];
|
|
|
|
if ([uid length] > 0)
|
|
|
|
{
|
|
|
|
[co performSelector: action
|
|
|
|
withObject: [NSArray arrayWithObject: uid]];
|
|
|
|
response = [self responseWith204];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
response = [self responseWithStatus: 500
|
|
|
|
andString: @"Missing 'uid' parameter."];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
response = [self responseWithStatus: 403
|
|
|
|
andString: @"This action cannot be performed on secondary accounts."];
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) addDelegateAction
|
|
|
|
{
|
|
|
|
return [self _performDelegationAction: @selector (addDelegates:)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) removeDelegateAction
|
|
|
|
{
|
|
|
|
return [self _performDelegationAction: @selector (removeDelegates:)];
|
|
|
|
}
|
|
|
|
|
2007-05-25 23:14:53 +02:00
|
|
|
@end
|