diff --git a/AnotherIM/View/StartScreen.swift b/AnotherIM/View/StartScreen.swift index abb84b9..1c0028f 100644 --- a/AnotherIM/View/StartScreen.swift +++ b/AnotherIM/View/StartScreen.swift @@ -79,12 +79,18 @@ struct StartScreen: View { } } -final class TestStorage: XMPPClientStorageProtocol { - func updateCredentialsByUUID(_ uuid: UUID, _ credentials: XMPPClientCredentials) async { - print(uuid, credentials) +final class TestStorage: XMPPStorage { + private var rosterVer: [String: String] = [:] + + func getRosterVersion(jid: JID) async -> String? { + rosterVer[jid.bare] } - func getCredentialsByUUID(_ uuid: UUID) async -> XMPPClientCredentials? { + func setRosterVersion(jid: JID, ver: String) async { + rosterVer[jid.bare] = ver + } + + func getCredentialsByUUID(_ uuid: UUID) async -> Credentials? { print(uuid) return ["password": pass] } diff --git a/AnotherIM/xmpp/XMPPClient.swift b/AnotherIM/xmpp/XMPPClient.swift index c82353a..157a835 100644 --- a/AnotherIM/xmpp/XMPPClient.swift +++ b/AnotherIM/xmpp/XMPPClient.swift @@ -92,7 +92,7 @@ struct ClientState: Codable & Equatable { final class XMPPClient { private var state = ClientState.initial private let logger = ClientLogger() - private let storage: XMPPClientStorageProtocol + private let storage: XMPPStorage private lazy var modules: [any XmppModule] = [ SRVResolverModule(), ConnectionModule(self.fire), @@ -104,7 +104,7 @@ final class XMPPClient { RosterModule() ] - init(storage: any XMPPClientStorageProtocol, userAgent: UserAgent) { + init(storage: any XMPPStorage, userAgent: UserAgent) { self.storage = storage state.userAgent = userAgent } diff --git a/AnotherIM/xmpp/XMPPClientStorageProtocol.swift b/AnotherIM/xmpp/XMPPClientStorageProtocol.swift deleted file mode 100644 index 2630c2e..0000000 --- a/AnotherIM/xmpp/XMPPClientStorageProtocol.swift +++ /dev/null @@ -1,22 +0,0 @@ -import Foundation - -typealias XMPPClientStorageProtocol = - AnyObject & - XMPPClientStorageCredentials & - XMPPClientStorageHistory - -// For storing credentials -typealias XMPPClientCredentials = [String: String] -protocol XMPPClientStorageCredentials: AnyObject { - func updateCredentialsByUUID(_ uuid: UUID, _ credentials: XMPPClientCredentials) async - func getCredentialsByUUID(_ uuid: UUID) async -> XMPPClientCredentials? -} - -// For storing stanza history -protocol XMPPClientStorageHistory: - XMPPClientStorageHistoryIqs & - XMPPClientStorageHistoryMessages & - XMPPClientStorageHistoryPresences {} -protocol XMPPClientStorageHistoryIqs: AnyObject {} -protocol XMPPClientStorageHistoryMessages: AnyObject {} -protocol XMPPClientStorageHistoryPresences: AnyObject {} diff --git a/AnotherIM/xmpp/XMPPStorage.swift b/AnotherIM/xmpp/XMPPStorage.swift new file mode 100644 index 0000000..e3bca87 --- /dev/null +++ b/AnotherIM/xmpp/XMPPStorage.swift @@ -0,0 +1,18 @@ +import Foundation + +typealias Credentials = [String: String] + +protocol XMPPStorage: AnyObject { + // credentials + func getCredentialsByUUID(_ uuid: UUID) async -> Credentials? + + // roster + func getRosterVersion(jid: JID) async -> String? + func setRosterVersion(jid: JID, ver: String) async + + // messages + + // omemo + + // whatever else +} diff --git a/AnotherIM/xmpp/modules/auth/AuthorizationMechanisms.swift b/AnotherIM/xmpp/modules/auth/AuthorizationMechanisms.swift index 1fbe224..7a83e98 100644 --- a/AnotherIM/xmpp/modules/auth/AuthorizationMechanisms.swift +++ b/AnotherIM/xmpp/modules/auth/AuthorizationMechanisms.swift @@ -42,7 +42,7 @@ protocol AuthorizationMechanism { final class AuthorizationMechanismImpl: AuthorizationMechanism { private let type: AuthorizationMechanismType private let jid: JID - private let credentials: XMPPClientCredentials? + private let credentials: Credentials? private let userAgent: UserAgent private let saslType: SaslType private let channelBind: String? @@ -52,7 +52,7 @@ final class AuthorizationMechanismImpl: AuthorizationMechanism { private var clientNonce = "" private var serverSignature = "" - init(type: AuthorizationMechanismType, jid: JID, credentials: XMPPClientCredentials?, saslType: SaslType, userAgent: UserAgent, channelBind: String?, inlines: XMLElement?) { + init(type: AuthorizationMechanismType, jid: JID, credentials: Credentials?, saslType: SaslType, userAgent: UserAgent, channelBind: String?, inlines: XMLElement?) { self.type = type self.jid = jid self.credentials = credentials diff --git a/AnotherIM/xmpp/modules/auth/AuthorizationModule.swift b/AnotherIM/xmpp/modules/auth/AuthorizationModule.swift index d43c98b..7624e97 100644 --- a/AnotherIM/xmpp/modules/auth/AuthorizationModule.swift +++ b/AnotherIM/xmpp/modules/auth/AuthorizationModule.swift @@ -17,10 +17,10 @@ enum AuthorizationError: Error { final class AuthorizationModule: XmppModule { let id = "Authorization module" - private weak var storage: (any XMPPClientStorageCredentials)? + private weak var storage: (any XMPPStorage)? private var mechanism: AuthorizationMechanism? - init(_ storage: any XMPPClientStorageCredentials) { + init(_ storage: any XMPPStorage) { self.storage = storage } @@ -79,7 +79,7 @@ private extension AuthorizationModule { ["tls-exporter", "tls-server-end-point"] } - func selectBestAuthMechanism(_ xml: XMLElement, _ isPlainAllowed: Bool, _ state: ClientState, _ creds: XMPPClientCredentials?) async -> Event { + func selectBestAuthMechanism(_ xml: XMLElement, _ isPlainAllowed: Bool, _ state: ClientState, _ creds: Credentials?) async -> Event { await withCheckedContinuation { continuation in var sasl1: [AuthorizationMechanismType] = [] var channelBindings: [String] = [] diff --git a/AnotherIM/xmpp/modules/stanza/StanzaModule.swift b/AnotherIM/xmpp/modules/stanza/StanzaModule.swift index d5fa365..5ae723f 100644 --- a/AnotherIM/xmpp/modules/stanza/StanzaModule.swift +++ b/AnotherIM/xmpp/modules/stanza/StanzaModule.swift @@ -3,9 +3,9 @@ import Foundation final class StanzaModule: XmppModule { let id = "Stanza module" - private weak var storage: XMPPClientStorageHistory? + private weak var storage: XMPPStorage? - init(_ storage: any XMPPClientStorageHistory) { + init(_ storage: any XMPPStorage) { self.storage = storage } @@ -43,6 +43,7 @@ final class StanzaModule: XmppModule { // MARK: Save history private extension StanzaModule { func saveStanza(_ state: ClientState, _ stanza: Stanza, inbound: Bool) async { + guard let storage else { return } print(state, stanza, inbound) } }