Files
swift-nio-imap/Examples/IMAPTool/Sources/IMAPToolLib/Message/MessageInfo.swift
T
Daniel Eggert 3cd39758d4 Use [UInt8] for small model fields
Small fields (mailbox names, atoms, quoted strings, envelope fields,
base64, URLs, the authentication challenge, catenateURL, ...) become
[UInt8]. The four raw-payload cases stay ByteBuffer until "Make payload
cases IMAPBuffer".

The grammar's string productions return [UInt8]. The parser still runs
on ParseBuffer, so its cursor methods copy out of the ByteBuffer for now.

Review focus: the model files' encoders and ModifiedUTF7 on arrays.

Updates IMAPTool and the IMAPCommands catenate-URL wrapper.

Public API breaks:
- ~38 public declarations ByteBuffer -> [UInt8].
- ModifiedUTF7.EncodingRoundtripError.buffer -> .bytes.
- Response.authenticationChallenge and ContinuationRequest.data are
  [UInt8]; an empty challenge is [].

Internal: writeBufferAsBase64 -> writeBytesAsBase64; the ByteBuffer
writeIMAPString overload is gone.
2026-11-24 09:47:33 +09:00

150 lines
4.7 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 NIOIMAP
/// A summary of a fetched message, including its envelope and flags.
struct MessageInfo: Sendable, Hashable, Encodable {
/// The UID of the message.
var uid: UID
/// The RFC 2822 date string from the envelope.
var date: String?
/// The date as seconds since the Unix epoch.
var dateSeconds: Double?
/// The subject line of the message.
var subject: String?
/// The sender of the message.
var from: String?
/// The primary recipients of the message.
var to: String?
/// The carbon-copy recipients of the message.
var cc: String?
/// The `Message-ID` header value.
var messageID: String?
/// The `In-Reply-To` header value.
var inReplyTo: String?
/// The flags set on the message.
var flags: [String]
func encode(to encoder: Encoder) throws {
var c = encoder.container(keyedBy: CodingKeys.self)
try c.encode(UInt32(uid), forKey: .uid)
try c.encodeIfPresent(date, forKey: .date)
try c.encodeIfPresent(dateSeconds, forKey: .dateSeconds)
try c.encodeIfPresent(subject, forKey: .subject)
try c.encodeIfPresent(from, forKey: .from)
try c.encodeIfPresent(to, forKey: .to)
try c.encodeIfPresent(cc, forKey: .cc)
try c.encodeIfPresent(messageID, forKey: .messageID)
try c.encodeIfPresent(inReplyTo, forKey: .inReplyTo)
try c.encode(flags, forKey: .flags)
}
enum CodingKeys: String, CodingKey {
case uid
case date
case dateSeconds
case subject
case from
case to
case cc
case messageID
case inReplyTo
case flags
}
}
extension MessageInfo {
init?(
uid: UID,
attributes: [MessageAttribute]
) {
var uid: UID = uid
var envelope: Envelope?
var flags: [Flag]?
for attribute in attributes {
switch attribute {
case .uid(let u):
uid = u
case .envelope(let e):
envelope = e
case .flags(let f):
flags = f
default:
break
}
}
guard
let envelope,
let flags
else { return nil }
self.init(uid: uid, envelope: envelope, flags: flags)
}
init(uid: UID, envelope: Envelope, flags: [Flag]) {
self.uid = uid
let date = envelope.date.map { String($0) }
self.date = date
if let date, let seconds = InternetMessageDate(date).parse() {
self.dateSeconds = seconds.timeIntervalSince1970
}
self.subject = envelope.subject.map { String(lossyUTF8: $0) }
self.from = envelope.from.isEmpty ? nil : String(envelope.from)
self.to = envelope.to.isEmpty ? nil : String(envelope.to)
self.cc = envelope.cc.isEmpty ? nil : String(envelope.cc)
self.messageID = envelope.messageID.map { String($0) }
self.inReplyTo = envelope.inReplyTo.map { String($0) }
self.flags = flags.map { String($0) }
}
}
extension String {
/// Decodes envelope bytes, replacing invalid UTF-8 rather than failing.
fileprivate init(lossyUTF8 bytes: [UInt8]) {
self.init(decoding: bytes, as: UTF8.self)
}
fileprivate init(_ list: [EmailAddressListElement]) {
self =
list
.map {
String($0)
}
.joined(separator: ", ")
}
fileprivate init(_ listElement: EmailAddressListElement) {
switch listElement {
case .singleAddress(let address):
self.init(address)
case .group(let group):
let groupName = String(lossyUTF8: group.groupName)
let members = group.children.map { String($0) }.joined(separator: ", ")
self = "\"\(groupName)\": {\(members)}"
}
}
fileprivate init(_ address: EmailAddress) {
switch (address.personName, address.mailbox, address.host) {
case (let name?, let mailbox?, let host?):
self = "\"\(String(lossyUTF8: name))\" <\(String(lossyUTF8: mailbox))@\(String(lossyUTF8: host))>"
case (_, let mailbox?, let host?):
self = "\(String(lossyUTF8: mailbox))@\(String(lossyUTF8: host))"
default:
self = "<>"
}
}
}