diff options
author | fiaxh <git@mx.ax.lt> | 2017-03-10 17:01:45 +0100 |
---|---|---|
committer | fiaxh <git@mx.ax.lt> | 2017-03-10 17:11:25 +0100 |
commit | 2fe8489d368a371aefbfbe66e74621a8df14cdc2 (patch) | |
tree | 4dec90236b28101383753ffe4a1c7a34b09b0208 /xmpp-vala/src/module/xep/0030_service_discovery | |
parent | 7a1aa8c806a63cfd031c082524501e26d4a181ee (diff) | |
download | dino-2fe8489d368a371aefbfbe66e74621a8df14cdc2.tar.gz dino-2fe8489d368a371aefbfbe66e74621a8df14cdc2.zip |
Rename vala-xmpp library to xmpp-vala
Diffstat (limited to 'xmpp-vala/src/module/xep/0030_service_discovery')
4 files changed, 275 insertions, 0 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 new file mode 100644 index 00000000..5be9f2eb --- /dev/null +++ b/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala @@ -0,0 +1,33 @@ +using Gee; + +using Xmpp.Core; + +namespace Xmpp.Xep.ServiceDiscovery { + +public class Flag : XmppStreamFlag { + public const string ID = "service_discovery"; + + private HashMap<string, ArrayList<string>> entity_features = new HashMap<string, ArrayList<string>>(); + public ArrayList<string> features = new ArrayList<string>(); + + public bool? has_entity_feature(string jid, string feature) { + if (!entity_features.has_key(jid)) return null; + return entity_features[jid].contains(feature); + } + + public void set_entitiy_features(string jid, ArrayList<string> features) { + entity_features[jid] = features; + } + + public void add_own_feature(string feature) { features.add(feature); } + + public static Flag? get_flag(XmppStream stream) { return (Flag?) stream.get_flag(NS_URI, ID); } + + public static bool has_flag(XmppStream stream) { return get_flag(stream) != null; } + + public override string get_ns() { return NS_URI; } + + public override string get_id() { return ID; } +} + +}
\ 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 new file mode 100644 index 00000000..7e0f0ea4 --- /dev/null +++ b/xmpp-vala/src/module/xep/0030_service_discovery/info_result.vala @@ -0,0 +1,78 @@ +using Gee; + +using Xmpp.Core; + +namespace Xmpp.Xep.ServiceDiscovery { + +public class InfoResult { + public Iq.Stanza iq { get; private set; } + + public ArrayList<string> features { + owned get { + ArrayList<string> ret = new ArrayList<string>(); + foreach (StanzaNode feature_node in iq.stanza.get_subnode("query", NS_URI_INFO).get_subnodes("feature", NS_URI_INFO)) { + ret.add(feature_node.get_attribute("var", NS_URI_INFO)); + } + return ret; + } + set { + foreach (string feature in value) { + add_feature(feature); + } + } + } + + public ArrayList<Identity> identities { + owned get { + ArrayList<Identity> ret = new ArrayList<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), + feature_node.get_attribute("name", NS_URI_INFO))); + } + return ret; + } + set { + foreach (Identity identity in value) { + add_identity(identity); + } + } + } + + 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()); + } + + 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); + if (query_node == null) return null; + StanzaNode feature_node = query_node.get_subnode("feature", NS_URI_INFO); + if (feature_node == null) return null; + StanzaNode identity_node = query_node.get_subnode("identity", NS_URI_INFO); + if (identity_node == null) return null; + return new ServiceDiscovery.InfoResult.from_iq(iq); + } +} + +}
\ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0030_service_discovery/items_result.vala b/xmpp-vala/src/module/xep/0030_service_discovery/items_result.vala new file mode 100644 index 00000000..2c29c320 --- /dev/null +++ b/xmpp-vala/src/module/xep/0030_service_discovery/items_result.vala @@ -0,0 +1,27 @@ +using Gee; + +using Xmpp.Core; + +namespace Xmpp.Xep.ServiceDiscovery { + +public class ItemsResult { + public Iq.Stanza iq { get; private set; } + + public ArrayList<Item> items { + owned get { + ArrayList<Item> ret = new ArrayList<Item>(); + foreach (StanzaNode feature_node in iq.stanza.get_subnode("query", NS_URI_ITEMS).get_subnodes("identity", NS_URI_INFO)) { + ret.add(new Item(feature_node.get_attribute("jid", NS_URI_ITEMS), + feature_node.get_attribute("name", NS_URI_ITEMS), + feature_node.get_attribute("node", NS_URI_ITEMS))); + } + return ret; + } + } + + public ItemsResult.from_iq(Iq.Stanza iq) { + this.iq = iq; + } +} + +}
\ 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 new file mode 100644 index 00000000..109da897 --- /dev/null +++ b/xmpp-vala/src/module/xep/0030_service_discovery/module.vala @@ -0,0 +1,137 @@ +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 const string ID = "0030_service_discovery_module"; + + public ArrayList<Identity> identities = new ArrayList<Identity>(); + + public Module.with_identity(string category, string type, string? name = null) { + add_identity(category, type, name); + } + + public void add_feature(XmppStream stream, string feature) { + Flag.get_flag(stream).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)); + } + + public void request_info(XmppStream stream, string jid, InfoResponseListener response_listener) { + Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("query", NS_URI_INFO).add_self_xmlns()); + iq.to = jid; + Iq.Module.get_module(stream).send_iq(stream, iq, new IqInfoResponseListener(response_listener)); + } + + private class IqInfoResponseListener : Iq.ResponseListener, Object { + InfoResponseListener response_listener; + public IqInfoResponseListener(InfoResponseListener response_listener) { + this.response_listener = response_listener; + } + public void on_result(XmppStream stream, Iq.Stanza iq) { + InfoResult? result = InfoResult.create_from_iq(iq); + if (result != null) { + Flag.get_flag(stream).set_entitiy_features(iq.from, result.features); + response_listener.on_result(stream, result); + } else { + response_listener.on_error(stream, iq); + } + } + } + + public void request_items(XmppStream stream, string jid, ItemsResponseListener response_listener) { + Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("query", NS_URI_ITEMS).add_self_xmlns()); + iq.to = jid; + Iq.Module.get_module(stream).send_iq(stream, iq, new IqItemsResponseListener(response_listener)); + } + + private class IqItemsResponseListener : Iq.ResponseListener, Object { + ItemsResponseListener response_listener; + public IqItemsResponseListener(ItemsResponseListener response_listener) { this.response_listener = response_listener; } + public void on_result(XmppStream stream, Iq.Stanza iq) { + //response_listener.on_result(stream, new ServiceDiscoveryItemsResult.from_iq(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 void on_iq_set(XmppStream stream, Iq.Stanza iq) { } + + public override void attach(XmppStream stream) { + Iq.Module.require(stream); + Iq.Module.get_module(stream).register_for_namespace(NS_URI_INFO, this); + stream.add_flag(new Flag()); + add_feature(stream, NS_URI_INFO); + } + + public override void detach(XmppStream stream) { } + + public static Module? get_module(XmppStream stream) { + return (Module?) stream.get_module(NS_URI, ID); + } + + public static void require(XmppStream stream) { + if (get_module(stream) == null) stream.add_module(new ServiceDiscovery.Module()); + } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return ID; } + + private void send_query_result(XmppStream stream, Iq.Stanza iq_request) { + InfoResult query_result = new ServiceDiscovery.InfoResult(iq_request); + query_result.features = Flag.get_flag(stream).features; + query_result.identities = identities; + Iq.Module.get_module(stream).send_iq(stream, query_result.iq, null); + } + } + + public class Identity { + 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; + } + } + + 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; + } + } + + public interface InfoResponseListener : Object { + public abstract void on_result(XmppStream stream, InfoResult query_result); + public void on_error(XmppStream stream, Iq.Stanza iq) { } + } + + public interface ItemsResponseListener : Object { + public abstract void on_result(XmppStream stream, ItemsResult query_result); + public void on_error(XmppStream stream, Iq.Stanza iq) { } + } +} |