IRC 512 message limit

I’m using the irc library for my bot. During testing to make sure non-priviledged members cannot abuse the commands I encountered this case:

> !gline 2rqy1KLL5D2MMNVDf9WdKXxZLKxOr0SwGvnMV3DAxJTd3DrJkt519I8ps8nIvko2rqy1KLL5D2MMNVDf9WdKXxZLKxOr0SwGvnMV3DAxJTd3DrJkt519I8ps8nIvko2rqy1KLL5D2MMNVDf9WdKXxZLKxOr0SwGvnMV3DAxJTd3DrJkt519I8ps8nIvko2rqy1KLL5D2MMNVDf9WdKXxZLKxOr0SwGvnMV3DAxJTd3DrJkt519I8ps8nIvko2rqy1KLL5D2MMNVDf9WdKXxZLKxOr0SwGvnMV3DAxJTd3DrJkt519I8ps8nIvko

Running this command kills the bot with irc.client.MessageTooLong: Messages limited to 512 bytes including CR/LF

However this message is only 322 characters long:

msg = "!gline 2rqy1KLL5D2MMNVDf9WdKXxZLKxOr0SwGvnMV3DAxJTd3DrJkt519I8ps8nIvko2rqy1KLL5D2MMNVDf9WdKXxZLKxOr0SwGvnMV3DAxJTd3DrJkt519I8ps8nIvko2rqy1KLL5D2MMNVDf9WdKXxZLKxOr0SwGvnMV3DAxJTd3DrJkt519I8ps8nIvko2rqy1KLL5D2MMNVDf9WdKXxZLKxOr0SwGvnMV3DAxJTd3DrJkt519I8ps8nIvko2rqy1KLL5D2MMNVDf9WdKXxZLKxOr0SwGvnMV3DAxJTd3DrJkt519I8ps8nIvko"

print(len(msg))
# 322
    def on_pubmsg(self, server: ServerConnection, event: Event) -> None:
        """
        Public command handler
        """

        # The person who invoked the command
        invoker = event.source.nick

        # Name of the channel where the command was invoked by the invoker
        channel_name = event.target

        # Channel object corresponding to the channel above channel name
        channel_obj = self.channels[channel_name]

        # The command (along with it's arguments)
        message = event.arguments[0]

        # This check doesn't actually protect me in this case, because the offending command is only 322 characters long
        if len(message) >= 512:
            server.notice(invoker, "Commands cannot be longer than 512 characters.")
            return

So clearly message and msg are not actually the same content. So what is message inside the function actually? I would suggest to print or log its repr.

Keep in mind that the IRC protocol’s message limit is not just for
the content but also associated metadata sent between the client and
server (the combined length of any “prefix”, “command” and one or
more “parameters” plus necessary “separators” and end-of-line
marker).

See IETF RFC 2812 §2.3 for details:

https://www.rfc-editor.org/rfc/rfc2812#section-2.3

Probably what you want to do is wrap your function call in a
try/except block to catch that exception and then break up the
content into multiple messages when necessary, or proactively check
the length first and break it into a series of suitably shorter
payloads, but how you go about that will depend on what you’re doing
with the content that’s being sent.

It’s also possible the irc module already has a separate method for
breaking up long strings into multiple message parameters, I don’t
know. You’d need to dig into the documentation (or source code) to
find out.

msg is not part of the code, I just added it to show that it’s only 322 characters.

That makes sense, I’ll dig around a bit and see. Thank you.