diff sat.tac @ 64:d46f849664aa

SàT: multi-profile, plugins updated - core: 2 new convenient methods: getJidNStream and getClient - new param in plugin info: "handler" to know if there is a handler to plug on profiles clients - plugins with handler now use an other class which is returned to profile client with the new method "getHandler" and pluged when connecting
author Goffi <goffi@goffi.org>
date Sat, 30 Jan 2010 16:17:33 +1100
parents 0db25931b60d
children d35c5edab53f
line wrap: on
line diff
--- a/sat.tac	Fri Jan 29 14:17:15 2010 +1100
+++ b/sat.tac	Sat Jan 30 16:17:33 2010 +1100
@@ -70,12 +70,12 @@
 
 class SatXMPPClient(client.XMPPClient):
     
-    def __init__(self, bridge, profile, user_jid, password, host=None, port=5222):
+    def __init__(self, host_app, profile, user_jid, password, host=None, port=5222):
         client.XMPPClient.__init__(self, user_jid, password, host, port)
         self.factory.clientConnectionLost = self.connectionLost
         self.__connected=False
-        self.bridge = bridge
         self.profile = profile
+        self.host_app = host_app
 
     def _authd(self, xmlstream):
         print "SatXMPPClient"
@@ -83,7 +83,7 @@
         self.__connected=True
         print "********** CONNECTED **********"
         self.streamInitialized()
-        self.bridge.connected() #we send the signal to the clients
+        self.host_app.bridge.connected() #we send the signal to the clients
 
     def streamInitialized(self):
         """Called after _authd"""
@@ -99,9 +99,8 @@
         self.roster.requestRoster()
         
         self.presence.available()
-        
-        #self.disco.requestInfo(jid.JID(self.memory.getParamA("Server", "Connection"))).addCallback(self.host.serverDisco)  #gof: FIXME
- 
+       
+        self.disco.requestInfo(jid.JID(self.host_app.memory.getParamA("Server", "Connection", profile_key=self.profile))).addCallback(self.host_app.serverDisco)  #FIXME: use these informations
 
     def isConnected(self):
         return self.__connected
@@ -113,7 +112,7 @@
             self.keep_alife.stop()
         except AttributeError:
             debug("No keep_alife")
-        self.bridge.disconnected() #we send the signal to the clients
+        self.host_app.bridge.disconnected() #we send the signal to the clients
 
 
 class SatMessageProtocol(xmppim.MessageProtocol):
@@ -351,14 +350,14 @@
         self.bridge.register("confirmationAnswer", self.confirmationAnswer)
         self.bridge.register("getProgress", self.getProgress)
 
-        #self._import_plugins() #gof:
+        self._import_plugins()
 
 
     def _import_plugins(self):
         """Import all plugins found in plugins directory"""
         #TODO: manage dependencies
         plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename,glob ("plugins/plugin*.py"))]
-        
+
         for plug in plug_lst:
             plug_path = 'plugins.'+plug
             __import__(plug_path)
@@ -366,6 +365,10 @@
             plug_info = mod.PLUGIN_INFO
             info ("importing plugin: %s", plug_info['name'])
             self.plugins[plug_info['import_name']] = getattr(mod, plug_info['main'])(self)
+            if plug_info.has_key('handler') and plug_info['handler'] == 'yes':
+                self.plugins[plug_info['import_name']].is_handler = True
+            else:
+                self.plugins[plug_info['import_name']].is_handler = False
             #TODO: test xmppclient presence and register handler parent
 
     def connect(self, profile_key = '@DEFAULT@'):
@@ -381,12 +384,10 @@
             info("already connected !")
             return
         print "connecting..."
-        current = self.profiles[profile] = SatXMPPClient(self.bridge, profile,
-            jid.JID(self.memory.getParamA("JabberID", "Connection"), profile),
-            self.memory.getParamA("Password", "Connection"),
-            self.memory.getParamA("Server", "Connection"), 5222)
-
-        #current.client.streamInitialized = self.streamInitialized   #gof:
+        current = self.profiles[profile] = SatXMPPClient(self, profile,
+            jid.JID(self.memory.getParamA("JabberID", "Connection", profile_key = profile_key), profile),
+            self.memory.getParamA("Password", "Connection", profile_key = profile_key),
+            self.memory.getParamA("Server", "Connection", profile_key = profile_key), 5222)
 
         current.messageProt = SatMessageProtocol(self)
         current.messageProt.setHandlerParent(current)
@@ -408,8 +409,8 @@
         
         #FIXME: gof
         for plugin in self.plugins.iteritems():
-            if isinstance(plugin[1], XMPPHandler):
-                plugin[1].setHandlerParent(current)
+            if plugin[1].is_handler:
+                plugin[1].getHandler().setHandlerParent(current)
 
         current.startService()
     
@@ -440,6 +441,22 @@
         
     ## Misc methods ##
    
+    def getJidNStream(self, profile_key):
+        """Convenient method to get jid and stream from profile key
+        @return: tuple (jid, xmlstream) from profile, can be None"""
+        profile = self.memory.getProfileName(profile_key)
+        if not profile or not self.profiles[profile].isConnected():
+            return (None, None)
+        return (self.profiles[profile].jid, self.profiles[profile].xmlstream)
+
+    def getClient(self, profile_key):
+        """Convenient method to get client from profile key
+        @return: client or None if it doesn't exist"""
+        profile = self.memory.getProfileName(profile_key)
+        if not profile:
+            return None
+        return self.profiles[profile]
+
     def registerNewAccount(self, login, password, server, port = 5222, id = None):
         """Connect to a server and create a new account using in-band registration"""
 
@@ -451,6 +468,7 @@
         return next_id 
 
     def registerNewAccountCB(self, id, data):
+        #FIXME: gof: profile not managed here !
         user = jid.parse(self.memory.getParamA("JabberID", "Connection"))[0]
         password = self.memory.getParamA("Password", "Connection")
         server = self.memory.getParamA("Server", "Connection")
@@ -472,6 +490,7 @@
         print "data=",data
 
     def regisConfirmCB(self, id, accepted, data):
+        #FIXME: gof: profile not managed here !
         print "register Confirmation CB ! (%s)" % str(accepted)
         action_id = self.__private_data[id]
         del self.__private_data[id]