aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0199_ping.vala
blob: 82da1d2322eb0262b60fc0567e4cfd8f55ff0026 (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
using Gee;

using Xmpp.Core;

namespace Xmpp.Xep.Ping {
    private const string NS_URI = "urn:xmpp:ping";

    public class Module : XmppStreamModule {
        public const string ID = "0199_ping";

        public void send_ping(XmppStream stream, string jid, ResponseListener? listener = null) {
            Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("ping", NS_URI).add_self_xmlns());
            iq.to = jid;
            Iq.Module.get_module(stream).send_iq(stream, iq, listener == null? null : new IqResponseListenerImpl(listener));
        }

        private class IqResponseListenerImpl : Iq.ResponseListener, Object {
            ResponseListener listener;
            public IqResponseListenerImpl(ResponseListener listener) {
                this.listener = listener;
            }
            public void on_result(XmppStream stream, Iq.Stanza iq) {
                listener.on_result(stream);
            }
        }

        public override void attach(XmppStream stream) {
            Iq.Module.require(stream);
            Iq.Module.get_module(stream).register_for_namespace(NS_URI, new IqHandlerImpl());
        }

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

        public override string get_ns() { return NS_URI; }
        public override string get_id() { return ID; }

        private class IqHandlerImpl : Iq.Handler, Object {
            public void on_iq_get(XmppStream stream, Iq.Stanza iq) {
                Iq.Module.get_module(stream).send_iq(stream, new Iq.Stanza.result(iq));
            }
            public void on_iq_set(XmppStream stream, Iq.Stanza iq) { }
        }
    }

    public interface ResponseListener : Object {
        public abstract void on_result(XmppStream stream);
    }
}