File transfer:

- proxy managed in XEP-0065 (Socks5 bytestream)
- bug: fixed a bad id used during stream negociation
This commit is contained in:
Goffi 2011-10-05 16:49:57 +02:00
commit dc936be42b

View file

@ -70,8 +70,9 @@ class XEP_0096():
except KeyError:
warning(_("kill id called on a non existant approval id"))
def transferRequest(self, from_jid, si_id, si_mime_type, si_el, profile):
def transferRequest(self, iq_id, from_jid, si_id, si_mime_type, si_el, profile):
"""Called when a file transfer is requested
@param iq_id: id of the iq request
@param from_jid: jid of the sender
@param si_id: Stream Initiation session id
@param si_mime_type: Mime type of the file (or default "application/octet-stream" if unknown)
@ -102,7 +103,7 @@ class XEP_0096():
can_range = True
else:
warning(_("No file element found"))
self.host.plugins["XEP-0095"].sendBadRequestError(si_id, from_jid, profile)
self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
return
if feature_elts:
@ -112,19 +113,19 @@ class XEP_0096():
stream_method = self.host.plugins["XEP-0020"].negociate(feature_el, 'stream-method',self.managed_stream_m)
except KeyError:
warning(_("No stream method found"))
self.host.plugins["XEP-0095"].sendBadRequestError(si_id, from_jid, profile)
self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
return
if not stream_method:
warning(_("Can't find a valid stream method"))
self.host.plugins["XEP-0095"].sendFailedError(si_id, from_jid, profile)
self.host.plugins["XEP-0095"].sendFailedError(iq_id, from_jid, profile)
return
else:
warning(_("No feature element found"))
self.host.plugins["XEP-0095"].sendBadRequestError(si_id, from_jid, profile)
self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
return
#if we are here, the transfer can start, we just need user's agreement
data={ "filename":filename, "from":from_jid, "size":file_size, "date":file_date, "hash":file_hash, "desc":file_desc, "can_range": str(can_range) }
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) }
self._waiting_for_approval[si_id] = [data, reactor.callLater(300, self._kill_id, si_id), stream_method, [], profile]
self.host.askConfirmation(si_id, "FILE_TRANSFER", data, self.confirmationCB)
@ -137,12 +138,12 @@ class XEP_0096():
@return: File Object"""
return open(dest_path, "ab" if can_range else "wb")
def confirmationCB(self, id, accepted, frontend_data):
def confirmationCB(self, sid, accepted, frontend_data):
"""Called on confirmation answer
@param id: file transfer session id
@param sid: file transfer session id
@param accepted: True if file transfer is accepted
@param frontend_data: data sent by frontend"""
data, timeout, stream_method, failed_methods, profile = self._waiting_for_approval[id]
data, timeout, stream_method, failed_methods, profile = self._waiting_for_approval[sid]
can_range = data['can_range'] == "True"
range_offset = 0
if accepted:
@ -152,20 +153,19 @@ class XEP_0096():
dest_path = frontend_data['dest_path']
except KeyError:
error(_('dest path not found in frontend_data'))
del(self._waiting_for_approval[id])
del(self._waiting_for_approval[sid])
return
if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
file_obj = self._getFileObject(dest_path, can_range)
range_offset = file_obj.tell()
self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), id, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed)
#self.host.plugins["XEP-0065"].setData(data, id)
self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), sid, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed)
elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
file_obj = self._getFileObject(dest_path, can_range)
range_offset = file_obj.tell()
self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), id, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed)
self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), sid, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed)
else:
error(_("Unknown stream method, this should not happen at this stage, cancelling transfer"))
del(self._waiting_for_approval[id])
del(self._waiting_for_approval[sid])
return
#we can send the iq result
@ -177,11 +177,11 @@ class XEP_0096():
range_elt['offset'] = str(range_offset)
#TODO: manage range length
misc_elts.append(range_elt)
self.host.plugins["XEP-0095"].acceptStream(id, data['from'], feature_elt, misc_elts, profile)
self.host.plugins["XEP-0095"].acceptStream(data["id"], data['from'], feature_elt, misc_elts, profile)
else:
debug (_("Transfer [%s] refused"), id)
self.host.plugins["XEP-0095"].sendRejectedError (id, data['from'], profile=profile)
del(self._waiting_for_approval[id])
debug (_("Transfer [%s] refused"), sid)
self.host.plugins["XEP-0095"].sendRejectedError (data["id"], data['from'], profile=profile)
del(self._waiting_for_approval[sid])
def _transferSucceeded(self, sid, file_obj, stream_method):
"""Called by the stream method when transfer successfuly finished
@ -289,4 +289,4 @@ class XEP_0096():
def sendFailureCb(self, sid, file_obj, stream_method, reason):
file_obj.close()
warning(_('Transfer %(id)s failed with stream method %(s_method)s') % { 'id': sid, s_method: stream_method })
warning(_('Transfer %(id)s failed with stream method %(s_method)s') % { 'id': sid, "s_method": stream_method })