diff options
author | fiaxh <git@mx.ax.lt> | 2017-03-31 01:17:01 +0200 |
---|---|---|
committer | fiaxh <git@mx.ax.lt> | 2017-03-31 16:39:28 +0200 |
commit | 22adbd38dca0868f0e10754314a3859bba0a7d87 (patch) | |
tree | 44ecf9cf8af6aef78030dade95aecb1de2b99873 /xmpp-vala/src/module/xep/0030_service_discovery | |
parent | 7d2f995a097086be01426cc79c9c801dabaf9e3b (diff) | |
download | dino-22adbd38dca0868f0e10754314a3859bba0a7d87.tar.gz dino-22adbd38dca0868f0e10754314a3859bba0a7d87.zip |
Handle MUC private messages
Diffstat (limited to 'xmpp-vala/src/module/xep/0030_service_discovery')
5 files changed, 141 insertions, 104 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 0f82f498..7c49fc30 100644 --- a/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala +++ b/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala @@ -7,15 +7,34 @@ namespace Xmpp.Xep.ServiceDiscovery { public class Flag : XmppStreamFlag { public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "service_discovery"); - private HashMap<string, ArrayList<string>> entity_features = new HashMap<string, ArrayList<string>>(); + private HashMap<string, ArrayList<string>?> entity_features = new HashMap<string, ArrayList<string>?>(); + private HashMap<string, ArrayList<Identity>?> entity_identities = new HashMap<string, ArrayList<Identity>?>(); public ArrayList<string> features = new ArrayList<string>(); + public ArrayList<Identity>? get_entity_categories(string jid) { + return entity_identities.has_key(jid) ? entity_identities[jid] : null; // TODO isnt this default for hashmap + } + + public bool? has_entity_identity(string jid, string category, string type) { + if (!entity_identities.has_key(jid)) return null; + if (entity_identities[jid] == null) return false; + foreach (Identity identity in entity_identities[jid]) { + if (identity.category == category && identity.type_ == type) return true; + } + return false; + } + public bool? has_entity_feature(string jid, string feature) { if (!entity_features.has_key(jid)) return null; + if (entity_features[jid] == null) return false; return entity_features[jid].contains(feature); } - public void set_entitiy_features(string jid, ArrayList<string> features) { + public void set_entity_identities(string jid, ArrayList<Identity>? identities) { + entity_identities[jid] = identities; + } + + public void set_entity_features(string jid, ArrayList<string>? features) { entity_features[jid] = features; } diff --git a/xmpp-vala/src/module/xep/0030_service_discovery/identity.vala b/xmpp-vala/src/module/xep/0030_service_discovery/identity.vala new file mode 100644 index 00000000..e29eb111 --- /dev/null +++ b/xmpp-vala/src/module/xep/0030_service_discovery/identity.vala @@ -0,0 +1,18 @@ +namespace Xmpp.Xep.ServiceDiscovery { + +public class Identity { + public const string CATEGORY_CLIENT = "client"; + public const string CATEGORY_CONFERENCE = "conference"; + + public string category { get; set; } + public string type_ { get; set; } + public string? name { get; set; } + + public Identity(string category, string type, string? name = null) { + this.category = category; + this.type_ = type; + this.name = name; + } +} + +}
\ No newline at end of file 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 7e0f0ea4..ae6b9caf 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 @@ -39,30 +39,16 @@ public class InfoResult { } } - public void add_feature(string feature) { - iq.stanza.get_subnode("query", NS_URI_INFO).put_node(new StanzaNode.build("feature", NS_URI_INFO).put_attribute("var", feature)); - } - - public void add_identity(Identity identity) { - StanzaNode identity_node = new StanzaNode.build("identity", NS_URI_INFO) - .put_attribute("category", identity.category) - .put_attribute("type", identity.type_); - if (identity.name != null) { - identity_node.put_attribute("name", identity.name); - } - iq.stanza.get_subnode("query", NS_URI_INFO).put_node(identity_node); - } - - private InfoResult.from_iq(Iq.Stanza iq) { - this.iq = iq; - } - public InfoResult(Iq.Stanza iq_request) { iq = new Iq.Stanza.result(iq_request); iq.to = iq_request.from; iq.stanza.put_node(new StanzaNode.build("query", NS_URI_INFO).add_self_xmlns()); } + private InfoResult.from_iq(Iq.Stanza iq) { + this.iq = iq; + } + public static InfoResult? create_from_iq(Iq.Stanza iq) { if (iq.is_error()) return null; StanzaNode query_node = iq.stanza.get_subnode("query", NS_URI_INFO); @@ -73,6 +59,20 @@ public class InfoResult { if (identity_node == null) return null; return new ServiceDiscovery.InfoResult.from_iq(iq); } + + public void add_feature(string feature) { + iq.stanza.get_subnode("query", NS_URI_INFO).put_node(new StanzaNode.build("feature", NS_URI_INFO).put_attribute("var", feature)); + } + + public void add_identity(Identity identity) { + StanzaNode identity_node = new StanzaNode.build("identity", NS_URI_INFO) + .put_attribute("category", identity.category) + .put_attribute("type", identity.type_); + if (identity.name != null) { + identity_node.put_attribute("name", identity.name); + } + iq.stanza.get_subnode("query", NS_URI_INFO).put_node(identity_node); + } } }
\ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0030_service_discovery/item.vala b/xmpp-vala/src/module/xep/0030_service_discovery/item.vala new file mode 100644 index 00000000..9d8f1f3e --- /dev/null +++ b/xmpp-vala/src/module/xep/0030_service_discovery/item.vala @@ -0,0 +1,15 @@ +namespace Xmpp.Xep.ServiceDiscovery { + +public class Item { + public string jid; + public string? name; + public string? node; + + public Item(string jid, string? name = null, string? node = null) { + this.jid = jid; + this.name = name; + this.node = node; + } +} + +}
\ No newline at end of file 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 4b51e230..2ec0f630 100644 --- a/xmpp-vala/src/module/xep/0030_service_discovery/module.vala +++ b/xmpp-vala/src/module/xep/0030_service_discovery/module.vala @@ -3,109 +3,94 @@ using Gee; using Xmpp.Core; namespace Xmpp.Xep.ServiceDiscovery { - private const string NS_URI = "http://jabber.org/protocol/disco"; - public const string NS_URI_INFO = NS_URI + "#info"; - 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"); +private const string NS_URI = "http://jabber.org/protocol/disco"; +public const string NS_URI_INFO = NS_URI + "#info"; +public const string NS_URI_ITEMS = NS_URI + "#items"; - public ArrayList<Identity> identities = new ArrayList<Identity>(); +public class Module : XmppStreamModule, Iq.Handler { + public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0030_service_discovery_module"); - public Module.with_identity(string category, string type, string? name = null) { - add_identity(category, type, name); - } - - public void add_feature(XmppStream stream, string feature) { - stream.get_flag(Flag.IDENTITY).add_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)); - } - - [CCode (has_target = false)] public delegate void OnInfoResult(XmppStream stream, InfoResult query_result, Object? store); - public void request_info(XmppStream stream, string jid, OnInfoResult listener, Object? store) { - Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("query", NS_URI_INFO).add_self_xmlns()); - iq.to = jid; - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, on_request_info_response, Tuple.create(listener, store)); - } - - [CCode (has_target = false)] public delegate void OnItemsResult(XmppStream stream, ItemsResult query_result); - public void request_items(XmppStream stream, string jid, OnItemsResult listener, Object? store) { - Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("query", NS_URI_ITEMS).add_self_xmlns()); - iq.to = jid; - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); - } - - public void on_iq_get(XmppStream stream, Iq.Stanza iq) { - StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI_INFO); - if (query_node != null) { - send_query_result(stream, iq); - } - } + public ArrayList<Identity> identities = new ArrayList<Identity>(); - public void on_iq_set(XmppStream stream, Iq.Stanza iq) { } + public Module.with_identity(string category, string type, string? name = null) { + add_identity(category, type, name); + } - public override void attach(XmppStream stream) { - Iq.Module.require(stream); - stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI_INFO, this); - stream.add_flag(new Flag()); - add_feature(stream, NS_URI_INFO); - } + public void add_feature(XmppStream stream, string feature) { + stream.get_flag(Flag.IDENTITY).add_own_feature(feature); + } - public override void detach(XmppStream stream) { } + public void add_feature_notify(XmppStream stream, string feature) { + add_feature(stream, feature + "+notify"); + } - public static void require(XmppStream stream) { - if (stream.get_module(IDENTITY) == null) stream.add_module(new ServiceDiscovery.Module()); - } + public void add_identity(string category, string type, string? name = null) { + identities.add(new Identity(category, type, name)); + } - public override string get_ns() { return NS_URI; } - public override string get_id() { return IDENTITY.id; } + [CCode (has_target = false)] public delegate void HasEntryCategoryRes(XmppStream stream, ArrayList<Identity>? identities, Object? store); + public void get_entity_categories(XmppStream stream, string jid, HasEntryCategoryRes on_result, Object? store) { + ArrayList<Identity>? res = stream.get_flag(Flag.IDENTITY).get_entity_categories(jid); + if (res != null) on_result(stream, res, store); + request_info(stream, jid, (stream, query_result, store) => { + Tuple<HasEntryCategoryRes, Object> tuple = store as Tuple<HasEntryCategoryRes, Object>; + tuple.a(stream, query_result != null ? query_result.identities : null, tuple.b); + }, Tuple.create(on_result, store)); + } - private static void on_request_info_response(XmppStream stream, Iq.Stanza iq, Object o) { + [CCode (has_target = false)] public delegate void OnInfoResult(XmppStream stream, InfoResult? query_result, Object? store); + public void request_info(XmppStream stream, string jid, OnInfoResult listener, Object? store) { + Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("query", NS_URI_INFO).add_self_xmlns()); + iq.to = jid; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq, o) => { Tuple<OnInfoResult, Object> tuple = o as Tuple<OnInfoResult, Object>; OnInfoResult on_result = tuple.a; InfoResult? result = InfoResult.create_from_iq(iq); - if (result != null) { - stream.get_flag(Flag.IDENTITY).set_entitiy_features(iq.from, result.features); - on_result(stream, result, tuple.b); - } - } + stream.get_flag(Flag.IDENTITY).set_entity_features(iq.from, result != null ? result.features : null); + stream.get_flag(Flag.IDENTITY).set_entity_identities(iq.from, result != null ? result.identities : null); + on_result(stream, result, tuple.b); + }, Tuple.create(listener, store)); + } + + [CCode (has_target = false)] public delegate void OnItemsResult(XmppStream stream, ItemsResult query_result); + public void request_items(XmppStream stream, string jid, OnItemsResult listener, Object? store) { + Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("query", NS_URI_ITEMS).add_self_xmlns()); + iq.to = jid; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); + } - 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; - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, query_result.iq); + public void on_iq_get(XmppStream stream, Iq.Stanza iq) { + StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI_INFO); + if (query_node != null) { + send_query_result(stream, iq); } } - public class Identity { - public string category { get; set; } - public string type_ { get; set; } - public string? name { get; set; } + public void on_iq_set(XmppStream stream, Iq.Stanza iq) { } - public Identity(string category, string type, string? name = null) { - this.category = category; - this.type_ = type; - this.name = name; - } + public override void attach(XmppStream stream) { + Iq.Module.require(stream); + stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI_INFO, this); + stream.add_flag(new Flag()); + add_feature(stream, NS_URI_INFO); } - public class Item { - public string jid; - public string? name; - public string? node; + public override void detach(XmppStream stream) { } - public Item(string jid, string? name = null, string? node = null) { - this.jid = jid; - this.name = name; - this.node = node; - } + public static void require(XmppStream stream) { + if (stream.get_module(IDENTITY) == null) stream.add_module(new ServiceDiscovery.Module()); } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return IDENTITY.id; } + + 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; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, query_result.iq); + } +} + } |