Sort the output list correctly

This commit is contained in:
Scott Wallace 2021-06-01 17:08:43 +01:00
parent 1a0f36c233
commit 451de8d5d6
Signed by: scott
GPG key ID: AA742FDC5AFE2A72
4 changed files with 28 additions and 33 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
*.mbp *.mbp
*.zip *.zip
.pyenv/
.vscode/

View file

@ -1,8 +1,10 @@
maubot: 0.1.0 maubot: 0.1.0
id: sh.wallace.matrix.shameotron id: sh.wallace.matrix.shameotron
version: 0.0.14 version: 0.0.16
license: MIT license: MIT
modules: modules:
- shameotron - shameotron
main_class: ShameOTron main_class: ShameOTron
database: false database: false
extra_files:
- base-config.yaml

3
requirements.txt Normal file
View file

@ -0,0 +1,3 @@
maubot
mautrix
requests

View file

@ -12,8 +12,14 @@ from typing import Dict, List, Type
import requests import requests
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
from mautrix.types import TextMessageEventContent, MessageType, Format, \ from mautrix.types import (
EventID, RoomID, UserID TextMessageEventContent,
MessageType,
Format,
EventID,
RoomID,
UserID,
)
from mautrix.util import markdown from mautrix.util import markdown
from maubot import Plugin, MessageEvent from maubot import Plugin, MessageEvent
@ -24,6 +30,7 @@ class Config(BaseProxyConfig):
""" """
Config class Config class
""" """
def do_update(self, helper: ConfigUpdateHelper) -> None: def do_update(self, helper: ConfigUpdateHelper) -> None:
""" """
Class method to update the config Class method to update the config
@ -36,13 +43,13 @@ class ShameOTron(Plugin):
""" """
Main class for the Shame-o-Tron Main class for the Shame-o-Tron
""" """
async def start(self) -> None: async def start(self) -> None:
""" """
Class method for plugin startup Class method for plugin startup
""" """
self.on_external_config_update() self.on_external_config_update()
@classmethod @classmethod
def get_config_class(cls) -> Type[Config]: def get_config_class(cls) -> Type[Config]:
""" """
@ -50,7 +57,6 @@ class ShameOTron(Plugin):
""" """
return Config return Config
async def _edit(self, room_id: RoomID, event_id: EventID, text: str) -> None: async def _edit(self, room_id: RoomID, event_id: EventID, text: str) -> None:
""" """
Class method to update an existing message event Class method to update an existing message event
@ -59,12 +65,11 @@ class ShameOTron(Plugin):
msgtype=MessageType.NOTICE, msgtype=MessageType.NOTICE,
body=text, body=text,
format=Format.HTML, format=Format.HTML,
formatted_body=markdown.render(text) formatted_body=markdown.render(text),
) )
content.set_edit(event_id) content.set_edit(event_id)
await self.client.send_message(room_id, content) await self.client.send_message(room_id, content)
async def _load_members(self, room_id: RoomID) -> Dict[str, List[UserID]]: async def _load_members(self, room_id: RoomID) -> Dict[str, List[UserID]]:
""" """
Class method to return the servers and room members Class method to return the servers and room members
@ -76,7 +81,6 @@ class ShameOTron(Plugin):
servers.setdefault(server, []).append(user) servers.setdefault(server, []).append(user)
return servers return servers
async def get_ssl_expiry(self, addr, host): async def get_ssl_expiry(self, addr, host):
""" """
Class method to return the expiry date of a specific instance 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 # parse the string from the certificate into a Python datetime object
return datetime.strptime(ssl_info['notAfter'], ssl_date_fmt) return datetime.strptime(ssl_info['notAfter'], ssl_date_fmt)
async def query_homeserver(self, host): async def query_homeserver(self, host):
""" """
Class method to query the Federation Tester to retrieve the running Class method to query the Federation Tester to retrieve the running
@ -112,8 +115,7 @@ class ShameOTron(Plugin):
version = None version = None
try: try:
req = requests.get( req = requests.get(
self.config["federation_tester"].format(server=host), self.config["federation_tester"].format(server=host), timeout=10000
timeout=10000
) )
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
version = '[TIMEOUT]' version = '[TIMEOUT]'
@ -126,11 +128,7 @@ class ShameOTron(Plugin):
try: try:
addr = list(data['ConnectionReports'].keys())[0] addr = list(data['ConnectionReports'].keys())[0]
ssl_expiry = await self.get_ssl_expiry(addr, host) ssl_expiry = await self.get_ssl_expiry(addr, host)
except ( except (ssl.SSLCertVerificationError, ssl.SSLError, IndexError) as error:
ssl.SSLCertVerificationError,
ssl.SSLError,
IndexError
) as error:
self.log.warning('SSL error for: %s: %s', host, error) self.log.warning('SSL error for: %s: %s', host, error)
ssl_expiry = None ssl_expiry = None
@ -141,11 +139,7 @@ class ShameOTron(Plugin):
self.log.error(error) self.log.error(error)
version = '[ERROR]' version = '[ERROR]'
return { return {'version': version, 'ssl_expiry': ssl_expiry}
'version': version,
'ssl_expiry': ssl_expiry
}
@command.new('shame', help='Show versions of all homeservers in the room') @command.new('shame', help='Show versions of all homeservers in the room')
@command.argument("candidate", pass_raw=True, required=False) @command.argument("candidate", pass_raw=True, required=False)
@ -163,20 +157,16 @@ class ShameOTron(Plugin):
dead_servers = self.config['dead_servers'] dead_servers = self.config['dead_servers']
if dead_servers: if dead_servers:
# Return a unique list # Return a unique list
member_servers = sorted( member_servers = list(set(member_servers.keys() - set(dead_servers)))
list(
set(member_servers.keys() - set(dead_servers))
)
)
await self._edit( await self._edit(
evt.room_id, evt.room_id,
event_id, event_id,
'Member list loaded, fetching versions... please wait...' 'Member list loaded, fetching versions... please wait...',
) )
versions = [] versions = []
for host in member_servers: for host in sorted(member_servers):
data = await self.query_homeserver(host) data = await self.query_homeserver(host)
warning = '' warning = ''
@ -188,9 +178,7 @@ class ShameOTron(Plugin):
else: else:
warning = '(SSL error)' warning = '(SSL error)'
versions.append( versions.append((host, f"{data['version']} {warning}"))
(host, f"{data['version']} {warning}")
)
await self._edit( await self._edit(
evt.room_id, evt.room_id,
@ -201,5 +189,5 @@ class ShameOTron(Plugin):
f'* {host}: [{version}]({self.config["federation_tester"].format(server=host)})' f'* {host}: [{version}]({self.config["federation_tester"].format(server=host)})'
for host, version in versions for host, version in versions
) )
) ),
) )