aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0166_jingle
diff options
context:
space:
mode:
authorfiaxh <git@lightrise.org>2021-05-11 12:57:02 +0200
committerfiaxh <git@lightrise.org>2021-05-11 12:57:02 +0200
commitd71604913dd5b3372a823320db83c37c845fac5c (patch)
tree2ffbff97a02c81d48d8aef4a4b7ee870507236e9 /xmpp-vala/src/module/xep/0166_jingle
parente92ed27317ae398c867c946cf7206b1f0b32f3b4 (diff)
parent90f9ecf62b2ebfef14de2874e7942552409632bf (diff)
downloaddino-d71604913dd5b3372a823320db83c37c845fac5c.tar.gz
dino-d71604913dd5b3372a823320db83c37c845fac5c.zip
Merge remote-tracking branch 'origin/feature/calls'
Diffstat (limited to 'xmpp-vala/src/module/xep/0166_jingle')
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/component.vala62
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/content.vala239
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/content_description.vala27
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/content_node.vala112
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/content_security.vala18
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/content_transport.vala29
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/jingle_flag.vala38
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/jingle_module.vala235
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/jingle_structs.vala73
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/reason_element.vala30
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/session.vala561
-rw-r--r--xmpp-vala/src/module/xep/0166_jingle/session_info.vala12
12 files changed, 1436 insertions, 0 deletions
diff --git a/xmpp-vala/src/module/xep/0166_jingle/component.vala b/xmpp-vala/src/module/xep/0166_jingle/component.vala
new file mode 100644
index 00000000..5d573522
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/component.vala
@@ -0,0 +1,62 @@
+namespace Xmpp.Xep.Jingle {
+
+ public abstract class ComponentConnection : Object {
+ public uint8 component_id { get; set; default = 0; }
+ public abstract async void terminate(bool we_terminated, string? reason_name = null, string? reason_text = null);
+ public signal void connection_closed();
+ public signal void connection_error(IOError e);
+ }
+
+ public abstract class DatagramConnection : ComponentConnection {
+ public bool ready { get; set; default = false; }
+ private string? terminate_reason_name = null;
+ private string? terminate_reason_text = null;
+ private bool terminated = false;
+
+ public override async void terminate(bool we_terminated, string? reason_string = null, string? reason_text = null) {
+ if (!terminated) {
+ terminated = true;
+ terminate_reason_name = reason_string;
+ terminate_reason_text = reason_text;
+ connection_closed();
+ }
+ }
+
+ public signal void datagram_received(Bytes datagram);
+ public abstract void send_datagram(Bytes datagram);
+ }
+
+ public class StreamingConnection : ComponentConnection {
+ public Gee.Future<IOStream> stream { get { return promise.future; } }
+ protected Gee.Promise<IOStream> promise = new Gee.Promise<IOStream>();
+ private string? terminated = null;
+
+ public async void set_stream(IOStream? stream) {
+ if (stream == null) {
+ promise.set_exception(new IOError.FAILED("Jingle connection failed"));
+ return;
+ }
+ assert(!this.stream.ready);
+ promise.set_value(stream);
+ if (terminated != null) {
+ yield stream.close_async();
+ }
+ }
+
+ public void set_error(GLib.Error? e) {
+ promise.set_exception(e);
+ }
+
+ public override async void terminate(bool we_terminated, string? reason_name = null, string? reason_text = null) {
+ if (terminated == null) {
+ terminated = (reason_name ?? "") + " - " + (reason_text ?? "") + @"we terminated? $we_terminated";
+ if (stream.ready) {
+ yield stream.value.close_async();
+ } else {
+ promise.set_exception(new IOError.FAILED("Jingle connection failed"));
+ }
+ }
+ }
+ }
+}
+
diff --git a/xmpp-vala/src/module/xep/0166_jingle/content.vala b/xmpp-vala/src/module/xep/0166_jingle/content.vala
new file mode 100644
index 00000000..41310aeb
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/content.vala
@@ -0,0 +1,239 @@
+using Gee;
+using Xmpp;
+
+public class Xmpp.Xep.Jingle.Content : Object {
+
+ public signal void senders_modify_incoming(Senders proposed_senders);
+
+ // INITIATE_SENT -> CONNECTING -> [REPLACING_TRANSPORT -> CONNECTING ->]... ACTIVE -> ENDED
+ // INITIATE_RECEIVED -> CONNECTING -> [WAITING_FOR_TRANSPORT_REPLACE -> CONNECTING ->].. ACTIVE -> ENDED
+ public enum State {
+ PENDING,
+ WANTS_TO_BE_ACCEPTED,
+ ACCEPTED,
+ REPLACING_TRANSPORT,
+ WAITING_FOR_TRANSPORT_REPLACE
+ }
+
+ public State state { get; set; }
+
+ public Role role { get; private set; }
+ public Jid local_full_jid { get; private set; }
+ public Jid peer_full_jid { get; private set; }
+ public Role content_creator { get; private set; }
+ public string content_name { get; private set; }
+ public Senders senders { get; private set; }
+
+ public ContentType content_type;
+ public ContentParameters content_params;
+ public Transport transport;
+ public TransportParameters? transport_params;
+ public SecurityPrecondition security_precondition;
+ public SecurityParameters? security_params;
+
+ public weak Session session;
+ public Map<uint8, ComponentConnection> component_connections = new HashMap<uint8, ComponentConnection>(); // TODO private
+
+ public HashMap<string, ContentEncryption> encryptions = new HashMap<string, ContentEncryption>();
+
+ private Set<string> tried_transport_methods = new HashSet<string>();
+
+
+ public Content.initiate_sent(string content_name, Senders senders,
+ ContentType content_type, ContentParameters content_params,
+ Transport transport, TransportParameters? transport_params,
+ SecurityPrecondition? security_precondition, SecurityParameters? security_params,
+ Jid local_full_jid, Jid peer_full_jid) {
+ this.content_name = content_name;
+ this.senders = senders;
+ this.role = Role.INITIATOR;
+ this.local_full_jid = local_full_jid;
+ this.peer_full_jid = peer_full_jid;
+ this.content_creator = Role.INITIATOR;
+
+ this.content_type = content_type;
+ this.content_params = content_params;
+ this.transport = transport;
+ this.transport_params = transport_params;
+ this.security_precondition = security_precondition;
+ this.security_params = security_params;
+
+ this.tried_transport_methods.add(transport.ns_uri);
+
+ state = State.PENDING;
+ }
+
+ public Content.initiate_received(string content_name, Senders senders,
+ ContentType content_type, ContentParameters content_params,
+ Transport transport, TransportParameters? transport_params,
+ SecurityPrecondition? security_precondition, SecurityParameters? security_params,
+ Jid local_full_jid, Jid peer_full_jid) throws Error {
+ this.content_name = content_name;
+ this.senders = senders;
+ this.role = Role.RESPONDER;
+ this.local_full_jid = local_full_jid;
+ this.peer_full_jid = peer_full_jid;
+ this.content_creator = Role.INITIATOR;
+
+ this.content_type = content_type;
+ this.content_params = content_params;
+ this.transport = transport;
+ this.transport_params = transport_params;
+ this.security_precondition = security_precondition;
+ this.security_params = security_params;
+
+ if (transport != null) {
+ this.tried_transport_methods.add(transport.ns_uri);
+ }
+
+ state = State.PENDING;
+ }
+
+ public void set_session(Session session) {
+ this.session = session;
+ this.transport_params.set_content(this);
+ }
+
+ public void accept() {
+ state = State.WANTS_TO_BE_ACCEPTED;
+
+ session.accept_content(this);
+ }
+
+ public void reject() {
+ session.reject_content(this);
+ }
+
+ public void terminate(bool we_terminated, string? reason_name, string? reason_text) {
+ content_params.terminate(we_terminated, reason_name, reason_text);
+ transport_params.dispose();
+
+ foreach (ComponentConnection connection in component_connections.values) {
+ connection.terminate.begin(we_terminated, reason_name, reason_text);
+ }
+ }
+
+ public void modify(Senders new_sender) {
+ session.send_content_modify(this, new_sender);
+ this.senders = new_sender;
+ }
+
+ public void accept_content_modify(Senders senders) {
+ this.senders = senders;
+ }
+
+ internal void handle_content_modify(XmppStream stream, Senders proposed_senders) {
+ senders_modify_incoming(proposed_senders);
+ }
+
+ internal void on_accept(XmppStream stream) {
+ this.transport_params.create_transport_connection(stream, this);
+ this.content_params.accept(stream, session, this);
+ }
+
+ internal void handle_accept(XmppStream stream, ContentNode content_node) {
+ this.transport_params.handle_transport_accept(content_node.transport);
+ this.transport_params.create_transport_connection(stream, this);
+ this.content_params.handle_accept(stream, this.session, this, content_node.description);
+ }
+
+ private async void select_new_transport() {
+ XmppStream stream = session.stream;
+ Transport? new_transport = yield stream.get_module(Module.IDENTITY).select_transport(stream, transport.type_, transport_params.components, peer_full_jid, tried_transport_methods);
+ if (new_transport == null) {
+ session.terminate(ReasonElement.FAILED_TRANSPORT, null, "failed transport");
+ // TODO should we only terminate this content or really the whole session?
+ return;
+ }
+ tried_transport_methods.add(new_transport.ns_uri);
+ transport_params = new_transport.create_transport_parameters(stream, transport_params.components, local_full_jid, peer_full_jid);
+ set_transport_params(transport_params);
+ session.send_transport_replace(this, transport_params);
+ state = State.REPLACING_TRANSPORT;
+ }
+
+ public void handle_transport_accept(XmppStream stream, StanzaNode transport_node, StanzaNode jingle, Iq.Stanza iq) throws IqError {
+ if (state != State.REPLACING_TRANSPORT) {
+ throw new IqError.OUT_OF_ORDER("no outstanding transport-replace request");
+ }
+ if (transport_node.ns_uri != transport.ns_uri) {
+ throw new IqError.BAD_REQUEST("transport-accept with unnegotiated transport method");
+ }
+ transport_params.handle_transport_accept(transport_node);
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+ transport_params.create_transport_connection(stream, this);
+ }
+
+ public void handle_transport_reject(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError {
+ if (state != State.REPLACING_TRANSPORT) {
+ throw new IqError.OUT_OF_ORDER("no outstanding transport-replace request");
+ }
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+ select_new_transport.begin();
+ }
+
+ public void handle_transport_replace(XmppStream stream, StanzaNode transport_node, StanzaNode jingle, Iq.Stanza iq) throws IqError {
+ Transport? transport = stream.get_module(Module.IDENTITY).get_transport(transport_node.ns_uri);
+ TransportParameters? parameters = null;
+ if (transport != null) {
+ // Just parse the transport info for the errors.
+ parameters = transport.parse_transport_parameters(stream, content_type.required_components, local_full_jid, peer_full_jid, transport_node);
+ }
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+ if (state != State.WAITING_FOR_TRANSPORT_REPLACE || transport == null) {
+ session.send_transport_reject(this, transport_node);
+ return;
+ }
+ set_transport_params(parameters);
+ session.send_transport_accept(this, parameters);
+
+ this.transport_params.create_transport_connection(stream, this);
+ }
+
+ public void handle_transport_info(XmppStream stream, StanzaNode transport, StanzaNode jingle, Iq.Stanza iq) throws IqError {
+ this.transport_params.handle_transport_info(transport);
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+ }
+
+ public void on_description_info(XmppStream stream, StanzaNode description, StanzaNode jinglq, Iq.Stanza iq) throws IqError {
+ // TODO: do something.
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+ }
+
+ public void set_transport_connection(ComponentConnection? conn, uint8 component = 1) {
+ debug(@"set_transport_connection: %s, %s, %i, %s, overwrites: %s", this.content_name, this.state.to_string(), component, (conn != null).to_string(), component_connections.has_key(component).to_string());
+
+ if (conn != null) {
+ component_connections[component] = conn;
+ if (transport_params.components == component) {
+ state = State.ACCEPTED;
+ tried_transport_methods.clear();
+ }
+ } else {
+ if (role == Role.INITIATOR) {
+ select_new_transport.begin();
+ } else {
+ state = State.WAITING_FOR_TRANSPORT_REPLACE;
+ }
+ }
+ }
+
+ private void set_transport_params(TransportParameters transport_params) {
+ this.transport_params = transport_params;
+ }
+
+ public ComponentConnection? get_transport_connection(uint8 component = 1) {
+ return component_connections[component];
+ }
+
+ public void send_transport_info(StanzaNode transport) {
+ session.send_transport_info(this, transport);
+ }
+}
+
+public class Xmpp.Xep.Jingle.ContentEncryption : Object {
+ public string encryption_ns { get; set; }
+ public string encryption_name { get; set; }
+ public uint8[] our_key { get; set; }
+ public uint8[] peer_key { get; set; }
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0166_jingle/content_description.vala b/xmpp-vala/src/module/xep/0166_jingle/content_description.vala
new file mode 100644
index 00000000..1a24e52e
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/content_description.vala
@@ -0,0 +1,27 @@
+using Gee;
+using Xmpp.Xep;
+using Xmpp;
+
+namespace Xmpp.Xep.Jingle {
+
+ public interface ContentType : Object {
+ public abstract string ns_uri { get; }
+ public abstract TransportType required_transport_type { get; }
+ public abstract uint8 required_components { get; }
+ public abstract ContentParameters parse_content_parameters(StanzaNode description) throws IqError;
+ }
+
+ public interface ContentParameters : Object {
+ /** Called when the counterpart proposes the content */
+ public abstract async void handle_proposed_content(XmppStream stream, Jingle.Session session, Content content);
+
+ /** Called when we accept the content that was proposed by the counterpart */
+ public abstract void accept(XmppStream stream, Jingle.Session session, Jingle.Content content);
+ /** Called when the counterpart accepts the content that was proposed by us*/
+ public abstract void handle_accept(XmppStream stream, Jingle.Session session, Jingle.Content content, StanzaNode description_node);
+
+ public abstract void terminate(bool we_terminated, string? reason_name, string? reason_text);
+
+ public abstract StanzaNode get_description_node();
+ }
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0166_jingle/content_node.vala b/xmpp-vala/src/module/xep/0166_jingle/content_node.vala
new file mode 100644
index 00000000..7d8d56c8
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/content_node.vala
@@ -0,0 +1,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;
+ }
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0166_jingle/content_security.vala b/xmpp-vala/src/module/xep/0166_jingle/content_security.vala
new file mode 100644
index 00000000..0e10311d
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/content_security.vala
@@ -0,0 +1,18 @@
+using Gee;
+using Xmpp.Xep;
+using Xmpp;
+
+namespace Xmpp.Xep.Jingle {
+
+ public interface SecurityPrecondition : Object {
+ public abstract string security_ns_uri();
+ public abstract SecurityParameters? create_security_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, Object options) throws Jingle.Error;
+ public abstract SecurityParameters? parse_security_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, StanzaNode security) throws IqError;
+ }
+
+ public interface SecurityParameters : Object {
+ public abstract string security_ns_uri();
+ public abstract StanzaNode to_security_stanza_node(XmppStream stream, Jid local_full_jid, Jid peer_full_jid);
+ public abstract IOStream wrap_stream(IOStream stream);
+ }
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0166_jingle/content_transport.vala b/xmpp-vala/src/module/xep/0166_jingle/content_transport.vala
new file mode 100644
index 00000000..2697a01c
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/content_transport.vala
@@ -0,0 +1,29 @@
+namespace Xmpp.Xep.Jingle {
+
+ public interface Transport : Object {
+ public abstract string ns_uri { get; }
+ public async abstract bool is_transport_available(XmppStream stream, uint8 components, Jid full_jid);
+ public abstract TransportType type_ { get; }
+ public abstract int priority { get; }
+ public abstract TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) throws Error;
+ public abstract TransportParameters parse_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws IqError;
+ }
+
+ public enum TransportType {
+ DATAGRAM,
+ STREAMING,
+ }
+
+ // Gets a null `stream` if connection setup was unsuccessful and another
+ // transport method should be tried.
+ public interface TransportParameters : Object {
+ public abstract string ns_uri { get; }
+ public abstract uint8 components { get; }
+
+ public abstract void set_content(Content content);
+ public abstract StanzaNode to_transport_stanza_node(string action_type);
+ public abstract void handle_transport_accept(StanzaNode transport) throws IqError;
+ public abstract void handle_transport_info(StanzaNode transport) throws IqError;
+ public abstract void create_transport_connection(XmppStream stream, Content content);
+ }
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0166_jingle/jingle_flag.vala b/xmpp-vala/src/module/xep/0166_jingle/jingle_flag.vala
new file mode 100644
index 00000000..9f0acd27
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/jingle_flag.vala
@@ -0,0 +1,38 @@
+using Gee;
+using Xmpp;
+
+public class Xmpp.Xep.Jingle.Flag : XmppStreamFlag {
+ public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "jingle");
+
+ public HashMap<string, Session> sessions = new HashMap<string, Session>();
+ public HashMap<string, Promise<Session?>> promises = new HashMap<string, Promise<Session?>>();
+
+ // We might get transport-infos about a session before we finished fully creating the session. (e.g. telepathy outgoing calls)
+ // Thus, we "pre add" the session as soon as possible and can then await it.
+ public void pre_add_session(string sid) {
+ var promise = new Promise<Session?>();
+ promises[sid] = promise;
+ }
+
+ public void add_session(Session session) {
+ if (promises.has_key(session.sid)) {
+ promises[session.sid].set_value(session);
+ promises.unset(session.sid);
+ }
+ sessions[session.sid] = session;
+ }
+
+ public async Session? get_session(string sid) {
+ if (promises.has_key(sid)) {
+ return yield promises[sid].future.wait_async();
+ }
+ return sessions[sid];
+ }
+
+ public void remove_session(string sid) {
+ sessions.unset(sid);
+ }
+
+ public override string get_ns() { return NS_URI; }
+ public override string get_id() { return IDENTITY.id; }
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0166_jingle/jingle_module.vala b/xmpp-vala/src/module/xep/0166_jingle/jingle_module.vala
new file mode 100644
index 00000000..186848f6
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/jingle_module.vala
@@ -0,0 +1,235 @@
+using Gee;
+using Xmpp;
+
+namespace Xmpp.Xep.Jingle {
+
+ public const string NS_URI = "urn:xmpp:jingle:1";
+ private const string ERROR_NS_URI = "urn:xmpp:jingle:errors:1";
+
+ // This module can only be attached to one stream at a time.
+ public class Module : XmppStreamModule, Iq.Handler {
+ public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0166_jingle");
+
+ public signal void session_initiate_received(XmppStream stream, Session session);
+
+ private HashMap<string, ContentType> content_types = new HashMap<string, ContentType>();
+ private HashMap<string, SessionInfoNs> session_info_types = new HashMap<string, SessionInfoNs>();
+ private HashMap<string, Transport> transports = new HashMap<string, Transport>();
+ private HashMap<string, SecurityPrecondition> security_preconditions = new HashMap<string, SecurityPrecondition>();
+
+ public override void attach(XmppStream stream) {
+ stream.add_flag(new Flag());
+ stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI);
+ stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI, this);
+
+ // TODO update stream in all sessions
+ }
+
+ public override void detach(XmppStream stream) {
+ stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
+ stream.get_module(Iq.Module.IDENTITY).unregister_from_namespace(NS_URI, this);
+ }
+
+ public void register_content_type(ContentType content_type) {
+ content_types[content_type.ns_uri] = content_type;
+ }
+
+ public void register_session_info_type(SessionInfoNs info_ns) {
+ session_info_types[info_ns.ns_uri] = info_ns;
+ }
+
+ public ContentType? get_content_type(string ns_uri) {
+ if (!content_types.has_key(ns_uri)) {
+ return null;
+ }
+ return content_types[ns_uri];
+ }
+
+ public SessionInfoNs? get_session_info_type(string ns_uri) {
+ return session_info_types[ns_uri];
+ }
+
+ public void register_transport(Transport transport) {
+ transports[transport.ns_uri] = transport;
+ }
+
+ public Transport? get_transport(string ns_uri) {
+ if (!transports.has_key(ns_uri)) {
+ return null;
+ }
+ return transports[ns_uri];
+ }
+
+ public async Transport? select_transport(XmppStream stream, TransportType type, uint8 components, Jid receiver_full_jid, Set<string> blacklist) {
+ Transport? result = null;
+ foreach (Transport transport in transports.values) {
+ if (transport.type_ != type) {
+ continue;
+ }
+ if (transport.ns_uri in blacklist) {
+ continue;
+ }
+ if (yield transport.is_transport_available(stream, components, receiver_full_jid)) {
+ if (result != null) {
+ if (result.priority >= transport.priority) {
+ continue;
+ }
+ }
+ result = transport;
+ }
+ }
+ return result;
+ }
+
+ public void register_security_precondition(SecurityPrecondition precondition) {
+ security_preconditions[precondition.security_ns_uri()] = precondition;
+ }
+
+ public SecurityPrecondition? get_security_precondition(string? ns_uri) {
+ if (ns_uri == null) return null;
+ if (!security_preconditions.has_key(ns_uri)) {
+ return null;
+ }
+ return security_preconditions[ns_uri];
+ }
+
+ private async bool is_jingle_available(XmppStream stream, Jid full_jid) {
+ bool? has_jingle = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI);
+ return has_jingle != null && has_jingle;
+ }
+
+ public async bool is_available(XmppStream stream, TransportType type, uint8 components, Jid full_jid) {
+ return (yield is_jingle_available(stream, full_jid)) && (yield select_transport(stream, type, components, full_jid, Set.empty())) != null;
+ }
+
+ public async Session create_session(XmppStream stream, Gee.List<Content> contents, Jid receiver_full_jid, string? sid = null) throws Error {
+ if (!yield is_jingle_available(stream, receiver_full_jid)) {
+ throw new Error.NO_SHARED_PROTOCOLS("No Jingle support");
+ }
+ Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid;
+ if (my_jid == null) {
+ throw new Error.GENERAL("Couldn't determine own JID");
+ }
+
+ Session session = new Session.initiate_sent(stream, sid ?? random_uuid(), my_jid, receiver_full_jid);
+ session.terminated.connect((session, stream, _1, _2, _3) => { stream.get_flag(Flag.IDENTITY).remove_session(session.sid); });
+
+ foreach (Content content in contents) {
+ session.insert_content(content);
+ }
+
+ // Build & send session-initiate iq stanza
+ StanzaNode initiate_jingle_iq = new StanzaNode.build("jingle", NS_URI)
+ .add_self_xmlns()
+ .put_attribute("action", "session-initiate")
+ .put_attribute("initiator", my_jid.to_string())
+ .put_attribute("sid", session.sid);
+
+ foreach (Content content in contents) {
+ StanzaNode content_node = new StanzaNode.build("content", NS_URI)
+ .put_attribute("creator", "initiator")
+ .put_attribute("name", content.content_name)
+ .put_attribute("senders", content.senders.to_string())
+ .put_node(content.content_params.get_description_node())
+ .put_node(content.transport_params.to_transport_stanza_node("session-initiate"));
+ if (content.security_params != null) {
+ content_node.put_node(content.security_params.to_security_stanza_node(stream, my_jid, receiver_full_jid));
+ }
+ initiate_jingle_iq.put_node(content_node);
+ }
+
+ Iq.Stanza iq = new Iq.Stanza.set(initiate_jingle_iq) { to=receiver_full_jid };
+
+ stream.get_flag(Flag.IDENTITY).add_session(session);
+ // We might get a follow-up before the ack => add_session before send_iq returns
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => {
+ if (iq.is_error()) warning("Jingle session-initiate got error: %s", iq.stanza.to_string());
+ });
+
+ return session;
+ }
+
+ public async void handle_session_initiate(XmppStream stream, string sid, StanzaNode jingle, Iq.Stanza iq) throws IqError {
+ Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid;
+ if (my_jid == null) {
+ throw new IqError.RESOURCE_CONSTRAINT("Couldn't determine own JID");
+ }
+
+ Session session = new Session.initiate_received(stream, sid, my_jid, iq.from);
+ session.terminated.connect((stream) => { stream.get_flag(Flag.IDENTITY).remove_session(sid); });
+
+ stream.get_flag(Flag.IDENTITY).pre_add_session(session.sid);
+
+ foreach (ContentNode content_node in get_content_nodes(jingle)) {
+ yield session.insert_content_node(content_node, iq.from);
+ }
+
+ stream.get_flag(Flag.IDENTITY).add_session(session);
+
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+
+ session_initiate_received(stream, session);
+ }
+
+ public async void on_iq_set(XmppStream stream, Iq.Stanza iq) {
+ try {
+ yield handle_iq_set(stream, iq);
+ } catch (IqError e) {
+ send_iq_error(e, stream, iq);
+ }
+ }
+
+ public async void handle_iq_set(XmppStream stream, Iq.Stanza iq) throws IqError {
+ StanzaNode? jingle_node = iq.stanza.get_subnode("jingle", NS_URI);
+ if (jingle_node == null) {
+ throw new IqError.BAD_REQUEST("missing jingle node");
+ }
+ string? sid = jingle_node.get_attribute("sid");
+ string? action = jingle_node.get_attribute("action");
+ if (sid == null || action == null) {
+ throw new IqError.BAD_REQUEST("missing jingle node, sid or action");
+ }
+
+ Session? session = yield stream.get_flag(Flag.IDENTITY).get_session(sid);
+ if (action == "session-initiate") {
+ if (session != null) {
+ 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 });
+ return;
+ }
+ yield handle_session_initiate(stream, sid, jingle_node, iq);
+ return;
+ }
+ if (session == null) {
+ StanzaNode unknown_session = new StanzaNode.build("unknown-session", ERROR_NS_URI).add_self_xmlns();
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, new ErrorStanza.item_not_found(unknown_session)) { to=iq.from });
+ return;
+ }
+ session.handle_iq_set(action, jingle_node, iq);
+ }
+
+ public override string get_ns() { return NS_URI; }
+ public override string get_id() { return IDENTITY.id; }
+ }
+
+ void send_iq_error(IqError iq_error, XmppStream stream, Iq.Stanza iq) {
+ ErrorStanza error;
+ if (iq_error is IqError.BAD_REQUEST) {
+ error = new ErrorStanza.bad_request(iq_error.message);
+ } else if (iq_error is IqError.NOT_ACCEPTABLE) {
+ error = new ErrorStanza.not_acceptable(iq_error.message);
+ } else if (iq_error is IqError.NOT_IMPLEMENTED) {
+ error = new ErrorStanza.feature_not_implemented(iq_error.message);
+ } else if (iq_error is IqError.UNSUPPORTED_INFO) {
+ StanzaNode unsupported_info = new StanzaNode.build("unsupported-info", ERROR_NS_URI).add_self_xmlns();
+ error = new ErrorStanza.build(ErrorStanza.TYPE_CANCEL, ErrorStanza.CONDITION_FEATURE_NOT_IMPLEMENTED, iq_error.message, unsupported_info);
+ } else if (iq_error is IqError.OUT_OF_ORDER) {
+ StanzaNode out_of_order = new StanzaNode.build("out-of-order", ERROR_NS_URI).add_self_xmlns();
+ error = new ErrorStanza.build(ErrorStanza.TYPE_MODIFY, ErrorStanza.CONDITION_UNEXPECTED_REQUEST, iq_error.message, out_of_order);
+ } else if (iq_error is IqError.RESOURCE_CONSTRAINT) {
+ error = new ErrorStanza.resource_constraint(iq_error.message);
+ } else {
+ assert_not_reached();
+ }
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, error) { to=iq.from });
+ }
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0166_jingle/jingle_structs.vala b/xmpp-vala/src/module/xep/0166_jingle/jingle_structs.vala
new file mode 100644
index 00000000..0f283e0e
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/jingle_structs.vala
@@ -0,0 +1,73 @@
+using Gee;
+using Xmpp;
+
+namespace Xmpp.Xep.Jingle {
+
+ public errordomain IqError {
+ BAD_REQUEST,
+ NOT_ACCEPTABLE,
+ NOT_IMPLEMENTED,
+ UNSUPPORTED_INFO,
+ OUT_OF_ORDER,
+ RESOURCE_CONSTRAINT,
+ }
+
+ public errordomain Error {
+ GENERAL,
+ BAD_REQUEST,
+ INVALID_PARAMETERS,
+ UNSUPPORTED_TRANSPORT,
+ UNSUPPORTED_SECURITY,
+ NO_SHARED_PROTOCOLS,
+ TRANSPORT_ERROR,
+ }
+
+ public enum Senders {
+ BOTH,
+ INITIATOR,
+ NONE,
+ RESPONDER;
+
+ public string to_string() {
+ switch (this) {
+ case BOTH: return "both";
+ case INITIATOR: return "initiator";
+ case NONE: return "none";
+ case RESPONDER: return "responder";
+ }
+ assert_not_reached();
+ }
+
+ public static Senders parse(string? senders) throws IqError {
+ if (senders == null) return Senders.BOTH;
+ switch (senders) {
+ case "initiator": return Senders.INITIATOR;
+ case "responder": return Senders.RESPONDER;
+ case "both": return Senders.BOTH;
+ }
+ throw new IqError.BAD_REQUEST(@"invalid role $(senders)");
+ }
+ }
+
+ public enum Role {
+ INITIATOR,
+ RESPONDER;
+
+ public string to_string() {
+ switch (this) {
+ case INITIATOR: return "initiator";
+ case RESPONDER: return "responder";
+ }
+ assert_not_reached();
+ }
+
+ public static Role parse(string role) throws IqError {
+ switch (role) {
+ case "initiator": return INITIATOR;
+ case "responder": return RESPONDER;
+ }
+ throw new IqError.BAD_REQUEST(@"invalid role $(role)");
+ }
+ }
+
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0166_jingle/reason_element.vala b/xmpp-vala/src/module/xep/0166_jingle/reason_element.vala
new file mode 100644
index 00000000..4d47d4cd
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/reason_element.vala
@@ -0,0 +1,30 @@
+using Gee;
+using Xmpp;
+
+namespace Xmpp.Xep.Jingle.ReasonElement {
+ public const string ALTERNATIVE_SESSION = "alternative-session";
+ public const string BUSY = "busy";
+ public const string CANCEL = "cancel";
+ public const string CONNECTIVITY_ERROR = "connectivity-error";
+ public const string DECLINE = "decline";
+ public const string EXPIRED = "expired";
+ public const string FAILED_APPLICATION = "failed_application";
+ public const string FAILED_TRANSPORT = "failed_transport";
+ public const string GENERAL_ERROR = "general-error";
+ public const string GONE = "gone";
+ public const string INCOMPATIBLE_PARAMETERS = "incompatible-parameters";
+ public const string MEDIA_ERROR = "media-error";
+ public const string SECURITY_ERROR = "security-error";
+ public const string SUCCESS = "success";
+ public const string TIMEOUT = "timeout";
+ public const string UNSUPPORTED_APPLICATIONS = "unsupported-applications";
+ public const string UNSUPPORTED_TRANSPORTS = "unsupported-transports";
+
+ public const string[] NORMAL_TERMINATE_REASONS = {
+ BUSY,
+ CANCEL,
+ DECLINE,
+ GONE,
+ SUCCESS
+ };
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0166_jingle/session.vala b/xmpp-vala/src/module/xep/0166_jingle/session.vala
new file mode 100644
index 00000000..4d04c8d5
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/session.vala
@@ -0,0 +1,561 @@
+using Gee;
+using Xmpp;
+
+
+public delegate void Xmpp.Xep.Jingle.SessionTerminate(Jid to, string sid, StanzaNode reason);
+
+public class Xmpp.Xep.Jingle.Session : Object {
+
+ public signal void terminated(XmppStream stream, bool we_terminated, string? reason_name, string? reason_text);
+ public signal void additional_content_add_incoming(XmppStream stream, Content content);
+
+ // INITIATE_SENT/INITIATE_RECEIVED -> CONNECTING -> PENDING -> ACTIVE -> ENDED
+ public enum State {
+ INITIATE_SENT,
+ INITIATE_RECEIVED,
+ ACTIVE,
+ ENDED,
+ }
+
+ public XmppStream stream { get; set; }
+ public State state { get; set; }
+ public string sid { get; private set; }
+ public Jid local_full_jid { get; private set; }
+ public Jid peer_full_jid { get; private set; }
+ public bool we_initiated { get; private set; }
+
+ public HashMap<string, Content> contents_map = new HashMap<string, Content>();
+ public Gee.List<Content> contents = new ArrayList<Content>(); // Keep the order contents
+
+ public SecurityParameters? security { get { return contents.to_array()[0].security_params; } }
+
+ public Session.initiate_sent(XmppStream stream, string sid, Jid local_full_jid, Jid peer_full_jid) {
+ this.stream = stream;
+ this.sid = sid;
+ this.local_full_jid = local_full_jid;
+ this.peer_full_jid = peer_full_jid;
+ this.state = State.INITIATE_SENT;
+ this.we_initiated = true;
+ }
+
+ public Session.initiate_received(XmppStream stream, string sid, Jid local_full_jid, Jid peer_full_jid) {
+ this.stream = stream;
+ this.sid = sid;
+ this.local_full_jid = local_full_jid;
+ this.peer_full_jid = peer_full_jid;
+ this.state = State.INITIATE_RECEIVED;
+ this.we_initiated = false;
+ }
+
+ public void handle_iq_set(string action, StanzaNode jingle, Iq.Stanza iq) throws IqError {
+
+ if (action.has_prefix("session-")) {
+ switch (action) {
+ case "session-accept":
+ Gee.List<ContentNode> content_nodes = get_content_nodes(jingle);
+
+ if (state != State.INITIATE_SENT) {
+ throw new IqError.OUT_OF_ORDER("got session-accept while not waiting for one");
+ }
+ handle_session_accept(content_nodes, jingle, iq);
+ break;
+ case "session-info":
+ handle_session_info.begin(jingle, iq);
+ break;
+ case "session-terminate":
+ handle_session_terminate(jingle, iq);
+ break;
+ default:
+ throw new IqError.BAD_REQUEST("invalid action");
+ }
+
+
+ } else if (action.has_prefix("content-")) {
+ switch (action) {
+ case "content-accept":
+ ContentNode content_node = get_single_content_node(jingle);
+ handle_content_accept(content_node);
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+ break;
+ case "content-add":
+ ContentNode content_node = get_single_content_node(jingle);
+ insert_content_node.begin(content_node, peer_full_jid);
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+ break;
+ case "content-modify":
+ handle_content_modify(stream, jingle, iq);
+ break;
+ case "content-reject":
+ case "content-remove":
+ throw new IqError.NOT_IMPLEMENTED(@"$(action) is not implemented");
+ default:
+ throw new IqError.BAD_REQUEST("invalid action");
+ }
+
+
+ } else if (action.has_prefix("transport-")) {
+ ContentNode content_node = get_single_content_node(jingle);
+ if (!contents_map.has_key(content_node.name)) {
+ throw new IqError.BAD_REQUEST("unknown content");
+ }
+
+ if (content_node.transport == null) {
+ throw new IqError.BAD_REQUEST("missing transport node");
+ }
+
+ Content content = contents_map[content_node.name];
+
+ if (content_node.creator != content.content_creator) {
+ throw new IqError.BAD_REQUEST("unknown content; creator");
+ }
+
+ switch (action) {
+ case "transport-accept":
+ content.handle_transport_accept(stream, content_node.transport, jingle, iq);
+ break;
+ case "transport-info":
+ content.handle_transport_info(stream, content_node.transport, jingle, iq);
+ break;
+ case "transport-reject":
+ content.handle_transport_reject(stream, jingle, iq);
+ break;
+ case "transport-replace":
+ content.handle_transport_replace(stream, content_node.transport, jingle, iq);
+ break;
+ default:
+ throw new IqError.BAD_REQUEST("invalid action");
+ }
+
+
+ } else if (action == "description-info") {
+ ContentNode content_node = get_single_content_node(jingle);
+ if (!contents_map.has_key(content_node.name)) {
+ throw new IqError.BAD_REQUEST("unknown content");
+ }
+
+ Content content = contents_map[content_node.name];
+
+ if (content_node.creator != content.content_creator) {
+ throw new IqError.BAD_REQUEST("unknown content; creator");
+ }
+
+ content.on_description_info(stream, content_node.description, jingle, iq);
+ } else if (action == "security-info") {
+ throw new IqError.NOT_IMPLEMENTED(@"$(action) is not implemented");
+
+
+ } else {
+ throw new IqError.BAD_REQUEST("invalid action");
+ }
+ }
+
+ internal void insert_content(Content content) {
+ this.contents_map[content.content_name] = content;
+ this.contents.add(content);
+ content.set_session(this);
+ }
+
+ internal async void insert_content_node(ContentNode content_node, Jid peer_full_jid) throws IqError {
+ if (content_node.description == null || content_node.transport == null) {
+ throw new IqError.BAD_REQUEST("missing description or transport node");
+ }
+
+ Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid;
+
+ Transport? transport = stream.get_module(Jingle.Module.IDENTITY).get_transport(content_node.transport.ns_uri);
+ ContentType? content_type = stream.get_module(Jingle.Module.IDENTITY).get_content_type(content_node.description.ns_uri);
+
+ if (content_type == null) {
+ // TODO(hrxi): how do we signal an unknown content type?
+ throw new IqError.NOT_IMPLEMENTED("unknown content type");
+ }
+
+ TransportParameters? transport_params = null;
+ if (transport != null) {
+ transport_params = transport.parse_transport_parameters(stream, content_type.required_components, my_jid, peer_full_jid, content_node.transport);
+ } else {
+ // terminate the session below
+ }
+
+ ContentParameters content_params = content_type.parse_content_parameters(content_node.description);
+
+ SecurityPrecondition? precondition = content_node.security != null ? stream.get_module(Jingle.Module.IDENTITY).get_security_precondition(content_node.security.ns_uri) : null;
+ SecurityParameters? security_params = null;
+ if (precondition != null) {
+ debug("Using precondition %s", precondition.security_ns_uri());
+ security_params = precondition.parse_security_parameters(stream, my_jid, peer_full_jid, content_node.security);
+ } else if (content_node.security != null) {
+ throw new IqError.NOT_IMPLEMENTED("unknown security precondition");
+ }
+
+ TransportType type = content_type.required_transport_type;
+
+ if (transport == null || transport.type_ != type) {
+ terminate(ReasonElement.UNSUPPORTED_TRANSPORTS, null, "unsupported transports");
+ throw new IqError.NOT_IMPLEMENTED("unsupported transports");
+ }
+
+ Content content = new Content.initiate_received(content_node.name, content_node.senders,
+ content_type, content_params,
+ transport, transport_params,
+ precondition, security_params,
+ my_jid, peer_full_jid);
+ insert_content(content);
+
+ yield content_params.handle_proposed_content(stream, this, content);
+
+ if (this.state == State.ACTIVE) {
+ additional_content_add_incoming(stream, content);
+ }
+ }
+
+ public async void add_content(Content content) {
+ insert_content(content);
+
+ StanzaNode content_add_node = new StanzaNode.build("jingle", NS_URI)
+ .add_self_xmlns()
+ .put_attribute("action", "content-add")
+ .put_attribute("sid", sid)
+ .put_node(new StanzaNode.build("content", NS_URI)
+ .put_attribute("creator", "initiator")
+ .put_attribute("name", content.content_name)
+ .put_attribute("senders", content.senders.to_string())
+ .put_node(content.content_params.get_description_node())
+ .put_node(content.transport_params.to_transport_stanza_node("content-add")));
+
+ Iq.Stanza iq = new Iq.Stanza.set(content_add_node) { to=peer_full_jid };
+ yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
+ }
+
+ private void handle_content_accept(ContentNode content_node) throws IqError {
+ if (content_node.description == null || content_node.transport == null) throw new IqError.BAD_REQUEST("missing description or transport node");
+ if (!contents_map.has_key(content_node.name)) throw new IqError.BAD_REQUEST("unknown content");
+
+ Content content = contents_map[content_node.name];
+
+ if (content_node.creator != content.content_creator) warning("Counterpart accepts content with an unexpected `creator`");
+ if (content_node.senders != content.senders) warning("Counterpart accepts content with an unexpected `senders`");
+ if (content_node.transport.ns_uri != content.transport_params.ns_uri) throw new IqError.BAD_REQUEST("session-accept with unnegotiated transport method");
+
+ content.handle_accept(stream, content_node);
+ }
+
+ private void handle_content_modify(XmppStream stream, StanzaNode jingle_node, Iq.Stanza iq) throws IqError {
+ ContentNode content_node = get_single_content_node(jingle_node);
+
+ Content? content = contents_map[content_node.name];
+
+ if (content == null) throw new IqError.BAD_REQUEST("no such content");
+ if (content_node.creator != content.content_creator) throw new IqError.BAD_REQUEST("mismatching creator");
+
+ Iq.Stanza result_iq = new Iq.Stanza.result(iq) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, result_iq);
+
+ content.handle_content_modify(stream, content_node.senders);
+ }
+
+ private void handle_session_accept(Gee.List<ContentNode> content_nodes, StanzaNode jingle, Iq.Stanza iq) throws IqError {
+ string? responder_str = jingle.get_attribute("responder");
+ Jid responder = iq.from;
+ if (responder_str != null) {
+ try {
+ responder = new Jid(responder_str);
+ } catch (InvalidJidError e) {
+ warning("Received invalid session accept: %s", e.message);
+ }
+ }
+ // TODO(hrxi): more sanity checking, perhaps replace who we're talking to
+ if (!responder.is_full()) {
+ throw new IqError.BAD_REQUEST("invalid responder JID");
+ }
+ foreach (ContentNode content_node in content_nodes) {
+ handle_content_accept(content_node);
+ }
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+
+ state = State.ACTIVE;
+ }
+
+ private void handle_session_terminate(StanzaNode jingle, Iq.Stanza iq) throws IqError {
+ string? reason_text = null;
+ string? reason_name = null;
+ StanzaNode? reason_node = iq.stanza.get_deep_subnode(NS_URI + ":jingle", NS_URI + ":reason");
+ if (reason_node != null) {
+ if (reason_node.sub_nodes.size > 2) warning("Jingle session-terminate reason node w/ >2 subnodes: %s", iq.stanza.to_string());
+
+ StanzaNode? specific_reason_node = null;
+ StanzaNode? text_node = null;
+ foreach (StanzaNode node in reason_node.sub_nodes) {
+ if (node.name == "text") {
+ text_node = node;
+ } else if (node.ns_uri == NS_URI) {
+ specific_reason_node = node;
+ }
+ }
+ reason_name = specific_reason_node != null ? specific_reason_node.name : null;
+ reason_text = text_node != null ? text_node.get_string_content() : null;
+
+ if (reason_name != null && !(specific_reason_node.name in ReasonElement.NORMAL_TERMINATE_REASONS)) {
+ warning("Jingle session terminated: %s : %s", reason_name ?? "", reason_text ?? "");
+ } else {
+ debug("Jingle session terminated: %s : %s", reason_name ?? "", reason_text ?? "");
+ }
+ }
+
+ foreach (Content content in contents) {
+ content.terminate(false, reason_name, reason_text);
+ }
+
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+ // TODO(hrxi): also handle presence type=unavailable
+
+ state = State.ENDED;
+ terminated(stream, false, reason_name, reason_text);
+ }
+
+ private async void handle_session_info(StanzaNode jingle, Iq.Stanza iq) throws IqError {
+ StanzaNode? info = get_single_node_anyns(jingle);
+ if (info == null) {
+ // Jingle session ping
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
+ return;
+ }
+ SessionInfoNs? info_ns = stream.get_module(Module.IDENTITY).get_session_info_type(info.ns_uri);
+ if (info_ns == null) {
+ throw new IqError.UNSUPPORTED_INFO("unknown session-info namespace");
+ }
+ info_ns.handle_content_session_info(stream, this, info, iq);
+
+ Iq.Stanza result_iq = new Iq.Stanza.result(iq) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, result_iq);
+ }
+
+ private void accept() {
+ if (state != State.INITIATE_RECEIVED) critical("Accepting a stream, but we're the initiator");
+
+ StanzaNode jingle = new StanzaNode.build("jingle", NS_URI)
+ .add_self_xmlns()
+ .put_attribute("action", "session-accept")
+ .put_attribute("sid", sid);
+ foreach (Content content in contents) {
+ StanzaNode content_node = new StanzaNode.build("content", NS_URI)
+ .put_attribute("creator", "initiator")
+ .put_attribute("name", content.content_name)
+ .put_attribute("senders", content.senders.to_string())
+ .put_node(content.content_params.get_description_node())
+ .put_node(content.transport_params.to_transport_stanza_node("session-accept"));
+ jingle.put_node(content_node);
+ }
+
+ Iq.Stanza iq = new Iq.Stanza.set(jingle) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
+
+
+ foreach (Content content2 in contents) {
+ content2.on_accept(stream);
+ }
+
+ state = State.ACTIVE;
+ }
+
+ internal void accept_content(Content content) {
+ if (state == State.INITIATE_RECEIVED) {
+ bool all_accepted = true;
+ foreach (Content c in contents) {
+ if (c.state != Content.State.WANTS_TO_BE_ACCEPTED) {
+ all_accepted = false;
+ }
+ }
+ if (all_accepted) {
+ accept();
+ }
+ } else if (state == State.ACTIVE) {
+ StanzaNode content_accept_node = new StanzaNode.build("jingle", NS_URI)
+ .add_self_xmlns()
+ .put_attribute("action", "content-accept")
+ .put_attribute("sid", sid)
+ .put_node(new StanzaNode.build("content", NS_URI)
+ .put_attribute("creator", "initiator")
+ .put_attribute("name", content.content_name)
+ .put_attribute("senders", content.senders.to_string())
+ .put_node(content.content_params.get_description_node())
+ .put_node(content.transport_params.to_transport_stanza_node("content-accept")));
+
+ Iq.Stanza iq = new Iq.Stanza.set(content_accept_node) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
+
+ content.on_accept(stream);
+ }
+ }
+
+ private void reject() {
+ if (state != State.INITIATE_RECEIVED) critical("Accepting a stream, but we're the initiator");
+ terminate(ReasonElement.DECLINE, null, "declined");
+ }
+
+ internal void reject_content(Content content) {
+ if (state == State.INITIATE_RECEIVED) {
+ reject();
+ } else {
+ warning("not really handeling content rejects");
+ }
+ }
+
+ public void set_application_error(StanzaNode? application_reason = null) {
+ terminate(ReasonElement.FAILED_APPLICATION, null, "application error");
+ }
+
+ public void terminate(string? reason_name, string? reason_text, string? local_reason) {
+ if (state == State.ENDED) return;
+
+ if (state == State.ACTIVE) {
+ string reason_str;
+ if (local_reason != null) {
+ reason_str = @"local session-terminate: $(local_reason)";
+ } else {
+ reason_str = "local session-terminate";
+ }
+ foreach (Content content in contents) {
+ content.terminate(true, reason_name, reason_text);
+ }
+ }
+
+ StanzaNode terminate_iq = new StanzaNode.build("jingle", NS_URI)
+ .add_self_xmlns()
+ .put_attribute("action", "session-terminate")
+ .put_attribute("sid", sid);
+ if (reason_name != null || reason_text != null) {
+ StanzaNode reason_node = new StanzaNode.build("reason", NS_URI);
+ if (reason_name != null) {
+ reason_node.put_node(new StanzaNode.build(reason_name, NS_URI));
+ }
+ if (reason_text != null) {
+ reason_node.put_node(new StanzaNode.text(reason_text));
+ }
+ terminate_iq.put_node(reason_node);
+ }
+ Iq.Stanza iq = new Iq.Stanza.set(terminate_iq) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
+
+ state = State.ENDED;
+ terminated(stream, true, reason_name, reason_text);
+ }
+
+ internal void send_session_info(StanzaNode child_node) {
+ if (state == State.ENDED) return;
+
+ StanzaNode node = new StanzaNode.build("jingle", NS_URI).add_self_xmlns()
+ .put_attribute("action", "session-info")
+ .put_attribute("sid", sid)
+ // TODO put `initiator`?
+ .put_node(child_node);
+ Iq.Stanza iq = new Iq.Stanza.set(node) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
+ }
+
+ internal void send_content_modify(Content content, Senders senders) {
+ if (state == State.ENDED) return;
+
+ StanzaNode node = new StanzaNode.build("jingle", NS_URI).add_self_xmlns()
+ .put_attribute("action", "content-modify")
+ .put_attribute("sid", sid)
+ .put_node(new StanzaNode.build("content", NS_URI)
+ .put_attribute("creator", content.content_creator.to_string())
+ .put_attribute("name", content.content_name)
+ .put_attribute("senders", senders.to_string()));
+ Iq.Stanza iq = new Iq.Stanza.set(node) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
+ }
+
+ internal void send_transport_accept(Content content, TransportParameters transport_params) {
+ if (state == State.ENDED) return;
+
+ StanzaNode jingle_response = new StanzaNode.build("jingle", NS_URI)
+ .add_self_xmlns()
+ .put_attribute("action", "transport-accept")
+ .put_attribute("sid", sid)
+ .put_node(new StanzaNode.build("content", NS_URI)
+ .put_attribute("creator", "initiator")
+ .put_attribute("name", content.content_name)
+ .put_node(transport_params.to_transport_stanza_node("transport-accept"))
+ );
+ Iq.Stanza iq_response = new Iq.Stanza.set(jingle_response) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq_response);
+ }
+
+ internal void send_transport_replace(Content content, TransportParameters transport_params) {
+ if (state == State.ENDED) return;
+
+ StanzaNode jingle = new StanzaNode.build("jingle", NS_URI)
+ .add_self_xmlns()
+ .put_attribute("action", "transport-replace")
+ .put_attribute("sid", sid)
+ .put_node(new StanzaNode.build("content", NS_URI)
+ .put_attribute("creator", "initiator")
+ .put_attribute("name", content.content_name)
+ .put_node(transport_params.to_transport_stanza_node("transport-replace"))
+ );
+ Iq.Stanza iq = new Iq.Stanza.set(jingle) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
+ }
+
+ internal void send_transport_reject(Content content, StanzaNode transport_node) {
+ if (state == State.ENDED) return;
+
+ StanzaNode jingle_response = new StanzaNode.build("jingle", NS_URI)
+ .add_self_xmlns()
+ .put_attribute("action", "transport-reject")
+ .put_attribute("sid", sid)
+ .put_node(new StanzaNode.build("content", NS_URI)
+ .put_attribute("creator", "initiator")
+ .put_attribute("name", content.content_name)
+ .put_node(transport_node)
+ );
+ Iq.Stanza iq_response = new Iq.Stanza.set(jingle_response) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq_response);
+ }
+
+ internal void send_transport_info(Content content, StanzaNode transport) {
+ if (state == State.ENDED) return;
+
+ StanzaNode jingle = new StanzaNode.build("jingle", NS_URI)
+ .add_self_xmlns()
+ .put_attribute("action", "transport-info")
+ .put_attribute("sid", sid)
+ .put_node(new StanzaNode.build("content", NS_URI)
+ .put_attribute("creator", "initiator")
+ .put_attribute("name", content.content_name)
+ .put_node(transport)
+ );
+ Iq.Stanza iq = new Iq.Stanza.set(jingle) { to=peer_full_jid };
+ stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
+ }
+
+ public bool senders_include_us(Senders senders) {
+ switch (senders) {
+ case Senders.BOTH:
+ return true;
+ case Senders.NONE:
+ return false;
+ case Senders.INITIATOR:
+ return we_initiated;
+ case Senders.RESPONDER:
+ return !we_initiated;
+ }
+ assert_not_reached();
+ }
+
+ public bool senders_include_counterpart(Senders senders) {
+ switch (senders) {
+ case Senders.BOTH:
+ return true;
+ case Senders.NONE:
+ return false;
+ case Senders.INITIATOR:
+ return !we_initiated;
+ case Senders.RESPONDER:
+ return we_initiated;
+ }
+ assert_not_reached();
+ }
+} \ No newline at end of file
diff --git a/xmpp-vala/src/module/xep/0166_jingle/session_info.vala b/xmpp-vala/src/module/xep/0166_jingle/session_info.vala
new file mode 100644
index 00000000..fcf7584f
--- /dev/null
+++ b/xmpp-vala/src/module/xep/0166_jingle/session_info.vala
@@ -0,0 +1,12 @@
+using Gee;
+using Xmpp.Xep;
+using Xmpp;
+
+namespace Xmpp.Xep.Jingle {
+
+ public interface SessionInfoNs : Object {
+ public abstract string ns_uri { get; }
+
+ public abstract void handle_content_session_info(XmppStream stream, Session session, StanzaNode info, Iq.Stanza iq) throws IqError;
+ }
+} \ No newline at end of file