From 97d3a59e33c459c73df17a63ce975f273bb37ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 19 Sep 2018 11:40:08 +0200 Subject: [PATCH] server: Enable Olm support. --- matrix/buffer.py | 33 +++++++++++++++++++++------------ matrix/server.py | 22 +++++++++++++++++----- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/matrix/buffer.py b/matrix/buffer.py index f100f84..980abee 100644 --- a/matrix/buffer.py +++ b/matrix/buffer.py @@ -37,6 +37,7 @@ from nio import ( RoomMessageUnknown, RoomNameEvent, RoomTopicEvent, + MegolmEvent ) from . import globals as G @@ -1098,17 +1099,25 @@ class RoomBuffer(object): elif isinstance(event, PowerLevelsEvent): self._handle_power_level(event) - # elif isinstance(event, UndecryptedEvent): - # nick = self.find_nick(event.sender) - # date = server_ts_to_weechat(event.server_timestamp) - # data = ("Error decrypting event session " - # "id: {}".format(event.session_id)) - # self.weechat_buffer.message( - # nick, - # data, - # date, - # self.get_event_tags(event) - # ) + elif isinstance(event, MegolmEvent): + nick = self.find_nick(event.sender) + date = server_ts_to_weechat(event.server_timestamp) + "{del_color}<{log_color}Message redacted by: " + "{censor}{log_color}{reason}{del_color}>" + "{ncolor}" + data = ("{del_color}<{log_color}Unable to decrypt: " + "The sender's device has not sent us " + "the keys for this message{del_color}>{ncolor}").format( + del_color=W.color("chat_delimiters"), + log_color=W.color("logger.color.backlog_line"), + ncolor=W.color("reset")) + session_id_tag = SCRIPT_NAME + "_sessionid_" + event.session_id + self.weechat_buffer.message( + nick, + data, + date, + self.get_event_tags(event) + [session_id_tag] + ) else: W.prnt( @@ -1249,7 +1258,7 @@ class RoomBuffer(object): break if leave_index: - timeline_events = info.timeline.events[leave_index + 1 :] + timeline_events = info.timeline.events[leave_index + 1:] # Handle our leave as a state event since we're not in the # nicklist anymore but we're already printed out our leave self.handle_state_event(info.timeline.events[leave_index]) diff --git a/matrix/server.py b/matrix/server.py index cc3f94d..b70914a 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -259,7 +259,12 @@ class MatrixServer(object): def _change_client(self): host = ":".join([self.config.address, str(self.config.port)]) - self.client = HttpClient(host, self.config.username, self.device_id) + self.client = HttpClient( + host, + self.config.username, + self.device_id, + self.get_session_path() + ) def update_option(self, option, option_name): if option_name == "address": @@ -633,6 +638,10 @@ class MatrixServer(object): self.own_message_queue[uuid] = own_message self.send_or_queue(request) + def keys_upload(self): + _, request = self.client.keys_upload() + self.send_or_queue(request) + def _print_message_error(self, message): server_buffer_prnt( self, @@ -683,10 +692,8 @@ class MatrixServer(object): W.prnt(self.server_buffer, message) - # if not self.olm: - # self.create_olm() - # self.store_olm() - # self.upload_keys(device_keys=True, one_time_keys=False) + if not self.client.olm_account_shared: + self.keys_upload() sync_filter = { "room": { @@ -743,7 +750,12 @@ class MatrixServer(object): return self._handle_room_info(response) + self.next_batch = response.next_batch + + if self.client.should_upload_keys: + self.keys_upload() + self.schedule_sync() def handle_transport_response(self, response):