Files
swift-nio-imap/Sources/NIOIMAPCore/Grammar/StoreModifier.swift
T
Daniel Eggert 2e535609b1 Add TaggedExtensionLabel for tagged-extension parameters
`.labelled(.init(key: "X\r\nA2 DELETE INBOX\r\nA3 NOOP", value: nil))`
was written verbatim, injecting a command into CREATE; likewise SELECT,
RENAME, FETCH, STORE, GETMETADATA, APPEND and SEARCH RETURN parameters.
An ESEARCH data extension key `"X\r\nA1 OK"` injected a response.

RFC 4466: `tagged-ext-label = tagged-label-fchar *tagged-label-char`.
New `TaggedExtensionLabel` replaces the `String` keys of `KeyValue` and
`OrderedDictionary` parameters; `init?(_:)` validates, literals trap on
invalid input. The parser already enforces the production.
2026-10-24 11:36:53 +02:00

81 lines
3.6 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
//
//===----------------------------------------------------------------------===//
/// A modifier that constrains when the `STORE` command modifies message metadata (RFC 3501 and extensions).
///
/// The `STORE` command supports optional modifiers that control whether the server actually performs the requested
/// operation. These modifiers are typically used for conditional storage operations to prevent conflicts when
/// multiple clients are modifying the same mailbox simultaneously.
///
/// Without modifiers, the server unconditionally applies the flag changes. With modifiers, the server only
/// applies changes if specific conditions are met, providing a form of optimistic locking for distributed clients.
///
/// ### Example
///
/// ```
/// C: A001 STORE 1 (UNCHANGEDSINCE 12345) +FLAGS (\Seen)
/// S: * 1 FETCH (FLAGS (\Seen) MODSEQ (12346))
/// S: A001 OK STORE completed
/// ```
///
/// The `(UNCHANGEDSINCE 12345)` portion represents the ``unchangedSince(_:)`` modifier. The server only
/// sets the `\Seen` flag if the message's current mod-sequence is 12345 or lower. If a different client has
/// modified the message (increasing its mod-sequence), the `STORE` operation is rejected.
///
/// - SeeAlso: [RFC 7162 Section 3.1.3](https://datatracker.ietf.org/doc/html/rfc7162#section-3.1.3)
public enum StoreModifier: Hashable, Sendable {
/// Only perform the store operation if the message's modification sequence is unchanged (RFC 7162 `CONDSTORE` extension).
///
/// Implements optimistic concurrency control. The server compares the message's current mod-sequence
/// with the specified value. If the message's mod-sequence is equal to or less than the specified value, the `STORE`
/// operation succeeds. Otherwise, the operation is rejected, indicating that another client has modified the message.
///
/// Prevents "lost updates" where one client's changes could overwrite another client's changes in a
/// multimailbox environment.
///
/// **Requires server capability:** ``Capability/condStore``
///
/// - SeeAlso: [RFC 7162 Section 3.1.3](https://datatracker.ietf.org/doc/html/rfc7162#section-3.1.3)
case unchangedSince(UnchangedSinceModifier)
/// A server extension modifier not defined in this library.
///
/// Captures future `STORE` modifiers defined by extensions, allowing forward compatibility
/// with new IMAP capabilities without requiring library updates.
case other(KeyValue<TaggedExtensionLabel, ParameterValue?>)
}
// MARK: - Encoding
extension EncodeBuffer {
@discardableResult mutating func writeStoreModifiers(_ array: [StoreModifier]) -> Int {
guard array.count > 0 else {
return 0
}
return self.writeArray(array, prefix: " ", separator: " ", suffix: "", parenthesis: true) { (element, self) in
self.writeStoreModifier(element)
}
}
@discardableResult mutating func writeStoreModifier(_ val: StoreModifier) -> Int {
switch val {
case .unchangedSince(let unchangedSince):
return self.writeUnchangedSinceModifier(unchangedSince)
case .other(let param):
return self.writeParameter(param)
}
}
}