From 11ac5867fff9b53b432793a834bf24a8bd3ff1e2 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sun, 6 Dec 2020 21:28:40 +0100 Subject: XEP-0333: Rename plugin file from "hints" to "markers" --- slixmpp/plugins/xep_0333/__init__.py | 2 +- slixmpp/plugins/xep_0333/hints.py | 55 ------------------------------------ slixmpp/plugins/xep_0333/markers.py | 55 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 56 deletions(-) delete mode 100644 slixmpp/plugins/xep_0333/hints.py create mode 100644 slixmpp/plugins/xep_0333/markers.py diff --git a/slixmpp/plugins/xep_0333/__init__.py b/slixmpp/plugins/xep_0333/__init__.py index 472971d6..a50dbe2b 100644 --- a/slixmpp/plugins/xep_0333/__init__.py +++ b/slixmpp/plugins/xep_0333/__init__.py @@ -9,6 +9,6 @@ from slixmpp.plugins.base import register_plugin from slixmpp.plugins.xep_0333.stanza import Markable, Received, Displayed, Acknowledged -from slixmpp.plugins.xep_0333.hints import XEP_0333 +from slixmpp.plugins.xep_0333.markers import XEP_0333 register_plugin(XEP_0333) diff --git a/slixmpp/plugins/xep_0333/hints.py b/slixmpp/plugins/xep_0333/hints.py deleted file mode 100644 index 5a3d3f9a..00000000 --- a/slixmpp/plugins/xep_0333/hints.py +++ /dev/null @@ -1,55 +0,0 @@ -""" - slixmpp: The Slick XMPP Library - Copyright (C) 2016 Emmanuel Gil Peyrot - This file is part of slixmpp. - - See the file LICENSE for copying permission. -""" - -import logging - -from slixmpp import Message -from slixmpp.plugins import BasePlugin -from slixmpp.xmlstream import register_stanza_plugin -from slixmpp.xmlstream.handler import Callback -from slixmpp.xmlstream.matcher import StanzaPath -from slixmpp.plugins.xep_0333 import stanza, Markable, Received, Displayed, Acknowledged - -log = logging.getLogger(__name__) - -class XEP_0333(BasePlugin): - - name = 'xep_0333' - description = 'XEP-0333: Chat Markers' - stanza = stanza - - def plugin_init(self): - register_stanza_plugin(Message, Markable) - register_stanza_plugin(Message, Received) - register_stanza_plugin(Message, Displayed) - register_stanza_plugin(Message, Acknowledged) - - self.xmpp.register_handler( - Callback('Received Chat Marker', - StanzaPath('message/received'), - self._handle_received)) - self.xmpp.register_handler( - Callback('Displayed Chat Marker', - StanzaPath('message/displayed'), - self._handle_displayed)) - self.xmpp.register_handler( - Callback('Acknowledged Chat Marker', - StanzaPath('message/acknowledged'), - self._handle_acknowledged)) - - def _handle_received(self, message): - self.xmpp.event('marker_received', message) - self.xmpp.event('marker', message) - - def _handle_displayed(self, message): - self.xmpp.event('marker_displayed', message) - self.xmpp.event('marker', message) - - def _handle_acknowledged(self, message): - self.xmpp.event('marker_acknowledged', message) - self.xmpp.event('marker', message) diff --git a/slixmpp/plugins/xep_0333/markers.py b/slixmpp/plugins/xep_0333/markers.py new file mode 100644 index 00000000..5a3d3f9a --- /dev/null +++ b/slixmpp/plugins/xep_0333/markers.py @@ -0,0 +1,55 @@ +""" + slixmpp: The Slick XMPP Library + Copyright (C) 2016 Emmanuel Gil Peyrot + This file is part of slixmpp. + + See the file LICENSE for copying permission. +""" + +import logging + +from slixmpp import Message +from slixmpp.plugins import BasePlugin +from slixmpp.xmlstream import register_stanza_plugin +from slixmpp.xmlstream.handler import Callback +from slixmpp.xmlstream.matcher import StanzaPath +from slixmpp.plugins.xep_0333 import stanza, Markable, Received, Displayed, Acknowledged + +log = logging.getLogger(__name__) + +class XEP_0333(BasePlugin): + + name = 'xep_0333' + description = 'XEP-0333: Chat Markers' + stanza = stanza + + def plugin_init(self): + register_stanza_plugin(Message, Markable) + register_stanza_plugin(Message, Received) + register_stanza_plugin(Message, Displayed) + register_stanza_plugin(Message, Acknowledged) + + self.xmpp.register_handler( + Callback('Received Chat Marker', + StanzaPath('message/received'), + self._handle_received)) + self.xmpp.register_handler( + Callback('Displayed Chat Marker', + StanzaPath('message/displayed'), + self._handle_displayed)) + self.xmpp.register_handler( + Callback('Acknowledged Chat Marker', + StanzaPath('message/acknowledged'), + self._handle_acknowledged)) + + def _handle_received(self, message): + self.xmpp.event('marker_received', message) + self.xmpp.event('marker', message) + + def _handle_displayed(self, message): + self.xmpp.event('marker_displayed', message) + self.xmpp.event('marker', message) + + def _handle_acknowledged(self, message): + self.xmpp.event('marker_acknowledged', message) + self.xmpp.event('marker', message) -- cgit v1.2.3 From b3e2a8eb91f5521b7dc43020e388de8918aaa33e Mon Sep 17 00:00:00 2001 From: mathieui Date: Sun, 6 Dec 2020 21:45:22 +0100 Subject: XEP-0333: Add missing feature, and a send_marker method --- slixmpp/plugins/xep_0333/markers.py | 31 ++++++++++++++++++++++++++++++- slixmpp/plugins/xep_0333/stanza.py | 13 +++++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/slixmpp/plugins/xep_0333/markers.py b/slixmpp/plugins/xep_0333/markers.py index 5a3d3f9a..b8a003df 100644 --- a/slixmpp/plugins/xep_0333/markers.py +++ b/slixmpp/plugins/xep_0333/markers.py @@ -7,8 +7,9 @@ """ import logging +from typing import Optional -from slixmpp import Message +from slixmpp import Message, JID from slixmpp.plugins import BasePlugin from slixmpp.xmlstream import register_stanza_plugin from slixmpp.xmlstream.handler import Callback @@ -22,6 +23,7 @@ class XEP_0333(BasePlugin): name = 'xep_0333' description = 'XEP-0333: Chat Markers' stanza = stanza + dependencies = {'xep_0030'} def plugin_init(self): register_stanza_plugin(Message, Markable) @@ -42,6 +44,12 @@ class XEP_0333(BasePlugin): StanzaPath('message/acknowledged'), self._handle_acknowledged)) + def session_bind(self, jid): + self.xmpp.plugin['xep_0030'].add_feature(stanza.NS) + + def plugin_end(self): + self.xmpp.plugin['xep_0030'].del_feature(feature=stanza.NS) + def _handle_received(self, message): self.xmpp.event('marker_received', message) self.xmpp.event('marker', message) @@ -53,3 +61,24 @@ class XEP_0333(BasePlugin): def _handle_acknowledged(self, message): self.xmpp.event('marker_acknowledged', message) self.xmpp.event('marker', message) + + def send_marker(self, mto: JID, id: str, marker: str, + thread: Optional[str] = None, *, + mfrom: Optional[JID] = None): + """ + Send a chat marker. + + :param JID mto: recipient of the marker + :param str id: Identifier of the marked message + :param str marker: Marker to send (one of + displayed, retrieved, or acknowledged) + :param str thread: Message thread + :param str mfrom: Use a specific JID to send the message + """ + if marker not in ('displayed', 'retrieved', 'acknowledged'): + raise ValueError('Invalid marker: %s' % marker) + msg = self.xmpp.make_message(mto=mto, mfrom=mfrom) + if thread: + msg['thread'] = thread + msg[marker]['id'] = id + msg.send() diff --git a/slixmpp/plugins/xep_0333/stanza.py b/slixmpp/plugins/xep_0333/stanza.py index 42cfc21a..77cd47a4 100644 --- a/slixmpp/plugins/xep_0333/stanza.py +++ b/slixmpp/plugins/xep_0333/stanza.py @@ -8,25 +8,30 @@ from slixmpp.xmlstream import ElementBase +NS ='urn:xmpp:chat-markers:0' + class Markable(ElementBase): name = 'markable' plugin_attrib = 'markable' - namespace = 'urn:xmpp:chat-markers:0' + namespace = NS + class Received(ElementBase): name = 'received' plugin_attrib = 'received' - namespace = 'urn:xmpp:chat-markers:0' + namespace = NS interfaces = {'id'} + class Displayed(ElementBase): name = 'displayed' plugin_attrib = 'displayed' - namespace = 'urn:xmpp:chat-markers:0' + namespace = NS interfaces = {'id'} + class Acknowledged(ElementBase): name = 'acknowledged' plugin_attrib = 'acknowledged' - namespace = 'urn:xmpp:chat-markers:0' + namespace = NS interfaces = {'id'} -- cgit v1.2.3 From ef6dab81b9f1e3833b26c5d29e94bcba274798c1 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sun, 6 Dec 2020 21:45:53 +0100 Subject: itests: Add a chat markers test --- itests/test_chatmarkers.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 itests/test_chatmarkers.py diff --git a/itests/test_chatmarkers.py b/itests/test_chatmarkers.py new file mode 100644 index 00000000..f23833d8 --- /dev/null +++ b/itests/test_chatmarkers.py @@ -0,0 +1,27 @@ +import unittest +from slixmpp.test.integration import SlixIntegration + + +class TestMarkers(SlixIntegration): + async def asyncSetUp(self): + self.add_client( + self.envjid('CI_ACCOUNT1'), + self.envstr('CI_ACCOUNT1_PASSWORD'), + ) + self.add_client( + self.envjid('CI_ACCOUNT2'), + self.envstr('CI_ACCOUNT2_PASSWORD'), + ) + self.register_plugins(['xep_0333']) + await self.connect_clients() + + async def test_send_marker(self): + """Send and receive a chat marker""" + self.clients[0]['xep_0333'].send_marker( + self.clients[1].boundjid.full, + 'toto', + 'displayed', + ) + msg = await self.clients[1].wait_until('marker_displayed') + +suite = unittest.TestLoader().loadTestsFromTestCase(TestMarkers) -- cgit v1.2.3