From 451de8d5d6b552a6f92421e7f42cd21ddb54a4c1 Mon Sep 17 00:00:00 2001 From: Scott Wallace Date: Tue, 1 Jun 2021 17:08:43 +0100 Subject: [PATCH] Sort the output list correctly --- .gitignore | 2 ++ maubot.yaml | 6 ++++-- requirements.txt | 3 +++ shameotron.py | 50 ++++++++++++++++++------------------------------ 4 files changed, 28 insertions(+), 33 deletions(-) create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 5f561f5..0750c91 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.mbp *.zip +.pyenv/ +.vscode/ diff --git a/maubot.yaml b/maubot.yaml index b704017..5c7e775 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -1,8 +1,10 @@ maubot: 0.1.0 id: sh.wallace.matrix.shameotron -version: 0.0.14 +version: 0.0.16 license: MIT modules: -- shameotron + - shameotron main_class: ShameOTron database: false +extra_files: + - base-config.yaml diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fe77990 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +maubot +mautrix +requests diff --git a/shameotron.py b/shameotron.py index 9421493..0a57bfd 100644 --- a/shameotron.py +++ b/shameotron.py @@ -12,8 +12,14 @@ from typing import Dict, List, Type import requests from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper -from mautrix.types import TextMessageEventContent, MessageType, Format, \ - EventID, RoomID, UserID +from mautrix.types import ( + TextMessageEventContent, + MessageType, + Format, + EventID, + RoomID, + UserID, +) from mautrix.util import markdown from maubot import Plugin, MessageEvent @@ -24,6 +30,7 @@ class Config(BaseProxyConfig): """ Config class """ + def do_update(self, helper: ConfigUpdateHelper) -> None: """ Class method to update the config @@ -36,13 +43,13 @@ class ShameOTron(Plugin): """ Main class for the Shame-o-Tron """ + async def start(self) -> None: """ Class method for plugin startup """ self.on_external_config_update() - @classmethod def get_config_class(cls) -> Type[Config]: """ @@ -50,7 +57,6 @@ class ShameOTron(Plugin): """ return Config - async def _edit(self, room_id: RoomID, event_id: EventID, text: str) -> None: """ Class method to update an existing message event @@ -59,12 +65,11 @@ class ShameOTron(Plugin): msgtype=MessageType.NOTICE, body=text, format=Format.HTML, - formatted_body=markdown.render(text) + formatted_body=markdown.render(text), ) content.set_edit(event_id) await self.client.send_message(room_id, content) - async def _load_members(self, room_id: RoomID) -> Dict[str, List[UserID]]: """ Class method to return the servers and room members @@ -76,7 +81,6 @@ class ShameOTron(Plugin): servers.setdefault(server, []).append(user) return servers - async def get_ssl_expiry(self, addr, host): """ Class method to return the expiry date of a specific instance @@ -99,7 +103,6 @@ class ShameOTron(Plugin): # parse the string from the certificate into a Python datetime object return datetime.strptime(ssl_info['notAfter'], ssl_date_fmt) - async def query_homeserver(self, host): """ Class method to query the Federation Tester to retrieve the running @@ -112,8 +115,7 @@ class ShameOTron(Plugin): version = None try: req = requests.get( - self.config["federation_tester"].format(server=host), - timeout=10000 + self.config["federation_tester"].format(server=host), timeout=10000 ) except requests.exceptions.Timeout: version = '[TIMEOUT]' @@ -126,11 +128,7 @@ class ShameOTron(Plugin): try: addr = list(data['ConnectionReports'].keys())[0] ssl_expiry = await self.get_ssl_expiry(addr, host) - except ( - ssl.SSLCertVerificationError, - ssl.SSLError, - IndexError - ) as error: + except (ssl.SSLCertVerificationError, ssl.SSLError, IndexError) as error: self.log.warning('SSL error for: %s: %s', host, error) ssl_expiry = None @@ -141,11 +139,7 @@ class ShameOTron(Plugin): self.log.error(error) version = '[ERROR]' - return { - 'version': version, - 'ssl_expiry': ssl_expiry - } - + return {'version': version, 'ssl_expiry': ssl_expiry} @command.new('shame', help='Show versions of all homeservers in the room') @command.argument("candidate", pass_raw=True, required=False) @@ -163,20 +157,16 @@ class ShameOTron(Plugin): dead_servers = self.config['dead_servers'] if dead_servers: # Return a unique list - member_servers = sorted( - list( - set(member_servers.keys() - set(dead_servers)) - ) - ) + member_servers = list(set(member_servers.keys() - set(dead_servers))) await self._edit( evt.room_id, event_id, - 'Member list loaded, fetching versions... please wait...' + 'Member list loaded, fetching versions... please wait...', ) versions = [] - for host in member_servers: + for host in sorted(member_servers): data = await self.query_homeserver(host) warning = '' @@ -188,9 +178,7 @@ class ShameOTron(Plugin): else: warning = '(SSL error)' - versions.append( - (host, f"{data['version']} {warning}") - ) + versions.append((host, f"{data['version']} {warning}")) await self._edit( evt.room_id, @@ -201,5 +189,5 @@ class ShameOTron(Plugin): f'* {host}: [{version}]({self.config["federation_tester"].format(server=host)})' for host, version in versions ) - ) + ), )