Skip to content

Commit e6dab25

Browse files
committed
Changed the command lookups to use the raw message rather than the sanitized one
1 parent 93331b4 commit e6dab25

3 files changed

Lines changed: 30 additions & 27 deletions

File tree

cloudbot/bot.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,20 @@ def process(self, event):
240240

241241
if event.type is EventType.message:
242242
# Commands
243-
if event.chan.lower() == event.nick.lower(): # private message, no command prefix
244-
command_re = r'(?i)^(?:[{}]?|{}[,;:]+\s+)(\w+)(?:$|\s+)(.*)'.format(command_prefix, event.conn.nick)
245-
else:
246-
command_re = r'(?i)^(?:[{}]|{}[,;:]+\s+)(\w+)(?:$|\s+)(.*)'.format(command_prefix, event.conn.nick)
247-
248-
cmd_match = re.match(command_re, event.content)
249-
250-
if cmd_match:
251-
command = cmd_match.group(1).lower()
243+
text = event.content
244+
raw_text = event.content_raw
245+
command = None
246+
if raw_text[0] in command_prefix:
247+
command = raw_text[1:].split(maxsplit=1)[0]
248+
elif event.chan.lower() == event.nick.lower():
249+
command = raw_text.split(maxsplit=1)[0]
250+
251+
if command:
252+
_, _, text = text.partition(' ')
252253
if command in self.plugin_manager.commands:
253254
command_hook = self.plugin_manager.commands[command]
254-
command_event = CommandEvent(hook=command_hook, text=cmd_match.group(2).strip(),
255-
triggered_command=command, base_event=event)
255+
command_event = CommandEvent(hook=command_hook, text=text,
256+
triggered_command=command, base_event=event)
256257
tasks.append(self.plugin_manager.launch(command_hook, command_event))
257258
else:
258259
potential_matches = []
@@ -262,16 +263,16 @@ def process(self, event):
262263
if potential_matches:
263264
if len(potential_matches) == 1:
264265
command_hook = potential_matches[0][1]
265-
command_event = CommandEvent(hook=command_hook, text=cmd_match.group(2).strip(),
266-
triggered_command=command, base_event=event)
266+
command_event = CommandEvent(hook=command_hook, text=text,
267+
triggered_command=command, base_event=event)
267268
tasks.append(self.plugin_manager.launch(command_hook, command_event))
268269
else:
269270
event.notice("Possible matches: {}".format(
270271
formatting.get_text_list([command for command, plugin in potential_matches])))
271272

272273
# Regex hooks
273274
for regex, regex_hook in self.plugin_manager.regex_hooks:
274-
if not regex_hook.run_on_cmd and cmd_match:
275+
if not regex_hook.run_on_cmd and command:
275276
pass
276277
else:
277278
regex_match = regex.search(event.content)

cloudbot/clients/irc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,9 @@ def data_received(self, data):
400400

401401
# Set up parsed message
402402
# TODO: Do we really want to send the raw `prefix` and `command_params` here?
403-
event = Event(bot=self.bot, conn=self.conn, event_type=event_type, content=content, target=target,
404-
channel=channel, nick=nick, user=user, host=host, mask=mask, irc_raw=line, irc_prefix=prefix,
405-
irc_command=command, irc_paramlist=command_params, irc_ctcp_text=ctcp_text)
403+
event = Event(bot=self.bot, conn=self.conn, event_type=event_type, content_raw=content_raw, content=content,
404+
target=target, channel=channel, nick=nick, user=user, host=host, mask=mask, irc_raw=line,
405+
irc_prefix=prefix, irc_command=command, irc_paramlist=command_params, irc_ctcp_text=ctcp_text)
406406

407407
# handle the message, async
408408
asyncio.async(self.bot.process(event), loop=self.loop)

cloudbot/event.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class Event:
4141
"""
4242

4343
def __init__(self, *, bot=None, hook=None, conn=None, base_event=None, event_type=EventType.other, content=None,
44-
target=None, channel=None, nick=None, user=None, host=None, mask=None, irc_raw=None, irc_prefix=None,
45-
irc_command=None, irc_paramlist=None, irc_ctcp_text=None):
44+
content_raw=None, target=None, channel=None, nick=None, user=None, host=None, mask=None, irc_raw=None,
45+
irc_prefix=None, irc_command=None, irc_paramlist=None, irc_ctcp_text=None):
4646
"""
4747
All of these parameters except for `bot` and `hook` are optional.
4848
The irc_* parameters should only be specified for IRC events.
@@ -102,6 +102,7 @@ def __init__(self, *, bot=None, hook=None, conn=None, base_event=None, event_typ
102102
# If base_event is provided, don't check these parameters, just inherit
103103
self.type = base_event.type
104104
self.content = base_event.content
105+
self.content_raw = base_event.content_raw
105106
self.target = base_event.target
106107
self.chan = base_event.chan
107108
self.nick = base_event.nick
@@ -118,6 +119,7 @@ def __init__(self, *, bot=None, hook=None, conn=None, base_event=None, event_typ
118119
# Since base_event wasn't provided, we can take these parameters
119120
self.type = event_type
120121
self.content = content
122+
self.content_raw = content_raw
121123
self.target = target
122124
self.chan = channel
123125
self.nick = nick
@@ -328,17 +330,17 @@ class CommandEvent(Event):
328330
"""
329331

330332
def __init__(self, *, bot=None, hook, text, triggered_command, conn=None, base_event=None, event_type=None,
331-
content=None, target=None, channel=None, nick=None, user=None, host=None, mask=None, irc_raw=None,
332-
irc_prefix=None, irc_command=None, irc_paramlist=None):
333+
content=None, content_raw=None, target=None, channel=None, nick=None, user=None, host=None, mask=None,
334+
irc_raw=None, irc_prefix=None, irc_command=None, irc_paramlist=None):
333335
"""
334336
:param text: The arguments for the command
335337
:param triggered_command: The command that was triggered
336338
:type text: str
337339
:type triggered_command: str
338340
"""
339341
super().__init__(bot=bot, hook=hook, conn=conn, base_event=base_event, event_type=event_type, content=content,
340-
target=target, channel=channel, nick=nick, user=user, host=host, mask=mask, irc_raw=irc_raw,
341-
irc_prefix=irc_prefix, irc_command=irc_command, irc_paramlist=irc_paramlist)
342+
content_raw=content_raw, target=target, channel=channel, nick=nick, user=user, host=host, mask=mask,
343+
irc_raw=irc_raw, irc_prefix=irc_prefix, irc_command=irc_command, irc_paramlist=irc_paramlist)
342344
self.hook = hook
343345
self.text = text
344346
self.doc = self.hook.doc
@@ -370,14 +372,14 @@ class RegexEvent(Event):
370372
:type match: re.__Match
371373
"""
372374

373-
def __init__(self, *, bot=None, hook, match, conn=None, base_event=None, event_type=None, content=None, target=None,
374-
channel=None, nick=None, user=None, host=None, mask=None, irc_raw=None, irc_prefix=None,
375+
def __init__(self, *, bot=None, hook, match, conn=None, base_event=None, event_type=None, content=None, content_raw=None,
376+
target=None, channel=None, nick=None, user=None, host=None, mask=None, irc_raw=None, irc_prefix=None,
375377
irc_command=None, irc_paramlist=None):
376378
"""
377379
:param: match: The match objected returned by the regex search method
378380
:type match: re.__Match
379381
"""
380382
super().__init__(bot=bot, conn=conn, hook=hook, base_event=base_event, event_type=event_type, content=content,
381-
target=target, channel=channel, nick=nick, user=user, host=host, mask=mask, irc_raw=irc_raw,
382-
irc_prefix=irc_prefix, irc_command=irc_command, irc_paramlist=irc_paramlist)
383+
content_raw=content_raw, target=target, channel=channel, nick=nick, user=user, host=host, mask=mask,
384+
irc_raw=irc_raw, irc_prefix=irc_prefix, irc_command=irc_command, irc_paramlist=irc_paramlist)
383385
self.match = match

0 commit comments

Comments
 (0)