2024-08-27 13:00:39 +00:00
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
import Martin
|
|
|
|
import MartinOMEMO
|
|
|
|
|
|
|
|
final class ClientMartinOMEMO {
|
|
|
|
let credentials: Credentials
|
|
|
|
|
|
|
|
init(_ credentials: Credentials) {
|
|
|
|
self.credentials = credentials
|
|
|
|
}
|
|
|
|
|
|
|
|
var signal: (SignalStorage, SignalContext) {
|
|
|
|
let signalStorage = SignalStorage(sessionStore: self, preKeyStore: self, signedPreKeyStore: self, identityKeyStore: self, senderKeyStore: self)
|
|
|
|
// swiftlint:disable:next force_unwrapping
|
|
|
|
let signalContext = SignalContext(withStorage: signalStorage)!
|
|
|
|
signalStorage.setup(withContext: signalContext)
|
|
|
|
return (signalStorage, signalContext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-27 14:57:30 +00:00
|
|
|
// MARK: - Session
|
|
|
|
extension ClientMartinOMEMO: SignalSessionStoreProtocol {
|
2024-08-27 13:00:39 +00:00
|
|
|
func sessionRecord(forAddress address: MartinOMEMO.SignalAddress) -> Data? {
|
2024-08-27 14:57:30 +00:00
|
|
|
do {
|
|
|
|
let data = try Database.shared.dbQueue.read { db in
|
|
|
|
try Row.fetchOne(
|
|
|
|
db,
|
|
|
|
sql: "SELECT key FROM omemo_sessions WHERE account = :account AND name = :name AND device_id = :deviceId",
|
|
|
|
arguments: ["account": credentials.bareJid, "name": address.name, "deviceId": address.deviceId]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return data?["key"]
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-27 13:00:39 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 14:57:30 +00:00
|
|
|
func allDevices(for name: String, activeAndTrusted: Bool) -> [Int32] {
|
|
|
|
do {
|
|
|
|
let sql = activeAndTrusted ?
|
|
|
|
"""
|
|
|
|
SELECT s.device_id
|
|
|
|
FROM omemo_sessions s
|
|
|
|
LEFT JOIN omemo_identities i
|
|
|
|
ON s.account = i.account
|
|
|
|
AND s.name = i.name
|
|
|
|
AND s.device_id = i.device_id
|
|
|
|
WHERE s.account = :account
|
|
|
|
AND s.name = :name
|
|
|
|
AND ((i.status >= 0 AND i.status % 2 = 0) OR i.status IS NULL)
|
|
|
|
"""
|
|
|
|
:
|
|
|
|
"SELECT device_id FROM omemo_sessions WHERE account = :account AND name = :name"
|
|
|
|
let data = try Database.shared.dbQueue.read { db in
|
|
|
|
try Row.fetchAll(
|
|
|
|
db,
|
|
|
|
sql: sql,
|
|
|
|
arguments: ["account": credentials.bareJid, "name": name]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return data.map { $0["device_id"] }
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
|
|
|
|
return []
|
|
|
|
}
|
2024-08-27 13:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func storeSessionRecord(_ data: Data, forAddress: MartinOMEMO.SignalAddress) -> Bool {
|
2024-08-27 14:57:30 +00:00
|
|
|
do {
|
|
|
|
try Database.shared.dbQueue.write { db in
|
|
|
|
try db.execute(
|
|
|
|
sql: "INSERT INTO omemo_sessions (account, name, device_id, key) VALUES (:account, :name, :deviceId, :key)",
|
|
|
|
arguments: ["account": credentials.bareJid, "name": forAddress.name, "deviceId": forAddress.deviceId, "key": data]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
|
|
|
|
return false
|
|
|
|
}
|
2024-08-27 13:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func containsSessionRecord(forAddress: MartinOMEMO.SignalAddress) -> Bool {
|
2024-08-27 14:57:30 +00:00
|
|
|
do {
|
|
|
|
let rec = try Database.shared.dbQueue.read { db in
|
|
|
|
try Row.fetchOne(
|
|
|
|
db,
|
|
|
|
sql: "SELECT key FROM omemo_sessions WHERE account = :account AND name = :name AND device_id = :deviceId",
|
|
|
|
arguments: ["account": credentials.bareJid, "name": forAddress.name, "deviceId": forAddress.deviceId]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return rec != nil
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
|
|
|
|
return false
|
|
|
|
}
|
2024-08-27 13:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteSessionRecord(forAddress: MartinOMEMO.SignalAddress) -> Bool {
|
2024-08-27 14:57:30 +00:00
|
|
|
do {
|
|
|
|
try Database.shared.dbQueue.write { db in
|
|
|
|
try db.execute(
|
|
|
|
sql: "DELETE FROM omemo_sessions WHERE account = :account AND name = :name AND device_id = :deviceId",
|
|
|
|
arguments: ["account": credentials.bareJid, "name": forAddress.name, "deviceId": forAddress.deviceId]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
|
|
|
|
return false
|
|
|
|
}
|
2024-08-27 13:00:39 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 14:57:30 +00:00
|
|
|
func deleteAllSessions(for name: String) -> Bool {
|
|
|
|
do {
|
|
|
|
try Database.shared.dbQueue.write { db in
|
|
|
|
try db.execute(
|
|
|
|
sql: "DELETE FROM omemo_sessions WHERE account = :account AND name = :name",
|
|
|
|
arguments: ["account": credentials.bareJid, "name": name]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
|
|
|
|
return false
|
|
|
|
}
|
2024-08-27 13:00:39 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 14:57:30 +00:00
|
|
|
func sessionsWipe() {
|
|
|
|
do {
|
|
|
|
try Database.shared.dbQueue.write { db in
|
|
|
|
try db.execute(
|
|
|
|
sql: "DELETE FROM omemo_sessions WHERE account = :account",
|
|
|
|
arguments: ["account": credentials.bareJid]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - PreKey
|
|
|
|
extension ClientMartinOMEMO: SignalPreKeyStoreProtocol {
|
2024-08-27 13:00:39 +00:00
|
|
|
func currentPreKeyId() -> UInt32 {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadPreKey(withId: UInt32) -> Data? {
|
|
|
|
print(withId)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func storePreKey(_ data: Data, withId: UInt32) -> Bool {
|
|
|
|
print(data, withId)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsPreKey(withId: UInt32) -> Bool {
|
|
|
|
print(withId)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func deletePreKey(withId: UInt32) -> Bool {
|
|
|
|
print(withId)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func flushDeletedPreKeys() -> Bool {
|
|
|
|
false
|
|
|
|
}
|
2024-08-27 14:57:30 +00:00
|
|
|
}
|
2024-08-27 13:00:39 +00:00
|
|
|
|
2024-08-27 14:57:30 +00:00
|
|
|
// MARK: - SignedPreKey
|
|
|
|
extension ClientMartinOMEMO: SignalSignedPreKeyStoreProtocol {
|
2024-08-27 13:00:39 +00:00
|
|
|
func countSignedPreKeys() -> Int {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadSignedPreKey(withId: UInt32) -> Data? {
|
|
|
|
print(withId)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func storeSignedPreKey(_ data: Data, withId: UInt32) -> Bool {
|
|
|
|
print(data, withId)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsSignedPreKey(withId: UInt32) -> Bool {
|
|
|
|
print(withId)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteSignedPreKey(withId: UInt32) -> Bool {
|
|
|
|
print(withId)
|
|
|
|
return false
|
|
|
|
}
|
2024-08-27 14:57:30 +00:00
|
|
|
}
|
2024-08-27 13:00:39 +00:00
|
|
|
|
2024-08-27 14:57:30 +00:00
|
|
|
// MARK: - Identity
|
|
|
|
extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
|
2024-08-27 13:00:39 +00:00
|
|
|
func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? {
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func localRegistrationId() -> UInt32 {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
func save(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool {
|
|
|
|
print(identity, key)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isTrusted(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool {
|
|
|
|
print(identity, key)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func save(identity: MartinOMEMO.SignalAddress, publicKeyData: Data?) -> Bool {
|
|
|
|
print(identity, publicKeyData)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isTrusted(identity: MartinOMEMO.SignalAddress, publicKeyData: Data?) -> Bool {
|
|
|
|
print(identity, publicKeyData)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func setStatus(_ status: MartinOMEMO.IdentityStatus, forIdentity: MartinOMEMO.SignalAddress) -> Bool {
|
|
|
|
print(status, forIdentity)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func setStatus(active: Bool, forIdentity: MartinOMEMO.SignalAddress) -> Bool {
|
|
|
|
print(active, forIdentity)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func identities(forName: String) -> [MartinOMEMO.Identity] {
|
|
|
|
print(forName)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
func identityFingerprint(forAddress address: MartinOMEMO.SignalAddress) -> String? {
|
|
|
|
print(address)
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-27 14:57:30 +00:00
|
|
|
}
|
2024-08-27 13:00:39 +00:00
|
|
|
|
2024-08-27 14:57:30 +00:00
|
|
|
// MARK: - SenderKey
|
|
|
|
extension ClientMartinOMEMO: SignalSenderKeyStoreProtocol {
|
2024-08-27 13:00:39 +00:00
|
|
|
func storeSenderKey(_ key: Data, address: MartinOMEMO.SignalAddress?, groupId: String?) -> Bool {
|
|
|
|
print(key, address, groupId)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadSenderKey(forAddress: MartinOMEMO.SignalAddress?, groupId: String?) -> Data? {
|
|
|
|
print(forAddress, groupId)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|