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
|
|
|
|
2018-11-18 10:31:44 +00:00
|
|
|
REGEX_FACTOID = re.compile("{!factoid ([^}]+)}", re.I)
|
|
|
|
|
2018-11-18 07:55:43 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-11-18 10:31:44 +00:00
|
|
|
def _get_factoid(self, server, factoid):
|
|
|
|
name = factoid.lower().strip()
|
|
|
|
return name, server.get_setting("factoid-%s" % name, None)
|
|
|
|
|
2018-11-18 07:55:43 +00:00
|
|
|
@utils.hook("received.command.factoid", min_args=1)
|
|
|
|
def factoid(self, event):
|
2018-11-18 07:59:47 +00:00
|
|
|
"""
|
|
|
|
:help: Set/get a factoid
|
|
|
|
:usage: <key> [= value]
|
|
|
|
"""
|
2018-11-18 07:55:43 +00:00
|
|
|
if "=" in event["args"]:
|
|
|
|
key, _, value = event["args"].partition("=")
|
|
|
|
factoid = key.lower().strip()
|
2018-11-18 07:57:51 +00:00
|
|
|
event["server"].set_setting("factoid-%s" % factoid, value.strip())
|
2018-11-18 07:55:43 +00:00
|
|
|
|
|
|
|
event["stdout"].write("Set factoid '%s'" % factoid)
|
|
|
|
else:
|
2018-11-18 10:31:44 +00:00
|
|
|
name, value = self._get_factoid(event["server"], event["args"])
|
2018-11-18 07:55:43 +00:00
|
|
|
if value == None:
|
2018-11-18 10:31:44 +00:00
|
|
|
raise utils.EventError("Unknown factoid '%s'" % name)
|
|
|
|
event["stdout"].write("%s: %s" % (name, value))
|
|
|
|
|
|
|
|
@utils.hook("received.message.channel")
|
|
|
|
def channel_message(self, event):
|
|
|
|
match = REGEX_FACTOID.search(event["message"])
|
|
|
|
if match:
|
2019-05-09 12:48:45 +00:00
|
|
|
is_ignored_f = short_url = self.exports.get_one("is-ignored",
|
|
|
|
lambda _1, _2: False)
|
|
|
|
if is_ignored_f(event["server"], event["user"], "factoid"):
|
|
|
|
return
|
|
|
|
|
2018-11-18 10:31:44 +00:00
|
|
|
name, value = self._get_factoid(event["server"], match.group(1))
|
2018-11-18 11:18:37 +00:00
|
|
|
if not value == None:
|
2018-11-18 10:31:44 +00:00
|
|
|
self.events.on("send.stdout").call(target=event["channel"],
|
|
|
|
module_name="Factoids", server=event["server"],
|
|
|
|
message="%s: %s" % (name, value))
|