Skip to content

Commit 9e5a0da

Browse files
committed
support nested factoids (4 deep max)
1 parent d223009 commit 9e5a0da

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

modules/factoids.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,37 @@ def _get_factoid(self, targets, factoid):
1313
if not value == None:
1414
return target_type, value
1515
return None
16-
def _all_factoids(self, target):
16+
def _all_factoids(self, targets):
1717
factoids = {}
18-
for factoid, value in target.find_settings(prefix="factoid-"):
19-
factoids[factoid.replace("factoid-", "", 1)] = value
18+
for target in targets:
19+
for factoid, value in target.find_settings(prefix="factoid-"):
20+
factoid = factoid.replace("factoid-", "", 1)
21+
if not factoid in factoids:
22+
factoids[factoid] = value
2023
return factoids
2124

2225
def _set_factoid(self, target, factoid, value):
2326
target.set_setting("factoid-%s" % factoid, value)
2427
def _del_factoid(self, target, factoid):
2528
target.del_setting("factoid-%s" % factoid)
2629

30+
def _format_factoid(self, s, targets, depth=0):
31+
if depth == 5:
32+
return
33+
34+
for match in REGEX_FACTOID.finditer(s):
35+
key = match.group(1)
36+
value = self._get_factoid(targets, key)
37+
if value:
38+
target_desc, value = value
39+
value = self._format_factoid(value, targets, depth+1)
40+
s = s.replace(match.group(0), value, 1)
41+
return s
42+
2743
@utils.hook("received.command.factoid", permission="factoid")
2844
@utils.hook("received.command.cfactoid", require_mode="o",
2945
require_access="low,factoid")
3046
@utils.kwarg("help", "Set or get a factoid")
31-
3247
@utils.spec("!'list")
3348
@utils.spec("!'get !<name>wordlower")
3449
@utils.spec("!'add !<name>wordlower !<value>string")
@@ -87,4 +102,5 @@ def channel_message(self, event):
87102
value = self._get_factoid(targets, factoid)
88103
if not value == None:
89104
target_desc, value = value
105+
value = self._format_factoid(value, targets)
90106
event["stdout"].write("%s: %s" % (factoid, value))

0 commit comments

Comments
 (0)