comparison muc.py @ 214:0c99d38b84b8

(muc) added "auto_reconnect" attribute (defaults to True, turns to False when "leave" is called), created NotConnected exception Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sat, 06 Mar 2010 19:26:35 +0100
parents fbe40b397f67
children 45065f6f674c
comparison
equal deleted inserted replaced
213:fbe40b397f67 214:0c99d38b84b8
28 class RoomIsFull(Exception): pass 28 class RoomIsFull(Exception): pass
29 class RoomIsLocked(Exception): pass 29 class RoomIsLocked(Exception): pass
30 class ForgotNickname(Exception): pass 30 class ForgotNickname(Exception): pass
31 class UnknownError(Exception): pass 31 class UnknownError(Exception): pass
32 class RemoteServerNotFound(Exception): pass 32 class RemoteServerNotFound(Exception): pass
33 class NotConnected(Exception): pass
33 34
34 def __init__(self, room_jid): 35 def __init__(self, room_jid, auto_reconnect=True):
35 self.room_jid = room_jid 36 self.room_jid = room_jid
36 self.connected = False 37 self.connected = False
38 self.auto_reconnect = auto_reconnect
37 self.participants = {} 39 self.participants = {}
38 40
39 41
40 def _join(self, callback=None): 42 def _join(self, callback=None):
41 self.callback = callback 43 self.callback = callback
42 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) 44 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler)
43 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'})])]) 45 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'})])])
44 self._send(s) 46 self._send(s, force=True)
45 47
46 48
47 def _send(self, stanza): 49 def _send(self, stanza, force=False):
50 """Send a stanza.
51
52 The "force" optional argument bypasses the fact that we are not in the room yet, necessary to send initial presence"""
53
54 if not self.connected and not force:
55 raise self.NotConnected, stanza.__str__(fancy=1).encode('utf-8')
48 try: 56 try:
49 self.xmpp_c.send(stanza) 57 self.xmpp_c.send(stanza)
50 except IOError, xmpp.Conflict: 58 except IOError, xmpp.Conflict:
59 if not self.auto_reconnect:
60 raise self.NotConnected, stanza.__str__(fancy=1).encode('utf-8')
61
51 self.xmpp_c.reconnectAndReauth() 62 self.xmpp_c.reconnectAndReauth()
52 for m in self.xmpp_c.mucs: 63 for m in self.xmpp_c.mucs:
53 m.rejoin() 64 m.rejoin()
54 self.xmpp_c.send(stanza) 65 self.xmpp_c.send(stanza)
55 66
139 150
140 151
141 def leave(self, message=''): 152 def leave(self, message=''):
142 """Leave the room""" 153 """Leave the room"""
143 self.xmpp_c.lock.acquire() 154 self.xmpp_c.lock.acquire()
155 self.auto_reconnect = False
144 s = xmpp.protocol.Presence(to=self.jid, typ='unavailable', status=message) 156 s = xmpp.protocol.Presence(to=self.jid, typ='unavailable', status=message)
145 self._send(s) 157 self._send(s)
146 self.connected = False 158 self.connected = False
147 self.xmpp_c.lock.release() 159 self.xmpp_c.lock.release()
148 160