From bcb96909c9b53c2ca5287433f2fef103b0ddf317 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sun, 21 May 2017 23:30:30 +0200 Subject: Roster versioning --- xmpp-vala/CMakeLists.txt | 1 + xmpp-vala/src/module/roster/item.vala | 4 +- xmpp-vala/src/module/roster/module.vala | 173 +++++++++++---------- xmpp-vala/src/module/roster/versioning_module.vala | 75 +++++++++ 4 files changed, 165 insertions(+), 88 deletions(-) create mode 100644 xmpp-vala/src/module/roster/versioning_module.vala (limited to 'xmpp-vala') diff --git a/xmpp-vala/CMakeLists.txt b/xmpp-vala/CMakeLists.txt index b94d6d22..7f936fc2 100644 --- a/xmpp-vala/CMakeLists.txt +++ b/xmpp-vala/CMakeLists.txt @@ -26,6 +26,7 @@ SOURCES "src/module/roster/flag.vala" "src/module/roster/item.vala" "src/module/roster/module.vala" + "src/module/roster/versioning_module.vala" "src/module/sasl.vala" "src/module/stanza.vala" "src/module/stanza_error.vala" diff --git a/xmpp-vala/src/module/roster/item.vala b/xmpp-vala/src/module/roster/item.vala index 7ef76fd4..1e39fce2 100644 --- a/xmpp-vala/src/module/roster/item.vala +++ b/xmpp-vala/src/module/roster/item.vala @@ -25,12 +25,12 @@ public class Item { public string? name { get { return stanza_node.get_attribute(NODE_NAME); } - set { stanza_node.set_attribute(NODE_NAME, value); } + set { if (value != null) stanza_node.set_attribute(NODE_NAME, value); } } public string? subscription { get { return stanza_node.get_attribute(NODE_SUBSCRIPTION); } - set { stanza_node.set_attribute(NODE_SUBSCRIPTION, value); } + set { if (value != null) stanza_node.set_attribute(NODE_SUBSCRIPTION, value); } } public Item() { diff --git a/xmpp-vala/src/module/roster/module.vala b/xmpp-vala/src/module/roster/module.vala index 1ebb7f22..e0d8aeb3 100644 --- a/xmpp-vala/src/module/roster/module.vala +++ b/xmpp-vala/src/module/roster/module.vala @@ -3,117 +3,118 @@ using Gee; using Xmpp.Core; namespace Xmpp.Roster { - private const string NS_URI = "jabber:iq:roster"; - public class Module : XmppStreamModule, Iq.Handler { - public static ModuleIdentity IDENTITY = new ModuleIdentity(NS_URI, "roster_module"); +private const string NS_URI = "jabber:iq:roster"; - public signal void received_roster(XmppStream stream, Collection roster); - public signal void item_removed(XmppStream stream, Item roster_item); - public signal void item_updated(XmppStream stream, Item roster_item); +public class Module : XmppStreamModule, Iq.Handler { + public static ModuleIdentity IDENTITY = new ModuleIdentity(NS_URI, "roster_module"); - public bool interested_resource = true; + public signal void received_roster(XmppStream stream, Collection roster, Iq.Stanza stanza); + public signal void pre_get_roster(XmppStream stream, Iq.Stanza iq); + public signal void item_removed(XmppStream stream, Item item, Iq.Stanza iq); + public signal void item_updated(XmppStream stream, Item item, Iq.Stanza iq); - /** - * Add a jid to the roster - */ - public void add_jid(XmppStream stream, string jid, string? handle = null) { - Item roster_item = new Item(); - roster_item.jid = jid; - if (handle != null) { - roster_item.name = handle; - } - roster_set(stream, roster_item); + public bool interested_resource = true; + + public void add_jid(XmppStream stream, string jid, string? handle = null) { + Item roster_item = new Item(); + roster_item.jid = jid; + if (handle != null) { + roster_item.name = handle; } + roster_set(stream, roster_item); + } - /** - * Remove a jid from the roster - */ - public void remove_jid(XmppStream stream, string jid) { - Item roster_item = new Item(); - roster_item.jid = jid; - roster_item.subscription = Item.SUBSCRIPTION_REMOVE; + public void remove_jid(XmppStream stream, string jid) { + Item roster_item = new Item(); + roster_item.jid = jid; + roster_item.subscription = Item.SUBSCRIPTION_REMOVE; - roster_set(stream, roster_item); + roster_set(stream, roster_item); + } + + /** + * Set a handle for a jid + * @param handle Handle to be set. If null, any handle will be removed. + */ + public void set_jid_handle(XmppStream stream, string jid, string? handle) { + Item roster_item = new Item(); + roster_item.jid = jid; + if (handle != null) { + roster_item.name = handle; } - /** - * Set a handle for a jid - * @param handle Handle to be set. If null, any handle will be removed. - */ - public void set_jid_handle(XmppStream stream, string jid, string? handle) { - Item roster_item = new Item(); - roster_item.jid = jid; - if (handle != null) { - roster_item.name = handle; - } + roster_set(stream, roster_item); + } - roster_set(stream, roster_item); + public void on_iq_set(XmppStream stream, Iq.Stanza iq) { + StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI); + if (query_node == null) return; + + Flag flag = stream.get_flag(Flag.IDENTITY); + Item item = new Item.from_stanza_node(query_node.get_subnode("item", NS_URI)); + switch (item.subscription) { + case Item.SUBSCRIPTION_REMOVE: + flag.roster_items.unset(item.jid); + item_removed(stream, item, iq); + break; + default: + flag.roster_items[item.jid] = item; + item_updated(stream, item, iq); + break; } + } - public void on_iq_set(XmppStream stream, Iq.Stanza iq) { - StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI); - if (query_node == null) return; - - Flag flag = stream.get_flag(Flag.IDENTITY); - Item item = new Item.from_stanza_node(query_node.get_subnode("item", NS_URI)); - switch (item.subscription) { - case Item.SUBSCRIPTION_REMOVE: - flag.roster_items.unset(item.jid); - item_removed(stream, item); - break; - default: - flag.roster_items[item.jid] = item; - item_updated(stream, item); - break; - } - } + public void on_iq_get(XmppStream stream, Iq.Stanza iq) { } - public void on_iq_get(XmppStream stream, Iq.Stanza iq) { } + public static void require(XmppStream stream) { + if (stream.get_module(IDENTITY) == null) stream.add_module(new Module()); + } - public static void require(XmppStream stream) { - if (stream.get_module(IDENTITY) == null) stream.add_module(new Module()); - } + public override void attach(XmppStream stream) { + Iq.Module.require(stream); + stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI, this); + Presence.Module.require(stream); + stream.get_module(Presence.Module.IDENTITY).initial_presence_sent.connect(roster_get); + stream.add_flag(new Flag()); + } - public override void attach(XmppStream stream) { - Iq.Module.require(stream); - stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI, this); - Presence.Module.require(stream); - stream.get_module(Presence.Module.IDENTITY).initial_presence_sent.connect(roster_get); - stream.add_flag(new Flag()); - } + public override void detach(XmppStream stream) { + stream.get_module(Presence.Module.IDENTITY).initial_presence_sent.disconnect(roster_get); + } - public override void detach(XmppStream stream) { - stream.get_module(Presence.Module.IDENTITY).initial_presence_sent.disconnect(roster_get); - } + internal override string get_ns() { return NS_URI; } + internal override string get_id() { return IDENTITY.id; } - internal override string get_ns() { return NS_URI; } - internal override string get_id() { return IDENTITY.id; } + private void roster_get(XmppStream stream) { + stream.get_flag(Flag.IDENTITY).iq_id = random_uuid(); + StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns(); + Iq.Stanza iq = new Iq.Stanza.get(query_node, stream.get_flag(Flag.IDENTITY).iq_id); - private void roster_get(XmppStream stream) { - stream.get_flag(Flag.IDENTITY).iq_id = random_uuid(); - StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns(); - Iq.Stanza iq = new Iq.Stanza.get(query_node, stream.get_flag(Flag.IDENTITY).iq_id); - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, on_roster_get_received); - } + pre_get_roster(stream, iq); + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, on_roster_get_received); + } - private static void on_roster_get_received(XmppStream stream, Iq.Stanza iq) { - Flag flag = stream.get_flag(Flag.IDENTITY); - if (iq.id == flag.iq_id) { - StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI); + private static void on_roster_get_received(XmppStream stream, Iq.Stanza iq) { + Flag flag = stream.get_flag(Flag.IDENTITY); + if (iq.id == flag.iq_id) { + StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI); + if (query_node != null) { foreach (StanzaNode item_node in query_node.sub_nodes) { Item item = new Item.from_stanza_node(item_node); flag.roster_items[item.jid] = item; } - stream.get_module(Module.IDENTITY).received_roster(stream, flag.roster_items.values); } + stream.get_module(Module.IDENTITY).received_roster(stream, flag.roster_items.values, iq); } + } - private void roster_set(XmppStream stream, Item roster_item) { - StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns() - .put_node(roster_item.stanza_node); - Iq.Stanza iq = new Iq.Stanza.set(query_node); - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); - } + private void roster_set(XmppStream stream, Item roster_item) { + StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns() + .put_node(roster_item.stanza_node); + Iq.Stanza iq = new Iq.Stanza.set(query_node); + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); } } + +} diff --git a/xmpp-vala/src/module/roster/versioning_module.vala b/xmpp-vala/src/module/roster/versioning_module.vala new file mode 100644 index 00000000..fbdb6cef --- /dev/null +++ b/xmpp-vala/src/module/roster/versioning_module.vala @@ -0,0 +1,75 @@ +using Gee; + +using Xmpp.Core; + +namespace Xmpp.Roster { + +public class VersioningModule : XmppStreamModule { + private const string NS_URI_FEATURE = "urn:xmpp:features:rosterver"; + + public static ModuleIdentity IDENTITY = new ModuleIdentity(NS_URI, "roster_versioning"); + + private Storage storage; + + public VersioningModule(Storage storage) { + this.storage = storage; + } + + public override void attach(XmppStream stream) { + Module.require(stream); + stream.get_module(Module.IDENTITY).pre_get_roster.connect(on_pre_get_roster); + stream.get_module(Module.IDENTITY).received_roster.connect(on_received_roster); + stream.get_module(Module.IDENTITY).item_updated.connect(on_item_updated); + stream.get_module(Module.IDENTITY).item_removed.connect(on_item_removed); + } + + public override void detach(XmppStream stream) { + stream.get_module(Module.IDENTITY).pre_get_roster.disconnect(on_pre_get_roster); + } + + internal override string get_ns() { return NS_URI; } + internal override string get_id() { return IDENTITY.id; } + + private void on_pre_get_roster(XmppStream stream, Iq.Stanza iq) { + StanzaNode? ver_feature = stream.features.get_subnode("ver", NS_URI_FEATURE); + if (ver_feature != null) { + iq.stanza.get_subnode("query", NS_URI).set_attribute("ver", storage.get_roster_version() ?? ""); + } + } + + private void on_received_roster(XmppStream stream, Collection roster, Iq.Stanza iq) { + string? ver = iq.stanza.get_deep_attribute(NS_URI + ":query", NS_URI + ":ver"); + if (ver != null) storage.set_roster_version(ver); + if (iq.stanza.get_subnode("query", NS_URI) != null) { + storage.set_roster(roster); + } else { + Flag flag = stream.get_flag(Flag.IDENTITY); + foreach (Item item in storage.get_roster()) { + flag.roster_items[item.jid] = item; + } + } + } + + private void on_item_updated(XmppStream stream, Item item, Iq.Stanza iq) { + string? ver = iq.stanza.get_deep_attribute(NS_URI + ":query", NS_URI + ":ver"); + if (ver != null) storage.set_roster_version(ver); + storage.set_item(item); + } + + private void on_item_removed(XmppStream stream, Item item, Iq.Stanza iq) { + string? ver = iq.stanza.get_deep_attribute(NS_URI + ":query", NS_URI + ":ver"); + if (ver != null) storage.set_roster_version(ver); + storage.remove_item(item); + } +} + +public interface Storage : Object { + public abstract string? get_roster_version(); + public abstract Collection get_roster(); + public abstract void set_roster_version(string version); + public abstract void set_roster(Collection items); + public abstract void set_item(Roster.Item item); + public abstract void remove_item(Roster.Item item); +} + +} \ No newline at end of file -- cgit v1.2.3-70-g09d2