| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Iq { |
| 4 |
private const string NS_URI = "jabber:client"; |
| 5 |
|
| 6 |
public class Module : XmppStreamNegotiationModule { |
| 7 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "iq_module"); |
| 8 |
|
| 9 |
public signal void preprocess_incoming_iq_set_get(XmppStream stream, Stanza iq_stanza); |
| 10 |
public signal void preprocess_outgoing_iq_set_get(XmppStream stream, Stanza iq_stanza); |
| 11 |
|
| 12 |
private HashMap<string, ResponseListener> responseListeners = new HashMap<string, ResponseListener>(); |
| 13 |
private HashMap<string, ArrayList<Handler>> namespaceRegistrants = new HashMap<string, ArrayList<Handler>>(); |
| 14 |
|
| 15 |
public async Iq.Stanza send_iq_async(XmppStream stream, Iq.Stanza iq, int io_priority = Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { |
| 16 |
assert(iq.type_ == Iq.Stanza.TYPE_GET || iq.type_ == Iq.Stanza.TYPE_SET); |
| 17 |
|
| 18 |
preprocess_outgoing_iq_set_get(stream, iq); |
| 19 |
Iq.Stanza? return_stanza = null; |
| 20 |
responseListeners[iq.id] = new ResponseListener((_, result_iq) => { |
| 21 |
return_stanza = result_iq; |
| 22 |
Idle.add(send_iq_async.callback); |
| 23 |
}); |
| 24 |
stream.write_async(iq.stanza, io_priority, cancellable); |
| 25 |
yield; |
| 26 |
cancellable.set_error_if_cancelled(); |
| 27 |
return return_stanza; |
| 28 |
} |
| 29 |
|
| 30 |
public delegate void OnResult(XmppStream stream, Iq.Stanza iq); |
| 31 |
public void send_iq(XmppStream stream, Iq.Stanza iq, owned OnResult? listener = null, int io_priority = Priority.DEFAULT) { |
| 32 |
preprocess_outgoing_iq_set_get(stream, iq); |
| 33 |
stream.write(iq.stanza, io_priority); |
| 34 |
if (listener != null) { |
| 35 |
responseListeners[iq.id] = new ResponseListener((owned) listener); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public void register_for_namespace(string namespace, Handler module) { |
| 40 |
if (!namespaceRegistrants.has_key(namespace)) { |
| 41 |
namespaceRegistrants.set(namespace, new ArrayList<Handler>()); |
| 42 |
} |
| 43 |
namespaceRegistrants[namespace].add(module); |
| 44 |
} |
| 45 |
|
| 46 |
public void unregister_from_namespace(string namespace, Handler module) { |
| 47 |
ArrayList<Handler>? handlers = namespaceRegistrants[namespace]; |
| 48 |
if (handlers != null) handlers.remove(module); |
| 49 |
} |
| 50 |
|
| 51 |
public override void attach(XmppStream stream) { |
| 52 |
stream.received_iq_stanza.connect(on_received_iq_stanza); |
| 53 |
} |
| 54 |
|
| 55 |
public override void detach(XmppStream stream) { |
| 56 |
stream.received_iq_stanza.disconnect(on_received_iq_stanza); |
| 57 |
} |
| 58 |
|
| 59 |
public override bool mandatory_outstanding(XmppStream stream) { return false; } |
| 60 |
|
| 61 |
public override bool negotiation_active(XmppStream stream) { return false; } |
| 62 |
|
| 63 |
public override string get_ns() { return NS_URI; } |
| 64 |
public override string get_id() { return IDENTITY.id; } |
| 65 |
|
| 66 |
private async void on_received_iq_stanza(XmppStream stream, StanzaNode node) { |
| 67 |
Iq.Stanza iq = new Iq.Stanza.from_stanza(node, stream.has_flag(Bind.Flag.IDENTITY) ? stream.get_flag(Bind.Flag.IDENTITY).my_jid : null); |
| 68 |
|
| 69 |
if (iq.type_ == Iq.Stanza.TYPE_RESULT || iq.is_error()) { |
| 70 |
if (responseListeners.has_key(iq.id)) { |
| 71 |
ResponseListener? listener = responseListeners.get(iq.id); |
| 72 |
if (listener != null) { |
| 73 |
listener.on_result(stream, iq); |
| 74 |
} |
| 75 |
responseListeners.unset(iq.id); |
| 76 |
} |
| 77 |
} else { |
| 78 |
Gee.List<StanzaNode> children = node.get_all_subnodes(); |
| 79 |
if (children.size == 1 && namespaceRegistrants.has_key(children[0].ns_uri)) { |
| 80 |
preprocess_incoming_iq_set_get(stream, iq); |
| 81 |
Gee.List<Handler> handlers = namespaceRegistrants[children[0].ns_uri]; |
| 82 |
foreach (Handler handler in handlers) { |
| 83 |
if (iq.type_ == Iq.Stanza.TYPE_GET) { |
| 84 |
yield handler.on_iq_get(stream, iq); |
| 85 |
} else if (iq.type_ == Iq.Stanza.TYPE_SET) { |
| 86 |
yield handler.on_iq_set(stream, iq); |
| 87 |
} |
| 88 |
} |
| 89 |
} else { |
| 90 |
// Send error if we don't handle the NS of the IQ get/set payload (RFC6120 10.3.3 (2)) |
| 91 |
Iq.Stanza unavailable_error = new Iq.Stanza.error(iq, new ErrorStanza.service_unavailable()) { to=iq.from }; |
| 92 |
send_iq(stream, unavailable_error); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
private class ResponseListener { |
| 98 |
public OnResult on_result { get; private owned set; } |
| 99 |
|
| 100 |
public ResponseListener(owned OnResult on_result) { |
| 101 |
this.on_result = (owned) on_result; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public interface Handler : Object { |
| 107 |
public async virtual void on_iq_get(XmppStream stream, Iq.Stanza iq) { |
| 108 |
Iq.Stanza bad_request = new Iq.Stanza.error(iq, new ErrorStanza.bad_request("unexpected IQ get for this namespace")); |
| 109 |
stream.get_module(Module.IDENTITY).send_iq(stream, bad_request); |
| 110 |
} |
| 111 |
public async virtual void on_iq_set(XmppStream stream, Iq.Stanza iq) { |
| 112 |
Iq.Stanza bad_request = new Iq.Stanza.error(iq, new ErrorStanza.bad_request("unexpected IQ set for this namespace")); |
| 113 |
stream.get_module(Module.IDENTITY).send_iq(stream, bad_request); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
} |