MessageParser: Prevent empty matches from leaving unreplaced.

This commit is contained in:
Valentin Lorentz 2017-07-21 20:05:18 +02:00
parent b28f72fcee
commit 417f38b8c1
2 changed files with 21 additions and 1 deletions

View File

@ -175,7 +175,7 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
thisaction = action
self._updateRank(channel, regexp)
for (i, j) in enumerate(match.groups()):
if match.group(i+1):
if match.group(i+1) is not None:
thisaction = re.sub(r'\$' + str(i+1), match.group(i+1), thisaction)
actions.append(thisaction)
if max_triggers != 0 and max_triggers == len(actions):

View File

@ -64,6 +64,26 @@ class MessageParserTestCase(ChannelPluginTestCase):
world.testing = True
self.prefix = origuser
conf.supybot.plugins.MessageParser.requireManageCapability.setValue(origconf)
def testGroups(self):
self.assertNotError('messageparser add "this (.+) a(.*)" "echo $1 $2"')
self.feedMsg('this is a foo')
self.assertResponse(' ', 'is foo')
self.feedMsg('this is a')
self.assertResponse(' ', 'is')
self.assertNotError('messageparser remove "this (.+) a(.*)"')
self.assertNotError('messageparser add "this (.+) a(.*)" "echo $1"')
self.feedMsg('this is a foo')
self.assertResponse(' ', 'is')
self.feedMsg('this is a')
self.assertResponse(' ', 'is')
self.assertNotError('messageparser remove "this (.+) a(.*)"')
self.assertNotError('messageparser add "this( .+)? a(.*)" "echo $1 $2"')
self.feedMsg('this a foo')
self.assertResponse(' ', '$1 foo')
self.feedMsg('this a')
self.assertResponse(' ', '$1')
self.assertNotError('messageparser remove "this( .+)? a(.*)"')
def testShow(self):
self.assertNotError('messageparser add "stuff" "echo i saw some stuff"')