annotate src/plugins/plugin_xep_0234.py @ 2512:025afb04c10b

plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
author Goffi <goffi@goffi.org>
date Fri, 02 Mar 2018 17:53:19 +0100
parents 7ad5f2c4e34a
children a19b2c43e719
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Jingle File Transfer (XEP-0234)
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
20 from sat.core.i18n import _
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = getLogger(__name__)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core import exceptions
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import disco, iwokkel
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from zope.interface import implements
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.tools import utils
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
28 from sat.tools import stream
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import os.path
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from twisted.words.xish import domish
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from twisted.words.protocols.jabber import jid
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from twisted.python import failure
1566
ec3848916ee8 plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents: 1557
diff changeset
33 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
1571
c668081eba1c plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
34 from twisted.internet import defer
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
35 from twisted.internet import reactor
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
36 from twisted.internet import error as internet_error
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
37 from collections import namedtuple
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
38 import regex
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
39 import mimetypes
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
42 NS_JINGLE_FT = 'urn:xmpp:jingle:apps:file-transfer:5'
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
45 C.PI_NAME: "Jingle File Transfer",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
46 C.PI_IMPORT_NAME: "XEP-0234",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
47 C.PI_TYPE: "XEP",
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
48 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
49 C.PI_PROTOCOLS: ["XEP-0234"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
50 C.PI_DEPENDENCIES: ["XEP-0166", "XEP-0300", "FILE"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
51 C.PI_MAIN: "XEP_0234",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
52 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
53 C.PI_DESCRIPTION: _("""Implementation of Jingle File Transfer""")
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 }
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
56 EXTRA_ALLOWED = {u'path', u'namespace', u'file_desc', u'file_hash'}
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
57 Range = namedtuple('Range', ('offset', 'length'))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
58
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 class XEP_0234(object):
1571
c668081eba1c plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
61 # TODO: assure everything is closed when file is sent or session terminate is received
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
62 # TODO: call self._f.unregister when unloading order will be managing (i.e. when dependencies will be unloaded at the end)
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
63 Range = Range # we copy the class here, so it can be used by other plugins
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def __init__(self, host):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 log.info(_("plugin Jingle File Transfer initialization"))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.host = host
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
68 host.registerNamespace('jingle-ft', NS_JINGLE_FT)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self._j = host.plugins["XEP-0166"] # shortcut to access jingle
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self._j.registerApplication(NS_JINGLE_FT, self)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self._f = host.plugins["FILE"]
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
72 self._f.register(NS_JINGLE_FT, self.fileJingleSend, priority = 10000, method_name=u"Jingle")
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
73 self._hash = self.host.plugins["XEP-0300"]
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
74 host.bridge.addMethod("fileJingleSend", ".plugin", in_sign='ssssa{ss}s', out_sign='', method=self._fileJingleSend, async=True)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
75 host.bridge.addMethod("fileJingleRequest", ".plugin", in_sign='sssssa{ss}s', out_sign='s', method=self._fileJingleRequest, async=True)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
77 def getHandler(self, client):
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 return XEP_0234_handler()
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
80 def getProgressId(self, session, content_name):
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
81 """Return a unique progress ID
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
82
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
83 @param session(dict): jingle session
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
84 @param content_name(unicode): name of the content
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
85 @return (unicode): unique progress id
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
86 """
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
87 return u'{}_{}'.format(session['id'], content_name)
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
88
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
89 # generic methods
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
90
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
91 def buildFileElement(self, name, file_hash=None, hash_algo=None, size=None, mime_type=None, desc=None,
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
92 modified=None, transfer_range=None, path=None, namespace=None, file_elt=None, **kwargs):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
93 """Generate a <file> element with available metadata
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
94
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
95 @param file_hash(unicode, None): hash of the file
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
96 empty string to set <hash-used/> element
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
97 @param hash_algo(unicode, None): hash algorithm used
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
98 if file_hash is None and hash_algo is set, a <hash-used/> element will be generated
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
99 @param transfer_range(Range, None): where transfer must start/stop
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
100 @param modified(int, unicode, None): date of last modification
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
101 0 to use current date
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
102 int to use an unix timestamp
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
103 else must be an unicode string which will be used as it (it must be an XMPP time)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
104 @param file_elt(domish.Element, None): element to use
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
105 None to create a new one
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
106 @param **kwargs: data for plugin extension (ignored by default)
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
107 @return (domish.Element): generated element
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
108 @trigger XEP-0234_buildFileElement(file_elt, extra_args): can be used to extend elements to add
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
109 """
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
110 if file_elt is None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
111 file_elt = domish.Element((NS_JINGLE_FT, u'file'))
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
112 for name, value in ((u'name', name), (u'size', size), ('media-type', mime_type),
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
113 (u'desc', desc), (u'path', path), (u'namespace', namespace)):
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
114 if value is not None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
115 file_elt.addElement(name, content=unicode(value))
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
116
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
117 if modified is not None:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
118 if isinstance(modified, int):
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
119 file_elt.addElement(u'date', utils.xmpp_date(modified or None))
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
120 else:
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
121 file_elt.addElement(u'date', modified)
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
122 elif 'created' in kwargs:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
123 file_elt.addElement(u'date', utils.xmpp_date(kwargs.pop('created')))
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
124
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
125 range_elt = file_elt.addElement(u'range')
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
126 if transfer_range is not None:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
127 if transfer_range.offset is not None:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
128 range_elt[u'offset'] = transfer_range.offset
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
129 if transfer_range.length is not None:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
130 range_elt[u'length'] = transfer_range.length
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
131 if file_hash is not None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
132 if not file_hash:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
133 file_elt.addChild(self._hash.buildHashUsedElt())
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
134 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
135 file_elt.addChild(self._hash.buildHashElt(file_hash, hash_algo))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
136 elif hash_algo is not None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
137 file_elt.addChild(self._hash.buildHashUsedElt(hash_algo))
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
138 self.host.trigger.point(u'XEP-0234_buildFileElement', file_elt, extra_args=kwargs)
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
139 if kwargs:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
140 for kw in kwargs:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
141 log.debug('ignored keyword: {}'.format(kw))
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
142 return file_elt
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
143
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
144 def buildFileElementFromDict(self, file_data, **kwargs):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
145 """like buildFileElement but get values from a file_data dict
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
146
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
147 @param file_data(dict): metadata to use
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
148 @param **kwargs: data to override
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
149 """
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
150 if kwargs:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
151 file_data = file_data.copy()
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
152 file_data.update(kwargs)
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
153 return self. buildFileElement(**file_data)
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
154
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
155
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
156 def parseFileElement(self, file_elt, file_data=None, given=False, parent_elt=None, keep_empty_range=False):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
157 """Parse a <file> element and file dictionary accordingly
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
158
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
159 @param file_data(dict, None): dict where the data will be set
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
160 following keys will be set (and overwritten if they already exist):
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
161 name, file_hash, hash_algo, size, mime_type, desc, path, namespace, range
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
162 if None, a new dict is created
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
163 @param given(bool): if True, prefix hash key with "given_"
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
164 @param parent_elt(domish.Element, None): parent of the file element
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
165 if set, file_elt must not be set
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
166 @param keep_empty_range(bool): if True, keep empty range (i.e. range when offset and length are None)
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
167 empty range are useful to know if a peer_jid can handle range
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
168 @return (dict): file_data
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
169 @trigger XEP-0234_parseFileElement(file_elt, file_data): can be used to parse new elements
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
170 @raise exceptions.NotFound: there is not <file> element in parent_elt
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
171 @raise exceptions.DataError: if file_elt uri is not NS_JINGLE_FT
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
172 """
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
173 if parent_elt is not None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
174 if file_elt is not None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
175 raise exceptions.InternalError(u'file_elt must be None if parent_elt is set')
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
176 try:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
177 file_elt = next(parent_elt.elements(NS_JINGLE_FT, u'file'))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
178 except StopIteration:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
179 raise exceptions.NotFound()
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
180 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
181 if not file_elt or file_elt.uri != NS_JINGLE_FT:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
182 raise exceptions.DataError(u'invalid <file> element: {stanza}'.format(stanza = file_elt.toXml()))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
183
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
184 if file_data is None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
185 file_data = {}
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
186
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
187 for name in (u'name', u'desc', u'path', u'namespace'):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
188 try:
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
189 file_data[name] = unicode(next(file_elt.elements(NS_JINGLE_FT, name)))
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
190 except StopIteration:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
191 pass
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
192
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
193
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
194 name = file_data.get(u'name')
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
195 if name == u'..':
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
196 # we don't want to go to parent dir when joining to a path
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
197 name = u'--'
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
198 file_data[u'name'] = name
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
199 elif name is not None and u'/' in name or u'\\' in name:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
200 file_data[u'name'] = regex.pathEscape(name)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
201
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
202 try:
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
203 file_data[u'mime_type'] = unicode(next(file_elt.elements(NS_JINGLE_FT, u'media-type')))
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
204 except StopIteration:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
205 pass
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
206
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
207 try:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
208 file_data[u'size'] = int(unicode(next(file_elt.elements(NS_JINGLE_FT, u'size'))))
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
209 except StopIteration:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
210 pass
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
211
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
212 try:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
213 file_data[u'modified'] = utils.date_parse(next(file_elt.elements(NS_JINGLE_FT, u'date')))
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
214 except StopIteration:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
215 pass
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
216
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
217 try:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
218 range_elt = file_elt.elements(NS_JINGLE_FT, u'range').next()
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
219 except StopIteration:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
220 pass
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
221 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
222 offset = range_elt.getAttribute('offset')
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
223 length = range_elt.getAttribute('length')
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
224 if offset or length or keep_empty_range:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
225 file_data[u'transfer_range'] = Range(offset=offset, length=length)
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
226
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
227 prefix = u'given_' if given else u''
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
228 hash_algo_key, hash_key = u'hash_algo', prefix + u'file_hash'
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
229 try:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
230 file_data[hash_algo_key], file_data[hash_key] = self._hash.parseHashElt(file_elt)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
231 except exceptions.NotFound:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
232 pass
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
233
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
234 self.host.trigger.point(u'XEP-0234_parseFileElement', file_elt, file_data)
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
235
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
236 return file_data
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
237
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
238 # bridge methods
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
239
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
240 def _fileJingleSend(self, peer_jid, filepath, name="", file_desc="", extra=None, profile=C.PROF_KEY_NONE):
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
241 client = self.host.getClient(profile)
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
242 return self.fileJingleSend(client, jid.JID(peer_jid), filepath, name or None, file_desc or None, extra or None)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
243
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
244 @defer.inlineCallbacks
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
245 def fileJingleSend(self, client, peer_jid, filepath, name, file_desc=None, extra=None):
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
246 """Send a file using jingle file transfer
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
247
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
248 @param peer_jid(jid.JID): destinee jid
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
249 @param filepath(str): absolute path of the file
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
250 @param name(unicode, None): name of the file
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
251 @param file_desc(unicode, None): description of the file
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
252 @return (D(unicode)): progress id
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
253 """
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
254 progress_id_d = defer.Deferred()
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
255 if extra is None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
256 extra = {}
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
257 if file_desc is not None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
258 extra['file_desc'] = file_desc
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
259 yield self._j.initiate(client,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
260 peer_jid,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
261 [{'app_ns': NS_JINGLE_FT,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
262 'senders': self._j.ROLE_INITIATOR,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
263 'app_kwargs': {'filepath': filepath,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
264 'name': name,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
265 'extra': extra,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
266 'progress_id_d': progress_id_d},
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
267 }])
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
268 progress_id = yield progress_id_d
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
269 defer.returnValue(progress_id)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
270
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
271 def _fileJingleRequest(self, peer_jid, filepath, name="", file_hash="", hash_algo="", extra=None, profile=C.PROF_KEY_NONE):
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
272 client = self.host.getClient(profile)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
273 return self.fileJingleRequest(client, jid.JID(peer_jid), filepath, name or None, file_hash or None, hash_algo or None, extra or None)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
274
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
275 @defer.inlineCallbacks
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
276 def fileJingleRequest(self, client, peer_jid, filepath, name=None, file_hash=None, hash_algo=None, extra=None):
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
277 """Request a file using jingle file transfer
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
278
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
279 @param peer_jid(jid.JID): destinee jid
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
280 @param filepath(str): absolute path of the file
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
281 @param name(unicode, None): name of the file
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
282 @param file_hash(unicode, None): hash of the file
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
283 @return (D(unicode)): progress id
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
284 """
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
285 progress_id_d = defer.Deferred()
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
286 if extra is None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
287 extra = {}
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
288 if file_hash is not None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
289 if hash_algo is None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
290 raise ValueError(_(u"hash_algo must be set if file_hash is set"))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
291 extra['file_hash'] = file_hash
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
292 extra['hash_algo'] = hash_algo
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
293 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
294 if hash_algo is not None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
295 raise ValueError(_(u"file_hash must be set if hash_algo is set"))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
296 yield self._j.initiate(client,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
297 peer_jid,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
298 [{'app_ns': NS_JINGLE_FT,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
299 'senders': self._j.ROLE_RESPONDER,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
300 'app_kwargs': {'filepath': filepath,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
301 'name': name,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
302 'extra': extra,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
303 'progress_id_d': progress_id_d},
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
304 }])
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
305 progress_id = yield progress_id_d
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
306 defer.returnValue(progress_id)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
307
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
308 # jingle callbacks
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
309
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
310 def jingleSessionInit(self, client, session, content_name, filepath, name, extra, progress_id_d):
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
311 if extra is None:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
312 extra = {}
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
313 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
314 if not EXTRA_ALLOWED.issuperset(extra):
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
315 raise ValueError(_(u"only the following keys are allowed in extra: {keys}").format(
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
316 keys=u', '.join(EXTRA_ALLOWED)))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
317 progress_id_d.callback(self.getProgressId(session, content_name))
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
318 content_data = session['contents'][content_name]
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
319 application_data = content_data['application_data']
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
320 assert 'file_path' not in application_data
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
321 application_data['file_path'] = filepath
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
322 file_data = application_data['file_data'] = {}
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
323 desc_elt = domish.Element((NS_JINGLE_FT, 'description'))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
324 file_elt = desc_elt.addElement("file")
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
325
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
326 if content_data[u'senders'] == self._j.ROLE_INITIATOR:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
327 # we send a file
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
328 if name is None:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
329 name = os.path.basename(filepath)
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
330 file_data[u'date'] = utils.xmpp_date()
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
331 file_data[u'desc'] = extra.pop(u'file_desc', u'')
2512
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
332 file_data[u'name'] = name
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
333 mime_type = mimetypes.guess_type(name, strict=False)[0]
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
334 if mime_type is not None:
025afb04c10b plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents: 2502
diff changeset
335 file_data[u'mime_type'] = mime_type
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
336 file_data[u'size'] = os.path.getsize(filepath)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
337 if u'namespace' in extra:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
338 file_data[u'namespace'] = extra[u'namespace']
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
339 if u'path' in extra:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
340 file_data[u'path'] = extra[u'path']
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
341 self.buildFileElementFromDict(file_data, file_elt=file_elt, file_hash=u'')
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
342 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
343 # we request a file
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
344 file_hash = extra.pop(u'file_hash', u'')
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
345 if not name and not file_hash:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
346 raise ValueError(_(u'you need to provide at least name or file hash'))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
347 if name:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
348 file_data[u'name'] = name
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
349 if file_hash:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
350 file_data[u'file_hash'] = file_hash
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
351 file_data[u'hash_algo'] = extra[u'hash_algo']
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
352 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
353 file_data[u'hash_algo'] = self._hash.getDefaultAlgo()
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
354 if u'namespace' in extra:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
355 file_data[u'namespace'] = extra[u'namespace']
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
356 if u'path' in extra:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
357 file_data[u'path'] = extra[u'path']
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
358 self.buildFileElementFromDict(file_data, file_elt=file_elt)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
359
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
360 return desc_elt
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
361
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
362 def jingleRequestConfirmation(self, client, action, session, content_name, desc_elt):
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
363 """This method request confirmation for a jingle session"""
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
364 content_data = session['contents'][content_name]
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
365 senders = content_data[u'senders']
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
366 if senders not in (self._j.ROLE_INITIATOR, self._j.ROLE_RESPONDER):
1557
22f0307864b4 plugin XEP-0234: "senders" handling
Goffi <goffi@goffi.org>
parents: 1556
diff changeset
367 log.warning(u"Bad sender, assuming initiator")
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
368 senders = content_data[u'senders'] = self._j.ROLE_INITIATOR
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
369 # first we grab file informations
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
370 try:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
371 file_elt = desc_elt.elements(NS_JINGLE_FT, 'file').next()
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
372 except StopIteration:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
373 raise failure.Failure(exceptions.DataError)
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
374 file_data = {'progress_id': self.getProgressId(session, content_name)}
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
375
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
376 if senders == self._j.ROLE_RESPONDER:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
377 # we send the file
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
378 return self._fileSendingRequestConf(client, session, content_data, content_name, file_data, file_elt)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
379 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
380 # we receive the file
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
381 return self._fileReceivingRequestConf(client, session, content_data, content_name, file_data, file_elt)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
382
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
383 @defer.inlineCallbacks
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
384 def _fileSendingRequestConf(self, client, session, content_data, content_name, file_data, file_elt):
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
385 """parse file_elt, and handle file retrieving/permission checking"""
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
386 self.parseFileElement(file_elt, file_data)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
387 content_data['application_data']['file_data'] = file_data
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
388 finished_d = content_data['finished_d'] = defer.Deferred()
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
389
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
390 # confirmed_d is a deferred returning confimed value (only used if cont is False)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
391 cont, confirmed_d = self.host.trigger.returnPoint("XEP-0234_fileSendingRequest", client, session, content_data, content_name, file_data, file_elt)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
392 if not cont:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
393 confirmed = yield confirmed_d
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
394 if confirmed:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
395 args = [client, session, content_name, content_data]
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
396 finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
397 defer.returnValue(confirmed)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
398
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
399 log.warning(_(u'File continue is not implemented yet'))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
400 defer.returnValue(False)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
401
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
402 def _fileReceivingRequestConf(self, client, session, content_data, content_name, file_data, file_elt):
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
403 """parse file_elt, and handle user permission/file opening"""
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
404 self.parseFileElement(file_elt, file_data, given=True)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
405 try:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
406 hash_algo, file_data['given_file_hash'] = self._hash.parseHashElt(file_elt)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
407 except exceptions.NotFound:
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
408 try:
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
409 hash_algo = self._hash.parseHashUsedElt(file_elt)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
410 except exceptions.NotFound:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
411 raise failure.Failure(exceptions.DataError)
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
412
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
413 if hash_algo is not None:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
414 file_data['hash_algo'] = hash_algo
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
415 file_data['hash_hasher'] = hasher = self._hash.getHasher(hash_algo)
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
416 file_data['data_cb'] = lambda data: hasher.update(data)
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
417
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
418 try:
1575
833bdb227b16 plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
419 file_data['size'] = int(file_data['size'])
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
420 except ValueError:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
421 raise failure.Failure(exceptions.DataError)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
422
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
423 name = file_data['name']
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
424 if '/' in name or '\\' in name:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
425 log.warning(u"File name contain path characters, we replace them: {}".format(name))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
426 file_data['name'] = name.replace('/', '_').replace('\\', '_')
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
427
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
428 content_data['application_data']['file_data'] = file_data
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
429
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
430 # now we actualy request permission to user
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
431 def gotConfirmation(confirmed):
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
432 if confirmed:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
433 args = [client, session, content_name, content_data]
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
434 finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
435 return confirmed
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
436
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
437 # deferred to track end of transfer
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
438 finished_d = content_data['finished_d'] = defer.Deferred()
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
439 d = self._f.getDestDir(client, session['peer_jid'], content_data, file_data, stream_object=True)
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
440 d.addCallback(gotConfirmation)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
441 return d
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
442
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
443 def jingleHandler(self, client, action, session, content_name, desc_elt):
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
444 content_data = session['contents'][content_name]
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
445 application_data = content_data['application_data']
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
446 if action in (self._j.A_ACCEPTED_ACK,):
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
447 pass
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
448 elif action == self._j.A_SESSION_INITIATE:
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
449 file_elt = desc_elt.elements(NS_JINGLE_FT, 'file').next()
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
450 try:
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
451 file_elt.elements(NS_JINGLE_FT, 'range').next()
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
452 except StopIteration:
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
453 # initiator doesn't manage <range>, but we do so we advertise it
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
454 # FIXME: to be checked
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
455 log.debug("adding <range> element")
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
456 file_elt.addElement('range')
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
457 elif action == self._j.A_SESSION_ACCEPT:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
458 assert not 'stream_object' in content_data
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
459 file_data = application_data['file_data']
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
460 file_path = application_data['file_path']
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
461 senders = content_data[u'senders']
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
462 if senders != session[u'role']:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
463 # we are receiving the file
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
464 try:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
465 # did the responder specified the size of the file?
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
466 file_elt = next(desc_elt.elements(NS_JINGLE_FT, u'file'))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
467 size_elt = next(file_elt.elements(NS_JINGLE_FT, u'size'))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
468 size = int(unicode(size_elt))
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
469 except (StopIteration, ValueError):
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
470 size = None
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
471 # XXX: hash security is not critical here, so we just take the higher mandatory one
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
472 hasher = file_data['hash_hasher'] = self._hash.getHasher()
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
473 content_data['stream_object'] = stream.FileStreamObject(
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
474 self.host,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
475 client,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
476 file_path,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
477 mode='wb',
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
478 uid=self.getProgressId(session, content_name),
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
479 size=size,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
480 data_cb=lambda data: hasher.update(data),
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
481 )
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
482 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
483 # we are sending the file
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
484 size = file_data['size']
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
485 # XXX: hash security is not critical here, so we just take the higher mandatory one
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
486 hasher = file_data['hash_hasher'] = self._hash.getHasher()
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
487 content_data['stream_object'] = stream.FileStreamObject(
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
488 self.host,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
489 client,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
490 file_path,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
491 uid=self.getProgressId(session, content_name),
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
492 size=size,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
493 data_cb=lambda data: hasher.update(data),
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
494 )
1571
c668081eba1c plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
495 finished_d = content_data['finished_d'] = defer.Deferred()
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
496 args = [client, session, content_name, content_data]
1571
c668081eba1c plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
497 finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
498 else:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
499 log.warning(u"FIXME: unmanaged action {}".format(action))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
500 return desc_elt
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
501
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
502 def jingleSessionInfo(self, client, action, session, content_name, jingle_elt):
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
503 """Called on session-info action
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
504
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
505 manage checksum, and ignore <received/> element
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
506 """
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
507 # TODO: manage <received/> element
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
508 content_data = session['contents'][content_name]
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
509 elts = [elt for elt in jingle_elt.elements() if elt.uri == NS_JINGLE_FT]
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
510 if not elts:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
511 return
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
512 for elt in elts:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
513 if elt.name == 'received':
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
514 pass
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
515 elif elt.name == 'checksum':
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
516 # we have received the file hash, we need to parse it
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
517 if content_data['senders'] == session['role']:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
518 log.warning(u"unexpected checksum received while we are the file sender")
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
519 raise exceptions.DataError
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
520 info_content_name = elt['name']
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
521 if info_content_name != content_name:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
522 # it was for an other content...
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
523 return
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
524 file_data = content_data['application_data']['file_data']
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
525 try:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
526 file_elt = elt.elements((NS_JINGLE_FT, 'file')).next()
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
527 except StopIteration:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
528 raise exceptions.DataError
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
529 algo, file_data['given_file_hash'] = self._hash.parseHashElt(file_elt)
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
530 if algo != file_data.get('hash_algo'):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
531 log.warning(u"Hash algorithm used in given hash ({peer_algo}) doesn't correspond to the one we have used ({our_algo}) [{profile}]"
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
532 .format(peer_algo=algo, our_algo=file_data.get('hash_algo'), profile=client.profile))
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
533 else:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
534 self._receiverTryTerminate(client, session, content_name, content_data)
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
535 else:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
536 raise NotImplementedError
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
537
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
538 def jingleTerminate(self, client, action, session, content_name, jingle_elt):
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
539 if jingle_elt.decline:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
540 # progress is the only way to tell to frontends that session has been declined
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
541 progress_id = self.getProgressId(session, content_name)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
542 self.host.bridge.progressError(progress_id, C.PROGRESS_ERROR_DECLINED, client.profile)
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
543
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
544 def _sendCheckSum(self, client, session, content_name, content_data):
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
545 """Send the session-info with the hash checksum"""
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
546 file_data = content_data['application_data']['file_data']
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
547 hasher = file_data['hash_hasher']
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
548 hash_ = hasher.hexdigest()
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
549 log.debug(u"Calculated hash: {}".format(hash_))
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
550 iq_elt, jingle_elt = self._j.buildSessionInfo(client, session)
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
551 checksum_elt = jingle_elt.addElement((NS_JINGLE_FT, 'checksum'))
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
552 checksum_elt['creator'] = content_data['creator']
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
553 checksum_elt['name'] = content_name
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
554 file_elt = checksum_elt.addElement('file')
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
555 file_elt.addChild(self._hash.buildHashElt(hash_))
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
556 iq_elt.send()
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
557
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
558 def _receiverTryTerminate(self, client, session, content_name, content_data, last_try=False):
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
559 """Try to terminate the session
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
560
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
561 This method must only be used by the receiver.
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
562 It check if transfer is finished, and hash available,
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
563 if everything is OK, it check hash and terminate the session
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
564 @param last_try(bool): if True this mean than session must be terminated even given hash is not available
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
565 @return (bool): True if session was terminated
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
566 """
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
567 if not content_data.get('transfer_finished', False):
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
568 return False
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
569 file_data = content_data['application_data']['file_data']
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
570 given_hash = file_data.get('given_file_hash')
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
571 if given_hash is None:
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
572 if last_try:
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
573 log.warning(u"sender didn't sent hash checksum, we can't check the file [{profile}]".format(profile=client.profile))
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
574 self._j.delayedContentTerminate(client, session, content_name)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
575 content_data['stream_object'].close()
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
576 return True
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
577 return False
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
578 hasher = file_data['hash_hasher']
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
579 hash_ = hasher.hexdigest()
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
580
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
581 if hash_ == given_hash:
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
582 log.info(u"Hash checked, file was successfully transfered: {}".format(hash_))
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
583 progress_metadata = {'hash': hash_,
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
584 'hash_algo': file_data['hash_algo'],
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
585 'hash_verified': C.BOOL_TRUE
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
586 }
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
587 error = None
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
588 else:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
589 log.warning(u"Hash mismatch, the file was not transfered correctly")
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
590 progress_metadata=None
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
591 error = u"Hash mismatch: given={algo}:{given}, calculated={algo}:{our}".format(
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
592 algo = file_data['hash_algo'],
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
593 given = given_hash,
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
594 our = hash_)
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
595
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
596 self._j.delayedContentTerminate(client, session, content_name)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
597 content_data['stream_object'].close(progress_metadata, error)
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
598 # we may have the last_try timer still active, so we try to cancel it
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
599 try:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
600 content_data['last_try_timer'].cancel()
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
601 except (KeyError, internet_error.AlreadyCalled):
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
602 pass
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
603 return True
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
604
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
605 def _finishedCb(self, dummy, client, session, content_name, content_data):
1753
27a140aa5023 plugin XEP-0234: log file transfer "terminated" instead of "completed", as the file may not be complete (check is done after)
Goffi <goffi@goffi.org>
parents: 1669
diff changeset
606 log.info(u"File transfer terminated")
1571
c668081eba1c plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
607 if content_data['senders'] != session['role']:
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
608 # we terminate the session only if we are the receiver,
1571
c668081eba1c plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
609 # as recommanded in XEP-0234 §2 (after example 6)
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
610 content_data['transfer_finished'] = True
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
611 if not self._receiverTryTerminate(client, session, content_name, content_data):
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
612 # we have not received the hash yet, we wait 5 more seconds
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
613 content_data['last_try_timer'] = reactor.callLater(
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
614 5, self._receiverTryTerminate, client, session, content_name, content_data, last_try=True)
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
615 else:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
616 # we are the sender, we send the checksum
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
617 self._sendCheckSum(client, session, content_name, content_data)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
618 content_data['stream_object'].close()
1571
c668081eba1c plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
619
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
620 def _finishedEb(self, failure, client, session, content_name, content_data):
1669
697effba0310 plugin pipe: rewritten plugin as a jingle application. The current implentation can, in some cases, block the backend, and is experimental only. Improvments are needed in the future.
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
621 log.warning(u"Error while streaming file: {}".format(failure))
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
622 content_data['stream_object'].close()
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
623 self._j.contentTerminate(client, session, content_name, reason=self._j.REASON_FAILED_TRANSPORT)
1571
c668081eba1c plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
624
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
625
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
626 class XEP_0234_handler(XMPPHandler):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
627 implements(iwokkel.IDisco)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
628
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
629 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
630 return [disco.DiscoFeature(NS_JINGLE_FT)]
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
631
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
632 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
633 return []