diff src/tools/memory.py @ 399:3ed53803b3b3

core: added getLastResource method
author Goffi <goffi@goffi.org>
date Thu, 06 Oct 2011 21:16:03 +0200
parents cb0285372818
children 62b17854254e
line wrap: on
line diff
--- a/src/tools/memory.py	Wed Oct 05 16:49:57 2011 +0200
+++ b/src/tools/memory.py	Thu Oct 06 21:16:03 2011 +0200
@@ -419,6 +419,7 @@
         self.host = host
         self.contacts={}
         self.presenceStatus={}
+        self.lastResource={} #tmp, will be refactored with bdd integration
         self.subscriptions={}
         self.params=Param(host)
         self.history={}  #used to store chat history (key: short jid)
@@ -677,6 +678,19 @@
                 attr, groups = self.contacts[profile][contact]
                 ret.append([contact, attr, groups ])
         return ret
+
+    def getLastResource(self, contact, profile_key):
+        """Return the last resource used by a contact
+        @param contact: contact jid (unicode)
+        @param profile_key: %(doc_profile_key)s"""
+        profile = self.getProfileName(profile_key)
+        if not profile:
+            error(_('Asking contacts for a non-existant profile'))
+            return ""
+        try:
+            return self.lastResource[profile][jid.JID(contact).userhost()]
+        except:
+            return ""
     
     def addPresenceStatus(self, contact_jid, show, priority, statuses, profile_key):
         profile = self.getProfileName(profile_key)
@@ -685,9 +699,14 @@
             return
         if not self.presenceStatus.has_key(profile):
             self.presenceStatus[profile] = {}
+        if not self.lastResource.has_key(profile):
+            self.lastResource[profile] = {}
         if not self.presenceStatus[profile].has_key(contact_jid.userhost()):
             self.presenceStatus[profile][contact_jid.userhost()] = {}
         resource = jid.parse(contact_jid.full())[2] or ''
+        if resource:
+            self.lastResource[profile][contact_jid.userhost()] = resource
+
         self.presenceStatus[profile][contact_jid.userhost()][resource] = (show, priority, statuses)
 
     def addWaitingSub(self, type, contact_jid, profile_key):