changeset 305:15a12bf2bb62

core: server identities are now save in memory
author Goffi <goffi@goffi.org>
date Thu, 07 Apr 2011 22:21:16 +0200
parents e04ccf122bb6
children 169e7386650a
files src/sat.tac src/tools/memory.py
diffstat 2 files changed, 40 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/sat.tac	Sat Mar 26 17:51:19 2011 +0100
+++ b/src/sat.tac	Thu Apr 07 22:21:16 2011 +0200
@@ -29,7 +29,7 @@
 gettext.install('sat', "i18n", unicode=True)
 
 from twisted.application import internet, service
-from twisted.internet import glib2reactor, protocol, task
+from twisted.internet import glib2reactor, protocol, task, defer
 glib2reactor.install()
 
 from twisted.words.protocols.jabber import jid, xmlstream
@@ -80,6 +80,7 @@
         self.__connected=False
         self.profile = profile
         self.host_app = host_app
+        self.client_initialized = defer.Deferred()
 
     def _authd(self, xmlstream):
         if not self.host_app.trigger.point("XML Initialized", xmlstream, self.profile):
@@ -110,6 +111,7 @@
         self.presence.available()
        
         self.disco.requestInfo(jid.JID(self.host_app.memory.getParamA("Server", "Connection", profile_key=self.profile))).addCallback(self.host_app.serverDisco, self.profile)  #FIXME: use these informations
+        self.disco.requestItems(jid.JID(self.host_app.memory.getParamA("Server", "Connection", profile_key=self.profile))).addCallback(self.host_app.serverDiscoItems, self.disco, self.profile, self.client_initialized)
 
     def initializationFailed(self, reason):
         print ("initializationFailed: %s" % reason)
@@ -155,6 +157,7 @@
     def __init__(self, host):
         xmppim.RosterClientProtocol.__init__(self)
         self.host = host
+        self._groups=set()
     
     def rosterCb(self, roster):
         for raw_jid, item in roster.iteritems():
@@ -188,6 +191,7 @@
         info (_("new contact in roster list: %s"), item.jid.full())
         self.host.memory.addContact(item.jid, item_attr, item.groups, self.parent.profile)
         self.host.bridge.newContact(item.jid.full(), item_attr, item.groups, self.parent.profile)
+        self._groups.update(item.groups) 
     
     def onRosterRemove(self, entity):
         """Called when a roster removal event is received"""
@@ -195,6 +199,10 @@
         print _("removing %s from roster list") % entity.full()
         self.host.memory.delContact(entity, self.parent.profile)
 
+    def getGroups(self):
+        """Return a set of groups"""
+        return self._groups
+
 class SatPresenceProtocol(xmppim.PresenceClientProtocol):
 
     def __init__(self, host):
@@ -714,6 +722,23 @@
         for cat, type in disco.identities:
             debug (_("Identity found: [%(category)s/%(type)s] %(identity)s") % {'category':cat, 'type':type, 'identity':disco.identities[(cat,type)]})
 
+    def serverDiscoItems(self, disco_result, disco_client, profile, initialized):
+        """xep-0030 Discovery Protocol.
+        @param disco_result: result of the disco item querry
+        @param disco_client: SatDiscoProtocol instance 
+        @param profile: profile of the user 
+        @param initialized: deferred which must be chained when everything is done"""
+        def _check_entity_cb(result, entity, profile):
+            for category, type in result.identities:
+                debug (_('Identity added: (%(category)s,%(type)s) ==> %(entity)s [%(profile)s]') % {
+                         'category':category, 'type':type, 'entity':entity, 'profile':profile})
+                self.memory.addServerIdentity(category, type, entity, profile) 
+       
+        defer_list = []
+        for item in disco_result._items:
+            defer_list.append(disco_client.requestInfo(item.entity).addCallback(_check_entity_cb, item.entity, profile))
+        defer.DeferredList(defer_list).chainDeferred(initialized)
+
     
     ## Generic HMI ## 
     
--- a/src/tools/memory.py	Sat Mar 26 17:51:19 2011 +0100
+++ b/src/tools/memory.py	Thu Apr 07 22:21:16 2011 +0200
@@ -398,6 +398,7 @@
         self.history={}  #used to store chat history (key: short jid)
         self.private={}  #used to store private value
         self.server_features={} #used to store discovery's informations
+        self.server_identities={}
         host.set_const('savefile_history', SAVEFILE_HISTORY)
         host.set_const('savefile_private', SAVEFILE_PRIVATE)
         self.load()
@@ -545,6 +546,19 @@
         if not self.server_features.has_key(profile):
             self.server_features[profile] = []
         self.server_features[profile].append(feature)
+    
+    def addServerIdentity(self, category, type, entity, profile):
+        """Add an identity discovered from server
+        @param feature: string of the feature
+        @param profile: which profile is using this server ?"""
+        if not self.server_identities.has_key(profile):
+            self.server_identities[profile] = {}
+        self.server_identities[profile][(category, type)] = entity
+
+    def getServerServiceEntity(self, category, type, profile):
+        if self.server_identities.has_key(profile):
+            return self.server_identities[profile].get((category, type))
+        return None
 
     def hasServerFeature(self, feature, profile_key='@DEFAULT@'):
         """Tell if the server of the profile has the required feature"""