| 1 |
namespace Xmpp.Bind { |
| 2 |
private const string NS_URI = "urn:ietf:params:xml:ns:xmpp-bind"; |
| 3 |
|
| 4 |
/** The parties to a stream MUST consider resource binding as mandatory-to-negotiate. (RFC6120 7.3.1) */ |
| 5 |
public class Module : XmppStreamNegotiationModule { |
| 6 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "bind_module"); |
| 7 |
|
| 8 |
public string? requested_resource { get; set; } |
| 9 |
|
| 10 |
public signal void bound_to_resource(XmppStream stream, Jid my_jid); |
| 11 |
|
| 12 |
public Module(string? requested_resource) { |
| 13 |
this.requested_resource = requested_resource; |
| 14 |
} |
| 15 |
|
| 16 |
public void iq_response_stanza(XmppStream stream, Iq.Stanza iq) { |
| 17 |
var flag = stream.get_flag(Flag.IDENTITY); |
| 18 |
if (flag == null || flag.finished) return; |
| 19 |
|
| 20 |
if (iq.type_ == Iq.Stanza.TYPE_RESULT) { |
| 21 |
try { |
| 22 |
flag.my_jid = new Jid(iq.stanza.get_subnode("jid", NS_URI, true).get_string_content()); |
| 23 |
flag.finished = true; |
| 24 |
bound_to_resource(stream, flag.my_jid); |
| 25 |
} catch (InvalidJidError e) { |
| 26 |
warning("Received invalid Jid when binding: %s", e.message); |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public void received_features_node(XmppStream stream) { |
| 32 |
if (stream.is_setup_needed()) return; |
| 33 |
if (stream.is_negotiation_active()) return; |
| 34 |
|
| 35 |
var bind = stream.features.get_subnode("bind", NS_URI); |
| 36 |
if (bind != null) { |
| 37 |
var flag = new Flag(); |
| 38 |
StanzaNode bind_node = new StanzaNode.build("bind", NS_URI).add_self_xmlns(); |
| 39 |
if (requested_resource != null) { |
| 40 |
bind_node.put_node(new StanzaNode.build("resource", NS_URI).put_node(new StanzaNode.text(requested_resource))); |
| 41 |
} |
| 42 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.set(bind_node), iq_response_stanza); |
| 43 |
stream.add_flag(flag); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public override void attach(XmppStream stream) { |
| 48 |
stream.received_features_node.connect(this.received_features_node); |
| 49 |
} |
| 50 |
|
| 51 |
public override void detach(XmppStream stream) { |
| 52 |
stream.received_features_node.disconnect(this.received_features_node); |
| 53 |
} |
| 54 |
|
| 55 |
public override bool mandatory_outstanding(XmppStream stream) { |
| 56 |
return !stream.has_flag(Flag.IDENTITY) || !stream.get_flag(Flag.IDENTITY).finished; |
| 57 |
} |
| 58 |
|
| 59 |
public override bool negotiation_active(XmppStream stream) { |
| 60 |
return stream.has_flag(Flag.IDENTITY) && !stream.get_flag(Flag.IDENTITY).finished; |
| 61 |
} |
| 62 |
|
| 63 |
public override string get_ns() { return NS_URI; } |
| 64 |
public override string get_id() { return IDENTITY.id; } |
| 65 |
} |
| 66 |
|
| 67 |
public class Flag : XmppStreamFlag { |
| 68 |
public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "bind"); |
| 69 |
public Jid? my_jid; |
| 70 |
public bool finished = false; |
| 71 |
|
| 72 |
public static Jid? get_my_jid(XmppStream stream) { |
| 73 |
return stream.get_flag(IDENTITY).my_jid; |
| 74 |
} |
| 75 |
|
| 76 |
public override string get_ns() { return NS_URI; } |
| 77 |
public override string get_id() { return IDENTITY.id; } |
| 78 |
} |
| 79 |
} |