Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0234.py @ 4226:3f7ca590a5da
cli (pubsub): use `PEP` when service is not specified in error message.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 05 Mar 2024 17:31:56 +0100 |
parents | 5a0bddfa34ac |
children | e11b13418ba6 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
3 # SàT plugin for Jingle File Transfer (XEP-0234) |
3479 | 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # 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
|
7 # 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
|
8 # 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
|
9 # (at your option) any later version. |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # 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
|
12 # 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
|
13 # 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
|
14 # GNU Affero General Public License for more details. |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # 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
|
17 # 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
|
18 |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
19 from collections import namedtuple |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
20 import mimetypes |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
21 import os.path |
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
22 |
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
|
23 from twisted.internet import defer |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
24 from twisted.internet import reactor |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
25 from twisted.internet import error as internet_error |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
26 from twisted.python import failure |
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
27 from twisted.words.protocols.jabber import jid |
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
28 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
29 from twisted.words.xish import domish |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
30 from wokkel import disco, iwokkel |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
31 from zope.interface import implementer |
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
32 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4052
diff
changeset
|
33 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4052
diff
changeset
|
34 from libervia.backend.core.constants import Const as C |
4112
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
35 from libervia.backend.core.core_types import SatXMPPEntity |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4052
diff
changeset
|
36 from libervia.backend.core.i18n import D_, _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4052
diff
changeset
|
37 from libervia.backend.core.log import getLogger |
4112
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
38 from libervia.backend.tools import xml_tools |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4052
diff
changeset
|
39 from libervia.backend.tools import utils |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4052
diff
changeset
|
40 from libervia.backend.tools import stream |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4052
diff
changeset
|
41 from libervia.backend.tools.common import date_utils |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4052
diff
changeset
|
42 from libervia.backend.tools.common import regex |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
43 |
4112
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
44 from .plugin_xep_0166 import BaseApplicationHandler |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
45 |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
47 log = getLogger(__name__) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
49 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
|
50 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 C.PI_HANDLER: "yes", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
60 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
|
61 } |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
3040 | 63 EXTRA_ALLOWED = {"path", "namespace", "file_desc", "file_hash", "hash_algo"} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
64 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
|
65 |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
4112
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
67 class XEP_0234(BaseApplicationHandler): |
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
|
68 # TODO: assure everything is closed when file is sent or session terminate is received |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
69 # TODO: call self._f.unregister when unloading order will be managing (i.e. when |
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
70 # 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
|
71 Range = Range # we copy the class here, so it can be used by other plugins |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
72 name = PLUGIN_INFO[C.PI_NAME] |
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
73 human_name = D_("file transfer") |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 def __init__(self, host): |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 log.info(_("plugin Jingle File Transfer initialization")) |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 self.host = host |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
78 host.register_namespace("jingle-ft", NS_JINGLE_FT) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
79 self._j = host.plugins["XEP-0166"] # shortcut to access jingle |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
80 self._j.register_application(NS_JINGLE_FT, self) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 self._f = host.plugins["FILE"] |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
82 self._f.register(self, priority=10000) |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1598
diff
changeset
|
83 self._hash = self.host.plugins["XEP-0300"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
84 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
85 "file_jingle_send", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
86 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
87 in_sign="ssssa{ss}s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
88 out_sign="", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
89 method=self._file_send, |
3028 | 90 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
91 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
92 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
93 "file_jingle_request", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
94 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
95 in_sign="sssssa{ss}s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
96 out_sign="s", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
97 method=self._file_jingle_request, |
3028 | 98 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
99 ) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
101 def get_handler(self, client): |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 return XEP_0234_handler() |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
104 def get_progress_id(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
|
105 """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
|
106 |
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
|
107 @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
|
108 @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
|
109 @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
|
110 """ |
3028 | 111 return "{}_{}".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
|
112 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
113 async def can_handle_file_send(self, client, peer_jid, filepath): |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
114 if peer_jid.resource: |
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
115 return await self.host.hasFeature(client, NS_JINGLE_FT, peer_jid) |
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
116 else: |
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
117 # if we have a bare jid, Jingle Message Initiation will be tried |
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
118 return True |
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
119 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
120 # generic methods |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
121 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
122 def build_file_element( |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
123 self, client, name=None, file_hash=None, hash_algo=None, size=None, |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
124 mime_type=None, desc=None, modified=None, transfer_range=None, path=None, |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
125 namespace=None, file_elt=None, **kwargs): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
126 """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
|
127 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
128 @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
|
129 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
|
130 @param hash_algo(unicode, None): hash algorithm used |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
131 if file_hash is None and hash_algo is set, a <hash-used/> element will be |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
132 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
|
133 @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
|
134 @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
|
135 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
|
136 int to use an unix timestamp |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
137 else must be an unicode string which will be used as it (it must be an XMPP |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
138 time) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
139 @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
|
140 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
|
141 @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
|
142 @return (domish.Element): generated element |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
143 @trigger XEP-0234_buildFileElement(file_elt, extra_args): can be used to extend |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
144 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
|
145 """ |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
146 if file_elt is None: |
3028 | 147 file_elt = domish.Element((NS_JINGLE_FT, "file")) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
148 for name, value in ( |
3028 | 149 ("name", name), |
150 ("size", size), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
151 ("media-type", mime_type), |
3028 | 152 ("desc", desc), |
153 ("path", path), | |
154 ("namespace", namespace), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
155 ): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
156 if value is not None: |
3028 | 157 file_elt.addElement(name, content=str(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
|
158 |
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
|
159 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
|
160 if isinstance(modified, int): |
3028 | 161 file_elt.addElement("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
|
162 else: |
3028 | 163 file_elt.addElement("date", modified) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
164 elif "created" in kwargs: |
3028 | 165 file_elt.addElement("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
|
166 |
3028 | 167 range_elt = file_elt.addElement("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
|
168 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
|
169 if transfer_range.offset is not None: |
3028 | 170 range_elt["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
|
171 if transfer_range.length is not None: |
3028 | 172 range_elt["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
|
173 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
|
174 if not file_hash: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
175 file_elt.addChild(self._hash.build_hash_used_elt()) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
176 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
177 file_elt.addChild(self._hash.build_hash_elt(file_hash, 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
|
178 elif hash_algo is not None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
179 file_elt.addChild(self._hash.build_hash_used_elt(hash_algo)) |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
180 self.host.trigger.point( |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
181 "XEP-0234_buildFileElement", client, 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
|
182 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
|
183 for kw in kwargs: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
184 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
|
185 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
|
186 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
187 def build_file_element_from_dict(self, client, file_data, **kwargs): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
188 """like build_file_element but get values from a file_data dict |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
189 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
190 @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
|
191 @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
|
192 """ |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
193 if kwargs: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
194 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
|
195 file_data.update(kwargs) |
3345
3dc8835d96cc
plugin XEP-0234: fixed mime type in `buildFileElementFromDict`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
196 try: |
3dc8835d96cc
plugin XEP-0234: fixed mime type in `buildFileElementFromDict`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
197 file_data["mime_type"] = ( |
3dc8835d96cc
plugin XEP-0234: fixed mime type in `buildFileElementFromDict`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
198 f'{file_data.pop("media_type")}/{file_data.pop("media_subtype")}' |
3dc8835d96cc
plugin XEP-0234: fixed mime type in `buildFileElementFromDict`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
199 ) |
3dc8835d96cc
plugin XEP-0234: fixed mime type in `buildFileElementFromDict`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
200 except KeyError: |
3dc8835d96cc
plugin XEP-0234: fixed mime type in `buildFileElementFromDict`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
201 pass |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
202 return self.build_file_element(client, **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
|
203 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
204 async def parse_file_element( |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
205 self, client, file_elt, file_data=None, given=False, parent_elt=None, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
206 keep_empty_range=False): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
207 """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
|
208 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
209 @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
|
210 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
|
211 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
|
212 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
|
213 @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
|
214 @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
|
215 if set, file_elt must not be set |
3040 | 216 @param keep_empty_range(bool): if True, keep empty range (i.e. range when offset |
217 and length are None). | |
218 Empty range is 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
|
219 @return (dict): file_data |
3040 | 220 @trigger XEP-0234_parseFileElement(file_elt, file_data): can be used to parse new |
221 elements | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
222 @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
|
223 @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
|
224 """ |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
225 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
|
226 if file_elt is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
227 raise exceptions.InternalError( |
3028 | 228 "file_elt must be None if parent_elt is set" |
2624
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 try: |
3028 | 231 file_elt = next(parent_elt.elements(NS_JINGLE_FT, "file")) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
232 except StopIteration: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
233 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
|
234 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
235 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
|
236 raise exceptions.DataError( |
3028 | 237 "invalid <file> element: {stanza}".format(stanza=file_elt.toXml()) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
238 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
239 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
240 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
|
241 file_data = {} |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
242 |
3028 | 243 for name in ("name", "desc", "path", "namespace"): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
244 try: |
3028 | 245 file_data[name] = str(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
|
246 except StopIteration: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
247 pass |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
248 |
3028 | 249 name = file_data.get("name") |
250 if name == "..": | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
251 # we don't want to go to parent dir when joining to a path |
3028 | 252 name = "--" |
253 file_data["name"] = name | |
3040 | 254 elif name is not None and ("/" in name or "\\" in name): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
255 file_data["name"] = regex.path_escape(name) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
256 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
257 try: |
3028 | 258 file_data["mime_type"] = str( |
259 next(file_elt.elements(NS_JINGLE_FT, "media-type")) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
260 ) |
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
|
261 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
|
262 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
|
263 |
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
|
264 try: |
3028 | 265 file_data["size"] = int( |
266 str(next(file_elt.elements(NS_JINGLE_FT, "size"))) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
267 ) |
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
|
268 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
|
269 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
|
270 |
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
|
271 try: |
3028 | 272 file_data["modified"] = date_utils.date_parse( |
273 next(file_elt.elements(NS_JINGLE_FT, "date")) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
274 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
275 except StopIteration: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
276 pass |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
277 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
278 try: |
3028 | 279 range_elt = next(file_elt.elements(NS_JINGLE_FT, "range")) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
280 except StopIteration: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
281 pass |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
282 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
283 offset = range_elt.getAttribute("offset") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
284 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
|
285 if offset or length or keep_empty_range: |
3028 | 286 file_data["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
|
287 |
3028 | 288 prefix = "given_" if given else "" |
289 hash_algo_key, hash_key = "hash_algo", prefix + "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
|
290 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
291 file_data[hash_algo_key], file_data[hash_key] = self._hash.parse_hash_elt( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
292 file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
293 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
294 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
|
295 pass |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
296 |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
297 self.host.trigger.point("XEP-0234_parseFileElement", client, 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
|
298 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
299 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
|
300 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
301 # bridge methods |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
302 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
303 def _file_send( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
304 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
305 peer_jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
306 filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
307 name="", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
308 file_desc="", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
309 extra=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
310 profile=C.PROF_KEY_NONE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
311 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
312 client = self.host.get_client(profile) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
313 return defer.ensureDeferred(self.file_send( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
314 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
315 jid.JID(peer_jid), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
316 filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
317 name or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
318 file_desc or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
319 extra or None, |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
320 )) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
322 async def file_send( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
323 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
|
324 ): |
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
|
325 """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
|
326 |
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
|
327 @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
|
328 @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
|
329 @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
|
330 @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
|
331 @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
|
332 """ |
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
|
333 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
|
334 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
|
335 extra = {} |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
336 if file_desc is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
337 extra["file_desc"] = file_desc |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
338 encrypted = extra.pop("encrypted", False) |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
339 await self._j.initiate( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
340 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
341 peer_jid, |
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 "app_ns": NS_JINGLE_FT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
345 "senders": self._j.ROLE_INITIATOR, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
346 "app_kwargs": { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
347 "filepath": filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
348 "name": name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
349 "extra": extra, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
350 "progress_id_d": progress_id_d, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
351 }, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
352 } |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
353 ], |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
354 encrypted = encrypted |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
355 ) |
3403
404d4b29de52
plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents:
3345
diff
changeset
|
356 return await progress_id_d |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
357 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
358 def _file_jingle_request( |
3040 | 359 self, peer_jid, filepath, name="", file_hash="", hash_algo="", extra=None, |
360 profile=C.PROF_KEY_NONE): | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
361 client = self.host.get_client(profile) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
362 return defer.ensureDeferred(self.file_jingle_request( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
363 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
364 jid.JID(peer_jid), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
365 filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
366 name or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
367 file_hash or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
368 hash_algo or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
369 extra or None, |
3434
c84c54c6b046
plugin XEP-0234: make `fileJingleRequest` async to fix call of `initiate`
Goffi <goffi@goffi.org>
parents:
3404
diff
changeset
|
370 )) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
371 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
372 async def file_jingle_request( |
3040 | 373 self, client, peer_jid, filepath, name=None, file_hash=None, hash_algo=None, |
374 extra=None): | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
375 """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
|
376 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
377 @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
|
378 @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
|
379 @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
|
380 @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
|
381 @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
|
382 """ |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
383 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
|
384 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
|
385 extra = {} |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
386 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
|
387 if hash_algo is None: |
3028 | 388 raise ValueError(_("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
|
389 extra["file_hash"] = file_hash |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
390 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
|
391 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
392 if hash_algo is not None: |
3028 | 393 raise ValueError(_("file_hash must be set if hash_algo is set")) |
3434
c84c54c6b046
plugin XEP-0234: make `fileJingleRequest` async to fix call of `initiate`
Goffi <goffi@goffi.org>
parents:
3404
diff
changeset
|
394 await self._j.initiate( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
395 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
396 peer_jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
397 [ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
398 { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
399 "app_ns": NS_JINGLE_FT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
400 "senders": self._j.ROLE_RESPONDER, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
401 "app_kwargs": { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
402 "filepath": filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
403 "name": name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
404 "extra": extra, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
405 "progress_id_d": progress_id_d, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
406 }, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
407 } |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
408 ], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
409 ) |
3434
c84c54c6b046
plugin XEP-0234: make `fileJingleRequest` async to fix call of `initiate`
Goffi <goffi@goffi.org>
parents:
3404
diff
changeset
|
410 return await progress_id_d |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
411 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
412 # jingle callbacks |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
413 |
4112
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
414 async def jingle_preflight( |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
415 self, |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
416 client: SatXMPPEntity, |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
417 session: dict, |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
418 description_elt: domish.Element |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
419 ) -> None: |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
420 """Perform preflight checks for an incoming call session. |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
421 |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
422 Check if the calls is audio only or audio/video, then, prompts the user for |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
423 confirmation. |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
424 |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
425 @param client: The client instance. |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
426 @param session: Jingle session. |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
427 @param description_elt: The description element. It's parent attribute is used to |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
428 determine check siblings to see if it's an audio only or audio/video call. |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
429 |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
430 @raises exceptions.CancelError: If the user doesn't accept the incoming call. |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
431 """ |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
432 session_id = session["id"] |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
433 peer_jid = session["peer_jid"] |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
434 # FIXME: has been moved from XEP-0353, but it doesn't handle correctly file |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
435 # transfer (metadata are not used). We must check with other clients what is |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
436 # actually send, and if XEP-0353 is used, and do a better integration. |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
437 |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
438 if client.roster and peer_jid.userhostJID() not in client.roster: |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
439 confirm_msg = D_( |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
440 "Somebody not in your contact list ({peer_jid}) wants to do a " |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
441 '"{human_name}" session with you, this would leak your presence and ' |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
442 "possibly you IP (internet localisation), do you accept?" |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
443 ).format(peer_jid=peer_jid, human_name=self.human_name) |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
444 confirm_title = D_("File sent from an unknown contact") |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
445 action_type = C.META_TYPE_NOT_IN_ROSTER_LEAK |
4220
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
446 action_subtype = None |
4112
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
447 else: |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
448 confirm_msg = D_( |
4220
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
449 "{peer_jid} wants to send a file to you, do you accept?" |
4112
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
450 ).format(peer_jid=peer_jid) |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
451 confirm_title = D_("File Proposed") |
4220
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
452 action_type = C.META_TYPE_CONFIRM |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
453 action_subtype = C.META_TYPE_FILE |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
454 action_extra = { |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
455 "type": action_type, |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
456 "session_id": session_id, |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
457 "from_jid": peer_jid.full(), |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
458 } |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
459 if action_subtype is not None: |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
460 action_extra["subtype"] = action_subtype |
4112
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
461 accepted = await xml_tools.defer_confirm( |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
462 self.host, |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
463 confirm_msg, |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
464 confirm_title, |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
465 profile=client.profile, |
4220
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
466 action_extra=action_extra |
4112
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
467 ) |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
468 if accepted: |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
469 session["file_accepted"] = True |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
470 return accepted |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
471 |
4115
0da563780ffc
plugin XEP-0167, XEP-0353: handle ringing:
Goffi <goffi@goffi.org>
parents:
4112
diff
changeset
|
472 async def jingle_preflight_info( |
0da563780ffc
plugin XEP-0167, XEP-0353: handle ringing:
Goffi <goffi@goffi.org>
parents:
4112
diff
changeset
|
473 self, |
0da563780ffc
plugin XEP-0167, XEP-0353: handle ringing:
Goffi <goffi@goffi.org>
parents:
4112
diff
changeset
|
474 client: SatXMPPEntity, |
0da563780ffc
plugin XEP-0167, XEP-0353: handle ringing:
Goffi <goffi@goffi.org>
parents:
4112
diff
changeset
|
475 session: dict, |
0da563780ffc
plugin XEP-0167, XEP-0353: handle ringing:
Goffi <goffi@goffi.org>
parents:
4112
diff
changeset
|
476 info_type: str, |
0da563780ffc
plugin XEP-0167, XEP-0353: handle ringing:
Goffi <goffi@goffi.org>
parents:
4112
diff
changeset
|
477 info_data: dict|None = None |
0da563780ffc
plugin XEP-0167, XEP-0353: handle ringing:
Goffi <goffi@goffi.org>
parents:
4112
diff
changeset
|
478 ) -> None: |
0da563780ffc
plugin XEP-0167, XEP-0353: handle ringing:
Goffi <goffi@goffi.org>
parents:
4112
diff
changeset
|
479 pass |
0da563780ffc
plugin XEP-0167, XEP-0353: handle ringing:
Goffi <goffi@goffi.org>
parents:
4112
diff
changeset
|
480 |
4112
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
481 async def jingle_preflight_cancel( |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
482 self, |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
483 client: SatXMPPEntity, |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
484 session: dict, |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
485 cancel_error: exceptions.CancelError |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
486 ) -> None: |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
487 pass |
bc60875cb3b8
plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
488 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
489 def jingle_description_elt( |
3404
26a0af6e32c1
plugin XEP-0166: new trigger point + coroutines + helper methods:
Goffi <goffi@goffi.org>
parents:
3403
diff
changeset
|
490 self, client, session, content_name, filepath, name, extra, progress_id_d |
26a0af6e32c1
plugin XEP-0166: new trigger point + coroutines + helper methods:
Goffi <goffi@goffi.org>
parents:
3403
diff
changeset
|
491 ): |
26a0af6e32c1
plugin XEP-0166: new trigger point + coroutines + helper methods:
Goffi <goffi@goffi.org>
parents:
3403
diff
changeset
|
492 return domish.Element((NS_JINGLE_FT, "description")) |
26a0af6e32c1
plugin XEP-0166: new trigger point + coroutines + helper methods:
Goffi <goffi@goffi.org>
parents:
3403
diff
changeset
|
493 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
494 def jingle_session_init( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
495 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
|
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 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
|
498 extra = {} |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
499 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
500 if not EXTRA_ALLOWED.issuperset(extra): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
501 raise ValueError( |
3028 | 502 _("only the following keys are allowed in extra: {keys}").format( |
503 keys=", ".join(EXTRA_ALLOWED) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
504 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
505 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
506 progress_id_d.callback(self.get_progress_id(session, content_name)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
507 content_data = session["contents"][content_name] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
508 application_data = content_data["application_data"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
509 assert "file_path" not in application_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
510 application_data["file_path"] = filepath |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
511 file_data = application_data["file_data"] = {} |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
512 desc_elt = self.jingle_description_elt( |
3404
26a0af6e32c1
plugin XEP-0166: new trigger point + coroutines + helper methods:
Goffi <goffi@goffi.org>
parents:
3403
diff
changeset
|
513 client, session, content_name, filepath, name, extra, progress_id_d) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
514 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
|
515 |
3028 | 516 if content_data["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
|
517 # 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
|
518 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
|
519 name = os.path.basename(filepath) |
3028 | 520 file_data["date"] = utils.xmpp_date() |
521 file_data["desc"] = extra.pop("file_desc", "") | |
522 file_data["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
|
523 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
|
524 if mime_type is not None: |
3028 | 525 file_data["mime_type"] = mime_type |
526 file_data["size"] = os.path.getsize(filepath) | |
527 if "namespace" in extra: | |
528 file_data["namespace"] = extra["namespace"] | |
529 if "path" in extra: | |
530 file_data["path"] = extra["path"] | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
531 self.build_file_element_from_dict( |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
532 client, file_data, file_elt=file_elt, 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
|
533 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
534 # we request a file |
3028 | 535 file_hash = extra.pop("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
|
536 if not name and not file_hash: |
3028 | 537 raise ValueError(_("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
|
538 if name: |
3028 | 539 file_data["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
|
540 if file_hash: |
3028 | 541 file_data["file_hash"] = file_hash |
542 file_data["hash_algo"] = extra["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
|
543 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
544 file_data["hash_algo"] = self._hash.get_default_algo() |
3028 | 545 if "namespace" in extra: |
546 file_data["namespace"] = extra["namespace"] | |
547 if "path" in extra: | |
548 file_data["path"] = extra["path"] | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
549 self.build_file_element_from_dict(client, file_data, file_elt=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
|
550 |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
551 return desc_elt |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
552 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
553 async def jingle_request_confirmation( |
4220
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
554 self, |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
555 client: SatXMPPEntity, |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
556 action: str, |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
557 session: dict, |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
558 content_name: str, |
5a0bddfa34ac
plugin XEP-0234: fix action type when pre-flight file confirmation is confirmed:
Goffi <goffi@goffi.org>
parents:
4115
diff
changeset
|
559 desc_elt: domish.Element, |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
560 ): |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
561 """This method request confirmation for a jingle session""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
562 content_data = session["contents"][content_name] |
3028 | 563 senders = content_data["senders"] |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
564 if senders not in (self._j.ROLE_INITIATOR, self._j.ROLE_RESPONDER): |
3028 | 565 log.warning("Bad sender, assuming initiator") |
566 senders = content_data["senders"] = self._j.ROLE_INITIATOR | |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
567 # first we grab file informations |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
568 try: |
3028 | 569 file_elt = next(desc_elt.elements(NS_JINGLE_FT, "file")) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
570 except StopIteration: |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
571 raise failure.Failure(exceptions.DataError) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
572 file_data = {"progress_id": self.get_progress_id(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
|
573 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
574 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
|
575 # we send the file |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
576 return await self._file_sending_request_conf( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
577 client, session, content_data, content_name, file_data, file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
578 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
579 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
580 # we receive the file |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
581 return await self._file_receiving_request_conf( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
582 client, session, content_data, content_name, file_data, file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
583 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
584 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
585 async def _file_sending_request_conf( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
586 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
|
587 ): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
588 """parse file_elt, and handle file retrieving/permission checking""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
589 await self.parse_file_element(client, file_elt, file_data) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
590 content_data["application_data"]["file_data"] = file_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
591 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
|
592 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
593 # confirmed_d is a deferred returning confimed value (only used if cont is False) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
594 cont, confirmed_d = self.host.trigger.return_point( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
595 "XEP-0234_fileSendingRequest", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
596 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
597 session, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
598 content_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
599 content_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
600 file_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
601 file_elt, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
602 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
603 if not cont: |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
604 confirmed = await confirmed_d |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
605 if confirmed: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
606 args = [client, session, content_name, content_data] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
607 finished_d.addCallbacks( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
608 self._finished_cb, self._finished_eb, args, None, args |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
609 ) |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
610 return confirmed |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
611 |
3028 | 612 log.warning(_("File continue is not implemented yet")) |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
613 return False |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
614 |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
615 async def _file_receiving_request_conf( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
616 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
|
617 ): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
618 """parse file_elt, and handle user permission/file opening""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
619 await self.parse_file_element(client, file_elt, file_data, given=True) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
620 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
621 hash_algo, file_data["given_file_hash"] = self._hash.parse_hash_elt(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
|
622 except exceptions.NotFound: |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
623 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
624 hash_algo = self._hash.parse_hash_used_elt(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
|
625 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
|
626 raise failure.Failure(exceptions.DataError) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
627 |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
628 if hash_algo is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
629 file_data["hash_algo"] = hash_algo |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
630 file_data["hash_hasher"] = hasher = self._hash.get_hasher(hash_algo) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
631 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
|
632 |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
633 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
634 file_data["size"] = int(file_data["size"]) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
635 except ValueError: |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
636 raise failure.Failure(exceptions.DataError) |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
637 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
638 name = file_data["name"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
639 if "/" in name or "\\" in name: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
640 log.warning( |
3028 | 641 "File name contain path characters, we replace them: {}".format(name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
642 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
643 file_data["name"] = name.replace("/", "_").replace("\\", "_") |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
644 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
645 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
|
646 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
647 # 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
|
648 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
649 # deferred to track end of transfer |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
650 finished_d = content_data["finished_d"] = defer.Deferred() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
651 confirmed = await self._f.get_dest_dir( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
652 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
|
653 ) |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
654 if confirmed: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
655 await self.host.trigger.async_point( |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
656 "XEP-0234_file_receiving_request_conf", |
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
657 client, session, content_data, file_elt |
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
658 ) |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
659 args = [client, session, content_name, content_data] |
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
660 finished_d.addCallbacks( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
661 self._finished_cb, self._finished_eb, args, None, args |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
662 ) |
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
663 return confirmed |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
664 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
665 async def jingle_handler(self, client, action, session, content_name, desc_elt): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
666 content_data = session["contents"][content_name] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
667 application_data = content_data["application_data"] |
1556
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
668 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
|
669 pass |
1556
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
670 elif action == self._j.A_SESSION_INITIATE: |
3028 | 671 file_elt = next(desc_elt.elements(NS_JINGLE_FT, "file")) |
1556
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
672 try: |
3028 | 673 next(file_elt.elements(NS_JINGLE_FT, "range")) |
1556
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
674 except StopIteration: |
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
675 # initiator doesn't manage <range>, but we do so we advertise it |
3040 | 676 # FIXME: to be checked |
1556
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
677 log.debug("adding <range> element") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
678 file_elt.addElement("range") |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
679 elif action == self._j.A_SESSION_ACCEPT: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
680 assert not "stream_object" in content_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
681 file_data = application_data["file_data"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
682 file_path = application_data["file_path"] |
3028 | 683 senders = content_data["senders"] |
684 if senders != session["role"]: | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
685 # 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
|
686 try: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
687 # did the responder specified the size of the file? |
3028 | 688 file_elt = next(desc_elt.elements(NS_JINGLE_FT, "file")) |
689 size_elt = next(file_elt.elements(NS_JINGLE_FT, "size")) | |
690 size = int(str(size_elt)) | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
691 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
|
692 size = None |
3040 | 693 # XXX: hash security is not critical here, so we just take the higher |
694 # mandatory one | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
695 hasher = file_data["hash_hasher"] = self._hash.get_hasher() |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
696 progress_id = self.get_progress_id(session, content_name) |
3040 | 697 try: |
698 content_data["stream_object"] = stream.FileStreamObject( | |
699 self.host, | |
700 client, | |
701 file_path, | |
702 mode="wb", | |
703 uid=progress_id, | |
704 size=size, | |
705 data_cb=lambda data: hasher.update(data), | |
706 ) | |
707 except Exception as e: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
708 self.host.bridge.progress_error( |
3040 | 709 progress_id, C.PROGRESS_ERROR_FAILED, client.profile |
710 ) | |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
711 await self._j.terminate( |
3040 | 712 client, self._j.REASON_FAILED_APPLICATION, session) |
713 raise e | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
714 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
715 # we are sending the file |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
716 size = file_data["size"] |
3040 | 717 # XXX: hash security is not critical here, so we just take the higher |
718 # mandatory one | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
719 hasher = file_data["hash_hasher"] = self._hash.get_hasher() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
720 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
|
721 self.host, |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
722 client, |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
723 file_path, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
724 uid=self.get_progress_id(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
|
725 size=size, |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
726 data_cb=lambda data: hasher.update(data), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
727 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
728 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
|
729 args = [client, session, content_name, content_data] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
730 finished_d.addCallbacks(self._finished_cb, self._finished_eb, args, None, args) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
731 await self.host.trigger.async_point( |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
732 "XEP-0234_jingle_handler", |
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
733 client, session, content_data, desc_elt |
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
734 ) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
735 else: |
3028 | 736 log.warning("FIXME: unmanaged action {}".format(action)) |
3969
8e7d5796fb23
plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO):
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
737 return desc_elt |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
738 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
739 def jingle_session_info(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
|
740 """Called on session-info action |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
741 |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
742 manage checksum, and ignore <received/> element |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
743 """ |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
744 # TODO: manage <received/> element |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
745 content_data = session["contents"][content_name] |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
746 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
|
747 if not elts: |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
748 return |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
749 for elt in elts: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
750 if elt.name == "received": |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
751 pass |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
752 elif elt.name == "checksum": |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
753 # 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
|
754 if content_data["senders"] == session["role"]: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
755 log.warning( |
3028 | 756 "unexpected checksum received while we are the file sender" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
757 ) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
758 raise exceptions.DataError |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
759 info_content_name = elt["name"] |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
760 if info_content_name != content_name: |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
761 # it was for an other content... |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
762 return |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
763 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
|
764 try: |
3859
3ef988734869
core: fix calls to `domish.Element.elements`:
Goffi <goffi@goffi.org>
parents:
3527
diff
changeset
|
765 file_elt = next(elt.elements(NS_JINGLE_FT, "file")) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
766 except StopIteration: |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
767 raise exceptions.DataError |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
768 algo, file_data["given_file_hash"] = self._hash.parse_hash_elt(file_elt) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
769 if algo != file_data.get("hash_algo"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
770 log.warning( |
3028 | 771 "Hash algorithm used in given hash ({peer_algo}) doesn't correspond to the one we have used ({our_algo}) [{profile}]".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
772 peer_algo=algo, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
773 our_algo=file_data.get("hash_algo"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
774 profile=client.profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
775 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
776 ) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
777 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
778 self._receiver_try_terminate( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
779 client, session, content_name, content_data |
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 raise NotImplementedError |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
783 |
4052
2ced30f6d5de
plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
784 def jingle_terminate(self, client, action, session, content_name, reason_elt): |
2ced30f6d5de
plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
785 if reason_elt.decline: |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
786 # progress is the only way to tell to frontends that session has been declined |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
787 progress_id = self.get_progress_id(session, content_name) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
788 self.host.bridge.progress_error( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
789 progress_id, C.PROGRESS_ERROR_DECLINED, client.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
790 ) |
4052
2ced30f6d5de
plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
791 elif not reason_elt.success: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
792 progress_id = self.get_progress_id(session, content_name) |
4052
2ced30f6d5de
plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
793 first_child = reason_elt.firstChildElement() |
3040 | 794 if first_child is not None: |
795 reason = first_child.name | |
4052
2ced30f6d5de
plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
796 if reason_elt.text is not None: |
2ced30f6d5de
plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
797 reason = f"{reason} - {reason_elt.text}" |
3040 | 798 else: |
799 reason = C.PROGRESS_ERROR_FAILED | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
800 self.host.bridge.progress_error( |
3040 | 801 progress_id, reason, client.profile |
802 ) | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
803 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
804 def _send_check_sum(self, client, session, content_name, content_data): |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
805 """Send the session-info with the hash checksum""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
806 file_data = content_data["application_data"]["file_data"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
807 hasher = file_data["hash_hasher"] |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
808 hash_ = hasher.hexdigest() |
3028 | 809 log.debug("Calculated hash: {}".format(hash_)) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
810 iq_elt, jingle_elt = self._j.build_session_info(client, session) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
811 checksum_elt = jingle_elt.addElement((NS_JINGLE_FT, "checksum")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
812 checksum_elt["creator"] = content_data["creator"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
813 checksum_elt["name"] = content_name |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
814 file_elt = checksum_elt.addElement("file") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
815 file_elt.addChild(self._hash.build_hash_elt(hash_)) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
816 iq_elt.send() |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
817 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
818 def _receiver_try_terminate( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
819 self, client, session, content_name, content_data, last_try=False |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
820 ): |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
821 """Try to terminate the session |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
822 |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
823 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
|
824 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
|
825 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
|
826 @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
|
827 @return (bool): True if session was terminated |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
828 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
829 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
|
830 return False |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
831 file_data = content_data["application_data"]["file_data"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
832 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
|
833 if given_hash is None: |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
834 if last_try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
835 log.warning( |
3028 | 836 "sender didn't sent hash checksum, we can't check the file [{profile}]".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
837 profile=client.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
838 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
839 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
840 self._j.delayed_content_terminate(client, session, content_name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
841 content_data["stream_object"].close() |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
842 return True |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
843 return False |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
844 hasher = file_data["hash_hasher"] |
3040 | 845 hash_ = hasher.hexdigest() |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
846 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
847 if hash_ == given_hash: |
3040 | 848 log.info(f"Hash checked, file was successfully transfered: {hash_}") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
849 progress_metadata = { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
850 "hash": hash_, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
851 "hash_algo": file_data["hash_algo"], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
852 "hash_verified": C.BOOL_TRUE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
853 } |
1626
63cef4dbf2a4
core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
854 error = None |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
855 else: |
3028 | 856 log.warning("Hash mismatch, the file was not transfered correctly") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
857 progress_metadata = None |
3028 | 858 error = "Hash mismatch: given={algo}:{given}, calculated={algo}:{our}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
859 algo=file_data["hash_algo"], given=given_hash, our=hash_ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
860 ) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
861 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
862 self._j.delayed_content_terminate(client, session, content_name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
863 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
|
864 # 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
|
865 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
866 content_data["last_try_timer"].cancel() |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
867 except (KeyError, internet_error.AlreadyCalled): |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
868 pass |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
869 return True |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
870 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
871 def _finished_cb(self, __, client, session, content_name, content_data): |
3028 | 872 log.info("File transfer terminated") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
873 if content_data["senders"] != session["role"]: |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
874 # 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
|
875 # as recommanded in XEP-0234 §2 (after example 6) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
876 content_data["transfer_finished"] = True |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
877 if not self._receiver_try_terminate( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
878 client, session, content_name, content_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
879 ): |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
880 # 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
|
881 content_data["last_try_timer"] = reactor.callLater( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
882 5, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
883 self._receiver_try_terminate, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
884 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
885 session, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
886 content_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
887 content_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
888 last_try=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
889 ) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
890 else: |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
891 # we are the sender, we send the checksum |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
892 self._send_check_sum(client, session, content_name, content_data) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
893 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
|
894 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
895 def _finished_eb(self, failure, client, session, content_name, content_data): |
3028 | 896 log.warning("Error while streaming file: {}".format(failure)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
897 content_data["stream_object"].close() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3969
diff
changeset
|
898 self._j.content_terminate( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
899 client, session, content_name, reason=self._j.REASON_FAILED_TRANSPORT |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
900 ) |
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
|
901 |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
902 |
3028 | 903 @implementer(iwokkel.IDisco) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
904 class XEP_0234_handler(XMPPHandler): |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
905 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
906 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
907 return [disco.DiscoFeature(NS_JINGLE_FT)] |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
908 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
909 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
910 return [] |