Patch is based on https://github.com/cyrusimap/cyrus-imapd/pull/4736 and https://github.com/cyrusimap/cyrus-imapd/pull/4741