From 4860ffee11debd213ac26cd317f6b92e280ca358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 1 Oct 2018 17:11:09 +0200 Subject: [PATCH] server: Handle group session sharing responses. --- matrix/buffer.py | 10 ++++++++-- matrix/server.py | 36 ++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/matrix/buffer.py b/matrix/buffer.py index aeb070b..cdd5623 100644 --- a/matrix/buffer.py +++ b/matrix/buffer.py @@ -39,7 +39,8 @@ from nio import ( RoomNameEvent, RoomTopicEvent, MegolmEvent, - Event + Event, + OlmTrustError ) from . import globals as G @@ -89,7 +90,12 @@ def room_buffer_input_cb(server_name, buffer, input_data): formatted_data = Formatted.from_input_line(data) - server.room_send_message(room_buffer, formatted_data, "m.text") + try: + server.room_send_message(room_buffer, formatted_data, "m.text") + except OlmTrustError as e: + m = ("Untrusted devices found in room: {}".format(e)) + server.error(m) + pass return W.WEECHAT_RC_OK diff --git a/matrix/server.py b/matrix/server.py index 1f4ce11..12f9b15 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -32,6 +32,7 @@ from nio import ( Rooms, RoomSendResponse, SyncResponse, + ShareGroupSessionResponse, TransportResponse, TransportType, RoomMessagesResponse, @@ -624,11 +625,11 @@ class MatrixServer(object): self.send_or_queue(request) def room_send_message(self, room_buffer, formatted, msgtype="m.text"): - # type: (RoomBuffer, Formatted, str) -> None + # type: (RoomBuffer, Formatted, str) -> bool room = room_buffer.room if not self.client: - return + return False body = {"msgtype": msgtype, "body": formatted.to_plain()} @@ -641,14 +642,11 @@ class MatrixServer(object): room.room_id, "m.room.message", body ) except EncryptionError: - try: - uuid, request = self.client.share_group_session(room.room_id) - message = EncrytpionQueueItem(msgtype, formatted) - self.encryption_queue[room.room_id].append(message) - except OlmTrustError as e: - m = ("Untrusted devices found in room: {}".format(e)) - self.error(m) - return + _, request = self.client.share_group_session(room.room_id) + message = EncrytpionQueueItem(msgtype, formatted) + self.encryption_queue[room.room_id].append(message) + self.send_or_queue(request) + return False if msgtype == "m.emote": message_class = OwnAction @@ -661,6 +659,7 @@ class MatrixServer(object): self.own_message_queue[uuid] = own_message self.send_or_queue(request) + return True def keys_upload(self): _, request = self.client.keys_upload() @@ -863,9 +862,26 @@ class MatrixServer(object): elif isinstance(response, RoomSendResponse): self.handle_own_messages(response) + elif isinstance(response, RoomMessagesResponse): self.handle_backlog_response(response) + elif isinstance(response, ShareGroupSessionResponse): + room_id = response.room_id + room_buffer = self.room_buffers[room_id] + + while self.encryption_queue[room_id]: + message = self.encryption_queue[room_id].popleft() + try: + if not self.room_send_message(room_buffer, + message.formatted_message, + message.message_type): + self.encryption_queue.pop() + self.encryption_queue[room_id].appendleft(message) + break + except OlmTrustError: + break + def create_room_buffer(self, room_id, prev_batch): room = self.client.rooms[room_id] buf = RoomBuffer(room, self.name, prev_batch)