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 | |
| parent | 2956401e48b4ff62cb2d9960ff67a51802e092b3 (diff) | |
xmpp-vala: Add basic BOB support
| -rw-r--r-- | libdino/src/service/module_manager.vala | 1 | ||||
| -rw-r--r-- | xmpp-vala/meson.build | 1 | ||||
| -rw-r--r-- | xmpp-vala/src/module/xep/0231_bits_of_binary.vala | 76 |
3 files changed, 78 insertions, 0 deletions
diff --git a/libdino/src/service/module_manager.vala b/libdino/src/service/module_manager.vala index eb9e4fbc..4fcfb048 100644 --- a/libdino/src/service/module_manager.vala +++ b/libdino/src/service/module_manager.vala @@ -59,6 +59,7 @@ public class ModuleManager { module_map[account].add(new Xmpp.MessageModule()); module_map[account].add(new Xmpp.MessageArchiveManagement.Module()); module_map[account].add(new Xep.MessageCarbons.Module()); + module_map[account].add(new Xep.BitsOfBinary.Module()); module_map[account].add(new Xep.Muc.Module()); module_map[account].add(new Xep.Pubsub.Module()); module_map[account].add(new Xep.MessageDeliveryReceipts.Module()); diff --git a/xmpp-vala/meson.build b/xmpp-vala/meson.build index 16912f05..4ce5a8e5 100644 --- a/xmpp-vala/meson.build +++ b/xmpp-vala/meson.build @@ -95,6 +95,7 @@ sources = files( 'src/module/xep/0199_ping.vala', 'src/module/xep/0203_delayed_delivery.vala', 'src/module/xep/0215_external_service_discovery.vala', + 'src/module/xep/0231_bits_of_binary.vala', 'src/module/xep/0234_jingle_file_transfer.vala', 'src/module/xep/0249_direct_muc_invitations.vala', 'src/module/xep/0260_jingle_socks5_bytestreams.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<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 |
