MessageParser: Fix crash (and unexpected behavior) when matching backslashes

Closes GH-1497.
This commit is contained in:
Valentin Lorentz 2021-11-26 00:55:31 +01:00
parent 068488c546
commit 2d5c80736d
2 changed files with 10 additions and 1 deletions

View File

@ -183,7 +183,9 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
self._updateRank(irc.network, channel, regexp) self._updateRank(irc.network, channel, regexp)
for (i, j) in enumerate(match.groups()): for (i, j) in enumerate(match.groups()):
if match.group(i+1) is not None: 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) actions.append(thisaction)
if max_triggers != 0 and max_triggers == len(actions): if max_triggers != 0 and max_triggers == len(actions):
break break

View File

@ -91,6 +91,13 @@ class MessageParserTestCase(ChannelPluginTestCase):
self.feedMsg('test') self.feedMsg('test')
self.assertResponse(' ', 'Error: No closing quotation') 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): def testShow(self):
self.assertNotError('messageparser add "stuff" "echo i saw some stuff"') self.assertNotError('messageparser add "stuff" "echo i saw some stuff"')
self.assertRegexp('messageparser show "nostuff"', 'there is no such regexp trigger') self.assertRegexp('messageparser show "nostuff"', 'there is no such regexp trigger')