From 417f38b8c14351701280a2197ab120fe4337a33a Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Fri, 21 Jul 2017 20:05:18 +0200 Subject: [PATCH] MessageParser: Prevent empty matches from leaving unreplaced. --- plugins/MessageParser/plugin.py | 2 +- plugins/MessageParser/test.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/plugins/MessageParser/plugin.py b/plugins/MessageParser/plugin.py index 4bc5e23de..b410f1517 100644 --- a/plugins/MessageParser/plugin.py +++ b/plugins/MessageParser/plugin.py @@ -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): diff --git a/plugins/MessageParser/test.py b/plugins/MessageParser/test.py index e547d2004..ec381b50c 100644 --- a/plugins/MessageParser/test.py +++ b/plugins/MessageParser/test.py @@ -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"')