From 41c59618cc82bf18d2a1dd9d0bc8ed0989a0a9f3 Mon Sep 17 00:00:00 2001 From: Hash Borgir Date: Sun, 3 Mar 2024 15:01:44 -0700 Subject: [PATCH] Funguy Plugin Enable/Disable feature per room, save to conf file --- funguy.conf | 4 +++ funguy.py | 69 +++++++++++++++++++++++++++++++++++--- plugins/youtube-preview.py | 2 +- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/funguy.conf b/funguy.conf index 81a6589..d53c3d0 100644 --- a/funguy.conf +++ b/funguy.conf @@ -10,3 +10,7 @@ blocklist = [] admin_user = "@hashborgir:mozilla.org" prefix = "!" config_file = "funguy.conf" + +[plugins.disabled] +"!uFhErnfpYhhlauJsNK:matrix.org" = [ "youtube-preview",] +"!vYcfWXpPvxeQvhlFdV:matrix.org" = [] diff --git a/funguy.py b/funguy.py index 156abd8..1dcd9df 100755 --- a/funguy.py +++ b/funguy.py @@ -1,5 +1,8 @@ #!/usr/bin/env python -# funguy.py + +""" +Funguy Bot Class +""" import os import logging @@ -8,22 +11,23 @@ import simplematrixbotlib as botlib from dotenv import load_dotenv import time import sys +import toml + from plugins.config import FunguyConfig class FunguyBot: - bot = None - config = None - def __init__(self): self.PLUGINS_DIR = "plugins" self.PLUGINS = {} self.config = None self.bot = None + self.disabled_plugins = {} # Dictionary to store disabled plugins for each room self.load_dotenv() self.setup_logging() self.load_plugins() self.load_config() + self.load_disabled_plugins() def load_dotenv(self): load_dotenv() @@ -54,6 +58,21 @@ class FunguyBot: def load_config(self): self.config = FunguyConfig() + def load_disabled_plugins(self): + if os.path.exists('funguy.conf'): + with open('funguy.conf', 'r') as f: + config_data = toml.load(f) + self.disabled_plugins = config_data.get('plugins', {}).get('disabled', {}) + + def save_disabled_plugins(self): + existing_config = {} + if os.path.exists('funguy.conf'): + with open('funguy.conf', 'r') as f: + existing_config = toml.load(f) + existing_config['plugins'] = {'disabled': self.disabled_plugins} + with open('funguy.conf', 'w') as f: + toml.dump(existing_config, f) + async def handle_commands(self, room, message): match = botlib.MessageMatch(room, message, self.bot, self.config.prefix) if match.is_not_from_this_bot() and match.prefix() and match.command("reload"): @@ -63,8 +82,33 @@ class FunguyBot: else: await self.bot.api.send_text_message(room.room_id, "You are not authorized to reload plugins.") + if match.is_not_from_this_bot() and match.prefix() and match.command("disable"): + if str(message.sender) == self.config.admin_user: + args = match.args() + if len(args) != 2: + await self.bot.api.send_text_message(room.room_id, "Usage: !disable ") + else: + plugin_name, room_id = args + await self.disable_plugin(room_id, plugin_name) + await self.bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' disabled for room '{room_id}'") + else: + await self.bot.api.send_text_message(room.room_id, "You are not authorized to disable plugins.") + + if match.is_not_from_this_bot() and match.prefix() and match.command("enable"): + if str(message.sender) == self.config.admin_user: + args = match.args() + if len(args) != 2: + await self.bot.api.send_text_message(room.room_id, "Usage: !enable ") + else: + plugin_name, room_id = args + await self.enable_plugin(room_id, plugin_name) + await self.bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' enabled for room '{room_id}'") + else: + await self.bot.api.send_text_message(room.room_id, "You are not authorized to enable plugins.") + for plugin_name, plugin_module in self.PLUGINS.items(): - await plugin_module.handle_command(room, message, self.bot, self.config.prefix, self.config) + if plugin_name not in self.disabled_plugins.get(room.room_id, []): + await plugin_module.handle_command(room, message, self.bot, self.config.prefix, self.config) if match.is_not_from_this_bot() and match.prefix() and match.command("rehash"): if str(message.sender) == self.config.admin_user: @@ -77,6 +121,18 @@ class FunguyBot: del self.config self.config = FunguyConfig() + async def disable_plugin(self, room_id, plugin_name): + if room_id not in self.disabled_plugins: + self.disabled_plugins[room_id] = [] + if plugin_name not in self.disabled_plugins[room_id]: + self.disabled_plugins[room_id].append(plugin_name) + self.save_disabled_plugins() + + async def enable_plugin(self, room_id, plugin_name): + if room_id in self.disabled_plugins and plugin_name in self.disabled_plugins[room_id]: + self.disabled_plugins[room_id].remove(plugin_name) + self.save_disabled_plugins() + def run(self): MATRIX_URL = os.getenv("MATRIX_URL") MATRIX_USER = os.getenv("MATRIX_USER") @@ -93,3 +149,6 @@ class FunguyBot: if __name__ == "__main__": bot = FunguyBot() bot.run() + + + diff --git a/plugins/youtube-preview.py b/plugins/youtube-preview.py index 9613c58..905a6f5 100644 --- a/plugins/youtube-preview.py +++ b/plugins/youtube-preview.py @@ -32,7 +32,7 @@ async def fetch_youtube_info(youtube_url): async def handle_command(room, message, bot, prefix, config): match = botlib.MessageMatch(room, message, bot, prefix) - if match.is_not_from_this_bot() and re.search(r'youtube\.com/watch\?v=', message.body) and room.room_id != '!uFhErnfpYhhlauJsNK:matrix.org': + if match.is_not_from_this_bot() and re.search(r'youtube\.com/watch\?v=', message.body): # and room.room_id != '!uFhErnfpYhhlauJsNK:matrix.org': logging.info("YouTube link detected") video_id_match = re.search(r'youtube\.com/watch\?v=([^\s]+)', message.body) if video_id_match: