use whitelist for invites instead of rooms

This commit is contained in:
F-Node-Karlsruhe 2022-11-23 12:08:04 +01:00
parent b5c9c3874f
commit a19e124cdf
2 changed files with 19 additions and 7 deletions

View File

@ -752,6 +752,8 @@ Typically set your own id into it.
`OWNERS_ONLY` is an optional variable once defined only the owners can operate the bot (this is a form of whitelisting) `OWNERS_ONLY` is an optional variable once defined only the owners can operate the bot (this is a form of whitelisting)
`INVITE_WHITELIST` (default empty) is an optional comma-separated list of matrix id's to restrict the acceptance of invites into rooms of the bot to users or servers. It supports wild cards: Example value: `@user:matrix.org,@*:myserver.org`
`LEAVE_EMPTY_ROOMS` (default true) if this is set to false, the bot will stay in empty rooms `LEAVE_EMPTY_ROOMS` (default true) if this is set to false, the bot will stay in empty rooms
__*ATTENTION:*__ Don't include bot itself in `BOT_OWNERS` if cron or any other module that can cause bot to send custom commands is used, as it could potentially be used to run owner commands as the bot itself. __*ATTENTION:*__ Don't include bot itself in `BOT_OWNERS` if cron or any other module that can cause bot to send custom commands is used, as it could potentially be used to run owner commands as the bot itself.

24
bot.py
View File

@ -35,7 +35,7 @@ class Bot:
self.version = '1.5' self.version = '1.5'
self.client = None self.client = None
self.join_on_invite = False self.join_on_invite = False
self.homeserver_only = True self.invite_whitelist = []
self.modules = dict() self.modules = dict()
self.module_aliases = dict() self.module_aliases = dict()
self.leave_empty_rooms = True self.leave_empty_rooms = True
@ -459,13 +459,23 @@ class Bot:
def starts_with_command(body): def starts_with_command(body):
"""Checks if body starts with ! and has one or more letters after it""" """Checks if body starts with ! and has one or more letters after it"""
return re.match(r"^!\w.*", body) is not None return re.match(r"^!\w.*", body) is not None
def on_invite_whitelist(self, sender):
for entry in self.invite_whitelist:
if entry == sender:
return True
controll_value = entry.split(':')
if controll_value[0] == '@*' and controll_value[1] == sender.split(':')[1]:
return True
return False
async def invite_cb(self, room, event): async def invite_cb(self, room, event):
room: MatrixRoom room: MatrixRoom
event: InviteEvent event: InviteEvent
if self.homeserver_only and room.room_id.split(':')[1] != self.matrix_user.split(':')[1]: if len(self.invite_whitelist) > 0 and self.on_invite_whitelist(event.sender):
self.logger.error(f'Cannot join room {room.display_name}, as it is not on the homeserver') self.logger.error(f'Cannot join room {room.display_name}, as {event.sender} is not whitelisted for invites!')
return return
if self.join_on_invite or self.is_owner(event): if self.join_on_invite or self.is_owner(event):
@ -563,7 +573,7 @@ class Bot:
bot_owners = os.getenv('BOT_OWNERS') bot_owners = os.getenv('BOT_OWNERS')
access_token = os.getenv('MATRIX_ACCESS_TOKEN') access_token = os.getenv('MATRIX_ACCESS_TOKEN')
join_on_invite = os.getenv('JOIN_ON_INVITE') join_on_invite = os.getenv('JOIN_ON_INVITE')
homeserver_only = os.getenv('HOMESERVER_ONLY') invite_whitelist = os.getenv('INVITE_WHITELIST')
owners_only = os.getenv('OWNERS_ONLY') is not None owners_only = os.getenv('OWNERS_ONLY') is not None
leave_empty_rooms = os.getenv('LEAVE_EMPTY_ROOMS') leave_empty_rooms = os.getenv('LEAVE_EMPTY_ROOMS')
@ -571,7 +581,7 @@ class Bot:
self.client = AsyncClient(matrix_server, self.matrix_user, ssl = matrix_server.startswith("https://")) self.client = AsyncClient(matrix_server, self.matrix_user, ssl = matrix_server.startswith("https://"))
self.client.access_token = access_token self.client.access_token = access_token
self.join_on_invite = (join_on_invite or '').lower() == 'true' self.join_on_invite = (join_on_invite or '').lower() == 'true'
self.homeserver_only = (homeserver_only or '').lower() == 'true' self.invite_whitelist = invite_whitelist.split(',') if invite_whitelist is not None else []
self.leave_empty_rooms = (leave_empty_rooms or 'true').lower() == 'true' self.leave_empty_rooms = (leave_empty_rooms or 'true').lower() == 'true'
self.owners = bot_owners.split(',') self.owners = bot_owners.split(',')
self.owners_only = owners_only self.owners_only = owners_only
@ -623,8 +633,8 @@ class Bot:
if self.join_on_invite: if self.join_on_invite:
self.logger.info('Note: Bot will join rooms if invited') self.logger.info('Note: Bot will join rooms if invited')
if self.homeserver_only: if len(self.invite_whitelist) > 0:
self.logger.info('Note: Bot will only join rooms located on its homeserver') self.logger.info(f'Note: Bot will only join rooms when the inviting user is contained in {self.invite_whitelist}')
self.logger.info('Bot running as %s, owners %s', self.client.user, self.owners) self.logger.info('Bot running as %s, owners %s', self.client.user, self.owners)
self.bot_task = asyncio.create_task(self.client.sync_forever(timeout=30000)) self.bot_task = asyncio.create_task(self.client.sync_forever(timeout=30000))
await self.bot_task await self.bot_task