From 84a7ac020f0944b7a8df1deb029f04da5ccea8db Mon Sep 17 00:00:00 2001 From: nicoco Date: Wed, 19 Jul 2023 07:29:37 +0200 Subject: [PATCH] XEP-0461: rely on XEP-0428 for fallback Breaks the previous fallback helpers, we now rely on XEP-0461 instead --- slixmpp/plugins/xep_0461/reply.py | 2 +- slixmpp/plugins/xep_0461/stanza.py | 81 ++++++++---------------------- tests/test_stanza_xep_0461.py | 20 ++++---- tests/test_stream_xep_0461.py | 4 +- 4 files changed, 35 insertions(+), 72 deletions(-) diff --git a/slixmpp/plugins/xep_0461/reply.py b/slixmpp/plugins/xep_0461/reply.py index 6607012a..29bdf8b8 100644 --- a/slixmpp/plugins/xep_0461/reply.py +++ b/slixmpp/plugins/xep_0461/reply.py @@ -13,7 +13,7 @@ class XEP_0461(BasePlugin): name = "xep_0461" description = "XEP-0461: Message Replies" - dependencies = {"xep_0030"} + dependencies = {"xep_0030", "xep_0428"} stanza = stanza namespace = stanza.NS diff --git a/slixmpp/plugins/xep_0461/stanza.py b/slixmpp/plugins/xep_0461/stanza.py index 8e4981f7..bb010a7e 100644 --- a/slixmpp/plugins/xep_0461/stanza.py +++ b/slixmpp/plugins/xep_0461/stanza.py @@ -2,6 +2,7 @@ from typing import Optional from slixmpp.stanza import Message from slixmpp.xmlstream import ElementBase, register_stanza_plugin +from slixmpp.plugins.xep_0428.stanza import Fallback NS = "urn:xmpp:reply:0" @@ -12,39 +13,11 @@ class Reply(ElementBase): plugin_attrib = "reply" interfaces = {"id", "to"} - -class FeatureFallBack(ElementBase): - # should also be a multi attrib - namespace = "urn:xmpp:fallback:0" - name = "fallback" - plugin_attrib = "feature_fallback" - interfaces = {"for"} - - def get_fallback_body(self): - # only works for a single fallback_body attrib - start = self["fallback_body"]["start"] - end = self["fallback_body"]["end"] - body = self.parent()["body"] - if start <= end: - return body[start:end] - else: - return "" - - def get_stripped_body(self): - # only works for a single fallback_body attrib - start = self["fallback_body"]["start"] - end = self["fallback_body"]["end"] - body = self.parent()["body"] - if start <= end < len(body): - return body[:start] + body[end:] - else: - return body - def add_quoted_fallback(self, fallback: str, nickname: Optional[str] = None): """ Add plain text fallback for clients not implementing XEP-0461. - ``msg["feature_fallback"].add_quoted_fallback("Some text", "Bob")`` will + ``msg["reply"].add_quoted_fallback("Some text", "Bob")`` will prepend "> Bob:\n> Some text\n" to the body of the message, and set the fallback_body attributes accordingly, so that clients implementing XEP-0461 can hide the fallback text. @@ -57,39 +30,27 @@ class FeatureFallBack(ElementBase): if nickname: quoted = "> " + nickname + ":\n" + quoted msg["body"] = quoted + msg["body"] - msg["feature_fallback"]["for"] = NS - msg["feature_fallback"]["fallback_body"]["start"] = 0 - msg["feature_fallback"]["fallback_body"]["end"] = len(quoted) + fallback = Fallback() + fallback["for"] = NS + fallback["body"]["start"] = 0 + fallback["body"]["end"] = len(quoted) + msg.append(fallback) - -class FallBackBody(ElementBase): - # According to https://xmpp.org/extensions/inbox/compatibility-fallback.html - # this should be a multi_attrib *but* since it's a protoXEP, we'll see... - namespace = FeatureFallBack.namespace - name = "body" - plugin_attrib = "fallback_body" - interfaces = {"start", "end"} - - def set_start(self, v: int): - self._set_attr("start", str(v)) - - def get_start(self): - try: - return int(self._get_attr("start")) - except ValueError: - return 0 - - def set_end(self, v: int): - self._set_attr("end", str(v)) - - def get_end(self): - try: - return int(self._get_attr("end")) - except ValueError: - return 0 + def get_fallback_body(self) -> str: + msg = self.parent() + for fallback in msg["fallbacks"]: + if fallback["for"] == NS: + break + else: + return "" + start = fallback["body"]["start"] + end = fallback["body"]["end"] + body = msg["body"] + if start <= end: + return body[start:end] + else: + return "" def register_plugins(): register_stanza_plugin(Message, Reply) - register_stanza_plugin(Message, FeatureFallBack) - register_stanza_plugin(FeatureFallBack, FallBackBody) diff --git a/tests/test_stanza_xep_0461.py b/tests/test_stanza_xep_0461.py index 12c92bd4..6803d7f3 100644 --- a/tests/test_stanza_xep_0461.py +++ b/tests/test_stanza_xep_0461.py @@ -1,11 +1,13 @@ import unittest from slixmpp import Message from slixmpp.test import SlixTest +from slixmpp.plugins.xep_0428 import stanza as fallback_stanza from slixmpp.plugins.xep_0461 import stanza class TestReply(SlixTest): def setUp(self): + fallback_stanza.register_plugins() stanza.register_plugins() def testReply(self): @@ -26,9 +28,9 @@ class TestReply(SlixTest): def testFallback(self): message = Message() message["body"] = "12345\nrealbody" - message["feature_fallback"]["for"] = "NS" - message["feature_fallback"]["fallback_body"]["start"] = 0 - message["feature_fallback"]["fallback_body"]["end"] = 6 + message["fallback"]["for"] = "NS" + message["fallback"]["body"]["start"] = 0 + message["fallback"]["body"]["end"] = 6 self.check( message, @@ -42,18 +44,18 @@ class TestReply(SlixTest): """, ) - assert message["feature_fallback"].get_stripped_body() == "realbody" + assert message["fallback"].get_stripped_body("NS") == "realbody" def testAddFallBackHelper(self): msg = Message() msg["body"] = "Great" - msg["feature_fallback"].add_quoted_fallback("Anna wrote:\nHi, how are you?") - # ugly dedent but the test does not pass without it + msg["reply"].add_quoted_fallback("Anna wrote:\nHi, how are you?") self.check( - msg, + msg, # language=XML """ > Anna wrote:\n> Hi, how are you?\nGreat + @@ -67,8 +69,8 @@ class TestReply(SlixTest): msg = Message() msg["body"] = "Great" - msg["feature_fallback"].add_quoted_fallback(body) - body2 = msg["feature_fallback"].get_fallback_body() + msg["reply"].add_quoted_fallback(body) + body2 = msg["reply"].get_fallback_body() self.assertTrue(body2 == quoted, body2) diff --git a/tests/test_stream_xep_0461.py b/tests/test_stream_xep_0461.py index 91388f1d..cd4cfa94 100644 --- a/tests/test_stream_xep_0461.py +++ b/tests/test_stream_xep_0461.py @@ -9,8 +9,8 @@ class TestReply(SlixTest): def testFallBackBody(self): async def on_reply(msg): - start = msg["feature_fallback"]["fallback_body"]["start"] - end = msg["feature_fallback"]["fallback_body"]["end"] + start = msg["fallback"]["body"]["start"] + end = msg["fallback"]["body"]["end"] self.xmpp["xep_0461"].send_reply( reply_to=msg.get_from(), reply_id=msg.get_id(),