MessageParser: Add supybot.MessageParser.maxTriggers variable.

This commit is contained in:
Valentin Lorentz 2013-02-22 18:27:10 +01:00
parent 0ef1347bc6
commit 4069b2eba1
3 changed files with 25 additions and 0 deletions

View File

@ -76,5 +76,8 @@ conf.registerChannelValue(MessageParser, 'requireManageCapability',
conf.registerChannelValue(MessageParser, 'listSeparator',
registry.String(', ', _("""Determines the separator used between regexps when
shown by the list command.""")))
conf.registerChannelValue(MessageParser, 'maxTriggers',
registry.Integer(0, _("""Determines the maximum number of triggers in
one message. Set this to 0 to allow an infinite number of triggers.""")))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -167,6 +167,7 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
results.extend(map(lambda x: (channel,)+x, cursor.fetchall()))
if len(results) == 0:
return
max_triggers = self.registryValue('maxTriggers', channel)
for (channel, regexp, action) in results:
for match in re.finditer(regexp, msg.args[1]):
if match is not None:
@ -175,6 +176,11 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
for (i, j) in enumerate(match.groups()):
thisaction = re.sub(r'\$' + str(i+1), match.group(i+1), thisaction)
actions.append(thisaction)
if max_triggers != 0 and max_triggers == len(actions):
break
if max_triggers != 0 and max_triggers == len(actions):
break
for action in actions:
self._runCommandFunction(irc, msg, action)

View File

@ -90,6 +90,22 @@ class MessageParserTestCase(ChannelPluginTestCase):
m = self.getMsg(' ')
self.failUnless(str(m).startswith('PRIVMSG #test :i saw some stuff'))
def testMaxTriggers(self):
self.assertNotError('messageparser add "stuff" "echo i saw some stuff"')
self.assertNotError('messageparser add "sbd" "echo i saw somebody"')
self.feedMsg('this message issued by sbd has some stuff in it')
m = self.getMsg(' ')
self.failUnless(str(m).startswith('PRIVMSG #test :i saw some'))
m = self.getMsg(' ')
self.failUnless(str(m).startswith('PRIVMSG #test :i saw some'))
with conf.supybot.plugins.messageparser.maxtriggers.context(1):
self.feedMsg('this message issued by sbd has some stuff in it')
m = self.getMsg(' ')
self.failUnless(str(m).startswith('PRIVMSG #test :i saw some'))
m = self.getMsg(' ')
self.failIf(m)
def testLock(self):
self.assertNotError('messageparser add "stuff" "echo i saw some stuff"')
self.assertNotError('messageparser lock "stuff"')