From 01189376e29663174e6c525f4ec35734b4e84031 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 9 Apr 2012 21:41:59 -0400 Subject: Add initial support for XEP-0153. --- sleekxmpp/plugins/xep_0153/__init__.py | 15 +++++ sleekxmpp/plugins/xep_0153/stanza.py | 29 ++++++++ sleekxmpp/plugins/xep_0153/vcard_avatar.py | 104 +++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 sleekxmpp/plugins/xep_0153/__init__.py create mode 100644 sleekxmpp/plugins/xep_0153/stanza.py create mode 100644 sleekxmpp/plugins/xep_0153/vcard_avatar.py (limited to 'sleekxmpp/plugins/xep_0153') diff --git a/sleekxmpp/plugins/xep_0153/__init__.py b/sleekxmpp/plugins/xep_0153/__init__.py new file mode 100644 index 00000000..f52b7712 --- /dev/null +++ b/sleekxmpp/plugins/xep_0153/__init__.py @@ -0,0 +1,15 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +from sleekxmpp.plugins.base import register_plugin + +from sleekxmpp.plugins.xep_0153.stanza import VCardTempUpdate +from sleekxmpp.plugins.xep_0153.vcard_avatar import XEP_0153 + + +register_plugin(XEP_0153) diff --git a/sleekxmpp/plugins/xep_0153/stanza.py b/sleekxmpp/plugins/xep_0153/stanza.py new file mode 100644 index 00000000..4e6a660f --- /dev/null +++ b/sleekxmpp/plugins/xep_0153/stanza.py @@ -0,0 +1,29 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +from sleekxmpp.xmlstream import ElementBase + + +class VCardTempUpdate(ElementBase): + name = 'x' + namespace = 'vcard-temp:x:update' + plugin_attrib = 'vcard_temp_update' + interfaces = set(['photo']) + sub_interfaces = interfaces + + def set_photo(self, value): + if value is not None: + self._set_sub_text('photo', value, keep=True) + else: + self._del_sub('photo') + + def get_photo(self): + photo = self.xml.find('{%s}photo' % self.namespace) + if photo is None: + return None + return photo.text diff --git a/sleekxmpp/plugins/xep_0153/vcard_avatar.py b/sleekxmpp/plugins/xep_0153/vcard_avatar.py new file mode 100644 index 00000000..e8d9ea6a --- /dev/null +++ b/sleekxmpp/plugins/xep_0153/vcard_avatar.py @@ -0,0 +1,104 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +import hashlib +import logging + +from sleekxmpp.stanza import Presence +from sleekxmpp.xmlstream import register_stanza_plugin +from sleekxmpp.xmlstream.matcher import StanzaPath +from sleekxmpp.xmlstream.handler import Callback +from sleekxmpp.plugins.base import BasePlugin +from sleekxmpp.plugins.xep_0153 import stanza, VCardTempUpdate + + +log = logging.getLogger(__name__) + + +class XEP_0153(BasePlugin): + + name = 'xep_0153' + description = 'XEP-0153: vCard-Based Avatars' + dependencies = set(['xep_0054']) + stanza = stanza + + def plugin_init(self): + self._hashes = {} + + register_stanza_plugin(Presence, VCardTempUpdate) + + self.xmpp.add_filter('out', self._update_presence) + + self.xmpp.add_event_handler('session_start', self._start) + + self.xmpp.add_event_handler('presence_available', self._recv_presence) + self.xmpp.add_event_handler('presence_dnd', self._recv_presence) + self.xmpp.add_event_handler('presence_xa', self._recv_presence) + self.xmpp.add_event_handler('presence_chat', self._recv_presence) + self.xmpp.add_event_handler('presence_away', self._recv_presence) + + self.api.register(self._set_hash, 'set_hash', default=True) + self.api.register(self._get_hash, 'get_hash', default=True) + + def set_avatar(self, jid=None, avatar=None, mtype=None, block=True, + timeout=None, callback=None): + vcard = self.xmpp['xep_0054'].get_vcard(jid, cached=True) + vcard = vcard['vcard_temp'] + vcard['PHOTO']['TYPE'] = mtype + vcard['PHOTO']['BINVAL'] = avatar + self.xmpp['xep_0054'].publish_vcard(jid=jid, vcard=vcard) + self._reset_hash(jid) + + def _start(self, event): + self.xmpp['xep_0054'].get_vcard() + + def _update_presence(self, stanza): + if not isinstance(stanza, Presence): + return stanza + + current_hash = self.api['get_hash'](stanza['from']) + stanza['vcard_temp_update']['photo'] = current_hash + return stanza + + def _reset_hash(self, jid=None): + own_jid = jid.bare == self.xmpp.boundjid.bare + if self.xmpp.is_component: + own_jid = jid.domain = self.xmpp.boundjid.domain + + if jid is not None: + jid = jid.bare + self.api['set_hash'](jid, args=None) + if own_jid: + self.xmpp.roster[jid].send_last_presence() + iq = self.xmpp['xep_0054'].get_vcard(jid=jid, ifrom=jid) + data = iq['vcard_temp']['PHOTO']['BINVAL'] + if not data: + new_hash = '' + else: + new_hash = hashlib.sha1(data).hexdigest() + self.api['set_hash'](jid, args=new_hash) + if own_jid: + self.xmpp.roster[jid].send_last_presence() + + def _recv_presence(self, pres): + if not pres.match('presence/vcard_temp_update'): + self.api['set_hash'](pres['from'], args=None) + return + data = pres['vcard_temp_update']['photo'] + if data is None: + return + elif data == '' or data != self.api['get_hash'](pres['to']): + self._reset_hash(pres['from']) + + # ================================================================= + + def _get_hash(self, jid, node, ifrom, args): + return self._hashes.get(jid.bare, None) + + def _set_hash(self, jid, node, ifrom, args): + self._hashes[jid.bare] = args -- cgit v1.2.3