aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0166_jingle/content_node.vala
blob: 7d8d56c838da24341c27eba994b558ea352a1756 (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
using Gee;
using Xmpp.Xep;
using Xmpp;

namespace Xmpp.Xep.Jingle {

    class ContentNode {
        public Role creator;
        public string name;
        public Senders senders;
        public StanzaNode? description;
        public StanzaNode? transport;
        public StanzaNode? security;
    }

    [Version(deprecated = true)]
    ContentNode get_single_content_node(StanzaNode jingle) throws IqError {
        Gee.List<StanzaNode> contents = jingle.get_subnodes("content");
        if (contents.size == 0) {
            throw new IqError.BAD_REQUEST("missing content node");
        }
        if (contents.size > 1) {
            throw new IqError.NOT_IMPLEMENTED("can't process multiple content nodes");
        }
        StanzaNode content = contents[0];
        string? creator_str = content.get_attribute("creator");
        // Vala can't typecheck the ternary operator here.
        Role? creator = null;
        if (creator_str != null) {
            creator = Role.parse(creator_str);
        } else {
            // TODO(hrxi): now, is the creator attribute optional or not (XEP-0166
            // Jingle)?
            creator = Role.INITIATOR;
        }

        string? name = content.get_attribute("name");

        Senders senders = Senders.parse(content.get_attribute("senders"));

        StanzaNode? description = get_single_node_anyns(content, "description");
        StanzaNode? transport = get_single_node_anyns(content, "transport");
        StanzaNode? security = get_single_node_anyns(content, "security");
        if (name == null || creator == null) {
            throw new IqError.BAD_REQUEST("missing name or creator");
        }

        return new ContentNode() {
            creator=creator,
            name=name,
            senders=senders,
            description=description,
            transport=transport,
            security=security
        };
    }

    Gee.List<ContentNode> get_content_nodes(StanzaNode jingle) throws IqError {
        Gee.List<StanzaNode> contents = jingle.get_subnodes("content");
        if (contents.size == 0) {
            throw new IqError.BAD_REQUEST("missing content node");
        }
        Gee.List<ContentNode> list = new ArrayList<ContentNode>();
        foreach (StanzaNode content in contents) {
            string? creator_str = content.get_attribute("creator");
            // Vala can't typecheck the ternary operator here.
            Role? creator = null;
            if (creator_str != null) {
                creator = Role.parse(creator_str);
            } else {
                // TODO(hrxi): now, is the creator attribute optional or not (XEP-0166
                // Jingle)?
                creator = Role.INITIATOR;
            }

            string? name = content.get_attribute("name");
            Senders senders = Senders.parse(content.get_attribute("senders"));
            StanzaNode? description = get_single_node_anyns(content, "description");
            StanzaNode? transport = get_single_node_anyns(content, "transport");
            StanzaNode? security = get_single_node_anyns(content, "security");
            if (name == null || creator == null) {
                throw new IqError.BAD_REQUEST("missing name or creator");
            }
            list.add(new ContentNode() {
                creator=creator,
                name=name,
                senders=senders,
                description=description,
                transport=transport,
                security=security
            });
        }
        return list;
    }

    StanzaNode? get_single_node_anyns(StanzaNode parent, string? node_name = null) throws IqError {
        StanzaNode? result = null;
        foreach (StanzaNode child in parent.get_all_subnodes()) {
            if (node_name == null || child.name == node_name) {
                if (result != null) {
                    if (node_name != null) {
                        throw new IqError.BAD_REQUEST(@"multiple $(node_name) nodes");
                    } else {
                        throw new IqError.BAD_REQUEST(@"expected single subnode");
                    }
                }
                result = child;
            }
        }
        return result;
    }
}