From 694903e8bd829da145418edad5f720f8807dd7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 17 May 2019 14:56:40 +0200 Subject: [PATCH] pantalaimon: Allow pan users to be dynamically updated in the ui thread. --- pantalaimon/daemon.py | 2 ++ pantalaimon/main.py | 5 ++++- pantalaimon/panctl.py | 45 ++++++++++++++++++++++++++++++++----------- pantalaimon/ui.py | 28 ++++++++++++++++++++------- 4 files changed, 61 insertions(+), 19 deletions(-) diff --git a/pantalaimon/daemon.py b/pantalaimon/daemon.py index 9dacfbd..59b6645 100755 --- a/pantalaimon/daemon.py +++ b/pantalaimon/daemon.py @@ -411,6 +411,8 @@ class ProxyDaemon: logger.info(f"Succesfully started new background sync client for " f"{user_id}") + await self.send_queue.put(UpdateUsersMessage()) + self.pan_clients[user_id] = pan_client keyring.set_password( diff --git a/pantalaimon/main.py b/pantalaimon/main.py index 5daa858..4fc567a 100644 --- a/pantalaimon/main.py +++ b/pantalaimon/main.py @@ -160,7 +160,10 @@ def main( servers.append((proxy, runner, site)) proxies.append(proxy) - glib_thread = GlibT(pan_queue.sync_q, ui_queue.sync_q, data_dir) + print(pan_conf.servers.keys()) + + glib_thread = GlibT(pan_queue.sync_q, ui_queue.sync_q, data_dir, + pan_conf.servers.values()) glib_fut = loop.run_in_executor( None, diff --git a/pantalaimon/panctl.py b/pantalaimon/panctl.py index 0e841e4..118aacc 100644 --- a/pantalaimon/panctl.py +++ b/pantalaimon/panctl.py @@ -39,7 +39,7 @@ class PanctlParser(): def __init__(self): self.parser = PanctlArgParse() subparsers = self.parser.add_subparsers(dest="subcommand") - subparsers.add_parser("list-users") + subparsers.add_parser("list-servers") list_devices = subparsers.add_parser("list-devices") list_devices.add_argument("pan_user", type=str) @@ -134,8 +134,9 @@ class PanCompleter(Completer): return compl_words def complete_pan_users(self, last_word): - users = self.ctl.ListUsers() - compl_words = self.filter_words([i[0] for i in users], last_word) + servers = self.ctl.ListServers() + users = [item[0] for sublist in servers.values() for item in sublist] + compl_words = self.filter_words(users, last_word) for compl_word in compl_words: yield Completion(compl_word, -len(last_word)) @@ -260,7 +261,7 @@ class PanCtl: devices = attr.ib(init=False) commands = [ - "list-users", + "list-servers", "list-devices", "export-keys", "import-keys", @@ -348,12 +349,34 @@ class PanCtl: f"user {pan_user} from {user_id} via " f"{device_id}:\n{short_string}") - def list_users(self): + def list_servers(self): """List the daemons users.""" - users = self.ctl.ListUsers() - print("pantalaimon users:") - for user, device in users: - print(" ", user, device) + servers = self.ctl.ListServers() + + print("pantalaimon servers:") + + for server, server_users in servers.items(): + server_c = get_color(server) + + server_list = [] + + print_formatted_text(HTML( + f" - Name: <{server_c}>{server}" + )) + + user_list = [] + + for user, device in server_users: + user_c = get_color(user) + device_c = get_color(device) + + user_list.append(f" - <{user_c}>{user} " + f"<{device_c}>{device}") + + if user_list: + print(f" - Pan users:") + user_string = "\n".join(user_list) + print_formatted_text(HTML(user_string)) def list_devices(self, args): devices = self.devices.ListUserDevices(args.pan_user, args.user_id) @@ -398,8 +421,8 @@ class PanCtl: command = args.subcommand - if command == "list-users": - self.list_users() + if command == "list-servers": + self.list_servers() elif command == "import-keys": self.own_message_ids.append( diff --git a/pantalaimon/ui.py b/pantalaimon/ui.py index 07c568d..27f39bd 100644 --- a/pantalaimon/ui.py +++ b/pantalaimon/ui.py @@ -37,8 +37,8 @@ class Control: """ - - + + @@ -66,16 +66,25 @@ class Control: Response = signal() - def __init__(self, queue, user_list, id_counter): - self.users = user_list + def __init__(self, queue, store, server_list, id_counter): + self.server_list = server_list + self.store = store self.queue = queue self.id_counter = id_counter + self.users = defaultdict(list) + self.update_users() + + def update_users(self): + for server in self.server_list: + self.users[server.name] = self.store.load_users( + server.homeserver.hostname + ) @property def message_id(self): return self.id_counter.message_id - def ListUsers(self): + def ListServers(self): """Return the list of pan users.""" return self.users @@ -230,7 +239,7 @@ class Devices: self.queue.put(message) return message.message_id - def UnVerify(self, pan_user, user_id, device_id): + def Unverify(self, pan_user, user_id, device_id): message = DeviceUnverifyMessage( self.message_id, pan_user, @@ -289,6 +298,7 @@ class GlibT: receive_queue = attr.ib() send_queue = attr.ib() data_dir = attr.ib() + server_list = attr.ib() loop = attr.ib(init=False) store = attr.ib(init=False) @@ -306,7 +316,8 @@ class GlibT: id_counter = IdCounter() - self.control_if = Control(self.send_queue, self.users, id_counter) + self.control_if = Control(self.send_queue, self.store, + self.server_list, id_counter) self.device_if = Devices(self.send_queue, self.store, id_counter) self.bus = SessionBus() @@ -323,6 +334,9 @@ class GlibT: if isinstance(message, UpdateDevicesMessage): self.device_if.update_devices() + if isinstance(message, UpdateUsersMessage): + self.control_if.update_users() + elif isinstance(message, InviteSasSignal): self.device_if.VerificationInvite( message.pan_user,