changeset 259:3b318108e9e0

(muc) created "state" attribute to replace the "connected" one Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Thu, 11 Mar 2010 14:15:31 +0100
parents 3b4826e14642
children 9a2302e8382b
files bridge.py muc.py participant.py
diffstat 3 files changed, 15 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/bridge.py
+++ b/bridge.py
@@ -142,7 +142,7 @@ class Bridge:
 		try:
 			p = self.get_participant(nickname)
 			if p.protocol != from_protocol:
-				if from_protocol == 'irc' and isinstance(p.irc_connection, ServerConnection) and p.irc_connection.logged_in and p.irc_connection.real_nickname == nickname or from_protocol == 'xmpp' and isinstance(p.xmpp_c, xmpp.client.Client) and isinstance(p.muc, xmpp.muc) and p.xmpp_c.nickname == nickname:
+				if from_protocol == 'irc' and isinstance(p.irc_connection, ServerConnection) and p.irc_connection.logged_in or from_protocol == 'xmpp' and isinstance(p.muc, xmpp.muc) and p.muc.state >= p.muc.JOINING:
 					return p
 				p.set_both_sides()
 			return p
@@ -301,7 +301,6 @@ class Bridge:
 					else:
 						c = self.bot.irc.get_connection(self.irc_server, self.irc_port, p.duplicate_nickname)
 						if not (c and self.irc_room in c.left_channels):
-							p._close_irc_connection(leave_message)
 							p.create_duplicate_on_irc()
 					return
 		
@@ -317,7 +316,7 @@ class Bridge:
 				if left_protocol == 'irc':
 					was_on_both = False
 				elif left_protocol == 'xmpp':
-					if isinstance(p.muc, xmpp.muc) and not p.muc.connected:
+					if isinstance(p.muc, xmpp.muc) and p.muc.state <= p.muc.LEAVING:
 						return
 					# got disconnected somehow
 					if isinstance(p.xmpp_c, xmpp.client.Client):
--- a/muc.py
+++ b/muc.py
@@ -32,12 +32,15 @@ class muc:
 	class RemoteServerNotFound(Exception): pass
 	class NotConnected(Exception): pass
 	
+	LEFT, LEAVING, NOT_IN, JOINING, JOINED = range(5)
+	
 	def __init__(self, room_jid):
 		self.room_jid = room_jid
-		self.connected = False
+		self.state = self.NOT_IN
 	
 	
 	def _join(self, callback=None):
+		self.state = self.JOINING
 		self.callback = callback
 		self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler)
 		s = xmpp.protocol.Presence(to=self.jid, status=self.status, payload=[xmpp.simplexml.Node(tag='x', attrs={'xmlns': 'http://jabber.org/protocol/muc'}, payload=[xmpp.simplexml.Node(tag='history', attrs={'maxchars': '0'})])])
@@ -49,7 +52,7 @@ class muc:
 		
 		The "force" optional argument bypasses the fact that we are not in the room yet, necessary to send initial presence"""
 		
-		if not self.connected and not force:
+		if self.state != self.JOINED and not force:
 			raise self.NotConnected, self.jid+'\n'+stanza.__str__(fancy=1).encode('utf-8')
 		try:
 			self.xmpp_c.send(stanza)
@@ -113,7 +116,7 @@ class muc:
 				if len(errors) == 0:
 					errors.append(self.__class__.UnknownError(presence.__str__(fancy=1).encode('utf-8')))
 			else:
-				self.connected = True
+				self.state = self.JOINED
 				xmpp_c.UnregisterHandler('presence', self._xmpp_presence_handler)
 			if self.callback != None:
 				self.callback(errors)
@@ -154,24 +157,23 @@ class muc:
 		"""Leave the room"""
 		self.xmpp_c.lock.acquire()
 		self.auto_reconnect = False
+		self.state = self.LEAVING
 		s = xmpp.protocol.Presence(to=self.jid, typ='unavailable', status=message)
 		try:
-			self._send(s)
+			self._send(s, force=True)
 		except self.NotConnected:
 			pass
-		self.connected = False
 		self.xmpp_c.lock.release()
 	
 	
 	def rejoin(self, callback=None):
 		"""Rejoin room"""
-		self.connected = False
+		self.state = self.JOINING
 		self._join(callback=callback)
 	
 	
 	def __del__(self):
-		if self.connected:
-			self.leave()
+		self.leave()
 		if self in self.xmpp_c.mucs:
 			self.xmpp_c.mucs.remove(self)
 
--- a/participant.py
+++ b/participant.py
@@ -321,14 +321,14 @@ class Participant:
 	
 	
 	def say_on_xmpp(self, message, action=False):
-		if isinstance(self.muc, xmpp.muc) and self.muc.connected:
+		if isinstance(self.muc, xmpp.muc) and self.muc.state == self.muc.JOINED:
 			self.muc.say(message, action=action)
 		elif not isinstance(self.irc_connection, ServerConnection):
 			self.bridge.say_on_behalf(self.nickname, message, 'xmpp', action=action)
 	
 	
 	def say_on_xmpp_to(self, to, message, action=False):
-		if isinstance(self.muc, xmpp.muc) and self.muc.connected:
+		if isinstance(self.muc, xmpp.muc) and self.muc.state == self.muc.JOINED:
 			self.muc.say_to(to, message, action=action)
 		elif not isinstance(self.irc_connection, ServerConnection):
 			if self.bridge.mode not in ['normal', 'bypass']:
@@ -367,7 +367,7 @@ class Participant:
 		if isinstance(self.irc_connection, ServerConnection):
 			r += '\nself.irc_connection='+str(self.irc_connection)+'\n'+'self.irc_connection.logged_in='+str(self.irc_connection.logged_in)
 		if isinstance(self.muc, xmpp.muc):
-			r += '\nself.muc.connected='+str(self.muc.connected)
+			r += '\nself.muc.state='+str(self.muc.state)
 		return r