aboutsummaryrefslogtreecommitdiff
path: root/vala-xmpp/src/module/xep/0280_message_carbons.vala
blob: 18b2ecdf1bd0a08f635cb77b4b350ade6020e5a3 (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
using Xmpp.Core;

namespace Xmpp.Xep.MessageCarbons {
    private const string NS_URI = "urn:xmpp:carbons:2";

    public class Module : XmppStreamModule {
        public const string ID = "0280_message_carbons_module";

        public void enable(XmppStream stream) {
            Iq.Stanza iq = new Iq.Stanza.set(new StanzaNode.build("enable", NS_URI).add_self_xmlns());
            Iq.Module.get_module(stream).send_iq(stream, iq);
        }

        public void disable(XmppStream stream) {
            Iq.Stanza iq = new Iq.Stanza.set(new StanzaNode.build("disable", NS_URI).add_self_xmlns());
            Iq.Module.get_module(stream).send_iq(stream, iq);
        }

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

            stream.stream_negotiated.connect(enable);
            Message.Module.get_module(stream).pre_received_message.connect(pre_received_message);
            ServiceDiscovery.Module.get_module(stream).add_feature(stream, NS_URI);
        }

        public override void detach(XmppStream stream) {
            stream.stream_negotiated.disconnect(enable);
            Message.Module.get_module(stream).pre_received_message.disconnect(pre_received_message);
        }

        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 void pre_received_message(XmppStream stream, Message.Stanza message) {
            StanzaNode? received_node = message.stanza.get_subnode("received", NS_URI);
            StanzaNode? sent_node = received_node == null ? message.stanza.get_subnode("sent", NS_URI) : null;
            StanzaNode? carbons_node = received_node != null ? received_node : sent_node;
            if (carbons_node != null) {
                StanzaNode? forwarded_node = carbons_node.get_subnode("forwarded", "urn:xmpp:forward:0");
                if (forwarded_node != null) {
                    StanzaNode? message_node = forwarded_node.get_subnode("message", Message.NS_URI);
                    string? from_attribute = message_node.get_attribute("from", Message.NS_URI);
                    // The security model assumed by this document is that all of the resources for a single user are in the same trust boundary.
                    // Any forwarded copies received by a Carbons-enabled client MUST be from that user's bare JID; any copies that do not meet this requirement MUST be ignored.
                    if (from_attribute != null && from_attribute == get_bare_jid(Bind.Flag.get_flag(stream).my_jid)) {
                        if (received_node != null) {
                            message.add_flag(new MessageFlag(MessageFlag.TYPE_RECEIVED));
                        } else if (sent_node != null) {
                            message.add_flag(new MessageFlag(MessageFlag.TYPE_SENT));
                        }
                        message.stanza = message_node;
                        message.rerun_parsing = true;
                    }
                    message.stanza = message_node;
                    message.rerun_parsing = true;
                }
            }
        }
    }

    public class MessageFlag : Message.MessageFlag {
        public const string id = "message_carbons";

        public const string TYPE_RECEIVED = "received";
        public const string TYPE_SENT = "sent";
        private string type_;

        public MessageFlag(string type) {
            this.type_ = type;
        }

        public static MessageFlag? get_flag(Message.Stanza message) {
            return (MessageFlag) message.get_flag(NS_URI, id);
        }

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