From 2d5c80736df08819abdcc2b6e2168d671503aaba Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Fri, 26 Nov 2021 00:55:31 +0100 Subject: [PATCH] MessageParser: Fix crash (and unexpected behavior) when matching backslashes Closes GH-1497. --- plugins/MessageParser/plugin.py | 4 +++- plugins/MessageParser/test.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/plugins/MessageParser/plugin.py b/plugins/MessageParser/plugin.py index 186ae081a..bbb8ae0a5 100644 --- a/plugins/MessageParser/plugin.py +++ b/plugins/MessageParser/plugin.py @@ -183,7 +183,9 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler): self._updateRank(irc.network, channel, regexp) for (i, j) in enumerate(match.groups()): if match.group(i+1) is not None: - thisaction = re.sub(r'\$' + str(i+1), match.group(i+1), thisaction) + # Need a lambda to prevent re.sub from + # interpreting backslashes in the replacement + thisaction = re.sub(r'\$' + str(i+1), lambda _: match.group(i+1), thisaction) actions.append(thisaction) if max_triggers != 0 and max_triggers == len(actions): break diff --git a/plugins/MessageParser/test.py b/plugins/MessageParser/test.py index 08c3e5515..b460868fd 100644 --- a/plugins/MessageParser/test.py +++ b/plugins/MessageParser/test.py @@ -91,6 +91,13 @@ class MessageParserTestCase(ChannelPluginTestCase): self.feedMsg('test') self.assertResponse(' ', 'Error: No closing quotation') + def testMatchedBackslashes(self): + # Makes sure backslashes in matched arguments are not interpreted + # (re.sub interprets them in the repl argument for some reason...) + self.assertNotError(r'messageparser add test(.*)test "echo $1"') + self.feedMsg(r'testhello\xhellotest') + self.assertResponse(' ', r'hello\xhello') + def testShow(self): self.assertNotError('messageparser add "stuff" "echo i saw some stuff"') self.assertRegexp('messageparser show "nostuff"', 'there is no such regexp trigger')