diff options
| author | fiaxh <git@lightrise.org> | 2025-06-14 16:59:41 +0200 |
|---|---|---|
| committer | fiaxh <git@lightrise.org> | 2025-06-14 17:00:24 +0200 |
| commit | 5eabd7ac3805c6dde2ba2c810b73fca14d4fb130 (patch) | |
| tree | f4eac272f92d3d23584d6ea0b14e9d163d4a117e /xmpp-vala/src/module/xep/0231_bits_of_binary.vala | |
| parent | 2956401e48b4ff62cb2d9960ff67a51802e092b3 (diff) | |
xmpp-vala: Add basic BOB support
Diffstat (limited to 'xmpp-vala/src/module/xep/0231_bits_of_binary.vala')
| -rw-r--r-- | xmpp-vala/src/module/xep/0231_bits_of_binary.vala | 76 |
1 files changed, 76 insertions, 0 deletions
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<string, Bytes> 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<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "bits_of_binary"); + + private ReceivedPipelineListener received_pipeline_listener = new ReceivedPipelineListener(); + + public override void attach(XmppStream stream) { + known_bobs = new HashMap<string, Bytes>(); + 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<StanzaNode> 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<MessageStanza> { + + 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<StanzaNode> 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 |
