2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
|
2018-11-18 10:31:44 +00:00
|
|
|
import re
|
2018-11-18 07:56:55 +00:00
|
|
|
from src import ModuleManager, utils
|
2018-11-18 07:55:43 +00:00
|
|
|
|
2020-02-07 13:35:28 +00:00
|
|
|
REGEX_FACTOID = re.compile("{!([^}]+)}", re.I)
|
2019-06-26 13:37:26 +00:00
|
|
|
|
2018-11-18 07:55:43 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2020-02-07 13:35:28 +00:00
|
|
|
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
|
2020-02-09 00:24:47 +00:00
|
|
|
def _all_factoids(self, targets):
|
2020-02-07 13:35:28 +00:00
|
|
|
factoids = {}
|
2020-02-09 00:24:47 +00:00
|
|
|
for target in targets:
|
|
|
|
for factoid, value in target.find_settings(prefix="factoid-"):
|
|
|
|
factoid = factoid.replace("factoid-", "", 1)
|
|
|
|
if not factoid in factoids:
|
|
|
|
factoids[factoid] = value
|
2020-02-07 13:35:28 +00:00
|
|
|
return factoids
|
2018-11-18 10:31:44 +00:00
|
|
|
|
2020-02-07 13:35:28 +00:00
|
|
|
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)
|
|
|
|
|
2020-02-09 00:24:47 +00:00
|
|
|
def _format_factoid(self, s, targets, depth=0):
|
|
|
|
if depth == 5:
|
|
|
|
return
|
|
|
|
|
|
|
|
for match in REGEX_FACTOID.finditer(s):
|
|
|
|
key = match.group(1)
|
|
|
|
value = self._get_factoid(targets, key)
|
|
|
|
if value:
|
|
|
|
target_desc, value = value
|
|
|
|
value = self._format_factoid(value, targets, depth+1)
|
|
|
|
s = s.replace(match.group(0), value, 1)
|
|
|
|
return s
|
|
|
|
|
2020-02-07 13:35:28 +00:00
|
|
|
@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")
|
2018-11-18 07:55:43 +00:00
|
|
|
def factoid(self, event):
|
2020-02-07 13:35:28 +00:00
|
|
|
factoid = event["spec"].get(1, None)
|
|
|
|
|
|
|
|
if event["command"] == "cfactoid":
|
|
|
|
target = event["target"]
|
|
|
|
target_desc = "channel"
|
2018-11-18 07:55:43 +00:00
|
|
|
else:
|
2020-02-07 13:35:28 +00:00
|
|
|
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)
|
2018-11-18 07:55:43 +00:00
|
|
|
if value == None:
|
2020-02-07 13:35:28 +00:00
|
|
|
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))
|
|
|
|
|
2018-11-18 10:31:44 +00:00
|
|
|
|
2019-06-26 13:37:26 +00:00
|
|
|
@utils.hook("command.regex")
|
|
|
|
@utils.kwarg("ignore_action", False)
|
|
|
|
@utils.kwarg("command", "factoid")
|
|
|
|
@utils.kwarg("pattern", REGEX_FACTOID)
|
2018-11-18 10:31:44 +00:00
|
|
|
def channel_message(self, event):
|
2020-02-07 13:35:28 +00:00
|
|
|
targets = [["server", event["server"]], ["channel", event["target"]]]
|
|
|
|
factoid = event["match"].group(1)
|
|
|
|
value = self._get_factoid(targets, factoid)
|
2019-05-18 19:18:55 +00:00
|
|
|
if not value == None:
|
2020-02-07 13:35:28 +00:00
|
|
|
target_desc, value = value
|
2020-02-09 00:24:47 +00:00
|
|
|
value = self._format_factoid(value, targets)
|
2020-02-07 13:35:28 +00:00
|
|
|
event["stdout"].write("%s: %s" % (factoid, value))
|