comparison src/plugins/plugin_xep_0045.py @ 1986:fbd313cfd40b

plugin xep-0045: ask for password when getting a "not-allowed" error
author Goffi <goffi@goffi.org>
date Fri, 01 Jul 2016 00:00:09 +0200
parents de4fac507dc4
children 757c512fe06c
comparison
equal deleted inserted replaced
1985:9ad1a06ed0f8 1986:fbd313cfd40b
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23 log = getLogger(__name__) 23 log = getLogger(__name__)
24 from twisted.internet import defer 24 from twisted.internet import defer
25 from twisted.words.protocols.jabber import jid 25 from twisted.words.protocols.jabber import jid
26 from twisted.python import failure
26 from dateutil.tz import tzutc 27 from dateutil.tz import tzutc
27 28
28 from sat.core import exceptions 29 from sat.core import exceptions
29 from sat.memory import memory 30 from sat.memory import memory
30 31
38 39
39 from zope.interface import implements 40 from zope.interface import implements
40 41
41 42
42 PLUGIN_INFO = { 43 PLUGIN_INFO = {
43 "name": "XEP 0045 Plugin", 44 "name": "XEP-0045 Plugin",
44 "import_name": "XEP-0045", 45 "import_name": "XEP-0045",
45 "type": "XEP", 46 "type": "XEP",
46 "protocols": ["XEP-0045"], 47 "protocols": ["XEP-0045"],
47 "dependencies": [], 48 "dependencies": [],
48 "recommendations": [C.TEXT_CMDS], 49 "recommendations": [C.TEXT_CMDS],
134 except exceptions.NotFound: 135 except exceptions.NotFound:
135 return False 136 return False
136 else: 137 else:
137 return True 138 return True
138 139
139 def _joinCb(self, room, client): 140 def _passwordUICb(self, data, client, room_jid, nick):
141 """Called when the user has given room password (or cancelled)"""
142 if C.bool(data.get(C.XMLUI_DATA_CANCELLED, "false")):
143 log.info(u"room join for {} is cancelled".format(room_jid.userhost()))
144 raise failure.Failure(exceptions.CancelError(D_(u"Room joining cancelled by user")))
145 password = data[xml_tools.formEscape('password')]
146 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, (client, room_jid, nick), errbackArgs=(client, room_jid, nick, password))
147
148 def _joinCb(self, room, client, room_jid, nick):
140 """Called when the user is in the requested room""" 149 """Called when the user is in the requested room"""
141 if room.locked: 150 if room.locked:
142 # FIXME: the current behaviour is to create an instant room 151 # FIXME: the current behaviour is to create an instant room
143 # and send the signal only when the room is unlocked 152 # and send the signal only when the room is unlocked
144 # a proper configuration management should be done 153 # a proper configuration management should be done
155 msg_suffix = '' 164 msg_suffix = ''
156 else: 165 else:
157 if condition == 'conflict': 166 if condition == 'conflict':
158 # we have a nickname conflict, we try again with "_" suffixed to current nickname 167 # we have a nickname conflict, we try again with "_" suffixed to current nickname
159 nick += '_' 168 nick += '_'
160 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, callbackKeywords={client: client}, errbackArgs=[client, room_jid, nick, password]) 169 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, (client, room_jid, nick), errbackArgs=(client, room_jid, nick, password))
170 elif condition == 'not-allowed':
171 # room is restricted, we need a password
172 password_ui = xml_tools.XMLUI("form", title=D_(u'Room {} is restricted').format(room_jid.userhost()), submit_id='')
173 password_ui.addText(D_("This room is restricted, please enter the password"))
174 password_ui.addPassword('password')
175 d = xml_tools.deferXMLUI(self.host, password_ui, profile=client.profile)
176 d.addCallback(self._passwordUICb, client, room_jid, nick)
177 return d
178
161 msg_suffix = ' with condition "{}"'.format(failure.value.condition) 179 msg_suffix = ' with condition "{}"'.format(failure.value.condition)
162 180
163 mess = D_(u"Error while joining the room {room}{suffix}".format( 181 mess = D_(u"Error while joining the room {room}{suffix}".format(
164 room = room_jid.userhost(), suffix = msg_suffix)) 182 room = room_jid.userhost(), suffix = msg_suffix))
165 log.error(mess) 183 log.error(mess)
385 return defer.fail(exceptions.ConflictError(_(u"The room has already been joined"))) 403 return defer.fail(exceptions.ConflictError(_(u"The room has already been joined")))
386 log.info(_(u"[{profile}] is joining room {room} with nick {nick}").format(profile=client.profile, room=room_jid.userhost(), nick=nick)) 404 log.info(_(u"[{profile}] is joining room {room} with nick {nick}").format(profile=client.profile, room=room_jid.userhost(), nick=nick))
387 405
388 password = options["password"] if "password" in options else None 406 password = options["password"] if "password" in options else None
389 407
390 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, callbackKeywords={'client': client}, errbackArgs=[client, room_jid, nick, password]) 408 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, (client, room_jid, nick), errbackArgs=(client, room_jid, nick, password))
391 409
392 def _nick(self, room_jid_s, nick, profile_key=C.PROF_KEY_NONE): 410 def _nick(self, room_jid_s, nick, profile_key=C.PROF_KEY_NONE):
393 client = self.host.getClient(profile_key) 411 client = self.host.getClient(profile_key)
394 return self.nick(client, jid.JID(room_jid_s), nick) 412 return self.nick(client, jid.JID(room_jid_s), nick)
395 413