mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-28 05:14:27 +00:00
`TaggedCommand(tag: "A1 NOOP\r\nA2 DELETE INBOX\r\nA3", command: .noop)` was written verbatim, injecting a command; likewise `APPEND` start. `TaggedResponse(tag: "* BYE x\r\nA1", …)` injected a response. RFC 3501: `tag = 1*<any ASTRING-CHAR except "+">`. New `Command.Tag` replaces the `String` in `TaggedCommand`, `TaggedResponse`, `Response`, `CommandStreamPart`, `AppendCommand`, `BadCommand` and NIOIMAP's state machine errors; `init?(_:)` validates, literals trap. The parser already enforces the production. IMAPCommands converts its numeric tags.
98 lines
2.9 KiB
Swift
98 lines
2.9 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2026 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 Logging
|
|
import NIOIMAP
|
|
|
|
extension IMAPConnection {
|
|
/// A command tag that uniquely identifies an in-flight IMAP command.
|
|
public struct Tag: Hashable, Sendable {
|
|
var rawValue: UInt64
|
|
}
|
|
}
|
|
|
|
extension IMAPConnection.Tag: CustomStringConvertible {
|
|
public var description: String { String(self) }
|
|
}
|
|
|
|
extension IMAPConnection.Tag {
|
|
/// The first tag in a sequence.
|
|
public static let first = IMAPConnection.Tag(rawValue: 1)
|
|
|
|
/// Returns the next tag in the sequence.
|
|
public func makeNext() -> IMAPConnection.Tag {
|
|
IMAPConnection.Tag(rawValue: self.rawValue &+ 1)
|
|
}
|
|
}
|
|
|
|
extension IMAPConnection.Tag: Encodable {}
|
|
|
|
extension IMAPConnection.Tag {
|
|
/// Logger metadata identifying the command this tag belongs to.
|
|
///
|
|
/// Bound around every command handler — together with `imap.connection`, see
|
|
/// ``IMAPConnection/loggerMetadata(for:)`` — so that anything the handler logs through
|
|
/// `Logger.current` is attributed to the command it belongs to. The key is namespaced
|
|
/// because the metadata is merged into the caller's own logger.
|
|
var loggerMetadata: Logger.Metadata {
|
|
["imap.tag": "\(self)"]
|
|
}
|
|
}
|
|
|
|
extension String {
|
|
public init(_ tag: IMAPConnection.Tag) {
|
|
self = "A\(tag.rawValue)"
|
|
}
|
|
}
|
|
|
|
extension IMAPConnection.Tag {
|
|
public init?(_ text: String) {
|
|
guard
|
|
text.count < 25,
|
|
text.allSatisfy({ $0.isASCII }),
|
|
text.first == "A"
|
|
else { return nil }
|
|
let remainder = text[text.index(after: text.startIndex)...]
|
|
guard
|
|
remainder.allSatisfy({
|
|
guard
|
|
let v = $0.asciiValue,
|
|
0x30 <= v,
|
|
v <= 0x39
|
|
else { return false }
|
|
return true
|
|
}),
|
|
let value = UInt64(remainder)
|
|
else { return nil }
|
|
self.init(rawValue: value)
|
|
}
|
|
}
|
|
|
|
extension Command.Tag {
|
|
/// Creates the wire tag for a connection tag, for example `A1`.
|
|
init(_ tag: IMAPConnection.Tag) {
|
|
guard let tag = Command.Tag(String(tag)) else {
|
|
preconditionFailure("Invalid command tag: \(String(reflecting: String(tag)))")
|
|
}
|
|
self = tag
|
|
}
|
|
}
|
|
|
|
extension IMAPConnection.Tag {
|
|
/// Creates a connection tag from a wire tag; `nil` if this connection didn't generate it.
|
|
init?(_ tag: Command.Tag) {
|
|
self.init(String(tag))
|
|
}
|
|
}
|