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/iq | |
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/iq')
-rw-r--r-- | xmpp-vala/src/module/iq/module.vala | 89 | ||||
-rw-r--r-- | xmpp-vala/src/module/iq/stanza.vala | 51 |
2 files changed, 140 insertions, 0 deletions
diff --git a/xmpp-vala/src/module/iq/module.vala b/xmpp-vala/src/module/iq/module.vala new file mode 100644 index 00000000..b5c50bd7 --- /dev/null +++ b/xmpp-vala/src/module/iq/module.vala @@ -0,0 +1,89 @@ +using Gee; + +using Xmpp.Core; + +namespace Xmpp.Iq { + private const string NS_URI = "jabber:client"; + + public class Module : XmppStreamNegotiationModule { + public const string ID = "iq_module"; + + private HashMap<string, ResponseListener> responseListeners = new HashMap<string, ResponseListener>(); + private HashMap<string, ArrayList<Handler>> namespaceRegistrants = new HashMap<string, ArrayList<Handler>>(); + + public void send_iq(XmppStream stream, Iq.Stanza iq, ResponseListener? listener = null) { + stream.write(iq.stanza); + if (listener != null) { + responseListeners.set(iq.id, listener); + } + } + + public void register_for_namespace(string namespace, Handler module) { + if (!namespaceRegistrants.has_key(namespace)) { + namespaceRegistrants.set(namespace, new ArrayList<Handler>()); + } + namespaceRegistrants[namespace].add(module); + } + + public override void attach(XmppStream stream) { + stream.received_iq_stanza.connect(on_received_iq_stanza); + } + + public override void detach(XmppStream stream) { + stream.received_iq_stanza.disconnect(on_received_iq_stanza); + } + + 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 Iq.Module()); + } + + public override bool mandatory_outstanding(XmppStream stream) { return false; } + + public override bool negotiation_active(XmppStream stream) { return false; } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return ID; } + + private void on_received_iq_stanza(XmppStream stream, StanzaNode node) { + Iq.Stanza iq = new Iq.Stanza.from_stanza(node, Bind.Flag.has_flag(stream) ? Bind.Flag.get_flag(stream).my_jid : null); + + if (iq.type_ == Iq.Stanza.TYPE_RESULT || iq.is_error()) { + if (responseListeners.has_key(iq.id)) { + ResponseListener? listener = responseListeners.get(iq.id); + if (listener != null) { + listener.on_result(stream, iq); + } + responseListeners.unset(iq.id); + } + } else { + ArrayList<StanzaNode> children = node.get_all_subnodes(); + if (children.size == 1 && namespaceRegistrants.has_key(children[0].ns_uri)) { + ArrayList<Handler> handlers = namespaceRegistrants[children[0].ns_uri]; + foreach (Handler handler in handlers) { + if (iq.type_ == Iq.Stanza.TYPE_GET) { + handler.on_iq_get(stream, iq); + } else if (iq.type_ == Iq.Stanza.TYPE_SET) { + handler.on_iq_set(stream, iq); + } + } + } else { + Iq.Stanza unaviable_error = new Iq.Stanza.error(iq, new StanzaNode.build("service-unaviable", "urn:ietf:params:xml:ns:xmpp-stanzas").add_self_xmlns()); + send_iq(stream, unaviable_error); + } + } + } + } + + public interface Handler : Object { + public abstract void on_iq_get(XmppStream stream, Iq.Stanza iq); + public abstract void on_iq_set(XmppStream stream, Iq.Stanza iq); + } + + public interface ResponseListener : Object { + public abstract void on_result(XmppStream stream, Iq.Stanza iq); + } +} diff --git a/xmpp-vala/src/module/iq/stanza.vala b/xmpp-vala/src/module/iq/stanza.vala new file mode 100644 index 00000000..561c5866 --- /dev/null +++ b/xmpp-vala/src/module/iq/stanza.vala @@ -0,0 +1,51 @@ +using Gee; + +using Xmpp.Core; + +namespace Xmpp.Iq { + +public class Stanza : Xmpp.Stanza { + + public const string TYPE_GET = "get"; + public const string TYPE_RESULT = "result"; + public const string TYPE_SET = "set"; + + private Stanza(string? id = null) { + base.outgoing(new StanzaNode.build("iq")); + this.id = id ?? random_uuid(); + } + + public Stanza.get(StanzaNode stanza_node, string? id = null) { + this(id); + this.type_ = TYPE_GET; + stanza.put_node(stanza_node); + } + + public Stanza.result(Stanza request, StanzaNode? stanza_node = null) { + this(request.id); + this.type_ = TYPE_RESULT; + if (stanza_node != null) { + stanza.put_node(stanza_node); + } + } + + public Stanza.set(StanzaNode stanza_node, string? id = null) { + this(id); + type_ = TYPE_SET; + stanza.put_node(stanza_node); + } + + public Stanza.error(Stanza request, StanzaNode error_stanza, StanzaNode? associated_child = null) { + this(request.id); + this.type_ = TYPE_ERROR; + stanza.put_node(error_stanza); + if (associated_child != null) { + stanza.put_node(associated_child); + } + } + public Stanza.from_stanza(StanzaNode stanza_node, string? my_jid) { + base.incoming(stanza_node, my_jid); + } +} + +} |