Mercurial > libervia-backend
diff src/plugins/plugin_xep_0077.py @ 807:be4c5e24dab9
plugin XEP-0077, plugin XEP-0100, frontends: gateways have been entirely implemented in backend using the new refactored XMLUI and AdvancedListContainer. The now useless code has been removed from frontends.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 04 Feb 2014 18:26:03 +0100 |
parents | bfabeedbf32e |
children | 1fe00f0c9a91 |
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0077.py Tue Feb 04 18:24:27 2014 +0100 +++ b/src/plugins/plugin_xep_0077.py Tue Feb 04 18:26:03 2014 +0100 @@ -18,12 +18,14 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from sat.core.i18n import _ -from logging import debug, info, error +from sat.core import exceptions +from logging import debug, info, warning, error from twisted.words.protocols.jabber import jid from twisted.words.protocols.jabber.xmlstream import IQ -from sat.tools.xml_tools import dataForm2XMLUI +from sat.tools import xml_tools +from sat.memory import memory -from wokkel import data_form +from wokkel import data_form, compat NS_REG = 'jabber:iq:register' @@ -44,97 +46,70 @@ info(_("Plugin XEP_0077 initialization")) self.host = host self.triggers = {} # used by other protocol (e.g. XEP-0100) to finish registration. key = target_jid - host.bridge.addMethod("in_band_register", ".plugin", in_sign='ss', out_sign='s', method=self.in_band_register) - host.bridge.addMethod("in_band_submit", ".plugin", in_sign='ssa(ss)s', out_sign='s', method=self.in_band_submit) + host.bridge.addMethod("inBandRegister", ".plugin", in_sign='ss', out_sign='s', + method=self._inBandRegister, + async=True) - def addTrigger(self, target, cb, profile): - """Add a callback which is called when registration to target is successful""" - self.triggers[target] = (cb, profile) - - def reg_ok(self, answer, profile): + def _regOk(self, answer, client, post_treat_cb): """Called after the first get IQ""" try: - x_elem = filter(lambda x: x.name == "x", answer.firstChildElement().elements())[0] # We only want the "x" element (data form) - except IndexError: - info(_("No data form found")) - #TODO: manage registration without data form - answer_data = {"reason": "unmanaged", "message": _("This gateway can't be managed by SàT, sorry :(")} - answer_type = "ERROR" - self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) - return + query_elt = answer.elements(NS_REG, 'query').next() + except StopIteration: + raise exceptions.DataError("Can't find expected query element") + + try: + x_elem = query_elt.elements(data_form.NS_X_DATA, 'x').next() + except StopIteration: + # XXX: it seems we have an old service which doesn't manage data forms + warning(_("Can't find data form")) + raise DataError(_("This gateway can't be managed by SàT, sorry :(")) + + def submitForm(data, profile): + form_elt = xml_tools.XMLUIResultToElt(data) + + iq_elt = compat.IQ(client.xmlstream, 'set') + iq_elt['id'] = answer['id'] + iq_elt['to'] = answer['from'] + query_elt = iq_elt.addElement("query", NS_REG) + query_elt.addChild(form_elt) + d = iq_elt.send() + d.addCallback(self._regSuccess, client, post_treat_cb) + d.addErrback(self._regFailure, client) + return d form = data_form.Form.fromElement(x_elem) - xml_data = dataForm2XMLUI(form, "").toXml() - self.host.bridge.actionResult("XMLUI", answer['id'], {"target": answer["from"], "type": "registration", "xml": xml_data}, profile) + submit_reg_id = self.host.registerCallback(submitForm, with_data=True, one_shot=True) + return xml_tools.dataForm2XMLUI(form, submit_reg_id) - def reg_err(self, failure, profile): + def _regErr(self, failure, client): """Called when something is wrong with registration""" info(_("Registration failure: %s") % str(failure.value)) - answer_data = {} - answer_data['reason'] = 'unknown' - answer_data = {"message": "%s [code: %s]" % (failure.value.condition, unicode(failure.value))} - answer_type = "ERROR" - self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile) + raise failure - def unregistrationAnswer(self, answer, profile): - debug(_("registration answer: %s") % answer.toXml()) - answer_type = "SUCCESS" - answer_data = {"message": _("Your are now unregistred")} - self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) - - def unregistrationFailure(self, failure, profile): - info(_("Unregistration failure: %s") % str(failure.value)) - answer_type = "ERROR" - answer_data = {} - answer_data['reason'] = 'unknown' - answer_data = {"message": _("Unregistration failed: %s") % failure.value.condition} - self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile) - - def registrationAnswer(self, answer, profile): + def _regSuccess(self, answer, client, post_treat_cb): debug(_("registration answer: %s") % answer.toXml()) - answer_type = "SUCCESS" - answer_data = {"message": _("Registration successfull")} - self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) - if answer["from"] in self.triggers: - callback, profile = self.triggers[answer["from"]] - callback(answer["from"], profile) - del self.triggers[answer["from"]] + if post_treat_cb is not None: + post_treat_cb(jid.JID(answer['from']), client.profile) + return {} - def registrationFailure(self, failure, profile): + def _regFailure(self, failure, client): info(_("Registration failure: %s") % str(failure.value)) - print failure.value.stanza.toXml() - answer_type = "ERROR" - answer_data = {} if failure.value.condition == 'conflict': - answer_data['reason'] = 'conflict' - answer_data = {"message": _("Username already exists, please choose an other one")} - else: - answer_data['reason'] = 'unknown' - answer_data = {"message": _("Registration failed")} - self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile) - if failure.value.stanza["from"] in self.triggers: - del self.triggers[failure.value.stanza["from"]] + raise exceptions.ConflictError( _("Username already exists, please choose an other one")) + raise failure - def in_band_submit(self, action, target, fields, profile): - """Submit a form for registration, using data_form""" - id, deferred = self.host.submitForm(action, target, fields, profile) - if action == 'CANCEL': - deferred.addCallbacks(self.unregistrationAnswer, self.unregistrationFailure, callbackArgs=[profile], errbackArgs=[profile]) - else: - deferred.addCallbacks(self.registrationAnswer, self.registrationFailure, callbackArgs=[profile], errbackArgs=[profile]) - return id + def _inBandRegister(self, to_jid_s, profile_key='@NONE@'): + return self.inBandRegister, jid.JID(to_jid_s, profile_key) - def in_band_register(self, target, profile_key='@DEFAULT@'): + def inBandRegister(self, to_jid, post_treat_cb=None, profile_key='@NONE@'): """register to a target JID""" client = self.host.getClient(profile_key) if not client: - error(_('Asking for an non-existant or not connected profile')) - return "" - to_jid = jid.JID(target) + raise exceptions.ProfileUnknownError debug(_("Asking registration for [%s]") % to_jid.full()) reg_request = IQ(client.xmlstream, 'get') reg_request["from"] = client.jid.full() reg_request["to"] = to_jid.full() reg_request.addElement('query', NS_REG) - reg_request.send(to_jid.full()).addCallbacks(self.reg_ok, self.reg_err, callbackArgs=[client.profile], errbackArgs=[client.profile]) - return reg_request["id"] + d = reg_request.send(to_jid.full()).addCallbacks(self._regOk, self._regErr, callbackArgs=[client, post_treat_cb], errbackArgs=[client]) + return d