diff options
author | Lance Stout <lancestout@gmail.com> | 2012-09-26 01:47:05 -0700 |
---|---|---|
committer | Lance Stout <lancestout@gmail.com> | 2012-09-26 01:47:05 -0700 |
commit | b5b1c932c7efd2cdf5b4731bde6c8145873da25f (patch) | |
tree | 1d97e8bce8071a200e305d8e0c91e71e709fcd99 /sleekxmpp | |
parent | b8f04983e1fe3ca45471125f005f41cc290d1bbf (diff) | |
download | slixmpp-b5b1c932c7efd2cdf5b4731bde6c8145873da25f.tar.gz slixmpp-b5b1c932c7efd2cdf5b4731bde6c8145873da25f.tar.bz2 slixmpp-b5b1c932c7efd2cdf5b4731bde6c8145873da25f.tar.xz slixmpp-b5b1c932c7efd2cdf5b4731bde6c8145873da25f.zip |
Add support for XEP-0013: Flexible Offline Message Retrieval
Diffstat (limited to 'sleekxmpp')
-rw-r--r-- | sleekxmpp/plugins/__init__.py | 1 | ||||
-rw-r--r-- | sleekxmpp/plugins/xep_0013/__init__.py | 15 | ||||
-rw-r--r-- | sleekxmpp/plugins/xep_0013/offline.py | 134 | ||||
-rw-r--r-- | sleekxmpp/plugins/xep_0013/stanza.py | 53 | ||||
-rw-r--r-- | sleekxmpp/plugins/xep_0313/stanza.py | 2 |
5 files changed, 203 insertions, 2 deletions
diff --git a/sleekxmpp/plugins/__init__.py b/sleekxmpp/plugins/__init__.py index 6188a65e..ad8b1419 100644 --- a/sleekxmpp/plugins/__init__.py +++ b/sleekxmpp/plugins/__init__.py @@ -18,6 +18,7 @@ __all__ = [ 'xep_0004', # Data Forms 'xep_0009', # Jabber-RPC 'xep_0012', # Last Activity + 'xep_0013', # Flexible Offline Message Retrieval 'xep_0016', # Privacy Lists 'xep_0027', # Current Jabber OpenPGP Usage 'xep_0030', # Service Discovery diff --git a/sleekxmpp/plugins/xep_0013/__init__.py b/sleekxmpp/plugins/xep_0013/__init__.py new file mode 100644 index 00000000..ad400949 --- /dev/null +++ b/sleekxmpp/plugins/xep_0013/__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 permissio +""" + +from sleekxmpp.plugins.base import register_plugin + +from sleekxmpp.plugins.xep_0013.stanza import Offline +from sleekxmpp.plugins.xep_0013.offline import XEP_0013 + + +register_plugin(XEP_0013) diff --git a/sleekxmpp/plugins/xep_0013/offline.py b/sleekxmpp/plugins/xep_0013/offline.py new file mode 100644 index 00000000..a0d992a7 --- /dev/null +++ b/sleekxmpp/plugins/xep_0013/offline.py @@ -0,0 +1,134 @@ +""" + 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 permissio +""" + +import logging + +import sleekxmpp +from sleekxmpp.stanza import Message, Iq +from sleekxmpp.exceptions import XMPPError +from sleekxmpp.xmlstream.handler import Collector +from sleekxmpp.xmlstream.matcher import StanzaPath +from sleekxmpp.xmlstream import register_stanza_plugin +from sleekxmpp.plugins import BasePlugin +from sleekxmpp.plugins.xep_0013 import stanza + + +log = logging.getLogger(__name__) + + +class XEP_0013(BasePlugin): + + """ + XEP-0013 Flexible Offline Message Retrieval + """ + + name = 'xep_0013' + description = 'XEP-0013: Flexible Offline Message Retrieval' + dependencies = set(['xep_0030']) + stanza = stanza + + def plugin_init(self): + register_stanza_plugin(Iq, stanza.Offline) + register_stanza_plugin(Message, stanza.Offline) + + def get_count(self, **kwargs): + return self.xmpp['xep_0030'].get_info( + node='http://jabber.org/protocol/offline', + local=False, + **kwargs) + + def get_headers(self, **kwargs): + return self.xmpp['xep_0030'].get_items( + node='http://jabber.org/protocol/offline', + local=False, + **kwargs) + + def view(self, nodes, ifrom=None, block=True, timeout=None, callback=None): + if not isinstance(nodes, (list, set)): + nodes = [nodes] + + iq = self.xmpp.Iq() + iq['type'] = 'get' + iq['from'] = ifrom + offline = iq['offline'] + for node in nodes: + item = stanza.Item() + item['node'] = node + item['action'] = 'view' + offline.append(item) + + collector = Collector( + 'Offline_Results_%s' % iq['id'], + StanzaPath('message/offline')) + self.xmpp.register_handler(collector) + + if not block and callback is not None: + def wrapped_cb(iq): + results = collector.stop() + if iq['type'] == 'result': + iq['offline']['results'] = results + callback(iq) + return iq.send(block=block, timeout=timeout, callback=wrapped_cb) + else: + try: + resp = iq.send(block=block, timeout=timeout, callback=callback) + resp['offline']['results'] = collector.stop() + return resp + except XMPPError as e: + collector.stop() + raise e + + def remove(self, nodes, ifrom=None, block=True, timeout=None, callback=None): + if not isinstance(nodes, (list, set)): + nodes = [nodes] + + iq = self.xmpp.Iq() + iq['type'] = 'set' + iq['from'] = ifrom + offline = iq['offline'] + for node in nodes: + item = stanza.Item() + item['node'] = node + item['action'] = 'remove' + offline.append(item) + + return iq.send(block=block, timeout=timeout, callback=callback) + + def fetch(self, ifrom=None, block=True, timeout=None, callback=None): + iq = self.xmpp.Iq() + iq['type'] = 'set' + iq['from'] = ifrom + iq['offline']['fetch'] = True + + collector = Collector( + 'Offline_Results_%s' % iq['id'], + StanzaPath('message/offline')) + self.xmpp.register_handler(collector) + + if not block and callback is not None: + def wrapped_cb(iq): + results = collector.stop() + if iq['type'] == 'result': + iq['offline']['results'] = results + callback(iq) + return iq.send(block=block, timeout=timeout, callback=wrapped_cb) + else: + try: + resp = iq.send(block=block, timeout=timeout, callback=callback) + resp['offline']['results'] = collector.stop() + return resp + except XMPPError as e: + collector.stop() + raise e + + def purge(self, ifrom=None, block=True, timeout=None, callback=None): + iq = self.xmpp.Iq() + iq['type'] = 'set' + iq['from'] = ifrom + iq['offline']['purge'] = True + return iq.send(block=block, timeout=timeout, callback=callback) diff --git a/sleekxmpp/plugins/xep_0013/stanza.py b/sleekxmpp/plugins/xep_0013/stanza.py new file mode 100644 index 00000000..c9c69786 --- /dev/null +++ b/sleekxmpp/plugins/xep_0013/stanza.py @@ -0,0 +1,53 @@ +""" + 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 permissio +""" + +from sleekxmpp.jid import JID +from sleekxmpp.xmlstream import ElementBase, register_stanza_plugin + + +class Offline(ElementBase): + name = 'offline' + namespace = 'http://jabber.org/protocol/offline' + plugin_attrib = 'offline' + interfaces = set(['fetch', 'purge', 'results']) + bool_interfaces = interfaces + + def setup(self, xml=None): + ElementBase.setup(self, xml) + self._results = [] + + # The results interface is meant only as an easy + # way to access the set of collected message responses + # from the query. + + def get_results(self): + return self._results + + def set_results(self, values): + self._results = values + + def del_results(self): + self._results = [] + + +class Item(ElementBase): + name = 'item' + namespace = 'http://jabber.org/protocol/offline' + plugin_attrib = 'item' + interfaces = set(['action', 'node', 'jid']) + + actions = set(['view', 'remove']) + + def get_jid(self): + return JID(self._get_attr('jid')) + + def set_jid(self, value): + self._set_attr('jid', str(value)) + + +register_stanza_plugin(Offline, Item, iterable=True) diff --git a/sleekxmpp/plugins/xep_0313/stanza.py b/sleekxmpp/plugins/xep_0313/stanza.py index 6552dc5a..a33c2e35 100644 --- a/sleekxmpp/plugins/xep_0313/stanza.py +++ b/sleekxmpp/plugins/xep_0313/stanza.py @@ -22,7 +22,6 @@ class MAM(ElementBase): def setup(self, xml=None): ElementBase.setup(self, xml) - self._results = [] def get_start(self): @@ -49,7 +48,6 @@ class MAM(ElementBase): def set_with(self, value): self._set_sub_text('with', str(value)) - # The results interface is meant only as an easy # way to access the set of collected message responses # from the query. |