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
This commit is contained in:
parent
7b520ddc93
commit
656bea30dd
1 changed files with 175 additions and 109 deletions
|
|
@ -58,11 +58,11 @@ PLUGIN_INFO = {
|
|||
"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 @@ class XEP_0054(XMPPHandler):
|
|||
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 @@ class XEP_0054(XMPPHandler):
|
|||
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 @@ class XEP_0054(XMPPHandler):
|
|||
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 @@ class XEP_0054(XMPPHandler):
|
|||
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 @@ class XEP_0054(XMPPHandler):
|
|||
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 @@ class XEP_0054(XMPPHandler):
|
|||
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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue