switch module whitelist/blacklist to its own config file
This commit is contained in:
parent
150148b4e0
commit
8e611c451e
2 changed files with 20 additions and 12 deletions
|
@ -6,8 +6,8 @@ URL: str = "https://bitbot.dev"
|
||||||
|
|
||||||
import enum, queue, os, queue, select, socket, sys, threading, time, traceback
|
import enum, queue, os, queue, select, socket, sys, threading, time, traceback
|
||||||
import typing, uuid
|
import typing, uuid
|
||||||
from src import EventManager, Exports, IRCServer, Logging, ModuleManager
|
from src import Config, EventManager, Exports, IRCServer, Logging
|
||||||
from src import PollHook, PollSource, Socket, Timers, utils
|
from src import ModuleManager, PollHook, PollSource, Socket, Timers, utils
|
||||||
|
|
||||||
class TriggerResult(enum.Enum):
|
class TriggerResult(enum.Enum):
|
||||||
Return = 1
|
Return = 1
|
||||||
|
@ -147,9 +147,16 @@ class Bot(object):
|
||||||
self.log.critical("panic() called: %s", [reason], exc_info=exc_info)
|
self.log.critical("panic() called: %s", [reason], exc_info=exc_info)
|
||||||
sys.exit(utils.consts.Exit.PANIC)
|
sys.exit(utils.consts.Exit.PANIC)
|
||||||
|
|
||||||
|
def get_config(self, name: str) -> Config.Config:
|
||||||
|
path = os.path.join(self.data_directory, "%s.conf" % name)
|
||||||
|
config = Config.Config(name, path)
|
||||||
|
config.load()
|
||||||
|
return config
|
||||||
|
|
||||||
def _module_lists(self):
|
def _module_lists(self):
|
||||||
whitelist = self.config.get_list("module-whitelist")
|
module_lists = self.get_config("modules")
|
||||||
blacklist = self.config.get_list("module-blacklist")
|
whitelist = module_lists.get_list("whitelist")
|
||||||
|
blacklist = module_lists.get_list("blacklist")
|
||||||
|
|
||||||
return whitelist, blacklist
|
return whitelist, blacklist
|
||||||
|
|
||||||
|
|
|
@ -102,10 +102,11 @@ class Module(ModuleManager.BaseModule):
|
||||||
event["stderr"].write(result.message)
|
event["stderr"].write(result.message)
|
||||||
|
|
||||||
def _get_blacklist(self):
|
def _get_blacklist(self):
|
||||||
return self.bot.config.get_list("module-blacklist")
|
config = self.bot.get_config("modules")
|
||||||
def _save_blacklist(self, modules):
|
return config, config.get_list("blacklist")
|
||||||
self.bot.config.set_list("module-blacklist", modules)
|
def _save_blacklist(self, config, modules):
|
||||||
self.bot.config.save()
|
config.set_list("blacklist", sorted(modules))
|
||||||
|
config.save()
|
||||||
|
|
||||||
@utils.hook("received.command.enablemodule")
|
@utils.hook("received.command.enablemodule")
|
||||||
@utils.kwarg("min_args", 1)
|
@utils.kwarg("min_args", 1)
|
||||||
|
@ -115,12 +116,12 @@ class Module(ModuleManager.BaseModule):
|
||||||
def enable(self, event):
|
def enable(self, event):
|
||||||
name = event["args_split"][0].lower()
|
name = event["args_split"][0].lower()
|
||||||
|
|
||||||
blacklist = self._get_blacklist()
|
config, blacklist = self._get_blacklist()
|
||||||
if not name in blacklist:
|
if not name in blacklist:
|
||||||
raise utils.EventError("Module '%s' isn't disabled" % name)
|
raise utils.EventError("Module '%s' isn't disabled" % name)
|
||||||
|
|
||||||
blacklist.remove(name)
|
blacklist.remove(name)
|
||||||
self._save_blacklist(blacklist)
|
self._save_blacklist(config, blacklist)
|
||||||
event["stdout"].write("Module '%s' has been enabled and can now "
|
event["stdout"].write("Module '%s' has been enabled and can now "
|
||||||
"be loaded" % name)
|
"be loaded" % name)
|
||||||
|
|
||||||
|
@ -136,11 +137,11 @@ class Module(ModuleManager.BaseModule):
|
||||||
self.bot.modules.unload_module(name)
|
self.bot.modules.unload_module(name)
|
||||||
and_unloaded = " and unloaded"
|
and_unloaded = " and unloaded"
|
||||||
|
|
||||||
blacklist = self._get_blacklist()
|
config, blacklist = self._get_blacklist()
|
||||||
if name in blacklist:
|
if name in blacklist:
|
||||||
raise utils.EventError("Module '%s' is already disabled" % name)
|
raise utils.EventError("Module '%s' is already disabled" % name)
|
||||||
|
|
||||||
blacklist.append(name)
|
blacklist.append(name)
|
||||||
self._save_blacklist(blacklist)
|
self._save_blacklist(config, blacklist)
|
||||||
event["stdout"].write("Module '%s' has been disabled%s" % (
|
event["stdout"].write("Module '%s' has been disabled%s" % (
|
||||||
name, and_unloaded))
|
name, and_unloaded))
|
||||||
|
|
Loading…
Reference in a new issue