Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_adhoc_registration.py @ 4332:71c939e34ca6
XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author | Syndace <me@syndace.dev> |
---|---|
date | Sat, 13 Jul 2024 18:28:28 +0200 |
parents | c6d85c31a59f |
children |
rev | line source |
---|---|
4185
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia plugin to handle web frontend registration links with Ad-Hoc Commands |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 import time |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from uuid import uuid4 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.internet import defer |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.words.xish import domish |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from wokkel import data_form |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from libervia.backend.core import exceptions |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from libervia.backend.core.constants import Const as C |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from libervia.backend.core.core_types import SatXMPPEntity |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from libervia.backend.core.i18n import D_, _ |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from libervia.backend.core.log import getLogger |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 from libervia.backend.memory import persistent |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 from libervia.backend.tools.common import data_format, date_utils |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 log = getLogger(__name__) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 PLUGIN_INFO = { |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 C.PI_NAME: "Ad-Hoc Commands - Registration", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 C.PI_IMPORT_NAME: "AD_HOC_REGISTRATION", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 C.PI_TYPE: "Misc", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 C.PI_PROTOCOLS: [], |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 C.PI_DEPENDENCIES: ["XEP-0050"], |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 C.PI_MAIN: "AdHocRegistration", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 C.PI_HANDLER: "no", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 C.PI_DESCRIPTION: _("""Add registration link handling Ad-Hoc commands"""), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 } |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 class AdHocRegistration: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 def __init__(self, host): |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization") |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self.host = host |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 self._c = host.plugins["XEP-0050"] |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 self.ad_hoc_registration_data = persistent.LazyPersistentBinaryDict( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 "registration_links" |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 host.bridge.add_method( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 "registration_link_get", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 ".plugin", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 in_sign="s", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 out_sign="s", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 method=self._get, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 async_=True, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 def _get(self, registration_link_id: str) -> defer.Deferred[str]: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 d = defer.ensureDeferred(self.get(registration_link_id)) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 d.addCallback(data_format.serialise) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 return d |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 async def get(self, registration_link_id: str) -> dict: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 """Retrieve registration link from its ID |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 @param registration_link_id: registration link data |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 @return: registration link data |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 @raise exceptions.NotFound: not registration link found with this ID. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 """ |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 link_data = await self.ad_hoc_registration_data.get(registration_link_id) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 if not link_data: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 raise exceptions.NotFound |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 expiration_timestamp = link_data.get("expiration_timestamp") |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 if expiration_timestamp is not None and expiration_timestamp < time.time(): |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 log.info(f"Deleting expiration link {registration_link_id}.") |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 await self.ad_hoc_registration_data.adel(registration_link_id) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 raise exceptions.NotFound |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 return link_data |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 async def profile_connected(self, client): |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 if client.is_admin: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 self._c.add_ad_hoc_command( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 client, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 self.create_registration_link, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 D_("Create Registration Link"), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 node="https://libervia.org/registration/create", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 self._c.add_ad_hoc_command( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 client, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 self.list_registration_links, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 D_("List Registration Links"), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 node="https://libervia.org/registration/list", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 self._c.add_ad_hoc_command( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 client, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 self.delete_registration_link, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 D_("Delete Registration Link"), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 node="https://libervia.org/registration/delete", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 async def create_registration_link( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 self, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 client: SatXMPPEntity, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 command_elt: domish.Element, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 session_data: dict, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 action: str, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 node: str, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 ) -> tuple[domish.Element | None, str, None, tuple[str, str] | None]: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 """Ad-hoc command used to create a registration link. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 This method presents a form to the user for creating a registration link, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 and processes the submitted form to generate and store the link with its |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 associated data. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 @param client: The XMPP client instance. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 @param command_elt: The command element. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 @param session_data: Data associated with the current session. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 @param action: The action being performed. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 @param node: The node identifier. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 @return: A tuple containing the payload if any, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 """ |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 actions = session_data.setdefault("actions", []) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 actions.append(action) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 if len(actions) == 1: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 # First request, presenting the form to the user |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 status = self._c.STATUS.EXECUTING |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 form = data_form.Form("form", title=D_("Create Registration Link")) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 form.addField( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 data_form.Field("text-single", "recipient", label=D_("Recipient Name")) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 form.addField( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 data_form.Field( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 "text-single", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 "expiration_time_pattern", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 label=D_("Expiration Date"), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 desc=D_( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 "Set the expiry duration for this link. Use the Libervia Time " |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 "Pattern (e.g., '1 week'). The link will expire after this " |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 "period." |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 ), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 value="1 week", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 required=True, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 form.addField( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 data_form.Field( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 "text-single", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 "registration_limit", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 label=D_("Maximum registrations limit"), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 desc=D_( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 "How many accounts can be registered using this link. Set to 0 " |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 "for unlimited registrations." |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 ), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 value="1", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 required=True, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 form.addField( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 data_form.Field( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 "text-multi", "welcome_message", label=D_("Welcome Message") |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 form.addField( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 data_form.Field( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 "text-multi", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 "extra_details", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 label=D_("Additional Details"), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 payload = form.toElement() |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 note = None |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 elif len(actions) == 2: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 try: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 answer_form = data_form.Form.fromElement(x_elt) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 recipient = answer_form.get("recipient", None) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 expiration_time_pattern = answer_form["expiration_time_pattern"] |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 if expiration_time_pattern == "never": |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 expiration_timestamp = None |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 else: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 expiration_timestamp = date_utils.date_parse_ext( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 expiration_time_pattern, default_tz=date_utils.TZ_LOCAL |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 registration_limit = int(answer_form["registration_limit"]) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 welcome_message = answer_form.get("welcome_message", "") |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 extra_details = answer_form.get("extra_details", "") |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 link_id = str(uuid4()) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 link_data = { |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 "recipient": recipient, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 "registration_limit": registration_limit, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 "welcome_message": welcome_message, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 "extra_details": extra_details, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 "creator_jid": session_data["requestor"].full(), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 "use_count": 0, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 "creation_timestamp": int(time.time()), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 } |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 if expiration_timestamp is not None: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 link_data["expiration_timestamp"] = expiration_timestamp |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 await self.ad_hoc_registration_data.aset(link_id, link_data) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 status = self._c.STATUS.COMPLETED |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 payload = None |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 note = ( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 self._c.NOTE.INFO, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 D_("Registration link created successfully: {link_id}").format( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 link_id=link_id |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 ), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 except (KeyError, StopIteration, ValueError): |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 raise self._c.AdHocError(self._c.ERROR.BAD_PAYLOAD) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 else: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 raise self._c.AdHocError(self._c.ERROR.INTERNAL) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 return (payload, status, None, note) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 async def list_registration_links( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 self, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 client: SatXMPPEntity, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 command_elt: domish.Element, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 session_data: dict, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 action: str, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 node: str, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 ) -> tuple[domish.Element | None, str, None, tuple[str, str] | None]: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 """Ad-hoc command used to list all registration links. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 This method retrieves all the registration links and presents them to the user. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 @param client: The XMPP client instance. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 @param command_elt: The command element. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 @param session_data: Data associated with the current session. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 @param action: The action being performed. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 @param node: The node identifier. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 @return: A tuple containing the payload if any, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 """ |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 actions = session_data.setdefault("actions", []) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 actions.append(action) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 if len(actions) == 1: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 all_links = await self.ad_hoc_registration_data.all() |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 status = self._c.STATUS.EXECUTING |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 form = data_form.Form("form", title=D_("Registered Links")) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 for link_id, link_data in all_links.items(): |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 form.addField( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 data_form.Field( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 "text-multi", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 var=f"link_{link_id}", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 label=D_("Link ID: {link_id}").format(link_id=link_id), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 value=str(link_data), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 payload = form.toElement() |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 note = None |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
274 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 status = self._c.STATUS.COMPLETED |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 else: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 raise self._c.AdHocError(self._c.ERROR.INTERNAL) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 return (payload, status, None, note) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 async def delete_registration_link( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 self, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
284 client: SatXMPPEntity, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
285 command_elt: domish.Element, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 session_data: dict, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
287 action: str, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 node: str, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 ) -> tuple[domish.Element | None, str, None, tuple[str, str] | None]: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 """Ad-hoc command used to delete a registration link. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 This method presents a form to the user for selecting a registration link to |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
293 delete, and processes the submitted form to delete the selected link. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 @param client: The XMPP client instance. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
296 @param command_elt: The command element. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
297 @param session_data: Data associated with the current session. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 @param action: The action being performed. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 @param node: The node identifier. |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 @return: A tuple containing the payload if any, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 """ |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 actions = session_data.setdefault("actions", []) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 actions.append(action) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 if len(actions) == 1: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 all_links = await self.ad_hoc_registration_data.all() |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
308 status = self._c.STATUS.EXECUTING |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
309 form = data_form.Form("form", title=D_("Delete Registration Link")) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 link_options = [data_form.Option(link_id, link_id) for link_id in all_links] |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 form.addField( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 data_form.Field( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 "list-single", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 "link_to_delete", |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 label=D_("Select Link to Delete"), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 options=link_options, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 required=True, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
319 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 payload = form.toElement() |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 note = None |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 elif len(actions) == 2: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 try: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
328 answer_form = data_form.Form.fromElement(x_elt) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
329 link_to_delete = answer_form["link_to_delete"] |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 await self.ad_hoc_registration_data.adel(link_to_delete) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 status = self._c.STATUS.COMPLETED |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
334 payload = None |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
335 note = ( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 self._c.NOTE.INFO, |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 D_("Registration link {link_id} deleted successfully.").format( |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 link_id=link_to_delete |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 ), |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
340 ) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 except (KeyError, StopIteration, ValueError): |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 raise self._c.AdHocError(self._c.ERROR.BAD_PAYLOAD) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 else: |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 raise self._c.AdHocError(self._c.ERROR.INTERNAL) |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 |
c6d85c31a59f
plugin ad-hoc registration: Implement plugin to handle registration links:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 return (payload, status, None, note) |