//===----------------------------------------------------------------------===// // // 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 NIO.ByteBuffer /// Message data returned in an untagged server response. /// /// These responses convey information about messages independently of FETCH responses. /// They report changes to mailbox state such as message expunges, vanished messages due to /// CONDSTORE resynchronization, or URL-related operations. /// /// ### Example /// /// ``` /// S: * 3 EXPUNGE /// S: * VANISHED 1,4,9 /// S: * VANISHED (EARLIER) 5,7,11 /// ``` /// /// The first line indicates message 3 was permanently removed (``expunge(_:)`` case). /// The second indicates UIDs 1, 4, 9 were expunged recently (``vanished(_:)`` case). /// The third indicates UIDs 5, 7, 11 were expunged earlier (``vanishedEarlier(_:)`` case from RFC 7162 QRESYNC). /// /// - SeeAlso: [RFC 3501 Section 7.4.1 EXPUNGE](https://datatracker.ietf.org/doc/html/rfc3501#section-7.4.1) /// - SeeAlso: [RFC 7162 VANISHED Response](https://datatracker.ietf.org/doc/html/rfc7162) /// - SeeAlso: [RFC 4467 URLAUTH GENURLAUTH and URLFETCH](https://datatracker.ietf.org/doc/html/rfc4467) public enum MessageData: Hashable, Sendable { /// A message has been permanently removed from the mailbox. /// /// The EXPUNGE response indicates that the message at the specified sequence number /// no longer exists. Clients must renumber remaining messages accordingly. /// Not sent for UID-based commands. /// /// - SeeAlso: [RFC 3501 Section 7.4.1 EXPUNGE](https://datatracker.ietf.org/doc/html/rfc3501#section-7.4.1) case expunge(SequenceNumber) /// UIDs of recently expunged messages (RFC 7162 CONDSTORE). /// /// The VANISHED response reports UIDs of messages that have been expunged from the mailbox /// and whose modification sequence is newer than the specified MODSEQ value. This response /// is used with the QRESYNC extension for efficient synchronization. /// /// - SeeAlso: [RFC 7162 VANISHED Response](https://datatracker.ietf.org/doc/html/rfc7162) case vanished(UIDSet) /// UIDs of messages expunged earlier (RFC 7162 QRESYNC). /// /// The VANISHED (EARLIER) response reports UIDs of messages that were expunged before the /// last synchronization point. This response is sent in response to a UID FETCH VANISHED /// command or a SELECT/EXAMINE QRESYNC parameter. /// /// - SeeAlso: [RFC 7162 QRESYNC Extension](https://datatracker.ietf.org/doc/html/rfc7162) case vanishedEarlier(UIDSet) /// Authorized URLs generated by ``generateAuthorizedURL(_:)`` command (RFC 4467). /// /// Contains an array of `URLAUTH`-authorized IMAP URLs that clients can use for authenticated access. /// /// - SeeAlso: [RFC 4467 URLAUTH](https://datatracker.ietf.org/doc/html/rfc4467) case generateAuthorizedURL([ByteBuffer]) /// Message data fetched via IMAP URL (RFC 4467). /// /// Contains an array of URLFetchData pairs mapping URLs to their corresponding message data. /// /// - SeeAlso: [RFC 4467 URLFETCH Response](https://datatracker.ietf.org/doc/html/rfc4467) case urlFetch([URLFetchData]) } // MARK: - Encoding extension EncodeBuffer { @discardableResult mutating func writeMessageData(_ data: MessageData) -> Int { switch data { case .expunge(let number): return self.writeSequenceNumber(number) + self.writeString(" EXPUNGE") case .vanished(let set): return self.writeString("VANISHED ") + self.writeUIDSet(set) case .vanishedEarlier(let set): return self.writeString("VANISHED (EARLIER) ") + self.writeUIDSet(set) case .generateAuthorizedURL(let array): return self.writeString("GENURLAUTH") + self.writeArray(array, prefix: " ", parenthesis: false) { data, buffer in buffer.writeIMAPString(data) } case .urlFetch(let array): // `"URLFETCH" 1*(SP url-full SP nstring)`, not parenthesized. return self.writeString("URLFETCH") + self.writeArray(array, prefix: " ", parenthesis: false) { data, buffer in buffer.writeURLFetchData(data) } } } @discardableResult mutating func writeMessageDataEnd(_: MessageData) -> Int { self.writeString(")") } }