Skip to content

Commit 382dc0f

Browse files
authored
Merge pull request CloudBotIRC#189 from linuxdaemon/gonzobot+new-parser
Rewrite core parser based off parser used for IrcOut hooks
2 parents b224d30 + 0d6202c commit 382dc0f

File tree

1 file changed

+31
-46
lines changed

1 file changed

+31
-46
lines changed

cloudbot/clients/irc.py

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from cloudbot.client import Client
99
from cloudbot.event import Event, EventType, IrcOutEvent
1010
from cloudbot.util import async_util
11+
from cloudbot.util.parsers.irc import Message
1112

1213
logger = logging.getLogger("cloudbot")
1314

@@ -335,43 +336,17 @@ def data_received(self, data):
335336
line_data, self._input_buffer = self._input_buffer.split(b"\r\n", 1)
336337
line = decode(line_data)
337338

338-
# parse the line into a message
339-
if line.startswith(":"):
340-
prefix_line_match = irc_prefix_re.match(line)
341-
if prefix_line_match is None:
342-
logger.critical("[{}] Received invalid IRC line '{}' from {}".format(
343-
self.conn.name, line, self.conn.describe_server()))
344-
continue
345-
346-
netmask_prefix, command, params = prefix_line_match.groups()
347-
prefix = ":" + netmask_prefix # TODO: Do we need to know this?
348-
netmask_match = irc_netmask_re.match(netmask_prefix)
349-
if netmask_match is None:
350-
# This isn't in the format of a netmask
351-
nick = netmask_prefix
352-
user = None
353-
host = None
354-
mask = netmask_prefix
355-
else:
356-
nick = netmask_match.group(1)
357-
user = netmask_match.group(2)
358-
host = netmask_match.group(3)
359-
mask = netmask_prefix
360-
else:
361-
prefix = None
362-
noprefix_line_match = irc_noprefix_re.match(line)
363-
if noprefix_line_match is None:
364-
logger.critical("[{}] Received invalid IRC line '{}' from {}".format(
365-
self.conn.name, line, self.conn.describe_server()))
366-
continue
367-
command = noprefix_line_match.group(1)
368-
params = noprefix_line_match.group(2)
369-
nick = None
370-
user = None
371-
host = None
372-
mask = None
373-
374-
command_params = irc_param_re.findall(params)
339+
try:
340+
message = Message.parse(line)
341+
except Exception:
342+
logger.exception(
343+
"[%s] Error occurred while parsing IRC line '%s' from %s",
344+
self.conn.name, line, self.conn.describe_server()
345+
)
346+
continue
347+
348+
command = message.command
349+
command_params = message.parameters
375350

376351
# Reply to pings immediately
377352

@@ -381,9 +356,8 @@ def data_received(self, data):
381356
# Parse the command and params
382357

383358
# Content
384-
if command_params and command_params[-1].startswith(":"):
385-
# If the last param is in the format of `:content` remove the `:` from it, and set content from it
386-
content_raw = command_params[-1][1:]
359+
if command_params.has_trail:
360+
content_raw = command_params[-1]
387361
content = irc_clean(content_raw)
388362
else:
389363
content_raw = None
@@ -426,21 +400,32 @@ def data_received(self, data):
426400
channel = command_params[0]
427401
elif command == "INVITE":
428402
channel = command_params[1]
429-
elif len(command_params) > 2 or command_params[0][0] != ':':
403+
elif len(command_params) > 2 or not (command_params.has_trail and len(command_params) == 1):
430404
channel = command_params[0]
431405

406+
prefix = message.prefix
407+
408+
nick = prefix.nick
409+
user = prefix.user
410+
host = prefix.host
411+
mask = prefix.mask
412+
432413
if channel:
414+
# TODO Migrate plugins to accept the original case of the channel
433415
channel = channel.lower()
434-
if channel[0] == ':':
435-
channel = channel[1:].split()[0] # Just in case there is more data
416+
417+
channel = channel.split()[0] # Just in case there is more data
418+
436419
if channel == self.conn.nick.lower():
437420
channel = nick.lower()
438421

439422
# Set up parsed message
440423
# TODO: Do we really want to send the raw `prefix` and `command_params` here?
441-
event = Event(bot=self.bot, conn=self.conn, event_type=event_type, content_raw=content_raw, content=content,
442-
target=target, channel=channel, nick=nick, user=user, host=host, mask=mask, irc_raw=line,
443-
irc_prefix=prefix, irc_command=command, irc_paramlist=command_params, irc_ctcp_text=ctcp_text)
424+
event = Event(
425+
bot=self.bot, conn=self.conn, event_type=event_type, content_raw=content_raw, content=content,
426+
target=target, channel=channel, nick=nick, user=user, host=host, mask=mask, irc_raw=line,
427+
irc_prefix=mask, irc_command=command, irc_paramlist=command_params, irc_ctcp_text=ctcp_text
428+
)
444429

445430
# handle the message, async
446431
async_util.wrap_future(self.bot.process(event), loop=self.loop)

0 commit comments

Comments
 (0)