From 1ed977528c8af4155903edeaae7301cc3e5315f4 Mon Sep 17 00:00:00 2001 From: Jorge Lobo <12179834+jorgeavlobo@users.noreply.github.com> Date: Tue, 10 Nov 2026 08:27:40 +0000 Subject: [PATCH] fix(eas): answer Sync of non-selectable mail folders as empty folders FolderSync also advertises mail folders that can't be opened, so that their subfolders keep their place in the hierarchy: the "Other Users"/"Shared" namespace roots, Dovecot's per-user parents ("Shared/jdoe@example.com") and other \Noselect or \NonExistent parents. A Sync with SyncKey 0 on them gets no collection, because the IMAP STATUS behind -collectionFromId:type: fails, so the reply carries no SyncKey for them (an empty HTTP 200 on 12.1 and later). iOS then repeats its initial Sync forever, 50 to 170 times per minute per device. Answer these folders exactly like an empty folder: SyncKey -1 and Status 1, pushed on the initial Sync. Any other SyncKey dates from when the folder could be opened, so ask for a complete resync (Status 3, also pushed) and the client drops what it still holds. A folder qualifies only if the IMAP server confirms it can't be opened (LIST \Noselect/\NonExistent, or a namespace root that LIST omits); anything else, such as a real folder that failed to open, keeps the current behaviour. Fixes #5872 --- ActiveSync/SOGoActiveSyncDispatcher+Sync.m | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/ActiveSync/SOGoActiveSyncDispatcher+Sync.m b/ActiveSync/SOGoActiveSyncDispatcher+Sync.m index 57fae8b10..dbd68a2ec 100644 --- a/ActiveSync/SOGoActiveSyncDispatcher+Sync.m +++ b/ActiveSync/SOGoActiveSyncDispatcher+Sync.m @@ -43,6 +43,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #import #import +#import +#import #import #import @@ -50,6 +52,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #import #import #import +#import #import #import @@ -63,6 +66,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #import +#import #import #import @@ -1866,6 +1870,35 @@ FIXME } } +// +// FolderSync also advertises mail folders that can't be opened, so that their +// subfolders keep their place in the hierarchy: the "Other Users" or "Shared" +// namespace roots, Dovecot's per-user parents ("Shared/jdoe@example.com") and +// other \Noselect or \NonExistent parents. Returns NO if the IMAP server can't +// confirm it, as theFolderName may then be a real folder we failed to open. +// +- (BOOL) _isNonSelectableMailFolder: (NSString *) theFolderName +{ + SOGoMailAccount *accountFolder; + NSDictionary *result; + NSArray *flags; + + accountFolder = [[[[context activeUser] homeFolderInContext: context] lookupName: @"Mail" inContext: context acquire: NO] + lookupName: @"0" inContext: context acquire: NO]; + + result = [[[accountFolder imap4Connection] client] list: @"" pattern: theFolderName]; + if (![[result objectForKey: @"result"] boolValue]) + return NO; + + flags = [[result objectForKey: @"list"] objectForKey: [NSString stringWithFormat: @"/%@", theFolderName]]; + if (flags) + return ([flags containsObject: @"noselect"] || [flags containsObject: @"nonexistent"]); + + // Cyrus doesn't return the namespace roots in LIST + return ([[accountFolder otherUsersFolderNamespaces] containsObject: theFolderName] || + [[accountFolder sharedFolderNamespaces] containsObject: theFolderName]); +} + // // // @@ -1896,6 +1929,9 @@ FIXME if (collection == nil) { + NSString *nameInCache; + BOOL knownKey; + // Collection not found - next folderSync will do the cleanup //NSLog(@"Sync Collection not found %@ %@", collectionId, realCollectionId); //Outlook doesn't like following response @@ -1904,6 +1940,38 @@ FIXME //[theBuffer appendFormat: @"%@", collectionId]; //[theBuffer appendFormat: @"%d", 8]; //[theBuffer appendString: @""]; + + // Mail folders that FolderSync advertises but that can't be opened (see + // -_isNonSelectableMailFolder:) are answered as empty folders. Without a SyncKey + // in the reply, clients like iOS repeat their initial Sync forever (bug #5872). + nameInCache = nil; + if (realCollectionId) + nameInCache = [NSString stringWithFormat: @"folder%@", realCollectionId]; + + if (folderType == ActiveSyncMailFolder && nameInCache && + [[imapFolderGUIDS objectForKey: nameInCache] isEqualToString: nameInCache] && + [self _isNonSelectableMailFolder: realCollectionId]) + { + if (debugOn) + [self logWithFormat: @"EAS - processSyncCollection: %@ can't be opened, answering as an empty folder", realCollectionId]; + + // We only ever hand out SyncKey -1 for these folders. Any other key dates from + // when the folder could be opened: ask for a complete resync (Status 3) so that + // the client drops the messages and pending changes it still holds for it. + knownKey = ([syncKey isEqualToString: @"0"] || [syncKey isEqualToString: @"-1"]); + + [theBuffer appendString: @""]; + [theBuffer appendString: @"Email"]; + [theBuffer appendFormat: @"%@", (knownKey ? @"-1" : @"0")]; + [theBuffer appendFormat: @"%@", collectionId]; + [theBuffer appendFormat: @"%d", (knownKey ? 1 : 3)]; + [theBuffer appendString: @""]; + + // Like for any folder, only the first SyncKey or a resync is a change to push. + if (![syncKey isEqualToString: @"-1"]) + *changeDetected = YES; + } + return; }