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 committed by Valentin Lorentz
parent 8ed94257ba
commit b12d8a8a04
2 changed files with 15 additions and 3 deletions

View File

@ -245,7 +245,8 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
if factoids: if factoids:
if number: if number:
try: try:
irc.reply(factoids[number-1][0]) irc.reply(ircutils.standardSubstitute(irc, msg,
factoids[number-1][0]))
self._updateRank(channel, [factoids[number-1]]) self._updateRank(channel, [factoids[number-1]])
except IndexError: except IndexError:
irc.error(_('That\'s not a valid number for that key.')) irc.error(_('That\'s not a valid number for that key.'))
@ -258,12 +259,15 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
return ircutils.standardSubstitute(irc, msg, return ircutils.standardSubstitute(irc, msg,
formatter, env) formatter, env)
if len(factoids) == 1: if len(factoids) == 1:
irc.reply(prefixer(factoids[0][0])) irc.reply(ircutils.standardSubstitute(irc, msg,
prefixer(factoids[0][0])))
else: else:
factoidsS = [] factoidsS = []
counter = 1 counter = 1
for factoid in factoids: 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 counter += 1
irc.replies(factoidsS, prefixer=prefixer, irc.replies(factoidsS, prefixer=prefixer,
joiner=', or ', onlyPrefixFirst=True) joiner=', or ', onlyPrefixFirst=True)

View File

@ -174,6 +174,14 @@ class FactoidsTestCase(ChannelPluginTestCase):
self.assertNotError('learn foob as barb') self.assertNotError('learn foob as barb')
self.assertRegexp('whatis foom', 'foo.*foob') 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): def testAlias(self):
self.assertNotError('learn foo as bar') self.assertNotError('learn foo as bar')
self.assertNotError('alias foo zoog') self.assertNotError('alias foo zoog')