diff --git a/slixmpp/plugins/xep_0461/reply.py b/slixmpp/plugins/xep_0461/reply.py index 29bdf8b8..83b60868 100644 --- a/slixmpp/plugins/xep_0461/reply.py +++ b/slixmpp/plugins/xep_0461/reply.py @@ -1,3 +1,5 @@ +from typing import Optional + from slixmpp.plugins import BasePlugin from slixmpp.types import JidStr from slixmpp.xmlstream import StanzaBase @@ -36,13 +38,34 @@ class XEP_0461(BasePlugin): def _handle_reply_to_message(self, msg: StanzaBase): self.xmpp.event("message_reply", msg) - def send_reply(self, reply_to: JidStr, reply_id: str, **msg_kwargs): + def make_reply(self, reply_to: JidStr, reply_id: str, + fallback: Optional[str] = None, + quoted_nick: Optional[str] = None, **msg_kwargs): + """Create a replies message stanza + + :param reply_to: Full JID of the quoted author + :param reply_id: ID of the message to reply to + :param fallback: Body of the quoted message + :param quoted_nick: nickname of the quoted participant + :param msg_kwargs: Additional message parameters + """ + + msg = self.xmpp.make_message(**msg_kwargs) + msg["reply"]["to"] = reply_to + msg["reply"]["id"] = reply_id + if fallback: + msg["reply"].add_quoted_fallback(fallback, quoted_nick) + return msg + + def send_reply(self, reply_to: JidStr, reply_id: str, + fallback: Optional[str] = None, + quoted_nick: Optional[str] = None, **msg_kwargs): """ :param reply_to: Full JID of the quoted author :param reply_id: ID of the message to reply to + :param fallback: Body of the quoted message + :param quoted_nick: nickname of the quoted participant """ - msg = self.xmpp.make_message(**msg_kwargs) - msg["reply"]["to"] = reply_to - msg["reply"]["id"] = reply_id + msg = self.make_reply(reply_to, reply_id, fallback, quoted_nick, **msg_kwargs) msg.send() diff --git a/slixmpp/plugins/xep_0461/stanza.py b/slixmpp/plugins/xep_0461/stanza.py index bb010a7e..b3dfea09 100644 --- a/slixmpp/plugins/xep_0461/stanza.py +++ b/slixmpp/plugins/xep_0461/stanza.py @@ -30,11 +30,11 @@ class Reply(ElementBase): if nickname: quoted = "> " + nickname + ":\n" + quoted msg["body"] = quoted + msg["body"] - fallback = Fallback() - fallback["for"] = NS - fallback["body"]["start"] = 0 - fallback["body"]["end"] = len(quoted) - msg.append(fallback) + fallback_elem = Fallback() + fallback_elem["for"] = NS + fallback_elem["body"]["start"] = 0 + fallback_elem["body"]["end"] = len(quoted) + msg.append(fallback_elem) def get_fallback_body(self) -> str: msg = self.parent() @@ -50,6 +50,23 @@ class Reply(ElementBase): return body[start:end] else: return "" + + def strip_fallback_content(self) -> str: + msg = self.parent() + for fallback in msg["fallbacks"]: + if fallback["for"] == NS: + break + else: + return msg["body"] + + start = fallback["body"]["start"] + end = fallback["body"]["end"] + body = msg["body"] + + if 0 <= start < end <= len(body): + return body[:start] + body[end:] + else: + return body def register_plugins(): diff --git a/tests/test_stream_xep_0461.py b/tests/test_stream_xep_0461.py index cd4cfa94..af5676b3 100644 --- a/tests/test_stream_xep_0461.py +++ b/tests/test_stream_xep_0461.py @@ -8,12 +8,14 @@ class TestReply(SlixTest): self.stream_start(plugins=["xep_0461"]) def testFallBackBody(self): - async def on_reply(msg): + def on_reply(msg): 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(), + fallback=msg["reply"].strip_fallback_content(), + quoted_nick="res", mto="test@test.com", mbody=f"{start} to {end}", ) @@ -26,7 +28,7 @@ class TestReply(SlixTest): > quoted\nsome-body - + """ @@ -34,8 +36,11 @@ class TestReply(SlixTest): self.send( """ + > res:\n> some-body\n0 to 9 - 0 to 8 + + + """ )