Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0077.py @ 993:301b342c697a
core: use of the new core.log module:
/!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 19 Apr 2014 19:19:19 +0200 |
parents | c6d8fc63b1db |
children | 069ad98b360d |
comparison
equal
deleted
inserted
replaced
992:f51a1895275c | 993:301b342c697a |
---|---|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from sat.core.constants import Const as C | 21 from sat.core.constants import Const as C |
22 from sat.core import exceptions | 22 from sat.core import exceptions |
23 from logging import debug, info, warning, error | 23 from sat.core.log import getLogger |
24 log = getLogger(__name__) | |
24 from twisted.words.protocols.jabber import jid | 25 from twisted.words.protocols.jabber import jid |
25 from twisted.words.protocols.jabber.xmlstream import IQ | 26 from twisted.words.protocols.jabber.xmlstream import IQ |
26 from sat.tools import xml_tools | 27 from sat.tools import xml_tools |
27 | 28 |
28 from wokkel import data_form, compat | 29 from wokkel import data_form, compat |
41 | 42 |
42 | 43 |
43 class XEP_0077(object): | 44 class XEP_0077(object): |
44 | 45 |
45 def __init__(self, host): | 46 def __init__(self, host): |
46 info(_("Plugin XEP_0077 initialization")) | 47 log.info(_("Plugin XEP_0077 initialization")) |
47 self.host = host | 48 self.host = host |
48 self.triggers = {} # used by other protocol (e.g. XEP-0100) to finish registration. key = target_jid | 49 self.triggers = {} # used by other protocol (e.g. XEP-0100) to finish registration. key = target_jid |
49 host.bridge.addMethod("inBandRegister", ".plugin", in_sign='ss', out_sign='s', | 50 host.bridge.addMethod("inBandRegister", ".plugin", in_sign='ss', out_sign='s', |
50 method=self._inBandRegister, | 51 method=self._inBandRegister, |
51 async=True) | 52 async=True) |
59 | 60 |
60 try: | 61 try: |
61 x_elem = query_elt.elements(data_form.NS_X_DATA, 'x').next() | 62 x_elem = query_elt.elements(data_form.NS_X_DATA, 'x').next() |
62 except StopIteration: | 63 except StopIteration: |
63 # XXX: it seems we have an old service which doesn't manage data forms | 64 # XXX: it seems we have an old service which doesn't manage data forms |
64 warning(_("Can't find data form")) | 65 log.warning(_("Can't find data form")) |
65 raise exceptions.DataError(_("This gateway can't be managed by SàT, sorry :(")) | 66 raise exceptions.DataError(_("This gateway can't be managed by SàT, sorry :(")) |
66 | 67 |
67 def submitForm(data, profile): | 68 def submitForm(data, profile): |
68 form_elt = xml_tools.XMLUIResultToElt(data) | 69 form_elt = xml_tools.XMLUIResultToElt(data) |
69 | 70 |
81 submit_reg_id = self.host.registerCallback(submitForm, with_data=True, one_shot=True) | 82 submit_reg_id = self.host.registerCallback(submitForm, with_data=True, one_shot=True) |
82 return xml_tools.dataForm2XMLUI(form, submit_reg_id) | 83 return xml_tools.dataForm2XMLUI(form, submit_reg_id) |
83 | 84 |
84 def _regErr(self, failure, client): | 85 def _regErr(self, failure, client): |
85 """Called when something is wrong with registration""" | 86 """Called when something is wrong with registration""" |
86 info(_("Registration failure: %s") % str(failure.value)) | 87 log.info(_("Registration failure: %s") % str(failure.value)) |
87 raise failure | 88 raise failure |
88 | 89 |
89 def _regSuccess(self, answer, client, post_treat_cb): | 90 def _regSuccess(self, answer, client, post_treat_cb): |
90 debug(_("registration answer: %s") % answer.toXml()) | 91 log.debug(_("registration answer: %s") % answer.toXml()) |
91 if post_treat_cb is not None: | 92 if post_treat_cb is not None: |
92 post_treat_cb(jid.JID(answer['from']), client.profile) | 93 post_treat_cb(jid.JID(answer['from']), client.profile) |
93 return {} | 94 return {} |
94 | 95 |
95 def _regFailure(self, failure, client): | 96 def _regFailure(self, failure, client): |
96 info(_("Registration failure: %s") % str(failure.value)) | 97 log.info(_("Registration failure: %s") % str(failure.value)) |
97 if failure.value.condition == 'conflict': | 98 if failure.value.condition == 'conflict': |
98 raise exceptions.ConflictError( _("Username already exists, please choose an other one")) | 99 raise exceptions.ConflictError( _("Username already exists, please choose an other one")) |
99 raise failure | 100 raise failure |
100 | 101 |
101 def _inBandRegister(self, to_jid_s, profile_key=C.PROF_KEY_NONE): | 102 def _inBandRegister(self, to_jid_s, profile_key=C.PROF_KEY_NONE): |
102 return self.inBandRegister, jid.JID(to_jid_s, profile_key) | 103 return self.inBandRegister, jid.JID(to_jid_s, profile_key) |
103 | 104 |
104 def inBandRegister(self, to_jid, post_treat_cb=None, profile_key=C.PROF_KEY_NONE): | 105 def inBandRegister(self, to_jid, post_treat_cb=None, profile_key=C.PROF_KEY_NONE): |
105 """register to a target JID""" | 106 """register to a target JID""" |
106 client = self.host.getClient(profile_key) | 107 client = self.host.getClient(profile_key) |
107 debug(_("Asking registration for [%s]") % to_jid.full()) | 108 log.debug(_("Asking registration for [%s]") % to_jid.full()) |
108 reg_request = IQ(client.xmlstream, 'get') | 109 reg_request = IQ(client.xmlstream, 'get') |
109 reg_request["from"] = client.jid.full() | 110 reg_request["from"] = client.jid.full() |
110 reg_request["to"] = to_jid.full() | 111 reg_request["to"] = to_jid.full() |
111 reg_request.addElement('query', NS_REG) | 112 reg_request.addElement('query', NS_REG) |
112 d = reg_request.send(to_jid.full()).addCallbacks(self._regOk, self._regErr, callbackArgs=[client, post_treat_cb], errbackArgs=[client]) | 113 d = reg_request.send(to_jid.full()).addCallbacks(self._regOk, self._regErr, callbackArgs=[client, post_treat_cb], errbackArgs=[client]) |