Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0103.py @ 3922:0ff265725489
plugin XEP-0447: handle attachment and download:
- plugin XEP-0447 can now be used in message attachments and to retrieve an attachment
- plugin attach: `attachment` being processed is added to `extra` so the handler can inspect it
- plugin attach: `size` is added to attachment
- plugin download: a whole attachment dict is now used in `download` and
`file_download`/`file_download_complete`. `download_uri` can be used as a shortcut when
just a URI is used. In addition to URI scheme handler, whole attachment handlers can now
be registered with `register_download_handler`
- plugin XEP-0363: `file_http_upload` `XEP-0363_upload_size` triggers have been renamed to
`XEP-0363_upload_pre_slot` and is now using a dict with arguments, allowing for the size
but also the filename to be modified, which is necessary for encryption (filename may
be hidden from URL this way).
- plugin XEP-0446: fix wrong element name
- plugin XEP-0447: source handler can now be registered (`url-data` is registered by
default)
- plugin XEP-0447: source parsing has been put in a separated `parse_sources_elt` method,
as it may be useful to do it independently (notably with XEP-0448)
- plugin XEP-0447: parse received message and complete attachments when suitable
- plugin XEP-0447: can now be used with message attachments
- plugin XEP-0447: can now be used with attachments download
- renamed `options` arguments to `extra` for consistency
- some style change (progressive move from legacy camelCase to PEP8 snake_case)
- some typing
rel 379
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 06 Oct 2022 16:02:05 +0200 |
parents | eb0a77bea363 |
children | 524856bd7b19 |
rev | line source |
---|---|
3895
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org) |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # This program is free software: you can redistribute it and/or modify |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # it under the terms of the GNU Affero General Public License as published by |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # the Free Software Foundation, either version 3 of the License, or |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # (at your option) any later version. |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # This program is distributed in the hope that it will be useful, |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # GNU Affero General Public License for more details. |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # You should have received a copy of the GNU Affero General Public License |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 from typing import Dict, Any |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from twisted.words.xish import domish |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.constants import Const as C |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core.i18n import _ |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat.core.log import getLogger |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from sat.core import exceptions |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 log = getLogger(__name__) |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 PLUGIN_INFO = { |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 C.PI_NAME: "URL Address Information", |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 C.PI_IMPORT_NAME: "XEP-0103", |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 C.PI_TYPE: "XEP", |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 C.PI_MODES: C.PLUG_MODE_BOTH, |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 C.PI_PROTOCOLS: ["XEP-0103"], |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 C.PI_MAIN: "XEP_0103", |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 C.PI_HANDLER: "no", |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 C.PI_DESCRIPTION: _("""Implementation of XEP-0103 (URL Address Information)"""), |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 } |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 NS_URL_DATA = "http://jabber.org/protocol/url-data" |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 class XEP_0103: |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 namespace = NS_URL_DATA |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 def __init__(self, host): |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 log.info(_("XEP-0103 (URL Address Information) plugin initialization")) |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 host.registerNamespace("url-data", NS_URL_DATA) |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 def get_url_data_elt( |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 self, |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 url: str, |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 **kwargs |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 ) -> domish.Element: |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 """Generate the element describing the URL |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 @param url: URL to use |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 @param extra: extra metadata describing how to access the URL |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 @return: ``<url-data/>`` element |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 """ |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 url_data_elt = domish.Element((NS_URL_DATA, "url-data")) |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 url_data_elt["target"] = url |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 return url_data_elt |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 def parse_url_data_elt( |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 self, |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 url_data_elt: domish.Element |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 ) -> Dict[str, Any]: |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 """Parse <url-data/> element |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 @param url_data_elt: <url-data/> element |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 a parent element can also be used |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 @return: url-data data. It's a dict whose keys correspond to |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 [get_url_data_elt] parameters |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 @raise exceptions.NotFound: no <url-data/> element has been found |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 """ |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 if url_data_elt.name != "url-data": |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 try: |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 url_data_elt = next( |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 url_data_elt.elements(NS_URL_DATA, "url-data") |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 ) |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 except StopIteration: |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 raise exceptions.NotFound |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 try: |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 data: Dict[str, Any] = {"url": url_data_elt["target"]} |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 except KeyError: |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 raise ValueError(f'"target" attribute is missing: {url_data_elt.toXml}') |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 return data |