10b7cb2fd2
- fix deferral resurfacing using VTODOParser.formatDateOnly instead of ISO8601 (date format mismatch) - add SELECT mailbox before UID STORE in storeFlags (IMAP protocol requirement) - pass credentials to SyncCoordinator so IDLE monitoring activates - add selectedItem tracking to MailViewModel, wire List selection in GTD views - fix startPeriodicSync to sleep-first, preventing duplicate sync on launch - add deinit cleanup for EventLoopGroup in IMAPConnection, SMTPConnection - use separate IMAP client for attachment downloads, avoid shared connection interference - remove [weak self] from IMAPIdleClient actor Task to prevent orphaned connections - fix isGTDPerspective to check selectedMailbox instead of items.isEmpty - fix fetchBody to use complete RFC822 fetch instead of BODY[TEXT] - reuse single IMAP connection per ActionQueue.flush() batch - add requiresIMAP to ActionPayload for connection batching - load task categories from label store instead of hardcoded empty array - suppress NIOSSLHandler Sendable warnings via Package.swift unsafeFlags - fix unused variable warnings across codebase Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
75 lines
2.2 KiB
Swift
75 lines
2.2 KiB
Swift
import NIO
|
|
import NIOIMAPCore
|
|
import NIOIMAP
|
|
import NIOSSL
|
|
|
|
actor IMAPConnection {
|
|
private let host: String
|
|
private let port: Int
|
|
private let group: EventLoopGroup
|
|
private var channel: Channel?
|
|
private let responseHandler: IMAPResponseHandler
|
|
|
|
init(host: String, port: Int) {
|
|
self.host = host
|
|
self.port = port
|
|
self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
|
self.responseHandler = IMAPResponseHandler()
|
|
}
|
|
|
|
deinit {
|
|
try? group.syncShutdownGracefully()
|
|
}
|
|
|
|
func connect() async throws {
|
|
let sslContext = try NIOSSLContext(configuration: TLSConfiguration.makeClientConfiguration())
|
|
let handler = responseHandler
|
|
let hostname = host
|
|
|
|
nonisolated(unsafe) let sslHandler = try NIOSSLClientHandler(context: sslContext, serverHostname: hostname)
|
|
let bootstrap = ClientBootstrap(group: group)
|
|
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
|
|
.channelInitializer { channel in
|
|
channel.pipeline.addHandlers([
|
|
sslHandler,
|
|
IMAPClientHandler(),
|
|
handler,
|
|
])
|
|
}
|
|
|
|
channel = try await bootstrap.connect(host: host, port: port).get()
|
|
try await handler.waitForGreeting()
|
|
}
|
|
|
|
func sendCommand(_ tag: String, command: CommandStreamPart) async throws -> [Response] {
|
|
guard let channel else { throw IMAPError.notConnected }
|
|
let handler = responseHandler
|
|
return try await withCheckedThrowingContinuation { continuation in
|
|
handler.sendCommand(tag: tag, continuation: continuation)
|
|
channel.writeAndFlush(IMAPClientHandler.Message.part(command), promise: nil)
|
|
}
|
|
}
|
|
|
|
/// Send multiple command parts as a single multi-part command (e.g. APPEND).
|
|
/// The tag must match the tag embedded in the first part.
|
|
func sendMultiPartCommand(_ tag: String, parts: [CommandStreamPart]) async throws -> [Response] {
|
|
guard let channel else { throw IMAPError.notConnected }
|
|
let handler = responseHandler
|
|
return try await withCheckedThrowingContinuation { continuation in
|
|
handler.sendCommand(tag: tag, continuation: continuation)
|
|
for part in parts {
|
|
channel.writeAndFlush(IMAPClientHandler.Message.part(part), promise: nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
func disconnect() async throws {
|
|
try await channel?.close()
|
|
channel = nil
|
|
}
|
|
|
|
func shutdown() async throws {
|
|
try await group.shutdownGracefully()
|
|
}
|
|
}
|