annotate src/plugins/plugin_xep_0231.py @ 2511:20a5e7db0609

plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
author Goffi <goffi@goffi.org>
date Fri, 02 Mar 2018 17:45:23 +0100
parents 0046283a285d
children 95c31756944c
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
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Jingle File Transfer (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
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 functools import partial
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import base64
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
36 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
37 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
38 C.PI_TYPE: "XEP",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
39 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
40 C.PI_DEPENDENCIES: ["XEP-0071"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
41 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
42 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
43 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
44 }
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 NS_BOB = u'urn:xmpp:bob'
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 class XEP_0231(object):
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 def __init__(self, host):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 log.info(_(u"plugin Bits of Binary initialization"))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.host = host
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 host.trigger.add("xhtml_post_treat", self.XHTMLTrigger)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
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
56 def dumpData(self, cache, data_elt, cid):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 """save file encoded in data_elt to cache
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
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
59 @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
60 @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
61 @param cid(unicode): content-id
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 @return(unicode): full path to dumped file
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 """
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 # 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
65 # 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
66 try:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 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
68 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
69 raise ValueError
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 except (KeyError, ValueError):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 log.warning(u'invalid max-age found')
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 max_age = None
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 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
75 PLUGIN_INFO[C.PI_IMPORT_NAME],
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 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
77 data_elt.getAttribute('type'),
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 max_age) as f:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 file_path = f.name
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 f.write(base64.b64decode(str(data_elt)))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 return file_path
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2110
diff changeset
85 def getHandler(self, client):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 return XEP_0231_handler()
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
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
88 def _requestCb(self, iq_elt, cache, cid):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 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
90 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
91 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
92 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
93
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
94 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
95 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
96 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
97 ))
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
98 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
99
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 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
101 """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
102 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
103 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
104
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 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
106 """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
107
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
108 @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
109 @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
110 @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
111 client.cache will be used if 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
112 """
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 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
114 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
115 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
116 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
117 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
118 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
119 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
120 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
121 d.addErrback(self._requestEb)
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 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
124 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
125
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 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
127 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
128 source = img_elt.getAttribute(u'src','')
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 if source.startswith(u'cid:'):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 cid = source[4:]
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 file_path = client.cache.getFilePath(cid)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 if file_path is not None:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 # image is in cache, we change change the url
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 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
135 continue
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 # 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
138 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
139 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
140 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
141 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
142 break
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 # 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
145 # 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
146 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
147 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
148 treat_d.addCallback(lambda dummy: d)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150
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 class XEP_0231_handler(xmlstream.XMPPHandler):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 implements(iwokkel.IDisco)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 return [disco.DiscoFeature(NS_BOB)]
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 return []