summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2012-04-09 21:41:59 -0400
committerLance Stout <lancestout@gmail.com>2012-04-09 21:41:59 -0400
commit01189376e29663174e6c525f4ec35734b4e84031 (patch)
tree12a53f42d26aac9df7b26dbc196641730ed8ad01
parent60195cf2dcd7ef8261c4d74c332cc3a531337339 (diff)
downloadslixmpp-01189376e29663174e6c525f4ec35734b4e84031.tar.gz
slixmpp-01189376e29663174e6c525f4ec35734b4e84031.tar.bz2
slixmpp-01189376e29663174e6c525f4ec35734b4e84031.tar.xz
slixmpp-01189376e29663174e6c525f4ec35734b4e84031.zip
Add initial support for XEP-0153.
-rw-r--r--sleekxmpp/plugins/__init__.py1
-rw-r--r--sleekxmpp/plugins/xep_0153/__init__.py15
-rw-r--r--sleekxmpp/plugins/xep_0153/stanza.py29
-rw-r--r--sleekxmpp/plugins/xep_0153/vcard_avatar.py104
4 files changed, 149 insertions, 0 deletions
diff --git a/sleekxmpp/plugins/__init__.py b/sleekxmpp/plugins/__init__.py
index b0aa5409..9ad3fbba 100644
--- a/sleekxmpp/plugins/__init__.py
+++ b/sleekxmpp/plugins/__init__.py
@@ -40,6 +40,7 @@ __all__ = [
'xep_0115', # Entity Capabilities
'xep_0118', # User Tune
'xep_0128', # Extended Service Discovery
+ 'xep_0153', # vCard-Based Avatars
'xep_0163', # Personal Eventing Protocol
'xep_0172', # User Nickname
'xep_0184', # Message Receipts
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