- added config to limit rooms where bot works.

This commit is contained in:
Dr Serge Victor 2023-10-03 11:34:48 +00:00
parent c2e7f9ec51
commit 562d85153e
3 changed files with 29 additions and 1 deletions

2
base-config.yaml Normal file
View file

@ -0,0 +1,2 @@
# If not empty, bot will serve only rooms on the list
rooms: []

View file

@ -3,12 +3,19 @@ from io import BytesIO
from typing import Tuple
from PIL import Image
from pillow_heif import HeifImagePlugin
from typing import Type
from maubot import Plugin, MessageEvent
from maubot.handlers import command
from mautrix.client import Client as MatrixClient
from mautrix.crypto import attachments
from mautrix.types import EncryptedFile, ImageInfo, MediaMessageEventContent, MessageType
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
class Config(BaseProxyConfig):
def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("rooms")
async def download_encrypted_media(
@ -108,6 +115,13 @@ async def send_unencrypted_message(
# BOT
class HateHeifBot(Plugin):
async def start(self) -> None:
await super().start()
self.config.load_and_update()
self.rooms = self.config['rooms'] if self.config['rooms'] else None
@command.passive("", msgtypes=(MessageType.IMAGE,))
async def hate_heif_message(
self,
@ -116,6 +130,12 @@ class HateHeifBot(Plugin):
"""
If heif = make it jpg.
"""
# If there are rooms specified in config, serve only these rooms
if self.rooms:
if evt.room_id not in self.rooms:
self.log.debug(f"Current room {evt.room_id} is not listed in {self.rooms} so I ignore it.")
return
# Double check if it is an image message
if evt.content.msgtype != MessageType.IMAGE:
return
@ -161,5 +181,8 @@ class HateHeifBot(Plugin):
content.info,
evt.client
)
@classmethod
def get_config_class(cls) -> Type[BaseProxyConfig]:
return Config
# the end.

View file

@ -1,8 +1,11 @@
id: eu.sergevictor.hateheif
version: 0.99.99
version: 0.99.100
license: MIT
modules:
- hateheif
config: true
extra_files:
- base-config.yaml
main_class: HateHeifBot
dependencies:
- pillow-heif