comparison sat/plugins/plugin_xep_0077.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 6c89cf856d28
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
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 sat.core.log import getLogger 23 from sat.core.log import getLogger
24
24 log = getLogger(__name__) 25 log = getLogger(__name__)
25 from twisted.words.protocols.jabber import jid 26 from twisted.words.protocols.jabber import jid
26 from twisted.words.protocols.jabber import xmlstream 27 from twisted.words.protocols.jabber import xmlstream
27 from twisted.internet import defer, reactor 28 from twisted.internet import defer, reactor
28 from sat.tools import xml_tools 29 from sat.tools import xml_tools
29 30
30 from wokkel import data_form 31 from wokkel import data_form
31 32
32 NS_REG = 'jabber:iq:register' 33 NS_REG = "jabber:iq:register"
33 34
34 PLUGIN_INFO = { 35 PLUGIN_INFO = {
35 C.PI_NAME: "XEP 0077 Plugin", 36 C.PI_NAME: "XEP 0077 Plugin",
36 C.PI_IMPORT_NAME: "XEP-0077", 37 C.PI_IMPORT_NAME: "XEP-0077",
37 C.PI_TYPE: "XEP", 38 C.PI_TYPE: "XEP",
38 C.PI_PROTOCOLS: ["XEP-0077"], 39 C.PI_PROTOCOLS: ["XEP-0077"],
39 C.PI_DEPENDENCIES: [], 40 C.PI_DEPENDENCIES: [],
40 C.PI_MAIN: "XEP_0077", 41 C.PI_MAIN: "XEP_0077",
41 C.PI_DESCRIPTION: _("""Implementation of in-band registration""") 42 C.PI_DESCRIPTION: _("""Implementation of in-band registration"""),
42 } 43 }
43 44
44 # FIXME: this implementation is incomplete 45 # FIXME: this implementation is incomplete
46
45 47
46 class RegisteringAuthenticator(xmlstream.ConnectAuthenticator): 48 class RegisteringAuthenticator(xmlstream.ConnectAuthenticator):
47 # FIXME: request IQ is not send to check available fields, while XEP recommand to use it 49 # FIXME: request IQ is not send to check available fields, while XEP recommand to use it
48 # FIXME: doesn't handle data form or oob 50 # FIXME: doesn't handle data form or oob
49 51
51 xmlstream.ConnectAuthenticator.__init__(self, jid_.host) 53 xmlstream.ConnectAuthenticator.__init__(self, jid_.host)
52 self.jid = jid_ 54 self.jid = jid_
53 self.password = password 55 self.password = password
54 self.email = email 56 self.email = email
55 self.registered = defer.Deferred() 57 self.registered = defer.Deferred()
56 log.debug(_(u"Registration asked for {jid}").format( 58 log.debug(_(u"Registration asked for {jid}").format(jid=jid_))
57 jid = jid_))
58 59
59 def connectionMade(self): 60 def connectionMade(self):
60 log.debug(_(u"Connection made with {server}".format(server=self.jid.host))) 61 log.debug(_(u"Connection made with {server}".format(server=self.jid.host)))
61 self.xmlstream.otherEntity = jid.JID(self.jid.host) 62 self.xmlstream.otherEntity = jid.JID(self.jid.host)
62 self.xmlstream.namespace = C.NS_CLIENT 63 self.xmlstream.namespace = C.NS_CLIENT
75 self.xmlstream.sendFooter() 76 self.xmlstream.sendFooter()
76 raise failure_ 77 raise failure_
77 78
78 79
79 class XEP_0077(object): 80 class XEP_0077(object):
80
81 def __init__(self, host): 81 def __init__(self, host):
82 log.info(_("Plugin XEP_0077 initialization")) 82 log.info(_("Plugin XEP_0077 initialization"))
83 self.host = host 83 self.host = host
84 host.bridge.addMethod("inBandRegister", ".plugin", in_sign='ss', out_sign='', 84 host.bridge.addMethod(
85 method=self._inBandRegister, 85 "inBandRegister",
86 async=True) 86 ".plugin",
87 host.bridge.addMethod("inBandAccountNew", ".plugin", in_sign='ssssi', out_sign='', 87 in_sign="ss",
88 method=self._registerNewAccount, 88 out_sign="",
89 async=True) 89 method=self._inBandRegister,
90 host.bridge.addMethod("inBandUnregister", ".plugin", in_sign='ss', out_sign='', 90 async=True,
91 method=self._unregister, 91 )
92 async=True) 92 host.bridge.addMethod(
93 host.bridge.addMethod("inBandPasswordChange", ".plugin", in_sign='ss', out_sign='', 93 "inBandAccountNew",
94 method=self._changePassword, 94 ".plugin",
95 async=True) 95 in_sign="ssssi",
96 out_sign="",
97 method=self._registerNewAccount,
98 async=True,
99 )
100 host.bridge.addMethod(
101 "inBandUnregister",
102 ".plugin",
103 in_sign="ss",
104 out_sign="",
105 method=self._unregister,
106 async=True,
107 )
108 host.bridge.addMethod(
109 "inBandPasswordChange",
110 ".plugin",
111 in_sign="ss",
112 out_sign="",
113 method=self._changePassword,
114 async=True,
115 )
96 116
97 @staticmethod 117 @staticmethod
98 def buildRegisterIQ(xmlstream_, jid_, password, email=None): 118 def buildRegisterIQ(xmlstream_, jid_, password, email=None):
99 iq_elt = xmlstream.IQ(xmlstream_, 'set') 119 iq_elt = xmlstream.IQ(xmlstream_, "set")
100 iq_elt["to"] = jid_.host 120 iq_elt["to"] = jid_.host
101 query_elt = iq_elt.addElement(('jabber:iq:register', 'query')) 121 query_elt = iq_elt.addElement(("jabber:iq:register", "query"))
102 username_elt = query_elt.addElement('username') 122 username_elt = query_elt.addElement("username")
103 username_elt.addContent(jid_.user) 123 username_elt.addContent(jid_.user)
104 password_elt = query_elt.addElement('password') 124 password_elt = query_elt.addElement("password")
105 password_elt.addContent(password) 125 password_elt.addContent(password)
106 if email is not None: 126 if email is not None:
107 email_elt = query_elt.addElement('email') 127 email_elt = query_elt.addElement("email")
108 email_elt.addContent(email) 128 email_elt.addContent(email)
109 return iq_elt 129 return iq_elt
110 130
111 def _regCb(self, answer, client, post_treat_cb): 131 def _regCb(self, answer, client, post_treat_cb):
112 """Called after the first get IQ""" 132 """Called after the first get IQ"""
113 try: 133 try:
114 query_elt = answer.elements(NS_REG, 'query').next() 134 query_elt = answer.elements(NS_REG, "query").next()
115 except StopIteration: 135 except StopIteration:
116 raise exceptions.DataError("Can't find expected query element") 136 raise exceptions.DataError("Can't find expected query element")
117 137
118 try: 138 try:
119 x_elem = query_elt.elements(data_form.NS_X_DATA, 'x').next() 139 x_elem = query_elt.elements(data_form.NS_X_DATA, "x").next()
120 except StopIteration: 140 except StopIteration:
121 # XXX: it seems we have an old service which doesn't manage data forms 141 # XXX: it seems we have an old service which doesn't manage data forms
122 log.warning(_("Can't find data form")) 142 log.warning(_("Can't find data form"))
123 raise exceptions.DataError(_("This gateway can't be managed by SàT, sorry :(")) 143 raise exceptions.DataError(
144 _("This gateway can't be managed by SàT, sorry :(")
145 )
124 146
125 def submitForm(data, profile): 147 def submitForm(data, profile):
126 form_elt = xml_tools.XMLUIResultToElt(data) 148 form_elt = xml_tools.XMLUIResultToElt(data)
127 149
128 iq_elt = client.IQ() 150 iq_elt = client.IQ()
129 iq_elt['id'] = answer['id'] 151 iq_elt["id"] = answer["id"]
130 iq_elt['to'] = answer['from'] 152 iq_elt["to"] = answer["from"]
131 query_elt = iq_elt.addElement("query", NS_REG) 153 query_elt = iq_elt.addElement("query", NS_REG)
132 query_elt.addChild(form_elt) 154 query_elt.addChild(form_elt)
133 d = iq_elt.send() 155 d = iq_elt.send()
134 d.addCallback(self._regSuccess, client, post_treat_cb) 156 d.addCallback(self._regSuccess, client, post_treat_cb)
135 d.addErrback(self._regFailure, client) 157 d.addErrback(self._regFailure, client)
136 return d 158 return d
137 159
138 form = data_form.Form.fromElement(x_elem) 160 form = data_form.Form.fromElement(x_elem)
139 submit_reg_id = self.host.registerCallback(submitForm, with_data=True, one_shot=True) 161 submit_reg_id = self.host.registerCallback(
162 submitForm, with_data=True, one_shot=True
163 )
140 return xml_tools.dataForm2XMLUI(form, submit_reg_id) 164 return xml_tools.dataForm2XMLUI(form, submit_reg_id)
141 165
142 def _regEb(self, failure, client): 166 def _regEb(self, failure, client):
143 """Called when something is wrong with registration""" 167 """Called when something is wrong with registration"""
144 log.info(_("Registration failure: %s") % unicode(failure.value)) 168 log.info(_("Registration failure: %s") % unicode(failure.value))
145 raise failure 169 raise failure
146 170
147 def _regSuccess(self, answer, client, post_treat_cb): 171 def _regSuccess(self, answer, client, post_treat_cb):
148 log.debug(_(u"registration answer: %s") % answer.toXml()) 172 log.debug(_(u"registration answer: %s") % answer.toXml())
149 if post_treat_cb is not None: 173 if post_treat_cb is not None:
150 post_treat_cb(jid.JID(answer['from']), client.profile) 174 post_treat_cb(jid.JID(answer["from"]), client.profile)
151 return {} 175 return {}
152 176
153 def _regFailure(self, failure, client): 177 def _regFailure(self, failure, client):
154 log.info(_(u"Registration failure: %s") % unicode(failure.value)) 178 log.info(_(u"Registration failure: %s") % unicode(failure.value))
155 if failure.value.condition == 'conflict': 179 if failure.value.condition == "conflict":
156 raise exceptions.ConflictError( _("Username already exists, please choose an other one")) 180 raise exceptions.ConflictError(
181 _("Username already exists, please choose an other one")
182 )
157 raise failure 183 raise failure
158 184
159 def _inBandRegister(self, to_jid_s, profile_key=C.PROF_KEY_NONE): 185 def _inBandRegister(self, to_jid_s, profile_key=C.PROF_KEY_NONE):
160 return self.inBandRegister, jid.JID(to_jid_s, profile_key) 186 return self.inBandRegister, jid.JID(to_jid_s, profile_key)
161 187
165 @param to_jid(jid.JID): jid of the service to register to 191 @param to_jid(jid.JID): jid of the service to register to
166 """ 192 """
167 # FIXME: this post_treat_cb arguments seems wrong, check it 193 # FIXME: this post_treat_cb arguments seems wrong, check it
168 client = self.host.getClient(profile_key) 194 client = self.host.getClient(profile_key)
169 log.debug(_(u"Asking registration for {}").format(to_jid.full())) 195 log.debug(_(u"Asking registration for {}").format(to_jid.full()))
170 reg_request = client.IQ(u'get') 196 reg_request = client.IQ(u"get")
171 reg_request["from"] = client.jid.full() 197 reg_request["from"] = client.jid.full()
172 reg_request["to"] = to_jid.full() 198 reg_request["to"] = to_jid.full()
173 reg_request.addElement('query', NS_REG) 199 reg_request.addElement("query", NS_REG)
174 d = reg_request.send(to_jid.full()).addCallbacks(self._regCb, self._regEb, callbackArgs=[client, post_treat_cb], errbackArgs=[client]) 200 d = reg_request.send(to_jid.full()).addCallbacks(
201 self._regCb,
202 self._regEb,
203 callbackArgs=[client, post_treat_cb],
204 errbackArgs=[client],
205 )
175 return d 206 return d
176 207
177 def _registerNewAccount(self, jid_, password, email, host, port): 208 def _registerNewAccount(self, jid_, password, email, host, port):
178 kwargs = {} 209 kwargs = {}
179 if email: 210 if email:
180 kwargs['email'] = email 211 kwargs["email"] = email
181 if host: 212 if host:
182 kwargs['host'] = host 213 kwargs["host"] = host
183 if port: 214 if port:
184 kwargs['port'] = port 215 kwargs["port"] = port
185 return self.registerNewAccount(jid.JID(jid_), password, **kwargs) 216 return self.registerNewAccount(jid.JID(jid_), password, **kwargs)
186 217
187 def registerNewAccount(self, jid_, password, email=None, host=u"127.0.0.1", port=C.XMPP_C2S_PORT): 218 def registerNewAccount(
219 self, jid_, password, email=None, host=u"127.0.0.1", port=C.XMPP_C2S_PORT
220 ):
188 """register a new account on a XMPP server 221 """register a new account on a XMPP server
189 222
190 @param jid_(jid.JID): request jid to register 223 @param jid_(jid.JID): request jid to register
191 @param password(unicode): password of the account 224 @param password(unicode): password of the account
192 @param email(unicode): email of the account 225 @param email(unicode): email of the account
205 return self.changePassword(client, new_password) 238 return self.changePassword(client, new_password)
206 239
207 def changePassword(self, client, new_password): 240 def changePassword(self, client, new_password):
208 iq_elt = self.buildRegisterIQ(client.xmlstream, client.jid, new_password) 241 iq_elt = self.buildRegisterIQ(client.xmlstream, client.jid, new_password)
209 d = iq_elt.send(client.jid.host) 242 d = iq_elt.send(client.jid.host)
210 d.addCallback(lambda dummy: self.host.memory.setParam("Password", new_password, "Connection", profile_key=client.profile)) 243 d.addCallback(
244 lambda dummy: self.host.memory.setParam(
245 "Password", new_password, "Connection", profile_key=client.profile
246 )
247 )
211 return d 248 return d
212 249
213 def _unregister(self, to_jid_s, profile_key): 250 def _unregister(self, to_jid_s, profile_key):
214 client = self.host.getClient(profile_key) 251 client = self.host.getClient(profile_key)
215 return self.unregister(client, jid.JID(to_jid_s)) 252 return self.unregister(client, jid.JID(to_jid_s))
220 BEWARE! if you remove registration from profile own server, this will 257 BEWARE! if you remove registration from profile own server, this will
221 DELETE THE XMPP ACCOUNT WITHOUT WARNING 258 DELETE THE XMPP ACCOUNT WITHOUT WARNING
222 @param to_jid(jid.JID): jid of the service or server 259 @param to_jid(jid.JID): jid of the service or server
223 """ 260 """
224 iq_elt = client.IQ() 261 iq_elt = client.IQ()
225 iq_elt['to'] = to_jid.full() 262 iq_elt["to"] = to_jid.full()
226 query_elt = iq_elt.addElement((NS_REG, u'query')) 263 query_elt = iq_elt.addElement((NS_REG, u"query"))
227 query_elt.addElement(u'remove') 264 query_elt.addElement(u"remove")
228 return iq_elt.send() 265 return iq_elt.send()