annotate src/plugins/plugin_xep_0231.py @ 2382:a49a19f06e38

plugin import ticket, import ticket bugzilla: some normalization in values: normalization (i.e. recommandations for importer to have similar values) has been done for some values: - "status" is now one of "queued", "started", "review", "closed" - "priority" and "severity" must be stripped and lower case
author Goffi <goffi@goffi.org>
date Mon, 16 Oct 2017 07:39:54 +0200
parents 33c8c4973743
children 8b37a62336c3
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)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
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
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = getLogger(__name__)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.tools import xml_tools
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import disco, iwokkel
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from zope.interface import implements
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 import base64
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
32 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
33 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
34 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
35 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
36 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
37 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
38 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
39 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
40 }
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 NS_BOB = u'urn:xmpp:bob'
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
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 class XEP_0231(object):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def __init__(self, host):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 log.info(_(u"plugin Bits of Binary initialization"))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.host = host
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 host.trigger.add("xhtml_post_treat", self.XHTMLTrigger)
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 def dumpData(self, client, data_elt, cid):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 """save file encoded in data_elt to cache
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 @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
56 @param cid(unicode): content-id
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 @return(unicode): full path to dumped file
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 """
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 # 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
60 # 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
61 mime_type = data_elt.getAttribute('type','')
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 try:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 max_age = int(data_elt['max-age'])
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 except (KeyError, ValueError):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 log.warning(u'invalid max-age found')
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 max_age = None
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 with client.cache.cacheData(
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 PLUGIN_INFO['import_name'],
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 cid,
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 mime_type,
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 max_age) as f:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 file_path = f.name
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 f.write(base64.b64decode(str(data_elt)))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 return file_path
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2110
diff changeset
80 def getHandler(self, client):
2110
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 return XEP_0231_handler()
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 def _dataCb(self, iq_elt, client, img_elt, cid):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 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
85 if data_elt.getAttribute('cid') == cid:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 file_path = self.dumpData(client, data_elt, cid)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 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
88 break
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 log.warning(u"invalid data stanza received, requested cid was not found:\n{iq_elt}\nrequested cid: {cid}".format(
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 iq_elt = iq_elt,
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 cid = cid
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 ))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 def _dataEb(self, iq_elt):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 log.warning(u"Can't get requested data:\n{iq_elt}".format(iq_elt=iq_elt))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 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
99 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
100 source = img_elt.getAttribute(u'src','')
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 if source.startswith(u'cid:'):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 cid = source[4:]
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 file_path = client.cache.getFilePath(cid)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 if file_path is not None:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 # 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
106 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
107 continue
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 # 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
110 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
111 if data_elt.getAttribute('cid') == cid:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 file_path = self.dumpData(data_elt, cid)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 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
114 break
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 else:
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 # 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
117 # so we use the deferred
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 iq_elt = client.IQ('get')
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 iq_elt['to'] = message_elt['from']
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 data_elt = iq_elt.addElement((NS_BOB, 'data'))
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 data_elt['cid'] = cid
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 d = iq_elt.send()
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 d.addCallback(self._dataCb, client, img_elt, cid)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 d.addErrback(self._dataEb)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 treat_d.addCallback(lambda dummy: d)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 class XEP_0231_handler(XMPPHandler):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 implements(iwokkel.IDisco)
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 return [disco.DiscoFeature(NS_BOB)]
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
2d633b3c923d plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 return []