mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-22 04:30:43 +00:00
99 lines
3.0 KiB
Swift
99 lines
3.0 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 IMAPCommands
|
|
#if canImport(FoundationEssentials)
|
|
import FoundationEssentials
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
import NIO
|
|
import NIOIMAP
|
|
|
|
extension ConnectionProtocol {
|
|
/// Creates the mailbox unless it already exists, then runs `LIST` and `STATUS` on it.
|
|
func createIfNeededAndList(
|
|
capabilities: [Capability],
|
|
mailbox: NewMailboxAction
|
|
) async throws -> MailboxInfoAndStatus {
|
|
switch mailbox {
|
|
case .alreadyExists(let name):
|
|
return try await listAlreadyExisting(
|
|
capabilities: capabilities,
|
|
mailbox: name
|
|
)
|
|
case .create(let name, let parameters):
|
|
return try await createAndList(
|
|
capabilities: capabilities,
|
|
mailbox: name,
|
|
parameters: parameters
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Creates the mailbox and then runs `LIST` and `STATUS` on it.
|
|
func createAndList(
|
|
capabilities: [Capability],
|
|
mailbox: MailboxName,
|
|
parameters _parameters: [CreateParameter]
|
|
) async throws -> MailboxInfoAndStatus {
|
|
let parameters: [CreateParameter]
|
|
if capabilities.contains(.createSpecialUse) {
|
|
parameters = _parameters
|
|
} else {
|
|
if !_parameters.isEmpty {
|
|
writeStatus("Server does not support CREATE-SPECIAL-USE")
|
|
}
|
|
parameters = []
|
|
}
|
|
|
|
let createText = try await send(.create(mailbox, parameters)) { tag, responses in
|
|
writeStatus("Did send CREATE mailbox '\(mailbox)' with tag \(tag)")
|
|
return try await responses.waitForCompletion()
|
|
}.getOK()
|
|
writeStatus("Did CREATE mailbox '\(mailbox)': \(createText)")
|
|
|
|
guard
|
|
let info = try await listMailbox(
|
|
capabilities: capabilities,
|
|
mailbox: mailbox
|
|
)
|
|
else { throw NoListResponseAfterCreate(mailbox: mailbox) }
|
|
return info
|
|
}
|
|
}
|
|
|
|
struct NoListResponseAfterCreate: Swift.Error {
|
|
var mailbox: MailboxName
|
|
}
|
|
|
|
extension ConnectionProtocol {
|
|
private func listAlreadyExisting(
|
|
capabilities: [Capability],
|
|
mailbox: MailboxName,
|
|
) async throws -> MailboxInfoAndStatus {
|
|
guard
|
|
let info = try await listMailbox(
|
|
capabilities: capabilities,
|
|
mailbox: mailbox
|
|
)
|
|
else { throw NoListResponseForExistingMailbox(mailbox: mailbox) }
|
|
return info
|
|
}
|
|
}
|
|
|
|
struct NoListResponseForExistingMailbox: Swift.Error {
|
|
var mailbox: MailboxName
|
|
}
|