revamp factoids.py; allow for both server-wide and per-channel factoids
This commit is contained in:
parent
3c6eb6f8e7
commit
e9f6e8a06a
1 changed files with 71 additions and 20 deletions
|
@ -3,37 +3,88 @@
|
||||||
import re
|
import re
|
||||||
from src import ModuleManager, utils
|
from src import ModuleManager, utils
|
||||||
|
|
||||||
REGEX_FACTOID = re.compile("{!factoid ([^}]+)}", re.I)
|
REGEX_FACTOID = re.compile("{!([^}]+)}", re.I)
|
||||||
|
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
def _get_factoid(self, server, factoid):
|
def _get_factoid(self, targets, factoid):
|
||||||
name = factoid.lower().strip()
|
setting = "factoid-%s" % factoid
|
||||||
return name, server.get_setting("factoid-%s" % name, None)
|
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 !<name>wordlower")
|
||||||
|
@utils.spec("!'add !<name>wordlower !<value>string")
|
||||||
|
@utils.spec("!'remove !<name>wordlower")
|
||||||
def factoid(self, event):
|
def factoid(self, event):
|
||||||
"""
|
factoid = event["spec"].get(1, None)
|
||||||
:help: Set/get a factoid
|
|
||||||
:usage: <key> [= value]
|
|
||||||
"""
|
|
||||||
if "=" in event["args"]:
|
|
||||||
key, _, value = event["args"].partition("=")
|
|
||||||
factoid = key.lower().strip()
|
|
||||||
event["server"].set_setting("factoid-%s" % factoid, value.strip())
|
|
||||||
|
|
||||||
event["stdout"].write("Set factoid '%s'" % factoid)
|
if event["command"] == "cfactoid":
|
||||||
|
target = event["target"]
|
||||||
|
target_desc = "channel"
|
||||||
else:
|
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:
|
if value == None:
|
||||||
raise utils.EventError("Unknown factoid '%s'" % name)
|
raise utils.EventError("Unknown %s factoid '%s'" % factoid)
|
||||||
event["stdout"].write("%s: %s" % (name, value))
|
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.hook("command.regex")
|
||||||
@utils.kwarg("ignore_action", False)
|
@utils.kwarg("ignore_action", False)
|
||||||
@utils.kwarg("command", "factoid")
|
@utils.kwarg("command", "factoid")
|
||||||
@utils.kwarg("pattern", REGEX_FACTOID)
|
@utils.kwarg("pattern", REGEX_FACTOID)
|
||||||
def channel_message(self, event):
|
def channel_message(self, event):
|
||||||
name, value = self._get_factoid(event["server"],
|
targets = [["server", event["server"]], ["channel", event["target"]]]
|
||||||
event["match"].group(1))
|
factoid = event["match"].group(1)
|
||||||
|
value = self._get_factoid(targets, factoid)
|
||||||
if not value == None:
|
if not value == None:
|
||||||
event["stdout"].write("%s: %s" % (name, value))
|
target_desc, value = value
|
||||||
|
event["stdout"].write("%s: %s" % (factoid, value))
|
||||||
|
|
Loading…
Reference in a new issue