From 2e44ac0f9b9b513b40e1b490ac1d48e846108d5f Mon Sep 17 00:00:00 2001 From: Ludovic Marcotte Date: Mon, 13 Jan 2014 10:19:00 -0500 Subject: [PATCH] Fixed MoveItems and also fixed Ping with no content --- ActiveSync/SOGoActiveSyncDispatcher.m | 75 ++++++++++++++++++++------- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/ActiveSync/SOGoActiveSyncDispatcher.m b/ActiveSync/SOGoActiveSyncDispatcher.m index e8ca17985..eeb04aa49 100644 --- a/ActiveSync/SOGoActiveSyncDispatcher.m +++ b/ActiveSync/SOGoActiveSyncDispatcher.m @@ -47,6 +47,8 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVE #import #import +#import +#import #import #import @@ -558,7 +560,7 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVE - (void) processMoveItems: (id ) theDocumentElement inResponse: (WOResponse *) theResponse { - NSString *srcMessageId, *srcFolderId, *dstFolderId; + NSString *srcMessageId, *srcFolderId, *dstFolderId, *dstMessageId; SOGoMicrosoftActiveSyncFolderType srcFolderType, dstFolderType; srcMessageId = [[(id)[theDocumentElement getElementsByTagName: @"SrcMsgId"] lastObject] textValue]; @@ -571,8 +573,11 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVE SOGoMailAccounts *accountsFolder; SOGoMailFolder *currentFolder; SOGoUserFolder *userFolder; + NGImap4Client *client; id currentCollection; - NSException *ex; + + NSDictionary *response; + NSString *v; userFolder = [[context activeUser] homeFolderInContext: context]; accountsFolder = [userFolder lookupName: @"Mail" inContext: context acquire: NO]; @@ -582,20 +587,38 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVE inContext: context acquire: NO]; - // FIXME: improve destination folder - ex = (id)[currentCollection moveUIDs: [NSArray arrayWithObject: srcMessageId] - toFolder: [NSString stringWithFormat: @"/0/folder%@", dstFolderId] - inContext: context]; - if (ex) + client = [[currentCollection imap4Connection] client]; + [client select: srcFolderId]; + response = [client copyUid: [srcMessageId intValue] + toFolder: [NSString stringWithFormat: @"/%@", dstFolderId]]; + + // We extract the destionation message id + if ([[response objectForKey: @"result"] boolValue] + && (v = [[[response objectForKey: @"RawResponse"] objectForKey: @"ResponseResult"] objectForKey: @"flag"]) + && [v hasPrefix: @"COPYUID "]) + { + dstMessageId = [[v componentsSeparatedByString: @" "] lastObject]; + + // We mark the original message as deleted + response = [client storeFlags: [NSArray arrayWithObject: @"Deleted"] + forUIDs: [NSArray arrayWithObject: srcMessageId] + addOrRemove: YES]; + + if ([[response valueForKey: @"result"] boolValue]) + [currentCollection markForExpunge]; + + } + + if (!dstMessageId) { [theResponse setStatus: 500]; - [theResponse appendContentString: [ex reason]]; + [theResponse appendContentString: @"Unable to move message"]; } else { NSMutableString *s; NSData *d; - + // Everything is alright, lets return the proper response s = [NSMutableString string]; @@ -603,11 +626,10 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVE [s appendString: @""]; [s appendString: @""]; [s appendFormat: @"%@", srcMessageId]; - [s appendFormat: @"%@", srcMessageId]; + [s appendFormat: @"%@", dstMessageId]; [s appendFormat: @"%d", 1]; [s appendString: @""]; - d = [[s dataUsingEncoding: NSUTF8StringEncoding] xml2wbxml]; [theResponse setContent: d]; @@ -616,6 +638,7 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVE else { [theResponse setStatus: 500]; + [theResponse appendContentString: @"Unsupported move operation"]; } } @@ -1155,16 +1178,30 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVE if (!d) { - return [NSException exceptionWithHTTPStatus: 500]; + // We check if it's a Ping command with no body. + // See http://msdn.microsoft.com/en-us/library/ee200913(v=exchg.80).aspx for details + cmdName = [[theRequest uri] command]; + + if ([cmdName caseInsensitiveCompare: @"Ping"] != NSOrderedSame) + return [NSException exceptionWithHTTPStatus: 500]; } - builder = [[[NSClassFromString(@"DOMSaxBuilder") alloc] init] autorelease]; - dom = [builder buildFromData: d]; - documentElement = [dom documentElement]; - - // See 2.2.2 Commands - http://msdn.microsoft.com/en-us/library/ee202197(v=exchg.80).aspx - // for all potential commands - cmdName = [NSString stringWithFormat: @"process%@:inResponse:", [documentElement tagName]]; + if (d) + { + builder = [[[NSClassFromString(@"DOMSaxBuilder") alloc] init] autorelease]; + dom = [builder buildFromData: d]; + documentElement = [dom documentElement]; + + // See 2.2.2 Commands - http://msdn.microsoft.com/en-us/library/ee202197(v=exchg.80).aspx + // for all potential commands + cmdName = [NSString stringWithFormat: @"process%@:inResponse:", [documentElement tagName]]; + } + else + { + // Ping command with empty body + cmdName = [NSString stringWithFormat: @"process%@:inResponse:", cmdName]; + } + aSelector = NSSelectorFromString(cmdName); [self performSelector: aSelector withObject: documentElement withObject: theResponse];