Skip to content

Commit 4f25061

Browse files
authored
Merge pull request CloudBotIRC#124 from linuxdaemon/gonzobot+command-bypass-fix
Change command lookups to help avoid bypassing +g filters
2 parents dbeac49 + 2348112 commit 4f25061

3 files changed

Lines changed: 27 additions & 21 deletions

File tree

cloudbot/bot.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from cloudbot.plugin import PluginManager
1919
from cloudbot.event import Event, CommandEvent, RegexEvent, EventType
2020
from cloudbot.util import database, formatting
21-
from cloudbot.clients.irc import IrcClient
21+
from cloudbot.clients.irc import IrcClient, irc_clean
2222

2323
try:
2424
from cloudbot.web.main import WebInterface
@@ -242,18 +242,22 @@ def process(self, event):
242242
if event.type is EventType.message:
243243
# Commands
244244
if event.chan.lower() == event.nick.lower(): # private message, no command prefix
245-
command_re = r'(?i)^(?:[{}]?|{}[,;:]+\s+)(\w+)(?:$|\s+)(.*)'.format(command_prefix, event.conn.nick)
245+
command_re = r'(?i)^(?:[{}]?|{}[,;:]+\s+)(\w+)(?:$|\s+)(.*)'
246246
else:
247-
command_re = r'(?i)^(?:[{}]|{}[,;:]+\s+)(\w+)(?:$|\s+)(.*)'.format(command_prefix, event.conn.nick)
247+
command_re = r'(?i)^(?:[{}]|{}[,;:]+\s+)(\w+)(?:$|\s+)(.*)'
248248

249-
cmd_match = re.match(command_re, event.content)
249+
cmd_match = re.match(
250+
command_re.format(command_prefix, event.conn.nick),
251+
event.content_raw
252+
)
250253

251254
if cmd_match:
252255
command = cmd_match.group(1).lower()
256+
text = irc_clean(cmd_match.group(2).strip())
253257
if command in self.plugin_manager.commands:
254258
command_hook = self.plugin_manager.commands[command]
255-
command_event = CommandEvent(hook=command_hook, text=cmd_match.group(2).strip(),
256-
triggered_command=command, base_event=event)
259+
command_event = CommandEvent(hook=command_hook, text=text,
260+
triggered_command=command, base_event=event)
257261
tasks.append(self.plugin_manager.launch(command_hook, command_event))
258262
else:
259263
potential_matches = []
@@ -263,8 +267,8 @@ def process(self, event):
263267
if potential_matches:
264268
if len(potential_matches) == 1:
265269
command_hook = potential_matches[0][1]
266-
command_event = CommandEvent(hook=command_hook, text=cmd_match.group(2).strip(),
267-
triggered_command=command, base_event=event)
270+
command_event = CommandEvent(hook=command_hook, text=text,
271+
triggered_command=command, base_event=event)
268272
tasks.append(self.plugin_manager.launch(command_hook, command_event))
269273
else:
270274
event.notice("Possible matches: {}".format(

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)