comparison src/plugins/plugin_xep_0054.py @ 2072:11fb5f5e2f89

plugin XEP-0054(XEP-0153): added a getAvatar: getAvatar can be used to do the whole process to get avatar file (or empty string if not avatar is found) : it first look in cache and request vCard if nothing is found.
author Goffi <goffi@goffi.org>
date Tue, 13 Sep 2016 22:02:52 +0200
parents 528e5fafc11b
children c42aab22c2c0
comparison
equal deleted inserted replaced
2071:c2fdee1bd908 2072:11fb5f5e2f89
84 self.host = host 84 self.host = host
85 self.avatar_path = os.path.join(self.host.memory.getConfig('', 'local_dir'), AVATAR_PATH) 85 self.avatar_path = os.path.join(self.host.memory.getConfig('', 'local_dir'), AVATAR_PATH)
86 if not os.path.exists(self.avatar_path): 86 if not os.path.exists(self.avatar_path):
87 os.makedirs(self.avatar_path) 87 os.makedirs(self.avatar_path)
88 self.cache = {} 88 self.cache = {}
89 host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='s', method=self._getCard) 89 host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='', method=self._getCard, async=True)
90 host.bridge.addMethod("getAvatarFile", ".plugin", in_sign='s', out_sign='s', method=self.getAvatarFile) 90 host.bridge.addMethod("getAvatarFile", ".plugin", in_sign='s', out_sign='s', method=self.getAvatarFile)
91 host.bridge.addMethod("getAvatar", ".plugin", in_sign='ss', out_sign='s', method=self._getAvatar, async=True)
91 host.bridge.addMethod("setAvatar", ".plugin", in_sign='ss', out_sign='', method=self.setAvatar, async=True) 92 host.bridge.addMethod("setAvatar", ".plugin", in_sign='ss', out_sign='', method=self.setAvatar, async=True)
92 host.trigger.add("presence_available", self.presenceAvailableTrigger) 93 host.trigger.add("presence_available", self.presenceAvailableTrigger)
93 host.memory.setSignalOnUpdate("avatar") 94 host.memory.setSignalOnUpdate("avatar")
94 host.memory.setSignalOnUpdate("nick") 95 host.memory.setSignalOnUpdate("nick")
95 96
305 log.debug(_(u"Asking for %s's VCard") % to_jid.userhost()) 306 log.debug(_(u"Asking for %s's VCard") % to_jid.userhost())
306 reg_request = client.IQ('get') 307 reg_request = client.IQ('get')
307 reg_request["from"] = client.jid.full() 308 reg_request["from"] = client.jid.full()
308 reg_request["to"] = to_jid.full() 309 reg_request["to"] = to_jid.full()
309 reg_request.addElement('vCard', NS_VCARD) 310 reg_request.addElement('vCard', NS_VCARD)
310 reg_request.send(to_jid.full()).addCallbacks(self._getCardCb, self._getCardEb, callbackArgs=[to_jid, client], errbackArgs=[to_jid, client]) 311 d = reg_request.send(to_jid.full()).addCallbacks(self._getCardCb, self._getCardEb, callbackArgs=[to_jid, client], errbackArgs=[to_jid, client])
311 return reg_request["id"] 312 return d
312 313
313 def getAvatarFile(self, avatar_hash): 314 def getAvatarFile(self, avatar_hash):
314 """Give the full path of avatar from hash 315 """Give the full path of avatar from hash
315 @param hash: SHA1 hash 316 @param avatar_hash(unicode): SHA1 hash
316 @return full_path 317 @return(unicode): full_path or empty string if avatar_hash is empty
317 """ 318 or if avatar is not found in cache
319 """
320 if not avatar_hash:
321 return ""
318 filename = self.avatar_path + '/' + avatar_hash 322 filename = self.avatar_path + '/' + avatar_hash
319 if not os.path.exists(filename): 323 if not os.path.exists(filename):
320 log.error(_(u"Asking for an uncached avatar [%s]") % avatar_hash) 324 log.error(_(u"Asking for an uncached avatar [%s]") % avatar_hash)
321 return "" 325 return ""
322 return filename 326 return filename
327
328 def _getAvatar(self, entity, profile):
329 client = self.host.getClient(profile)
330 return self.getAvatar(client, jid.JID(entity))
331
332 def _getAvatarGotCard(self, dummy, client, entity):
333 try:
334 self.cache[client.profile][entity.full()]['avatar']
335 except KeyError:
336 return ""
337
338 def getAvatar(self, client, entity):
339 try:
340 avatar_hash = self.cache[client.profile][entity.full()]['avatar']
341 except KeyError:
342 d = self.getCard(client, entity)
343 d.addCallback(self._getAvatarGotCard, client, entity)
344 else:
345 d = defer.succeed(avatar_hash)
346 d.addCallback(self.getAvatarFile)
347 return d
323 348
324 def _buildSetAvatar(self, vcard_set, filepath): 349 def _buildSetAvatar(self, vcard_set, filepath):
325 try: 350 try:
326 img = Image.open(filepath) 351 img = Image.open(filepath)
327 except IOError: 352 except IOError: