Files
Daniel Eggert d67ffdf153 Fix encode/parse round-trip bugs (#868)
* Separate LIST from its independent select options

`.listIndependent([.remote], …)` encoded as `LIST(REMOTE) …`.

RFC 5258: `list = "LIST" [SP list-select-opts] SP mailbox SP mbox-or-pat`.

* Keep leading zeros in fractional seconds

`12:34:56.05` parsed as `fraction: 5` and re-encoded as `12:34:56.5`,
changing the value.

RFC 3339: `time-secfrac = "." 1*DIGIT`. `FullTime.fraction` becomes
`FullTime.Fraction`, which stores the digits; `init?(_:)` validates,
literals trap, `String.init` reads.

* Model the RFC 3339 time offset

`;EXPIRE=2025-12-31T23:59:59Z` failed to parse inside a URL: the
parser stopped before `Z`. The encoder never wrote an offset.

RFC 4467: `expire = ";EXPIRE=" date-time`; RFC 3339: `full-time =
partial-time time-offset`, `time-offset = "Z" / time-numoffset`. Adds
the required `FullTime.offset`.

* Encode LIST extended data

`MailboxInfo.extensions` was parsed but never written, so
`* LIST (\Marked) "/" "Fruit" ("CHILDINFO" ("SUBSCRIBED"))` lost
`("CHILDINFO" …)` on re-encode.

RFC 5258: `mailbox-list = … mailbox [SP mbox-list-extended]`.

* Round-trip multi-value LIST option values

`.array(["a", "b"])` encoded as `(("a" "b"))`; the parser rejected both
that and `("a" "b")`: it took the `astring` branch first and could not
backtrack.

RFC 5258: `option-value = "(" option-val-comp ")"`, `option-val-comp =
astring / option-val-comp *(SP option-val-comp) / "(" option-val-comp
")"`. The parser reads a sequence of elements; the encoder writes
several values inside the `option-value` parentheses.

* Validate JMAPACCESS URLs

`.jmapAccess(URL(string: "jmap", relativeTo: base))` encoded as
`JMAPACCESS "jmap -- https://example.com/"` (`URL.description`); `http`
URLs were written though the parser rejects them.

The payload becomes `ResponsePayload.JMAPAccessURL`: `init?(_:)` resolves
against the base and requires `https`, a host, and no escaping inside
`quoted`; `URL.init` reads. The parser uses the same check.

* Make RESETKEY mechanisms require a mailbox

`.resetKey(mailbox: nil, mechanisms: [.internal])` encoded as bare
`RESETKEY`, silently dropping the mechanisms and resetting every key.

RFC 4467: `resetkey = "RESETKEY" [SP mailbox *(SP mechanism)]`. The
payload becomes `Command.ResetKeyScope`: `.allMailboxes` or
`.mailbox(_:mechanisms:)`.

* Hoist a #require the compiler flagged as redundant

* Cite RFC 7162 for SEARCH MODSEQ entry types

The docs described these as RFC 5464 GETMETADATA options, with
GETMETADATA examples. The grammar is RFC 7162 `search-modseq-ext`.

* Fix doc nits

- `ExtendedSearchScopeOptions.content` is a dictionary, not an array.
- `NamespaceDescription` init in the `NamespaceResponse` example needs
  `try` and `responseExtensions:`.
- `TaggedExtensionLabel` example: `SELECT INBOX (CONDSTORE)` parses as
  `.condStore`; use `RELEVANCY`.
- Stray `’` in a parser hint.
2026-11-04 17:41:53 +00:00

81 lines
3.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import struct OrderedCollections.OrderedDictionary
/// A search criterion that filters messages based on modification sequence value (RFC 7162 CONDSTORE extension).
///
/// The modification sequence (mod-sequence) is a per-message value that increments each time a message's
/// metadata (such as flags) is modified. This type allows clients to request only messages that have been
/// modified since a specific mod-sequence value, enabling efficient synchronization of mailbox state changes.
///
/// **Requires server capability:** ``Capability/condStore``
///
/// When used in a `SEARCH` or `UID SEARCH` command with the `MODSEQ` criterion, this type encapsulates
/// the search parameters including any extension-specific entry attributes and the minimum mod-sequence value
/// to match.
///
/// ### Example
///
/// ```
/// C: A001 SEARCH MODSEQ 12345
/// S: * ESEARCH (TAG "A001") ALL 2,5:7 MODSEQ 12352
/// S: A001 OK SEARCH completed
/// ```
///
/// The `SEARCH MODSEQ 12345` clause is represented by a ``SearchModificationSequence`` with
/// `sequenceValue: 12345` and empty `extensions`. The response includes the highest mod-sequence
/// value (``SearchReturnData/modificationSequence(_:)``) of all returned messages.
///
/// - SeeAlso: [RFC 7162 Section 3.1.5](https://datatracker.ietf.org/doc/html/rfc7162#section-3.1.5)
/// - SeeAlso: ``SearchKey``
/// - SeeAlso: ``ModificationSequenceValue``
public struct SearchModificationSequence: Hashable, Sendable {
/// The flag whose modification sequence to test, and which metadata items to consider:
/// `search-modseq-ext = SP entry-name SP entry-type-req`. Empty tests the message's
/// modification sequence.
///
/// RFC 7162 allows at most one entry.
public var extensions: OrderedDictionary<EntryFlagName, EntryKindRequest>
/// The minimum modification sequence value that matching messages must have.
///
/// Messages returned by the search will have a mod-sequence value greater than or equal to this value.
public var sequenceValue: ModificationSequenceValue
/// Creates a new `SearchModificationSequence`.
/// - parameter extensions: The flag and metadata item type to test; at most one, usually empty
/// - parameter sequenceValue: The minimum modification sequence value to match
public init(
extensions: OrderedDictionary<EntryFlagName, EntryKindRequest>,
sequenceValue: ModificationSequenceValue
) {
self.extensions = extensions
self.sequenceValue = sequenceValue
}
}
// MARK: - Encoding
extension EncodeBuffer {
@discardableResult mutating func writeSearchModificationSequence(_ data: SearchModificationSequence) -> Int {
self.writeString("MODSEQ")
+ self.writeOrderedDictionary(data.extensions, separator: "", parenthesis: false) {
(element, self) -> Int in
self.writeSpace() + self.writeEntryFlagName(element.key) + self.writeSpace()
+ self.writeEntryKindRequest(element.value)
} + self.writeSpace() + self.writeModificationSequenceValue(data.sequenceValue)
}
}