From 9e5a0da6636a530ffeb21c1c5eb5ab6fcbcdeb78 Mon Sep 17 00:00:00 2001 From: jesopo Date: Sun, 9 Feb 2020 00:24:47 +0000 Subject: support nested factoids (4 deep max) --- modules/factoids.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'modules') diff --git a/modules/factoids.py b/modules/factoids.py index 389477d4..57fb1006 100644 --- a/modules/factoids.py +++ b/modules/factoids.py @@ -13,10 +13,13 @@ class Module(ModuleManager.BaseModule): if not value == None: return target_type, value return None - def _all_factoids(self, target): + def _all_factoids(self, targets): factoids = {} - for factoid, value in target.find_settings(prefix="factoid-"): - factoids[factoid.replace("factoid-", "", 1)] = value + 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 return factoids def _set_factoid(self, target, factoid, value): @@ -24,11 +27,23 @@ class Module(ModuleManager.BaseModule): def _del_factoid(self, target, factoid): target.del_setting("factoid-%s" % factoid) + 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 + @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") @@ -87,4 +102,5 @@ class Module(ModuleManager.BaseModule): value = self._get_factoid(targets, factoid) if not value == None: target_desc, value = value + value = self._format_factoid(value, targets) event["stdout"].write("%s: %s" % (factoid, value)) -- cgit v1.3.1-10-gc9f91