annotate src/plugins/plugin_xep_0234.py @ 1601:e0a152f2cf6d

core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
author Goffi <goffi@goffi.org>
date Sun, 15 Nov 2015 23:11:27 +0100
parents b144babc2658
children 0de5f210fe56
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Jingle File Transfer (XEP-0234)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
20 from sat.core.i18n import _
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = getLogger(__name__)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core import exceptions
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import disco, iwokkel
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from zope.interface import implements
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.tools import utils
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 import os.path
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from twisted.words.xish import domish
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from twisted.words.protocols.jabber import jid
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from twisted.python import failure
1566
ec3848916ee8 plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents: 1557
diff changeset
32 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
1571
c668081eba1c plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
33 from twisted.internet import defer
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 NS_JINGLE_FT = 'urn:xmpp:jingle:apps:file-transfer:4'
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 PLUGIN_INFO = {
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 "name": "Jingle File Transfer",
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 "import_name": "XEP-0234",
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 "type": "XEP",
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 "protocols": ["XEP-0234"],
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 "dependencies": ["XEP-0166", "XEP-0300", "FILE"],
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "main": "XEP_0234",
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "handler": "yes",
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "description": _("""Implementation of Jingle File Transfer""")
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 }
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 class XEP_0234(object):
1571
c668081eba1c plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
51 # TODO: assure everything is closed when file is sent or session terminate is received
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
52 # TODO: call self._f.unregister when unloading order will be managing (i.e. when depenencies will be unloaded at the end)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def __init__(self, host):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 log.info(_("plugin Jingle File Transfer initialization"))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.host = host
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self._j = host.plugins["XEP-0166"] # shortcut to access jingle
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self._j.registerApplication(NS_JINGLE_FT, self)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self._f = host.plugins["FILE"]
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
60 self._f.register(NS_JINGLE_FT, self.fileJingleSend, priority = 10000, method_name=u"Jingle")
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 host.bridge.addMethod("fileJingleSend", ".plugin", in_sign='sssss', out_sign='', method=self._fileJingleSend)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def getHandler(self, profile):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 return XEP_0234_handler()
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65
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
66 def _getProgressId(self, session, content_name):
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
67 """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
68
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
69 @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
70 @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
71 @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
72 """
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
73 return u'{}_{}'.format(session['id'], content_name)
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
74
1567
268fda4236ca plugins XE0166, XEP-0234, XEP-0260, XEP-0261: renamed session key managing other peer's jid to "peer_jid" instead of "to_jid"
Goffi <goffi@goffi.org>
parents: 1566
diff changeset
75 def _fileJingleSend(self, peer_jid, filepath, name="", file_desc="", profile=C.PROF_KEY_NONE):
268fda4236ca plugins XE0166, XEP-0234, XEP-0260, XEP-0261: renamed session key managing other peer's jid to "peer_jid" instead of "to_jid"
Goffi <goffi@goffi.org>
parents: 1566
diff changeset
76 return self.fileJingleSend(jid.JID(peer_jid), filepath, name or None, file_desc or None, profile)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77
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
78 def fileJingleSend(self, peer_jid, filepath, name, file_desc=None, profile=C.PROF_KEY_NONE):
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
79 """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
80
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
81 @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
82 @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
83 @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
84 @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
85 @param profile: %(doc_profile)s
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
86 @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
87 """
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
88 progress_id_d = defer.Deferred()
1567
268fda4236ca plugins XE0166, XEP-0234, XEP-0260, XEP-0261: renamed session key managing other peer's jid to "peer_jid" instead of "to_jid"
Goffi <goffi@goffi.org>
parents: 1566
diff changeset
89 self._j.initiate(peer_jid,
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 [{'app_ns': NS_JINGLE_FT,
1557
22f0307864b4 plugin XEP-0234: "senders" handling
Goffi <goffi@goffi.org>
parents: 1556
diff changeset
91 'senders': self._j.ROLE_INITIATOR,
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 'app_kwargs': {'filepath': filepath,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 'name': 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
94 'file_desc': file_desc,
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
95 'progress_id_d': progress_id_d},
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 }],
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 profile=profile)
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
98 return progress_id_d
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 # jingle callbacks
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101
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
102 def jingleSessionInit(self, session, content_name, filepath, name, file_desc, progress_id_d, profile=C.PROF_KEY_NONE):
1598
b144babc2658 core, plugin file: fixed progress id + data is now returned by getProgress, instead of being an argument to fill
Goffi <goffi@goffi.org>
parents: 1585
diff changeset
103 progress_id_d.callback(self._getProgressId(session, content_name))
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 content_data = session['contents'][content_name]
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
105 application_data = content_data['application_data']
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
106 assert 'file_path' not in application_data
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
107 application_data['file_path'] = filepath
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
108 file_data = application_data['file_data'] = {}
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 file_data['date'] = utils.xmpp_date()
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 file_data['desc'] = file_desc or ''
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 file_data['media-type'] = "application/octet-stream" # TODO
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
112 file_data['name'] = os.path.basename(filepath) if name is None else name
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 file_data['size'] = os.path.getsize(filepath)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 desc_elt = domish.Element((NS_JINGLE_FT, 'description'))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115 file_elt = desc_elt.addElement("file")
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116 for name in ('date', 'desc', 'media-type', 'name', 'size'):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 file_elt.addElement(name, content=unicode(file_data[name]))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118 file_elt.addElement("range") # TODO
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 file_elt.addChild(self.host.plugins["XEP-0300"].buidHash())
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 return desc_elt
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 def jingleRequestConfirmation(self, action, session, content_name, desc_elt, profile):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 """This method request confirmation for a jingle session"""
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 content_data = session['contents'][content_name]
1557
22f0307864b4 plugin XEP-0234: "senders" handling
Goffi <goffi@goffi.org>
parents: 1556
diff changeset
125 if content_data['senders'] not in (self._j.ROLE_INITIATOR, self._j.ROLE_RESPONDER):
22f0307864b4 plugin XEP-0234: "senders" handling
Goffi <goffi@goffi.org>
parents: 1556
diff changeset
126 log.warning(u"Bad sender, assuming initiator")
22f0307864b4 plugin XEP-0234: "senders" handling
Goffi <goffi@goffi.org>
parents: 1556
diff changeset
127 content_data['senders'] = self._j.ROLE_INITIATOR
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 # first we grab file informations
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 try:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130 file_elt = desc_elt.elements(NS_JINGLE_FT, 'file').next()
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131 except StopIteration:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 raise failure.Failure(exceptions.DataError)
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
133 file_data = {'progress_id': self._getProgressId(session, content_name)}
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134 for name in ('date', 'desc', 'media-type', 'name', 'range', 'size'):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 try:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136 file_data[name] = unicode(file_elt.elements(NS_JINGLE_FT, name).next())
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137 except StopIteration:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 file_data[name] = ''
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
139
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
140 try:
1575
833bdb227b16 plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
141 file_data['size'] = int(file_data['size'])
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 except ValueError:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143 raise failure.Failure(exceptions.DataError)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
145 name = file_data['name']
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 if '/' in name or '\\' in name:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 log.warning(u"File name contain path characters, we replace them: {}".format(name))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 file_data['name'] = name.replace('/', '_').replace('\\', '_')
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 # TODO: parse hash using plugin XEP-0300
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
152 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
153
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
154 # 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
155 def gotConfirmation(confirmed):
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
156 if confirmed:
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
157 finished_d = content_data['finished_d'] = defer.Deferred()
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
158 args = [session, content_name, content_data, profile]
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
159 finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
160 return confirmed
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
161
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
162 d = self._f.getDestDir(session['peer_jid'], content_data, file_data, profile)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
163 d.addCallback(gotConfirmation)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
164 return d
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
165
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
166
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
167 def jingleHandler(self, action, session, content_name, desc_elt, profile):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 content_data = session['contents'][content_name]
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
169 application_data = content_data['application_data']
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
170 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
171 pass
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
172 elif action == self._j.A_SESSION_INITIATE:
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
173 file_elt = desc_elt.elements(NS_JINGLE_FT, 'file').next()
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
174 try:
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
175 file_elt.elements(NS_JINGLE_FT, 'range').next()
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
176 except StopIteration:
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
177 # initiator doesn't manage <range>, but we do so we advertise it
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
178 log.debug("adding <range> element")
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
179 file_elt.addElement('range')
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
180 elif action == self._j.A_SESSION_ACCEPT:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
181 assert not 'file_obj' in content_data
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
182 file_path = application_data['file_path']
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
183 size = application_data['file_data']['size']
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
184 content_data['file_obj'] = self._f.File(self.host,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
185 file_path,
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
186 uid=self._getProgressId(session, content_name),
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
187 size=size,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
188 profile=profile
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
189 )
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
190 finished_d = content_data['finished_d'] = defer.Deferred()
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
191 args = [session, content_name, content_data, profile]
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
192 finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193 else:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
194 log.warning(u"FIXME: unmanaged action {}".format(action))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
195 return desc_elt
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
197 def _finishedCb(self, dummy, session, content_name, content_data, profile):
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
198 log.info(u"File transfer completed successfuly")
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
199 if content_data['senders'] != session['role']:
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
200 # we terminate the session only if we are the received,
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
201 # as recommanded in XEP-0234 §2 (after example 6)
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
202 self._j.contentTerminate(session, content_name, profile=profile)
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
203 content_data['file_obj'].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
204
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
205 def _finishedEb(self, failure, session, content_name, content_data, profile):
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
206 log.warning(u"Error while streaming through s5b: {}".format(failure))
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
207 content_data['file_obj'].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
208 self._j.contentTerminate(session, content_name, reason=self._j.REASON_FAILED_TRANSPORT, profile=profile)
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
209
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
210
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
211 class XEP_0234_handler(XMPPHandler):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
212 implements(iwokkel.IDisco)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
213
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
214 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215 return [disco.DiscoFeature(NS_JINGLE_FT)]
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
218 return []