From 7c4260eed718961874fc0ea665263ea2ce59338b Mon Sep 17 00:00:00 2001 From: fiaxh Date: Tue, 21 Apr 2020 16:25:21 +0200 Subject: Remove features from service discovery module when detaching module from stream fixes #179 fixes #812 --- .../module/xep/0030_service_discovery/flag.vala | 36 +++++++++++++++++++--- .../xep/0030_service_discovery/identity.vala | 12 ++++++++ .../xep/0030_service_discovery/info_result.vala | 4 +-- .../module/xep/0030_service_discovery/module.vala | 33 +++++++++++++++----- 4 files changed, 70 insertions(+), 15 deletions(-) (limited to 'xmpp-vala/src/module/xep/0030_service_discovery') diff --git a/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala b/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala index 4661fede..d4874fca 100644 --- a/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala +++ b/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala @@ -6,11 +6,24 @@ public class Flag : XmppStreamFlag { public static FlagIdentity IDENTITY = new FlagIdentity(NS_URI, "service_discovery"); private HashMap?> entity_features = new HashMap?>(Jid.hash_func, Jid.equals_func); - private HashMap?> entity_identities = new HashMap?>(Jid.hash_func, Jid.equals_func); + private HashMap?> entity_identities = new HashMap?>(Jid.hash_func, Jid.equals_func); private HashMap?> entity_items = new HashMap?>(Jid.hash_func, Jid.equals_func); - public Gee.List features = new ArrayList(); - public Gee.List? get_entity_categories(Jid jid) { + private Gee.Set own_features_ = new HashSet(); + public Gee.List own_features { + owned get { + var ret = new ArrayList(); + foreach (var feature in own_features_) ret.add(feature); + return ret; + } + } + + private Gee.Set own_identities_ = new HashSet(Identity.hash_func, Identity.equals_func); + public Gee.Set own_identities { + owned get { return own_identities_.read_only_view; } + } + + public Gee.Set? get_entity_categories(Jid jid) { return entity_identities.has_key(jid) ? entity_identities[jid] : null; // TODO isn’t this default for hashmap } @@ -29,7 +42,7 @@ public class Flag : XmppStreamFlag { return entity_features[jid].contains(feature); } - public void set_entity_identities(Jid jid, Gee.List? identities) { + public void set_entity_identities(Jid jid, Gee.Set? identities) { entity_identities[jid] = identities; } @@ -41,7 +54,20 @@ public class Flag : XmppStreamFlag { entity_items[jid] = features; } - public void add_own_feature(string feature) { features.add(feature); } + public void add_own_feature(string feature) { + if (own_features_.contains(feature)) { + warning("Tried to add the feature %s a second time".printf(feature)); + return; + } + own_features_.add(feature); + } + + public void remove_own_feature(string feature) { + own_features_.remove(feature); + } + + public void add_own_identity(Identity identity) { own_identities_.add(identity); } + public void remove_own_identity(Identity identity) { own_identities_.remove(identity); } public override string get_ns() { return NS_URI; } diff --git a/xmpp-vala/src/module/xep/0030_service_discovery/identity.vala b/xmpp-vala/src/module/xep/0030_service_discovery/identity.vala index 43aecfb2..aec2ed24 100644 --- a/xmpp-vala/src/module/xep/0030_service_discovery/identity.vala +++ b/xmpp-vala/src/module/xep/0030_service_discovery/identity.vala @@ -17,6 +17,18 @@ public class Identity { this.type_ = type; this.name = name; } + + public static uint hash_func(Identity a) { + uint hash = a.category.hash() ^ a.type_.hash(); + if (a.name != null) { + hash ^= a.name.hash(); + } + return hash; + } + + public static bool equals_func(Identity a, Identity b) { + return a.category == b.category && a.type_ == b.type_ && a.name == b.name; + } } } diff --git a/xmpp-vala/src/module/xep/0030_service_discovery/info_result.vala b/xmpp-vala/src/module/xep/0030_service_discovery/info_result.vala index 65252261..a9606115 100644 --- a/xmpp-vala/src/module/xep/0030_service_discovery/info_result.vala +++ b/xmpp-vala/src/module/xep/0030_service_discovery/info_result.vala @@ -20,9 +20,9 @@ public class InfoResult { } } - public Gee.List identities { + public Gee.Set identities { owned get { - ArrayList ret = new ArrayList(); + HashSet ret = new HashSet(); foreach (StanzaNode feature_node in iq.stanza.get_subnode("query", NS_URI_INFO).get_subnodes("identity", NS_URI_INFO)) { ret.add(new Identity(feature_node.get_attribute("category", NS_URI_INFO), feature_node.get_attribute("type", NS_URI_INFO), diff --git a/xmpp-vala/src/module/xep/0030_service_discovery/module.vala b/xmpp-vala/src/module/xep/0030_service_discovery/module.vala index e2bb46f8..332e8fc3 100644 --- a/xmpp-vala/src/module/xep/0030_service_discovery/module.vala +++ b/xmpp-vala/src/module/xep/0030_service_discovery/module.vala @@ -9,27 +9,39 @@ public const string NS_URI_ITEMS = NS_URI + "#items"; public class Module : XmppStreamModule, Iq.Handler { public static ModuleIdentity IDENTITY = new ModuleIdentity(NS_URI, "0030_service_discovery_module"); - public ArrayList identities = new ArrayList(); + public Identity own_identity; public Module.with_identity(string category, string type, string? name = null) { - add_identity(category, type, name); + own_identity = new Identity(category, type, name); } public void add_feature(XmppStream stream, string feature) { stream.get_flag(Flag.IDENTITY).add_own_feature(feature); } + public void remove_feature(XmppStream stream, string feature) { + stream.get_flag(Flag.IDENTITY).remove_own_feature(feature); + } + public void add_feature_notify(XmppStream stream, string feature) { add_feature(stream, feature + "+notify"); } - public void add_identity(string category, string type, string? name = null) { - identities.add(new Identity(category, type, name)); + public void remove_feature_notify(XmppStream stream, string feature) { + remove_feature(stream, feature + "+notify"); + } + + public void add_identity(XmppStream stream, Identity identity) { + stream.get_flag(Flag.IDENTITY).add_own_identity(identity); } - public delegate void HasEntryCategoryRes(XmppStream stream, Gee.List? identities); + public void remove_identity(XmppStream stream, Identity identity) { + stream.get_flag(Flag.IDENTITY).remove_own_identity(identity); + } + + public delegate void HasEntryCategoryRes(XmppStream stream, Gee.Set? identities); public void get_entity_categories(XmppStream stream, Jid jid, owned HasEntryCategoryRes listener) { - Gee.List? res = stream.get_flag(Flag.IDENTITY).get_entity_categories(jid); + Gee.Set? res = stream.get_flag(Flag.IDENTITY).get_entity_categories(jid); if (res != null) listener(stream, res); request_info(stream, jid, (stream, query_result) => { listener(stream, query_result != null ? query_result.identities : null); @@ -68,12 +80,17 @@ public class Module : XmppStreamModule, Iq.Handler { public override void attach(XmppStream stream) { stream.add_flag(new Flag()); + add_identity(stream, own_identity); + stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI_INFO, this); add_feature(stream, NS_URI_INFO); } public override void detach(XmppStream stream) { + remove_identity(stream, own_identity); + stream.get_module(Iq.Module.IDENTITY).unregister_from_namespace(NS_URI_INFO, this); + remove_feature(stream, NS_URI_INFO); } public static void require(XmppStream stream) { @@ -85,8 +102,8 @@ public class Module : XmppStreamModule, Iq.Handler { private void send_query_result(XmppStream stream, Iq.Stanza iq_request) { InfoResult query_result = new ServiceDiscovery.InfoResult(iq_request); - query_result.features = stream.get_flag(Flag.IDENTITY).features; - query_result.identities = identities; + query_result.features = stream.get_flag(Flag.IDENTITY).own_features; + query_result.identities = stream.get_flag(Flag.IDENTITY).own_identities; stream.get_module(Iq.Module.IDENTITY).send_iq(stream, query_result.iq); } } -- cgit v1.2.3-54-g00ecf