[Twisted-Python] IRCClient: Handle lines that are too long for the server

Adrian Overbury skaarjj at gmail.com
Fri May 7 04:40:48 MDT 2010


So, there's a todo item in IRCClient, to handle lines that are longer
than the RFC maximum length (510 characters including the sender,
recipient and command, plus `\n` and `\r`).

I had thought to have a method sitting between the
IRCClient.sendLine() and the method which actually calls out to the
transport to put the data on the wire.  The default method would
simply pass the message through, sending it on to the server as is, to
be inevitably truncated at the other end.  The documentation could
contain examples of possible implementations, such as one which splits
the message up into multiple messages and sends them on, and one which
truncates the message.

For example, IRCClient could contain the method:

def longLineHandler(self, message):
   return message

This would necessitate adding a check to IRCClient to see what is
returned by longLineHandler().  I would suggest that it would accept a
single `str` or a list of `str`s as valid input, and anything else
will either make the sendLine() method do nothing and just silently
return, or will raise an exception.  If it returns silently, it would
allow the user-defined longLineHandler() to implement splitting a
message up into multiple messages by either returning a list of
`str`s, or by calling self.sendLine() themselves and then returning
None to have the original sendLine() return silently.

For example, if the user overrode longLineHandler() and made it split
the message into multiple messages, in peudocode it could go like this

call self.sendLine
   call self.longLineHandler with parameter 'message'
       is message longer than 510 characters?
           yes: split up command prefix and payload
                   use textwrap module to split payload into multiple
                     messages no longer than "510 - length of command
                     prefix"
                   prepend command prefix to each message and call
                     self.sendLine with each one
                   return None
           no: return message
   did self.longLineHandler return None?
           yes: return
   did self.longLineHandler return a list of `str`?
           yes: for each `str` in list: call self.reallySendLine or
            place on message queue
           no: call self.reallySendLine or place on message queue

The user might also want to have their longLineHandler raise an
exception if the line is too long, or they might want to have it make
the lines shorter than 510 characters total, or strip control codes if
they're too long to try and shorten it... there's a number of
different use cases for this, so I'm trying to build a framework for
it that's flexible enough to handle more or less anything.  I'd value
any comments anyone has on this.  I also have an enhancement ticket
open in the bug tracker, ticket #4416.

-- 

Adrian




More information about the Twisted-Python mailing list