diff src/plugins/plugin_xep_0115.py @ 437:02c633a0ddcf

plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
author Goffi <goffi@goffi.org>
date Wed, 30 Nov 2011 00:24:41 +0100
parents 7c79d4a8c9e6
children cf005701624b
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0115.py	Wed Nov 30 00:23:50 2011 +0100
+++ b/src/plugins/plugin_xep_0115.py	Wed Nov 30 00:24:41 2011 +0100
@@ -24,6 +24,7 @@
 from twisted.words.protocols.jabber import client, jid, xmlstream
 from twisted.words.protocols.jabber import error as jab_error
 from twisted.words.protocols.jabber.xmlstream import IQ
+from sat.memory.persistent import PersistentBinaryDict
 import os.path
 import types
 
@@ -73,14 +74,16 @@
            
 
 class XEP_0115():
-    cap_hash = None
+    cap_hash = None #capabilities hash is class variable as it is common to all profiles
+    #TODO: this code is really dirty, need to clean it and try to move it to Wokkel
 
     def __init__(self, host):
         info(_("Plugin XEP_0115 initialization"))
         self.host = host
         host.trigger.add("Disco Handled", self.checkHash)
-        self.hash_cache = host.memory.getPrivate("entity_capabilities_cache") or {}  #key = hash or jid
-        self.jid_hash = {} #jid to hash mapping, map to a discoInfo if the hash method is unknown 
+        self.hash_cache = PersistentBinaryDict(NS_ENTITY_CAPABILITY) #key = hash or jid, value = features
+        self.hash_cache.load()
+        self.jid_hash = {} #jid to hash mapping, map to a discoInfo features if the hash method is unknown 
 
     def checkHash(self, profile):
         if not XEP_0115.cap_hash:
@@ -165,15 +168,13 @@
 
     def _updateCache(self, discoResult, from_jid, key):
         """Actually update the cache
-        @param discoResult: result of the requestInfo
-        @param known_hash: True if it's a hash method we know, we don't save the cache else"""
+        @param discoResult: result of the requestInfo"""
         if key:
             self.plugin_parent.jid_hash[from_jid] = key
-            self.plugin_parent.hash_cache[key] = discoResult
+            self.plugin_parent.hash_cache[key] = discoResult.features
         else:
-            #No key, that mean unknown hash method
-            self.plugin_parent.jid_hash[from_jid] = discoResult
-        self.host.memory.setPrivate("entity_capabilities_cache", self.plugin_parent.hash_cache)
+            #No key, that means unknown hash method
+            self.plugin_parent.jid_hash[from_jid] = discoResult.features
 
 
     def update(self, presence):
@@ -191,8 +192,8 @@
         except KeyError:
             warning('Received invalid capabilities tag')
             return
-        if not self.plugin_parent.jid_hash.has_key(from_jid):
-            if self.plugin_parent.hash_cache.has_key(ver):
+        if not from_jid in self.plugin_parent.jid_hash:
+            if ver in self.plugin_parent.hash_cache:
                 #we know that hash, we just link it with the jid
                 self.plugin_parent.jid_hash[from_jid] = ver
             else: