Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0103.py @ 3895:eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
rel 379
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 21 Sep 2022 22:26:48 +0200 |
parents | |
children | 524856bd7b19 |
comparison
equal
deleted
inserted
replaced
3894:ed470361ea2e | 3895:eb0a77bea363 |
---|---|
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 sat.core.constants import Const as C | |
23 from sat.core.i18n import _ | |
24 from sat.core.log import getLogger | |
25 from sat.core import exceptions | |
26 | |
27 log = getLogger(__name__) | |
28 | |
29 | |
30 PLUGIN_INFO = { | |
31 C.PI_NAME: "URL Address Information", | |
32 C.PI_IMPORT_NAME: "XEP-0103", | |
33 C.PI_TYPE: "XEP", | |
34 C.PI_MODES: C.PLUG_MODE_BOTH, | |
35 C.PI_PROTOCOLS: ["XEP-0103"], | |
36 C.PI_MAIN: "XEP_0103", | |
37 C.PI_HANDLER: "no", | |
38 C.PI_DESCRIPTION: _("""Implementation of XEP-0103 (URL Address Information)"""), | |
39 } | |
40 | |
41 NS_URL_DATA = "http://jabber.org/protocol/url-data" | |
42 | |
43 | |
44 class XEP_0103: | |
45 namespace = NS_URL_DATA | |
46 | |
47 def __init__(self, host): | |
48 log.info(_("XEP-0103 (URL Address Information) plugin initialization")) | |
49 host.registerNamespace("url-data", NS_URL_DATA) | |
50 | |
51 def get_url_data_elt( | |
52 self, | |
53 url: str, | |
54 **kwargs | |
55 ) -> domish.Element: | |
56 """Generate the element describing the URL | |
57 | |
58 @param url: URL to use | |
59 @param extra: extra metadata describing how to access the URL | |
60 @return: ``<url-data/>`` element | |
61 """ | |
62 url_data_elt = domish.Element((NS_URL_DATA, "url-data")) | |
63 url_data_elt["target"] = url | |
64 return url_data_elt | |
65 | |
66 def parse_url_data_elt( | |
67 self, | |
68 url_data_elt: domish.Element | |
69 ) -> Dict[str, Any]: | |
70 """Parse <url-data/> element | |
71 | |
72 @param url_data_elt: <url-data/> element | |
73 a parent element can also be used | |
74 @return: url-data data. It's a dict whose keys correspond to | |
75 [get_url_data_elt] parameters | |
76 @raise exceptions.NotFound: no <url-data/> element has been found | |
77 """ | |
78 if url_data_elt.name != "url-data": | |
79 try: | |
80 url_data_elt = next( | |
81 url_data_elt.elements(NS_URL_DATA, "url-data") | |
82 ) | |
83 except StopIteration: | |
84 raise exceptions.NotFound | |
85 try: | |
86 data: Dict[str, Any] = {"url": url_data_elt["target"]} | |
87 except KeyError: | |
88 raise ValueError(f'"target" attribute is missing: {url_data_elt.toXml}') | |
89 | |
90 return data |