Debian Codegraph @

xmpp-vala/src/module/xep/0166_jingle/jingle_module.vala

raw
1 using Gee;
2 using Xmpp;
3
4 namespace Xmpp.Xep.Jingle {
5
6 public const string NS_URI = "urn:xmpp:jingle:1";
7 private const string ERROR_NS_URI = "urn:xmpp:jingle:errors:1";
8
9 // This module can only be attached to one stream at a time.
10 public class Module : XmppStreamModule, Iq.Handler {
11 public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0166_jingle");
12
13 public signal void session_initiate_received(XmppStream stream, Session session);
14
15 private HashMap<string, ContentType> content_types = new HashMap<string, ContentType>();
16 private HashMap<string, SessionInfoNs> session_info_types = new HashMap<string, SessionInfoNs>();
17 private HashMap<string, Transport> transports = new HashMap<string, Transport>();
18 private HashMap<string, SecurityPrecondition> security_preconditions = new HashMap<string, SecurityPrecondition>();
19
20 public override void attach(XmppStream stream) {
21 stream.add_flag(new Flag());
22 stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI);
23 stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI, this);
24
25 // TODO update stream in all sessions
26 }
27
28 public override void detach(XmppStream stream) {
29 stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
30 stream.get_module(Iq.Module.IDENTITY).unregister_from_namespace(NS_URI, this);
31 }
32
33 public void register_content_type(ContentType content_type) {
34 content_types[content_type.ns_uri] = content_type;
35 }
36
37 public void register_session_info_type(SessionInfoNs info_ns) {
38 session_info_types[info_ns.ns_uri] = info_ns;
39 }
40
41 public ContentType? get_content_type(string ns_uri) {
42 if (!content_types.has_key(ns_uri)) {
43 return null;
44 }
45 return content_types[ns_uri];
46 }
47
48 public SessionInfoNs? get_session_info_type(string ns_uri) {
49 return session_info_types[ns_uri];
50 }
51
52 public void register_transport(Transport transport) {
53 transports[transport.ns_uri] = transport;
54 }
55
56 public Transport? get_transport(string ns_uri) {
57 if (!transports.has_key(ns_uri)) {
58 return null;
59 }
60 return transports[ns_uri];
61 }
62
63 public async Transport? select_transport(XmppStream stream, TransportType type, uint8 components, Jid receiver_full_jid, Set<string> blacklist) {
64 Transport? result = null;
65 foreach (Transport transport in transports.values) {
66 if (transport.type_ != type) {
67 continue;
68 }
69 if (transport.ns_uri in blacklist) {
70 continue;
71 }
72 if (yield transport.is_transport_available(stream, components, receiver_full_jid)) {
73 if (result != null) {
74 if (result.priority >= transport.priority) {
75 continue;
76 }
77 }
78 result = transport;
79 }
80 }
81 return result;
82 }
83
84 public void register_security_precondition(SecurityPrecondition precondition) {
85 security_preconditions[precondition.security_ns_uri()] = precondition;
86 }
87
88 public SecurityPrecondition? get_security_precondition(string? ns_uri) {
89 if (ns_uri == null) return null;
90 if (!security_preconditions.has_key(ns_uri)) {
91 return null;
92 }
93 return security_preconditions[ns_uri];
94 }
95
96 private async bool is_jingle_available(XmppStream stream, Jid full_jid) {
97 bool? has_jingle = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI);
98 return has_jingle != null && has_jingle;
99 }
100
101 public async bool is_available(XmppStream stream, TransportType type, uint8 components, Jid full_jid) {
102 return (yield is_jingle_available(stream, full_jid)) && (yield select_transport(stream, type, components, full_jid, Set.empty())) != null;
103 }
104
105 public async Session create_session(XmppStream stream, Gee.List<Content> contents, Jid receiver_full_jid, string? sid = null, Jid? muji_room = null) throws Error {
106 if (!yield is_jingle_available(stream, receiver_full_jid)) {
107 throw new Error.NO_SHARED_PROTOCOLS("No Jingle support");
108 }
109 Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid;
110 if (my_jid == null) {
111 throw new Error.GENERAL("Couldn't determine own JID");
112 }
113
114 Session session = new Session.initiate_sent(stream, sid ?? random_uuid(), my_jid, receiver_full_jid);
115 session.terminated.connect((session, stream, _1, _2, _3) => { stream.get_flag(Flag.IDENTITY).remove_session(session.sid); });
116
117 foreach (Content content in contents) {
118 session.insert_content(content);
119 }
120
121 // Build & send session-initiate iq stanza
122 StanzaNode initiate_jingle_iq = new StanzaNode.build("jingle", NS_URI)
123 .add_self_xmlns()
124 .put_attribute("action", "session-initiate")
125 .put_attribute("initiator", my_jid.to_string())
126 .put_attribute("sid", session.sid);
127
128 foreach (Content content in contents) {
129 StanzaNode content_node = new StanzaNode.build("content", NS_URI)
130 .put_attribute("creator", "initiator")
131 .put_attribute("name", content.content_name)
132 .put_attribute("senders", content.senders.to_string())
133 .put_node(content.content_params.get_description_node())
134 .put_node(content.transport_params.to_transport_stanza_node("session-initiate"));
135 if (content.security_params != null) {
136 content_node.put_node(content.security_params.to_security_stanza_node(stream, my_jid, receiver_full_jid));
137 }
138 initiate_jingle_iq.put_node(content_node);
139 }
140
141 if (muji_room != null) {
142 initiate_jingle_iq.put_node(new StanzaNode.build("muji", Xep.Muji.NS_URI).add_self_xmlns().put_attribute("room", muji_room.to_string()));
143 }
144
145 Iq.Stanza iq = new Iq.Stanza.set(initiate_jingle_iq) { to=receiver_full_jid };
146
147 stream.get_flag(Flag.IDENTITY).add_session(session);
148 // We might get a follow-up before the ack => add_session before send_iq returns
149 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => {
150 if (iq.is_error()) warning("Jingle session-initiate got error: %s", iq.stanza.to_string());
151 });
152
153 return session;
154 }
155
156 public async void handle_session_initiate(XmppStream stream, string sid, StanzaNode jingle, Iq.Stanza iq) throws IqError {
157 Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid;
158 if (my_jid == null) {
159 throw new IqError.RESOURCE_CONSTRAINT("Couldn't determine own JID");
160 }
161
162 Session session = new Session.initiate_received(stream, sid, my_jid, iq.from);
163 session.terminated.connect((stream) => { stream.get_flag(Flag.IDENTITY).remove_session(sid); });
164
165 string? muji_room_str = iq.stanza.get_deep_attribute(NS_URI + ":jingle", Xep.Muji.NS_URI + ":muji", "room");
166 if (muji_room_str != null) {
167 try {
168 session.muji_room = new Jid(muji_room_str);
169 } catch (InvalidJidError e) {
170 // Ignore
171 }
172 }
173
174 stream.get_flag(Flag.IDENTITY).pre_add_session(session.sid);
175
176 foreach (ContentNode content_node in get_content_nodes(jingle)) {
177 yield session.insert_content_node(content_node, iq.from);
178 }
179
180 stream.get_flag(Flag.IDENTITY).add_session(session);
181
182 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
183
184 session_initiate_received(stream, session);
185 }
186
187 public async void on_iq_set(XmppStream stream, Iq.Stanza iq) {
188 try {
189 yield handle_iq_set(stream, iq);
190 } catch (IqError e) {
191 send_iq_error(e, stream, iq);
192 }
193 }
194
195 public async void handle_iq_set(XmppStream stream, Iq.Stanza iq) throws IqError {
196 StanzaNode? jingle_node = iq.stanza.get_subnode("jingle", NS_URI);
197 if (jingle_node == null) {
198 throw new IqError.BAD_REQUEST("missing jingle node");
199 }
200 string? sid = jingle_node.get_attribute("sid");
201 string? action = jingle_node.get_attribute("action");
202 if (sid == null || action == null) {
203 throw new IqError.BAD_REQUEST("missing jingle node, sid or action");
204 }
205
206 Session? session = yield stream.get_flag(Flag.IDENTITY).get_session(sid);
207 if (action == "session-initiate") {
208 if (session != null) {
209 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, new ErrorStanza.build(ErrorStanza.TYPE_MODIFY, ErrorStanza.CONDITION_CONFLICT, "session ID already in use", null)) { to=iq.from });
210 return;
211 }
212 yield handle_session_initiate(stream, sid, jingle_node, iq);
213 return;
214 }
215 if (session == null) {
216 StanzaNode unknown_session = new StanzaNode.build("unknown-session", ERROR_NS_URI).add_self_xmlns();
217 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, new ErrorStanza.item_not_found(unknown_session)) { to=iq.from });
218 return;
219 }
220 session.handle_iq_set(action, jingle_node, iq);
221 }
222
223 public override string get_ns() { return NS_URI; }
224 public override string get_id() { return IDENTITY.id; }
225 }
226
227 void send_iq_error(IqError iq_error, XmppStream stream, Iq.Stanza iq) {
228 ErrorStanza error;
229 if (iq_error is IqError.BAD_REQUEST) {
230 error = new ErrorStanza.bad_request(iq_error.message);
231 } else if (iq_error is IqError.NOT_ACCEPTABLE) {
232 error = new ErrorStanza.not_acceptable(iq_error.message);
233 } else if (iq_error is IqError.NOT_IMPLEMENTED) {
234 error = new ErrorStanza.feature_not_implemented(iq_error.message);
235 } else if (iq_error is IqError.UNSUPPORTED_INFO) {
236 StanzaNode unsupported_info = new StanzaNode.build("unsupported-info", ERROR_NS_URI).add_self_xmlns();
237 error = new ErrorStanza.build(ErrorStanza.TYPE_CANCEL, ErrorStanza.CONDITION_FEATURE_NOT_IMPLEMENTED, iq_error.message, unsupported_info);
238 } else if (iq_error is IqError.OUT_OF_ORDER) {
239 StanzaNode out_of_order = new StanzaNode.build("out-of-order", ERROR_NS_URI).add_self_xmlns();
240 error = new ErrorStanza.build(ErrorStanza.TYPE_MODIFY, ErrorStanza.CONDITION_UNEXPECTED_REQUEST, iq_error.message, out_of_order);
241 } else if (iq_error is IqError.RESOURCE_CONSTRAINT) {
242 error = new ErrorStanza.resource_constraint(iq_error.message);
243 } else {
244 assert_not_reached();
245 }
246 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, error) { to=iq.from });
247 }
248 }