Skip to content

Commit 9ce5a99

Browse files
committed
Handle blank commands in IRCMessages
See ircdocs/parser-tests#20
1 parent 70d81a6 commit 9ce5a99

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

ircstream.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ def from_message(cls, message: str) -> IRCMessage:
175175
source = parts[0][1:]
176176
parts = parts[1:]
177177

178-
command = parts[0].upper()
178+
try:
179+
command = parts[0].upper()
180+
except IndexError:
181+
raise ValueError("Invalid IRC message (no command specified)")
182+
179183
original_params = parts[1:]
180184
params = []
181185

@@ -379,7 +383,12 @@ def _handle_line(self, bline: bytes) -> None:
379383
if not line:
380384
return
381385
self.log.debug("Data received", message=line)
382-
msg = IRCMessage.from_message(line)
386+
387+
try:
388+
msg = IRCMessage.from_message(line)
389+
except ValueError:
390+
# ignore unparseable commands
391+
return
383392

384393
whitelisted = ("CAP", "PASS", "USER", "NICK", "QUIT", "PING", "PONG")
385394
if not self.identified and msg.command not in whitelisted:

tests/test_rawsocket.py

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ def test_erroneous(clientsock):
113113
data = clientsock.readlines()
114114
assert any([b"432 *" in response for response in data])
115115

116+
clientsock.sendall(b":NOCOMMAND\n")
117+
data = clientsock.readlines()
118+
assert data == []
119+
116120

117121
def test_unicodeerror(ircserver, clientsock):
118122
"""Test for UnicodeError handling in both directions."""

0 commit comments

Comments
 (0)