annotate src/plugins/plugin_xep_0234.py @ 1557:22f0307864b4

plugin XEP-0234: "senders" handling
author Goffi <goffi@goffi.org>
date Mon, 02 Nov 2015 22:02:41 +0100
parents cbfbe028d099
children ec3848916ee8
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
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _, D_
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 sat.tools import xml_tools
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from wokkel import disco, iwokkel
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from zope.interface import implements
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.tools import utils
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import os.path
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from twisted.words.xish import domish
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from twisted.words.protocols.jabber import jid
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from twisted.python import failure
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 try:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from twisted.words.protocols.xmlstream import XMPPHandler
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 except ImportError:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from wokkel.subprotocols import XMPPHandler
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 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
41 CONFIRM = D_(u'{entity} wants to send the file "{name}" to you:\n{desc}\n\nThe file has a size of {size_human}\n\nDo you accept ?')
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 CONFIRM_TITLE = D_(u'Confirm file transfer')
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 CONFIRM_OVERWRITE = D_(u'File {} already exists, are you sure you want to overwrite ?')
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 CONFIRM_OVERWRITE_TITLE = D_(u'File exists')
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 PLUGIN_INFO = {
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "name": "Jingle File Transfer",
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "import_name": "XEP-0234",
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "type": "XEP",
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "protocols": ["XEP-0234"],
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "dependencies": ["XEP-0166", "XEP-0300", "FILE"],
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "main": "XEP_0234",
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 "handler": "yes",
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "description": _("""Implementation of Jingle File Transfer""")
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 }
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 class XEP_0234(object):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def __init__(self, host):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 log.info(_("plugin Jingle File Transfer initialization"))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.host = host
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 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
64 self._j.registerApplication(NS_JINGLE_FT, self)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self._f = host.plugins["FILE"]
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 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
67
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 def getHandler(self, profile):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 return XEP_0234_handler()
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def _fileJingleSend(self, to_jid, filepath, name="", file_desc="", profile=C.PROF_KEY_NONE):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return self.fileJingleSend(jid.JID(to_jid), filepath, name or None, file_desc or None, profile)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def fileJingleSend(self, to_jid, filepath, name=None, file_desc=None, profile=C.PROF_KEY_NONE):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self._j.initiate(to_jid,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 [{'app_ns': NS_JINGLE_FT,
1557
22f0307864b4 plugin XEP-0234: "senders" handling
Goffi <goffi@goffi.org>
parents: 1556
diff changeset
77 'senders': self._j.ROLE_INITIATOR,
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 'app_kwargs': {'filepath': filepath,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 'name': name,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 'file_desc': file_desc},
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 }],
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 profile=profile)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 # Dialogs with user
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 # the overwrite check is done here
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def _getDestDir(self, session, content_data, profile):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 """Request confirmation and destination dir to user
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 if transfer is confirmed, session is filled
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 @param session(dict): jingle session data
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 @param content_data(dict): content informations
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 @param profile: %(doc_profile)s
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 return (defer.Deferred): True if transfer is accepted
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 """
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
97 application_data = content_data['application_data']
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
98 file_data = application_data['file_data']
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 d = xml_tools.deferDialog(self.host,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 _(CONFIRM).format(entity=session['to_jid'].full(), **file_data),
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 _(CONFIRM_TITLE),
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 type_=C.XMLUI_DIALOG_FILE,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 options={C.XMLUI_DATA_FILETYPE: C.XMLUI_DATA_FILETYPE_DIR},
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 profile=profile)
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
105 d.addCallback(self._gotConfirmation, session, content_data, application_data, profile)
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106 return d
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
108 def _gotConfirmation(self, data, session, content_data, application_data, profile):
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 """Called when the permission and dest path have been received
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 @param data(dict): xmlui data received from file dialog
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
112 return (bool): True if copy is wanted and OK
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 False if user wants to cancel
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 if fill exists ask confirmation and call again self._getDestDir if needed
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115 """
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116 if data.get('cancelled', False):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 return False
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
118 file_data = application_data['file_data']
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 path = data['path']
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 file_data['file_path'] = file_path = os.path.join(path, file_data['name'])
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 log.debug(u'destination file path set to {}'.format(file_path))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 # we manage case where file already exists
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 if os.path.exists(file_path):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
125 def check_overwrite(overwrite):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 if overwrite:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127 assert 'file_obj' not in content_data
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 content_data['file_obj'] = open(file_path, 'w')
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return True
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130 else:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131 return self._getDestDir(session, content_data, profile)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133 exists_d = xml_tools.deferConfirm(
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134 self.host,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 _(CONFIRM_OVERWRITE).format(file_path),
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136 _(CONFIRM_OVERWRITE_TITLE),
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137 profile=profile)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 exists_d.addCallback(check_overwrite)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
139 return exists_d
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
140
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141 assert 'file_obj' not in content_data
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 content_data['file_obj'] = open(file_path, 'w')
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143 return True
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 # jingle callbacks
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 def jingleSessionInit(self, session, content_name, filepath, name=None, file_desc=None, profile=C.PROF_KEY_NONE):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 content_data = session['contents'][content_name]
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
149 application_data = content_data['application_data']
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
150 assert 'file_path' not in application_data
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
151 application_data['file_path'] = filepath
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
152 file_data = application_data['file_data'] = {}
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153 file_data['date'] = utils.xmpp_date()
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
154 file_data['desc'] = file_desc or ''
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155 file_data['media-type'] = "application/octet-stream" # TODO
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156 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
157 file_data['size'] = os.path.getsize(filepath)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158 desc_elt = domish.Element((NS_JINGLE_FT, 'description'))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
159 file_elt = desc_elt.addElement("file")
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 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
161 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
162 file_elt.addElement("range") # TODO
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
163 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
164 return desc_elt
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 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
167 """This method request confirmation for a jingle session"""
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 content_data = session['contents'][content_name]
1557
22f0307864b4 plugin XEP-0234: "senders" handling
Goffi <goffi@goffi.org>
parents: 1556
diff changeset
169 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
170 log.warning(u"Bad sender, assuming initiator")
22f0307864b4 plugin XEP-0234: "senders" handling
Goffi <goffi@goffi.org>
parents: 1556
diff changeset
171 content_data['senders'] = self._j.ROLE_INITIATOR
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172 # first we grab file informations
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
173 try:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
174 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
175 except StopIteration:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
176 raise failure.Failure(exceptions.DataError)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
177 file_data = {}
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
178 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
179 try:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
180 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
181 except StopIteration:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
182 file_data[name] = ''
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
183
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
184 try:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
185 size = file_data['size'] = int(file_data['size'])
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
186 except ValueError:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187 raise failure.Failure(exceptions.DataError)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
188 else:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
189 # human readable size
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
190 file_data['size_human'] = u'{:.6n} Mio'.format(float(size)/(1024**2))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
191
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192 name = file_data['name']
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193 if '/' in name or '\\' in name:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
194 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
195 file_data['name'] = name.replace('/', '_').replace('\\', '_')
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
197 # TODO: parse hash using plugin XEP-0300
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
198
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
199 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
200
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
201 # now we actualy request permission to user
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
202 return self._getDestDir(session, content_data, profile)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
204
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205 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
206 content_data = session['contents'][content_name]
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
207 application_data = content_data['application_data']
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
208 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
209 pass
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
210 elif action == self._j.A_SESSION_INITIATE:
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
211 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
212 try:
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
213 file_elt.elements(NS_JINGLE_FT, 'range').next()
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
214 except StopIteration:
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
215 # 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
216 log.debug("adding <range> element")
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
217 file_elt.addElement('range')
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
218 elif action == self._j.A_SESSION_ACCEPT:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
219 assert not 'file_obj' in content_data
1556
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
220 file_path = application_data['file_path']
cbfbe028d099 plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents: 1529
diff changeset
221 size = application_data['file_data']['size']
1528
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
222 file_obj = content_data['file_obj'] = self._f.File(self.host,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
223 file_path,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
224 size=size,
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
225 profile=profile
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
226 )
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
227 file_obj.eof.addCallback(lambda dummy: file_obj.close())
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
228 else:
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
229 log.warning(u"FIXME: unmanaged action {}".format(action))
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
230 return desc_elt
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
231
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
232
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
233 class XEP_0234_handler(XMPPHandler):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
234 implements(iwokkel.IDisco)
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
235
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
236 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
237 return [disco.DiscoFeature(NS_JINGLE_FT)]
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
238
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
239 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
1c71d7335d02 plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
240 return []