From fad11967951b27bba6d44f0a7b6d2ad2750528f7 Mon Sep 17 00:00:00 2001 From: Niklas Arnitz Date: Mon, 29 Jun 2026 18:19:02 +0200 Subject: [PATCH] fix(sync): tolerate PERMANENTFLAGS IMAP errors Treat IMAP STATUS PERMANENTFLAGS failures as custom flags being unsupported instead of aborting account synchronization. Some servers, including iCloud, can fail this status query while regular mailbox sync otherwise works. Fixes: #12506 Assisted-by: Amp:gpt-5-codex Signed-off-by: Niklas Arnitz Amp-Thread-ID: https://ampcode.com/threads/T-019f140d-88ab-73ec-bbae-9b54eec909f1 Co-authored-by: Amp --- lib/Service/MailManager.php | 11 ++++++----- tests/Unit/Service/MailManagerTest.php | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/Service/MailManager.php b/lib/Service/MailManager.php index 1fb1ce58fa..bb8ad80c4f 100644 --- a/lib/Service/MailManager.php +++ b/lib/Service/MailManager.php @@ -759,11 +759,12 @@ public function isPermflagsEnabled(Horde_Imap_Client_Socket $client, Account $ac try { $capabilities = $client->status($mailbox, Horde_Imap_Client::STATUS_PERMFLAGS); } catch (Horde_Imap_Client_Exception $e) { - throw new ServiceException( - 'Could not get message flag options from IMAP: ' . $e->getMessage(), - $e->getCode(), - $e - ); + $this->logger->debug('Could not get IMAP PERMANENTFLAGS support; treating custom flags as unsupported', [ + 'exception' => $e, + 'mailbox' => $mailbox, + ]); + + return false; } return (is_array($capabilities) === true && array_key_exists('permflags', $capabilities) === true && in_array("\*", $capabilities['permflags'], true) === true); } diff --git a/tests/Unit/Service/MailManagerTest.php b/tests/Unit/Service/MailManagerTest.php index 5bd24acc40..3c3df9e7bb 100644 --- a/tests/Unit/Service/MailManagerTest.php +++ b/tests/Unit/Service/MailManagerTest.php @@ -10,6 +10,7 @@ namespace OCA\Mail\Tests\Unit\Service; use ChristophWurst\Nextcloud\Testing\TestCase; +use Horde_Imap_Client_Exception; use Horde_Imap_Client_Socket; use OCA\Mail\Account; use OCA\Mail\Attachment; @@ -404,6 +405,24 @@ public function testIsPermflagsEnabledFalse(): void { $this->assertFalse($this->manager->isPermflagsEnabled($client, $account, 'INBOX')); } + public function testIsPermflagsEnabledReturnsFalseOnImapError(): void { + $account = $this->createStub(Account::class); + $client = $this->createMock(Horde_Imap_Client_Socket::class); + + $client->expects($this->once()) + ->method('status') + ->with('INBOX', \Horde_Imap_Client::STATUS_PERMFLAGS) + ->willThrowException(new Horde_Imap_Client_Exception('IMAP error reported by server.')); + $this->logger->expects($this->once()) + ->method('debug') + ->with( + 'Could not get IMAP PERMANENTFLAGS support; treating custom flags as unsupported', + $this->callback(static fn (array $context): bool => $context['mailbox'] === 'INBOX' && $context['exception'] instanceof Horde_Imap_Client_Exception), + ); + + $this->assertFalse($this->manager->isPermflagsEnabled($client, $account, 'INBOX')); + } + public function testRemoveFlag(): void { $client = $this->createStub(Horde_Imap_Client_Socket::class); $account = $this->createStub(Account::class);