annotate libervia/backend/plugins/plugin_xep_0077.py @ 4320:9658c534287e

plugin XEP-0215, XEP-0376: fix bad calls to `hasFeature`: `hasFeature` was called like blocking code, missing the `await`. This has been fixed, and is now using the `memory.disco.has_feature` version.
author Goffi <goffi@goffi.org>
date Mon, 30 Sep 2024 14:14:38 +0200
parents 7ded09452875
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
4 # SAT plugin for managing xep-0077
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3426
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
10 # (at your option) any later version.
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
15 # GNU Affero General Public License for more details.
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
20 from typing import Awaitable, Callable, NamedTuple, cast
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
21
3354
aafb53248872 plugin XEP-0077: fixed "check_certificate" handling
Goffi <goffi@goffi.org>
parents: 3334
diff changeset
22 from twisted.internet import defer, reactor, ssl
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
23 from twisted.internet.interfaces import IReactorCore
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
24 from twisted.python.failure import Failure
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
25 from twisted.words.protocols.jabber import (
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
26 client,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
27 error as jabber_error,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
28 jid,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
29 xmlstream as xmlstream_mod,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
30 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
31 from twisted.words.xish import domish
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
32 from wokkel import data_form, disco, iwokkel
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
33 from zope.interface import implementer
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
34
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
35 from libervia.backend.core import exceptions
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
36 from libervia.backend.core.constants import Const as C
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
37 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
38 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
39 from libervia.backend.core.xmpp import SatXMPPEntity
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
40 from libervia.backend.tools import xml_tools
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
41 from libervia.backend.tools.utils import as_deferred, ensure_deferred
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
42
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
43 log = getLogger(__name__)
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents: 30
diff changeset
44
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
45 NS_IQ_REGISTER = "jabber:iq:register"
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
46 IQ_REGISTER_REQUEST = f'{C.IQ_GET}/query[@xmlns="{NS_IQ_REGISTER}"]'
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
47 IQ_SUBMIT_REQUEST = f'{C.IQ_SET}/query[@xmlns="{NS_IQ_REGISTER}"]'
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 PLUGIN_INFO = {
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
50 C.PI_NAME: "In-Band Registration",
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
51 C.PI_IMPORT_NAME: "XEP-0077",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
52 C.PI_TYPE: "XEP",
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
53 C.PI_MODES: C.PLUG_MODE_BOTH,
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
54 C.PI_PROTOCOLS: ["XEP-0077"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
55 C.PI_DEPENDENCIES: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
56 C.PI_MAIN: "XEP_0077",
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
57 C.PI_DESCRIPTION: _("""Implementation of in-band registration."""),
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
58 C.PI_HANDLER: C.BOOL_TRUE,
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 }
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
61 # FIXME: this implementation is incomplete
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
62
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
64 class RegistrationHandlers(NamedTuple):
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
65 form_handler: Callable
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
66 submit_handler: Callable
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
67
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
68
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
69 class RegisteringAuthenticator(xmlstream_mod.ConnectAuthenticator):
2634
6c89cf856d28 plugin XEP-0077: fixed RegisteringAuthenticator to use TLS if possible
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
70 # FIXME: request IQ is not send to check available fields,
6c89cf856d28 plugin XEP-0077: fixed RegisteringAuthenticator to use TLS if possible
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
71 # while XEP recommand to use it
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
72 # FIXME: doesn't handle data form or oob
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
73 namespace = "jabber:client"
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
74
2838
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
75 def __init__(self, jid_, password, email=None, check_certificate=True):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
76 log.debug(_("Registration asked for {jid}").format(jid=jid_))
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
77 xmlstream_mod.ConnectAuthenticator.__init__(self, jid_.host)
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
78 self.jid = jid_
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
79 self.password = password
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
80 self.email = email
2838
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
81 self.check_certificate = check_certificate
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
82 self.registered = defer.Deferred()
2634
6c89cf856d28 plugin XEP-0077: fixed RegisteringAuthenticator to use TLS if possible
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
83
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
84 def associateWithStream(self, xmlstream):
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
85 xmlstream_mod.ConnectAuthenticator.associateWithStream(self, xmlstream)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
86 xmlstream.addObserver(xmlstream_mod.STREAM_AUTHD_EVENT, self.register)
2634
6c89cf856d28 plugin XEP-0077: fixed RegisteringAuthenticator to use TLS if possible
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
87
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
88 xmlstream.initializers = [client.CheckVersionInitializer(xmlstream)]
3354
aafb53248872 plugin XEP-0077: fixed "check_certificate" handling
Goffi <goffi@goffi.org>
parents: 3334
diff changeset
89 if self.check_certificate:
aafb53248872 plugin XEP-0077: fixed "check_certificate" handling
Goffi <goffi@goffi.org>
parents: 3334
diff changeset
90 tls_required, configurationForTLS = True, None
aafb53248872 plugin XEP-0077: fixed "check_certificate" handling
Goffi <goffi@goffi.org>
parents: 3334
diff changeset
91 else:
aafb53248872 plugin XEP-0077: fixed "check_certificate" handling
Goffi <goffi@goffi.org>
parents: 3334
diff changeset
92 tls_required = False
aafb53248872 plugin XEP-0077: fixed "check_certificate" handling
Goffi <goffi@goffi.org>
parents: 3334
diff changeset
93 configurationForTLS = ssl.CertificateOptions(trustRoot=None)
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
94 tls_init = xmlstream_mod.TLSInitiatingInitializer(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
95 xmlstream, required=tls_required, configurationForTLS=configurationForTLS
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
96 )
3354
aafb53248872 plugin XEP-0077: fixed "check_certificate" handling
Goffi <goffi@goffi.org>
parents: 3334
diff changeset
97
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
98 xmlstream.initializers.append(tls_init)
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
99
2634
6c89cf856d28 plugin XEP-0077: fixed RegisteringAuthenticator to use TLS if possible
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
100 def register(self, xmlstream):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
101 log.debug(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
102 _(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
103 "Stream started with {server}, now registering".format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
104 server=self.jid.host
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
105 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
106 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
107 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
108 iq = XEP_0077.build_register_iq(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
109 self.xmlstream, self.jid, self.password, self.email
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
110 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
111 d = iq.send(self.jid.host).addCallbacks(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
112 self.registration_cb, self.registration_eb
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
113 )
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
114 d.chainDeferred(self.registered)
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
115
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
116 def registration_cb(self, answer):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
117 log.debug(_("Registration answer: {}").format(answer.toXml()))
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
118 assert self.xmlstream is not None
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
119 self.xmlstream.sendFooter()
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
120
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
121 def registration_eb(self, failure_):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
122 log.info(_("Registration failure: {}").format(str(failure_.value)))
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
123 assert self.xmlstream is not None
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
124 self.xmlstream.sendFooter()
2183
1b42bd8c10fb plugin XEP-0077: minor raise fix
Goffi <goffi@goffi.org>
parents: 2177
diff changeset
125 raise failure_
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
126
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
127
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
128 class ServerRegister(xmlstream_mod.XmlStreamFactory):
2838
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
129
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
130 def __init__(self, *args, **kwargs):
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
131 xmlstream_mod.XmlStreamFactory.__init__(self, *args, **kwargs)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
132 self.addBootstrap(xmlstream_mod.STREAM_END_EVENT, self._disconnected)
2838
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
133
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
134 def clientConnectionLost(self, connector, unused_reason):
2838
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
135 connector.disconnect()
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
136
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
137 def _disconnected(self, reason):
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
138 if not self.authenticator.registered.called:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
139 err = jabber_error.StreamError("Server unexpectedly closed the connection")
2838
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
140 try:
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
141 if reason.value.args[0][0][2] == "certificate verify failed":
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
142 err = exceptions.InvalidCertificate()
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
143 except (IndexError, TypeError):
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
144 pass
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
145 self.authenticator.registered.errback(err)
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
146
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
147
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
148 class XEP_0077:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
149
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 def __init__(self, host):
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
151 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self.host = host
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
153 host.register_namespace("iq-register", NS_IQ_REGISTER)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
154 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
155 "in_band_register",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
157 in_sign="ss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
158 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
159 method=self._in_band_register,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
160 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
161 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
162 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
163 "in_band_account_new",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
164 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
165 in_sign="ssssi",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
166 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
167 method=self._register_new_account,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
168 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
169 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
170 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
171 "in_band_unregister",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
172 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
173 in_sign="ss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
174 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 method=self._unregister,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
176 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
177 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
178 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
179 "in_band_password_change",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
180 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
181 in_sign="ss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
182 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
183 method=self._change_password,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
184 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
185 )
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
186 self.handlers = set()
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
187
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
188 def get_handler(self, client: SatXMPPEntity) -> xmlstream_mod.XMPPHandler:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
189 return XEP_0077_handler(self)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
190
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
191 def register_handler(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
192 self,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
193 form_handler: Callable[
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
194 [SatXMPPEntity, domish.Element],
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
195 Awaitable[tuple[bool, data_form.Form] | None]
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
196 | defer.Deferred[tuple[bool, data_form.Form] | None]
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
197 | tuple[bool, data_form.Form]
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
198 | None,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
199 ],
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
200 submit_handler: Callable[
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
201 [SatXMPPEntity, domish.Element, data_form.Form | None],
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
202 Awaitable[bool | None] | defer.Deferred[bool | None] | bool | None,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
203 ],
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
204 ) -> None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
205 """
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
206 Register a new handler.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
207
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
208 Mostly useful for component which handle In-Band Registration.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
209 @param form_handler: method to call on registration request to get a data form.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
210 May be async.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
211 The handler must return a data_form.Form instance if it can handle the
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
212 request, otherwise other handlers will be tried until one returns a data form.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
213 @param submit_handler: method to call on registration request to submit the
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
214 handler.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
215 In case of "unregister" request, None will be used instead of a
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
216 data_form.Form.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
217 """
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
218 self.handlers.add(RegistrationHandlers(form_handler, submit_handler))
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
219
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
220 def _on_register_request(self, iq_elt: domish.Element, client: SatXMPPEntity) -> None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
221 defer.ensureDeferred(self._on_request(iq_elt, client, True))
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
222
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
223 def _on_submit_request(self, iq_elt: domish.Element, client: SatXMPPEntity) -> None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
224 defer.ensureDeferred(self._on_request(iq_elt, client, False))
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
225
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
226 async def _on_request(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
227 self, iq_elt: domish.Element, client: SatXMPPEntity, is_register: bool
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
228 ) -> None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
229 """Handle a register or submit request.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
230
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
231 @param iq_elt: The IQ element of the request.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
232 @param client: Client session.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
233 @param is_register: Whether this is a register request (True) or a submit request
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
234 (False).
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
235 """
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
236 iq_elt.handled = True
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
237
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
238 # Submit request must have a form with submitted values.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
239 if is_register:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
240 handler_type = "register"
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
241 submit_form = None
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
242 else:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
243 handler_type = "submit"
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
244 remove_elt = next(iq_elt.query.elements(NS_IQ_REGISTER, "remove"), None)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
245 if remove_elt is not None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
246 # This is a unregister request.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
247 submit_form = None
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
248 else:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
249 submit_form = data_form.findForm(iq_elt.query, NS_IQ_REGISTER)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
250 if submit_form is None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
251 log.warning(f"Data form not found, invalid request: {iq_elt.toXml()}")
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
252 client.send(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
253 jabber_error.StanzaError(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
254 "bad-request", text="No data form found."
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
255 ).toResponse(iq_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
256 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
257 return
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
258
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
259 # We look and run relevant handler.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
260 for handlers in self.handlers:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
261 if is_register:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
262 handler = handlers.form_handler
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
263 handler_call = as_deferred(handler, client, iq_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
264 else:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
265 handler = handlers.submit_handler
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
266 handler_call = as_deferred(handler, client, iq_elt, submit_form)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
267 try:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
268 callback_ret = await handler_call
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
269 except jabber_error.StanzaError as e:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
270 iq_error_elt = e.toResponse(iq_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
271 client.send(iq_error_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
272 return
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
273 except exceptions.PasswordError as e:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
274 log.warning("Invalid login or password while registering to service.")
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
275 iq_error_elt = jabber_error.StanzaError(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
276 "forbidden", text=str(e)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
277 ).toResponse(iq_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
278 client.send(iq_error_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
279 return
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
280 except Exception:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
281 log.exception(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
282 f"Error while handling {handler_type} request with {handler}, "
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
283 "ignoring it."
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
284 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
285 continue
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
286 if callback_ret is not None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
287 if is_register:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
288 try:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
289 registered, registration_form = callback_ret
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
290 assert isinstance(registered, bool)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
291 assert isinstance(registration_form, data_form.Form)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
292 except (TypeError, ValueError, AssertionError) as e:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
293 log.warning(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
294 f"Invalid return value from {handler}, ignoring it: {e}"
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
295 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
296 continue
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
297 # We need to be sure to have the right namespace for the form.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
298 registration_form.formNamespace = NS_IQ_REGISTER
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
299 iq_result_elt = xmlstream_mod.toResponse(iq_elt, "result")
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
300 query_elt = iq_result_elt.addElement((NS_IQ_REGISTER, "query"))
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
301
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
302 if registered:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
303 # The requestor is already registered, we indicate it.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
304 query_elt.addElement("registered")
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
305
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
306 query_elt.addChild(registration_form.toElement())
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
307 client.send(iq_result_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
308 else:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
309 if callback_ret is True:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
310 if submit_form is None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
311 log.info(f"User {iq_elt['from']} successfully unregistered.")
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
312 else:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
313 log.info(f"User {iq_elt['from']} successfully registered.")
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
314 else:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
315 log.error(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
316 f"Unexpected return value from {handler}, was expecting "
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
317 '"True".'
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
318 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
319 client.send(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
320 jabber_error.StanzaError(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
321 "internal-server-error", text="Error in request handler."
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
322 ).toResponse(iq_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
323 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
324 return
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
325 iq_result_elt = xmlstream_mod.toResponse(iq_elt, "result")
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
326 client.send(iq_result_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
327 break
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
328 else:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
329 log.warning(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
330 f"No handler found for in-band registration {handler_type} request: "
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
331 f"{iq_elt.toXml()}."
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
332 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
333 iq_error_elt = jabber_error.StanzaError("service-unavailable").toResponse(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
334 iq_elt
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
335 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
336 client.send(iq_error_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
337 return
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
338
2175
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
339 @staticmethod
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
340 def build_register_iq(xmlstream, jid_, password, email=None):
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
341 iq_elt = xmlstream_mod.IQ(xmlstream, "set")
2175
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
342 iq_elt["to"] = jid_.host
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
343 query_elt = iq_elt.addElement(("jabber:iq:register", "query"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
344 username_elt = query_elt.addElement("username")
2175
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
345 username_elt.addContent(jid_.user)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
346 password_elt = query_elt.addElement("password")
2175
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
347 password_elt.addContent(password)
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
348 if email is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
349 email_elt = query_elt.addElement("email")
2175
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
350 email_elt.addContent(email)
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
351 return iq_elt
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
352
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
353 def _in_band_register(self, to_jid_s, profile_key=C.PROF_KEY_NONE):
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
354 client = self.host.get_client(profile_key)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
355 return defer.ensureDeferred(self.in_band_register(client, jid.JID(to_jid_s)))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
356
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
357 async def in_band_register(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
358 self,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
359 client: SatXMPPEntity,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
360 to_jid: jid.JID,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
361 post_treat_cb: Callable | None = None,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
362 ) -> xml_tools.XMLUI:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
363 """Register to a service.
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
364
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
365 Send an IQ request to register with the given service and return the registration
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
366 form as XML UI.
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
367
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
368 @param client: client session.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
369 @param to_jid: The JID of the service to register to.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
370 @param post_treat_cb: A callback function to handle the registration result, if
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
371 provided.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
372 @return: The registration form as XML UI.
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
373 """
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
374 # FIXME: this post_treat_cb arguments seems wrong, check it
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
375 log.debug(_("Asking registration for {}").format(to_jid.full()))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
376 reg_request = client.IQ("get")
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
377 reg_request["from"] = client.jid.full()
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
378 reg_request["to"] = to_jid.full()
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
379 reg_request.addElement("query", NS_IQ_REGISTER)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
380 try:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
381 iq_result_elt = await reg_request.send(to_jid.full())
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
382 except Exception as e:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
383 log.warning(_("Registration failure: {}").format(e))
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
384 raise e
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
385 else:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
386 try:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
387 query_elt = next(iq_result_elt.elements(NS_IQ_REGISTER, "query"))
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
388 except StopIteration:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
389 raise exceptions.DataError(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
390 f"Can't find expected query element: {iq_result_elt.toXml()}"
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
391 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
392
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
393 try:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
394 x_elem = next(query_elt.elements(data_form.NS_X_DATA, "x"))
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
395 except StopIteration:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
396 # XXX: it seems we have an old service which doesn't manage data forms
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
397 log.warning("Can't find data form: {iq_result_elt.toXml()}")
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
398 raise exceptions.DataError(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
399 _(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
400 "This gateway is outdated and can't be managed by Libervia, "
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
401 "sorry :("
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
402 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
403 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
404
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
405 form = data_form.Form.fromElement(x_elem)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
406 xml_ui = xml_tools.data_form_2_xmlui(form, "")
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
407 d = xml_tools.deferred_ui(self.host, xml_ui)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
408 d.addCallback(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
409 self._on_xml_ui_cb,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
410 client,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
411 iq_result_elt["id"],
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
412 iq_result_elt["from"],
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
413 post_treat_cb,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
414 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
415 d.addErrback(self._on_xml_ui_eb)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
416 d.addTimeout(600, cast(IReactorCore, reactor))
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
417 return xml_ui
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
418
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
419 @ensure_deferred
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
420 async def _on_xml_ui_cb(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
421 self,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
422 data: dict,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
423 client: SatXMPPEntity,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
424 stanza_id: str,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
425 to_: str,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
426 post_treat_cb: Callable | None,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
427 ) -> None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
428 """Handle the XML UI result of a registration form.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
429
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
430 Process the filled registration form (from frontend) and send it back to complete
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
431 the registration process.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
432
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
433 @param data: The filled registration form as a XMLUI Form result.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
434 @param client: Client session.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
435 @param stanza_id: The ID of the IQ stanza of the registration process.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
436 @param to_: The JID of the service that sent the registration form.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
437 @param post_treat_cb: A callback function to handle the registration result, if
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
438 provided.
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
439 """
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
440 form_elt = xml_tools.xmlui_result_to_elt(data, NS_IQ_REGISTER)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
441
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
442 iq_elt = client.IQ()
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
443 iq_elt["id"] = stanza_id
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
444 iq_elt["to"] = to_
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
445 query_elt = iq_elt.addElement("query", NS_IQ_REGISTER)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
446 query_elt.addChild(form_elt)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
447 try:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
448 answer = await iq_elt.send()
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
449 log.debug(_("registration answer: %s") % answer.toXml())
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
450 if post_treat_cb is not None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
451 post_treat_cb(jid.JID(answer["from"]), client.profile)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
452 except jabber_error.StanzaError as e:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
453 log.info(_("Registration failure: {}").format(e))
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
454 if e.condition == "conflict":
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
455 raise exceptions.ConflictError(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
456 _("Username already exists, please choose another one.")
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
457 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
458 raise e
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
459
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
460 def _on_xml_ui_eb(self, failure_: Failure) -> None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
461 """Handle error during handling of registration form by frontend."""
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
462 log.warning(f"Error while handling registration form to frontend: {failure_}")
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
463
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
464 def _register_new_account(self, jid_, password, email, host, port):
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
465 kwargs = {}
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
466 if email:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
467 kwargs["email"] = email
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
468 if host:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
469 kwargs["host"] = host
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
470 if port:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
471 kwargs["port"] = port
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
472 return self.register_new_account(jid.JID(jid_), password, **kwargs)
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
473
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
474 def register_new_account(
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
475 self,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
476 jid_: jid.JID,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
477 password: str,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
478 email: str | None = None,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
479 host: str | None = None,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
480 port: int = C.XMPP_C2S_PORT,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
481 ):
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
482 """Register a new account on a XMPP server.
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
483
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
484 @param jid_: request jid to register
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
485 @param password: password of the account
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
486 @param email: email of the account
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
487 @param host: host of the server to register to
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
488 @param port: port of the server to register to
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
489 """
3426
8c25489bb79c plugin XEP-0077: use `xmpp_domain` when set for `host`
Goffi <goffi@goffi.org>
parents: 3400
diff changeset
490 if host is None:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
491 host = self.host.memory.config_get("", "xmpp_domain", "127.0.0.1")
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
492 check_certificate = host != "127.0.0.1"
2838
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
493 authenticator = RegisteringAuthenticator(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
494 jid_, password, email, check_certificate=check_certificate
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
495 )
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
496 registered_d = authenticator.registered
2838
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
497 server_register = ServerRegister(authenticator)
8018cf9aa55b plugin XEP-0077: correctly report invalid certificate + errback on unexpected stream close
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
498 reactor.connectTCP(host, port, server_register)
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
499 return registered_d
2175
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
500
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
501 def _change_password(self, new_password, profile_key):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
502 client = self.host.get_client(profile_key)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
503 return self.change_password(client, new_password)
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
504
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
505 def change_password(self, client, new_password):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
506 iq_elt = self.build_register_iq(client.xmlstream, client.jid, new_password)
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
507 d = iq_elt.send(client.jid.host)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
508 d.addCallback(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
509 lambda __: self.host.memory.param_set(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
510 "Password", new_password, "Connection", profile_key=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
511 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
512 )
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
513 return d
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
514
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
515 def _unregister(self, to_jid_s, profile_key):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
516 client = self.host.get_client(profile_key)
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
517 return self.unregister(client, jid.JID(to_jid_s))
2175
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
518
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
519 def unregister(self, client: SatXMPPEntity, to_jid: jid.JID) -> defer.Deferred:
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
520 """Remove registration from a server/service.
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
521
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
522 BEWARE! if you remove registration from profile own server, this will
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
523 DELETE THE XMPP ACCOUNT WITHOUT WARNING
3334
2cd54c72fae4 plugin XEP-0077: None can now be used in `to_jid` argument in `unregister`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
524 @param to_jid: jid of the service or server
2cd54c72fae4 plugin XEP-0077: None can now be used in `to_jid` argument in `unregister`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
525 None to delete client's account (DANGEROUS!)
2177
09cfec4d8d19 plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
526 """
2175
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
527 iq_elt = client.IQ()
3334
2cd54c72fae4 plugin XEP-0077: None can now be used in `to_jid` argument in `unregister`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
528 if to_jid is not None:
2cd54c72fae4 plugin XEP-0077: None can now be used in `to_jid` argument in `unregister`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
529 iq_elt["to"] = to_jid.full()
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
530 query_elt = iq_elt.addElement((NS_IQ_REGISTER, "query"))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2838
diff changeset
531 query_elt.addElement("remove")
3400
77ad87763f32 plugin XEP-0077: disconnect client when deleting self account:
Goffi <goffi@goffi.org>
parents: 3354
diff changeset
532 d = iq_elt.send()
77ad87763f32 plugin XEP-0077: disconnect client when deleting self account:
Goffi <goffi@goffi.org>
parents: 3354
diff changeset
533 if not to_jid or to_jid == jid.JID(client.jid.host):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
534 d.addCallback(lambda __: client.entity_disconnect())
3400
77ad87763f32 plugin XEP-0077: disconnect client when deleting self account:
Goffi <goffi@goffi.org>
parents: 3354
diff changeset
535 return d
4300
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
536
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
537
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
538 @implementer(iwokkel.IDisco)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
539 class XEP_0077_handler(xmlstream_mod.XMPPHandler):
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
540
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
541 def __init__(self, plugin_parent: XEP_0077) -> None:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
542 self.plugin_parent = plugin_parent
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
543
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
544 def connectionInitialized(self):
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
545 client = cast(SatXMPPEntity, self.parent)
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
546 if client.is_component:
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
547 self.xmlstream.addObserver(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
548 IQ_REGISTER_REQUEST,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
549 self.plugin_parent._on_register_request,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
550 client=client,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
551 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
552 self.xmlstream.addObserver(
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
553 IQ_SUBMIT_REQUEST,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
554 self.plugin_parent._on_submit_request,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
555 client=client,
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
556 )
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
557
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
558 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
559 return [disco.DiscoFeature(NS_IQ_REGISTER)]
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
560
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
561 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
7ded09452875 plugin XEP-0077, XEP-0100: Adapt to component, and modernize:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
562 return []