| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Xep.ServiceDiscovery { |
| 4 |
|
| 5 |
public const string NS_URI = "http://jabber.org/protocol/disco"; |
| 6 |
public const string NS_URI_INFO = NS_URI + "#info"; |
| 7 |
public const string NS_URI_ITEMS = NS_URI + "#items"; |
| 8 |
|
| 9 |
public class Module : XmppStreamModule, Iq.Handler { |
| 10 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0030_service_discovery_module"); |
| 11 |
|
| 12 |
private HashMap<Jid, Future<InfoResult?>> active_info_requests = new HashMap<Jid, Future<InfoResult?>>(Jid.hash_func, Jid.equals_func); |
| 13 |
|
| 14 |
public Identity own_identity; |
| 15 |
public CapsCache cache; |
| 16 |
|
| 17 |
public Module.with_identity(string category, string type, string? name = null) { |
| 18 |
this.own_identity = new Identity(category, type, name); |
| 19 |
} |
| 20 |
|
| 21 |
public void add_feature(XmppStream stream, string feature) { |
| 22 |
stream.get_flag(Flag.IDENTITY).add_own_feature(feature); |
| 23 |
} |
| 24 |
|
| 25 |
public void remove_feature(XmppStream stream, string feature) { |
| 26 |
Flag? flag = stream.get_flag(Flag.IDENTITY); |
| 27 |
if (flag != null) { |
| 28 |
flag.remove_own_feature(feature); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
public void add_feature_notify(XmppStream stream, string feature) { |
| 33 |
add_feature(stream, feature + "+notify"); |
| 34 |
} |
| 35 |
|
| 36 |
public void remove_feature_notify(XmppStream stream, string feature) { |
| 37 |
remove_feature(stream, feature + "+notify"); |
| 38 |
} |
| 39 |
|
| 40 |
public async bool has_entity_feature(XmppStream stream, Jid jid, string feature) { |
| 41 |
return yield this.cache.has_entity_feature(jid, feature); |
| 42 |
} |
| 43 |
|
| 44 |
public async Gee.Set<Identity>? get_entity_identities(XmppStream stream, Jid jid) { |
| 45 |
return yield this.cache.get_entity_identities(jid); |
| 46 |
} |
| 47 |
|
| 48 |
public async InfoResult? request_info(XmppStream stream, Jid jid) { |
| 49 |
var future = active_info_requests[jid]; |
| 50 |
if (future == null) { |
| 51 |
var promise = new Promise<InfoResult?>(); |
| 52 |
future = promise.future; |
| 53 |
active_info_requests[jid] = future; |
| 54 |
|
| 55 |
Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("query", NS_URI_INFO).add_self_xmlns()) { to=jid }; |
| 56 |
Iq.Stanza iq_response = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq); |
| 57 |
InfoResult? result = InfoResult.create_from_iq(iq_response); |
| 58 |
|
| 59 |
promise.set_value(result); |
| 60 |
active_info_requests.unset(jid); |
| 61 |
} |
| 62 |
|
| 63 |
try { |
| 64 |
InfoResult? res = yield future.wait_async(); |
| 65 |
return res; |
| 66 |
} catch (FutureError error) { |
| 67 |
warning("Future error when waiting for info request result: %s", error.message); |
| 68 |
return null; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public async ItemsResult? request_items(XmppStream stream, Jid jid) { |
| 73 |
StanzaNode query_node = new StanzaNode.build("query", NS_URI_ITEMS).add_self_xmlns(); |
| 74 |
Iq.Stanza iq = new Iq.Stanza.get(query_node) { to=jid }; |
| 75 |
|
| 76 |
Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq); |
| 77 |
ItemsResult? result = ItemsResult.create_from_iq(iq_result); |
| 78 |
|
| 79 |
return result; |
| 80 |
} |
| 81 |
|
| 82 |
public async void on_iq_get(XmppStream stream, Iq.Stanza iq) { |
| 83 |
StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI_INFO); |
| 84 |
if (query_node != null) { |
| 85 |
send_query_result(stream, iq); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public override void attach(XmppStream stream) { |
| 90 |
stream.add_flag(new Flag()); |
| 91 |
stream.get_flag(Flag.IDENTITY).add_own_identity(own_identity); |
| 92 |
|
| 93 |
stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI_INFO, this); |
| 94 |
add_feature(stream, NS_URI_INFO); |
| 95 |
} |
| 96 |
|
| 97 |
public override void detach(XmppStream stream) { |
| 98 |
active_info_requests.clear(); |
| 99 |
|
| 100 |
Flag? flag = stream.get_flag(Flag.IDENTITY); |
| 101 |
if (flag != null) flag.remove_own_identity(own_identity); |
| 102 |
|
| 103 |
stream.get_module(Iq.Module.IDENTITY).unregister_from_namespace(NS_URI_INFO, this); |
| 104 |
remove_feature(stream, NS_URI_INFO); |
| 105 |
} |
| 106 |
|
| 107 |
public static void require(XmppStream stream) { |
| 108 |
if (stream.get_module(IDENTITY) == null) stream.add_module(new ServiceDiscovery.Module()); |
| 109 |
} |
| 110 |
|
| 111 |
public override string get_ns() { return NS_URI; } |
| 112 |
public override string get_id() { return IDENTITY.id; } |
| 113 |
|
| 114 |
private void send_query_result(XmppStream stream, Iq.Stanza iq_request) { |
| 115 |
InfoResult query_result = new ServiceDiscovery.InfoResult(iq_request); |
| 116 |
query_result.features = stream.get_flag(Flag.IDENTITY).own_features; |
| 117 |
query_result.identities = stream.get_flag(Flag.IDENTITY).own_identities; |
| 118 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, query_result.iq); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
public interface CapsCache : Object { |
| 123 |
public abstract async bool has_entity_feature(Jid jid, string feature); |
| 124 |
public abstract async Gee.Set<Identity> get_entity_identities(Jid jid); |
| 125 |
} |
| 126 |
|
| 127 |
} |