Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

feat: ✨ accept joins only from allowed users #155

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/manual/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 12 additions & 4 deletions simplematrixbotlib/callbacks.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An open question is whether actively rejecting, hence giving the inviter feedback and blocking further invites, is better than simply ignoring invites.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should ignore by default, but provide a config setting to allow rejection.

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
Expand Down