aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0060_pubsub.vala
blob: 8ba669956b435e0185ee48908b15bd866107e5b6 (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
using Gee;

using Xmpp.Core;

namespace Xmpp.Xep.Pubsub {
    private const string NS_URI = "http://jabber.org/protocol/pubsub";
    private const string NS_URI_EVENT = NS_URI + "#event";

    public class Module : XmppStreamModule {
        public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0060_pubsub_module");

        private HashMap<string, EventListenerDelegate> event_listeners = new HashMap<string, EventListenerDelegate>();

        public void add_filtered_notification(XmppStream stream, string node, EventListenerDelegate.ResultFunc on_result, Object? reference = null) {
            stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature_notify(stream, node);
            event_listeners[node] = new EventListenerDelegate(on_result, reference);
        }

        [CCode (has_target = false)] public delegate void OnResult(XmppStream stream, string jid, string? id, StanzaNode? node, Object? storage);
        public void request(XmppStream stream, string jid, string node, OnResult listener, Object? store) { // TODO multiple nodes gehen auch
            Iq.Stanza a = new Iq.Stanza.get(new StanzaNode.build("pubsub", NS_URI).add_self_xmlns().put_node(new StanzaNode.build("items", NS_URI).put_attribute("node", node)));
            a.to = jid;
            stream.get_module(Iq.Module.IDENTITY).send_iq(stream, a, on_received_request_response, Tuple.create(listener, store));
        }

        public void publish(XmppStream stream, string? jid, string node_id, string node, string item_id, StanzaNode content) {
            StanzaNode pubsub_node = new StanzaNode.build("pubsub", NS_URI).add_self_xmlns();
            StanzaNode publish_node = new StanzaNode.build("publish", NS_URI).put_attribute("node", node_id);
            pubsub_node.put_node(publish_node);
            StanzaNode items_node = new StanzaNode.build("item", NS_URI).put_attribute("id", item_id);
            items_node.put_node(content);
            publish_node.put_node(items_node);
            Iq.Stanza iq = new Iq.Stanza.set(pubsub_node);
            stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, null);
        }

        public override void attach(XmppStream stream) {
            Iq.Module.require(stream);
            Message.Module.require(stream);
            ServiceDiscovery.Module.require(stream);
            stream.get_module(Message.Module.IDENTITY).received_message.connect(on_received_message);
        }

        public override void detach(XmppStream stream) {
            stream.get_module(Message.Module.IDENTITY).received_message.disconnect(on_received_message);
        }

        public static void require(XmppStream stream) {
            if (stream.get_module(IDENTITY) == null) stream.add_module(new Module());
        }

        public override string get_ns() { return NS_URI; }
        public override string get_id() { return IDENTITY.id; }

        private void on_received_message(XmppStream stream, Message.Stanza message) {
            StanzaNode event_node = message.stanza.get_subnode("event", NS_URI_EVENT); if (event_node == null) return;
            StanzaNode items_node = event_node.get_subnode("items", NS_URI_EVENT); if (items_node == null) return;
            StanzaNode item_node = items_node.get_subnode("item", NS_URI_EVENT); if (item_node == null) return;
            string node = items_node.get_attribute("node", NS_URI_EVENT);
            string id = item_node.get_attribute("id", NS_URI_EVENT);
            if (event_listeners.has_key(node)) {
                event_listeners[node].on_result(stream, message.from, id, item_node.sub_nodes[0], event_listeners[node].reference);
            }
        }

        private static void on_received_request_response(XmppStream stream, Iq.Stanza iq, Object? o) {
            Tuple<OnResult, Object?> tuple = o as Tuple<OnResult, Object?>;
            OnResult on_result = tuple.a;
            StanzaNode event_node = iq.stanza.get_subnode("pubsub", NS_URI);
            StanzaNode items_node = event_node != null ? event_node.get_subnode("items", NS_URI) : null;
            StanzaNode item_node = items_node != null ? items_node.get_subnode("item", NS_URI) : null;
            string? id = item_node != null ? item_node.get_attribute("id", NS_URI) : null;
            on_result(stream, iq.from, id, item_node != null ? item_node.sub_nodes[0] : null, tuple.b);
        }
    }

    public class EventListenerDelegate {
        [CCode (has_target = false)]
        public delegate void ResultFunc(XmppStream stream, string jid, string id, StanzaNode node, Object? object);
        public ResultFunc on_result { get; private set; }
        public Object? reference { get; private set; }

        public EventListenerDelegate(ResultFunc on_result, Object? reference = null) {
            this.on_result = on_result;
            this.reference = reference;
        }
    }
}