comparison src/plugins/plugin_xep_0065.py @ 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
author Goffi <goffi@goffi.org>
date Fri, 28 Mar 2014 18:07:02 +0100
parents 1fe00f0c9a91
children e1842ebcb2f3
comparison
equal deleted inserted replaced
940:92e41e7c7e00 941:c6d8fc63b1db
254 self.bindRequested(addr, port) 254 self.bindRequested(addr, port)
255 else: 255 else:
256 # Any other command is not supported 256 # Any other command is not supported
257 self.sendErrorReply(REPLY_CMD_NOT_SUPPORTED) 257 self.sendErrorReply(REPLY_CMD_NOT_SUPPORTED)
258 258
259 except struct.error, why: 259 except struct.error:
260 return None 260 return None
261 261
262 def _makeRequest(self): 262 def _makeRequest(self):
263 debug("_makeRequest") 263 debug("_makeRequest")
264 self.state = STATE_TARGET_REQUEST 264 self.state = STATE_TARGET_REQUEST
298 self.factory.activateCb(self.sid, self.factory.iq_id, self.startTransfer, self.profile) 298 self.factory.activateCb(self.sid, self.factory.iq_id, self.startTransfer, self.profile)
299 else: 299 else:
300 self.state = STATE_TARGET_READY 300 self.state = STATE_TARGET_READY
301 self.factory.activateCb(self.sid, self.factory.iq_id, self.profile) 301 self.factory.activateCb(self.sid, self.factory.iq_id, self.profile)
302 302
303 except struct.error, why: 303 except struct.error:
304 return None 304 return None
305 305
306 def connectionMade(self): 306 def connectionMade(self):
307 debug("connectionMade (mode = %s)" % "requester" if isinstance(self.factory, Socks5ServerFactory) else "target") 307 debug("connectionMade (mode = %s)" % "requester" if isinstance(self.factory, Socks5ServerFactory) else "target")
308 308
321 #no: we refuse it 321 #no: we refuse it
322 self.sendErrorReply(REPLY_CONN_REFUSED) 322 self.sendErrorReply(REPLY_CONN_REFUSED)
323 return 323 return
324 self.sid, self.profile = self.factory.hash_sid_map[addr] 324 self.sid, self.profile = self.factory.hash_sid_map[addr]
325 client = self.factory.host.getClient(self.profile) 325 client = self.factory.host.getClient(self.profile)
326 if not client:
327 raise ProfileNotInCacheError
328 client.xep_0065_current_stream[self.sid]["start_transfer_cb"] = self.startTransfer 326 client.xep_0065_current_stream[self.sid]["start_transfer_cb"] = self.startTransfer
329 self.connectCompleted(addr, 0) 327 self.connectCompleted(addr, 0)
330 self.transport.stopReading() 328 self.transport.stopReading()
331 329
332 def startTransfer(self, file_obj): 330 def startTransfer(self, file_obj):
479 def getHandler(self, profile): 477 def getHandler(self, profile):
480 return XEP_0065_handler(self) 478 return XEP_0065_handler(self)
481 479
482 def profileConnected(self, profile): 480 def profileConnected(self, profile):
483 client = self.host.getClient(profile) 481 client = self.host.getClient(profile)
484 if not client:
485 raise ProfileNotInCacheError
486 client.xep_0065_current_stream = {} # key: stream_id, value: data(dict) 482 client.xep_0065_current_stream = {} # key: stream_id, value: data(dict)
487 483
488 def getExternalIP(self): 484 def getExternalIP(self):
489 """Return IP visible from outside, by asking to a website""" 485 """Return IP visible from outside, by asking to a website"""
490 return getPage("http://www.goffi.org/sat_tools/get_ip.php") 486 return getPage("http://www.goffi.org/sat_tools/get_ip.php")
491 487
492 def getProgress(self, sid, data, profile): 488 def getProgress(self, sid, data, profile):
493 """Fill data with position of current transfer""" 489 """Fill data with position of current transfer"""
494 client = self.host.getClient(profile) 490 client = self.host.getClient(profile)
495 if not client:
496 raise ProfileNotInCacheError
497 try: 491 try:
498 file_obj = client.xep_0065_current_stream[sid]["file_obj"] 492 file_obj = client.xep_0065_current_stream[sid]["file_obj"]
499 data["position"] = str(file_obj.tell()) 493 data["position"] = str(file_obj.tell())
500 data["size"] = str(client.xep_0065_current_stream[sid]["size"]) 494 data["size"] = str(client.xep_0065_current_stream[sid]["size"])
501 except: 495 except:
511 def _killId(self, sid, success=False, failure_reason="UNKNOWN", profile=None): 505 def _killId(self, sid, success=False, failure_reason="UNKNOWN", profile=None):
512 """Delete an current_stream id, clean up associated observers 506 """Delete an current_stream id, clean up associated observers
513 @param sid: id of client.xep_0065_current_stream""" 507 @param sid: id of client.xep_0065_current_stream"""
514 assert(profile) 508 assert(profile)
515 client = self.host.getClient(profile) 509 client = self.host.getClient(profile)
516 if not client:
517 warning(_("Client no more in cache"))
518 return
519 if sid not in client.xep_0065_current_stream: 510 if sid not in client.xep_0065_current_stream:
520 warning(_("kill id called on a non existant id")) 511 warning(_("kill id called on a non existant id"))
521 return 512 return
522 if "observer_cb" in client.xep_0065_current_stream[sid]: 513 if "observer_cb" in client.xep_0065_current_stream[sid]:
523 xmlstream = client.xep_0065_current_stream[sid]["xmlstream"] 514 xmlstream = client.xep_0065_current_stream[sid]["xmlstream"]
551 @param successCb: method to call when stream successfuly finished 542 @param successCb: method to call when stream successfuly finished
552 @param failureCb: method to call when something goes wrong 543 @param failureCb: method to call when something goes wrong
553 @param profile: %(doc_profile)s""" 544 @param profile: %(doc_profile)s"""
554 assert(profile) 545 assert(profile)
555 client = self.host.getClient(profile) 546 client = self.host.getClient(profile)
556 if not client:
557 error(_("Unknown profile, this should not happen"))
558 raise ProfileNotInCacheError
559 547
560 if length is not None: 548 if length is not None:
561 error(_('stream length not managed yet')) 549 error(_('stream length not managed yet'))
562 return 550 return
563 551
602 """Called when the result of open iq is received""" 590 """Called when the result of open iq is received"""
603 if iq_elt["type"] == "error": 591 if iq_elt["type"] == "error":
604 warning(_("Transfer failed")) 592 warning(_("Transfer failed"))
605 return 593 return
606 client = self.host.getClient(profile) 594 client = self.host.getClient(profile)
607 if not client:
608 raise ProfileNotInCacheError
609 try: 595 try:
610 data = client.xep_0065_current_stream[sid] 596 data = client.xep_0065_current_stream[sid]
611 file_obj = data["file_obj"] 597 file_obj = data["file_obj"]
612 timer = data["timer"] 598 timer = data["timer"]
613 except KeyError: 599 except KeyError:
639 data["start_transfer_cb"](file_obj) # We now activate the stream 625 data["start_transfer_cb"](file_obj) # We now activate the stream
640 626
641 def activateProxyStream(self, sid, iq_id, start_transfer_cb, profile): 627 def activateProxyStream(self, sid, iq_id, start_transfer_cb, profile):
642 debug(_("activating stream")) 628 debug(_("activating stream"))
643 client = self.host.getClient(profile) 629 client = self.host.getClient(profile)
644 if not client:
645 raise ProfileNotInCacheError
646 data = client.xep_0065_current_stream[sid] 630 data = client.xep_0065_current_stream[sid]
647 profile_jid, xmlstream = self.host.getJidNStream(profile) 631 profile_jid, xmlstream = self.host.getJidNStream(profile)
648 632
649 iq_elt = client.IQ(xmlstream, 'set') 633 iq_elt = client.IQ(xmlstream, 'set')
650 iq_elt["from"] = profile_jid.full() 634 iq_elt["from"] = profile_jid.full()
670 @param size: full size of the data, or None if unknown 654 @param size: full size of the data, or None if unknown
671 @param success_cb: method to call when successfuly finished 655 @param success_cb: method to call when successfuly finished
672 @param failure_cb: method to call when something goes wrong 656 @param failure_cb: method to call when something goes wrong
673 @param profile: %(doc_profile)s""" 657 @param profile: %(doc_profile)s"""
674 client = self.host.getClient(profile) 658 client = self.host.getClient(profile)
675 if not client:
676 raise ProfileNotInCacheError
677 data = client.xep_0065_current_stream[sid] = {} 659 data = client.xep_0065_current_stream[sid] = {}
678 data["from"] = from_jid 660 data["from"] = from_jid
679 data["file_obj"] = file_obj 661 data["file_obj"] = file_obj
680 data["seq"] = -1 662 data["seq"] = -1
681 if size: 663 if size:
729 factory = Socks5ClientFactory(client.xep_0065_current_stream, sid, iq_elt["id"], self.activateStream, lambda sid, success, profile: self._killId(sid, success, profile=profile), profile=profile) 711 factory = Socks5ClientFactory(client.xep_0065_current_stream, sid, iq_elt["id"], self.activateStream, lambda sid, success, profile: self._killId(sid, success, profile=profile), profile=profile)
730 reactor.connectTCP(sh_host, int(sh_port), factory) 712 reactor.connectTCP(sh_host, int(sh_port), factory)
731 713
732 def activateStream(self, sid, iq_id, profile): 714 def activateStream(self, sid, iq_id, profile):
733 client = self.host.getClient(profile) 715 client = self.host.getClient(profile)
734 if not client:
735 raise ProfileNotInCacheError
736 debug(_("activating stream")) 716 debug(_("activating stream"))
737 result = domish.Element((None, 'iq')) 717 result = domish.Element((None, 'iq'))
738 data = client.xep_0065_current_stream[sid] 718 data = client.xep_0065_current_stream[sid]
739 result['type'] = 'result' 719 result['type'] = 'result'
740 result['id'] = iq_id 720 result['id'] = iq_id
797 warning(_("No streamhost found in stream query")) 777 warning(_("No streamhost found in stream query"))
798 return 778 return
799 if len(streamhost_elts) != 1: 779 if len(streamhost_elts) != 1:
800 warning(_("Multiple streamhost elements in proxy not managed, keeping only the first one")) 780 warning(_("Multiple streamhost elements in proxy not managed, keeping only the first one"))
801 streamhost_elt = streamhost_elts[0] 781 streamhost_elt = streamhost_elts[0]
802 proxy = self.host.memory.setParam("Proxy", streamhost_elt.getAttribute("jid", ""), 782 self.host.memory.setParam("Proxy", streamhost_elt.getAttribute("jid", ""),
803 "File Transfer", profile_key=self.parent.profile) 783 "File Transfer", profile_key=self.parent.profile)
804 proxy = self.host.memory.setParam("Proxy host", streamhost_elt.getAttribute("host", ""), 784 self.host.memory.setParam("Proxy host", streamhost_elt.getAttribute("host", ""),
805 "File Transfer", profile_key=self.parent.profile) 785 "File Transfer", profile_key=self.parent.profile)
806 proxy = self.host.memory.setParam("Proxy port", streamhost_elt.getAttribute("port", ""), 786 self.host.memory.setParam("Proxy port", streamhost_elt.getAttribute("port", ""),
807 "File Transfer", profile_key=self.parent.profile) 787 "File Transfer", profile_key=self.parent.profile)
808 788
809 def connectionInitialized(self): 789 def connectionInitialized(self):
810 def after_init(ignore): 790 def after_init(ignore):
811 proxy_ent = self.host.memory.getServerServiceEntity("proxy", "bytestreams", profile=self.parent.profile) 791 proxy_ent = self.host.memory.getServerServiceEntity("proxy", "bytestreams", profile=self.parent.profile)
812 if not proxy_ent: 792 if not proxy_ent:
813 debug(_("No proxy found on this server")) 793 debug(_("No proxy found on this server"))
814 return 794 return
815 iq_elt = jabber_client.IQ(self.parent.xmlstream, 'get') 795 iq_elt = jabber_client.IQ(self.parent.xmlstream, 'get')
816 iq_elt["to"] = proxy_ent.full() 796 iq_elt["to"] = proxy_ent.full()
817 query_elt = iq_elt.addElement('query', NS_BS) 797 iq_elt.addElement('query', NS_BS)
818 iq_elt.addCallback(self._proxyDataResult) 798 iq_elt.addCallback(self._proxyDataResult)
819 iq_elt.send() 799 iq_elt.send()
820 800
821 self.xmlstream.addObserver(BS_REQUEST, self.plugin_parent.streamQuery, profile=self.parent.profile) 801 self.xmlstream.addObserver(BS_REQUEST, self.plugin_parent.streamQuery, profile=self.parent.profile)
822 proxy = self.host.memory.getParamA("Proxy", "File Transfer", profile_key=self.parent.profile) 802 proxy = self.host.memory.getParamA("Proxy", "File Transfer", profile_key=self.parent.profile)