actively track new ban events in banlist rooms

This commit is contained in:
William Kray
2024-10-10 09:07:58 -07:00
parent ea1e8462e2
commit 00a09e6d91
+44 -17
View File
@@ -14,6 +14,7 @@ from mautrix.errors import MNotFound
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
from maubot import Plugin, MessageEvent from maubot import Plugin, MessageEvent
from maubot.handlers import command, event from maubot.handlers import command, event
BAN_STATE_EVENT = EventType.find("m.policy.rule.user", EventType.Class.STATE)
# database table related things # database table related things
from .db import upgrade_table from .db import upgrade_table
@@ -150,21 +151,11 @@ class CommunityBot(Plugin):
# fetch banlist data # fetch banlist data
is_banned = False is_banned = False
myrooms = await self.client.get_joined_rooms() myrooms = await self.client.get_joined_rooms()
for l in self.config['banlists']: banlist_roomids = await self.get_banlist_roomids()
#self.log.debug(f"DEBUG getting banlist {l}")
if l.startswith('#'): for list_id in banlist_roomids:
try:
l_id = await self.client.resolve_room_alias(l)
list_id = l_id["room_id"]
#self.log.debug(f"DEBUG banlist id resolves to: {list_id}")
except:
evt.reply("i don't recognize that list, sorry")
return
else:
list_id = l
if list_id not in myrooms: if list_id not in myrooms:
self.log.error(f"Bot must be in {l} before attempting to use it as a banlist.") self.log.error(f"Bot must be in {list_id} before attempting to use it as a banlist.")
pass pass
#self.log.debug(f"DEBUG looking up state in {list_id}") #self.log.debug(f"DEBUG looking up state in {list_id}")
@@ -186,12 +177,12 @@ class CommunityBot(Plugin):
else: else:
pass pass
except Exception as e: except Exception as e:
self.log.error(f"Found something funny in the banlist {l} for {rule['content']}: {e}") self.log.debug(f"Found something funny in the banlist {l} for {rule['content']}: {e}")
pass pass
# if we haven't exited by now, we must not be banned! # if we haven't exited by now, we must not be banned!
return is_banned return is_banned
async def ban_this_user(self, user): async def ban_this_user(self, user, reason="banned"):
#self.log.debug(f"DEBUG getting list of rooms") #self.log.debug(f"DEBUG getting list of rooms")
roomlist = await self.get_space_roomlist() roomlist = await self.get_space_roomlist()
# don't forget to kick from the space itself # don't forget to kick from the space itself
@@ -209,7 +200,7 @@ class CommunityBot(Plugin):
# ban user even if they're not in the room! # ban user even if they're not in the room!
#await self.client.get_state_event(room, EventType.ROOM_MEMBER, user) #await self.client.get_state_event(room, EventType.ROOM_MEMBER, user)
await self.client.ban_user(room, user, reason='banned') await self.client.ban_user(room, user, reason=reason)
if roomname: if roomname:
ban_event_map['ban_list'][user].append(roomname) ban_event_map['ban_list'][user].append(roomname)
else: else:
@@ -224,6 +215,42 @@ class CommunityBot(Plugin):
return ban_event_map return ban_event_map
async def get_banlist_roomids(self):
banlist_roomids = []
for l in self.config['banlists']:
#self.log.debug(f"DEBUG getting banlist {l}")
if l.startswith('#'):
try:
l_id = await self.client.resolve_room_alias(l)
list_id = l_id["room_id"]
#self.log.debug(f"DEBUG banlist id resolves to: {list_id}")
except:
evt.reply("i don't recognize that list, sorry")
return
else:
list_id = l
banlist_roomids.append(list_id)
return banlist_roomids
@event.on(BAN_STATE_EVENT)
async def check_ban_event(self, evt:StateEvent) -> None:
banlist_roomids = await self.get_banlist_roomids()
# we only care about ban events in rooms in the banlist
if evt.room_id not in banlist_roomids:
return
else:
try:
entity = evt.content["entity"]
recommendation = evt.content["recommendation"]
self.log.debug(f"DEBUG new ban rule found: {entity} should have action {recommendation}")
if bool(re.search('ban$', recommendation)):
await self.ban_this_user(entity)
except Exception as e:
self.log.error(e)
@event.on(InternalEventType.JOIN) @event.on(InternalEventType.JOIN)
async def newjoin(self, evt:StateEvent) -> None: async def newjoin(self, evt:StateEvent) -> None: