comparison src/plugins/plugin_xep_0096.py @ 394:8f3551ceee17

plugin XEP-0065: refactored and misc stuff fixed. Still not finished plugins XEP-0096: XEP-0065 (Socks5 stream method) managed
author Goffi <goffi@goffi.org>
date Mon, 03 Oct 2011 18:05:15 +0200
parents c34fd9d6242e
children cb0285372818
comparison
equal deleted inserted replaced
393:393b35aa86d2 394:8f3551ceee17
54 def __init__(self, host): 54 def __init__(self, host):
55 info(_("Plugin XEP_0096 initialization")) 55 info(_("Plugin XEP_0096 initialization"))
56 self.host = host 56 self.host = host
57 self._waiting_for_approval = {} #key = id, value = [transfer data, IdelayedCall Reactor timeout, 57 self._waiting_for_approval = {} #key = id, value = [transfer data, IdelayedCall Reactor timeout,
58 # current stream method, [failed stream methods], profile] 58 # current stream method, [failed stream methods], profile]
59 self.managed_stream_m = [#self.host.plugins["XEP-0065"].NS_BS, 59 self.managed_stream_m = [self.host.plugins["XEP-0065"].NAMESPACE,
60 self.host.plugins["XEP-0047"].NAMESPACE] #Stream methods managed 60 self.host.plugins["XEP-0047"].NAMESPACE] #Stream methods managed
61 self.host.plugins["XEP-0095"].registerSIProfile(PROFILE_NAME, self.transferRequest) 61 self.host.plugins["XEP-0095"].registerSIProfile(PROFILE_NAME, self.transferRequest)
62 host.bridge.addMethod("sendFile", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self.sendFile) 62 host.bridge.addMethod("sendFile", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self.sendFile)
63 63
64 def _kill_id(self, approval_id): 64 def _kill_id(self, approval_id):
153 except KeyError: 153 except KeyError:
154 error(_('dest path not found in frontend_data')) 154 error(_('dest path not found in frontend_data'))
155 del(self._waiting_for_approval[id]) 155 del(self._waiting_for_approval[id])
156 return 156 return
157 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE: 157 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
158 self.host.plugins["XEP-0065"].setData(data, id) 158 file_obj = self._getFileObject(dest_path, can_range)
159 range_offset = file_obj.tell()
160 self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), id, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed)
161 #self.host.plugins["XEP-0065"].setData(data, id)
159 elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE: 162 elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
160 file_obj = self._getFileObject(dest_path, can_range) 163 file_obj = self._getFileObject(dest_path, can_range)
161 range_offset = file_obj.tell() 164 range_offset = file_obj.tell()
162 self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), id, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed) 165 self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), id, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed)
163 else: 166 else:
197 filepath = file_obj.name 200 filepath = file_obj.name
198 file_obj.close() 201 file_obj.close()
199 os.remove(filepath) 202 os.remove(filepath)
200 #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 203 #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
201 warning(_("All stream methods failed, can't transfer the file")) 204 warning(_("All stream methods failed, can't transfer the file"))
202 self.host.plugins["XEP-0095"].sendFailedError(id, data['from'], profile) 205 del(self._waiting_for_approval[sid])
203 del(self._waiting_for_approval[id])
204 206
205 def fileCb(self, profile, filepath, sid, size, IQ): 207 def fileCb(self, profile, filepath, sid, size, IQ):
206 if IQ['type'] == "error": 208 if IQ['type'] == "error":
207 stanza_err = jab_error.exceptionFromStanza(IQ) 209 stanza_err = jab_error.exceptionFromStanza(IQ)
208 if stanza_err.code == '403' and stanza_err.condition == 'forbidden': 210 if stanza_err.code == '403' and stanza_err.condition == 'forbidden':
238 range_elt = range_elts[0] 240 range_elt = range_elts[0]
239 range_offset = range_elt.getAttribute("offset", 0) 241 range_offset = range_elt.getAttribute("offset", 0)
240 range_length = range_elt.getAttribute("length") 242 range_length = range_elt.getAttribute("length")
241 243
242 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE: 244 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
243 info("SENDING UGLY ANSWER") 245 file_obj = open(filepath, 'r')
244 """offer=client.IQ(xmlstream,'set') 246 if range_offset:
245 offer["from"]=current_jid.full() 247 file_obj.seek(range_offset)
246 offer["to"]=answer['from'] 248 self.host.plugins["XEP-0065"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self.sendSuccessCb, self.sendFailureCb, size, profile)
247 query=offer.addElement('query', 'http://jabber.org/protocol/ibb')
248 #query=offer.addElement('query', 'http://jabber.org/protocol/bytestreams')
249 query['mode']='tcp'
250 streamhost=query.addElement('streamhost')
251 streamhost['host']=self.host.memory.getParamA("IP", "File Transfer")
252 streamhost['port']=self.host.memory.getParamA("Port", "File Transfer")
253 streamhost['jid']=current_jid.full()
254 offer.send()"""
255 elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE: 249 elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
256 file_obj = open(filepath, 'r') 250 file_obj = open(filepath, 'r')
257 if range_offset: 251 if range_offset:
258 file_obj.seek(range_offset) 252 file_obj.seek(range_offset)
259 self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self.sendSuccessCb, self.sendFailureCb, size, profile) 253 self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self.sendSuccessCb, self.sendFailureCb, size, profile)
273 if not profile: 267 if not profile:
274 warning(_("Trying to send a file from an unknown profile")) 268 warning(_("Trying to send a file from an unknown profile"))
275 return "" 269 return ""
276 feature_elt = self.host.plugins["XEP-0020"].proposeFeatures({'stream-method': self.managed_stream_m}) 270 feature_elt = self.host.plugins["XEP-0020"].proposeFeatures({'stream-method': self.managed_stream_m})
277 271
278 #self.host.plugins["XEP-0065"].sendFile(offer["id"], filepath, str(statinfo.st_size))
279
280 file_transfer_elts = [] 272 file_transfer_elts = []
281 273
282 statinfo = os.stat(filepath) 274 statinfo = os.stat(filepath)
283 file_elt = domish.Element((PROFILE, 'file')) 275 file_elt = domish.Element((PROFILE, 'file'))
284 file_elt['name']=os.path.basename(filepath) 276 file_elt['name']=os.path.basename(filepath)
289 281
290 sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, file_transfer_elts, profile_key = profile) 282 sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, file_transfer_elts, profile_key = profile)
291 offer.addCallback(self.fileCb, profile, filepath, sid, size) 283 offer.addCallback(self.fileCb, profile, filepath, sid, size)
292 return sid 284 return sid
293 285
294
295 def sendSuccessCb(self, sid, file_obj, stream_method): 286 def sendSuccessCb(self, sid, file_obj, stream_method):
296 info(_('Transfer %s successfuly finished') % sid) 287 info(_('Transfer %s successfuly finished') % sid)
297 file_obj.close() 288 file_obj.close()
298 289
299 def sendFailureCb(self, sid, file_obj, stream_method, reason): 290 def sendFailureCb(self, sid, file_obj, stream_method, reason):