annotate sat/plugins/plugin_xep_0231.py @ 4043:9641ce286e07

core (main): if `keep_id` is set in `action_new`, use it as `action_id`
author Goffi <goffi@goffi.org>
date Mon, 15 May 2023 16:20:58 +0200
parents 524856bd7b19
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
4 # SAT plugin for Bit of Binary handling (XEP-0231)
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3333
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
3333
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
20 import base64
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
21 import time
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
22 from pathlib import Path
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
23 from functools import partial
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
24 from zope.interface import implementer
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
25 from twisted.python import failure
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
26 from twisted.words.protocols.jabber import xmlstream
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
27 from twisted.words.protocols.jabber import jid
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
28 from twisted.words.protocols.jabber import error as jabber_error
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
29 from twisted.internet import defer
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
30 from wokkel import disco, iwokkel
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
31 from sat.tools import xml_tools
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from sat.core.i18n import _
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from sat.core.constants import Const as C
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
34 from sat.core import exceptions
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
36
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 log = getLogger(__name__)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
41 C.PI_NAME: "Bits of Binary",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
42 C.PI_IMPORT_NAME: "XEP-0231",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
43 C.PI_TYPE: "XEP",
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
44 C.PI_MODES: C.PLUG_MODE_BOTH,
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
45 C.PI_PROTOCOLS: ["XEP-0231"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
46 C.PI_MAIN: "XEP_0231",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
47 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 C.PI_DESCRIPTION: _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 """Implementation of bits of binary (used for small images/files)"""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 ),
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 }
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
53 NS_BOB = "urn:xmpp:bob"
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
54 IQ_BOB_REQUEST = C.IQ_GET + '/data[@xmlns="' + NS_BOB + '"]'
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 class XEP_0231(object):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
59 log.info(_("plugin Bits of Binary initialization"))
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
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: 3479
diff changeset
61 host.register_namespace("bob", NS_BOB)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
62 host.trigger.add("xhtml_post_treat", self.xhtml_trigger)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
63 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
64 "bob_get_file",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 in_sign="sss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 out_sign="s",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
68 method=self._get_file,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
69 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 )
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
72 def dump_data(self, cache, data_elt, cid):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 """save file encoded in data_elt to cache
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
75 @param cache(memory.cache.Cache): cache to use to store the data
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @param data_elt(domish.Element): <data> as in XEP-0231
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 @param cid(unicode): content-id
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 @return(unicode): full path to dumped file
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
80 #  FIXME: is it needed to use a separate thread?
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 # probably not with the little data expected with BoB
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 max_age = int(data_elt["max-age"])
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
84 if max_age < 0:
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
85 raise ValueError
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 except (KeyError, ValueError):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
87 log.warning("invalid max-age found")
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 max_age = None
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
90 with cache.cache_data(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 PLUGIN_INFO[C.PI_IMPORT_NAME], cid, data_elt.getAttribute("type"), max_age
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 ) as f:
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
3333
ac9342f359e9 plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents: 3273
diff changeset
94 file_path = Path(f.name)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 f.write(base64.b64decode(str(data_elt)))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 return file_path
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
99 def get_handler(self, client):
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
100 return XEP_0231_handler(self)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
102 def _request_cb(self, iq_elt, cache, cid):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
103 for data_elt in iq_elt.elements(NS_BOB, "data"):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 if data_elt.getAttribute("cid") == cid:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
105 file_path = self.dump_data(cache, data_elt, cid)
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
106 return file_path
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
107
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
108 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
109 "invalid data stanza received, requested cid was not found:\n{iq_elt}\nrequested cid: {cid}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
110 iq_elt=iq_elt, cid=cid
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
112 )
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
113 raise failure.Failure(exceptions.DataError("missing data"))
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
114
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
115 def _request_eb(self, failure_):
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
116 """Log the error and continue errback chain"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
117 log.warning("Can't get requested data:\n{reason}".format(reason=failure_))
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
118 return failure_
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
119
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
120 def request_data(self, client, to_jid, cid, cache=None):
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
121 """Request data if we don't have it in cache
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
123 @param to_jid(jid.JID): jid to request the data to
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
124 @param cid(unicode): content id
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
125 @param cache(memory.cache.Cache, None): cache to use
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
126 client.cache will be used if None
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
127 @return D(unicode): path to file with data
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
128 """
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
129 if cache is None:
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
130 cache = client.cache
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
131 iq_elt = client.IQ("get")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132 iq_elt["to"] = to_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 data_elt = iq_elt.addElement((NS_BOB, "data"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 data_elt["cid"] = cid
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
135 d = iq_elt.send()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
136 d.addCallback(self._request_cb, cache, cid)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
137 d.addErrback(self._request_eb)
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
138 return d
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
139
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
140 def _set_img_elt_src(self, path, img_elt):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
141 img_elt["src"] = "file://{}".format(path)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
143 def xhtml_trigger(self, client, message_elt, body_elt, lang, treat_d):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
144 for img_elt in xml_tools.find_all(body_elt, C.NS_XHTML, "img"):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
145 source = img_elt.getAttribute("src", "")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
146 if source.startswith("cid:"):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 cid = source[4:]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
148 file_path = client.cache.get_file_path(cid)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 if file_path is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
150 #  image is in cache, we change the url
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
151 img_elt["src"] = "file://{}".format(file_path)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 continue
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 # image is not in cache, is it given locally?
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
155 for data_elt in message_elt.elements(NS_BOB, "data"):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156 if data_elt.getAttribute("cid") == cid:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
157 file_path = self.dump_data(client.cache, data_elt, cid)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
158 img_elt["src"] = "file://{}".format(file_path)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 break
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 # cid not found locally, we need to request it
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 # so we use the deferred
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
163 d = self.request_data(client, jid.JID(message_elt["from"]), cid)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
164 d.addCallback(partial(self._set_img_elt_src, img_elt=img_elt))
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
165 treat_d.addCallback(lambda __: d)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
167 def on_component_request(self, iq_elt, client):
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
168 """cache data is retrieve from common cache for components"""
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
169 # FIXME: this is a security/privacy issue as no access check is done
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
170 # but this is mitigated by the fact that the cid must be known.
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
171 # An access check should be implemented though.
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
172
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
173 iq_elt.handled = True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
174 data_elt = next(iq_elt.elements(NS_BOB, "data"))
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
175 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
176 cid = data_elt["cid"]
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
177 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
178 error_elt = jabber_error.StanzaError("not-acceptable").toResponse(iq_elt)
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
179 client.send(error_elt)
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
180 return
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
181
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
182 metadata = self.host.common_cache.get_metadata(cid)
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
183 if metadata is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
184 error_elt = jabber_error.StanzaError("item-not-found").toResponse(iq_elt)
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
185 client.send(error_elt)
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
186 return
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
187
3273
4230aaeab9a7 plugin XEP-0231, XEP-0264: fixed some remaining encoding issue following Python 3 port
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
188 with open(metadata["path"], 'rb') as f:
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
189 data = f.read()
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
190
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
191 result_elt = xmlstream.toResponse(iq_elt, "result")
3273
4230aaeab9a7 plugin XEP-0231, XEP-0264: fixed some remaining encoding issue following Python 3 port
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
192 data_elt = result_elt.addElement(
4230aaeab9a7 plugin XEP-0231, XEP-0264: fixed some remaining encoding issue following Python 3 port
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
193 (NS_BOB, "data"), content=base64.b64encode(data).decode())
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
194 data_elt["cid"] = cid
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
195 data_elt["type"] = metadata["mime_type"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
196 data_elt["max-age"] = str(int(max(0, metadata["eol"] - time.time())))
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
197 client.send(result_elt)
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
198
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
199 def _get_file(self, peer_jid_s, cid, profile):
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
200 peer_jid = jid.JID(peer_jid_s)
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
201 assert cid
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
202 client = self.host.get_client(profile)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
203 d = self.get_file(client, peer_jid, cid)
3273
4230aaeab9a7 plugin XEP-0231, XEP-0264: fixed some remaining encoding issue following Python 3 port
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
204 d.addCallback(lambda path: str(path))
4230aaeab9a7 plugin XEP-0231, XEP-0264: fixed some remaining encoding issue following Python 3 port
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
205 return d
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
206
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
207 def get_file(self, client, peer_jid, cid, parent_elt=None):
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
208 """Retrieve a file from it's content-id
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
209
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
210 @param peer_jid(jid.JID): jid of the entity offering the data
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
211 @param cid(unicode): content-id of file data
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
212 @param parent_elt(domish.Element, None): if file is not in cache,
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
213 data will be looked after in children of this elements.
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
214 None to ignore
3273
4230aaeab9a7 plugin XEP-0231, XEP-0264: fixed some remaining encoding issue following Python 3 port
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
215 @return D(Path): path to cached data
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
216 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
217 file_path = client.cache.get_file_path(cid)
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
218 if file_path is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
219 #  file is in cache
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
220 return defer.succeed(file_path)
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
221 else:
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
222 # file not in cache, is it given locally?
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
223 if parent_elt is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
224 for data_elt in parent_elt.elements(NS_BOB, "data"):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
225 if data_elt.getAttribute("cid") == cid:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
226 return defer.succeed(self.dump_data(client.cache, data_elt, cid))
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
227
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
228 # cid not found locally, we need to request it
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
229 # so we use the deferred
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
230 return self.request_data(client, peer_jid, cid)
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
231
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
233 @implementer(iwokkel.IDisco)
2511
20a5e7db0609 plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
234 class XEP_0231_handler(xmlstream.XMPPHandler):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
235
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
236 def __init__(self, plugin_parent):
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
237 self.plugin_parent = plugin_parent
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
238 self.host = plugin_parent.host
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
239
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
240 def connectionInitialized(self):
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
241 if self.parent.is_component:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
242 self.xmlstream.addObserver(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
243 IQ_BOB_REQUEST, self.plugin_parent.on_component_request, client=self.parent
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
244 )
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2511
diff changeset
245
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
246 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 return [disco.DiscoFeature(NS_BOB)]
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
248
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
249 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 return []