annotate src/plugins/plugin_xep_0231.py @ 2548:54b3853b55c0

plugin uri finder: plugin to find URIs in well-known locations
author Goffi <goffi@goffi.org>
date Sat, 31 Mar 2018 17:20:14 +0200
parents a201194fc461
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
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)
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 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
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.tools import xml_tools
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from wokkel import disco, iwokkel
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from zope.interface import implements
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
28 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
29 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
30 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
31 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
32 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
33 from functools import partial
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 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
35 import time
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
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 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
39 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
40 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
41 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
42 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
43 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
44 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
45 C.PI_HANDLER: "yes",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
46 C.PI_DESCRIPTION: _("""Implementation of bits of binary (used for small images/files)""")
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 }
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 NS_BOB = u'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
50 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
51
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 class XEP_0231(object):
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 def __init__(self, host):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 log.info(_(u"plugin Bits of Binary initialization"))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.host = host
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
58 host.registerNamespace('bob', NS_BOB)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 host.trigger.add("xhtml_post_treat", self.XHTMLTrigger)
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
60 host.bridge.addMethod("bobGetFile", ".plugin",
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
61 in_sign='sss', out_sign='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
62 method=self._getFile,
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
63 async=True)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
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
65 def dumpData(self, cache, data_elt, cid):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 """save file encoded in data_elt to cache
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
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
68 @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
69 @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
70 @param cid(unicode): content-id
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 @return(unicode): full path to dumped file
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 """
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 # FIXME: is it needed to use a separate thread?
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 # 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
75 try:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 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
77 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
78 raise ValueError
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 except (KeyError, ValueError):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 log.warning(u'invalid max-age found')
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 max_age = None
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
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 with cache.cacheData(
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 PLUGIN_INFO[C.PI_IMPORT_NAME],
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 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
86 data_elt.getAttribute('type'),
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 max_age) as f:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 file_path = f.name
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 f.write(base64.b64decode(str(data_elt)))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 return file_path
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2110
diff changeset
94 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
95 return XEP_0231_handler(self)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96
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
97 def _requestCb(self, iq_elt, cache, cid):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 for data_elt in iq_elt.elements(NS_BOB, u'data'):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 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
100 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
101 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
102
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
103 log.warning(u"invalid data stanza received, requested cid was not found:\n{iq_elt}\nrequested cid: {cid}".format(
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 iq_elt = iq_elt,
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 cid = 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
106 ))
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 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
108
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
109 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
110 """Log the error and continue errback chain"""
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
111 log.warning(u"Can't get requested data:\n{reason}".format(reason=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
112 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
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 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
115 """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
116
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 @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
118 @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
119 @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
120 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
121 @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
122 """
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 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
124 cache = client.cache
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 iq_elt = client.IQ('get')
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 iq_elt['to'] = to_jid.full()
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 data_elt = iq_elt.addElement((NS_BOB, '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
128 data_elt['cid'] = 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
129 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
130 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
131 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
132 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
133
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 def _setImgEltSrc(self, path, img_elt):
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 img_elt[u'src'] = u'file://{}'.format(path)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 def XHTMLTrigger(self, client, message_elt, body_elt, lang, treat_d):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 for img_elt in xml_tools.findAll(body_elt, C.NS_XHTML, u'img'):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 source = img_elt.getAttribute(u'src','')
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 if source.startswith(u'cid:'):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 cid = source[4:]
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 file_path = client.cache.getFilePath(cid)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 if file_path is not 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
144 # image is in cache, we change the url
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 img_elt[u'src'] = u'file://{}'.format(file_path)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 continue
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 # image is not in cache, is it given locally?
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 for data_elt in message_elt.elements(NS_BOB, u'data'):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 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
151 file_path = self.dumpData(client.cache, data_elt, cid)
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 img_elt[u'src'] = u'file://{}'.format(file_path)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 break
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 # 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
156 # so we use the deferred
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
157 d = self.requestData(client, jid.JID(message_elt['from']), 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
158 d.addCallback(partial(self._setImgEltSrc, img_elt=img_elt))
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 treat_d.addCallback(lambda dummy: d)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160
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
161 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
162 """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
163 # 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
164 # 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
165 # 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
166
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 iq_elt.handled = True
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 data_elt = next(iq_elt.elements(NS_BOB, '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
169 try:
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 cid = data_elt[u'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
171 except KeyError:
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 error_elt = jabber_error.StanzaError('not-acceptable').toResponse(iq_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
173 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
174 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
175
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 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
177 if metadata is 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
178 error_elt = jabber_error.StanzaError('item-not-found').toResponse(iq_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 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
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 with open(metadata['path']) as f:
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 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
184
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 result_elt = xmlstream.toResponse(iq_elt, 'result')
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 data_elt = result_elt.addElement((NS_BOB, 'data'), content = data.encode('base64'))
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 data_elt[u'cid'] = 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
188 data_elt[u'type'] = metadata[u'mime_type']
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_elt[u'max-age'] = unicode(int(max(0, metadata['eol'] - time.time())))
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 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
191
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
192 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
193 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
194 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
195 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
196 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
197
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 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
199 """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
200
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 @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
202 @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
203 @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
204 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
205 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
206 @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
207 """
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 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
209 if file_path is not 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
210 # file is 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
211 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
212 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
213 # 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
214 if parent_elt is not 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
215 for data_elt in parent_elt.elements(NS_BOB, u'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
216 if data_elt.getAttribute('cid') == 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
217 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
218
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 # 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
220 # 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
221 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
222
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223
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
224 class XEP_0231_handler(xmlstream.XMPPHandler):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 implements(iwokkel.IDisco)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
226
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 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
228 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
229 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
230
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 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
232 if self.parent.is_component:
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.xmlstream.addObserver(IQ_BOB_REQUEST, self.plugin_parent.onComponentRequest, client=self.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
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 return [disco.DiscoFeature(NS_BOB)]
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
237
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 return []