Exemplo n.º 1
0
 def onAuthenticated(self, xs):
     """
     xmlstream.STREAM_AUTHD_EVENT handler.
     Calls when client authenticated on server.
     Setup dispatcher and any features for client
     """
     
     self.dispatcher = Dispatcher(xs, self.client_jid)
     plugins.register(self.dispatcher, self)
     
     self.disco = Disco(self.dispatcher)
     self.disco.init()
     
     p = Presence(status="Use me plz!")
     self.roster = Roster(self.dispatcher, p)
     self.roster.init()
     
     self.version = ClientVersion(self.dispatcher, 
                                  "XmppBot", 
                                  'v%s' % version, 'Linux')
     self.version.init(self.disco)
     
     #set handlers for roster's signals
     dispatcher.connect(self.onSubscribe, 
                        self.roster.subscribe)
     dispatcher.connect(self.onRosterGot, 
                        self.roster.roster_got)
     dispatcher.connect(self.onAvailable, 
                        self.roster.resource_available)
     dispatcher.connect(self.onUnvailable, 
                        self.roster.resource_unavailable)
Exemplo n.º 2
0
    def componentConnected(self, xs):
        self.xmlstream = xs
        self.dispatcher = Dispatcher(xs, self.myjid)
        self.init()

        self.xmlstream.rawDataInFn = self.rawIn
        self.xmlstream.rawDataOutFn = self.rawOut
Exemplo n.º 3
0
    def componentConnected(self, xs):
        self.startTime = time.time()
        self.xmlstream = xs
        self.dispatcher = Dispatcher(xs, self.cJid)
        self.dispatcher.registerHandler((MyPresence, self))
        self.dispatcher.registerHandler((Message, self))
        self.version = ClientVersion(self.dispatcher,
                                     'Google Weather transport',
                                     self.VERSION, 'Linux')
        self.version.init(handlers=((WeatherVersionQuery, self.version),))
        self.myvcard = VCardQuery(nickname='gweather',
                                  jid=self.cJid,
                                  description='\
Google Weather XMPP translation service')
        self.vcard = VCard(self.dispatcher, myvcard=self.myvcard)
        self.vcard.init(handlers=((WeatherVCardQuery, self.vcard),))
        self.getOnline()
        self.lc = task.LoopingCall(self.updateStatus)
        self.lc.start(900)
        print 'Connected'
Exemplo n.º 4
0
class Client(object):
    """main class for client to server connection"""

    def __init__(self, reactor, client_jid, server, secret, port):
        """Setup handler and connect to server"""
        self.reactor = reactor
        self.client_jid = client_jid

        a = twisted_client.XMPPAuthenticator(client_jid, secret)
        self.f = XmlStreamFactory(a)
        
        #set handlers for xmlstream's events
        self.f.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, 
                            self.onConnected)
        self.f.addBootstrap(xmlstream.STREAM_END_EVENT, 
                            self.onDisconnected)
        self.f.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, 
                            self.onAuthenticated)
        self.f.addBootstrap(xmlstream.INIT_FAILED_EVENT, 
                            self.onInitFailed)

        self.connector = XMPPClientConnector(reactor, server, 
                                             self.f, port)
        self.connector.connect()

        self.xmlstream = None

    #handlers for xmlstream's events

    def onConnected(self, xs):
        """
        xmlstream.STREAM_CONNECTED_EVENT handler.
        Calls when client connected to server
        """
        
        self.xmlstream = xs
        self.xmlstream.rawDataInFn = self.rawIn
        self.xmlstream.rawDataOutFn = self.rawOut

    def rawIn(self,data):
        """data is the input stanza"""
        pass

    def rawOut(self,data):
        """data is the output stanza"""
        pass

    def onDisconnected(self, xs):
        """
        xmlstream.STREAM_END_EVENT handler.
        Calls when client disconnected from server
        """
        
        pass

    def onAuthenticated(self, xs):
        """
        xmlstream.STREAM_AUTHD_EVENT handler.
        Calls when client authenticated on server.
        Setup dispatcher and any features for client
        """
        
        self.dispatcher = Dispatcher(xs, self.client_jid)
        plugins.register(self.dispatcher, self)
        
        self.disco = Disco(self.dispatcher)
        self.disco.init()
        
        p = Presence(status="Use me plz!")
        self.roster = Roster(self.dispatcher, p)
        self.roster.init()
        
        self.version = ClientVersion(self.dispatcher, 
                                     "XmppBot", 
                                     'v%s' % version, 'Linux')
        self.version.init(self.disco)
        
        #set handlers for roster's signals
        dispatcher.connect(self.onSubscribe, 
                           self.roster.subscribe)
        dispatcher.connect(self.onRosterGot, 
                           self.roster.roster_got)
        dispatcher.connect(self.onAvailable, 
                           self.roster.resource_available)
        dispatcher.connect(self.onUnvailable, 
                           self.roster.resource_unavailable)
        
    def onInitFailed(self, xs):
        """
        xmlstream.STREAM_INIT_FAILED_EVENT handler.
        Calls when client authenticated on server was failed.
        """
        
        pass

    #handlers for roster's signals
    
    def onAvailable(self, sender, item, presence):
        """roster.resourse_available handler."""
        pass
        
    def onUnvailable(self, sender, item, presence):
        """roster.resourse_unavailable handler."""
        pass

    def onRosterGot(self, sender):
        """roster.roster_got handler."""
        pass

    def onSubscribe(self, sender, presence):
        """roster.subscribe handler."""
        
        presence.type_ = 'subscribed'
        presence.to = presence.from_
        presence.from_ = None
        self.dispatcher.send(presence)
        presence.type_ = 'subscribe'
        self.dispatcher.send(presence)
Exemplo n.º 5
0
 def onAuthenticated(self, xs):
     self.myjid = internJID(unicode(xs.authenticator.jid))
     self.dispatcher = Dispatcher(xs, self.myjid)
     self.init()
     self.deferred.callback(self)
     self.deferred = None
Exemplo n.º 6
0
class WeatherComponent(component.Service):
    def __init__(self, reactor, version, config, cJid):
        self.config = config
        self.reactor = reactor
        self.VERSION = version
        self.cJid = internJID(cJid)
        self.startTime = None
        self.xmlstream = None
        self.subscribed = SubscribedList(config)
        self.wbase = WeatherBase()
        self.online = []           
        
    def componentConnected(self, xs):
        self.startTime = time.time()
        self.xmlstream = xs
        self.dispatcher = Dispatcher(xs, self.cJid)
        self.dispatcher.registerHandler((MyPresence, self))
        self.dispatcher.registerHandler((Message, self))
        self.version = ClientVersion(self.dispatcher,
                                     'Google Weather transport',
                                     self.VERSION, 'Linux')
        self.version.init(handlers=((WeatherVersionQuery, self.version),))
        self.myvcard = VCardQuery(nickname='gweather',
                                  jid=self.cJid,
                                  description='\
Google Weather XMPP translation service')
        self.vcard = VCard(self.dispatcher, myvcard=self.myvcard)
        self.vcard.init(handlers=((WeatherVCardQuery, self.vcard),))
        self.getOnline()
        self.lc = task.LoopingCall(self.updateStatus)
        self.lc.start(900)
        print 'Connected'

    def addSubscr(self, from_, to):
        self.subscribed.add_subscr(from_, to)
    
    def rmSubscr(self, from_, to):
        self.subscribed.rm_subscr(from_, to)
    
    def addOnlinesubscr(self, from_, to):
        if (from_, to) not in self.online:
            self.online.append((from_, to))
            
    def rmOnlinesubscr(self, from_, to):
        if (from_, to) in self.online:
            self.online.remove((from_, to))
    
    def getOnline(self):
        for from_, to in self.subscribed.subscr_list:
            reply = Presence(
                          to=from_,
                          from_=to,
                          type_='probe',                          
                        )
            self.dispatcher.send(reply)
    
    def updateStatus(self):
        for from_, to in self.online:
            deff = self.wbase.get_condition(to.user)
            deff.addCallback(self._result, from_, to)
    
    def _result(self, respond, from_, to):
        reply = Presence(
                          to=from_,
                          from_=to,
                          status=respond,                          
                        )
        self.dispatcher.send(reply)
Exemplo n.º 7
0
 def setUp(self):
     self.dispatcher = Dispatcher(xmlEmul(), myjid='*****@*****.**')
Exemplo n.º 8
0
class TestDispatcher(unittest.TestCase):
    def setUp(self):
        self.dispatcher = Dispatcher(xmlEmul(), myjid='*****@*****.**')

    def test_registerHandler(self):
        res = self.dispatcher.registerHandler('handler')
        self.assertTrue(res)
        res = self.dispatcher.registerHandler('handler')
        self.assertEqual(res, None)

    def test_unregisterHandler(self):
        res = self.dispatcher.registerHandler('handler')
        res = self.dispatcher.unregisterHandler('handler')
        self.assertTrue(res)
        res = self.dispatcher.unregisterHandler('handler')
        self.assertEqual(res, None)

    def test_registerHook(self):
        res = self.dispatcher.registerHook('hook_name', 'hook')
        self.assertEqual(res, True)
        res = self.dispatcher.registerHook('hook_name', 'hook')
        self.assertEqual(res, None)

    def test_unregisterHook(self):
        res = self.dispatcher.registerHook('hook_name', 'hook')
        res = self.dispatcher.unregisterHook('hook_name', 'hook')
        self.assertEqual(res, True)
        res = self.dispatcher.unregisterHook('hook_name', 'hook')
        self.assertEqual(res, None)

    def test_getHooks(self):
        self.dispatcher.registerHook('hook_name', 'hook1')
        self.dispatcher.registerHook('hook_name', 'hook2')
        self.dispatcher.registerHook('hook_name', 'hook3')
        l = ['hook1', 'hook2', 'hook3']
        self.assertEqual(self.dispatcher.getHooks('hook_name'), l)

    def test_send(self):
        # XXX: implement
        pass

    def test_dispatch(self):
        # XXX: implement
        pass
Exemplo n.º 9
0
 def setUp(self):
     self.dispatcher = Dispatcher(xmlEmul(), myjid='*****@*****.**')
Exemplo n.º 10
0
class TestDispatcher(unittest.TestCase):
    
    def setUp(self):
        self.dispatcher = Dispatcher(xmlEmul(), myjid='*****@*****.**')
    
    def test_registerHandler(self):
        res = self.dispatcher.registerHandler('handler')
        self.assertTrue(res)
        res = self.dispatcher.registerHandler('handler')
        self.assertEqual(res, None)
        
    def test_unregisterHandler(self):
        res = self.dispatcher.registerHandler('handler')
        res = self.dispatcher.unregisterHandler('handler')
        self.assertTrue(res)
        res = self.dispatcher.unregisterHandler('handler')
        self.assertEqual(res, None)
    
    def test_registerHook(self):
        res = self.dispatcher.registerHook('hook_name', 'hook')
        self.assertEqual(res, True)
        res = self.dispatcher.registerHook('hook_name', 'hook')
        self.assertEqual(res, None)
    
    def test_unregisterHook(self):
        res = self.dispatcher.registerHook('hook_name', 'hook')
        res = self.dispatcher.unregisterHook('hook_name', 'hook')
        self.assertEqual(res, True)
        res = self.dispatcher.unregisterHook('hook_name', 'hook')
        self.assertEqual(res, None)
    
    def test_getHooks(self):
        self.dispatcher.registerHook('hook_name', 'hook1')
        self.dispatcher.registerHook('hook_name', 'hook2')
        self.dispatcher.registerHook('hook_name', 'hook3')
        l = ['hook1', 'hook2', 'hook3']
        self.assertEqual(self.dispatcher.getHooks('hook_name'), l)

    def test_send(self):
        # XXX: implement
        pass
    
    def test_dispatch(self):
        # XXX: implement
        pass