comparison libervia/backend/plugins/plugin_xep_0080.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_xep_0080.py@524856bd7b19
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org)
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 from typing import Dict, Any
19
20 from twisted.words.xish import domish
21
22 from libervia.backend.core.constants import Const as C
23 from libervia.backend.core.i18n import _
24 from libervia.backend.core.log import getLogger
25 from libervia.backend.core import exceptions
26 from libervia.backend.tools import utils
27
28 log = getLogger(__name__)
29
30
31 PLUGIN_INFO = {
32 C.PI_NAME: "User Location",
33 C.PI_IMPORT_NAME: "XEP-0080",
34 C.PI_TYPE: "XEP",
35 C.PI_MODES: C.PLUG_MODE_BOTH,
36 C.PI_PROTOCOLS: ["XEP-0080"],
37 C.PI_MAIN: "XEP_0080",
38 C.PI_HANDLER: "no",
39 C.PI_DESCRIPTION: _("""Implementation of XEP-0080 (User Location)"""),
40 }
41
42 NS_GEOLOC = "http://jabber.org/protocol/geoloc"
43 KEYS_TYPES = {
44 "accuracy": float,
45 "alt": float,
46 "altaccuracy": float,
47 "area": str,
48 "bearing": float,
49 "building": str,
50 "country": str,
51 "countrycode": str,
52 "datum": str,
53 "description": str,
54 "error": float,
55 "floor": str,
56 "lat": float,
57 "locality": str,
58 "lon": float,
59 "postalcode": str,
60 "region": str,
61 "room": str,
62 "speed": float,
63 "street": str,
64 "text": str,
65 "timestamp": "datetime",
66 "tzo": str,
67 "uri": str
68 }
69
70
71 class XEP_0080:
72
73 def __init__(self, host):
74 log.info(_("XEP-0080 (User Location) plugin initialization"))
75 host.register_namespace("geoloc", NS_GEOLOC)
76
77 def get_geoloc_elt(
78 self,
79 location_data: Dict[str, Any],
80 ) -> domish.Element:
81 """Generate the element describing the location
82
83 @param geoloc: metadata description the location
84 Keys correspond to ones found at
85 https://xmpp.org/extensions/xep-0080.html#format, with following additional
86 keys:
87 - id (str): Identifier for this location
88 - language (str): language of the human readable texts
89 All keys are optional.
90 @return: ``<geoloc/>`` element
91 """
92 geoloc_elt = domish.Element((NS_GEOLOC, "geoloc"))
93 for key, value in location_data.items():
94 try:
95 key_type = KEYS_TYPES[key]
96 except KeyError:
97 if key == "id":
98 # "id" attribute is not specified for XEP-0080's <geoloc/> element,
99 # but it may be used in a parent element (that's the case for events)
100 pass
101 elif key == "language":
102 geoloc_elt["xml:lang"] = value
103 else:
104 log.warning(f"Unknown location key {key}: {location_data}")
105 continue
106 if key_type == "datetime":
107 content = utils.xmpp_date(value)
108 else:
109 content = str(value)
110 geoloc_elt.addElement(key, content=content)
111
112 return geoloc_elt
113
114 def parse_geoloc_elt(
115 self,
116 geoloc_elt: domish.Element
117 ) -> Dict[str, Any]:
118 """Parse <geoloc/> element
119
120 @param geoloc_elt: <geoloc/> element
121 a parent element can also be used
122 @return: geoloc data. It's a dict whose keys correspond to
123 [get_geoloc_elt] parameters
124 @raise exceptions.NotFound: no <geoloc/> element has been found
125 """
126
127 if geoloc_elt.name != "geoloc":
128 try:
129 geoloc_elt = next(geoloc_elt.elements(NS_GEOLOC, "geoloc"))
130 except StopIteration:
131 raise exceptions.NotFound
132 data: Dict[str, Any] = {}
133 for elt in geoloc_elt.elements():
134 if elt.uri != NS_GEOLOC:
135 log.warning(f"unmanaged geoloc element: {elt.toXml()}")
136 continue
137 try:
138 data_type = KEYS_TYPES[elt.name]
139 except KeyError:
140 log.warning(f"unknown geoloc element: {elt.toXml()}")
141 continue
142 try:
143 if data_type == "datetime":
144 data[elt.name] = utils.parse_xmpp_date(str(elt))
145 else:
146 data[elt.name] = data_type(str(elt))
147 except Exception as e:
148 log.warning(f"can't parse element: {elt.toXml()}")
149 continue
150
151 return data