// 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 = "" /// 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 /// 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 || (!user.isEmpty && !domain.isEmpty) } /// 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() c.user = user c.password = password c.domain = 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[..