summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0047/stanza.py
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2012-02-02 18:27:23 +0100
committerLance Stout <lancestout@gmail.com>2012-02-02 18:27:23 +0100
commitbd52a5e6c12237e1ab4ceaf0d5933f3e5109800e (patch)
tree7097a2019b8535db8fc29b2a8a9f566b1ab3bd1e /sleekxmpp/plugins/xep_0047/stanza.py
parentf98e5a03de19d6e4af6478b902789a50fc1a3b1c (diff)
downloadslixmpp-bd52a5e6c12237e1ab4ceaf0d5933f3e5109800e.tar.gz
slixmpp-bd52a5e6c12237e1ab4ceaf0d5933f3e5109800e.tar.bz2
slixmpp-bd52a5e6c12237e1ab4ceaf0d5933f3e5109800e.tar.xz
slixmpp-bd52a5e6c12237e1ab4ceaf0d5933f3e5109800e.zip
Initial, mostly working XEP-0047 plugin.
This is inspired by the version from macdiesel and tomstrummer, but their version was heavily linked with XEP-0096 and focused solely on file transfer. This version is a more generic implementation.
Diffstat (limited to 'sleekxmpp/plugins/xep_0047/stanza.py')
-rw-r--r--sleekxmpp/plugins/xep_0047/stanza.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/sleekxmpp/plugins/xep_0047/stanza.py b/sleekxmpp/plugins/xep_0047/stanza.py
new file mode 100644
index 00000000..e3b55511
--- /dev/null
+++ b/sleekxmpp/plugins/xep_0047/stanza.py
@@ -0,0 +1,63 @@
+import re
+import base64
+
+from sleekxmpp.exceptions import XMPPError
+from sleekxmpp.xmlstream import register_stanza_plugin, ET, ElementBase
+from sleekxmpp.thirdparty.suelta.util import bytes
+
+
+VALID_B64 = re.compile(r'[A-Za-z0-9\+\/]*=*')
+
+
+def to_b64(data):
+ return bytes(base64.b64encode(bytes(data))).decode('utf-8')
+
+def from_b64(data):
+ return bytes(base64.b64decode(bytes(data))).decode('utf-8')
+
+
+class Open(ElementBase):
+ name = 'open'
+ namespace = 'http://jabber.org/protocol/ibb'
+ plugin_attrib = 'ibb_open'
+ interfaces = set(('block_size', 'sid', 'stanza'))
+
+ def get_block_size(self):
+ return int(self._get_attr('block-size'))
+
+ def set_block_size(self, value):
+ self._set_attr('block-size', str(value))
+
+ def del_block_size(self):
+ self._del_attr('block-size')
+
+
+class Data(ElementBase):
+ name = 'data'
+ namespace = 'http://jabber.org/protocol/ibb'
+ plugin_attrib = 'ibb_data'
+ interfaces = set(('seq', 'sid', 'data'))
+ sub_interfaces = set(['data'])
+
+ def get_seq(self):
+ return int(self._get_attr('seq', '0'))
+
+ def set_seq(self, value):
+ self._set_attr('seq', str(value))
+
+ def get_data(self):
+ b64_data = self._get_sub_text('data', '')
+ if VALID_B64.match(b64_data).group() == b64_data:
+ return from_b64(b64_data)
+ else:
+ raise XMPPError('not-acceptable')
+
+ def set_data(self, value):
+ self._set_sub_text('data', to_b64(value))
+
+
+class Close(ElementBase):
+ name = 'close'
+ namespace = 'http://jabber.org/protocol/ibb'
+ plugin_attrib = 'ibb_close'
+ interfaces = set(['sid'])