Mercurial > xib
comparison muc.py @ 90:965dd6f5fcc3
Tried to improve XMPP disconnections handling
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Sun, 20 Sep 2009 11:56:03 +0200 |
parents | 22cfc31f9c90 |
children | 59401ac0f47a |
comparison
equal
deleted
inserted
replaced
89:94fc538cdf65 | 90:965dd6f5fcc3 |
---|---|
34 self.room_jid = room_jid | 34 self.room_jid = room_jid |
35 self.connected = False | 35 self.connected = False |
36 self.participants = {} | 36 self.participants = {} |
37 | 37 |
38 | 38 |
39 def _join(self, callback=None): | |
40 self.callback = callback | |
41 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) | |
42 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'})])]) | |
43 try: | |
44 self.xmpp_c.send(s) | |
45 except IOError, xmpp.Conflict: | |
46 self.xmpp_c.reconnectAndReauth() | |
47 for m in self.xmpp_c.mucs: | |
48 m.rejoin() | |
49 self.xmpp_c.send(s) | |
50 | |
51 | |
39 def join(self, xmpp_c, nickname, status=None, callback=None): | 52 def join(self, xmpp_c, nickname, status=None, callback=None): |
40 """Join room on xmpp_c connection using nickname""" | 53 """Join room on xmpp_c connection using nickname""" |
41 self.jid = self.room_jid+'/'+nickname | 54 self.jid = self.room_jid+'/'+nickname |
42 self.nickname = nickname | 55 self.nickname = nickname |
56 self.status = status | |
43 self.xmpp_c = xmpp_c | 57 self.xmpp_c = xmpp_c |
44 self.callback = callback | 58 self.xmpp_c.mucs.append(self) |
45 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) | 59 self._join(callback=callback) |
46 self.xmpp_c.lock.acquire() | |
47 try: | |
48 self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, status=status, payload=[xmpp.simplexml.Node(tag='x', attrs={'xmlns': 'http://jabber.org/protocol/muc'}, payload=[xmpp.simplexml.Node(tag='history', attrs={'maxchars': '0'})])])) | |
49 except IOError: | |
50 print 'IOError, reconnecting ...' | |
51 self.xmpp_c.reconnectAndReauth() | |
52 self.join(xmpp_c, nickname, status=status, callback=callback) | |
53 self.xmpp_c.lock.release() | |
54 | 60 |
55 | 61 |
56 def _xmpp_presence_handler(self, xmpp_c, presence): | 62 def _xmpp_presence_handler(self, xmpp_c, presence): |
57 if presence.getFrom() == self.jid: | 63 if presence.getFrom() == self.jid: |
58 errors = [] | 64 errors = [] |
96 | 102 |
97 | 103 |
98 def say(self, message): | 104 def say(self, message): |
99 """Say message in the room""" | 105 """Say message in the room""" |
100 self.xmpp_c.lock.acquire() | 106 self.xmpp_c.lock.acquire() |
107 s = xmpp.protocol.Message(to=self.room_jid, typ='groupchat', body=message) | |
101 try: | 108 try: |
102 self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid, typ='groupchat', body=message)) | 109 self.xmpp_c.send(s) |
103 except IOError: | 110 except IOError, xmpp.Conflict: |
104 print 'IOError, reconnecting ...' | |
105 self.xmpp_c.reconnectAndReauth() | 111 self.xmpp_c.reconnectAndReauth() |
106 self.say(message) | 112 for m in self.xmpp_c.mucs: |
113 m.rejoin() | |
114 self.xmpp_c.send(s) | |
107 self.xmpp_c.lock.release() | 115 self.xmpp_c.lock.release() |
108 | 116 |
109 | 117 |
110 def sayTo(self, to, message): | 118 def sayTo(self, to, message): |
111 """Send a private message""" | 119 """Send a private message""" |
112 self.xmpp_c.lock.acquire() | 120 self.xmpp_c.lock.acquire() |
121 s = xmpp.protocol.Message(to=self.room_jid+'/'+to, typ='chat', body=message) | |
113 try: | 122 try: |
114 self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid+'/'+to, typ='chat', body=message)) | 123 self.xmpp_c.send(s) |
115 except IOError: | 124 except IOError, xmpp.Conflict: |
116 print 'IOError, reconnecting ...' | |
117 self.xmpp_c.reconnectAndReauth() | 125 self.xmpp_c.reconnectAndReauth() |
118 self.sayTo(to, message) | 126 for m in self.xmpp_c.mucs: |
127 m.rejoin() | |
128 self.xmpp_c.send(s) | |
119 self.xmpp_c.lock.release() | 129 self.xmpp_c.lock.release() |
120 | 130 |
121 | 131 |
122 def change_nick(self, nickname, status=None, callback=None): | 132 def change_nick(self, nickname, status=None, callback=None): |
123 """Change nickname""" | 133 """Change nickname""" |
124 self.jid = self.room_jid+'/'+nickname | 134 self.jid = self.room_jid+'/'+nickname |
125 self.callback = callback | 135 self.callback = callback |
126 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) | 136 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) |
127 self.xmpp_c.lock.acquire() | 137 self.xmpp_c.lock.acquire() |
138 s = xmpp.protocol.Presence(to=self.jid, status=status) | |
128 try: | 139 try: |
129 self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, status=status)) | 140 self.xmpp_c.send(s) |
130 except IOError: | 141 except IOError, xmpp.Conflict: |
131 print 'IOError, reconnecting ...' | |
132 self.xmpp_c.reconnectAndReauth() | 142 self.xmpp_c.reconnectAndReauth() |
133 self.change_nick(nickname, status=status, callback=callback) | 143 for m in self.xmpp_c.mucs: |
144 m.rejoin() | |
145 self.xmpp_c.send(s) | |
134 self.xmpp_c.lock.release() | 146 self.xmpp_c.lock.release() |
135 | 147 |
136 | 148 |
137 def leave(self, message=''): | 149 def leave(self, message=''): |
138 """Leave the room""" | 150 """Leave the room""" |
139 self.xmpp_c.lock.acquire() | 151 self.xmpp_c.lock.acquire() |
152 s = xmpp.protocol.Presence(to=self.jid, typ='unavailable', status=message) | |
140 try: | 153 try: |
141 self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, typ='unavailable', status=message)) | 154 self.xmpp_c.send(s) |
142 except IOError: | 155 except IOError, xmpp.Conflict: |
143 print 'IOError, reconnecting ...' | |
144 self.xmpp_c.reconnectAndReauth() | 156 self.xmpp_c.reconnectAndReauth() |
145 self.leave(message=message) | 157 for m in self.xmpp_c.mucs: |
158 m.rejoin() | |
159 self.xmpp_c.send(s) | |
146 self.connected = False | 160 self.connected = False |
147 self.xmpp_c.lock.release() | 161 self.xmpp_c.lock.release() |
162 | |
163 | |
164 def rejoin(self, callback=None): | |
165 """Rejoin room""" | |
166 self.connected = False | |
167 self._join(callback=callback) | |
148 | 168 |
149 | 169 |
150 def __del__(self): | 170 def __del__(self): |
151 if self.connected: | 171 if self.connected: |
152 self.leave() | 172 self.leave() |
173 if self in self.xmpp_c.mucs: | |
174 self.xmpp_c.mucs.remove(self) | |
153 | 175 |
154 xmpp.muc = muc | 176 xmpp.muc = muc |