annotate sat/plugins/plugin_xep_0234.py @ 2808:51c53fc4fc4a

jp (blog/edit): changed comments handling: "-C" now allow comment and "--no-comments" explicitly disable it. This way when none of this option is used, the current state of the item is kept without deleting accidentaly the comments node.
author Goffi <goffi@goffi.org>
date Sat, 23 Feb 2019 19:00:25 +0100
parents 003b8b4b56a7
children 69e4716d6268
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)
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
5 # Copyright (C) 2009-2019 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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
23
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core import exceptions
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from wokkel import disco, iwokkel
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from zope.interface import implements
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.tools import utils
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
29 from sat.tools import stream
2612
3e4e78de9cca tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
30 from sat.tools.common import date_utils
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 import os.path
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from twisted.words.xish import domish
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from twisted.words.protocols.jabber import jid
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 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
35 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
36 from twisted.internet import defer
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
37 from twisted.internet import reactor
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
38 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
39 from collections import namedtuple
2535
a19b2c43e719 plugin XEP-0234: fixed regex import
Goffi <goffi@goffi.org>
parents: 2512
diff changeset
40 from sat.tools.common 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
41 import mimetypes
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
44 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
45
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
55 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
56 }
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
58 EXTRA_ALLOWED = {u"path", u"namespace", u"file_desc", u"file_hash"}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
59 Range = namedtuple("Range", ("offset", "length"))
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
60
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 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
63 # 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
64 # 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
65 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
66
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def __init__(self, host):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 log.info(_("plugin Jingle File Transfer initialization"))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.host = host
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
70 host.registerNamespace("jingle-ft", NS_JINGLE_FT)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
71 self._j = host.plugins["XEP-0166"] # shortcut to access jingle
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self._j.registerApplication(NS_JINGLE_FT, self)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self._f = host.plugins["FILE"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
74 self._f.register(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
75 NS_JINGLE_FT, self.fileJingleSend, priority=10000, method_name=u"Jingle"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
76 )
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
77 self._hash = self.host.plugins["XEP-0300"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
78 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
79 "fileJingleSend",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
80 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
81 in_sign="ssssa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
82 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
83 method=self._fileJingleSend,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
84 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
85 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
86 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
87 "fileJingleRequest",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
88 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
89 in_sign="sssssa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
90 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
91 method=self._fileJingleRequest,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
92 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
93 )
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
95 def getHandler(self, client):
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 return XEP_0234_handler()
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
98 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
99 """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
100
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
101 @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
102 @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
103 @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
104 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
105 return u"{}_{}".format(session["id"], 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
106
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
107 # generic methods
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
108
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
109 def buildFileElement(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
110 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
111 name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
112 file_hash=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
113 hash_algo=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
114 size=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
115 mime_type=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
116 desc=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
117 modified=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
118 transfer_range=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
119 path=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
120 namespace=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
121 file_elt=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
122 **kwargs
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
123 ):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
124 """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
125
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
126 @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
127 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
128 @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
129 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
130 @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
131 @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
132 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
133 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
134 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
135 @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
136 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
137 @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
138 @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
139 @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
140 """
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
141 if file_elt is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
142 file_elt = domish.Element((NS_JINGLE_FT, u"file"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
143 for name, value in (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
144 (u"name", name),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
145 (u"size", size),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
146 ("media-type", mime_type),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
147 (u"desc", desc),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
148 (u"path", path),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
149 (u"namespace", namespace),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
150 ):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
151 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
152 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
153
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
154 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
155 if isinstance(modified, int):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
156 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
157 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
158 file_elt.addElement(u"date", modified)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
159 elif "created" in kwargs:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
160 file_elt.addElement(u"date", utils.xmpp_date(kwargs.pop("created")))
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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
162 range_elt = file_elt.addElement(u"range")
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
163 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
164 if transfer_range.offset is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
165 range_elt[u"offset"] = transfer_range.offset
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 if transfer_range.length is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
167 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
168 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
169 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
170 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
171 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
172 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
173 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
174 file_elt.addChild(self._hash.buildHashUsedElt(hash_algo))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
175 self.host.trigger.point(u"XEP-0234_buildFileElement", file_elt, extra_args=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
176 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
177 for kw in kwargs:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
178 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
179 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
180
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
181 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
182 """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
183
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
184 @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
185 @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
186 """
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
187 if kwargs:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
188 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
189 file_data.update(kwargs)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
190 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
191
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
192 def parseFileElement(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
193 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
194 file_elt,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
195 file_data=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
196 given=False,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
197 parent_elt=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
198 keep_empty_range=False,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
199 ):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
200 """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
201
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
202 @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
203 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
204 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
205 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
206 @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
207 @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
208 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
209 @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
210 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
211 @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
212 @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
213 @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
214 @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
215 """
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
216 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
217 if file_elt is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
218 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
219 u"file_elt must be None if parent_elt is set"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
220 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
221 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
222 file_elt = next(parent_elt.elements(NS_JINGLE_FT, u"file"))
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
223 except StopIteration:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
224 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
225 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
226 if not file_elt or file_elt.uri != NS_JINGLE_FT:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
227 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
228 u"invalid <file> element: {stanza}".format(stanza=file_elt.toXml())
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
229 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
230
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
231 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
232 file_data = {}
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
233
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
234 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
235 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
236 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
237 except StopIteration:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
238 pass
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
239
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
240 name = file_data.get(u"name")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
241 if name == u"..":
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
242 # we don't want to go to parent dir when joining to a path
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
243 name = u"--"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
244 file_data[u"name"] = name
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
245 elif name is not None and u"/" in name or u"\\" in name:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
246 file_data[u"name"] = regex.pathEscape(name)
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
247
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
248 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
249 file_data[u"mime_type"] = unicode(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
250 next(file_elt.elements(NS_JINGLE_FT, u"media-type"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
251 )
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
252 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
253 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
254
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
255 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
256 file_data[u"size"] = int(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
257 unicode(next(file_elt.elements(NS_JINGLE_FT, u"size")))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
258 )
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
259 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
260 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
261
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
262 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
263 file_data[u"modified"] = date_utils.date_parse(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
264 next(file_elt.elements(NS_JINGLE_FT, u"date"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
265 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
266 except StopIteration:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
267 pass
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
268
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
269 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
270 range_elt = file_elt.elements(NS_JINGLE_FT, u"range").next()
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
271 except StopIteration:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
272 pass
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
273 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
274 offset = range_elt.getAttribute("offset")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
275 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
276 if offset or length or keep_empty_range:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
277 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
278
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
279 prefix = u"given_" if given else u""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
280 hash_algo_key, hash_key = u"hash_algo", prefix + u"file_hash"
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
281 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
282 file_data[hash_algo_key], file_data[hash_key] = self._hash.parseHashElt(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
283 file_elt
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
284 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
285 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
286 pass
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
287
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
288 self.host.trigger.point(u"XEP-0234_parseFileElement", file_elt, 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
289
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
290 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
291
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
292 # bridge methods
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
293
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
294 def _fileJingleSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
295 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
296 peer_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
297 filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
298 name="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
299 file_desc="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
300 extra=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
301 profile=C.PROF_KEY_NONE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
302 ):
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
303 client = self.host.getClient(profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
304 return self.fileJingleSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
305 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
306 jid.JID(peer_jid),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
307 filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
308 name or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
309 file_desc or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
310 extra or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
311 )
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
312
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
313 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
314 def fileJingleSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
315 self, client, peer_jid, filepath, name, file_desc=None, extra=None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
316 ):
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
317 """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
318
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
319 @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
320 @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
321 @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
322 @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
323 @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
324 """
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
325 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
326 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
327 extra = {}
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
328 if file_desc is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
329 extra["file_desc"] = file_desc
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
330 yield self._j.initiate(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
331 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
332 peer_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
333 [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
334 {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
335 "app_ns": NS_JINGLE_FT,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
336 "senders": self._j.ROLE_INITIATOR,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
337 "app_kwargs": {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
338 "filepath": filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
339 "name": name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
340 "extra": extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
341 "progress_id_d": progress_id_d,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
342 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
343 }
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
344 ],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
345 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
346 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
347 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
348
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
349 def _fileJingleRequest(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
350 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
351 peer_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
352 filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
353 name="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
354 file_hash="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
355 hash_algo="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
356 extra=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
357 profile=C.PROF_KEY_NONE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
358 ):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
359 client = self.host.getClient(profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
360 return self.fileJingleRequest(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
361 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
362 jid.JID(peer_jid),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
363 filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
364 name or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
365 file_hash or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
366 hash_algo or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
367 extra or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
368 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
369
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
370 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
371 def fileJingleRequest(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
372 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
373 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
374 peer_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
375 filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
376 name=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
377 file_hash=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
378 hash_algo=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
379 extra=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
380 ):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
381 """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
382
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
383 @param peer_jid(jid.JID): destinee jid
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
384 @param filepath(str): absolute path where the file will be downloaded
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
385 @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
386 @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
387 @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
388 """
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
389 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
390 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
391 extra = {}
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
392 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
393 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
394 raise ValueError(_(u"hash_algo must be set if file_hash is set"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
395 extra["file_hash"] = file_hash
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
396 extra["hash_algo"] = 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
397 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
398 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
399 raise ValueError(_(u"file_hash must be set if hash_algo is set"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
400 yield self._j.initiate(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
401 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
402 peer_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
403 [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
404 {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
405 "app_ns": NS_JINGLE_FT,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
406 "senders": self._j.ROLE_RESPONDER,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
407 "app_kwargs": {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
408 "filepath": filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
409 "name": name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
410 "extra": extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
411 "progress_id_d": progress_id_d,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
412 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
413 }
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
414 ],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
415 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
416 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
417 defer.returnValue(progress_id)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
418
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
419 # jingle callbacks
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
420
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
421 def jingleSessionInit(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
422 self, client, session, content_name, filepath, name, extra, progress_id_d
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
423 ):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
424 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
425 extra = {}
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
426 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
427 if not EXTRA_ALLOWED.issuperset(extra):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
428 raise ValueError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
429 _(u"only the following keys are allowed in extra: {keys}").format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
430 keys=u", ".join(EXTRA_ALLOWED)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
431 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
432 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
433 progress_id_d.callback(self.getProgressId(session, content_name))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
434 content_data = session["contents"][content_name]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
435 application_data = content_data["application_data"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
436 assert "file_path" not in application_data
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
437 application_data["file_path"] = filepath
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
438 file_data = application_data["file_data"] = {}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
439 desc_elt = domish.Element((NS_JINGLE_FT, "description"))
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
440 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
441
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
442 if content_data[u"senders"] == self._j.ROLE_INITIATOR:
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
443 # 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
444 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
445 name = os.path.basename(filepath)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
446 file_data[u"date"] = utils.xmpp_date()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
447 file_data[u"desc"] = extra.pop(u"file_desc", u"")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
448 file_data[u"name"] = name
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
449 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
450 if mime_type is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
451 file_data[u"mime_type"] = mime_type
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
452 file_data[u"size"] = os.path.getsize(filepath)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
453 if u"namespace" in extra:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
454 file_data[u"namespace"] = extra[u"namespace"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
455 if u"path" in extra:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
456 file_data[u"path"] = extra[u"path"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
457 self.buildFileElementFromDict(file_data, file_elt=file_elt, file_hash=u"")
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
458 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
459 # we request a file
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
460 file_hash = extra.pop(u"file_hash", u"")
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
461 if not name and not file_hash:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
462 raise ValueError(_(u"you need to provide at least name or file hash"))
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
463 if name:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
464 file_data[u"name"] = name
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
465 if file_hash:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
466 file_data[u"file_hash"] = file_hash
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
467 file_data[u"hash_algo"] = extra[u"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
468 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
469 file_data[u"hash_algo"] = self._hash.getDefaultAlgo()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
470 if u"namespace" in extra:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
471 file_data[u"namespace"] = extra[u"namespace"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
472 if u"path" in extra:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
473 file_data[u"path"] = extra[u"path"]
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
474 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
475
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
476 return desc_elt
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
477
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
478 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
479 """This method request confirmation for a jingle session"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
480 content_data = session["contents"][content_name]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
481 senders = content_data[u"senders"]
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
482 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
483 log.warning(u"Bad sender, assuming initiator")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
484 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
485 # first we grab file informations
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
486 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
487 file_elt = desc_elt.elements(NS_JINGLE_FT, "file").next()
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
488 except StopIteration:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
489 raise failure.Failure(exceptions.DataError)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
490 file_data = {"progress_id": self.getProgressId(session, 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
491
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
492 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
493 # we send the file
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
494 return self._fileSendingRequestConf(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
495 client, session, content_data, content_name, file_data, file_elt
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
496 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
497 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
498 # we receive the file
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
499 return self._fileReceivingRequestConf(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
500 client, session, content_data, content_name, file_data, file_elt
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
501 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
503 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
504 def _fileSendingRequestConf(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
505 self, client, session, content_data, content_name, file_data, file_elt
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
506 ):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
507 """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
508 self.parseFileElement(file_elt, file_data)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
509 content_data["application_data"]["file_data"] = file_data
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
510 finished_d = content_data["finished_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
511
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
512 # confirmed_d is a deferred returning confimed value (only used if cont is False)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
513 cont, confirmed_d = self.host.trigger.returnPoint(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
514 "XEP-0234_fileSendingRequest",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
515 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
516 session,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
517 content_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
518 content_name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
519 file_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
520 file_elt,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
521 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
522 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
523 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
524 if confirmed:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
525 args = [client, session, content_name, content_data]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
526 finished_d.addCallbacks(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
527 self._finishedCb, self._finishedEb, args, None, args
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
528 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
529 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
530
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
531 log.warning(_(u"File continue is not implemented yet"))
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
532 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
533
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
534 def _fileReceivingRequestConf(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
535 self, client, session, content_data, content_name, file_data, file_elt
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
536 ):
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
537 """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
538 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
539 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
540 hash_algo, file_data["given_file_hash"] = self._hash.parseHashElt(file_elt)
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
541 except exceptions.NotFound:
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
542 try:
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
543 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
544 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
545 raise failure.Failure(exceptions.DataError)
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
546
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
547 if hash_algo is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
548 file_data["hash_algo"] = hash_algo
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
549 file_data["hash_hasher"] = hasher = self._hash.getHasher(hash_algo)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
550 file_data["data_cb"] = lambda data: hasher.update(data)
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
551
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
552 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
553 file_data["size"] = int(file_data["size"])
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
554 except ValueError:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
555 raise failure.Failure(exceptions.DataError)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
556
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
557 name = file_data["name"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
558 if "/" in name or "\\" in name:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
559 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
560 u"File name contain path characters, we replace them: {}".format(name)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
561 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
562 file_data["name"] = name.replace("/", "_").replace("\\", "_")
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
563
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
564 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
565
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
566 # 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
567 def gotConfirmation(confirmed):
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
568 if confirmed:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
569 args = [client, session, content_name, content_data]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
570 finished_d.addCallbacks(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
571 self._finishedCb, self._finishedEb, args, None, args
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
572 )
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
573 return confirmed
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
574
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
575 # deferred to track end of transfer
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
576 finished_d = content_data["finished_d"] = defer.Deferred()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
577 d = self._f.getDestDir(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
578 client, session["peer_jid"], content_data, file_data, stream_object=True
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
579 )
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
580 d.addCallback(gotConfirmation)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
581 return d
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
582
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
583 def jingleHandler(self, client, action, session, content_name, desc_elt):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
584 content_data = session["contents"][content_name]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
585 application_data = content_data["application_data"]
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
586 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
587 pass
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
588 elif action == self._j.A_SESSION_INITIATE:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
589 file_elt = desc_elt.elements(NS_JINGLE_FT, "file").next()
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
590 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
591 file_elt.elements(NS_JINGLE_FT, "range").next()
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
592 except StopIteration:
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
593 # initiator doesn't manage <range>, but we do so we advertise it
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
594 #  FIXME: to be checked
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
595 log.debug("adding <range> element")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
596 file_elt.addElement("range")
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
597 elif action == self._j.A_SESSION_ACCEPT:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
598 assert not "stream_object" in content_data
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
599 file_data = application_data["file_data"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
600 file_path = application_data["file_path"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
601 senders = content_data[u"senders"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
602 if senders != session[u"role"]:
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
603 # 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
604 try:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
605 # did the responder specified the size of the file?
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
606 file_elt = next(desc_elt.elements(NS_JINGLE_FT, u"file"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
607 size_elt = next(file_elt.elements(NS_JINGLE_FT, u"size"))
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
608 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
609 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
610 size = None
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
611 # XXX: hash security is not critical here, so we just take the higher mandatory one
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
612 hasher = file_data["hash_hasher"] = self._hash.getHasher()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
613 content_data["stream_object"] = stream.FileStreamObject(
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
614 self.host,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
615 client,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
616 file_path,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
617 mode="wb",
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
618 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
619 size=size,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
620 data_cb=lambda data: hasher.update(data),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
621 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
622 else:
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
623 # we are sending the file
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
624 size = file_data["size"]
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
625 # XXX: hash security is not critical here, so we just take the higher mandatory one
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
626 hasher = file_data["hash_hasher"] = self._hash.getHasher()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
627 content_data["stream_object"] = stream.FileStreamObject(
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
628 self.host,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
629 client,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
630 file_path,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
631 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
632 size=size,
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
633 data_cb=lambda data: hasher.update(data),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
634 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
635 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
636 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
637 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
638 else:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
639 log.warning(u"FIXME: unmanaged action {}".format(action))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
640 return desc_elt
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
641
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
642 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
643 """Called on session-info action
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
644
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
645 manage checksum, and ignore <received/> element
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
646 """
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
647 # TODO: manage <received/> element
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
648 content_data = session["contents"][content_name]
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
649 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
650 if not elts:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
651 return
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
652 for elt in elts:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
653 if elt.name == "received":
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
654 pass
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
655 elif elt.name == "checksum":
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
656 # we have received the file hash, we need to parse it
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
657 if content_data["senders"] == session["role"]:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
658 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
659 u"unexpected checksum received while we are the file sender"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
660 )
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
661 raise exceptions.DataError
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
662 info_content_name = elt["name"]
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
663 if info_content_name != content_name:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
664 # it was for an other content...
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
665 return
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
666 file_data = content_data["application_data"]["file_data"]
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
667 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
668 file_elt = elt.elements((NS_JINGLE_FT, "file")).next()
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
669 except StopIteration:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
670 raise exceptions.DataError
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
671 algo, file_data["given_file_hash"] = self._hash.parseHashElt(file_elt)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
672 if algo != file_data.get("hash_algo"):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
673 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
674 u"Hash algorithm used in given hash ({peer_algo}) doesn't correspond to the one we have used ({our_algo}) [{profile}]".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
675 peer_algo=algo,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
676 our_algo=file_data.get("hash_algo"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
677 profile=client.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
678 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
679 )
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
680 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
681 self._receiverTryTerminate(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
682 client, session, content_name, content_data
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
683 )
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
684 else:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
685 raise NotImplementedError
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
686
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
687 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
688 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
689 # 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
690 progress_id = self.getProgressId(session, content_name)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
691 self.host.bridge.progressError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
692 progress_id, C.PROGRESS_ERROR_DECLINED, client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
693 )
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
694
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
695 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
696 """Send the session-info with the hash checksum"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
697 file_data = content_data["application_data"]["file_data"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
698 hasher = file_data["hash_hasher"]
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
699 hash_ = hasher.hexdigest()
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
700 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
701 iq_elt, jingle_elt = self._j.buildSessionInfo(client, session)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
702 checksum_elt = jingle_elt.addElement((NS_JINGLE_FT, "checksum"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
703 checksum_elt["creator"] = content_data["creator"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
704 checksum_elt["name"] = content_name
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
705 file_elt = checksum_elt.addElement("file")
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
706 file_elt.addChild(self._hash.buildHashElt(hash_))
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
707 iq_elt.send()
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
708
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
709 def _receiverTryTerminate(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
710 self, client, session, content_name, content_data, last_try=False
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
711 ):
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
712 """Try to terminate the session
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
713
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
714 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
715 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
716 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
717 @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
718 @return (bool): True if session was terminated
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
719 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
720 if not content_data.get("transfer_finished", False):
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
721 return False
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
722 file_data = content_data["application_data"]["file_data"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
723 given_hash = file_data.get("given_file_hash")
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
724 if given_hash is None:
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
725 if last_try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
726 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
727 u"sender didn't sent hash checksum, we can't check the file [{profile}]".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
728 profile=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
729 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
730 )
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
731 self._j.delayedContentTerminate(client, session, content_name)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
732 content_data["stream_object"].close()
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
733 return True
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
734 return False
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
735 hasher = file_data["hash_hasher"]
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
736 hash_ = hasher.hexdigest()
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
737
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
738 if hash_ == given_hash:
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
739 log.info(u"Hash checked, file was successfully transfered: {}".format(hash_))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
740 progress_metadata = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
741 "hash": hash_,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
742 "hash_algo": file_data["hash_algo"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
743 "hash_verified": C.BOOL_TRUE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
744 }
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
745 error = None
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
746 else:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
747 log.warning(u"Hash mismatch, the file was not transfered correctly")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
748 progress_metadata = None
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
749 error = u"Hash mismatch: given={algo}:{given}, calculated={algo}:{our}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
750 algo=file_data["hash_algo"], given=given_hash, our=hash_
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
751 )
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
752
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
753 self._j.delayedContentTerminate(client, session, content_name)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
754 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
755 # 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
756 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
757 content_data["last_try_timer"].cancel()
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
758 except (KeyError, internet_error.AlreadyCalled):
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
759 pass
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
760 return True
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
761
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
762 def _finishedCb(self, __, 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
763 log.info(u"File transfer terminated")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
764 if content_data["senders"] != session["role"]:
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
765 # 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
766 # as recommanded in XEP-0234 §2 (after example 6)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
767 content_data["transfer_finished"] = True
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
768 if not self._receiverTryTerminate(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
769 client, session, content_name, content_data
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
770 ):
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
771 # we have not received the hash yet, we wait 5 more seconds
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
772 content_data["last_try_timer"] = reactor.callLater(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
773 5,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
774 self._receiverTryTerminate,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
775 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
776 session,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
777 content_name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
778 content_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
779 last_try=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
780 )
1620
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
781 else:
4dd07d026214 plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
782 # 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
783 self._sendCheckSum(client, session, content_name, content_data)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
784 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
785
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
786 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
787 log.warning(u"Error while streaming file: {}".format(failure))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
788 content_data["stream_object"].close()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
789 self._j.contentTerminate(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
790 client, session, content_name, reason=self._j.REASON_FAILED_TRANSPORT
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
791 )
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
792
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
793
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
794 class XEP_0234_handler(XMPPHandler):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
795 implements(iwokkel.IDisco)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
796
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
797 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
798 return [disco.DiscoFeature(NS_JINGLE_FT)]
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
799
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
800 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
801 return []