From 974d5072924dcd005b3cd5c7598e71391ea113de Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 18 Feb 2020 11:49:02 +0100 Subject: [PATCH] [Client] Minor olm fixes --- lib/src/client.dart | 12 ++++++++++-- lib/src/room.dart | 8 ++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index b6ef73f..08a4d01 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1552,7 +1552,13 @@ class Client { if (existingSessions != null) { for (olm.Session session in existingSessions) { if ((type == 0 && session.matches_inbound(body) == 1) || type == 1) { - plaintext = session.decrypt(type, body); + try { + plaintext = session.decrypt(type, body); + storeOlmSession(senderKey, session); + break; + } catch (_) { + plaintext = null; + } } } } @@ -1565,8 +1571,8 @@ class Client { newSession.create_inbound_from(_olmAccount, senderKey, body); _olmAccount.remove_one_time_keys(newSession); storeAPI?.storeClient(); - storeOlmSession(senderKey, newSession); plaintext = newSession.decrypt(type, body); + storeOlmSession(senderKey, newSession); } final Map plainContent = json.decode(plaintext); if (plainContent.containsKey("sender") && @@ -1597,6 +1603,8 @@ class Client { if (!_olmSessions.containsKey(curve25519IdentityKey)) { _olmSessions[curve25519IdentityKey] = []; } + _olmSessions[curve25519IdentityKey] + .removeWhere((olm.Session s) => s.session_id() == session.session_id()); _olmSessions[curve25519IdentityKey].add(session); Map> pickleMap = {}; for (var entry in olmSessions.entries) { diff --git a/lib/src/room.dart b/lib/src/room.dart index dfaafbf..2224b9c 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -505,7 +505,9 @@ class Room { Future sendEvent(Map content, {String txid, Event inReplyTo}) async { final String type = "m.room.message"; - final String sendType = this.encrypted ? "m.room.encrypted" : type; + final String sendType = (this.encrypted && client.encryptionEnabled) + ? "m.room.encrypted" + : type; // Create new transaction id String messageID; @@ -555,7 +557,9 @@ class Room { final Map response = await client.jsonRequest( type: HTTPType.PUT, action: "/client/r0/rooms/${id}/send/$sendType/$messageID", - data: await encryptGroupMessagePayload(content)); + data: client.encryptionEnabled + ? await encryptGroupMessagePayload(content) + : content); final String res = response["event_id"]; eventUpdate.content["status"] = 1; eventUpdate.content["unsigned"] = {"transaction_id": messageID};