From e9f6e8a06a25922659b24ff31b24ae6576f857fe Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 7 Feb 2020 13:35:28 +0000 Subject: [PATCH] revamp factoids.py; allow for both server-wide and per-channel factoids --- modules/factoids.py | 91 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 20 deletions(-) diff --git a/modules/factoids.py b/modules/factoids.py index 955f7490..389477d4 100644 --- a/modules/factoids.py +++ b/modules/factoids.py @@ -3,37 +3,88 @@ import re from src import ModuleManager, utils -REGEX_FACTOID = re.compile("{!factoid ([^}]+)}", re.I) +REGEX_FACTOID = re.compile("{!([^}]+)}", re.I) class Module(ModuleManager.BaseModule): - def _get_factoid(self, server, factoid): - name = factoid.lower().strip() - return name, server.get_setting("factoid-%s" % name, None) + def _get_factoid(self, targets, factoid): + setting = "factoid-%s" % factoid + for target_type, target in targets: + value = target.get_setting(setting, None) + if not value == None: + return target_type, value + return None + def _all_factoids(self, target): + factoids = {} + for factoid, value in target.find_settings(prefix="factoid-"): + factoids[factoid.replace("factoid-", "", 1)] = value + return factoids - @utils.hook("received.command.factoid", min_args=1) + def _set_factoid(self, target, factoid, value): + target.set_setting("factoid-%s" % factoid, value) + def _del_factoid(self, target, factoid): + target.del_setting("factoid-%s" % factoid) + + @utils.hook("received.command.factoid", permission="factoid") + @utils.hook("received.command.cfactoid", require_mode="o", + require_access="low,factoid") + @utils.kwarg("help", "Set or get a factoid") + + @utils.spec("!'list") + @utils.spec("!'get !wordlower") + @utils.spec("!'add !wordlower !string") + @utils.spec("!'remove !wordlower") def factoid(self, event): - """ - :help: Set/get a factoid - :usage: [= value] - """ - if "=" in event["args"]: - key, _, value = event["args"].partition("=") - factoid = key.lower().strip() - event["server"].set_setting("factoid-%s" % factoid, value.strip()) + factoid = event["spec"].get(1, None) - event["stdout"].write("Set factoid '%s'" % factoid) + if event["command"] == "cfactoid": + target = event["target"] + target_desc = "channel" else: - name, value = self._get_factoid(event["server"], event["args"]) + target = event["server"] + target_desc = "server" + + exists = False + if not factoid == None: + exists = not self._get_factoid([["", target]], factoid) == None + + if event["spec"][0] == "list": + all_factoids = self._all_factoids(target) + event["stdout"].write("Available %s factoids: %s" + % (target_desc, ", ".join(sorted(all_factoids.keys())))) + + elif event["spec"][0] == "get": + targets = [["server", event["server"]], ["channel", event["target"]]] + value = self._get_factoid(targets, factoid) if value == None: - raise utils.EventError("Unknown factoid '%s'" % name) - event["stdout"].write("%s: %s" % (name, value)) + raise utils.EventError("Unknown %s factoid '%s'" % factoid) + target_desc, value = value + event["stdout"].write("%s: %s" % (factoid, value)) + + elif event["spec"][0] == "add": + if exists: + raise utils.EventError("%s factoid '%s' already exists" + % (target_desc.title(), factoid)) + self._set_factoid(target, factoid, event["spec"][2]) + event["stdout"].write("Set %s factoid '%s'" + % (target_desc, factoid)) + + elif event["spec"][0] == "remove": + if not exists: + raise utils.EventError("%s factoid '%s' doesn't exist" + % (target_desc.title(), factoid)) + self._del_factoid(target, factoid) + event["stdout"].write("Removed %s factoid '%s'" + % (target_desc, factoid)) + @utils.hook("command.regex") @utils.kwarg("ignore_action", False) @utils.kwarg("command", "factoid") @utils.kwarg("pattern", REGEX_FACTOID) def channel_message(self, event): - name, value = self._get_factoid(event["server"], - event["match"].group(1)) + targets = [["server", event["server"]], ["channel", event["target"]]] + factoid = event["match"].group(1) + value = self._get_factoid(targets, factoid) if not value == None: - event["stdout"].write("%s: %s" % (name, value)) + target_desc, value = value + event["stdout"].write("%s: %s" % (factoid, value))