diff --git a/doc/manual/config.md b/doc/manual/config.md index f6ff609..61dfca5 100644 --- a/doc/manual/config.md +++ b/doc/manual/config.md @@ -20,6 +20,7 @@ See also: [Additional Methods](#additional-methods) #### `join_on_invite` Boolean: whether the bot accepts all invites automatically. +When `allowlist` or `blocklist` are set, the bot will reject invites from any user that is not allowed. #### `encryption_enabled` Boolean: whether to enable encryption. diff --git a/simplematrixbotlib/callbacks.py b/simplematrixbotlib/callbacks.py index 57bb49f..7d107d0 100644 --- a/simplematrixbotlib/callbacks.py +++ b/simplematrixbotlib/callbacks.py @@ -1,7 +1,7 @@ import nio.events.room_events import nio.events.to_device -from nio import InviteMemberEvent -from nio import MegolmEvent, KeyVerificationStart, KeyVerificationCancel, KeyVerificationKey, KeyVerificationMac, ToDeviceError, KeyVerificationEvent +from nio import InviteMemberEvent, MegolmEvent, KeyVerificationStart, KeyVerificationCancel, KeyVerificationKey, KeyVerificationMac, ToDeviceError, KeyVerificationEvent +from simplematrixbotlib.match import Match class Callbacks: @@ -59,8 +59,16 @@ async def invite_callback(self, room, event, tries=1): return try: - await self.async_client.join(room.room_id) - print(f"Joined {room.room_id}") + match = Match(room, event, self.bot) + if match.is_from_allowed_user(): + await self.async_client.join(room.room_id) + print(f"Joined {room.room_id}") + else: + joined_rooms = await self.async_client.joined_rooms() + if room.room_id not in joined_rooms.rooms: + # prevents leaving if already in the room opposed to rejecting invites + await self.async_client.room_leave(room.room_id) + print(f"Rejected invite from user {event.sender} (user not allowed)") except Exception as e: print(f"Error joining {room.room_id}: {e}") tries += 1