aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/bind.vala
diff options
context:
space:
mode:
Diffstat (limited to 'xmpp-vala/src/module/bind.vala')
-rw-r--r--xmpp-vala/src/module/bind.vala93
1 files changed, 93 insertions, 0 deletions
diff --git a/xmpp-vala/src/module/bind.vala b/xmpp-vala/src/module/bind.vala
new file mode 100644
index 00000000..d01fda7a
--- /dev/null
+++ b/xmpp-vala/src/module/bind.vala
@@ -0,0 +1,93 @@
+using Xmpp.Core;
+
+namespace Xmpp.Bind {
+ private const string NS_URI = "urn:ietf:params:xml:ns:xmpp-bind";
+
+ /** The parties to a stream MUST consider resource binding as mandatory-to-negotiate. (RFC6120 7.3.1) */
+ public class Module : XmppStreamNegotiationModule {
+ public const string ID = "bind_module";
+
+ private string requested_resource;
+
+ public signal void bound_to_resource(XmppStream stream, string my_jid);
+
+ public Module(string requested_resource) {
+ this.requested_resource = requested_resource;
+ }
+
+ public void iq_response_stanza(XmppStream stream, Iq.Stanza iq) {
+ var flag = Flag.get_flag(stream);
+ if (flag == null || flag.finished) return;
+
+ if (iq.type_ == Iq.Stanza.TYPE_RESULT) {
+ flag.my_jid = iq.stanza.get_subnode("jid", NS_URI, true).get_string_content();
+ flag.finished = true;
+ bound_to_resource(stream, flag.my_jid);
+ }
+ }
+
+ public void received_features_node(XmppStream stream) {
+ if (stream.is_setup_needed()) return;
+
+ var bind = stream.features.get_subnode("bind", NS_URI);
+ if (bind != null) {
+ var flag = new Flag();
+ StanzaNode bind_node = new StanzaNode.build("bind", NS_URI).add_self_xmlns()
+ .put_node(new StanzaNode.build("resource", NS_URI).put_node(new StanzaNode.text(requested_resource)));
+ Iq.Module.get_module(stream).send_iq(stream, new Iq.Stanza.set(bind_node), new IqResponseListenerImpl());
+ stream.add_flag(flag);
+ }
+ }
+
+ private class IqResponseListenerImpl : Iq.ResponseListener, Object {
+ public void on_result(XmppStream stream, Iq.Stanza iq) {
+ Bind.Module.get_module(stream).iq_response_stanza(stream, iq);
+ }
+ }
+
+ public override void attach(XmppStream stream) {
+ Iq.Module.require(stream);
+ stream.received_features_node.connect(this.received_features_node);
+ }
+
+ public override void detach(XmppStream stream) {
+ stream.received_features_node.disconnect(this.received_features_node);
+ }
+
+ public static Module? get_module(XmppStream stream) {
+ return (Module?) stream.get_module(NS_URI, ID);
+ }
+
+ public static void require(XmppStream stream) {
+ if (get_module(stream) == null) stream.add_module(new Bind.Module(""));
+ }
+
+ public override bool mandatory_outstanding(XmppStream stream) {
+ return !Flag.has_flag(stream) || !Flag.get_flag(stream).finished;
+ }
+
+ public override bool negotiation_active(XmppStream stream) {
+ return Flag.has_flag(stream) && !Flag.get_flag(stream).finished;
+ }
+
+ public override string get_ns() { return NS_URI; }
+ public override string get_id() { return ID; }
+ }
+
+ public class Flag : XmppStreamFlag {
+ public const string ID = "bind";
+ public string? my_jid;
+ public bool finished = false;
+
+ public static Flag? get_flag(XmppStream stream) {
+ return (Flag?) stream.get_flag(NS_URI, ID);
+ }
+
+ public static bool has_flag(XmppStream stream) {
+ return get_flag(stream) != null;
+ }
+
+ public override string get_ns() { return NS_URI; }
+ public override string get_id() { return ID; }
+ }
+}