wokkel integration part III + memory saved again
- disco handler (plugins handled)
This commit is contained in:
parent
af75e43558
commit
fe0cf89f64
3 changed files with 86 additions and 42 deletions
|
|
@ -63,6 +63,20 @@ import struct
|
|||
from binascii import hexlify
|
||||
import hashlib, pdb
|
||||
|
||||
from zope.interface import implements
|
||||
|
||||
try:
|
||||
from twisted.words.protocols.xmlstream import XMPPHandler
|
||||
except ImportError:
|
||||
from wokkel.subprotocols import XMPPHandler
|
||||
|
||||
from wokkel import disco, iwokkel
|
||||
|
||||
IQ_SET = '/iq[@type="set"]'
|
||||
NS_BS = 'http://jabber.org/protocol/bytestreams'
|
||||
BS_REQUEST = IQ_SET + '/query[@xmlns="' + NS_BS + '"]'
|
||||
|
||||
|
||||
|
||||
PLUGIN_INFO = {
|
||||
"name": "XEP 0065 Plugin",
|
||||
|
|
@ -434,7 +448,9 @@ class Socks5ClientFactory(protocol.ClientFactory):
|
|||
debug ("Socks 5 client connection lost (reason: %s)", reason)
|
||||
|
||||
|
||||
class XEP_0065():
|
||||
class XEP_0065(XMPPHandler):
|
||||
implements(iwokkel.IDisco)
|
||||
|
||||
def __init__(self, host):
|
||||
info("Plugin XEP_0065 initialization")
|
||||
self.host = host
|
||||
|
|
@ -442,10 +458,20 @@ class XEP_0065():
|
|||
self.server_factory = Socks5ServerFactory()
|
||||
self.server_factory.protocol.host = self.host #needed for progress CB
|
||||
self.client_factory = Socks5ClientFactory()
|
||||
host.add_IQ_cb("http://jabber.org/protocol/bytestreams", self.getFile)
|
||||
port = int(self.host.memory.getParamV("Port", "File Transfert"))
|
||||
info("Launching Socks5 Stream server on port %d", port)
|
||||
reactor.listenTCP(port, self.server_factory)
|
||||
|
||||
def connectionInitialized(self):
|
||||
self.xmlstream.addObserver(BS_REQUEST, self.getFile)
|
||||
|
||||
|
||||
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
|
||||
return [disco.DiscoFeature(NS_BS)]
|
||||
|
||||
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
|
||||
return []
|
||||
|
||||
|
||||
def setData(self, data, id):
|
||||
self.data = data
|
||||
|
|
@ -459,10 +485,11 @@ class XEP_0065():
|
|||
self.server_factory.protocol.filesize = size
|
||||
self.server_factory.protocol.transfert_id = id
|
||||
|
||||
def getFile(self, stanza):
|
||||
def getFile(self, iq):
|
||||
"""Get file using byte stream"""
|
||||
SI_elem = stanza.firstChildElement()
|
||||
IQ_id = stanza['id']
|
||||
iq.handled = True
|
||||
SI_elem = iq.firstChildElement()
|
||||
IQ_id = iq['id']
|
||||
for element in SI_elem.elements():
|
||||
if element.name == "streamhost":
|
||||
info ("Stream proposed: host=[%s] port=[%s]", element['host'], element['port'])
|
||||
|
|
|
|||
|
|
@ -27,6 +27,19 @@ import os.path
|
|||
from twisted.internet import reactor #FIXME best way ???
|
||||
import pdb
|
||||
|
||||
from zope.interface import implements
|
||||
|
||||
try:
|
||||
from twisted.words.protocols.xmlstream import XMPPHandler
|
||||
except ImportError:
|
||||
from wokkel.subprotocols import XMPPHandler
|
||||
|
||||
from wokkel import disco, iwokkel
|
||||
|
||||
IQ_SET = '/iq[@type="set"]'
|
||||
NS_SI = 'http://jabber.org/protocol/si'
|
||||
SI_REQUEST = IQ_SET + '/si[@xmlns="' + NS_SI + '"]'
|
||||
|
||||
PLUGIN_INFO = {
|
||||
"name": "XEP 0096 Plugin",
|
||||
"import_name": "XEP_0096",
|
||||
|
|
@ -36,16 +49,28 @@ PLUGIN_INFO = {
|
|||
"description": """Implementation of SI File Transfert"""
|
||||
}
|
||||
|
||||
class XEP_0096:
|
||||
class XEP_0096(XMPPHandler):
|
||||
implements(iwokkel.IDisco)
|
||||
|
||||
def __init__(self, host):
|
||||
info("Plugin XEP_0096 initialization")
|
||||
self.host = host
|
||||
self._waiting_for_approval = {}
|
||||
host.add_IQ_cb("http://jabber.org/protocol/si", self.xep_96)
|
||||
host.bridge.addMethod("sendFile", ".communication", in_sign='ss', out_sign='s', method=self.sendFile)
|
||||
|
||||
def connectionInitialized(self):
|
||||
self.xmlstream.addObserver(SI_REQUEST, self.xep_96)
|
||||
|
||||
|
||||
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
|
||||
return [disco.DiscoFeature(NS_SI)]
|
||||
|
||||
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
|
||||
return []
|
||||
|
||||
def xep_96(self, IQ):
|
||||
info ("XEP-0096 management")
|
||||
IQ.handled=True
|
||||
SI_elem = IQ.firstChildElement()
|
||||
debug(SI_elem.toXml())
|
||||
filename = ""
|
||||
|
|
|
|||
62
sat.tac
62
sat.tac
|
|
@ -44,6 +44,11 @@ import os.path
|
|||
from tools.memory import Memory
|
||||
from glob import glob
|
||||
|
||||
try:
|
||||
from twisted.words.protocols.xmlstream import XMPPHandler
|
||||
except ImportError:
|
||||
from wokkel.subprotocols import XMPPHandler
|
||||
|
||||
|
||||
### logging configuration FIXME: put this elsewhere ###
|
||||
logging.basicConfig(level=logging.DEBUG,
|
||||
|
|
@ -174,6 +179,16 @@ class SatDiscoProtocol(disco.DiscoClientProtocol):
|
|||
def __init__(self, host):
|
||||
disco.DiscoClientProtocol.__init__(self)
|
||||
|
||||
class SatFallbackHandler(generic.FallbackHandler):
|
||||
def __init__(self, host):
|
||||
generic.FallbackHandler.__init__(self)
|
||||
|
||||
def iqFallback(self, iq):
|
||||
#pdb.set_trace()
|
||||
print "iqFallback: xml = [%s], handled=%s" % (iq.toXml(), "True" if iq.handled else "False")
|
||||
generic.FallbackHandler.iqFallback(self, iq)
|
||||
|
||||
|
||||
class SAT(service.Service):
|
||||
|
||||
def __init__(self):
|
||||
|
|
@ -181,7 +196,6 @@ class SAT(service.Service):
|
|||
self.memory=Memory()
|
||||
self.server_features=[] #XXX: temp dic, need to be transfered into self.memory in the future
|
||||
|
||||
self._iq_cb_map = {} #callback called when ns is found on IQ
|
||||
self._waiting_conf = {} #callback called when a confirmation is received
|
||||
self._progress_cb_map = {} #callback called when a progress is requested (key = progress id)
|
||||
self.plugins = {}
|
||||
|
|
@ -240,12 +254,17 @@ class SAT(service.Service):
|
|||
self.presence = SatPresenceProtocol(self)
|
||||
self.presence.setHandlerParent(self.xmppclient)
|
||||
|
||||
self.fallBack = generic.FallbackHandler()
|
||||
self.fallBack = SatFallbackHandler(self)
|
||||
self.fallBack.setHandlerParent(self.xmppclient)
|
||||
|
||||
self.versionHandler = generic.VersionHandler(unicode(client_name), client_version)
|
||||
self.versionHandler.setHandlerParent(self.xmppclient)
|
||||
|
||||
debug ("setting plugins parents")
|
||||
for plugin in self.plugins.iteritems():
|
||||
if isinstance(plugin[1], XMPPHandler):
|
||||
plugin[1].setHandlerParent(self.xmppclient)
|
||||
|
||||
self.xmppclient.startService()
|
||||
|
||||
def disconnect(self):
|
||||
|
|
@ -260,6 +279,7 @@ class SAT(service.Service):
|
|||
self.connect()
|
||||
|
||||
def stopService(self):
|
||||
self.memory.save()
|
||||
info("Salut aussi à Rantanplan")
|
||||
|
||||
def run(self):
|
||||
|
|
@ -277,21 +297,16 @@ class SAT(service.Service):
|
|||
self.xmlstream = self.xmppclient.xmlstream
|
||||
self.me = self.xmppclient.jid #in case of the ressource has changed
|
||||
|
||||
self.disco = SatDiscoProtocol(self)
|
||||
self.disco.setHandlerParent(self.xmppclient)
|
||||
self.discoHandler = disco.DiscoHandler()
|
||||
self.discoHandler.setHandlerParent(self.xmppclient)
|
||||
|
||||
self.roster.requestRoster()
|
||||
|
||||
self.presence.available()
|
||||
|
||||
#FIXME:tmp
|
||||
self.xmlstream.addObserver("/iq[@type='set' or @type='get']", self.iqCb)
|
||||
###FIXME: tmp disco ###
|
||||
self.disco = SatDiscoProtocol(self)
|
||||
self.disco.setHandlerParent(self.xmppclient)
|
||||
self.disco.requestInfo(jid.JID(self.memory.getParamV("Server", "Connection"))).addCallback(self.serverDisco)
|
||||
#we now send our presence status
|
||||
|
||||
# add a callback for the messages
|
||||
|
||||
#reactor.callLater(2,self.sendFile,"goffi2@jabber.goffi.int/Psi", "/tmp/fakefile")
|
||||
|
||||
|
||||
def sendMessage(self,to,msg,type='chat'):
|
||||
|
|
@ -363,25 +378,6 @@ class SAT(service.Service):
|
|||
|
||||
## callbacks ##
|
||||
|
||||
def add_IQ_cb(self, ns, cb):
|
||||
"""Add an IQ callback on namespace ns"""
|
||||
debug ("Registered callback for namespace %s", ns)
|
||||
self._iq_cb_map[ns]=cb
|
||||
|
||||
def iqCb(self, stanza):
|
||||
info ("iqCb")
|
||||
debug ("="*20)
|
||||
debug ("DEBUG:\n")
|
||||
debug (stanza.toXml().encode('utf-8'))
|
||||
debug ("="*20)
|
||||
#FIXME: temporary ugly code
|
||||
uri = stanza.firstChildElement().uri
|
||||
if self._iq_cb_map.has_key(uri):
|
||||
self._iq_cb_map[uri](stanza)
|
||||
#TODO: manage errors stanza
|
||||
|
||||
|
||||
|
||||
def serverDisco(self, disco):
|
||||
"""xep-0030 Discovery Protocol."""
|
||||
for feature in disco.features:
|
||||
|
|
@ -439,7 +435,3 @@ class SAT(service.Service):
|
|||
application = service.Application('SàT')
|
||||
service = SAT()
|
||||
service.setServiceParent(application)
|
||||
|
||||
|
||||
#app.memory.save() #FIXME: not the best place
|
||||
#debug("Good Bye")
|
||||
|
|
|
|||
Loading…
Reference in a new issue