Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0077.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | be6d91572633 |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
75 xs.initializers.append(tls_init) | 75 xs.initializers.append(tls_init) |
76 | 76 |
77 def register(self, xmlstream): | 77 def register(self, xmlstream): |
78 log.debug(_("Stream started with {server}, now registering" | 78 log.debug(_("Stream started with {server}, now registering" |
79 .format(server=self.jid.host))) | 79 .format(server=self.jid.host))) |
80 iq = XEP_0077.buildRegisterIQ(self.xmlstream, self.jid, self.password, self.email) | 80 iq = XEP_0077.build_register_iq(self.xmlstream, self.jid, self.password, self.email) |
81 d = iq.send(self.jid.host).addCallbacks(self.registrationCb, self.registrationEb) | 81 d = iq.send(self.jid.host).addCallbacks(self.registration_cb, self.registration_eb) |
82 d.chainDeferred(self.registered) | 82 d.chainDeferred(self.registered) |
83 | 83 |
84 def registrationCb(self, answer): | 84 def registration_cb(self, answer): |
85 log.debug(_("Registration answer: {}").format(answer.toXml())) | 85 log.debug(_("Registration answer: {}").format(answer.toXml())) |
86 self.xmlstream.sendFooter() | 86 self.xmlstream.sendFooter() |
87 | 87 |
88 def registrationEb(self, failure_): | 88 def registration_eb(self, failure_): |
89 log.info(_("Registration failure: {}").format(str(failure_.value))) | 89 log.info(_("Registration failure: {}").format(str(failure_.value))) |
90 self.xmlstream.sendFooter() | 90 self.xmlstream.sendFooter() |
91 raise failure_ | 91 raise failure_ |
92 | 92 |
93 | 93 |
113 | 113 |
114 class XEP_0077(object): | 114 class XEP_0077(object): |
115 def __init__(self, host): | 115 def __init__(self, host): |
116 log.info(_("Plugin XEP_0077 initialization")) | 116 log.info(_("Plugin XEP_0077 initialization")) |
117 self.host = host | 117 self.host = host |
118 host.bridge.addMethod( | 118 host.bridge.add_method( |
119 "inBandRegister", | 119 "in_band_register", |
120 ".plugin", | 120 ".plugin", |
121 in_sign="ss", | 121 in_sign="ss", |
122 out_sign="", | 122 out_sign="", |
123 method=self._inBandRegister, | 123 method=self._in_band_register, |
124 async_=True, | 124 async_=True, |
125 ) | 125 ) |
126 host.bridge.addMethod( | 126 host.bridge.add_method( |
127 "inBandAccountNew", | 127 "in_band_account_new", |
128 ".plugin", | 128 ".plugin", |
129 in_sign="ssssi", | 129 in_sign="ssssi", |
130 out_sign="", | 130 out_sign="", |
131 method=self._registerNewAccount, | 131 method=self._register_new_account, |
132 async_=True, | 132 async_=True, |
133 ) | 133 ) |
134 host.bridge.addMethod( | 134 host.bridge.add_method( |
135 "inBandUnregister", | 135 "in_band_unregister", |
136 ".plugin", | 136 ".plugin", |
137 in_sign="ss", | 137 in_sign="ss", |
138 out_sign="", | 138 out_sign="", |
139 method=self._unregister, | 139 method=self._unregister, |
140 async_=True, | 140 async_=True, |
141 ) | 141 ) |
142 host.bridge.addMethod( | 142 host.bridge.add_method( |
143 "inBandPasswordChange", | 143 "in_band_password_change", |
144 ".plugin", | 144 ".plugin", |
145 in_sign="ss", | 145 in_sign="ss", |
146 out_sign="", | 146 out_sign="", |
147 method=self._changePassword, | 147 method=self._change_password, |
148 async_=True, | 148 async_=True, |
149 ) | 149 ) |
150 | 150 |
151 @staticmethod | 151 @staticmethod |
152 def buildRegisterIQ(xmlstream_, jid_, password, email=None): | 152 def build_register_iq(xmlstream_, jid_, password, email=None): |
153 iq_elt = xmlstream.IQ(xmlstream_, "set") | 153 iq_elt = xmlstream.IQ(xmlstream_, "set") |
154 iq_elt["to"] = jid_.host | 154 iq_elt["to"] = jid_.host |
155 query_elt = iq_elt.addElement(("jabber:iq:register", "query")) | 155 query_elt = iq_elt.addElement(("jabber:iq:register", "query")) |
156 username_elt = query_elt.addElement("username") | 156 username_elt = query_elt.addElement("username") |
157 username_elt.addContent(jid_.user) | 157 username_elt.addContent(jid_.user) |
160 if email is not None: | 160 if email is not None: |
161 email_elt = query_elt.addElement("email") | 161 email_elt = query_elt.addElement("email") |
162 email_elt.addContent(email) | 162 email_elt.addContent(email) |
163 return iq_elt | 163 return iq_elt |
164 | 164 |
165 def _regCb(self, answer, client, post_treat_cb): | 165 def _reg_cb(self, answer, client, post_treat_cb): |
166 """Called after the first get IQ""" | 166 """Called after the first get IQ""" |
167 try: | 167 try: |
168 query_elt = next(answer.elements(NS_REG, "query")) | 168 query_elt = next(answer.elements(NS_REG, "query")) |
169 except StopIteration: | 169 except StopIteration: |
170 raise exceptions.DataError("Can't find expected query element") | 170 raise exceptions.DataError("Can't find expected query element") |
176 log.warning(_("Can't find data form")) | 176 log.warning(_("Can't find data form")) |
177 raise exceptions.DataError( | 177 raise exceptions.DataError( |
178 _("This gateway can't be managed by SàT, sorry :(") | 178 _("This gateway can't be managed by SàT, sorry :(") |
179 ) | 179 ) |
180 | 180 |
181 def submitForm(data, profile): | 181 def submit_form(data, profile): |
182 form_elt = xml_tools.XMLUIResultToElt(data) | 182 form_elt = xml_tools.xmlui_result_to_elt(data) |
183 | 183 |
184 iq_elt = client.IQ() | 184 iq_elt = client.IQ() |
185 iq_elt["id"] = answer["id"] | 185 iq_elt["id"] = answer["id"] |
186 iq_elt["to"] = answer["from"] | 186 iq_elt["to"] = answer["from"] |
187 query_elt = iq_elt.addElement("query", NS_REG) | 187 query_elt = iq_elt.addElement("query", NS_REG) |
188 query_elt.addChild(form_elt) | 188 query_elt.addChild(form_elt) |
189 d = iq_elt.send() | 189 d = iq_elt.send() |
190 d.addCallback(self._regSuccess, client, post_treat_cb) | 190 d.addCallback(self._reg_success, client, post_treat_cb) |
191 d.addErrback(self._regFailure, client) | 191 d.addErrback(self._reg_failure, client) |
192 return d | 192 return d |
193 | 193 |
194 form = data_form.Form.fromElement(x_elem) | 194 form = data_form.Form.fromElement(x_elem) |
195 submit_reg_id = self.host.registerCallback( | 195 submit_reg_id = self.host.register_callback( |
196 submitForm, with_data=True, one_shot=True | 196 submit_form, with_data=True, one_shot=True |
197 ) | 197 ) |
198 return xml_tools.dataForm2XMLUI(form, submit_reg_id) | 198 return xml_tools.data_form_2_xmlui(form, submit_reg_id) |
199 | 199 |
200 def _regEb(self, failure, client): | 200 def _reg_eb(self, failure, client): |
201 """Called when something is wrong with registration""" | 201 """Called when something is wrong with registration""" |
202 log.info(_("Registration failure: %s") % str(failure.value)) | 202 log.info(_("Registration failure: %s") % str(failure.value)) |
203 raise failure | 203 raise failure |
204 | 204 |
205 def _regSuccess(self, answer, client, post_treat_cb): | 205 def _reg_success(self, answer, client, post_treat_cb): |
206 log.debug(_("registration answer: %s") % answer.toXml()) | 206 log.debug(_("registration answer: %s") % answer.toXml()) |
207 if post_treat_cb is not None: | 207 if post_treat_cb is not None: |
208 post_treat_cb(jid.JID(answer["from"]), client.profile) | 208 post_treat_cb(jid.JID(answer["from"]), client.profile) |
209 return {} | 209 return {} |
210 | 210 |
211 def _regFailure(self, failure, client): | 211 def _reg_failure(self, failure, client): |
212 log.info(_("Registration failure: %s") % str(failure.value)) | 212 log.info(_("Registration failure: %s") % str(failure.value)) |
213 if failure.value.condition == "conflict": | 213 if failure.value.condition == "conflict": |
214 raise exceptions.ConflictError( | 214 raise exceptions.ConflictError( |
215 _("Username already exists, please choose an other one") | 215 _("Username already exists, please choose an other one") |
216 ) | 216 ) |
217 raise failure | 217 raise failure |
218 | 218 |
219 def _inBandRegister(self, to_jid_s, profile_key=C.PROF_KEY_NONE): | 219 def _in_band_register(self, to_jid_s, profile_key=C.PROF_KEY_NONE): |
220 return self.inBandRegister, jid.JID(to_jid_s, profile_key) | 220 return self.in_band_register, jid.JID(to_jid_s, profile_key) |
221 | 221 |
222 def inBandRegister(self, to_jid, post_treat_cb=None, profile_key=C.PROF_KEY_NONE): | 222 def in_band_register(self, to_jid, post_treat_cb=None, profile_key=C.PROF_KEY_NONE): |
223 """register to a service | 223 """register to a service |
224 | 224 |
225 @param to_jid(jid.JID): jid of the service to register to | 225 @param to_jid(jid.JID): jid of the service to register to |
226 """ | 226 """ |
227 # FIXME: this post_treat_cb arguments seems wrong, check it | 227 # FIXME: this post_treat_cb arguments seems wrong, check it |
228 client = self.host.getClient(profile_key) | 228 client = self.host.get_client(profile_key) |
229 log.debug(_("Asking registration for {}").format(to_jid.full())) | 229 log.debug(_("Asking registration for {}").format(to_jid.full())) |
230 reg_request = client.IQ("get") | 230 reg_request = client.IQ("get") |
231 reg_request["from"] = client.jid.full() | 231 reg_request["from"] = client.jid.full() |
232 reg_request["to"] = to_jid.full() | 232 reg_request["to"] = to_jid.full() |
233 reg_request.addElement("query", NS_REG) | 233 reg_request.addElement("query", NS_REG) |
234 d = reg_request.send(to_jid.full()).addCallbacks( | 234 d = reg_request.send(to_jid.full()).addCallbacks( |
235 self._regCb, | 235 self._reg_cb, |
236 self._regEb, | 236 self._reg_eb, |
237 callbackArgs=[client, post_treat_cb], | 237 callbackArgs=[client, post_treat_cb], |
238 errbackArgs=[client], | 238 errbackArgs=[client], |
239 ) | 239 ) |
240 return d | 240 return d |
241 | 241 |
242 def _registerNewAccount(self, jid_, password, email, host, port): | 242 def _register_new_account(self, jid_, password, email, host, port): |
243 kwargs = {} | 243 kwargs = {} |
244 if email: | 244 if email: |
245 kwargs["email"] = email | 245 kwargs["email"] = email |
246 if host: | 246 if host: |
247 kwargs["host"] = host | 247 kwargs["host"] = host |
248 if port: | 248 if port: |
249 kwargs["port"] = port | 249 kwargs["port"] = port |
250 return self.registerNewAccount(jid.JID(jid_), password, **kwargs) | 250 return self.register_new_account(jid.JID(jid_), password, **kwargs) |
251 | 251 |
252 def registerNewAccount( | 252 def register_new_account( |
253 self, jid_, password, email=None, host=None, port=C.XMPP_C2S_PORT | 253 self, jid_, password, email=None, host=None, port=C.XMPP_C2S_PORT |
254 ): | 254 ): |
255 """register a new account on a XMPP server | 255 """register a new account on a XMPP server |
256 | 256 |
257 @param jid_(jid.JID): request jid to register | 257 @param jid_(jid.JID): request jid to register |
259 @param email(unicode): email of the account | 259 @param email(unicode): email of the account |
260 @param host(None, unicode): host of the server to register to | 260 @param host(None, unicode): host of the server to register to |
261 @param port(int): port of the server to register to | 261 @param port(int): port of the server to register to |
262 """ | 262 """ |
263 if host is None: | 263 if host is None: |
264 host = self.host.memory.getConfig("", "xmpp_domain", "127.0.0.1") | 264 host = self.host.memory.config_get("", "xmpp_domain", "127.0.0.1") |
265 check_certificate = host != "127.0.0.1" | 265 check_certificate = host != "127.0.0.1" |
266 authenticator = RegisteringAuthenticator( | 266 authenticator = RegisteringAuthenticator( |
267 jid_, password, email, check_certificate=check_certificate) | 267 jid_, password, email, check_certificate=check_certificate) |
268 registered_d = authenticator.registered | 268 registered_d = authenticator.registered |
269 server_register = ServerRegister(authenticator) | 269 server_register = ServerRegister(authenticator) |
270 reactor.connectTCP(host, port, server_register) | 270 reactor.connectTCP(host, port, server_register) |
271 return registered_d | 271 return registered_d |
272 | 272 |
273 def _changePassword(self, new_password, profile_key): | 273 def _change_password(self, new_password, profile_key): |
274 client = self.host.getClient(profile_key) | 274 client = self.host.get_client(profile_key) |
275 return self.changePassword(client, new_password) | 275 return self.change_password(client, new_password) |
276 | 276 |
277 def changePassword(self, client, new_password): | 277 def change_password(self, client, new_password): |
278 iq_elt = self.buildRegisterIQ(client.xmlstream, client.jid, new_password) | 278 iq_elt = self.build_register_iq(client.xmlstream, client.jid, new_password) |
279 d = iq_elt.send(client.jid.host) | 279 d = iq_elt.send(client.jid.host) |
280 d.addCallback( | 280 d.addCallback( |
281 lambda __: self.host.memory.setParam( | 281 lambda __: self.host.memory.param_set( |
282 "Password", new_password, "Connection", profile_key=client.profile | 282 "Password", new_password, "Connection", profile_key=client.profile |
283 ) | 283 ) |
284 ) | 284 ) |
285 return d | 285 return d |
286 | 286 |
287 def _unregister(self, to_jid_s, profile_key): | 287 def _unregister(self, to_jid_s, profile_key): |
288 client = self.host.getClient(profile_key) | 288 client = self.host.get_client(profile_key) |
289 return self.unregister(client, jid.JID(to_jid_s)) | 289 return self.unregister(client, jid.JID(to_jid_s)) |
290 | 290 |
291 def unregister( | 291 def unregister( |
292 self, | 292 self, |
293 client: SatXMPPEntity, | 293 client: SatXMPPEntity, |
305 iq_elt["to"] = to_jid.full() | 305 iq_elt["to"] = to_jid.full() |
306 query_elt = iq_elt.addElement((NS_REG, "query")) | 306 query_elt = iq_elt.addElement((NS_REG, "query")) |
307 query_elt.addElement("remove") | 307 query_elt.addElement("remove") |
308 d = iq_elt.send() | 308 d = iq_elt.send() |
309 if not to_jid or to_jid == jid.JID(client.jid.host): | 309 if not to_jid or to_jid == jid.JID(client.jid.host): |
310 d.addCallback(lambda __: client.entityDisconnect()) | 310 d.addCallback(lambda __: client.entity_disconnect()) |
311 return d | 311 return d |
312 | 312 |