| 1 |
using Gee; |
| 2 |
using Xmpp.Xep; |
| 3 |
using Xmpp; |
| 4 |
|
| 5 |
namespace Xmpp.Xep.Jingle { |
| 6 |
|
| 7 |
class ContentNode { |
| 8 |
public Role creator; |
| 9 |
public string name; |
| 10 |
public Senders senders; |
| 11 |
public StanzaNode? description; |
| 12 |
public StanzaNode? transport; |
| 13 |
public StanzaNode? security; |
| 14 |
} |
| 15 |
|
| 16 |
[Version(deprecated = true)] |
| 17 |
ContentNode get_single_content_node(StanzaNode jingle) throws IqError { |
| 18 |
Gee.List<StanzaNode> contents = jingle.get_subnodes("content"); |
| 19 |
if (contents.size == 0) { |
| 20 |
throw new IqError.BAD_REQUEST("missing content node"); |
| 21 |
} |
| 22 |
if (contents.size > 1) { |
| 23 |
throw new IqError.NOT_IMPLEMENTED("can't process multiple content nodes"); |
| 24 |
} |
| 25 |
StanzaNode content = contents[0]; |
| 26 |
string? creator_str = content.get_attribute("creator"); |
| 27 |
// Vala can't typecheck the ternary operator here. |
| 28 |
Role? creator = null; |
| 29 |
if (creator_str != null) { |
| 30 |
creator = Role.parse(creator_str); |
| 31 |
} else { |
| 32 |
// TODO(hrxi): now, is the creator attribute optional or not (XEP-0166 |
| 33 |
// Jingle)? |
| 34 |
creator = Role.INITIATOR; |
| 35 |
} |
| 36 |
|
| 37 |
string? name = content.get_attribute("name"); |
| 38 |
|
| 39 |
Senders senders = Senders.parse(content.get_attribute("senders")); |
| 40 |
|
| 41 |
StanzaNode? description = get_single_node_anyns(content, "description"); |
| 42 |
StanzaNode? transport = get_single_node_anyns(content, "transport"); |
| 43 |
StanzaNode? security = get_single_node_anyns(content, "security"); |
| 44 |
if (name == null || creator == null) { |
| 45 |
throw new IqError.BAD_REQUEST("missing name or creator"); |
| 46 |
} |
| 47 |
|
| 48 |
return new ContentNode() { |
| 49 |
creator=creator, |
| 50 |
name=name, |
| 51 |
senders=senders, |
| 52 |
description=description, |
| 53 |
transport=transport, |
| 54 |
security=security |
| 55 |
}; |
| 56 |
} |
| 57 |
|
| 58 |
Gee.List<ContentNode> get_content_nodes(StanzaNode jingle) throws IqError { |
| 59 |
Gee.List<StanzaNode> contents = jingle.get_subnodes("content"); |
| 60 |
if (contents.size == 0) { |
| 61 |
throw new IqError.BAD_REQUEST("missing content node"); |
| 62 |
} |
| 63 |
Gee.List<ContentNode> list = new ArrayList<ContentNode>(); |
| 64 |
foreach (StanzaNode content in contents) { |
| 65 |
string? creator_str = content.get_attribute("creator"); |
| 66 |
// Vala can't typecheck the ternary operator here. |
| 67 |
Role? creator = null; |
| 68 |
if (creator_str != null) { |
| 69 |
creator = Role.parse(creator_str); |
| 70 |
} else { |
| 71 |
// TODO(hrxi): now, is the creator attribute optional or not (XEP-0166 |
| 72 |
// Jingle)? |
| 73 |
creator = Role.INITIATOR; |
| 74 |
} |
| 75 |
|
| 76 |
string? name = content.get_attribute("name"); |
| 77 |
Senders senders = Senders.parse(content.get_attribute("senders")); |
| 78 |
StanzaNode? description = get_single_node_anyns(content, "description"); |
| 79 |
StanzaNode? transport = get_single_node_anyns(content, "transport"); |
| 80 |
StanzaNode? security = get_single_node_anyns(content, "security"); |
| 81 |
if (name == null || creator == null) { |
| 82 |
throw new IqError.BAD_REQUEST("missing name or creator"); |
| 83 |
} |
| 84 |
list.add(new ContentNode() { |
| 85 |
creator=creator, |
| 86 |
name=name, |
| 87 |
senders=senders, |
| 88 |
description=description, |
| 89 |
transport=transport, |
| 90 |
security=security |
| 91 |
}); |
| 92 |
} |
| 93 |
return list; |
| 94 |
} |
| 95 |
|
| 96 |
StanzaNode? get_single_node_anyns(StanzaNode parent, string? node_name = null) throws IqError { |
| 97 |
StanzaNode? result = null; |
| 98 |
foreach (StanzaNode child in parent.get_all_subnodes()) { |
| 99 |
if (node_name == null || child.name == node_name) { |
| 100 |
if (result != null) { |
| 101 |
if (node_name != null) { |
| 102 |
throw new IqError.BAD_REQUEST(@"multiple $(node_name) nodes"); |
| 103 |
} else { |
| 104 |
throw new IqError.BAD_REQUEST(@"expected single subnode"); |
| 105 |
} |
| 106 |
} |
| 107 |
result = child; |
| 108 |
} |
| 109 |
} |
| 110 |
return result; |
| 111 |
} |
| 112 |
} |