annotate src/plugins/plugin_xep_0096.py @ 1542:94901070478e

plugins: added new MissingModule exceptions to plugins using third party modules
author Goffi <goffi@goffi.org>
date Wed, 30 Sep 2015 17:25:09 +0200
parents 7b0fcefd52d4
children 7cc29634b6ef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #!/usr/bin/python
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
4 # SAT plugin for managing xep-0096
1396
069ad98b360d 2015 copyright dates update
Goffi <goffi@goffi.org>
parents: 1179
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
10 # (at your option) any later version.
0
goffi@necton2
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
15 # GNU Affero General Public License for more details.
0
goffi@necton2
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
goffi@necton2
parents:
diff changeset
19
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 721
diff changeset
20 from sat.core.i18n import _
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 811
diff changeset
21 from sat.core.constants import Const as C
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
22 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
23 log = getLogger(__name__)
0
goffi@necton2
parents:
diff changeset
24 from twisted.words.xish import domish
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
25 from twisted.words.protocols.jabber import jid
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
26 from twisted.words.protocols import jabber
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
27 import os
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
28 from twisted.internet import reactor
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
29 from twisted.python import failure
0
goffi@necton2
parents:
diff changeset
30
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
31 from wokkel import data_form
15
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 8
diff changeset
32
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 8
diff changeset
33 IQ_SET = '/iq[@type="set"]'
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
34 PROFILE_NAME = "file-transfer"
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
35 PROFILE = "http://jabber.org/protocol/si/profile/" + PROFILE_NAME
15
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 8
diff changeset
36
0
goffi@necton2
parents:
diff changeset
37 PLUGIN_INFO = {
721
0077912bc9ba jp: removed named argument for pipeOut, as it's not managed with dynamicaly added methods
Goffi <goffi@goffi.org>
parents: 638
diff changeset
38 "name": "XEP-0096 Plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
39 "import_name": "XEP-0096",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
40 "type": "XEP",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
41 "protocols": ["XEP-0096"],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
42 "dependencies": ["XEP-0020", "XEP-0095", "XEP-0065", "XEP-0047"],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
43 "main": "XEP_0096",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
44 "handler": "no",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
45 "description": _("""Implementation of SI File Transfer""")
0
goffi@necton2
parents:
diff changeset
46 }
goffi@necton2
parents:
diff changeset
47
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
48
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
49 class XEP_0096(object):
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 15
diff changeset
50
0
goffi@necton2
parents:
diff changeset
51 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
52 log.info(_("Plugin XEP_0096 initialization"))
0
goffi@necton2
parents:
diff changeset
53 self.host = host
540
47e45a577ab7 plugin XEP-0096: restored XEP-0065 usage
Goffi <goffi@goffi.org>
parents: 538
diff changeset
54 self.managed_stream_m = [self.host.plugins["XEP-0065"].NAMESPACE,
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
55 self.host.plugins["XEP-0047"].NAMESPACE] # Stream methods managed
391
c34fd9d6242e spelling
Goffi <goffi@goffi.org>
parents: 386
diff changeset
56 self.host.plugins["XEP-0095"].registerSIProfile(PROFILE_NAME, self.transferRequest)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
57 host.bridge.addMethod("sendFile", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self.sendFile)
15
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 8
diff changeset
58
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
59 def profileConnected(self, profile):
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
60 client = self.host.getClient(profile)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
61 client._xep_0096_waiting_for_approval = {} # key = id, value = [transfer data, IdelayedCall Reactor timeout,
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
62 # current stream method, [failed stream methods], profile]
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
63
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
64 def _kill_id(self, approval_id, profile):
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
65 """Delete a waiting_for_approval id, called after timeout
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
66 @param approval_id: id of _xep_0096_waiting_for_approval"""
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
67 log.info(_("SI File Transfer: TimeOut reached for id %s") % approval_id)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
68 try:
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
69 client = self.host.getClient(profile)
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
70 del client._xep_0096_waiting_for_approval[approval_id]
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
71 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
72 log.warning(_("kill id called on a non existant approval id"))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
73
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
74 def transferRequest(self, iq_id, from_jid, si_id, si_mime_type, si_el, profile):
391
c34fd9d6242e spelling
Goffi <goffi@goffi.org>
parents: 386
diff changeset
75 """Called when a file transfer is requested
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
76 @param iq_id: id of the iq request
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
77 @param from_jid: jid of the sender
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
78 @param si_id: Stream Initiation session id
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
79 @param si_mime_type: Mime type of the file (or default "application/octet-stream" if unknown)
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
80 @param si_el: domish.Element of the request
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
81 @param profile: %(doc_profile)s"""
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
82 log.info(_("XEP-0096 file transfer requested"))
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
83 log.debug(si_el.toXml())
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
84 client = self.host.getClient(profile)
0
goffi@necton2
parents:
diff changeset
85 filename = ""
goffi@necton2
parents:
diff changeset
86 file_size = ""
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
87 file_date = None
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
88 file_hash = None
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
89 file_desc = ""
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
90 can_range = False
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
91 file_elts = filter(lambda elt: elt.name == 'file', si_el.elements())
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
92 feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_el)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
93
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
94 if file_elts:
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
95 file_el = file_elts[0]
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
96 filename = file_el["name"]
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
97 file_size = file_el["size"]
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
98 file_date = file_el.getAttribute("date", "")
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
99 file_hash = file_el.getAttribute("hash", "")
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
100 log.info(_(u"File proposed: name=[%(name)s] size=%(size)s") % {'name': filename, 'size': file_size})
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
101 for file_child_el in file_el.elements():
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
102 if file_child_el.name == "desc":
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
103 file_desc = unicode(file_child_el)
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
104 elif file_child_el.name == "range":
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
105 can_range = True
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
106 else:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
107 log.warning(_("No file element found"))
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
108 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
109 return
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
110
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
111 if feature_elts:
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
112 feature_el = feature_elts[0]
941
c6d8fc63b1db core, plugins: host.getClient now raise an exception instead of returning None when no profile is found, plugins have been adapted consequently and a bit cleaned
Goffi <goffi@goffi.org>
parents: 916
diff changeset
113 data_form.Form.fromElement(feature_el.firstChildElement())
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
114 try:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
115 stream_method = self.host.plugins["XEP-0020"].negociate(feature_el, 'stream-method', self.managed_stream_m)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
116 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
117 log.warning(_("No stream method found"))
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
118 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
119 return
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
120 if not stream_method:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
121 log.warning(_("Can't find a valid stream method"))
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
122 self.host.plugins["XEP-0095"].sendFailedError(iq_id, from_jid, profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
123 return
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
124 else:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
125 log.warning(_("No feature element found"))
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
126 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
127 return
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
128
391
c34fd9d6242e spelling
Goffi <goffi@goffi.org>
parents: 386
diff changeset
129 #if we are here, the transfer can start, we just need user's agreement
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
130 data = {"filename": filename, "id": iq_id, "from": from_jid, "size": file_size, "date": file_date, "hash": file_hash, "desc": file_desc, "can_range": str(can_range)}
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
131 client._xep_0096_waiting_for_approval[si_id] = [data, reactor.callLater(300, self._kill_id, si_id, profile), stream_method, []]
0
goffi@necton2
parents:
diff changeset
132
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
133 self.host.askConfirmation(si_id, "FILE_TRANSFER", data, self.confirmationCB, profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
134
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
135 def _getFileObject(self, dest_path, can_range=False):
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
136 """Open file, put file pointer to the end if the file if needed
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
137 @param dest_path: path of the destination file
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
138 @param can_range: True if the file pointer can be moved
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
139 @return: File Object"""
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
140 return open(dest_path, "ab" if can_range else "wb")
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
141
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
142 def confirmationCB(self, sid, accepted, frontend_data, profile):
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
143 """Called on confirmation answer
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
144 @param sid: file transfer session id
391
c34fd9d6242e spelling
Goffi <goffi@goffi.org>
parents: 386
diff changeset
145 @param accepted: True if file transfer is accepted
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
146 @param frontend_data: data sent by frontend"""
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
147 client = self.host.getClient(profile)
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
148 data, timeout, stream_method, failed_methods = client._xep_0096_waiting_for_approval[sid]
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
149 can_range = data['can_range'] == "True"
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
150 range_offset = 0
0
goffi@necton2
parents:
diff changeset
151 if accepted:
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
152 if timeout.active():
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
153 timeout.cancel()
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
154 try:
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
155 dest_path = frontend_data['dest_path']
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
156 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
157 log.error(_('dest path not found in frontend_data'))
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
158 del(client._xep_0096_waiting_for_approval[sid])
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
159 return
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
160 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 391
diff changeset
161 file_obj = self._getFileObject(dest_path, can_range)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 391
diff changeset
162 range_offset = file_obj.tell()
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
163 self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), sid, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed, profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
164 elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
165 file_obj = self._getFileObject(dest_path, can_range)
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
166 range_offset = file_obj.tell()
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
167 d = self.host.plugins["XEP-0047"].createSession(file_obj, jid.JID(data['from']), sid, int(data["size"]), profile)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
168 d.addCallback(self._transferSucceeded, sid, file_obj, self.host.plugins["XEP-0047"].NAMESPACE, profile)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
169 d.addErrback(self._transferFailed, sid, file_obj, self.host.plugins["XEP-0047"].NAMESPACE, profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
170 else:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
171 log.error(_("Unknown stream method, this should not happen at this stage, cancelling transfer"))
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
172 del(client._xep_0096_waiting_for_approval[sid])
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
173 return
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
174
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
175 #we can send the iq result
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
176 feature_elt = self.host.plugins["XEP-0020"].chooseOption({'stream-method': stream_method})
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
177 misc_elts = []
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
178 misc_elts.append(domish.Element((PROFILE, "file")))
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
179 if can_range:
536
a31abb97310d core, plugins: fixed bad namespace in stanza creation
Goffi <goffi@goffi.org>
parents: 480
diff changeset
180 range_elt = domish.Element((None, "range"))
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
181 range_elt['offset'] = str(range_offset)
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
182 #TODO: manage range length
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
183 misc_elts.append(range_elt)
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
184 self.host.plugins["XEP-0095"].acceptStream(data["id"], data['from'], feature_elt, misc_elts, profile)
0
goffi@necton2
parents:
diff changeset
185 else:
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
186 log.debug(_(u"Transfer [%s] refused") % sid)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
187 self.host.plugins["XEP-0095"].sendRejectedError(data["id"], data['from'], profile=profile)
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
188 del(client._xep_0096_waiting_for_approval[sid])
0
goffi@necton2
parents:
diff changeset
189
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
190 def _transferSucceeded(self, dummy, sid, file_obj, stream_method, profile):
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
191 self.transferSucceeded(sid, file_obj, stream_method, profile)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
192
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
193 def transferSucceeded(self, dummy, sid, file_obj, stream_method, profile):
391
c34fd9d6242e spelling
Goffi <goffi@goffi.org>
parents: 386
diff changeset
194 """Called by the stream method when transfer successfuly finished
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
195 @param id: stream id"""
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
196 client = self.host.getClient(profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
197 file_obj.close()
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
198 log.info(_('Transfer %s successfuly finished') % sid)
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
199 del(client._xep_0096_waiting_for_approval[sid])
0
goffi@necton2
parents:
diff changeset
200
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
201 def _transferFailed(self, sid, file_obj, stream_method, reason, profile):
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
202 self.transferFailed(failure.Failure(Exception(reason)), sid, file_obj, stream_method, profile)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
203
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
204 def transferFailed(self, failure, sid, file_obj, stream_method, profile):
391
c34fd9d6242e spelling
Goffi <goffi@goffi.org>
parents: 386
diff changeset
205 """Called when something went wrong with the transfer
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
206
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
207 @param id: stream id
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
208 """
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
209 client = self.host.getClient(profile)
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
210 data, timeout, stream_method, failed_methods = client._xep_0096_waiting_for_approval[sid]
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
211 log.warning(_(u'Transfer %(id)s failed with stream method %(s_method)s: %(reason)s') % {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
212 'id': sid,
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
213 's_method': stream_method,
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
214 'reason': unicode(failure)})
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
215 filepath = file_obj.name
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
216 file_obj.close()
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
217 os.remove(filepath)
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
218 #TODO: session remenber (within a time limit) when a stream method fail, and avoid that stream method with full jid for the rest of the session
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
219 log.warning(_("All stream methods failed, can't transfer the file"))
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
220 del(client._xep_0096_waiting_for_approval[sid])
0
goffi@necton2
parents:
diff changeset
221
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
222 def fileCb(self, filepath, sid, size, profile, IQ):
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
223 if IQ['type'] == "error":
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
224 stanza_err = jabber.error.exceptionFromStanza(IQ)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
225 if stanza_err.code == '403' and stanza_err.condition == 'forbidden':
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
226 log.debug(_(u"File transfer refused by %s") % IQ['from'])
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
227 self.host.bridge.newAlert(_("The contact %s refused your file") % IQ['from'], _("File refused"), "INFO", profile)
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
228 else:
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
229 log.warning(_(u"Error during file transfer with %s") % IQ['from'])
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
230 self.host.bridge.newAlert(_("Something went wrong during the file transfer session intialisation with %s") % IQ['from'], _("File transfer error"), "ERROR", profile)
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
231 return
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
232
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
233 si_elt = IQ.firstChildElement()
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
234
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
235 if IQ['type'] != "result" or not si_elt or si_elt.name != "si":
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
236 log.error(_("Protocol error during file transfer"))
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
237 return
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
238
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
239 feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_elt)
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
240 if not feature_elts:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
241 log.warning(_("No feature element"))
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
242 return
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
243
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
244 choosed_options = self.host.plugins["XEP-0020"].getChoosedOptions(feature_elts[0])
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
245 try:
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
246 stream_method = choosed_options["stream-method"]
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
247 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
248 log.warning(_("No stream method choosed"))
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
249 return
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
250
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
251 range_offset = 0
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
252 range_length = None
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
253 range_elts = filter(lambda elt: elt.name == 'range', si_elt.elements())
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
254 if range_elts:
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
255 range_elt = range_elts[0]
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
256 range_offset = range_elt.getAttribute("offset", 0)
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
257 range_length = range_elt.getAttribute("length")
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
258
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
259 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
260 file_obj = open(filepath, 'r')
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
261 if range_offset:
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
262 file_obj.seek(range_offset)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
263 self.host.plugins["XEP-0065"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self._sendSuccessCb, self._sendFailureCb, size, profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
264 elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
265 file_obj = open(filepath, 'r')
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
266 if range_offset:
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
267 file_obj.seek(range_offset)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
268 d = self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, profile=profile)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
269 d.addCallback(self.sendSuccessCb, sid, file_obj, self.host.plugins["XEP-0047"].NAMESPACE, profile)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
270 d.addErrback(self.sendFailureCb, sid, file_obj, self.host.plugins["XEP-0047"].NAMESPACE, profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
271 else:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
272 log.warning(_("Invalid stream method received"))
0
goffi@necton2
parents:
diff changeset
273
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 811
diff changeset
274 def sendFile(self, to_jid, filepath, data={}, profile_key=C.PROF_KEY_NONE):
0
goffi@necton2
parents:
diff changeset
275 """send a file using XEP-0096
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
276 @to_jid: recipient
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
277 @filepath: absolute path to the file to send
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
278 @data: dictionnary with the optional following keys:
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
279 - "description": description of the file
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
280 @param profile_key: %(doc_profile_key)s
391
c34fd9d6242e spelling
Goffi <goffi@goffi.org>
parents: 386
diff changeset
281 @return: an unique id to identify the transfer
0
goffi@necton2
parents:
diff changeset
282 """
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
283 profile = self.host.memory.getProfileName(profile_key)
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
284 if not profile:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
285 log.warning(_("Trying to send a file from an unknown profile"))
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
286 return ""
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
287 feature_elt = self.host.plugins["XEP-0020"].proposeFeatures({'stream-method': self.managed_stream_m})
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
288
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
289 file_transfer_elts = []
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
290
995
4dbe8e57ff51 plugin XEP-0096: fixed a bug introduced by the log refactoring
Goffi <goffi@goffi.org>
parents: 993
diff changeset
291 statinfo = os.stat(filepath)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
292 file_elt = domish.Element((PROFILE, 'file'))
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
293 file_elt['name'] = os.path.basename(filepath)
995
4dbe8e57ff51 plugin XEP-0096: fixed a bug introduced by the log refactoring
Goffi <goffi@goffi.org>
parents: 993
diff changeset
294 size = file_elt['size'] = str(statinfo.st_size)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
295 file_transfer_elts.append(file_elt)
0
goffi@necton2
parents:
diff changeset
296
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
297 file_transfer_elts.append(domish.Element((None, 'range')))
0
goffi@necton2
parents:
diff changeset
298
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
299 sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, file_transfer_elts, profile_key=profile)
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
300 offer.addCallback(self.fileCb, filepath, sid, size, profile)
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
301 return sid
0
goffi@necton2
parents:
diff changeset
302
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
303 def _sendSuccessCb(self, sid, file_obj, stream_method, profile):
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
304 self.sendSuccessCb(sid, file_obj, stream_method, profile)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
305
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
306 def sendSuccessCb(self, dummy, sid, file_obj, stream_method, profile):
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
307 log.info(_(u'Transfer %(sid)s successfuly finished [%(profile)s]')
638
6821fc06a324 misc: a few "cosmetic" changes (PEP 8...)
souliane <souliane@mailoo.org>
parents: 609
diff changeset
308 % {"sid": sid, "profile": profile})
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
309 file_obj.close()
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
310
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
311 def _sendFailureCb(self, sid, file_obj, stream_method, reason, profile):
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
312 self.sendFailureCb(failure.Failure(Exception(reason)), sid, file_obj, stream_method, profile)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
313
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
314 def sendFailureCb(self, failure, sid, file_obj, stream_method, profile):
386
deeebf697d9a plugins: plugin XEP-0096 update, use of XEP-0047 (IBB)
Goffi <goffi@goffi.org>
parents: 372
diff changeset
315 file_obj.close()
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
316 log.warning(_(u'Transfer %(id)s failed with stream method %(s_method)s: %(reason)s [%(profile)s]') % {'id': sid, "s_method": stream_method, 'reason': unicode(failure), 'profile': profile})