aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src
diff options
context:
space:
mode:
authorfiaxh <git@lightrise.org>2023-01-06 13:19:42 +0100
committerfiaxh <git@lightrise.org>2023-01-06 14:03:54 +0100
commitdc52e7595cca06d0a2da7d11b3c88cb2f7ce529c (patch)
tree111f4a86a8541ce51bba7ec56f5b32197fcefc83 /xmpp-vala/src
parent4d7809bb12199a598b531ca3ca019a4bb5a867f7 (diff)
downloaddino-dc52e7595cca06d0a2da7d11b3c88cb2f7ce529c.tar.gz
dino-dc52e7595cca06d0a2da7d11b3c88cb2f7ce529c.zip
Add support for XEP-0461 replies (with fallback)
Diffstat (limited to 'xmpp-vala/src')
-rw-r--r--xmpp-vala/src/module/xep/0428_fallback_indication.vala67
-rw-r--r--xmpp-vala/src/module/xep/0461_replies.vala41
2 files changed, 108 insertions, 0 deletions
diff --git a/xmpp-vala/src/module/xep/0428_fallback_indication.vala b/xmpp-vala/src/module/xep/0428_fallback_indication.vala
new file mode 100644
index 00000000..6686b0ee
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0428_fallback_indication.vala
@@ -0,0 +1,67 @@
+using Gee;
+
+namespace Xmpp.Xep.FallbackIndication {
+
+ public const string NS_URI = "urn:xmpp:fallback:0";
+
+ public class Fallback {
+ public string ns_uri { get; set; }
+ public FallbackLocation[] locations;
+
+
+ public Fallback(string ns_uri, FallbackLocation[] locations) {
+ this.ns_uri = ns_uri;
+ this.locations = locations;
+ }
+ }
+
+ public class FallbackLocation {
+ public int from_char { get; set; }
+ public int to_char { get; set; }
+
+ public FallbackLocation(int from_char, int to_char) {
+ this.from_char = from_char;
+ this.to_char = to_char;
+ }
+ }
+
+ public static void set_fallback(MessageStanza message, Fallback fallback) {
+ StanzaNode fallback_node = (new StanzaNode.build("fallback", NS_URI))
+ .add_self_xmlns()
+ .put_attribute("for", fallback.ns_uri);
+ foreach (FallbackLocation location in fallback.locations) {
+ fallback_node.put_node(new StanzaNode.build("body", NS_URI)
+ .add_self_xmlns()
+ .put_attribute("start", location.from_char.to_string())
+ .put_attribute("end", location.to_char.to_string()));
+ }
+ message.stanza.put_node(fallback_node);
+ }
+
+ public Gee.List<Fallback> get_fallbacks(MessageStanza message) {
+ var ret = new ArrayList<Fallback>();
+
+ Gee.List<StanzaNode> fallback_nodes = message.stanza.get_subnodes("fallback", NS_URI);
+ if (fallback_nodes.is_empty) return ret;
+
+ foreach (StanzaNode fallback_node in fallback_nodes) {
+ string? ns_uri = fallback_node.get_attribute("for");
+ if (ns_uri == null) continue;
+
+ Gee.List<StanzaNode> body_nodes = fallback_node.get_subnodes("body", NS_URI);
+ if (body_nodes.is_empty) continue;
+
+ var locations = new ArrayList<FallbackLocation>();
+ foreach (StanzaNode body_node in body_nodes) {
+ int start_char = body_node.get_attribute_int("start", -1);
+ int end_char = body_node.get_attribute_int("end", -1);
+ if (start_char == -1 || end_char == -1) continue;
+ locations.add(new FallbackLocation(start_char, end_char));
+ }
+ if (locations.is_empty) continue;
+ ret.add(new Fallback(ns_uri, locations.to_array()));
+ }
+
+ return ret;
+ }
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0461_replies.vala b/xmpp-vala/src/module/xep/0461_replies.vala
new file mode 100644
index 00000000..870df295
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0461_replies.vala
@@ -0,0 +1,41 @@
+namespace Xmpp.Xep.Replies {
+
+ public const string NS_URI = "urn:xmpp:reply:0";
+
+ public class ReplyTo {
+ public Jid to_jid { get; set; }
+ public string to_message_id { get; set; }
+
+ public ReplyTo(Jid to_jid, string to_message_id) {
+ this.to_jid = to_jid;
+ this.to_message_id = to_message_id;
+ }
+ }
+
+ public static void set_reply_to(MessageStanza message, ReplyTo reply_to) {
+ StanzaNode reply_node = (new StanzaNode.build("reply", NS_URI))
+ .add_self_xmlns()
+ .put_attribute("to", reply_to.to_jid.to_string())
+ .put_attribute("id", reply_to.to_message_id);
+ message.stanza.put_node(reply_node);
+ }
+
+ public ReplyTo? get_reply_to(MessageStanza message) {
+ StanzaNode? reply_node = message.stanza.get_subnode("reply", NS_URI);
+ if (reply_node == null) return null;
+
+ string? to_str = reply_node.get_attribute("to");
+ if (to_str == null) return null;
+ try {
+ Jid to_jid = new Jid(to_str);
+
+ string? id = reply_node.get_attribute("id");
+ if (id == null) return null;
+
+ return new ReplyTo(to_jid, id);
+ } catch (InvalidJidError e) {
+ return null;
+ }
+ return null;
+ }
+} \ No newline at end of file