wip
This commit is contained in:
parent
40ebe72e1b
commit
a60fae1a3a
|
@ -3,7 +3,10 @@ import GRDB
|
|||
import Martin
|
||||
import MartinOMEMO
|
||||
|
||||
final class ClientMartinOMEMO {
|
||||
!!! https: // xmpp.org/extensions/xep-0384.html#usecases-announcing
|
||||
|
||||
final class ClientMartinOMEMO
|
||||
{
|
||||
let credentials: Credentials
|
||||
|
||||
init(_ credentials: Credentials) {
|
||||
|
@ -27,20 +30,14 @@ final class ClientMartinOMEMO {
|
|||
OMEMOPreKey.wipe(account: credentials.bareJid)
|
||||
OMEMOSignedPreKey.wipe(account: credentials.bareJid)
|
||||
OMEMOIdentity.wipe(account: credentials.bareJid)
|
||||
Settings.getFor(credentials.bareJid)?.wipeOmemoRegId()
|
||||
UserSettings.omemoDeviceId = 0
|
||||
}
|
||||
|
||||
let hasKeyPair = keyPair() != nil
|
||||
if wipe || localRegistrationId() == 0 || !hasKeyPair {
|
||||
let regId = context.generateRegistrationId()
|
||||
if wipe || UserSettings.omemoDeviceId == 0 || !hasKeyPair {
|
||||
let regId: UInt32 = context.generateRegistrationId()
|
||||
let address = SignalAddress(name: credentials.bareJid, deviceId: Int32(regId))
|
||||
|
||||
if var settings = Settings.getFor(credentials.bareJid) {
|
||||
settings.omemoRegId = Int(regId)
|
||||
settings.save()
|
||||
} else {
|
||||
Settings(bareJid: credentials.bareJid, omemoRegId: Int(regId)).save()
|
||||
}
|
||||
UserSettings.omemoDeviceId = regId
|
||||
|
||||
guard let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context), let publicKey = keyPair.publicKey else {
|
||||
return false
|
||||
|
@ -51,6 +48,7 @@ final class ClientMartinOMEMO {
|
|||
|
||||
return save(address: address, fingerprint: fingerprint, own: true, data: keyPair.serialized())
|
||||
}
|
||||
print("omemoDeviceId \(UserSettings.omemoDeviceId)")
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -165,7 +163,7 @@ extension ClientMartinOMEMO: SignalSessionStoreProtocol {
|
|||
// MARK: - Identity
|
||||
extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
|
||||
func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? {
|
||||
let deviceId = localRegistrationId()
|
||||
let deviceId = UserSettings.omemoDeviceId
|
||||
guard deviceId != 0 else {
|
||||
return nil
|
||||
}
|
||||
|
@ -188,11 +186,7 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
|
|||
}
|
||||
|
||||
func localRegistrationId() -> UInt32 {
|
||||
if let settings = Settings.getFor(credentials.bareJid) {
|
||||
return UInt32(settings.omemoRegId)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
UserSettings.omemoDeviceId
|
||||
}
|
||||
|
||||
func save(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool {
|
||||
|
|
|
@ -3,6 +3,7 @@ import Foundation
|
|||
import GRDB
|
||||
import Martin
|
||||
import MartinOMEMO
|
||||
import UIKit
|
||||
|
||||
enum ClientState: Equatable {
|
||||
enum ClientConnectionState {
|
||||
|
@ -226,9 +227,9 @@ extension Client {
|
|||
private extension Client {
|
||||
static func prepareConnection(_ credentials: Credentials, _ roster: RosterManager, _ chat: ChatManager) -> XMPPClient {
|
||||
let client = XMPPClient()
|
||||
client.connectionConfiguration.resource = UIDevice.current.name
|
||||
|
||||
// register modules
|
||||
// core modules RFC 6120
|
||||
client.modulesManager.register(StreamFeaturesModule())
|
||||
client.modulesManager.register(SaslModule())
|
||||
client.modulesManager.register(AuthModule())
|
||||
|
@ -236,20 +237,19 @@ private extension Client {
|
|||
client.modulesManager.register(ResourceBinderModule())
|
||||
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(PresenceModule())
|
||||
|
||||
client.modulesManager.register(PubSubModule())
|
||||
client.modulesManager.register(PEPUserAvatarModule())
|
||||
client.modulesManager.register(PEPBookmarksModule())
|
||||
|
||||
client.modulesManager.register(MessageModule(chatManager: chat))
|
||||
client.modulesManager.register(MessageArchiveManagementModule())
|
||||
|
||||
client.modulesManager.register(MessageCarbonsModule())
|
||||
|
||||
// file transfer modules
|
||||
client.modulesManager.register(HttpFileUploadModule())
|
||||
|
||||
// extensions
|
||||
client.modulesManager.register(PresenceModule())
|
||||
client.modulesManager.register(SoftwareVersionModule())
|
||||
client.modulesManager.register(PingModule())
|
||||
client.connectionConfiguration.userJid = .init(credentials.bareJid)
|
||||
|
|
|
@ -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)")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}()
|
||||
|
|
|
@ -24,9 +24,9 @@ struct Storage<T> {
|
|||
}
|
||||
|
||||
// Storage
|
||||
private let keyLocalizationSelected = "conversations.classic.user.defaults.localizationSelected"
|
||||
private let kOmemoDeviceId = "conversations.classic.user.defaults.omemoDeviceId"
|
||||
|
||||
enum UserSettings {
|
||||
@Storage(key: keyLocalizationSelected, defaultValue: false)
|
||||
static var localizationSelectedByUser: Bool
|
||||
@Storage(key: kOmemoDeviceId, defaultValue: 0)
|
||||
static var omemoDeviceId: UInt32
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue