import Foundation public enum SupportKind: Hashable, Sendable { case problem, feature var choiceTitle: String { switch self { case .problem: SupportText.text("Problem") case .feature: SupportText.text("Idea") } } var title: String { switch self { case .problem: SupportText.text("Report a Problem…") case .feature: SupportText.text("Suggest a Feature…") } } var subject: String { switch self { case .problem: SupportText.text("Parcel problem report") case .feature: SupportText.text("Parcel feature suggestion") } } var message: String { switch self { case .problem: SupportText.text("What happened? What did you expect to happen?") case .feature: SupportText.text("What would you like Parcel to do?") } } } struct SupportDiagnostics { let appVersion: String? let buildVersion: String? let osVersion: OperatingSystemVersion var runtime: SupportRuntimeDiagnostics? static func current(bundle: Bundle = .main) -> Self { Self( appVersion: safeVersion(bundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String), buildVersion: safeVersion(bundle.object(forInfoDictionaryKey: "CFBundleVersion") as? String), osVersion: ProcessInfo.processInfo.operatingSystemVersion, runtime: nil) } static func safeVersion(_ value: String?) -> String? { guard let value, !value.isEmpty, value.utf8.count <= 64, value.utf8.allSatisfy({ (48...57).contains($0) || $0 == 46 }), value.first != ".", value.last != ".", !value.contains("..") else { return nil } return value } var text: String { let unavailable = SupportText.text("Unavailable") let base = [ SupportText.text("Diagnostics"), String( format: SupportText.text("Parcel version: %@"), locale: .current, Self.safeVersion(appVersion) ?? unavailable), String( format: SupportText.text("Build: %@"), locale: .current, Self.safeVersion(buildVersion) ?? unavailable), String( format: SupportText.text("macOS: %lld.%lld.%lld"), locale: .current, Int64(osVersion.majorVersion), Int64(osVersion.minorVersion), Int64(osVersion.patchVersion)), ].joined(separator: "\n") guard let runtime else { return base } return base + "\n" + runtime.text } } struct SupportDraft { var kind: SupportKind let recipient: String var diagnostics: SupportDiagnostics var issuesURL = "https://tangled.org/vinnymac.dev/parcel/issues" var includeDiagnostics = false var usesEmail: Bool { !recipient.isEmpty } var submissionTitle: String { SupportText.text(usesEmail ? "Open Email Draft" : "Copy Message and Open Issues") } var readyToSubmit: Bool { !includeDiagnostics || diagnostics.runtime != nil } func submission() throws -> SupportSubmission { guard readyToSubmit else { throw SupportSubmissionError.diagnosticsUnavailable } if usesEmail { guard let url = mailtoURL() else { throw SupportSubmissionError.invalidEmail } return .email(url) } guard let components = URLComponents(string: issuesURL), components.scheme == "https", let host = components.host, !host.isEmpty, components.user == nil, components.password == nil, (components.fragment ?? "").isEmpty, !issuesURL.unicodeScalars.contains(where: CharacterSet.whitespacesAndNewlines.contains), let url = components.url else { throw SupportSubmissionError.invalidIssuesURL } return .issues(url, copiedText: body) } var body: String { let message = kind.message + "\n\n" return includeDiagnostics ? message + "\n" + diagnostics.text : message } static func validAddress(_ value: String) -> Bool { let parts = value.split(separator: "@", omittingEmptySubsequences: false) guard parts.count == 2, !parts[0].isEmpty, !parts[1].isEmpty, value.utf8.count <= 254 else { return false } let local = parts[0] let domain = parts[1] func asciiLetterOrDigit(_ byte: UInt8) -> Bool { (65...90).contains(byte) || (97...122).contains(byte) || (48...57).contains(byte) } guard local.utf8.allSatisfy({ asciiLetterOrDigit($0) || [46, 95, 37, 43, 45].contains($0) }), local.first != ".", local.last != ".", !local.contains("..") else { return false } let labels = domain.split(separator: ".", omittingEmptySubsequences: false) return labels.count >= 2 && labels.allSatisfy { !$0.isEmpty && $0.utf8.count <= 63 && $0.first != "-" && $0.last != "-" && $0.utf8.allSatisfy { asciiLetterOrDigit($0) || $0 == 45 } } } func mailtoURL() -> URL? { guard Self.validAddress(recipient), !includeDiagnostics || diagnostics.runtime != nil else { return nil } let allowed = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~") var addressAllowed = allowed addressAllowed.insert(charactersIn: "@") guard let address = recipient.addingPercentEncoding(withAllowedCharacters: addressAllowed), let subject = kind.subject.addingPercentEncoding(withAllowedCharacters: allowed), let body = body.addingPercentEncoding(withAllowedCharacters: allowed) else { return nil } return URL(string: "mailto:\(address)?subject=\(subject)&body=\(body)") } } enum SupportText { static func text(_ key: String) -> String { Bundle.module.localizedString(forKey: key, value: nil, table: "Support") } }