aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/muji_meta.vala
blob: fa161f28ee218a6ffc5418e0f1485f5130604612 (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
using Gee;
namespace Xmpp.Xep.MujiMeta {

    public const string NS_URI = "http://telepathy.freedesktop.org/muji";

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

        public signal void call_proposed(Jid from, Jid to, Jid muc_jid, Gee.List<StanzaNode> descriptions, string message_type);
        public signal void call_retracted(Jid from, Jid to, Jid muc_jid, string message_type);
        public signal void call_accepted(Jid from, Jid muc_jid, string message_type);
        public signal void call_rejected(Jid from, Jid to, Jid muc_jid, string message_type);

        public void send_invite(XmppStream stream, Jid invitee, Jid muc_jid, bool video, string message_type) {
            var invite_node = new StanzaNode.build("propose", NS_URI).put_attribute("muc", muc_jid.to_string());
            invite_node.put_node(new StanzaNode.build("description", Xep.JingleRtp.NS_URI).add_self_xmlns().put_attribute("media", "audio"));
            if (video) {
                invite_node.put_node(new StanzaNode.build("description", Xep.JingleRtp.NS_URI).add_self_xmlns().put_attribute("media", "video"));
            }
            var muji_node = new StanzaNode.build("muji", NS_URI).add_self_xmlns().put_node(invite_node);
            MessageStanza invite_message = new MessageStanza() { to=invitee, type_=message_type };
            invite_message.stanza.put_node(muji_node);
            stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, invite_message);
        }

        public void send_invite_retract_to_peer(XmppStream stream, Jid invitee, Jid muc_jid, string message_type) {
            send_jmi_message(stream, "retract", invitee, muc_jid, message_type);
        }

        public void send_invite_accept_to_peer(XmppStream stream, Jid invitor, Jid muc_jid, string message_type) {
            send_jmi_message(stream, "accept", invitor, muc_jid, message_type);
        }

        public void send_invite_accept_to_self(XmppStream stream, Jid muc_jid) {
            send_jmi_message(stream, "accept", Bind.Flag.get_my_jid(stream).bare_jid, muc_jid, MessageStanza.TYPE_CHAT);
        }

        public void send_invite_reject_to_peer(XmppStream stream, Jid invitor, Jid muc_jid, string message_type) {
            send_jmi_message(stream, "reject", invitor, muc_jid, message_type);
        }

        public void send_invite_reject_to_self(XmppStream stream, Jid muc_jid) {
            send_jmi_message(stream, "reject", Bind.Flag.get_my_jid(stream).bare_jid, muc_jid, MessageStanza.TYPE_CHAT);
        }

        private void send_jmi_message(XmppStream stream, string name, Jid to, Jid muc, string message_type) {
            var jmi_node = new StanzaNode.build(name, NS_URI).add_self_xmlns().put_attribute("muc", muc.to_string());
            var muji_node = new StanzaNode.build("muji", NS_URI).add_self_xmlns().put_node(jmi_node);

            MessageStanza accepted_message = new MessageStanza() { to=to, type_= message_type ?? MessageStanza.TYPE_CHAT };
            accepted_message.stanza.put_node(muji_node);
            stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, accepted_message);
        }

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

            var muji_node = message.stanza.get_subnode("muji", NS_URI);
            if (muji_node == null) return;

            StanzaNode? mi_node = null;
            foreach (StanzaNode node in muji_node.sub_nodes) {
                if (node.ns_uri == NS_URI) {
                    mi_node = node;
                }
            }
            if (mi_node == null) return;

            string? jid_str = mi_node.get_attribute("muc");
            if (jid_str == null) return;

            Jid muc_jid = null;
            try {
                muc_jid = new Jid(jid_str);
            } catch (Error e) {
                return;
            }

            switch (mi_node.name) {
                case "accept":
                case "proceed":
                    call_accepted(message.from, muc_jid, message.type_);
                    break;
                case "propose":
                    ArrayList<StanzaNode> descriptions = new ArrayList<StanzaNode>();

                    foreach (StanzaNode node in mi_node.sub_nodes) {
                        if (node.name != "description") continue;
                        descriptions.add(node);
                    }

                    if (descriptions.size > 0) {
                        call_proposed(message.from, message.to, muc_jid, descriptions, message.type_);
                    }
                    break;
                case "retract":
                    call_retracted(message.from, message.to, muc_jid, message.type_);
                    break;
                case "reject":
                    call_rejected(message.from, message.to, muc_jid, 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; }
    }
}