changeset 5:cb0daec4b778

Added support for IRC "nick" event, fixed participant.changeNickname(), fixed handling of IRC "namreply" event, removed muc._check() because waiting does not solve the problem if it is blocking incoming messages handling Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sun, 16 Aug 2009 17:59:00 +0200
parents 012593ed4e12
children a9bfe4f7bf72
files bot.py bridge.py irclib.py muc.py participant.py
diffstat 5 files changed, 35 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/bot.py
+++ b/bot.py
@@ -225,8 +225,11 @@ class bot(Thread):
 				# Lost bridge IRC connection, we must reconnect if we want the bridge to work
 				self.recreate_bridge(connection.bridge)
 				return
-			if connection.bridge.mode == 'normal' and event.arguments()[0] != 'Closing object':
+			if connection.bridge.mode == 'normal' and connection.closing == False:
 				connection.bridge.switchToLimitedMode()
+			if connection.closing == True:
+				connection.close()
+			return
 		elif event.eventtype() == 'nicknameinuse':
 			if connection.nick_callback:
 				connection.nick_callback('nicknameinuse')
@@ -247,7 +250,9 @@ class bot(Thread):
 				self.error('connection.nick_callback='+str(connection.nick_callback), debug=True)
 			return
 		elif event.eventtype() == 'namreply':
-			for nickname in re.split(' [@\+]?', event.arguments()[2].strip()):
+			for nickname in re.split('(?:^[@\+]?|(?: [@\+]?)*)', event.arguments()[2].strip()):
+				if nickname == '':
+					continue
 				try:
 					connection.bridge.addParticipant('irc', nickname)
 				except:
@@ -262,12 +267,15 @@ class bot(Thread):
 					connection.bridge.getParticipant(nickname)
 				except NoSuchParticipantException:
 					connection.bridge.addParticipant('irc', nickname)
+			return
 		try:
 			from_ = connection.bridge.getParticipant(event.source().split('!')[0])
 			if event.eventtype() == 'quit' or event.eventtype() == 'part' and event.target() == connection.bridge.irc_room:
 				if from_.protocol in ['irc', 'both']:
 					connection.bridge.removeParticipant('irc', from_.nickname, event.arguments()[0])
 				return
+			if event.eventtype() == 'nick' and from_.protocol == 'irc':
+				from_.changeNickname(event.target(), 'xmpp')
 		except NoSuchParticipantException:
 			return
 		except AttributeError:
--- a/bridge.py
+++ b/bridge.py
@@ -151,8 +151,8 @@ class bridge:
 			if p.protocol == 'xmpp':
 				i += 1
 				if p.irc_connection:
+					p.irc_connection.closing = True
 					p.irc_connection.disconnect('Bridge is switching to limited mode')
-					p.irc_connection.close()
 					p.irc_connection = None
 		self.irc_connections_limit = i
 		self.bot.error('===> Bridge is switching to limited mode.')
--- a/irclib.py
+++ b/irclib.py
@@ -411,6 +411,7 @@ class ServerConnection(Connection):
         if self.connected:
             self.disconnect("Changing servers")
 
+        self.closing = False  # added for xib
         self.previous_buffer = ""
         self.handlers = {}
         self.real_server_name = ""
--- a/muc.py
+++ b/muc.py
@@ -88,24 +88,13 @@ class muc:
 				self.callback(errors)
 	
 	
-	def _check(self):
-		i = 0
-		while not self.connected:
-			i += 1
-			if i > 30:
-				raise Exception('Error: connection to room timed out')
-			sleep(1)
-	
-	
 	def say(self, message):
 		"""Say message in the room"""
-		self._check()
 		self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid, typ='groupchat', body=message))
 	
 	
 	def sayTo(self, to, message):
 		"""Send a private message"""
-		self._check()
 		self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid+'/'+to, typ='chat', body=message))
 	
 	
--- a/participant.py
+++ b/participant.py
@@ -62,9 +62,6 @@ class participant:
 		if ' ' in self.nickname:
 			self.bridge.bot.error('===> Debug: "'+self.nickname+'" contains a white space character, duplicate cannot be created on the IRC chan of bridge "'+str(self.bridge)+'"', debug=True)
 			self.bridge.say('[Warning] The nickname "'+self.nickname+'" contains a white space character, duplicate cannot be created on IRC, please avoid that if possible')
-			if self.irc_connection:
-				self.irc_connection.close()
-				self.irc_connection = None
 			return
 		sleep(1) # try to prevent "reconnecting too fast" shit
 		self.irc_connection = self.bridge.bot.irc.server()
@@ -83,12 +80,14 @@ class participant:
 				self.bridge.bot.error('===> Debug: "'+self.nickname+'" is already used in the IRC chan of bridge "'+str(self.bridge)+'"', debug=True)
 				self.bridge.say('[Warning] The nickname "'+self.nickname+'" is used on both rooms or reserved on the IRC server, please avoid that if possible')
 				self.protocol = 'both'
-				self.irc_connection.close()
+				self.irc_connection.closing = True
+				self.irc_connection.disconnect()
 				self.irc_connection = None
 			elif error == 'erroneusnickname':
 				self.bridge.bot.error('===> Debug: "'+self.nickname+'" got "erroneusnickname" on bridge "'+str(self.bridge)+'"', debug=True)
 				self.bridge.say('[Warning] The nickname "'+self.nickname+'" contains non-ASCII characters and cannot be used in the IRC channel, please avoid that if possible')
-				self.irc_connection.close()
+				self.irc_connection.closing = True
+				self.irc_connection.disconnect()
 				self.irc_connection = None
 	
 	
@@ -118,15 +117,27 @@ class participant:
 		if self.protocol == 'xmpp':
 			if on_protocol == 'xmpp':
 				raise Exception('Internal Error: wanted to change nickname on bad protocol')
-			if self.irc_connection:
+			self.nickname = newnick
+			if ' ' in self.nickname:
+				self.bridge.bot.error('===> Debug: "'+self.nickname+'" contains a white space character, duplicate cannot be created on the IRC chan of bridge "'+str(self.bridge)+'"', debug=True)
+				self.bridge.say('[Warning] The nickname "'+self.nickname+'" contains a white space character, duplicate cannot be created on IRC, please avoid that if possible')
+				if self.irc_connection != None:
+					self.irc_connection.closing = True
+					self.irc_connection.disconnect()
+					self.irc_connection = None
+				return
+			if self.irc_connection != None:
 				self.irc_connection.nick(newnick)
-			self.nickname = newnick
+			else:
+				self.createDuplicateOnIRC()
 		elif self.protocol == 'irc':
 			if on_protocol == 'irc':
 				raise Exception('Internal Error: wanted to change nickname on bad protocol')
+			self.nickname = newnick
 			if self.muc:
 				self.muc.change_nick(newnick, callback=self._xmpp_join_callback)
-			self.nickname = newnick
+			else:
+				self.createDuplicateOnXMPP()
 		elif self.protocol == 'both':
 			if on_protocol == 'irc':
 				self.protocol = 'xmpp'
@@ -188,14 +199,12 @@ class participant:
 	def leave(self, message):
 		if message == None:
 			message = ''
-		try:
+		if self.muc:
 			self.muc.leave(message)
-		except AttributeError:
-			pass
-		try:
+		if self.irc_connection:
+			self.irc_connection.closing = True
 			self.irc_connection.disconnect(message)
-		except AttributeError:
-			pass
+			self.irc_connection = None
 		self.nickname = None