do splitting in DrBotServerConnection.privmsg

This commit is contained in:
Brian S. Stephan 2010-07-30 08:01:45 -05:00
parent cfc2923356
commit e1fe0eb4ca
2 changed files with 20 additions and 3 deletions

1
TODO
View File

@ -18,5 +18,4 @@ dr.botzo --- TODO
* any interesting web service stuff?
* obligatory info command
* similar to FactFile module, a generic Trigger module
* do splitting in DrBotServerConnection.privmsg
* settle on docstrings: http://www.python.org/dev/peps/pep-0257/

View File

@ -17,6 +17,7 @@
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
import os
import re
import socket
import sys
import inspect
@ -34,8 +35,25 @@ class DrBotServerConnection(irclib.ServerConnection):
def privmsg(self, target, text):
# Send a PRIVMSG command.
# TODO: length limiting or splitting
self.send_raw("PRIVMSG %s :%s" % (target, text))
splitter = "..."
# split messages that are too long. Max length is 512.
# TODO: this does not properly handle when the hostname has been
# masked by the ircd
space = 512 - len('\r\n') - len('PRIVMSG ') - len(' :') - len(target) - len(self.nickname) - len('!') - len(self.username) - len('@') - len(socket.getfqdn())
splitspace = space - (len(splitter) + 1)
if len(text) > space:
while len(text) > splitspace:
splitpos = text.rfind(' ', 0, splitspace)
splittext = text[0:splitpos] + ' ' + splitter
text = splitter + ' ' + text[splitpos+1:]
self.send_raw("PRIVMSG %s :%s" % (target, splittext))
# done splitting
self.send_raw("PRIVMSG %s :%s" % (target, text))
else:
self.send_raw("PRIVMSG %s :%s" % (target, text))
# DrBotIRC subclasses irclib's IRC, in order to create a DrBotServerConnection.