summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2012-04-08 23:27:19 -0400
committerLance Stout <lancestout@gmail.com>2012-04-08 23:27:19 -0400
commit60195cf2dcd7ef8261c4d74c332cc3a531337339 (patch)
treeb2a0bdd0f3ed2e46329e2348580ed469e0c221bb
parent15ef27314189baeee64fbe41ab87f1eebef6e85c (diff)
downloadslixmpp-60195cf2dcd7ef8261c4d74c332cc3a531337339.tar.gz
slixmpp-60195cf2dcd7ef8261c4d74c332cc3a531337339.tar.bz2
slixmpp-60195cf2dcd7ef8261c4d74c332cc3a531337339.tar.xz
slixmpp-60195cf2dcd7ef8261c4d74c332cc3a531337339.zip
Initial support for XEP-0231.
-rw-r--r--sleekxmpp/plugins/__init__.py1
-rw-r--r--sleekxmpp/plugins/xep_0231/__init__.py16
-rw-r--r--sleekxmpp/plugins/xep_0231/bob.py132
-rw-r--r--sleekxmpp/plugins/xep_0231/stanza.py35
4 files changed, 184 insertions, 0 deletions
diff --git a/sleekxmpp/plugins/__init__.py b/sleekxmpp/plugins/__init__.py
index 1c6c89a2..b0aa5409 100644
--- a/sleekxmpp/plugins/__init__.py
+++ b/sleekxmpp/plugins/__init__.py
@@ -48,5 +48,6 @@ __all__ = [
'xep_0202', # Entity Time
'xep_0203', # Delayed Delivery
'xep_0224', # Attention
+ 'xep_0231', # Bits of Binary
'xep_0249', # Direct MUC Invitations
]
diff --git a/sleekxmpp/plugins/xep_0231/__init__.py b/sleekxmpp/plugins/xep_0231/__init__.py
new file mode 100644
index 00000000..6a70cc07
--- /dev/null
+++ b/sleekxmpp/plugins/xep_0231/__init__.py
@@ -0,0 +1,16 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2012 Nathanael C. Fritz,
+ Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+from sleekxmpp.plugins.base import register_plugin
+
+from sleekxmpp.plugins.xep_0231.stanza import BitsOfBinary
+from sleekxmpp.plugins.xep_0231.bob import XEP_0231
+
+
+register_plugin(XEP_0231)
diff --git a/sleekxmpp/plugins/xep_0231/bob.py b/sleekxmpp/plugins/xep_0231/bob.py
new file mode 100644
index 00000000..011a1952
--- /dev/null
+++ b/sleekxmpp/plugins/xep_0231/bob.py
@@ -0,0 +1,132 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2012 Nathanael C. Fritz,
+ Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+import logging
+import hashlib
+
+from sleekxmpp.stanza import Iq
+from sleekxmpp.exceptions import XMPPError
+from sleekxmpp.xmlstream.handler import Callback
+from sleekxmpp.xmlstream.matcher import StanzaPath
+from sleekxmpp.xmlstream import register_stanza_plugin
+from sleekxmpp.plugins.base import BasePlugin
+from sleekxmpp.plugins.xep_0231 import stanza, BitsOfBinary
+
+
+log = logging.getLogger(__name__)
+
+
+class XEP_0231(BasePlugin):
+
+ """
+ XEP-0231 Bits of Binary
+ """
+
+ name = 'xep_0231'
+ description = 'XEP-0231: Bits of Binary'
+ dependencies = set(['xep_0030'])
+
+ def plugin_init(self):
+ self._cids = {}
+
+ self.xmpp['xep_0030'].add_feature('urn:xmpp:bob')
+
+ register_stanza_plugin(Iq, BitsOfBinary)
+
+ self.xmpp.register_handler(
+ Callback('Bits of Binary - Iq',
+ StanzaPath('iq/bob'),
+ self._handle_bob_iq))
+
+ self.xmpp.register_handler(
+ Callback('Bits of Binary - Message',
+ StanzaPath('message/bob'),
+ self._handle_bob))
+
+ self.xmpp.register_handler(
+ Callback('Bits of Binary - Presence',
+ StanzaPath('presence/bob'),
+ self._handle_bob))
+
+ self.api.register(self._get_bob, 'get_bob', default=True)
+ self.api.register(self._set_bob, 'set_bob', default=True)
+ self.api.register(self._del_bob, 'del_bob', default=True)
+
+
+ def set_bob(self, data, mtype, cid=None, max_age=None):
+ if cid is None:
+ cid = 'sha1+%s@bob.xmpp.org' % hashlib.sha1(data).hexdigest()
+
+ bob = BitsOfBinary()
+ bob['data'] = data
+ bob['type'] = mtype
+ bob['cid'] = cid
+ bob['max_age'] = max_age
+
+ self.api['set_bob'](args=bob)
+
+ return cid
+
+ def get_bob(self, jid=None, cid=None, cached=True, ifrom=None,
+ block=True, timeout=None, callback=None):
+ if cached:
+ data = self.api['get_bob'](None, None, ifrom, args=cid)
+ if data is not None:
+ if not isinstance(data, Iq):
+ iq = self.xmpp.Iq()
+ iq.append(data)
+ return iq
+ return data
+
+ iq = self.xmpp.Iq()
+ iq['to'] = jid
+ iq['from'] = ifrom
+ iq['type'] = 'get'
+ iq['bob']['cid'] = cid
+ return iq.send(block=block, timeout=timeout, callback=callback)
+
+ def del_bob(self, cid):
+ self.api['del_bob'](args=cid)
+
+ def _handle_bob_iq(self, iq):
+ cid = iq['bob']['cid']
+
+ if iq['type'] == 'result':
+ self.api['set_bob'](iq['from'], None, iq['to'], args=iq['bob'])
+ self.xmpp.event('bob', iq)
+ elif iq['type'] == 'get':
+ data = self.api['get_bob'](iq['to'], None, iq['from'], args=cid)
+ if isinstance(data, Iq):
+ data['id'] = iq['id']
+ data.send()
+ return
+
+ iq.reply()
+ iq.append(data)
+ iq.send()
+
+ def _handle_bob(self, stanza):
+ self.api['set_bob'](stanza['from'], None,
+ stanza['to'], args=stanza['bob'])
+ self.xmpp.event('bob', stanza)
+
+ # =================================================================
+
+ def _set_bob(self, jid, node, ifrom, bob):
+ self._cids[bob['cid']] = bob
+
+ def _get_bob(self, jid, node, ifrom, cid):
+ if cid in self._cids:
+ return self._cids[cid]
+ else:
+ raise XMPPError('item-not-found')
+
+ def _del_bob(self, jid, node, ifrom, cid):
+ if cid in self._cids:
+ del self._cids[cid]
diff --git a/sleekxmpp/plugins/xep_0231/stanza.py b/sleekxmpp/plugins/xep_0231/stanza.py
new file mode 100644
index 00000000..13d7a5db
--- /dev/null
+++ b/sleekxmpp/plugins/xep_0231/stanza.py
@@ -0,0 +1,35 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2012 Nathanael C. Fritz,
+ Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+
+from base64 import b64encode, b64decode
+
+from sleekxmpp.xmlstream import ElementBase
+
+
+class BitsOfBinary(ElementBase):
+ name = 'data'
+ namespace = 'urn:xmpp:bob'
+ plugin_attrib = 'bob'
+ interfaces = set(('cid', 'max_age', 'type', 'data'))
+
+ def get_max_age(self):
+ return self._get_attr('max-age')
+
+ def set_max_age(self, value):
+ self._set_attr('max-age', value)
+
+ def get_data(self):
+ return b64decode(self.xml.text)
+
+ def set_data(self, value):
+ self.xml.text = b64encode(value)
+
+ def del_data(self):
+ self.xml.text = ''