Skip to content

Commit

Permalink
MessageParser: On syntax error, detail which action caused the error
Browse files Browse the repository at this point in the history
This can help users debug it.
  • Loading branch information
progval committed Aug 2, 2023
1 parent bb3d456 commit 71ae97e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 6 additions & 5 deletions plugins/MessageParser/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,16 @@ def _updateRank(self, network, channel, regexp):
cursor.execute("UPDATE triggers SET usage_count=? WHERE regexp=?", (old_count + 1, regexp,))
db.commit()

def _runCommandFunction(self, irc, msg, command):
def _runCommandFunction(self, irc, msg, command, action_name):
"""Run a command from message, as if command was sent over IRC."""
try:
tokens = callbacks.tokenize(command,
channel=msg.channel, network=irc.network)
except SyntaxError as e:
# Emulate what callbacks.py does
self.log.debug('Error return: %s', utils.exnToString(e))
irc.error(str(e))
irc.error(format('%s, in %r (triggered by %r)',
e, command, action_name))
try:
self.Proxy(irc.irc, msg, tokens)
except Exception as e:
Expand Down Expand Up @@ -200,15 +201,15 @@ def do_privmsg_notice(self, irc, msg):
# 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((regexp, 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)
for (regexp, action) in actions:
self._runCommandFunction(irc, msg, action, regexp)

def doPrivmsg(self, irc, msg):
if not callbacks.addressed(irc, msg): #message is not direct command
Expand Down
5 changes: 4 additions & 1 deletion plugins/MessageParser/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ def testGroups(self):
def testSyntaxError(self):
self.assertNotError(r'messageparser add "test" "echo foo \" bar"')
self.feedMsg('test')
self.assertResponse(' ', 'Error: No closing quotation')
self.assertResponse(
' ',
r"""Error: No closing quotation, in """
r"""'echo foo " bar' (triggered by 'test')""")

def testMatchedBackslashes(self):
# Makes sure backslashes in matched arguments are not interpreted
Expand Down

0 comments on commit 71ae97e

Please sign in to comment.