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
*.zip
.pyenv/
.vscode/

View file

@ -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
main_class: ShameOTron
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
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
)
)
),
)