diff options
author | fiaxh <git@lightrise.org> | 2020-04-21 16:25:21 +0200 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2020-04-21 16:25:21 +0200 |
commit | 7c4260eed718961874fc0ea665263ea2ce59338b (patch) | |
tree | 7d583974ebf797e7e827836176c87735a87c39f6 /xmpp-vala/src/module/xep/0030_service_discovery | |
parent | 3a8fef7b7b3c101c4bcb8cd3d7460a009c98a41e (diff) | |
download | dino-7c4260eed718961874fc0ea665263ea2ce59338b.tar.gz dino-7c4260eed718961874fc0ea665263ea2ce59338b.zip |
Remove features from service discovery module when detaching module from stream
fixes #179
fixes #812
Diffstat (limited to 'xmpp-vala/src/module/xep/0030_service_discovery')
4 files changed, 70 insertions, 15 deletions
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<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "service_discovery"); private HashMap<Jid, Gee.List<string>?> entity_features = new HashMap<Jid, Gee.List<string>?>(Jid.hash_func, Jid.equals_func); - private HashMap<Jid, Gee.List<Identity>?> entity_identities = new HashMap<Jid, Gee.List<Identity>?>(Jid.hash_func, Jid.equals_func); + private HashMap<Jid, Gee.Set<Identity>?> entity_identities = new HashMap<Jid, Gee.Set<Identity>?>(Jid.hash_func, Jid.equals_func); private HashMap<Jid, Gee.List<Item>?> entity_items = new HashMap<Jid, Gee.List<Item>?>(Jid.hash_func, Jid.equals_func); - public Gee.List<string> features = new ArrayList<string>(); - public Gee.List<Identity>? get_entity_categories(Jid jid) { + private Gee.Set<string> own_features_ = new HashSet<string>(); + public Gee.List<string> own_features { + owned get { + var ret = new ArrayList<string>(); + foreach (var feature in own_features_) ret.add(feature); + return ret; + } + } + + private Gee.Set<Identity> own_identities_ = new HashSet<Identity>(Identity.hash_func, Identity.equals_func); + public Gee.Set<Identity> own_identities { + owned get { return own_identities_.read_only_view; } + } + + public Gee.Set<Identity>? 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<Identity>? identities) { + public void set_entity_identities(Jid jid, Gee.Set<Identity>? 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<Identity> identities { + public Gee.Set<Identity> identities { owned get { - ArrayList<Identity> ret = new ArrayList<Identity>(); + HashSet<Identity> ret = new HashSet<Identity>(); 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<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0030_service_discovery_module"); - public ArrayList<Identity> identities = new ArrayList<Identity>(); + 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<Identity>? 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<Identity>? identities); public void get_entity_categories(XmppStream stream, Jid jid, owned HasEntryCategoryRes listener) { - Gee.List<Identity>? res = stream.get_flag(Flag.IDENTITY).get_entity_categories(jid); + Gee.Set<Identity>? 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); } } |