From dec12a898e2ba6b82b0f4926849d39e232944074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 13 Jul 2018 16:05:18 +0200 Subject: [PATCH] rooms: Return an error event instead of None upon decryption failure. --- matrix/buffer.py | 15 ++++++++++++++- matrix/rooms.py | 20 +++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/matrix/buffer.py b/matrix/buffer.py index 4b4270a..44245c0 100644 --- a/matrix/buffer.py +++ b/matrix/buffer.py @@ -46,7 +46,8 @@ from .rooms import ( RoomRedactionEvent, RoomRedactedMessageEvent, RoomEncryptionEvent, - RoomPowerLevels + RoomPowerLevels, + UndecryptedEvent ) @@ -953,6 +954,18 @@ class RoomBuffer(object): elif isinstance(event, RoomPowerLevels): self._handle_power_level(event) + elif isinstance(event, UndecryptedEvent): + nick = self.find_nick(event.sender) + date = server_ts_to_weechat(event.timestamp) + data = ("Error decrypting event session " + "id: {}".format(event.session_id)) + self.weechat_buffer.message( + nick, + data, + date, + self.get_event_tags(event) + ) + else: W.prnt("", "Unhandled event of type {}.".format( type(event).__name__)) diff --git a/matrix/rooms.py b/matrix/rooms.py index 7bc33e0..67fc643 100644 --- a/matrix/rooms.py +++ b/matrix/rooms.py @@ -262,7 +262,7 @@ class RoomInfo(): ) if not plaintext: - return None + return UndecryptedEvent.from_dict(event_dict) parsed_plaintext = json.loads(plaintext, encoding="utf-8") @@ -328,6 +328,24 @@ class RoomEvent(object): self.timestamp = timestamp +class UndecryptedEvent(RoomEvent): + def __init__(self, event_id, sender, timestamp, session_id): + self.session_id = session_id + RoomEvent.__init__(self, event_id, sender, timestamp) + + @classmethod + def from_dict(cls, event): + event_id = (sanitize_id(event["event_id"]) + if "event_id" in event else None) + sender = (sanitize_id(event["sender"]) + if "sender" in event else None) + timestamp = (sanitize_ts(event["origin_server_ts"]) + if "origin_server_ts" in event else None) + session_id = event["content"]["session_id"] + + return cls(event_id, sender, timestamp, session_id) + + class BadEvent(RoomEvent): def __init__(self, event_id, sender, timestamp, source): RoomEvent.__init__(self, event_id, sender, timestamp)