aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/iq/module.vala
blob: eed3389d004f20c3ab38cbaaa5dbb19419af5f7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Gee;

using Xmpp.Core;

namespace Xmpp.Iq {
    private const string NS_URI = "jabber:client";

    public class Module : XmppStreamNegotiationModule {
        public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "iq_module");

        private HashMap<string, ResponseListener> responseListeners = new HashMap<string, ResponseListener>();
        private HashMap<string, ArrayList<Handler>> namespaceRegistrants = new HashMap<string, ArrayList<Handler>>();

        [CCode (has_target = false)] public delegate void OnResult(XmppStream stream, Iq.Stanza iq, Object reference);
        public void send_iq(XmppStream stream, Iq.Stanza iq, OnResult? listener = null, Object? reference = null) {
            try {
                stream.write(iq.stanza);
            } catch (IOStreamError e) {
                print(@"$(e.message)\n");
            }
            if (listener != null) {
                responseListeners[iq.id] = new ResponseListener(listener, reference);
            }
        }

        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 void require(XmppStream stream) {
            if (stream.get_module(IDENTITY) == 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 IDENTITY.id; }

        private void on_received_iq_stanza(XmppStream stream, StanzaNode node) {
            Iq.Stanza iq = new Iq.Stanza.from_stanza(node, stream.has_flag(Bind.Flag.IDENTITY) ? stream.get_flag(Bind.Flag.IDENTITY).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, listener.reference);
                    }
                    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);
                }
            }
        }

        private class ResponseListener {
            public OnResult on_result { get; private set; }
            public Object? reference { get; private set; }

            public ResponseListener(OnResult on_result, Object? reference = null) {
                this.on_result = on_result;
                this.reference = reference;
            }
        }
    }

    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);
    }

}