Previous Section  < Day Day Up >  Next Section

Hack 79 Text Over Multiple Lines

figs/beginner.gif figs/hack79.gif

If you send a message that is too long, the end of it could get chopped off, leaving the recipient wondering in anticipation what you meant to say. Why is this a problem and how can you solve it?

The IRC RFC states that all lines sent to and from the server cannot be longer than 512 bytes, including the trailing carriage return and linefeed pair (\r\n). In practice, this means that the maximum length of any message will be approximately 460 bytes, as there will also be bytes used at the start of the line to say who the sender is and to whom the message is being sent.

If your nickname is Jibbler, you can send yourself the message "message" by typing this:

/msg Jibbler message

The message will then be sent to the IRC server before being returned to your IRC client. Your IRC client will end up sending something like this to the server:

PRIVMSG Jibbler :message\r\n

Including the two trailing \r\n characters, this line takes up 26 characters when you send it to the server. The simple 7-byte message has grown quite a bit already, but when this line arrives back from the server, you will see that it now looks even longer:

:Jibbler! PRIVMSG Jibbler :message\r\n

The line has now grown by 31 characters, to a size of 57 bytes. The extra characters appended to the beginning of the line say who the message is from. So in this case, you end up with a 50-byte overhead to any message that you send to yourself. This overhead will be even larger if you have a longer nickname, username, or hostname.

Also remember to take into account the length of channel names. Some servers allow ridiculously long channel names, and the length of your lines will reflect this when the channel inhabitants receive your messages, for example:

:Jibbler! PRIVMSG #arediculouslylongchannelname :message\r\n

13.3.1 Avoiding the Problem

There are many IRC clients out there, and each one has its own way of dealing with large messages. Some truncate the message and lose the end of it, while some will try to split it up and send it over multiple lines. If you are implementing your own client or bot, there is a simple way of spreading text over multiple lines while making each line as long as possible.

The first step is to send yourself a private message so you can see how long your message prefix will be. If you send the message "FLOODCHECK" to yourself, you will be expecting to see something like this back from the server:

:Jibbler! PRIVMSG Jibbler :FLOODCHECK\r\n

You could program your client or bot so that it doesn't display private messages from yourself. Take note of the length of the first part, as when you send a message to another user or channel, the only difference will be the target of the message. For example, when you send a message to #irchacks, the users in the channel will receive this line:

:Jibbler! PRIVMSG #irchacks :Hi guys\r\n

You can split this message up into four parts, as shown in Table 13-1.

Table 13-1. The four components of a message










Hi guys

When you send messages to other users or channels, you can assume that the length of the prefix will remain constant. If you are sending channel or private messages, you can also assume that the length of the type will remain the same. So the only parts that can change in length are the target and message.

You can now work out how long your received lines will be before you even send them. In the previous example, the prefix will take up 30 bytes and the type will take up 7 bytes. Add to this the three spaces that separate the parts of the line, the colon at the start of the message, and the trailing \r\n, and you end up with a minimum line length of 43 bytes.

You can now include the variable aspects of the line. If the target for your message is #irchacks, you can add another 9 bytes to accommodate that. Finally, you add on the length of your message. The message "Hi guys" increases the total line length by another 7 bytes, taking it to a total of 59.

Because this length of 59 is less than the maximum length of 512, you can tell straightaway that it is safe to send this message without its being truncated by the server. In fact, based on our previous reasoning, the longest message Jibbler can send to #irchacks is exactly 460 bytes (512-52).

If your message is greater than 460 bytes in length, you can chop it up and send it as separate messages, each being no longer than 460 bytes of course. Don't forget that longer nicknames, usernames, hostnames, and channel names will reduce this maximum message length.

Taking everything into account, it's pretty much safe to assume that messages less than 400 bytes in length can be sent without being truncated. The performance gains to be had by squeezing in a few more bytes per message is insignificant, so it's not really worth losing too much sleep about. If you do find yourself sending several huge lines of text in a row, then you should be asking yourself if IRC is really the most suitable medium for the output.

    Previous Section  < Day Day Up >  Next Section