annotate sat/plugins/plugin_xep_0231.py @ 3145:3f414c897e70

core (constants): don't parse `sat.conf` from current dir anymore + parse root_dir last
author Goffi <goffi@goffi.org>
date Wed, 29 Jan 2020 19:12:46 +0100
parents 559a625a236b
children 4230aaeab9a7
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)
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
5 # Copyright (C) 2009-2020 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
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 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
22 from sat.core import exceptions
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
24
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 log = getLogger(__name__)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.tools import xml_tools
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from wokkel import disco, iwokkel
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
28 from zope.interface import implementer
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
29 from twisted.python import 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
30 from twisted.words.protocols.jabber import xmlstream
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
31 from twisted.words.protocols.jabber import jid
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
32 from twisted.words.protocols.jabber import error as jabber_error
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
33 from twisted.internet import defer
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 functools import partial
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 import base64
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
36 import time
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
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 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
40 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
41 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
42 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
43 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
44 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
45 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
46 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47 C.PI_DESCRIPTION: _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 """Implementation of bits of binary (used for small images/files)"""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 ),
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 }
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
52 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
53 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
54
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 class XEP_0231(object):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
58 log.info(_("plugin Bits of Binary initialization"))
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.host = host
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 host.registerNamespace("bob", NS_BOB)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 host.trigger.add("xhtml_post_treat", self.XHTMLTrigger)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 "bobGetFile",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 in_sign="sss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 method=self._getFile,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
68 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 )
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70
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
71 def dumpData(self, cache, data_elt, cid):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 """save file encoded in data_elt to cache
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73
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
74 @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
75 @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
76 @param cid(unicode): content-id
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 @return(unicode): full path to dumped file
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 #  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
80 # 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
81 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 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
83 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
84 raise ValueError
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 except (KeyError, ValueError):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
86 log.warning("invalid max-age found")
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 max_age = None
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
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
89 with cache.cacheData(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 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
91 ) as f:
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 file_path = f.name
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 f.write(base64.b64decode(str(data_elt)))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 return file_path
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2110
diff changeset
98 def getHandler(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
99 return XEP_0231_handler(self)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
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
101 def _requestCb(self, iq_elt, cache, cid):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
102 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
103 if data_elt.getAttribute("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
104 file_path = self.dumpData(cache, data_elt, cid)
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
105 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
106
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
107 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
108 "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
109 iq_elt=iq_elt, cid=cid
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
110 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111 )
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
112 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
113
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 def _requestEb(self, 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
115 """Log the error and continue errback chain"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
116 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
117 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
118
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 def requestData(self, client, to_jid, cid, cache=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
120 """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
121
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
122 @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
123 @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
124 @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
125 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
126 @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
127 """
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 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
129 cache = client.cache
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 iq_elt = client.IQ("get")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
131 iq_elt["to"] = to_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132 data_elt = iq_elt.addElement((NS_BOB, "data"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 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
134 d = iq_elt.send()
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.addCallback(self._requestCb, cache, cid)
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
136 d.addErrback(self._requestEb)
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
137 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
138
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 def _setImgEltSrc(self, path, img_elt):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
140 img_elt["src"] = "file://{}".format(path)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 def XHTMLTrigger(self, client, message_elt, body_elt, lang, treat_d):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
143 for img_elt in xml_tools.findAll(body_elt, C.NS_XHTML, "img"):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
144 source = img_elt.getAttribute("src", "")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
145 if source.startswith("cid:"):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 cid = source[4:]
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 file_path = client.cache.getFilePath(cid)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if file_path is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
149 #  image is in cache, we change the url
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
150 img_elt["src"] = "file://{}".format(file_path)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 continue
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 # image is not in cache, is it given locally?
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
154 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
155 if data_elt.getAttribute("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
156 file_path = self.dumpData(client.cache, data_elt, cid)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
157 img_elt["src"] = "file://{}".format(file_path)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 break
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 # 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
161 # so we use the deferred
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
162 d = self.requestData(client, jid.JID(message_elt["from"]), 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
163 d.addCallback(partial(self._setImgEltSrc, 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
164 treat_d.addCallback(lambda __: d)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165
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
166 def onComponentRequest(self, iq_elt, client):
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
167 """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
168 # 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
169 # 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
170 # 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
171
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 iq_elt.handled = True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
173 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
174 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
175 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
176 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
177 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
178 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
179 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
180
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 metadata = self.host.common_cache.getMetadata(cid)
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
182 if metadata is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
183 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
184 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
185 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
186
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
187 with open(metadata["path"]) 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
188 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
189
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
190 result_elt = xmlstream.toResponse(iq_elt, "result")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
191 data_elt = result_elt.addElement((NS_BOB, "data"), content=data.encode("base64"))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
192 data_elt["cid"] = cid
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
193 data_elt["type"] = metadata["mime_type"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
194 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
195 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
196
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 def _getFile(self, peer_jid_s, cid, profile):
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 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
199 assert cid
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 client = self.host.getClient(profile)
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 return self.getFile(client, peer_jid, cid)
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
202
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
203 def getFile(self, client, peer_jid, cid, parent_elt=None):
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
204 """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
205
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 @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
207 @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
208 @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
209 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
210 None to ignore
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 @return D(unicode): path to cached 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 """
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 file_path = client.cache.getFilePath(cid)
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 if file_path is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
215 #  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
216 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
217 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
218 # 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
219 if parent_elt is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
220 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
221 if data_elt.getAttribute("cid") == 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
222 return defer.succeed(self.dumpData(client.cache, data_elt, cid))
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
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
224 # 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
225 # so we use the deferred
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
226 return self.requestData(client, peer_jid, cid)
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
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
229 @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
230 class XEP_0231_handler(xmlstream.XMPPHandler):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231
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
232 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
233 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
234 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
235
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 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
237 if self.parent.is_component:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
238 self.xmlstream.addObserver(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
239 IQ_BOB_REQUEST, self.plugin_parent.onComponentRequest, client=self.parent
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
240 )
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
241
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
242 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 return [disco.DiscoFeature(NS_BOB)]
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
245 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 return []