Use the ircutils.standardSubsitute function upon factoid output.

This allows inclusion of the usual standardSubstitute vars within factoids.

There is no config to disable this, since it is possible to escape the substitutions
by simply doubling the dollar signs, as per the python documentation:
http://docs.python.org/library/string.html#template-strings

Thus, if you want a factoid to output a literal "$channel", for example, all
you'd need to do is use "$$channel" in your factoid text, which will come out
as "$channel" when said by the bot.

Also added tests for this new behavior.
This commit is contained in:
Daniel Folkinshteyn 2010-04-28 00:10:48 -04:00
parent 71f88caa6b
commit 976ad82d69
2 changed files with 15 additions and 3 deletions

View File

@ -243,7 +243,8 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
if factoids:
if number:
try:
irc.reply(factoids[number-1][0])
irc.reply(ircutils.standardSubstitute(irc, msg,
factoids[number-1][0]))
self._updateRank(channel, [factoids[number-1]])
except IndexError:
irc.error('That\'s not a valid number for that key.')
@ -256,12 +257,15 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
return ircutils.standardSubstitute(irc, msg,
formatter, env)
if len(factoids) == 1:
irc.reply(prefixer(factoids[0][0]))
irc.reply(ircutils.standardSubstitute(irc, msg,
prefixer(factoids[0][0])))
else:
factoidsS = []
counter = 1
for factoid in factoids:
factoidsS.append(format('(#%i) %s', counter, factoid[0]))
factoidsS.append(format('(#%i) %s', counter,
ircutils.standardSubstitute(irc, msg,
factoid[0])))
counter += 1
irc.replies(factoidsS, prefixer=prefixer,
joiner=', or ', onlyPrefixFirst=True)

View File

@ -174,6 +174,14 @@ class FactoidsTestCase(ChannelPluginTestCase):
self.assertNotError('learn foob as barb')
self.assertRegexp('whatis foom', 'foo.*foob')
def testStandardSubstitute(self):
self.assertNotError('learn foo as this is $channel, and hour is $hour')
self.assertRegexp('whatis foo', 'this is #test, and hour is \d{1,2}')
self.assertNotError('learn bar as this is $$channel escaped')
self.assertRegexp('whatis bar', 'this is \$channel')
self.assertNotError('learn bar as this is $minute')
self.assertRegexp('whatis bar', '\$channel.*\d{1,2}')
def testAlias(self):
self.assertNotError('learn foo as bar')
self.assertNotError('alias foo zoog')