aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0184_message_delivery_receipts.vala
blob: b8597d0782c6ad5f70da27277e5fffd9affa59b2 (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
using Gdk;

using Xmpp.Core;

namespace Xmpp.Xep.MessageDeliveryReceipts {
    private const string NS_URI = "urn:xmpp:receipts";

    public class Module : XmppStreamModule {
        public const string ID = "0184_message_delivery_receipts";
        public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, ID);

        public signal void receipt_received(XmppStream stream, string jid, string id);

        public override void attach(XmppStream stream) {
            ServiceDiscovery.Module.require(stream);
            Message.Module.require(stream);

            stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI);
            stream.get_module(Message.Module.IDENTITY).received_message.connect(received_message);
            stream.get_module(Message.Module.IDENTITY).pre_send_message.connect(pre_send_message);
        }

        public override void detach(XmppStream stream) {
            stream.get_module(Message.Module.IDENTITY).received_message.disconnect(received_message);
            stream.get_module(Message.Module.IDENTITY).pre_send_message.disconnect(pre_send_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 ID; }

        private void received_message(XmppStream stream, Message.Stanza message) {
            StanzaNode? received_node = message.stanza.get_subnode("received", NS_URI);
            if (received_node != null) {
                receipt_received(stream, message.from, received_node.get_attribute("id", NS_URI));
            } else if (message.stanza.get_subnode("request", NS_URI) != null) {
                send_received(stream, message);
            }
        }

        private void send_received(XmppStream stream, Message.Stanza message) {
            Message.Stanza received_message = new Message.Stanza();
            received_message.to = message.from;
            received_message.stanza.put_node(new StanzaNode.build("received", NS_URI).add_self_xmlns().put_attribute("id", message.id));
            stream.get_module(Message.Module.IDENTITY).send_message(stream, received_message);
        }

        private void pre_send_message(XmppStream stream, Message.Stanza message) {
            StanzaNode? received_node = message.stanza.get_subnode("received", NS_URI);
            if (received_node != null) return;
            if (message.body == null) return;
            if (message.type_ == Message.Stanza.TYPE_GROUPCHAT) return;
            message.stanza.put_node(new StanzaNode.build("request", NS_URI).add_self_xmlns());
        }
    }
}