// SPDX-License-Identifier: AGPL-3.0-only // Copyright (C) 2026 Neo-Desktop import Foundation /// How the modem runs (the Android app's ModemSettings, the lines iOS has: /// a SIP account, or another phone with the app on the same Wi-Fi). public struct ModemSettings: Codable, Equatable, Sendable { public enum Line: String, Codable, Sendable { /// a SIP account; another phone with the app on Wi-Fi; nothing at all (the modem on its own) case sip, wifi, none } /// the SIP account public var user = "" public var password = "" /// the registrar's or proxy's host[:port] public var domain = "" /// SIP with no account (anonymous): nothing registered, calls dialled to a SIP address as it is /// (sip:user@host, or user@host) from "Anonymous" , calls that come /// straight to the phone's address taken; the account's fields kept, not used public var anonymous = false /// the local SIP port public var sipPort = 5060 /// register and take calls (on Wi-Fi: take the other phone's) public var listen = false /// fax calls may go over to T.38 public var t38 = true /// the modem's country (empty: USA) public var country = "" /// AT commands sent each time it starts (SIP has no dial tone: ATX3) public var initCommands = "ATX3" /// the modem's log level (0 errors .. 5), SIP's (0 none .. 4 the SIP messages) public var logLevel = 0 public var sipLogLevel = 1 /// the modem's speaker on the phone's: the dialling and the handshake heard public var speaker = true /// the modem's line: a SIP account, another phone with the app on Wi-Fi, or none public var line = Line.sip /// the other phone to call (host[:port]), and the PIN between the two public var peerAddress = "" public var peerPort = 5070 public var peerPin = "" /// fax: the identity sent (CSI/TSI), error correction, the fastest rate, fine resolution, /// ringing calls answered as faxes public var faxIdent = "" public var faxEcm = true public var faxMaxBps = 14400 public var faxFine = true public var faxAutoAnswer = false /// voice calls: the answering machine takes calls nobody answers (off until turned on), after this many /// rings, a message this long at most (s) public var voicemail = false public var voicemailRings = 4 public var voicemailMaxSeconds = 120 /// dial-up internet: the ISP's number, login, its login script (chat, if any), the modem's commands first public var ispNumber = "" public var ispUser = "" public var ispPassword = "" public var ispScript = "" public var ispInit = "" /// the defaults public init() {} /// the settings' secrets, kept apart from the rest (the Keychain), by name public static let secretKeys: [(String, WritableKeyPath)] = [ ("sip-password", \.password), ("peer-pin", \.peerPin), ("isp-password", \.ispPassword), ] /// the line another phone's (or none), not SIP public var peer: Bool { line != .sip } /// no line: the modem on its own (AT commands, the terminal); a dial gets NO DIALTONE public var noLine: Bool { line == .none } /// enough to start: an account (or the line another phone's) public var complete: Bool { peer || anonymous || (!user.isEmpty && !domain.isEmpty) } /// the SIP line, in a line: the account, or anonymous public var sipSummary: String { if anonymous { return "anonymous (no account)" } return complete ? "\(user)@\(domain)" : "not set up" } /// [address] as D-Modem dials a SIP address as it is: with sip: before it when it has no scheme public static func sipAddress(_ address: String) -> String { let a = address.trimmingCharacters(in: .whitespaces) let l = a.lowercased() return l.hasPrefix("sip:") || l.hasPrefix("sips:") ? a : "sip:" + a } /// the commands sent as the modem starts (empty: none) public func startCommands() -> String { initCommands.trimmingCharacters(in: .whitespacesAndNewlines) } /// the engine's configuration for these, with the DNS servers the network has public func engineConfig(dns: [String] = [], dataFile: String = "") -> EngineConfig { var c = EngineConfig() // (anonymous: no account, D-Modem's SIP then registers nothing and dials SIP addresses as they are) c.user = anonymous ? "" : user c.password = anonymous ? "" : password c.domain = anonymous ? "" : domain c.sipPort = sipPort c.listen = listen c.t38 = t38 c.dns = dns c.country = country c.dataFile = dataFile c.logLevel = logLevel c.sipLogLevel = sipLogLevel c.peer = peer return c } /// "user:password@host[:port]" (as dmodemd's SIP_LOGIN): its parts, nil if it isn't one public static func parseLogin(_ s: String) -> (user: String, password: String, domain: String)? { let t = s.trimmingCharacters(in: .whitespaces) // (the password may have an @ in it: the last one ends it) guard let at = t.range(of: "@", options: .backwards), let colon = t[..