changeset 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 787e97d62404
children 49d305b0bd9b
files bot.py bridge.py irclib.py
diffstat 3 files changed, 17 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/bot.py
+++ b/bot.py
@@ -665,7 +665,7 @@ class bot(Thread):
 					self.error('===> Debug: no bridge found for "'+event.target().lower()+' at '+connection.server+'"', debug=True)
 					return
 				for bridge in bridges:
-					bridge.addParticipant('irc', nickname)
+					bridge.addParticipant('irc', nickname, irc_id=event.source())
 				return
 		
 		
--- a/bridge.py
+++ b/bridge.py
@@ -133,7 +133,7 @@ class bridge:
 			self.stop(message='failed to connect to the XMPP room')
 	
 	
-	def addParticipant(self, from_protocol, nickname, real_jid=None):
+	def addParticipant(self, from_protocol, nickname, real_jid=None, irc_id=None):
 		"""Add a participant to the bridge."""
 		if (from_protocol == 'irc' and nickname == self.irc_connection.get_nickname()) or (from_protocol == 'xmpp' and nickname == self.xmpp_room.nickname):
 			self.bot.error('===> Debug: not adding self ('+self.bot.nickname+') to bridge "'+str(self)+'"', debug=True)
@@ -142,6 +142,8 @@ class bridge:
 			p = self.getParticipant(nickname)
 			if p.protocol != from_protocol:
 				if from_protocol == 'irc' and isinstance(p.irc_connection, ServerConnection) and p.irc_connection.really_connected == True or from_protocol == 'xmpp' and isinstance(p.xmpp_c, xmpp.client.Client) and isinstance(p.muc, xmpp.muc):
+					if irc_id:
+						p.irc_connection.irc_id = irc_id
 					return
 				self.bot.error('===> Debug: "'+nickname+'" is on both sides of bridge "'+str(self)+'"', debug=True)
 				self.say('[Warning] The nickname "'+nickname+'" is used on both sides of the bridge, please avoid that if possible')
--- a/irclib.py
+++ b/irclib.py
@@ -71,6 +71,7 @@ import time
 import types
 import threading
 import traceback
+import math
 
 VERSION = 0, 4, 8
 DEBUG = 0
@@ -456,6 +457,7 @@ class ServerConnection(Connection):
 
 
         self.nick_callbacks = []
+        self.irc_id = None
         self.previous_buffer = ""
         self.handlers = {}
         self.real_server_name = ""
@@ -852,15 +854,21 @@ class ServerConnection(Connection):
 
     def privmsg(self, target, text):
         """Send a PRIVMSG command."""
-        # Should limit len(text) here!
         for l in text.split('\n'):
-            self.send_raw("PRIVMSG %s :%s" % (target, l))
+            l_size = len(l.encode('utf-8'))
+            available_size = float(510-len('%s PRIVMSG %s :' % (self.irc_id, target)))  # 510 is the size limit for IRC messages defined in RFC 2812
+            e = 0
+            for i in range(math.ceil(l_size/available_size)):
+                s = e
+                e = s+int(available_size)
+                while len(l[s:e].encode('utf-8')) >= available_size:
+                    e -= 1
+                self.send_raw("PRIVMSG %s :%s" % (target, l[s:e]))
 
     def privmsg_many(self, targets, text):
         """Send a PRIVMSG command to multiple targets."""
-        # Should limit len(text) here!
-        for l in text.split('\n'):
-            self.send_raw("PRIVMSG %s :%s" % (",".join(targets), l))
+        # Size of targets should be limited
+        self.privmsg(','.join(targets), text)
 
     def quit(self, message=""):
         """Send a QUIT command."""