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>
95 lines
2.7 KiB
Swift
95 lines
2.7 KiB
Swift
import Foundation
|
|
import NIO
|
|
import NIOSSL
|
|
import Models
|
|
|
|
actor SMTPConnection {
|
|
private let host: String
|
|
private let port: Int
|
|
private let security: SMTPSecurity
|
|
private let group: EventLoopGroup
|
|
private var channel: Channel?
|
|
private let responseHandler: SMTPResponseHandler
|
|
|
|
init(host: String, port: Int, security: SMTPSecurity) {
|
|
self.host = host
|
|
self.port = port
|
|
self.security = security
|
|
self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
|
self.responseHandler = SMTPResponseHandler()
|
|
}
|
|
|
|
deinit {
|
|
try? group.syncShutdownGracefully()
|
|
}
|
|
|
|
func connect() async throws -> SMTPResponse {
|
|
let handler = responseHandler
|
|
let hostname = host
|
|
|
|
let bootstrap: ClientBootstrap
|
|
if security == .ssl {
|
|
let sslContext = try NIOSSLContext(configuration: TLSConfiguration.makeClientConfiguration())
|
|
nonisolated(unsafe) let sslHandler = try NIOSSLClientHandler(context: sslContext, serverHostname: hostname)
|
|
bootstrap = ClientBootstrap(group: group)
|
|
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
|
|
.channelInitializer { channel in
|
|
channel.pipeline.addHandlers([sslHandler, handler])
|
|
}
|
|
} else {
|
|
// STARTTLS: start plain, upgrade later
|
|
bootstrap = ClientBootstrap(group: group)
|
|
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
|
|
.channelInitializer { channel in
|
|
channel.pipeline.addHandler(handler)
|
|
}
|
|
}
|
|
|
|
do {
|
|
channel = try await bootstrap.connect(host: host, port: port).get()
|
|
} catch {
|
|
throw SMTPError.connectionFailed(error.localizedDescription)
|
|
}
|
|
|
|
return try await handler.waitForGreeting()
|
|
}
|
|
|
|
func sendCommand(_ command: String) async throws -> SMTPResponse {
|
|
guard let channel else { throw SMTPError.notConnected }
|
|
let handler = responseHandler
|
|
|
|
var buffer = channel.allocator.buffer(capacity: command.utf8.count + 2)
|
|
buffer.writeString(command + "\r\n")
|
|
channel.writeAndFlush(buffer, promise: nil)
|
|
|
|
return try await handler.waitForResponse()
|
|
}
|
|
|
|
func upgradeToTLS() async throws {
|
|
guard let channel else { throw SMTPError.notConnected }
|
|
let sslContext = try NIOSSLContext(configuration: TLSConfiguration.makeClientConfiguration())
|
|
let sslHandler = try NIOSSLClientHandler(context: sslContext, serverHostname: host)
|
|
do {
|
|
try await channel.pipeline.addHandler(sslHandler, position: .first).get()
|
|
} catch {
|
|
throw SMTPError.tlsUpgradeFailed
|
|
}
|
|
}
|
|
|
|
func sendRawBytes(_ data: Data) async throws {
|
|
guard let channel else { throw SMTPError.notConnected }
|
|
var buffer = channel.allocator.buffer(capacity: data.count)
|
|
buffer.writeBytes(data)
|
|
channel.writeAndFlush(buffer, promise: nil)
|
|
}
|
|
|
|
func disconnect() async throws {
|
|
try await channel?.close()
|
|
channel = nil
|
|
}
|
|
|
|
func shutdown() async throws {
|
|
try await group.shutdownGracefully()
|
|
}
|
|
}
|