From b00a81290f231b24382f74b9a0e881f1a3ed7e78 Mon Sep 17 00:00:00 2001 From: John Sullivan Date: Thu, 22 Feb 2024 22:12:10 -0500 Subject: [PATCH 1/2] use length in bytes of message/command to determine when to wrap IRC messages previously, we used len(message)/len(command) to determine when to wrap, but that doesn't necessarily correlate to the number of bytes, which is what the IRC spec cares about --- src/bobbit/protocol/irc.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bobbit/protocol/irc.py b/src/bobbit/protocol/irc.py index dfb5375..29cb505 100644 --- a/src/bobbit/protocol/irc.py +++ b/src/bobbit/protocol/irc.py @@ -149,9 +149,10 @@ async def send_message(self, message): if isinstance(message, Message): message = self.format_message(message) - if len(message) > MESSAGE_LENGTH_MAX: + # NOTE: use str.encode() to determine length of message/command since IRC only cares about number of bytes + if len(message.encode()) > MESSAGE_LENGTH_MAX: command, message = message.split(' :', 1) - messages = [f'{command} :{m}' for m in textwrap.wrap(message, MESSAGE_LENGTH_MAX - len(command) - 2)] + messages = [f'{command} :{m}' for m in textwrap.wrap(message, MESSAGE_LENGTH_MAX - len(command.encode()) - 2)] else: messages = [message] From 50a0d39cecb0a4f9247c44ea4fd5faa317e56c81 Mon Sep 17 00:00:00 2001 From: John Sullivan Date: Sat, 24 Feb 2024 11:22:27 -0500 Subject: [PATCH 2/2] changes bobbit to use a more conservative max message length for IRC IRC messages can be at most 512 bytes, including the sender's hostmask determining the hostmask 100% accurately before sending each message is hard, so setting a conservative max length makes the most sense --- src/bobbit/protocol/irc.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/bobbit/protocol/irc.py b/src/bobbit/protocol/irc.py index 29cb505..9069dd2 100644 --- a/src/bobbit/protocol/irc.py +++ b/src/bobbit/protocol/irc.py @@ -25,7 +25,11 @@ NICK_RE = re.compile(r':(?P.*?)!\S+\s+?NICK\s+(?P[^\s]+)') REGISTERED_RE = re.compile(r':NickServ!.*NOTICE.*:.*(identified|logged in|accepted).*') -MESSAGE_LENGTH_MAX = 512 - len(CRNL) +# NOTE: IRC message length limit is 512 bytes, but that includes the sender's hostmask +# since the hostmask can change during a session, the simplest way to ensure messages are under 512 bytes +# is to set a conservative maximum like 400 +# so few bobbit messages are that long that it shouldn't cost too many extra messages being sent +MESSAGE_LENGTH_MAX = 400 # IRC Client