Mercurial > xib
comparison muc.py @ 0:4c842d23d4ce
Initial commit, version 0.1
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Sun, 16 Aug 2009 01:47:03 +0200 |
parents | |
children | cb0daec4b778 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c842d23d4ce |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # This program is free software: you can redistribute it and/or modify | |
5 # it under the terms of the GNU General Public License as published by | |
6 # the Free Software Foundation, either version 3 of the License, or | |
7 # (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
14 # You should have received a copy of the GNU General Public License | |
15 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | |
17 | |
18 import xmppony as xmpp | |
19 from time import sleep | |
20 | |
21 | |
22 class muc: | |
23 | |
24 class PasswordNeeded(Exception): pass | |
25 class MembersOnlyRoom(Exception): pass | |
26 class BannedFromRoom(Exception): pass | |
27 class NicknameConflict(Exception): pass | |
28 class RoomIsFull(Exception): pass | |
29 class RoomIsLocked(Exception): pass | |
30 class ForgotNickname(Exception): pass | |
31 class UnknownError(Exception): pass | |
32 | |
33 def __init__(self, room_jid): | |
34 self.room_jid = room_jid | |
35 self.connected = False | |
36 self.participants = {} | |
37 | |
38 | |
39 def join(self, xmpp_c, nickname, status=None, callback=None): | |
40 """Join room on xmpp_c connection using nickname""" | |
41 self.jid = self.room_jid+'/'+nickname | |
42 self.nickname = nickname | |
43 self.xmpp_c = xmpp_c | |
44 self.callback = callback | |
45 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) | |
46 self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, status=status)) | |
47 | |
48 | |
49 def _xmpp_presence_handler(self, xmpp_c, presence): | |
50 if presence.getFrom() == self.jid: | |
51 errors = [] | |
52 if presence.getAttr('type') == 'error': | |
53 for c in presence.getChildren(): | |
54 if c.getName() == 'error': | |
55 for cc in c.getChildren(): | |
56 if cc.getNamespace() == 'urn:ietf:params:xml:ns:xmpp-stanzas' and cc.getName() != 'text': | |
57 err = c.getAttr('type')+' '+cc.getName() | |
58 if err == 'auth not-authorized': | |
59 # password-protected room | |
60 errors.append(self.__class__.PasswordNeeded()) | |
61 elif err == 'auth registration-required': | |
62 # members-only room | |
63 errors.append(self.__class__.MembersOnlyRoom()) | |
64 elif err == 'auth forbidden': | |
65 # banned from room | |
66 errors.append(self.__class__.BannedFromRoom()) | |
67 elif err == 'cancel conflict': | |
68 # nickname conflict | |
69 errors.append(self.__class__.NicknameConflict()) | |
70 elif err == 'wait service-unavailable': | |
71 # room is full | |
72 errors.append(self.__class__.RoomIsFull()) | |
73 elif err == 'cancel item-not-found': | |
74 # room is locked | |
75 errors.append(self.__class__.RoomIsLocked()) | |
76 elif err == 'modify jid-malformed': | |
77 # forgot to give a nickname | |
78 errors.append(self.__class__.ForgotNickname()) | |
79 else: | |
80 errors.append(self.__class__.UnknownError(presence.__str__(fancy=1).decode('utf-8'))) | |
81 break | |
82 if len(errors) == 0: | |
83 errors.append(self.__class__.UnknownError(presence.__str__(fancy=1).decode('utf-8'))) | |
84 else: | |
85 self.connected = True | |
86 xmpp_c.UnregisterHandler('presence', self._xmpp_presence_handler) | |
87 if self.callback != None: | |
88 self.callback(errors) | |
89 | |
90 | |
91 def _check(self): | |
92 i = 0 | |
93 while not self.connected: | |
94 i += 1 | |
95 if i > 30: | |
96 raise Exception('Error: connection to room timed out') | |
97 sleep(1) | |
98 | |
99 | |
100 def say(self, message): | |
101 """Say message in the room""" | |
102 self._check() | |
103 self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid, typ='groupchat', body=message)) | |
104 | |
105 | |
106 def sayTo(self, to, message): | |
107 """Send a private message""" | |
108 self._check() | |
109 self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid+'/'+to, typ='chat', body=message)) | |
110 | |
111 | |
112 def change_nick(self, nickname, callback=None): | |
113 """Change nickname""" | |
114 self._check() | |
115 self.jid = self.room_jid+'/'+nickname | |
116 self.callback = callback | |
117 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) | |
118 self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid)) | |
119 | |
120 | |
121 def leave(self, message=''): | |
122 """Leave the room""" | |
123 self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, typ='unavailable', status=message)) | |
124 self.connected = False | |
125 | |
126 | |
127 def __del__(self): | |
128 if self.connected: | |
129 self.leave() | |
130 | |
131 xmpp.muc = muc |