From 5eabd7ac3805c6dde2ba2c810b73fca14d4fb130 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sat, 14 Jun 2025 16:59:41 +0200 Subject: xmpp-vala: Add basic BOB support --- xmpp-vala/src/module/xep/0231_bits_of_binary.vala | 76 +++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 xmpp-vala/src/module/xep/0231_bits_of_binary.vala (limited to 'xmpp-vala/src/module/xep/0231_bits_of_binary.vala') diff --git a/xmpp-vala/src/module/xep/0231_bits_of_binary.vala b/xmpp-vala/src/module/xep/0231_bits_of_binary.vala new file mode 100644 index 00000000..cd756b5e --- /dev/null +++ b/xmpp-vala/src/module/xep/0231_bits_of_binary.vala @@ -0,0 +1,76 @@ +using Gee; + +namespace Xmpp.Xep.BitsOfBinary { + + public const string NS_URI = "urn:xmpp:bob"; + + public static HashMap known_bobs = null; + + public Bytes? get_data_for_uri(string uri) { + if (uri.has_suffix("@bob.xmpp.org")) { + string cid = uri.replace("cid:", ""); + return BitsOfBinary.known_bobs[cid]; + } else if (uri.has_prefix("data:image/png;base64,")) { + return new Bytes.take(Base64.decode(uri.replace("data:image/png;base64,", ""))); + } + return null; + } + + public class Module : XmppStreamModule { + public static ModuleIdentity IDENTITY = new ModuleIdentity(NS_URI, "bits_of_binary"); + + private ReceivedPipelineListener received_pipeline_listener = new ReceivedPipelineListener(); + + public override void attach(XmppStream stream) { + known_bobs = new HashMap(); + var message_module = stream.get_module(MessageModule.IDENTITY); + if (message_module != null) { + message_module.received_pipeline.connect(received_pipeline_listener); + } + stream.received_iq_stanza.connect(on_received_iq_stanza); + } + + public override void detach(XmppStream stream) { + var message_module = stream.get_module(MessageModule.IDENTITY); + if (message_module != null) { + message_module.received_pipeline.disconnect(received_pipeline_listener); + } + + stream.received_iq_stanza.disconnect(on_received_iq_stanza); + } + + private async void on_received_iq_stanza(XmppStream stream, StanzaNode node) { + if (node.sub_nodes == null || node.sub_nodes.size == 0) return; + Gee.List data_nodes = node.sub_nodes[0].get_subnodes("data", NS_URI); + foreach (var data_node in data_nodes) { + string cid = data_node.get_attribute("cid", NS_URI); + if (cid == null) continue; + + known_bobs[cid] = new Bytes.take(Base64.decode(data_node.get_string_content())); + } + } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return IDENTITY.id; } + } + + public class ReceivedPipelineListener : StanzaListener { + + private const string[] after_actions_const = {}; + + public override string action_group { get { return ""; } } + public override string[] after_actions { get { return after_actions_const; } } + + public override async bool run(XmppStream stream, MessageStanza message) { + Gee.List data_nodes = message.stanza.get_subnodes("data", NS_URI); + foreach (var data_node in data_nodes) { + string cid = data_node.get_attribute("cid", NS_URI); + if (cid == null) continue; + + known_bobs[cid] = new Bytes.take(Base64.decode(data_node.get_string_content())); + } + + return false; + } + } +} \ No newline at end of file -- cgit v1.3