From 186560dd4ffdbf15d32a1e7ed996a0de96378826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 22 May 2019 11:51:49 +0200 Subject: [PATCH] daemon: Connect the queue for unverified devices messages while sending. --- pantalaimon/daemon.py | 62 +++++++++++++++++++++++++++------- pantalaimon/panctl.py | 2 +- pantalaimon/thread_messages.py | 17 ++++++++++ pantalaimon/ui.py | 22 ++++++++++-- 4 files changed, 86 insertions(+), 17 deletions(-) diff --git a/pantalaimon/daemon.py b/pantalaimon/daemon.py index 73a1d67..63bb1fb 100755 --- a/pantalaimon/daemon.py +++ b/pantalaimon/daemon.py @@ -31,14 +31,17 @@ from pantalaimon.client import PanClient from pantalaimon.log import logger from pantalaimon.store import ClientInfo, PanStore from pantalaimon.thread_messages import (AcceptSasMessage, CancelSasMessage, + CancelSendingMessage, ConfirmSasMessage, DaemonResponse, DeviceBlacklistMessage, DeviceUnblacklistMessage, DeviceUnverifyMessage, DeviceVerifyMessage, ExportKeysMessage, ImportKeysMessage, - SasMessage, StartSasMessage, + SasMessage, SendAnywaysMessage, + StartSasMessage, UnverifiedDevicesSignal, + UnverifiedResponse, UpdateDevicesMessage, UpdateUsersMessage) @@ -55,7 +58,7 @@ class ProxyDaemon: ssl = attr.ib(default=None) decryption_timeout = 10 - unverified_send_timeout = 10 + unverified_send_timeout = 60 store = attr.ib(type=PanStore, init=False) homeserver_url = attr.ib(init=False, default=attr.Factory(dict)) @@ -314,6 +317,23 @@ class ProxyDaemon: info_msg ) + elif isinstance(message, UnverifiedResponse): + client = self.pan_clients[message.pan_user] + + if message.room_id not in client.send_decision_queues: + msg = (f"No send request found for user {message.pan_user} " + f"and room {message.room_id}.") + await self.send_response( + message.message_id, + message.pan_user, + "m.unknown_request", + msg + ) + return + + queue = client.send_decision_queues[message.room_id] + await queue.put(message) + def get_access_token(self, request): # type: (aiohttp.web.BaseRequest) -> str """Extract the access token from the request. @@ -777,15 +797,15 @@ class ProxyDaemon: # There are unverified/unblocked devices in the room, notify # the UI thread about this and wait for a response. queue = asyncio.Queue() + client.send_decision_queues[room_id] = queue message = UnverifiedDevicesSignal( client.user_id, room_id, room.display_name ) - await self.send_queue.put(message) - # TODO allow dbus clients to answer us here. + await self.send_queue.put(message) try: response = await asyncio.wait_for( @@ -793,12 +813,34 @@ class ProxyDaemon: self.unverified_send_timeout ) - if response == "cancel": + if isinstance(response, CancelSendingMessage): # The send was canceled notify the client that sent the # request about this. + info_msg = (f"Canceled message sending for room " + f"{room.display_name} ({room_id}).") + logger.info(info_msg) + await self.send_response( + response.message_id, + client.user_id, + "m.ok", + info_msg + ) + return web.Response(status=503, text=str(e)) - elif response == "send-anyways": + + elif isinstance(response, SendAnywaysMessage): # We are sending and ignoring devices along the way. + info_msg = (f"Ignoring unverified devices and sending " + f"message to room " + f"{room.display_name} ({room_id}).") + logger.info(info_msg) + await self.send_response( + response.message_id, + client.user_id, + "m.ok", + info_msg + ) + ret = await _send(True) await self.send_update_devcies() return ret @@ -807,11 +849,6 @@ class ProxyDaemon: # We didn't get a response to our signal, send out an error # response. - ret = await _send(True) - await self.send_update_devcies() - - return ret - return web.Response( status=503, text=(f"Room contains unverified devices and no " @@ -821,8 +858,7 @@ class ProxyDaemon: ) finally: - # Clear up the queue - pass + client.send_decision_queues.pop(room_id) async def filter(self, request): access_token = self.get_access_token(request) diff --git a/pantalaimon/panctl.py b/pantalaimon/panctl.py index 1adab47..29701e2 100644 --- a/pantalaimon/panctl.py +++ b/pantalaimon/panctl.py @@ -356,7 +356,7 @@ class PanCtl: self.completer.rooms[pan_user].add(room_id) print(f"Error sending message for user {pan_user}, " f"there are unverified devices in the room {display_name} " - f"({room_id}). Use the send-anyways or cancel-sending commands " + f"({room_id}).\nUse the send-anyways or cancel-sending commands " f"to ignore the devices or cancel the sending.") def show_response(self, response_id, pan_user, message): diff --git a/pantalaimon/thread_messages.py b/pantalaimon/thread_messages.py index dd83002..2df72b3 100644 --- a/pantalaimon/thread_messages.py +++ b/pantalaimon/thread_messages.py @@ -27,6 +27,23 @@ class UnverifiedDevicesSignal(Message): room_display_name = attr.ib() +@attr.s +class UnverifiedResponse(Message): + message_id = attr.ib() + pan_user = attr.ib() + room_id = attr.ib() + + +@attr.s +class SendAnywaysMessage(UnverifiedResponse): + pass + + +@attr.s +class CancelSendingMessage(UnverifiedResponse): + pass + + @attr.s class DaemonResponse(Message): message_id = attr.ib() diff --git a/pantalaimon/ui.py b/pantalaimon/ui.py index 54c5e56..a5b43ad 100644 --- a/pantalaimon/ui.py +++ b/pantalaimon/ui.py @@ -23,6 +23,7 @@ from pydbus.generic import signal from pantalaimon.log import logger from pantalaimon.store import PanStore from pantalaimon.thread_messages import (AcceptSasMessage, CancelSasMessage, + CancelSendingMessage, ConfirmSasMessage, DaemonResponse, DeviceBlacklistMessage, DeviceUnblacklistMessage, @@ -30,7 +31,8 @@ from pantalaimon.thread_messages import (AcceptSasMessage, CancelSasMessage, DeviceVerifyMessage, ExportKeysMessage, ImportKeysMessage, InviteSasSignal, SasDoneSignal, - ShowSasSignal, StartSasMessage, + SendAnywaysMessage, ShowSasSignal, + StartSasMessage, UnverifiedDevicesSignal, UpdateDevicesMessage, UpdateUsersMessage) @@ -73,11 +75,13 @@ class Control: + + @@ -140,10 +144,22 @@ class Control: return message.message_id def SendAnyways(self, pan_user, room_id): - pass + message = SendAnywaysMessage( + self.message_id, + pan_user, + room_id + ) + self.queue.put(message) + return message.message_id def CancelSending(self, pan_user, room_id): - pass + message = CancelSendingMessage( + self.message_id, + pan_user, + room_id + ) + self.queue.put(message) + return message.message_id class Devices: