aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0482_call_invites.vala
blob: 69980d7fcd641640db540ef9e25b2f7107a4432f (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using Gee;
namespace Xmpp.Xep.CallInvites {

    public const string NS_URI = "urn:xmpp:call-invites:0";
    public const string NS_URI_CUSTOM = "urn:xmpp:call-message:1";

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

        public signal void call_proposed(Jid from, Jid to, string call_id, bool video, Gee.List<StanzaNode> join_methods, MessageStanza message);
        public signal void call_retracted(Jid from, Jid to, string call_id, string message_type);
        public signal void call_accepted(Jid from, Jid to, string call_id, string message_type);
        public signal void call_rejected(Jid from, Jid to, string call_id, string message_type);
        public signal void call_left(Jid from, Jid to, string call_id, string message_type);

        public void send_jingle_propose(XmppStream stream, string call_id, Jid invitee, string sid, bool video) {
            MessageStanza invite_message = new MessageStanza() { to=invitee, type_=MessageStanza.TYPE_CHAT };
            invite_message.stanza.put_node(
                    new StanzaNode.build("invite", NS_URI).add_self_xmlns()
                            .put_attribute("id", call_id)
                            .put_attribute("video", video.to_string())
                            .put_attribute("multi", false.to_string())
                            .put_node(new StanzaNode.build("jingle", CallInvites.NS_URI).put_attribute("sid", sid))
            );
            invite_message.stanza.put_node( // Custom legacy protocol
                    new StanzaNode.build("propose", NS_URI_CUSTOM).add_self_xmlns()
                            .put_attribute("id", call_id)
                            .put_attribute("video", video.to_string())
                            .put_attribute("multi", false.to_string())
                            .put_node(new StanzaNode.build("jingle", CallInvites.NS_URI_CUSTOM).put_attribute("sid", sid))
            );
            MessageProcessingHints.set_message_hint(invite_message, MessageProcessingHints.HINT_STORE);
            stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, invite_message);
        }

        public void send_muji_propose(XmppStream stream, string call_id, Jid invitee, Jid muc_jid, bool video, string message_type) {
            MessageStanza invite_message = new MessageStanza() { to=invitee, type_=MessageStanza.TYPE_CHAT };
            invite_message.stanza.put_node(
                    new StanzaNode.build("invite", NS_URI).add_self_xmlns()
                            .put_attribute("id", call_id)
                            .put_attribute("video", video.to_string())
                            .put_attribute("multi", false.to_string())
                            .put_node(new StanzaNode.build("muji", Muji.NS_URI).add_self_xmlns().put_attribute("room", muc_jid.to_string()))
            );
            invite_message.stanza.put_node( // Custom legacy protocol
                    new StanzaNode.build("propose", NS_URI_CUSTOM).add_self_xmlns()
                    .put_attribute("id", call_id)
                    .put_attribute("video", video.to_string())
                    .put_attribute("multi", false.to_string())
                    .put_node(new StanzaNode.build("muji", Muji.NS_URI).add_self_xmlns().put_attribute("room", muc_jid.to_string()))
            );
            MessageProcessingHints.set_message_hint(invite_message, MessageProcessingHints.HINT_STORE);
            stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, invite_message);
        }

        public void send_jingle_accept(XmppStream stream, Jid inviter, string call_id, string sid, string message_type) {
            StanzaNode accept_node = new StanzaNode.build("accept", NS_URI).add_self_xmlns().put_attribute("id", call_id)
                    .put_node(new StanzaNode.build("jingle", NS_URI).put_attribute("sid", sid));

            // Custom legacy protocol
            StanzaNode custom_accept_node = new StanzaNode.build("accept", NS_URI_CUSTOM).add_self_xmlns().put_attribute("id", call_id)
                    .put_node(new StanzaNode.build("jingle", NS_URI_CUSTOM).put_attribute("sid", sid));

            MessageStanza invite_message = new MessageStanza() { to=inviter, type_=message_type };
            MessageProcessingHints.set_message_hint(invite_message, MessageProcessingHints.HINT_STORE);
            invite_message.stanza.put_node(accept_node);
            invite_message.stanza.put_node(custom_accept_node);
            stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, invite_message);
        }

        public void send_muji_accept(XmppStream stream, Jid inviter, string call_id, Jid room, string message_type) {
            StanzaNode accept_node = new StanzaNode.build("accept", NS_URI).add_self_xmlns().put_attribute("id", call_id)
                    .put_node(new StanzaNode.build("muji", Xep.Muji.NS_URI).add_self_xmlns().put_attribute("room", room.to_string()));

            // Custom legacy protocol
            StanzaNode custom_accept_node = new StanzaNode.build("accept", NS_URI_CUSTOM).add_self_xmlns().put_attribute("id", call_id)
                    .put_node(new StanzaNode.build("muji", Xep.Muji.NS_URI).add_self_xmlns().put_attribute("room", room.to_string()));

            MessageStanza invite_message = new MessageStanza() { to=inviter, type_=message_type };
            MessageProcessingHints.set_message_hint(invite_message, MessageProcessingHints.HINT_STORE);
            invite_message.stanza.put_node(accept_node);
            invite_message.stanza.put_node(custom_accept_node);
            stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, invite_message);
        }

        public void send_retract(XmppStream stream, Jid to, string call_id, string message_type) {
            send_message(stream, to, call_id, "retract", message_type);
        }

        public void send_reject(XmppStream stream, Jid to, string call_id, string message_type) {
            send_message(stream, to, call_id, "reject", message_type);
        }

        public void send_left(XmppStream stream, Jid to, string call_id, string message_type) {
            MessageStanza message = new MessageStanza() { to=to, type_=message_type };

            StanzaNode inner_node = new StanzaNode.build("left", NS_URI).add_self_xmlns().put_attribute("id", call_id);
            message.stanza.put_node(inner_node);

            // Custom legacy protocol
            StanzaNode custom_node = new StanzaNode.build("finish", NS_URI_CUSTOM).add_self_xmlns().put_attribute("id", call_id);
            message.stanza.put_node(custom_node);

            MessageProcessingHints.set_message_hint(message, MessageProcessingHints.HINT_STORE);
            stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, message);
        }

        private void send_message(XmppStream stream, Jid to, string call_id, string action, string message_type) {
            MessageStanza message = new MessageStanza() { to=to, type_=message_type };

            StanzaNode inner_node = new StanzaNode.build(action, NS_URI).add_self_xmlns().put_attribute("id", call_id);
            message.stanza.put_node(inner_node);

            // Custom legacy protocol
            StanzaNode custom_node = new StanzaNode.build(action, NS_URI_CUSTOM).add_self_xmlns().put_attribute("id", call_id);
            message.stanza.put_node(custom_node);

            MessageProcessingHints.set_message_hint(message, MessageProcessingHints.HINT_STORE);
            stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, message);
        }

        private void on_received_message(XmppStream stream, MessageStanza message) {
            Xmpp.MessageArchiveManagement.MessageFlag? mam_flag = Xmpp.MessageArchiveManagement.MessageFlag.get_flag(message);
            if (mam_flag != null) return;

            StanzaNode? relevant_node = null;

            foreach (StanzaNode node in message.stanza.sub_nodes) {
                if (node.ns_uri == NS_URI) {
                    relevant_node = node;
                    break;
                }
            }
            if (relevant_node == null) {
                foreach (StanzaNode node in message.stanza.sub_nodes) {
                    if (node.ns_uri == NS_URI_CUSTOM) {
                        relevant_node = node;
                        break;
                    }
                }
            }
            if (relevant_node == null) return;

            string? call_id = relevant_node.get_attribute("id");
            if (call_id == null) return;

            if (relevant_node.name == "invite" || /* custom legacy */relevant_node.name == "propose") {
                if (relevant_node.sub_nodes.is_empty) return;

                // If there's also a JMI node, just use that one instead.
                foreach (StanzaNode node in message.stanza.sub_nodes) {
                    if (node.ns_uri == JingleMessageInitiation.NS_URI) return;
                }

                bool video = relevant_node.get_attribute_bool("video", false);
                call_proposed(message.from, message.to, call_id, video, relevant_node.sub_nodes, message);
                return;
            }

            switch (relevant_node.name) {
                case "accept":
                    call_accepted(message.from, message.to, call_id, message.type_);
                    break;
                case "retract":
                    call_retracted(message.from, message.to, call_id, message.type_);
                    break;
                case "reject":
                    call_rejected(message.from, message.to, call_id, message.type_);
                    break;
                case "finish":
                    call_left(message.from, message.to, call_id, message.type_);
                    break;
            }
        }

        public override void attach(XmppStream stream) {
            stream.get_module(MessageModule.IDENTITY).received_message.connect(on_received_message);
        }

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

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