From cec34686fcfe09281931e977fcf322dac275aad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 27 May 2020 22:28:47 +0200 Subject: [PATCH 1/3] New XEP: 0421 Occupant-id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- slixmpp/plugins/xep_0421/__init__.py | 13 +++++++++ slixmpp/plugins/xep_0421/occupant_id.py | 26 +++++++++++++++++ slixmpp/plugins/xep_0421/stanza.py | 37 +++++++++++++++++++++++++ tests/test_stanza_xep_0421.py | 29 +++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 slixmpp/plugins/xep_0421/__init__.py create mode 100644 slixmpp/plugins/xep_0421/occupant_id.py create mode 100644 slixmpp/plugins/xep_0421/stanza.py create mode 100644 tests/test_stanza_xep_0421.py diff --git a/slixmpp/plugins/xep_0421/__init__.py b/slixmpp/plugins/xep_0421/__init__.py new file mode 100644 index 00000000..4595ffad --- /dev/null +++ b/slixmpp/plugins/xep_0421/__init__.py @@ -0,0 +1,13 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2020 "Maxime “pep” Buquet " + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +from slixmpp.plugins.base import register_plugin +from slixmpp.plugins.xep_0421.stanza import OccupantId +from slixmpp.plugins.xep_0421.occupant_id import XEP_0421 + +register_plugin(XEP_0421) diff --git a/slixmpp/plugins/xep_0421/occupant_id.py b/slixmpp/plugins/xep_0421/occupant_id.py new file mode 100644 index 00000000..51d374fc --- /dev/null +++ b/slixmpp/plugins/xep_0421/occupant_id.py @@ -0,0 +1,26 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2020 "Maxime “pep” Buquet " + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +from slixmpp import Message +from slixmpp.plugins import BasePlugin +from slixmpp.xmlstream import register_stanza_plugin +from slixmpp.plugins.xep_0421 import stanza +from slixmpp.plugins.xep_0421.stanza import OccupantId + + +class XEP_0421(BasePlugin): + '''XEP-0421: Anonymous unique occupant identifiers for MUCs''' + + name = 'xep_0421' + description = 'Anonymous unique occupant identifiers for MUCs' + dependencies = {'xep_0045'} + stanza = stanza + + def plugin_init(self) -> None: + # XXX: This should be MucMessage. Someday.. + register_stanza_plugin(Message, OccupantId) diff --git a/slixmpp/plugins/xep_0421/stanza.py b/slixmpp/plugins/xep_0421/stanza.py new file mode 100644 index 00000000..d05bcfc1 --- /dev/null +++ b/slixmpp/plugins/xep_0421/stanza.py @@ -0,0 +1,37 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2020 "Maxime “pep” Buquet " + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +from slixmpp.xmlstream import ElementBase + + +class OccupantId(ElementBase): + ''' + An Occupant-id tag. + + An tag is set by the MUC. + + This is useful in semi-anon MUCs (and MUC-PMs) as a stable identifier to + prevent the usual races with nicknames. + + Without occupant-id, getting the following messages from MUC history would + prevent a client from asserting senders are the same entity: + + + Some message + + + + Some correction + + + + ''' + + name = 'occupant-id' + namespace = 'urn:xmpp:occupant-id:0' + interface = {'id'} diff --git a/tests/test_stanza_xep_0421.py b/tests/test_stanza_xep_0421.py new file mode 100644 index 00000000..dbd7a592 --- /dev/null +++ b/tests/test_stanza_xep_0421.py @@ -0,0 +1,29 @@ +import unittest +from slixmpp import JID, Message +from slixmpp.test import SlixTest +import slixmpp.plugins.xep_0421 as xep_0421 +from slixmpp.xmlstream import register_stanza_plugin + + +class TestOccupantId(SlixTest): + + def setUp(self): + register_stanza_plugin(Message, xep_0421.stanza.OccupantId) + + def testReadOccupantId(self): + result = """ + + Some message + + + """ + + msg = self.Message() + msg['type'] = 'groupchat' + msg['from'] = JID('foo@muc/nick1') + msg['body'] = 'Some message' + msg['occupant-id']['id'] = 'unique-id1' + + self.check(msg, result) + +suite = unittest.TestLoader().loadTestsFromTestCase(TestOccupantId) From 07b1a4c1cdbf8c7f8ea1d0771501d2a9cc2eb2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 27 May 2020 22:36:28 +0200 Subject: [PATCH 2/3] XEP-0421: add has_feature helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- slixmpp/plugins/xep_0421/occupant_id.py | 8 ++++++-- slixmpp/plugins/xep_0421/stanza.py | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/slixmpp/plugins/xep_0421/occupant_id.py b/slixmpp/plugins/xep_0421/occupant_id.py index 51d374fc..ff7fcda5 100644 --- a/slixmpp/plugins/xep_0421/occupant_id.py +++ b/slixmpp/plugins/xep_0421/occupant_id.py @@ -6,7 +6,7 @@ See the file LICENSE for copying permission. """ -from slixmpp import Message +from slixmpp import JID, Message from slixmpp.plugins import BasePlugin from slixmpp.xmlstream import register_stanza_plugin from slixmpp.plugins.xep_0421 import stanza @@ -18,9 +18,13 @@ class XEP_0421(BasePlugin): name = 'xep_0421' description = 'Anonymous unique occupant identifiers for MUCs' - dependencies = {'xep_0045'} + dependencies = {'xep_0030', 'xep_0045'} stanza = stanza def plugin_init(self) -> None: # XXX: This should be MucMessage. Someday.. register_stanza_plugin(Message, OccupantId) + + async def has_feature(self, jid: JID) -> bool: + info = await self.xmpp['xep_0030'].get_info(jid) + return self.stanza.NS in info.get_features() diff --git a/slixmpp/plugins/xep_0421/stanza.py b/slixmpp/plugins/xep_0421/stanza.py index d05bcfc1..0cb93959 100644 --- a/slixmpp/plugins/xep_0421/stanza.py +++ b/slixmpp/plugins/xep_0421/stanza.py @@ -9,6 +9,9 @@ from slixmpp.xmlstream import ElementBase +NS = 'urn:xmpp:occupant-id:0' + + class OccupantId(ElementBase): ''' An Occupant-id tag. @@ -33,5 +36,5 @@ class OccupantId(ElementBase): ''' name = 'occupant-id' - namespace = 'urn:xmpp:occupant-id:0' + namespace = NS interface = {'id'} From a29ce4b54774de050f352606d06bb111ecff4a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 27 May 2020 23:45:41 +0200 Subject: [PATCH 3/3] XEP-0421: Add shorthand to namespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- slixmpp/plugins/xep_0421/occupant_id.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/slixmpp/plugins/xep_0421/occupant_id.py b/slixmpp/plugins/xep_0421/occupant_id.py index ff7fcda5..116bf2d9 100644 --- a/slixmpp/plugins/xep_0421/occupant_id.py +++ b/slixmpp/plugins/xep_0421/occupant_id.py @@ -20,6 +20,7 @@ class XEP_0421(BasePlugin): description = 'Anonymous unique occupant identifiers for MUCs' dependencies = {'xep_0030', 'xep_0045'} stanza = stanza + namespace = stanza.NS def plugin_init(self) -> None: # XXX: This should be MucMessage. Someday.. @@ -27,4 +28,4 @@ class XEP_0421(BasePlugin): async def has_feature(self, jid: JID) -> bool: info = await self.xmpp['xep_0030'].get_info(jid) - return self.stanza.NS in info.get_features() + return self.namespace in info.get_features()