From b12d8a8a04d3270013f638d31e88376f4027e78d Mon Sep 17 00:00:00 2001 From: Daniel Folkinshteyn Date: Wed, 28 Apr 2010 00:10:48 -0400 Subject: [PATCH] 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. --- plugins/Factoids/plugin.py | 10 +++++++--- plugins/Factoids/test.py | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/plugins/Factoids/plugin.py b/plugins/Factoids/plugin.py index 856e74f8a..aafde816f 100644 --- a/plugins/Factoids/plugin.py +++ b/plugins/Factoids/plugin.py @@ -245,7 +245,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.')) @@ -258,12 +259,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) diff --git a/plugins/Factoids/test.py b/plugins/Factoids/test.py index 46782c239..4c56f6698 100644 --- a/plugins/Factoids/test.py +++ b/plugins/Factoids/test.py @@ -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')