From b2ca8a059964a834b8832cb9f5b2a4c9efd0f0dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Thu, 14 Jun 2018 10:50:30 +0300 Subject: [PATCH] Only pass bytestrings (and not bytearrays) to user code --- botcmd.py | 5 +++-- ircbot.py | 2 ++ line_handling.py | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/botcmd.py b/botcmd.py index 25dbce8..217ab41 100644 --- a/botcmd.py +++ b/botcmd.py @@ -7,6 +7,7 @@ def initialize(*, config): # on_connect(*, irc) # Called after IRC bot has connected and sent the USER/NICk commands but not yet attempted anything else +# Called for every reconnect # Blocks the bot until it's done, including PING/PONG handling # irc is the IRC API object def on_connect(*, irc): @@ -26,7 +27,7 @@ def on_quit(*, irc): # nick is who sent the message # channel is where you should send the response (note: in queries nick == channel) # irc is the IRC API object -# All strings are bytestrings or bytearrays +# All strings are bytestrings def handle_message(*, prefix, message, nick, channel, irc): ... @@ -36,6 +37,6 @@ def handle_message(*, prefix, message, nick, channel, irc): # command is the command or number code # arguments is rest of the arguments of the command, represented as a list. ':'-arguments are handled automatically # irc is the IRC API object -# All strings are bytestrings or bytearrays +# All strings are bytestrings def handle_nonmessage(*, prefix, command, arguments, irc): ... diff --git a/ircbot.py b/ircbot.py index 97ae380..2c5214c 100644 --- a/ircbot.py +++ b/ircbot.py @@ -194,6 +194,8 @@ class ServerThread(threading.Thread): pass else: self.logging_channel.send((logmessage_types.received, line.decode(encoding = 'utf-8', errors = 'replace'))) + # Ensure we have a bytestring, because bytearray can be annoying to deal with + line = bytes(line) line_handling.handle_line(line, irc = self.api) def mainloop(self): diff --git a/line_handling.py b/line_handling.py index 6b84b45..21ccccf 100644 --- a/line_handling.py +++ b/line_handling.py @@ -114,7 +114,7 @@ class LineHandlerThread(threading.Thread): try: prefix, command, arguments = parse_line(self.line) except LineParsingError: - irc.error("Cannot parse line" + self.line.decode(encoding = 'utf-8', errors = 'replace')) + irc.error("Cannot parse line: " + self.line.decode(encoding = 'utf-8', errors = 'replace')) if command.upper() == b'PRIVMSG': # PRIVMSG should have two parameters: recipient and the message