diff options
author | Florent Le Coz <louiz@louiz.org> | 2014-07-17 14:19:04 +0200 |
---|---|---|
committer | Florent Le Coz <louiz@louiz.org> | 2014-07-17 14:19:04 +0200 |
commit | 5ab77c745270d7d5c016c1dc7ef2a82533a4b16e (patch) | |
tree | 259377cc666f8b9c7954fc4e7b8f7a912bcfe101 /slixmpp/plugins/xep_0184 | |
parent | e5582694c07236e6830c20361840360a1dde37f3 (diff) | |
download | slixmpp-5ab77c745270d7d5c016c1dc7ef2a82533a4b16e.tar.gz slixmpp-5ab77c745270d7d5c016c1dc7ef2a82533a4b16e.tar.bz2 slixmpp-5ab77c745270d7d5c016c1dc7ef2a82533a4b16e.tar.xz slixmpp-5ab77c745270d7d5c016c1dc7ef2a82533a4b16e.zip |
Rename to slixmpp
Diffstat (limited to 'slixmpp/plugins/xep_0184')
-rw-r--r-- | slixmpp/plugins/xep_0184/__init__.py | 19 | ||||
-rw-r--r-- | slixmpp/plugins/xep_0184/receipt.py | 131 | ||||
-rw-r--r-- | slixmpp/plugins/xep_0184/stanza.py | 72 |
3 files changed, 222 insertions, 0 deletions
diff --git a/slixmpp/plugins/xep_0184/__init__.py b/slixmpp/plugins/xep_0184/__init__.py new file mode 100644 index 00000000..83c2d7c2 --- /dev/null +++ b/slixmpp/plugins/xep_0184/__init__.py @@ -0,0 +1,19 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2012 Erik Reuterborg Larsson, Nathanael C. Fritz + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +from slixmpp.plugins.base import register_plugin + +from slixmpp.plugins.xep_0184.stanza import Request, Received +from slixmpp.plugins.xep_0184.receipt import XEP_0184 + + +register_plugin(XEP_0184) + + +# Retain some backwards compatibility +xep_0184 = XEP_0184 diff --git a/slixmpp/plugins/xep_0184/receipt.py b/slixmpp/plugins/xep_0184/receipt.py new file mode 100644 index 00000000..9ca138b9 --- /dev/null +++ b/slixmpp/plugins/xep_0184/receipt.py @@ -0,0 +1,131 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2012 Erik Reuterborg Larsson, Nathanael C. Fritz + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +import logging + +from slixmpp.stanza import Message +from slixmpp.xmlstream import register_stanza_plugin +from slixmpp.xmlstream.handler import Callback +from slixmpp.xmlstream.matcher import StanzaPath +from slixmpp.plugins import BasePlugin +from slixmpp.plugins.xep_0184 import stanza, Request, Received + + +class XEP_0184(BasePlugin): + + """ + XEP-0184: Message Delivery Receipts + """ + + name = 'xep_0184' + description = 'XEP-0184: Message Delivery Receipts' + dependencies = set(['xep_0030']) + stanza = stanza + default_config = { + 'auto_ack': True, + 'auto_request': False + } + + ack_types = ('normal', 'chat', 'headline') + + def plugin_init(self): + register_stanza_plugin(Message, Request) + register_stanza_plugin(Message, Received) + + self.xmpp.add_filter('out', self._filter_add_receipt_request) + + self.xmpp.register_handler( + Callback('Message Receipt', + StanzaPath('message/receipt'), + self._handle_receipt_received)) + + self.xmpp.register_handler( + Callback('Message Receipt Request', + StanzaPath('message/request_receipt'), + self._handle_receipt_request)) + + def plugin_end(self): + self.xmpp['xep_0030'].del_feature('urn:xmpp:receipts') + self.xmpp.del_filter('out', self._filter_add_receipt_request) + self.xmpp.remove_handler('Message Receipt') + self.xmpp.remove_handler('Message Receipt Request') + + def session_bind(self, jid): + self.xmpp['xep_0030'].add_feature('urn:xmpp:receipts') + + def ack(self, msg): + """ + Acknowledge a message by sending a receipt. + + Arguments: + msg -- The message to acknowledge. + """ + ack = self.xmpp.Message() + ack['to'] = msg['from'] + ack['from'] = msg['to'] + ack['receipt'] = msg['id'] + ack['id'] = msg['id'] + ack.send() + + def _handle_receipt_received(self, msg): + self.xmpp.event('receipt_received', msg) + + def _handle_receipt_request(self, msg): + """ + Auto-ack message receipt requests if ``self.auto_ack`` is ``True``. + + Arguments: + msg -- The incoming message requesting a receipt. + """ + if self.auto_ack: + if msg['type'] in self.ack_types: + if not msg['receipt']: + self.ack(msg) + + def _filter_add_receipt_request(self, stanza): + """ + Auto add receipt requests to outgoing messages, if: + + - ``self.auto_request`` is set to ``True`` + - The message is not for groupchat + - The message does not contain a receipt acknowledgment + - The recipient is a bare JID or, if a full JID, one + that has the ``urn:xmpp:receipts`` feature enabled + + The disco cache is checked if a full JID is specified in + the outgoing message, which may mean a round-trip disco#info + delay for the first message sent to the JID if entity caps + are not used. + """ + + if not self.auto_request: + return stanza + + if not isinstance(stanza, Message): + return stanza + + if stanza['request_receipt']: + return stanza + + if not stanza['type'] in self.ack_types: + return stanza + + if stanza['receipt']: + return stanza + + if not stanza['body']: + return stanza + + if stanza['to'].resource: + if not self.xmpp['xep_0030'].supports(stanza['to'], + feature='urn:xmpp:receipts', + cached=True): + return stanza + + stanza['request_receipt'] = True + return stanza diff --git a/slixmpp/plugins/xep_0184/stanza.py b/slixmpp/plugins/xep_0184/stanza.py new file mode 100644 index 00000000..16a640e7 --- /dev/null +++ b/slixmpp/plugins/xep_0184/stanza.py @@ -0,0 +1,72 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2012 Erik Reuterborg Larsson, Nathanael C. Fritz + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +from slixmpp.xmlstream.stanzabase import ElementBase, ET + + +class Request(ElementBase): + namespace = 'urn:xmpp:receipts' + name = 'request' + plugin_attrib = 'request_receipt' + interfaces = set(('request_receipt',)) + sub_interfaces = interfaces + is_extension = True + + def setup(self, xml=None): + self.xml = ET.Element('') + return True + + def set_request_receipt(self, val): + self.del_request_receipt() + if val: + parent = self.parent() + parent._set_sub_text("{%s}request" % self.namespace, keep=True) + if not parent['id']: + if parent.stream: + parent['id'] = parent.stream.new_id() + + def get_request_receipt(self): + parent = self.parent() + if parent.find("{%s}request" % self.namespace) is not None: + return True + else: + return False + + def del_request_receipt(self): + self.parent()._del_sub("{%s}request" % self.namespace) + + +class Received(ElementBase): + namespace = 'urn:xmpp:receipts' + name = 'received' + plugin_attrib = 'receipt' + interfaces = set(['receipt']) + sub_interfaces = interfaces + is_extension = True + + def setup(self, xml=None): + self.xml = ET.Element('') + return True + + def set_receipt(self, value): + self.del_receipt() + if value: + parent = self.parent() + xml = ET.Element("{%s}received" % self.namespace) + xml.attrib['id'] = value + parent.append(xml) + + def get_receipt(self): + parent = self.parent() + xml = parent.find("{%s}received" % self.namespace) + if xml is not None: + return xml.attrib.get('id', '') + return '' + + def del_receipt(self): + self.parent()._del_sub('{%s}received' % self.namespace) |