diff --git a/slixmpp/plugins/__init__.py b/slixmpp/plugins/__init__.py index ac7482ee..be01b06e 100644 --- a/slixmpp/plugins/__init__.py +++ b/slixmpp/plugins/__init__.py @@ -113,4 +113,5 @@ __all__ = [ 'xep_0439', # Quick Response 'xep_0441', # Message Archive Management Preferences 'xep_0444', # Message Reactions + 'xep_0461', # Message Replies ] diff --git a/slixmpp/plugins/xep_0461/stanza.py b/slixmpp/plugins/xep_0461/stanza.py index b99b2745..f52d0964 100644 --- a/slixmpp/plugins/xep_0461/stanza.py +++ b/slixmpp/plugins/xep_0461/stanza.py @@ -18,18 +18,33 @@ class FeatureFallBack(ElementBase): 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+1] + 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"] - try: - start = int(start) - end = int(end) - except ValueError: - return body - else: + if start <= end < len(body): return body[:start] + body[end:] + else: + return body + + def add_quoted_fallback(self, fallback: str): + msg = self.parent() + quoted = "\n".join("> " + x.strip() for x in fallback.split("\n")) + "\n" + 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) - 1 class FallBackBody(ElementBase): @@ -40,6 +55,24 @@ class FallBackBody(ElementBase): 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 register_plugins(): register_stanza_plugin(Message, Reply) diff --git a/tests/test_stanza_xep_0461.py b/tests/test_stanza_xep_0461.py index b9550481..e43b8138 100644 --- a/tests/test_stanza_xep_0461.py +++ b/tests/test_stanza_xep_0461.py @@ -27,13 +27,13 @@ class TestReply(SlixTest): 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["feature_fallback"]["fallback_body"]["start"] = 0 + message["feature_fallback"]["fallback_body"]["end"] = 6 self.check( message, """ - + M 12345\nrealbody @@ -44,5 +44,32 @@ class TestReply(SlixTest): assert message["feature_fallback"].get_stripped_body() == "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 + self.check( + msg, + """ + + > Anna wrote:\n> Hi, how are you?\nGreat + + + + + """ + ) + + def testGetFallBackBody(self): + body = "Anna wrote:\nHi, how are you?" + quoted = "> Anna wrote:\n> Hi, how are you?\n" + + msg = Message() + msg["body"] = "Great" + msg["feature_fallback"].add_quoted_fallback(body) + body2 = msg["feature_fallback"].get_fallback_body() + self.assertTrue(body2 == quoted, body2) + suite = unittest.TestLoader().loadTestsFromTestCase(TestReply)