comparison irclib.py @ 115:0ae0f8836a7a

split long messages when sending on IRC Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sat, 28 Nov 2009 23:33:48 +0100
parents 94fc538cdf65
children efdc038e757a
comparison
equal deleted inserted replaced
114:787e97d62404 115:0ae0f8836a7a
69 import sys 69 import sys
70 import time 70 import time
71 import types 71 import types
72 import threading 72 import threading
73 import traceback 73 import traceback
74 import math
74 75
75 VERSION = 0, 4, 8 76 VERSION = 0, 4, 8
76 DEBUG = 0 77 DEBUG = 0
77 78
78 # TODO 79 # TODO
454 self.lock.release() 455 self.lock.release()
455 return self 456 return self
456 457
457 458
458 self.nick_callbacks = [] 459 self.nick_callbacks = []
460 self.irc_id = None
459 self.previous_buffer = "" 461 self.previous_buffer = ""
460 self.handlers = {} 462 self.handlers = {}
461 self.real_server_name = "" 463 self.real_server_name = ""
462 self.real_nickname = self.nickname 464 self.real_nickname = self.nickname
463 self.username = username or self.nickname 465 self.username = username or self.nickname
850 """Send a PONG command.""" 852 """Send a PONG command."""
851 self.send_raw("PONG %s%s" % (target, target2 and (" " + target2))) 853 self.send_raw("PONG %s%s" % (target, target2 and (" " + target2)))
852 854
853 def privmsg(self, target, text): 855 def privmsg(self, target, text):
854 """Send a PRIVMSG command.""" 856 """Send a PRIVMSG command."""
855 # Should limit len(text) here!
856 for l in text.split('\n'): 857 for l in text.split('\n'):
857 self.send_raw("PRIVMSG %s :%s" % (target, l)) 858 l_size = len(l.encode('utf-8'))
859 available_size = float(510-len('%s PRIVMSG %s :' % (self.irc_id, target))) # 510 is the size limit for IRC messages defined in RFC 2812
860 e = 0
861 for i in range(math.ceil(l_size/available_size)):
862 s = e
863 e = s+int(available_size)
864 while len(l[s:e].encode('utf-8')) >= available_size:
865 e -= 1
866 self.send_raw("PRIVMSG %s :%s" % (target, l[s:e]))
858 867
859 def privmsg_many(self, targets, text): 868 def privmsg_many(self, targets, text):
860 """Send a PRIVMSG command to multiple targets.""" 869 """Send a PRIVMSG command to multiple targets."""
861 # Should limit len(text) here! 870 # Size of targets should be limited
862 for l in text.split('\n'): 871 self.privmsg(','.join(targets), text)
863 self.send_raw("PRIVMSG %s :%s" % (",".join(targets), l))
864 872
865 def quit(self, message=""): 873 def quit(self, message=""):
866 """Send a QUIT command.""" 874 """Send a QUIT command."""
867 # Note that many IRC servers don't use your QUIT message 875 # Note that many IRC servers don't use your QUIT message
868 # unless you've been connected for at least 5 minutes! 876 # unless you've been connected for at least 5 minutes!