*/
@class NSString, NSData, NSURL, NSArray, NSMutableDictionary, NSTimer;
+@class NSException;
@class NGImap4Client;
@interface SOGoMailManager : NSObject
- (NSData *)fetchContentOfBodyPart:(NSString *)_partId
atURL:(NSURL *)_url password:(NSString *)_pwd;
+/* managing folders */
+
+- (NSException *)createMailbox:(NSString *)_mailbox atURL:(NSURL *)_url
+ password:(NSString *)_pwd;
+- (NSException *)deleteMailboxAtURL:(NSURL *)_url password:(NSString *)_pwd;
+
@end
#endif /* __Mailer_SOGoMailManager_H__ */
@implementation SOGoMailManager
-static BOOL debugOn = YES;
+static BOOL debugOn = NO;
static BOOL debugCache = NO;
static BOOL debugKeys = NO;
static BOOL poolingOff = NO;
return result;
}
+/* managing folders */
+
+- (BOOL)isPermissionDeniedResult:(id)_result {
+ if ([[_result valueForKey:@"result"] intValue] != 0)
+ return NO;
+
+ return [[_result valueForKey:@"reason"]
+ isEqualToString:@"Permission denied"];
+}
+
+- (NSException *)createMailbox:(NSString *)_mailbox atURL:(NSURL *)_url
+ password:(NSString *)_pwd
+{
+ SOGoMailConnectionEntry *entry;
+ NSString *newPath;
+ id result;
+
+ /* check connection cache */
+
+ if ((entry = [self entryForURL:_url password:_pwd]) == nil) {
+ return [NSException exceptionWithHTTPStatus:404 /* Not Found */
+ reason:@"did not find IMAP4 folder"];
+ }
+
+ /* construct path */
+
+ newPath = [self imap4FolderNameForURL:_url];
+ newPath = [newPath stringByAppendingString:[self imap4Separator]];
+ newPath = [newPath stringByAppendingString:_mailbox];
+
+ /* create */
+
+ result = [[entry client] create:newPath];
+ if ([self isPermissionDeniedResult:result]) {
+ return [NSException exceptionWithHTTPStatus:403 /* forbidden */
+ reason:@"creation of folders not allowed"];
+ }
+ else if ([[result valueForKey:@"result"] intValue] == 0) {
+ return [NSException exceptionWithHTTPStatus:500 /* server error */
+ reason:[result valueForKey:@"reason"]];
+ }
+
+ [entry flushFolderHierarchyCache];
+ // [self debugWithFormat:@"created mailbox: %@: %@", newPath, result];
+ return nil;
+}
+
+- (NSException *)deleteMailboxAtURL:(NSURL *)_url password:(NSString *)_pwd {
+ SOGoMailConnectionEntry *entry;
+ NSString *path;
+ id result;
+
+ /* check connection cache */
+
+ if ((entry = [self entryForURL:_url password:_pwd]) == nil) {
+ return [NSException exceptionWithHTTPStatus:404 /* Not Found */
+ reason:@"did not find IMAP4 folder"];
+ }
+
+ /* delete */
+
+ path = [self imap4FolderNameForURL:_url];
+ result = [[entry client] delete:path];
+
+ if ([self isPermissionDeniedResult:result]) {
+ return [NSException exceptionWithHTTPStatus:403 /* forbidden */
+ reason:@"creation of folders not allowed"];
+ }
+ else if ([[result valueForKey:@"result"] intValue] == 0) {
+ return [NSException exceptionWithHTTPStatus:500 /* server error */
+ reason:[result valueForKey:@"reason"]];
+ }
+
+ [entry flushFolderHierarchyCache];
+#if 0
+ [self debugWithFormat:@"delete mailbox %@: %@", _url, result];
+#endif
+ return nil;
+}
+
/* debugging */
- (BOOL)isDebuggingEnabled {