changeset 100:6289ac5a2db7

Reconnect when a connection is closed Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sun, 04 Oct 2009 23:47:09 +0200
parents 5390e9abfa44
children 29d3b85c6286
files bot.py bridge.py
diffstat 2 files changed, 41 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/bot.py
+++ b/bot.py
@@ -105,9 +105,7 @@ class bot(Thread):
 				self.error('=> Debug: invalid stanza', debug=True)
 				unlock = True
 			except xmpp.Conflict:
-				c.reconnectAndReauth()
-				for m in c.mucs:
-					m.rejoin()
+				self.reopen_xmpp_connection(c)
 				unlock = True
 			except:
 				error = '[Error] Unkonwn exception on XMPP thread:\n'
@@ -643,6 +641,31 @@ class bot(Thread):
 		return c
 	
 	
+	def reopen_xmpp_connection(self, c):
+		if not isinstance(c, xmpp.client.Client):
+			return
+		mucs = c.mucs
+		nickname = c.nickname
+		used_by = c.used_by
+		participants = []
+		for b in self.bridges:
+			for p in b.participants:
+				if p.xmpp_c == c:
+					participants.append(p)
+					p.xmpp_c = None
+		self.error('===> Debug: reopening XMPP connection for "'+nickname+'"', debug=True)
+		self.xmpp_connections.pop(nickname)
+		c.send(xmpp.protocol.Presence(typ='unavailable'))
+		del c
+		c = self.get_xmpp_connection(nickname)
+		c.used_by = used_by
+		for p in participants:
+			p.xmpp_c = c
+		c.mucs = mucs
+		for m in c.mucs:
+			m.rejoin()
+	
+	
 	def close_xmpp_connection(self, nickname):
 		if not self.xmpp_connections.has_key(nickname):
 			return
--- a/bridge.py
+++ b/bridge.py
@@ -212,7 +212,15 @@ class bridge:
 				elif left_protocol == 'irc':
 					p.createDuplicateOnIRC()
 			else:
-				was_on_both = False
+				if left_protocol == 'xmpp':
+					was_on_both = False
+				elif left_protocol == 'irc':
+					try:
+						p.irc_connection.join(self.irc_room)
+					except:
+						p._close_irc_connection()
+						p.createDuplicateOnIRC()
+					return
 		
 		elif p.protocol == 'irc':
 			if p.xmpp_c == 'both':
@@ -223,7 +231,12 @@ class bridge:
 				elif left_protocol == 'xmpp':
 					p.createDuplicateOnXMPP()
 			else:
-				was_on_both = False
+				if left_protocol == 'irc':
+					was_on_both = False
+				elif left_protocol == 'xmpp':
+					if isinstance(p.xmpp_c, xmpp.client.Client):
+						self.bot.reopen_xmpp_connection(p.xmpp_c)
+					return
 		
 		else:
 			raise Exception('[Internal Error] bad protocol')