summaryrefslogtreecommitdiff
path: root/slixmpp/plugins/xep_0461
diff options
context:
space:
mode:
Diffstat (limited to 'slixmpp/plugins/xep_0461')
-rw-r--r--slixmpp/plugins/xep_0461/__init__.py6
-rw-r--r--slixmpp/plugins/xep_0461/reply.py48
-rw-r--r--slixmpp/plugins/xep_0461/stanza.py47
3 files changed, 101 insertions, 0 deletions
diff --git a/slixmpp/plugins/xep_0461/__init__.py b/slixmpp/plugins/xep_0461/__init__.py
new file mode 100644
index 00000000..1e9b2829
--- /dev/null
+++ b/slixmpp/plugins/xep_0461/__init__.py
@@ -0,0 +1,6 @@
+from slixmpp.plugins.base import register_plugin
+
+from .reply import XEP_0461
+from . import stanza
+
+register_plugin(XEP_0461)
diff --git a/slixmpp/plugins/xep_0461/reply.py b/slixmpp/plugins/xep_0461/reply.py
new file mode 100644
index 00000000..6607012a
--- /dev/null
+++ b/slixmpp/plugins/xep_0461/reply.py
@@ -0,0 +1,48 @@
+from slixmpp.plugins import BasePlugin
+from slixmpp.types import JidStr
+from slixmpp.xmlstream import StanzaBase
+from slixmpp.xmlstream.handler import Callback
+from slixmpp.xmlstream.matcher import StanzaPath
+
+from . import stanza
+
+
+class XEP_0461(BasePlugin):
+ """XEP-0461: Message Replies"""
+
+ name = "xep_0461"
+ description = "XEP-0461: Message Replies"
+
+ dependencies = {"xep_0030"}
+ stanza = stanza
+ namespace = stanza.NS
+
+ def plugin_init(self) -> None:
+ stanza.register_plugins()
+ self.xmpp.register_handler(
+ Callback(
+ "Message replied to",
+ StanzaPath("message/reply"),
+ self._handle_reply_to_message,
+ )
+ )
+
+ def plugin_end(self):
+ self.xmpp.plugin["xep_0030"].del_feature(feature=stanza.NS)
+
+ def session_bind(self, jid):
+ self.xmpp.plugin["xep_0030"].add_feature(feature=stanza.NS)
+
+ 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):
+ """
+
+ :param reply_to: Full JID of the quoted author
+ :param reply_id: ID of the message to reply to
+ """
+ msg = self.xmpp.make_message(**msg_kwargs)
+ msg["reply"]["to"] = reply_to
+ msg["reply"]["id"] = reply_id
+ msg.send()
diff --git a/slixmpp/plugins/xep_0461/stanza.py b/slixmpp/plugins/xep_0461/stanza.py
new file mode 100644
index 00000000..b99b2745
--- /dev/null
+++ b/slixmpp/plugins/xep_0461/stanza.py
@@ -0,0 +1,47 @@
+from slixmpp.stanza import Message
+from slixmpp.xmlstream import ElementBase, register_stanza_plugin
+
+NS = "urn:xmpp:reply:0"
+
+
+class Reply(ElementBase):
+ namespace = NS
+ name = "reply"
+ plugin_attrib = "reply"
+ interfaces = {"id", "to"}
+
+
+class FeatureFallBack(ElementBase):
+ # should also be a multi attrib
+ namespace = "urn:xmpp:feature-fallback:0"
+ name = "fallback"
+ plugin_attrib = "feature_fallback"
+ interfaces = {"for"}
+
+ 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:
+ return body[:start] + body[end:]
+
+
+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 register_plugins():
+ register_stanza_plugin(Message, Reply)
+ register_stanza_plugin(Message, FeatureFallBack)
+ register_stanza_plugin(FeatureFallBack, FallBackBody)