diff --git a/ActiveSync/SOGoActiveSyncDispatcher.m b/ActiveSync/SOGoActiveSyncDispatcher.m
index 7e44209da..bd02f2e2a 100644
--- a/ActiveSync/SOGoActiveSyncDispatcher.m
+++ b/ActiveSync/SOGoActiveSyncDispatcher.m
@@ -1384,13 +1384,13 @@ void handle_eas_terminate(int signum)
inResponse: (WOResponse *) theResponse
{
NSString *fileReference, *realCollectionId, *serverId, *bodyPreferenceType, *mimeSupport, *collectionId;
- NSMutableString *s;
NSArray *fetchRequests;
- id aFetch;
+ NSMutableString *s;
NSData *d;
- int i;
+ id aFetch;
SOGoMicrosoftActiveSyncFolderType folderType;
+ int i;
s = [NSMutableString string];
@@ -1417,6 +1417,20 @@ void handle_eas_terminate(int signum)
aFetch = [fetchRequests objectAtIndex: i];
fileReference = [[[(id)[aFetch getElementsByTagName: @"FileReference"] lastObject] textValue] stringByUnescapingURL];
collectionId = [[(id)[theDocumentElement getElementsByTagName: @"CollectionId"] lastObject] textValue];
+ serverId = nil;
+
+ // We might not have a CollectionId in our request if the ItemOperation (Fetch) is for getting
+ // Search results with a LongId. Apple iOS does that.
+ if (!collectionId)
+ {
+ NSString *longId;
+ NSRange r;
+
+ longId = [[(id)[theDocumentElement getElementsByTagName: @"LongId"] lastObject] textValue];
+ r = [longId rangeOfString: @"+" options: NSBackwardsSearch];
+ collectionId = [longId substringToIndex: r.location];
+ serverId = [longId substringFromIndex: r.location+1];
+ }
// its either a itemOperation to fetch an attachment or an email
if ([fileReference length])
@@ -1494,7 +1508,12 @@ void handle_eas_terminate(int signum)
{
// fetch mail
realCollectionId = [self globallyUniqueIDToIMAPFolderName: realCollectionId type: folderType];
- serverId = [[(id)[theDocumentElement getElementsByTagName: @"ServerId"] lastObject] textValue];
+
+ // ServerId might have been set if LongId was defined in the initial request. If not, it is
+ // a normal ItemOperations (Fetch) to get a complete email
+ if (!serverId)
+ serverId = [[(id)[theDocumentElement getElementsByTagName: @"ServerId"] lastObject] textValue];
+
bodyPreferenceType = [[(id)[[(id)[theDocumentElement getElementsByTagName: @"BodyPreference"] lastObject] getElementsByTagName: @"Type"] lastObject] textValue];
[context setObject: bodyPreferenceType forKey: @"BodyPreferenceType"];
mimeSupport = [[(id)[theDocumentElement getElementsByTagName: @"MIMESupport"] lastObject] textValue];
@@ -2783,7 +2802,7 @@ void handle_eas_terminate(int signum)
}
}
- [s appendFormat: @"0-%d", total-1];
+ [s appendFormat: @"0-%d", (total ? total-1 : 0)];
[s appendFormat: @"%d", total];
[s appendString: @""];
[s appendString: @""];
@@ -2920,10 +2939,13 @@ void handle_eas_terminate(int signum)
total = [sortedUIDs count];
for (i = 0; i < total; i++)
{
+ itemId = [[sortedUIDs objectAtIndex: i] stringValue];
+
[s appendString: @""];
+ [s appendFormat: @"%@+%@", folderId, itemId];
[s appendFormat: @"%@", folderId];
[s appendString: @""];
- itemId = [[sortedUIDs objectAtIndex: i] stringValue];
+
mailObject = [currentFolder lookupName: itemId inContext: context acquire: NO];
if ([mailObject isKindOfClass: [NSException class]])
@@ -2934,7 +2956,7 @@ void handle_eas_terminate(int signum)
[s appendFormat: @""];
}
- [s appendFormat: @"0-%d", total-1];
+ [s appendFormat: @"0-%d",(total ? total-1 : 0)];
[s appendFormat: @"%d", total];
[s appendString: @""];
[s appendString: @""];