annotate libervia/backend/plugins/plugin_xep_0215.py @ 4149:c36295487082

core: introduce Pydantic based models in `libervia.backend.models.core`
author Goffi <goffi@goffi.org>
date Wed, 22 Nov 2023 14:31:05 +0100
parents 4b842c1fb686
children 9658c534287e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4033
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
2
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
5
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
10
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
15
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
18
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import Dict, Final, List, Optional, Optional
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
20
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.internet import defer
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber import error, jid
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import data_form, disco, iwokkel
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from zope.interface import implementer
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
27
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
28 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
29 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
30 from libervia.backend.core.core_types import SatXMPPEntity
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
31 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
32 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
33 from libervia.backend.tools import xml_tools
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
34 from libervia.backend.tools import utils
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
35 from libervia.backend.tools.common import data_format
4033
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
36
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
37 log = getLogger(__name__)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
38
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
39
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
40 PLUGIN_INFO = {
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_NAME: "External Service Discovery",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_IMPORT_NAME: "XEP-0215",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_TYPE: "XEP",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_MODES: C.PLUG_MODE_BOTH,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_PROTOCOLS: [],
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_DEPENDENCIES: [],
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_RECOMMENDATIONS: [],
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_MAIN: "XEP_0215",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_HANDLER: "yes",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
50 C.PI_DESCRIPTION: _("""Discover services external to the XMPP network"""),
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
51 }
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
52
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
53 NS_EXTDISCO: Final = "urn:xmpp:extdisco:2"
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
54 IQ_PUSH: Final = f'{C.IQ_SET}/services[@xmlns="{NS_EXTDISCO}"]'
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
55
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
56
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
57 class XEP_0215:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
58 def __init__(self, host):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
59 log.info(_("External Service Discovery plugin initialization"))
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4033
diff changeset
61 host.bridge.add_method(
4033
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
62 "external_disco_get",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
63 ".plugin",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
64 in_sign="ss",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
65 out_sign="s",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
66 method=self._external_disco_get,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
67 async_=True,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
68 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4033
diff changeset
69 host.bridge.add_method(
4033
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
70 "external_disco_credentials_get",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
71 ".plugin",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
72 in_sign="ssis",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
73 out_sign="s",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
74 method=self._external_disco_credentials_get,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
75 async_=True,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
76 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
77
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4033
diff changeset
78 def get_handler(self, client):
4033
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
79 return XEP_0215_handler(self)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
80
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4033
diff changeset
81 async def profile_connecting(self, client: SatXMPPEntity) -> None:
4033
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
82 client._xep_0215_services = {}
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
83
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
84 def parse_services(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self, element: domish.Element, parent_elt_name: str = "services"
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
86 ) -> List[dict]:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
87 """Retrieve services from element
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
88
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
89 @param element: <[parent_elt_name]/> element or its parent
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
90 @param parent_elt_name: name of the parent element
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
91 can be "services" or "credentials"
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
92 @return: list of parsed services
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
93 """
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
94 if parent_elt_name not in ("services", "credentials"):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
95 raise exceptions.InternalError(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
96 f"invalid parent_elt_name: {parent_elt_name!r}"
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
97 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
98 if element.name == parent_elt_name and element.uri == NS_EXTDISCO:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
99 services_elt = element
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
100 else:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
101 try:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
102 services_elt = next(element.elements(NS_EXTDISCO, parent_elt_name))
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
103 except StopIteration:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
104 raise exceptions.DataError(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
105 f"XEP-0215 response is missing <{parent_elt_name}> element"
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
106 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
107
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
108 services = []
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
109 for service_elt in services_elt.elements(NS_EXTDISCO, "service"):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
110 service = {}
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
111 for key in [
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
112 "action",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
113 "expires",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
114 "host",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
115 "name",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
116 "password",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
117 "port",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
118 "restricted",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
119 "transport",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
120 "type",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
121 "username",
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
122 ]:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
123 value = service_elt.getAttribute(key)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
124 if value is not None:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
125 if key == "expires":
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
126 try:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
127 service[key] = utils.parse_xmpp_date(value)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
128 except ValueError:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
129 log.warning(f"invalid expiration date: {value!r}")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
130 continue
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
131 elif key == "port":
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
132 try:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
133 service[key] = int(value)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
134 except ValueError:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
135 log.warning(f"invalid port: {value!r}")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
136 continue
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
137 elif key == "restricted":
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
138 service[key] = C.bool(value)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
139 else:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
140 service[key] = value
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
141 if not {"host", "type"}.issubset(service):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
142 log.warning(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
143 'mandatory "host" or "type" are missing in service, ignoring it: '
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
144 "{service_elt.toXml()}"
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
145 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
146 continue
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
147 for x_elt in service_elt.elements(data_form.NS_X_DATA, "x"):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
148 form = data_form.Form.fromElement(x_elt)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
149 extended = service.setdefault("extended", [])
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4033
diff changeset
150 extended.append(xml_tools.data_form_2_data_dict(form))
4033
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
151 services.append(service)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
152
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
153 return services
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
154
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
155 def _external_disco_get(self, entity: str, profile_key: str) -> defer.Deferred:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4033
diff changeset
156 client = self.host.get_client(profile_key)
4033
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
157 d = defer.ensureDeferred(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
158 self.get_external_services(client, jid.JID(entity) if entity else None)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
159 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
160 d.addCallback(data_format.serialise)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
161 return d
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
162
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
163 async def get_external_services(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
164 self, client: SatXMPPEntity, entity: Optional[jid.JID] = None
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
165 ) -> List[Dict]:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
166 """Get non XMPP service proposed by the entity
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
167
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
168 Response is cached after first query
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
169
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
170 @param entity: XMPP entity to query. Default to our own server
4033
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
171 @return: found services
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
172 """
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
173 if entity is None:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
174 entity = client.server_jid
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
175
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
176 if entity.resource:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
177 raise exceptions.DataError("A bare jid was expected for target entity")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
178
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
179 try:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
180 cached_services = client._xep_0215_services[entity]
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
181 except KeyError:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
182 if not self.host.hasFeature(client, NS_EXTDISCO, entity):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
183 cached_services = client._xep_0215_services[entity] = None
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
184 else:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
185 iq_elt = client.IQ("get")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
186 iq_elt["to"] = entity.full()
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
187 iq_elt.addElement((NS_EXTDISCO, "services"))
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
188 try:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
189 iq_result_elt = await iq_elt.send()
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
190 except error.StanzaError as e:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
191 log.warning(f"Can't get external services: {e}")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
192 cached_services = client._xep_0215_services[entity] = None
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
193 else:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
194 cached_services = self.parse_services(iq_result_elt)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
195 client._xep_0215_services[entity] = cached_services
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
196
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
197 return cached_services or []
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
198
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
199 def _external_disco_credentials_get(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
200 self,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
201 entity: str,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
202 host: str,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
203 type_: str,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
204 port: int = 0,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
205 profile_key=C.PROF_KEY_NONE,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
206 ) -> defer.Deferred:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4033
diff changeset
207 client = self.host.get_client(profile_key)
4033
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
208 d = defer.ensureDeferred(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
209 self.request_credentials(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
210 client, host, type_, port or None, jid.JID(entity) if entity else None
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
211 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
212 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
213 d.addCallback(data_format.serialise)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
214 return d
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
215
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
216 async def request_credentials(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
217 self,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
218 client: SatXMPPEntity,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
219 host: str,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
220 type_: str,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
221 port: Optional[int] = None,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
222 entity: Optional[jid.JID] = None,
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
223 ) -> List[dict]:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
224 """Request credentials for specified service(s)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
225
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
226 While usually a single service is expected, several may be returned if the same
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
227 service is launched on several ports (cf. XEP-0215 §3.3)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
228 @param entity: XMPP entity to query. Defaut to our own server
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
229 @param host: service host
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
230 @param type_: service type
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
231 @param port: service port (to be used when several services have same host and
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
232 type but on different ports)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
233 @return: matching services with filled credentials
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
234 """
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
235 if entity is None:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
236 entity = client.server_jid
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
237
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
238 iq_elt = client.IQ("get")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
239 iq_elt["to"] = entity.full()
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
240 iq_elt.addElement((NS_EXTDISCO, "credentials"))
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
241 iq_result_elt = await iq_elt.send()
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
242 return self.parse_services(iq_result_elt, parent_elt_name="credentials")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
243
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
244 def get_matching_service(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
245 self, services: List[dict], host: str, type_: str, port: Optional[int]
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
246 ) -> Optional[dict]:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
247 """Retrieve service data from its characteristics"""
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
248 try:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
249 return next(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
250 s
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
251 for s in services
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
252 if (
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
253 s["host"] == host
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
254 and s["type"] == type_
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
255 and (port is None or s.get("port") == port)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
256 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
257 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
258 except StopIteration:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
259 return None
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
260
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
261 def on_services_push(self, iq_elt: domish.Element, client: SatXMPPEntity) -> None:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
262 iq_elt.handled = True
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
263 entity = jid.JID(iq_elt["from"]).userhostJID()
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
264 cached_services = client._xep_0215_services.get(entity)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
265 if cached_services is None:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
266 log.info(f"ignoring services push for uncached entity {entity}")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
267 return
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
268 try:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
269 services = self.parse_services(iq_elt)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
270 except Exception:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
271 log.exception(f"Can't parse services push: {iq_elt.toXml()}")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
272 return
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
273 for service in services:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
274 host = service["host"]
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
275 type_ = service["type"]
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
276 port = service.get("port")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
277
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
278 action = service.pop("action", None)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
279 if action is None:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
280 # action is not specified, we first check if the service exists
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
281 found_service = self.get_matching_service(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
282 cached_services, host, type_, port
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
283 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
284 if found_service is not None:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
285 # existing service, we replace by the new one
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
286 found_service.clear()
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
287 found_service.update(service)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
288 else:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
289 # new service
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
290 cached_services.append(service)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
291 elif action == "add":
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
292 cached_services.append(service)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
293 elif action in ("modify", "delete"):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
294 found_service = self.get_matching_service(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
295 cached_services, host, type_, port
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
296 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
297 if found_service is None:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
298 log.warning(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
299 f"{entity} want to {action} an unknow service, we ask for the "
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
300 "full list again"
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
301 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
302 # we delete cache and request a fresh list to make a new one
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
303 del client._xep_0215_services[entity]
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
304 defer.ensureDeferred(self.get_external_services(client, entity))
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
305 elif action == "modify":
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
306 found_service.clear()
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
307 found_service.update(service)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
308 else:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
309 cached_services.remove(found_service)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
310 else:
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
311 log.warning(f"unknown action for services push, ignoring: {action!r}")
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
312
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
313
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
314 @implementer(iwokkel.IDisco)
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
315 class XEP_0215_handler(XMPPHandler):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
316 def __init__(self, plugin_parent):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
317 self.plugin_parent = plugin_parent
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
318
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
319 def connectionInitialized(self):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
320 self.xmlstream.addObserver(
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
321 IQ_PUSH, self.plugin_parent.on_services_push, client=self.parent
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
322 )
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
323
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
324 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
325 return [disco.DiscoFeature(NS_EXTDISCO)]
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
326
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
327 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
5a42c7842556 core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
diff changeset
328 return []