diff plugins/plugin_xep_0054.py @ 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 a5b5fb5fc9fd
children 8147b4f40809
line wrap: on
line diff
--- a/plugins/plugin_xep_0054.py	Fri Jan 29 14:17:15 2010 +1100
+++ b/plugins/plugin_xep_0054.py	Sat Jan 30 16:17:33 2010 +1100
@@ -58,11 +58,11 @@
 "protocols": ["XEP-0054", "XEP-0153"],
 "dependencies": [],
 "main": "XEP_0054",
+"handler": "yes",
 "description": """Implementation of vcard-temp"""
 }
 
-class XEP_0054(XMPPHandler):
-    implements(iwokkel.IDisco)
+class XEP_0054():
 
     def __init__(self, host):
         info("Plugin XEP_0054 initialization")
@@ -71,9 +71,12 @@
         self.vcard_cache = host.memory.getPrivate("vcard_cache") or {}  #used to store nicknames and avatar, key = jid
         if not os.path.exists(self.avatar_path):
             os.makedirs(self.avatar_path)
-        host.bridge.addMethod("getProfile", ".communication", in_sign='s', out_sign='s', method=self.getProfile)
+        host.bridge.addMethod("getCard", ".communication", in_sign='ss', out_sign='s', method=self.getCard)
         host.bridge.addMethod("getAvatarFile", ".communication", in_sign='s', out_sign='s', method=self.getAvatarFile)
-        host.bridge.addMethod("getProfileCache", ".communication", in_sign='s', out_sign='a{ss}', method=self.getProfileCache)
+        host.bridge.addMethod("getCardCache", ".communication", in_sign='s', out_sign='a{ss}', method=self.getCardCache)
+
+    def getHandler(self):
+        return XEP_0054_handler(self)  
    
     def update_cache(self, jid, name, value):
         """update cache value
@@ -101,15 +104,6 @@
             return None
 
 
-    def connectionInitialized(self):
-        self.xmlstream.addObserver(VCARD_UPDATE, self.update)
-    
-    def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
-        return [disco.DiscoFeature(NS_VCARD)]
-
-    def getDiscoItems(self, requestor, target, nodeIdentifier=''):
-        return []
-
     def save_photo(self, photo_xml):
         """Parse a <PHOTO> elem and save the picture"""
         for elem in photo_xml.elements():
@@ -177,14 +171,18 @@
         error ("Can't find VCard of %s" % failure.value.stanza['from'])
         self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}) #FIXME: maybe an error message would be best
   
-    def getProfile(self, target):
+    def getCard(self, target, profile_key='@DEFAULT@'):
         """Ask server for VCard
         @param target: jid from which we want the VCard
         @result: id to retrieve the profile"""
+        current_jid, xmlstream = self.host.getJidNStream(profile_key)
+        if not xmlstream:
+            error ('Asking profile for an non-existant or not connected profile')
+            return ""
         to_jid = jid.JID(target)
         debug("Asking for %s's VCard" % to_jid.userhost())
-        reg_request=IQ(self.host.xmlstream,'get')
-        reg_request["from"]=self.host.me.full()
+        reg_request=IQ(xmlstream,'get')
+        reg_request["from"]=current_jid.full()
         reg_request["to"] = to_jid.userhost()
         query=reg_request.addElement('vCard', NS_VCARD)
         reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err)
@@ -201,7 +199,7 @@
             return ""
         return filename
 
-    def getProfileCache(self, target):
+    def getCardCache(self, target):
         """Request for cached values of profile 
         return the cached nickname and avatar if exists, else get VCard
         """
@@ -215,6 +213,24 @@
             result['avatar'] = avatar
         return result
 
+
+
+class XEP_0054_handler(XMPPHandler):
+    implements(iwokkel.IDisco)
+   
+    def __init__(self, plugin_parent):
+        self.plugin_parent = plugin_parent
+        self.host = plugin_parent.host
+
+    def connectionInitialized(self):
+        self.xmlstream.addObserver(VCARD_UPDATE, self.update)
+    
+    def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
+        return [disco.DiscoFeature(NS_VCARD)]
+
+    def getDiscoItems(self, requestor, target, nodeIdentifier=''):
+        return []
+    
     def update(self, presence):
         """Request for VCard's nickname
         return the cached nickname if exists, else get VCard
@@ -224,8 +240,7 @@
         for elem in x_elem.elements():
             if elem.name == 'photo':
                 hash = str(elem)
-                old_avatar = self.get_cache(to_jid, 'avatar')
+                old_avatar = self.plugin_parent.get_cache(to_jid, 'avatar')
                 if not old_avatar or old_avatar != hash:
                     debug('New avatar found, requesting vcard')
-                    self.getProfile(to_jid.userhost())
-
+                    self.plugin_parent.getCard(to_jid.userhost(), self.parent.profile)