This commit is contained in:
fmodf 2024-09-16 17:58:28 +02:00
parent 40ebe72e1b
commit a60fae1a3a
5 changed files with 20 additions and 82 deletions

View file

@ -3,7 +3,10 @@ import GRDB
import Martin import Martin
import MartinOMEMO import MartinOMEMO
final class ClientMartinOMEMO { !!! https: // xmpp.org/extensions/xep-0384.html#usecases-announcing
final class ClientMartinOMEMO
{
let credentials: Credentials let credentials: Credentials
init(_ credentials: Credentials) { init(_ credentials: Credentials) {
@ -27,20 +30,14 @@ final class ClientMartinOMEMO {
OMEMOPreKey.wipe(account: credentials.bareJid) OMEMOPreKey.wipe(account: credentials.bareJid)
OMEMOSignedPreKey.wipe(account: credentials.bareJid) OMEMOSignedPreKey.wipe(account: credentials.bareJid)
OMEMOIdentity.wipe(account: credentials.bareJid) OMEMOIdentity.wipe(account: credentials.bareJid)
Settings.getFor(credentials.bareJid)?.wipeOmemoRegId() UserSettings.omemoDeviceId = 0
} }
let hasKeyPair = keyPair() != nil let hasKeyPair = keyPair() != nil
if wipe || localRegistrationId() == 0 || !hasKeyPair { if wipe || UserSettings.omemoDeviceId == 0 || !hasKeyPair {
let regId = context.generateRegistrationId() let regId: UInt32 = context.generateRegistrationId()
let address = SignalAddress(name: credentials.bareJid, deviceId: Int32(regId)) let address = SignalAddress(name: credentials.bareJid, deviceId: Int32(regId))
UserSettings.omemoDeviceId = regId
if var settings = Settings.getFor(credentials.bareJid) {
settings.omemoRegId = Int(regId)
settings.save()
} else {
Settings(bareJid: credentials.bareJid, omemoRegId: Int(regId)).save()
}
guard let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context), let publicKey = keyPair.publicKey else { guard let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context), let publicKey = keyPair.publicKey else {
return false return false
@ -51,6 +48,7 @@ final class ClientMartinOMEMO {
return save(address: address, fingerprint: fingerprint, own: true, data: keyPair.serialized()) return save(address: address, fingerprint: fingerprint, own: true, data: keyPair.serialized())
} }
print("omemoDeviceId \(UserSettings.omemoDeviceId)")
return true return true
} }
@ -165,7 +163,7 @@ extension ClientMartinOMEMO: SignalSessionStoreProtocol {
// MARK: - Identity // MARK: - Identity
extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? { func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? {
let deviceId = localRegistrationId() let deviceId = UserSettings.omemoDeviceId
guard deviceId != 0 else { guard deviceId != 0 else {
return nil return nil
} }
@ -188,11 +186,7 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
} }
func localRegistrationId() -> UInt32 { func localRegistrationId() -> UInt32 {
if let settings = Settings.getFor(credentials.bareJid) { UserSettings.omemoDeviceId
return UInt32(settings.omemoRegId)
} else {
return 0
}
} }
func save(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool { func save(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool {

View file

@ -3,6 +3,7 @@ import Foundation
import GRDB import GRDB
import Martin import Martin
import MartinOMEMO import MartinOMEMO
import UIKit
enum ClientState: Equatable { enum ClientState: Equatable {
enum ClientConnectionState { enum ClientConnectionState {
@ -226,9 +227,9 @@ extension Client {
private extension Client { private extension Client {
static func prepareConnection(_ credentials: Credentials, _ roster: RosterManager, _ chat: ChatManager) -> XMPPClient { static func prepareConnection(_ credentials: Credentials, _ roster: RosterManager, _ chat: ChatManager) -> XMPPClient {
let client = XMPPClient() let client = XMPPClient()
client.connectionConfiguration.resource = UIDevice.current.name
// register modules // register modules
// core modules RFC 6120
client.modulesManager.register(StreamFeaturesModule()) client.modulesManager.register(StreamFeaturesModule())
client.modulesManager.register(SaslModule()) client.modulesManager.register(SaslModule())
client.modulesManager.register(AuthModule()) client.modulesManager.register(AuthModule())
@ -236,20 +237,19 @@ private extension Client {
client.modulesManager.register(ResourceBinderModule()) client.modulesManager.register(ResourceBinderModule())
client.modulesManager.register(DiscoveryModule(identity: .init(category: "client", type: "iOS", name: Const.appName))) client.modulesManager.register(DiscoveryModule(identity: .init(category: "client", type: "iOS", name: Const.appName)))
// messaging modules RFC 6121
client.modulesManager.register(RosterModule(rosterManager: roster)) client.modulesManager.register(RosterModule(rosterManager: roster))
client.modulesManager.register(PresenceModule())
client.modulesManager.register(PubSubModule()) client.modulesManager.register(PubSubModule())
client.modulesManager.register(PEPUserAvatarModule())
client.modulesManager.register(PEPBookmarksModule())
client.modulesManager.register(MessageModule(chatManager: chat)) client.modulesManager.register(MessageModule(chatManager: chat))
client.modulesManager.register(MessageArchiveManagementModule()) client.modulesManager.register(MessageArchiveManagementModule())
client.modulesManager.register(MessageCarbonsModule()) client.modulesManager.register(MessageCarbonsModule())
// file transfer modules
client.modulesManager.register(HttpFileUploadModule()) client.modulesManager.register(HttpFileUploadModule())
// extensions client.modulesManager.register(PresenceModule())
client.modulesManager.register(SoftwareVersionModule()) client.modulesManager.register(SoftwareVersionModule())
client.modulesManager.register(PingModule()) client.modulesManager.register(PingModule())
client.connectionConfiguration.userJid = .init(credentials.bareJid) client.connectionConfiguration.userJid = .init(credentials.bareJid)

View file

@ -1,49 +0,0 @@
import Foundation
import GRDB
struct Settings: DBStorable {
static let databaseTableName = "settings"
let bareJid: String
var omemoRegId: Int
var id: String {
bareJid
}
}
extension Settings {
static func getFor(_ bareJid: String) -> Settings? {
do {
return try Database.shared.dbQueue.read { db in
let settings = try Settings.filter(Column("bareJid") == bareJid).fetchOne(db)
return settings
}
} catch {
logIt(.error, "Settings not exists for \(bareJid)")
return nil
}
}
func wipeOmemoRegId() {
do {
_ = try Database.shared.dbQueue.write { db in
try Settings
.filter(Column("bareJid") == bareJid)
.updateAll(db, Column("omemoRegId").set(to: 0))
}
} catch {
logIt(.error, "Failed to wipe omemoRegId for \(bareJid)")
}
}
func save() {
do {
try Database.shared.dbQueue.write { db in
try self.insert(db)
}
} catch {
logIt(.error, "Failed to save settings for \(bareJid)")
}
}
}

View file

@ -99,13 +99,6 @@ extension Database {
} }
} }
migrator.registerMigration("Add settings table") { db in
try db.create(table: "settings", options: [.ifNotExists]) { table in
table.column("bareJid", .text).notNull().primaryKey().unique(onConflict: .replace)
table.column("omemoRegId", .integer).notNull()
}
}
// return migrator // return migrator
return migrator return migrator
}() }()

View file

@ -24,9 +24,9 @@ struct Storage<T> {
} }
// Storage // Storage
private let keyLocalizationSelected = "conversations.classic.user.defaults.localizationSelected" private let kOmemoDeviceId = "conversations.classic.user.defaults.omemoDeviceId"
enum UserSettings { enum UserSettings {
@Storage(key: keyLocalizationSelected, defaultValue: false) @Storage(key: kOmemoDeviceId, defaultValue: 0)
static var localizationSelectedByUser: Bool static var omemoDeviceId: UInt32
} }