xmpp-vala: Use more async

This commit is contained in:
fiaxh 2020-04-24 14:19:42 +02:00
parent 1db94905ae
commit f8f305efe5
16 changed files with 90 additions and 123 deletions

View File

@ -223,7 +223,7 @@ public class ConnectionManager : Object {
DateTime? last_activity_was = connections[account].last_activity;
XmppStream stream = connections[account].stream;
stream.get_module(Xep.Ping.Module.IDENTITY).send_ping(stream, account.bare_jid.domain_jid, () => {
stream.get_module(Xep.Ping.Module.IDENTITY).send_ping.begin(stream, account.bare_jid.domain_jid, () => {
acked = true;
if (connections[account].stream != stream) return;
change_connection_state(account, ConnectionState.CONNECTED);

View File

@ -54,7 +54,7 @@ namespace Xmpp.Iq {
public override string get_ns() { return NS_URI; }
public override string get_id() { return IDENTITY.id; }
private void on_received_iq_stanza(XmppStream stream, StanzaNode node) {
private async void on_received_iq_stanza(XmppStream stream, StanzaNode node) {
Iq.Stanza iq = new Iq.Stanza.from_stanza(node, stream.has_flag(Bind.Flag.IDENTITY) ? stream.get_flag(Bind.Flag.IDENTITY).my_jid : null);
if (iq.type_ == Iq.Stanza.TYPE_RESULT || iq.is_error()) {
@ -71,9 +71,9 @@ namespace Xmpp.Iq {
Gee.List<Handler> handlers = namespaceRegistrants[children[0].ns_uri];
foreach (Handler handler in handlers) {
if (iq.type_ == Iq.Stanza.TYPE_GET) {
handler.on_iq_get(stream, iq);
yield handler.on_iq_get(stream, iq);
} else if (iq.type_ == Iq.Stanza.TYPE_SET) {
handler.on_iq_set(stream, iq);
yield handler.on_iq_set(stream, iq);
}
}
} else {
@ -94,11 +94,11 @@ namespace Xmpp.Iq {
}
public interface Handler : Object {
public virtual void on_iq_get(XmppStream stream, Iq.Stanza iq) {
public async virtual void on_iq_get(XmppStream stream, Iq.Stanza iq) {
Iq.Stanza bad_request = new Iq.Stanza.error(iq, new ErrorStanza.bad_request("unexpected IQ get for this namespace"));
stream.get_module(Module.IDENTITY).send_iq(stream, bad_request);
}
public virtual void on_iq_set(XmppStream stream, Iq.Stanza iq) {
public async virtual void on_iq_set(XmppStream stream, Iq.Stanza iq) {
Iq.Stanza bad_request = new Iq.Stanza.error(iq, new ErrorStanza.bad_request("unexpected IQ set for this namespace"));
stream.get_module(Module.IDENTITY).send_iq(stream, bad_request);
}

View File

@ -44,7 +44,7 @@ public class Module : XmppStreamModule, Iq.Handler {
roster_set(stream, item);
}
public void on_iq_set(XmppStream stream, Iq.Stanza iq) {
public async void on_iq_set(XmppStream stream, Iq.Stanza iq) {
StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI);
if (query_node == null) return;
if (!iq.from.equals(stream.get_flag(Bind.Flag.IDENTITY).my_jid.bare_jid)) {

View File

@ -22,16 +22,16 @@ public class Module : XmppStreamNegotiationModule {
public override string get_ns() { return NS_URI; }
public override string get_id() { return IDENTITY.id; }
private void on_bound_resource(XmppStream stream, Jid my_jid) {
private async void on_bound_resource(XmppStream stream, Jid my_jid) {
StanzaNode? session_node = stream.features.get_subnode("session", NS_URI);
if (session_node != null && session_node.get_subnode("optional", NS_URI) == null) {
stream.add_flag(new Flag());
Iq.Stanza iq = new Iq.Stanza.set(new StanzaNode.build("session", NS_URI).add_self_xmlns()) { to=stream.remote_name };
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => {
if (!iq.is_error()) {
stream.get_flag(Flag.IDENTITY).finished = true;
}
});
Iq.Stanza result_iq = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
if (!result_iq.is_error()) {
stream.get_flag(Flag.IDENTITY).finished = true;
}
}
}
}

View File

@ -100,7 +100,7 @@ public class Module : XmppStreamModule, Iq.Handler {
return result;
}
public void on_iq_get(XmppStream stream, Iq.Stanza iq) {
public async void on_iq_get(XmppStream stream, Iq.Stanza iq) {
StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI_INFO);
if (query_node != null) {
send_query_result(stream, iq);

View File

@ -333,9 +333,9 @@ public class Module : XmppStreamModule {
Jid bare_jid = presence.from.bare_jid;
if (flag.get_enter_id(bare_jid) != null) {
query_affiliation(stream, bare_jid, "member", null);
query_affiliation(stream, bare_jid, "admin", null);
query_affiliation(stream, bare_jid, "owner", null);
query_affiliation.begin(stream, bare_jid, "member");
query_affiliation.begin(stream, bare_jid, "admin");
query_affiliation.begin(stream, bare_jid, "owner");
flag.finish_muc_enter(bare_jid, presence.from.resourcepart);
flag.enter_futures[bare_jid].set_value(new JoinResult() {nick=presence.from.resourcepart});
@ -437,36 +437,38 @@ public class Module : XmppStreamModule {
room_info_updated(stream, jid);
}
public delegate void OnAffiliationResult(XmppStream stream, Gee.List<Jid> jids);
private void query_affiliation(XmppStream stream, Jid jid, string affiliation, owned OnAffiliationResult? listener) {
private async Gee.List<Jid>? query_affiliation(XmppStream stream, Jid jid, string affiliation) {
Iq.Stanza iq = new Iq.Stanza.get(
new StanzaNode.build("query", NS_URI_ADMIN)
.add_self_xmlns()
.put_node(new StanzaNode.build("item", NS_URI_ADMIN)
.put_attribute("affiliation", affiliation))
) { to=jid };
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => {
if (iq.is_error()) return;
StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI_ADMIN);
if (query_node == null) return;
Gee.List<StanzaNode> item_nodes = query_node.get_subnodes("item", NS_URI_ADMIN);
Gee.List<Jid> ret_jids = new ArrayList<Jid>(Jid.equals_func);
foreach (StanzaNode item in item_nodes) {
string jid__ = item.get_attribute("jid");
string? affiliation_ = item.get_attribute("affiliation");
if (jid__ != null && affiliation_ != null) {
try {
Jid jid_ = new Jid(jid__);
stream.get_flag(Flag.IDENTITY).set_offline_member(iq.from, jid_, parse_affiliation(affiliation_));
ret_jids.add(jid_);
received_occupant_jid(stream, iq.from, jid_);
} catch (InvalidJidError e) {
warning("Received invalid occupant jid: %s", e.message);
}
Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
if (iq_result.is_error()) return null;
StanzaNode? query_node = iq_result.stanza.get_subnode("query", NS_URI_ADMIN);
if (query_node == null) return null;
Gee.List<StanzaNode> item_nodes = query_node.get_subnodes("item", NS_URI_ADMIN);
Gee.List<Jid> ret_jids = new ArrayList<Jid>(Jid.equals_func);
foreach (StanzaNode item in item_nodes) {
string jid__ = item.get_attribute("jid");
string? affiliation_ = item.get_attribute("affiliation");
if (jid__ != null && affiliation_ != null) {
try {
Jid jid_ = new Jid(jid__);
stream.get_flag(Flag.IDENTITY).set_offline_member(iq_result.from, jid_, parse_affiliation(affiliation_));
ret_jids.add(jid_);
received_occupant_jid(stream, iq_result.from, jid_);
} catch (InvalidJidError e) {
warning("Received invalid occupant jid: %s", e.message);
}
}
if (listener != null) listener(stream, ret_jids);
});
}
return ret_jids;
}
private static ArrayList<int> get_status_codes(StanzaNode x_node) {

View File

@ -18,7 +18,7 @@ public class Module : XmppStreamModule, Iq.Handler {
stream.get_module(Iq.Module.IDENTITY).unregister_from_namespace(NS_URI, this);
}
public void on_iq_set(XmppStream stream, Iq.Stanza iq) {
public async void on_iq_set(XmppStream stream, Iq.Stanza iq) {
// the iq module ensures that there's only one child node
StanzaNode? node = null;
node = (node != null) ? node : iq.stanza.get_subnode("open", NS_URI);

View File

@ -7,25 +7,21 @@ public class Module : BookmarksProvider, XmppStreamModule {
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0048_bookmarks_module");
public async Set<Conference>? get_conferences(XmppStream stream) {
Set<Conference> ret = new HashSet<Conference>(Conference.hash_func, Conference.equals_func);
StanzaNode get_node = new StanzaNode.build("storage", NS_URI).add_self_xmlns();
stream.get_module(PrivateXmlStorage.Module.IDENTITY).retrieve(stream, get_node, (stream, node) => {
if (node != null) {
Gee.List<StanzaNode> conferences_node = node.get_subnode("storage", NS_URI).get_subnodes("conference", NS_URI);
foreach (StanzaNode conference_node in conferences_node) {
Conference? conference = Bookmarks1Conference.create_from_stanza_node(conference_node);
ret.add(conference);
}
}
Idle.add(get_conferences.callback);
});
yield;
StanzaNode? result_node = yield stream.get_module(PrivateXmlStorage.Module.IDENTITY).retrieve(stream, get_node);
if (result_node == null) return null;
Set<Conference> ret = new HashSet<Conference>(Conference.hash_func, Conference.equals_func);
Gee.List<StanzaNode> conferences_node = result_node.get_subnode("storage", NS_URI).get_subnodes("conference", NS_URI);
foreach (StanzaNode conference_node in conferences_node) {
Conference? conference = Bookmarks1Conference.create_from_stanza_node(conference_node);
ret.add(conference);
}
return ret;
}
private void set_conferences(XmppStream stream, Set<Conference> conferences) {
private async void set_conferences(XmppStream stream, Set<Conference> conferences) {
StanzaNode storage_node = (new StanzaNode.build("storage", NS_URI)).add_self_xmlns();
foreach (Conference conference in conferences) {
Bookmarks1Conference? bookmarks1conference = conference as Bookmarks1Conference;
@ -46,15 +42,14 @@ public class Module : BookmarksProvider, XmppStreamModule {
storage_node.put_node(conference_node);
}
}
stream.get_module(PrivateXmlStorage.Module.IDENTITY).store(stream, storage_node, (stream) => {
stream.get_module(Module.IDENTITY).received_conferences(stream, conferences);
});
yield stream.get_module(PrivateXmlStorage.Module.IDENTITY).store(stream, storage_node);
stream.get_module(Module.IDENTITY).received_conferences(stream, conferences);
}
public async void add_conference(XmppStream stream, Conference conference) {
Set<Conference>? conferences = yield get_conferences(stream);
conferences.add(conference);
set_conferences(stream, conferences);
yield set_conferences(stream, conferences);
}
public async void replace_conference(XmppStream stream, Jid muc_jid, Conference modified_conference) {
@ -67,13 +62,13 @@ public class Module : BookmarksProvider, XmppStreamModule {
conference.password = modified_conference.password;
}
}
set_conferences(stream, conferences);
yield set_conferences(stream, conferences);
}
public async void remove_conference(XmppStream stream, Conference conference_remove) {
Set<Conference>? conferences = yield get_conferences(stream);
conferences.remove(conference_remove);
set_conferences(stream, conferences);
yield set_conferences(stream, conferences);
}
public override void attach(XmppStream stream) { }

View File

@ -6,22 +6,17 @@ namespace Xmpp.Xep.PrivateXmlStorage {
public class Module : XmppStreamModule {
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0049_private_xml_storage");
public delegate void OnSuccess(XmppStream stream);
public void store(XmppStream stream, StanzaNode node, owned OnSuccess listener) {
public async void store(XmppStream stream, StanzaNode node) {
StanzaNode queryNode = new StanzaNode.build("query", NS_URI).add_self_xmlns().put_node(node);
Iq.Stanza iq = new Iq.Stanza.set(queryNode);
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => {
listener(stream);
});
yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
}
public delegate void OnResponse(XmppStream stream, StanzaNode? node);
public void retrieve(XmppStream stream, StanzaNode node, owned OnResponse listener) {
public async StanzaNode? retrieve(XmppStream stream, StanzaNode node) {
StanzaNode queryNode = new StanzaNode.build("query", NS_URI).add_self_xmlns().put_node(node);
Iq.Stanza iq = new Iq.Stanza.get(queryNode);
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => {
listener(stream, iq.stanza.get_subnode("query", NS_URI));
});
Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
return iq_result.stanza.get_subnode("query", NS_URI);
}
public override void attach(XmppStream stream) { }

View File

@ -27,7 +27,7 @@ public class Module : XmppStreamModule, Iq.Handler {
}
public override void detach(XmppStream stream) { }
public void on_iq_set(XmppStream stream, Iq.Stanza iq) { }
public async void on_iq_set(XmppStream stream, Iq.Stanza iq) { }
public Gee.List<Proxy> get_proxies(XmppStream stream) {
return stream.get_flag(Flag.IDENTITY).proxies;

View File

@ -8,34 +8,27 @@ public class Module : XmppStreamNegotiationModule {
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0077_in_band_registration");
public async Form? get_from_server(XmppStream stream, Jid jid) {
Iq.Stanza request_form_iq = new Iq.Stanza.get(new StanzaNode.build("query", NS_URI).add_self_xmlns());
StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns();
Iq.Stanza request_form_iq = new Iq.Stanza.get(query_node) { to=jid };
request_form_iq.to = jid;
SourceFunc callback = get_from_server.callback;
Form? form = null;
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, request_form_iq, (stream, response_iq) => {
form = new Form.from_node(stream, response_iq);
Idle.add((owned)callback);
});
yield;
return form;
Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, request_form_iq);
return new Form.from_node(stream, iq_result);
}
public async string? submit_to_server(XmppStream stream, Jid jid, Form form) {
StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns();
query_node.put_node(form.get_submit_node());
Iq.Stanza iq = new Iq.Stanza.set(query_node);
iq.to = jid;
string? error_message = null;
SourceFunc callback = submit_to_server.callback;
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, response_iq) => {
if (response_iq.is_error()) {
ErrorStanza? error_stanza = response_iq.get_error();
error_message = error_stanza.text ?? "Error";
}
Idle.add((owned)callback);
});
yield;
return error_message;
Iq.Stanza iq = new Iq.Stanza.set(query_node) { to=jid };
Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
if (iq_result.is_error()) {
ErrorStanza? error_stanza = iq_result.get_error();
return error_stanza.text ?? "Error";
}
return null;
}
public override bool mandatory_outstanding(XmppStream stream) { return false; }

View File

@ -295,7 +295,7 @@ public class Module : XmppStreamModule, Iq.Handler {
current_stream.get_flag(Flag.IDENTITY).remove_session(sid);
}
public void on_iq_set(XmppStream stream, Iq.Stanza iq) {
public async void on_iq_set(XmppStream stream, Iq.Stanza iq) {
try {
handle_iq_set(stream, iq);
} catch (IqError e) {

View File

@ -45,7 +45,7 @@ public class Module : XmppStreamModule, Iq.Handler {
return stream.has_flag(Flag.IDENTITY);
}
private void on_iq_set(XmppStream stream, Iq.Stanza iq) {
private async void on_iq_set(XmppStream stream, Iq.Stanza iq) {
StanzaNode? block_node = iq.stanza.get_subnode("block", NS_URI);
StanzaNode? unblock_node = iq.stanza.get_subnode("unblock", NS_URI);
Gee.List<string> jids;

View File

@ -6,13 +6,10 @@ namespace Xmpp.Xep.Ping {
public class Module : XmppStreamModule, Iq.Handler {
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0199_ping");
public delegate void OnResult(XmppStream stream);
public void send_ping(XmppStream stream, Jid jid, owned OnResult? listener) {
Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("ping", NS_URI).add_self_xmlns());
iq.to = jid;
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream) => {
if (listener != null) listener(stream);
});
public async void send_ping(XmppStream stream, Jid jid) {
StanzaNode ping_node = new StanzaNode.build("ping", NS_URI).add_self_xmlns();
Iq.Stanza iq = new Iq.Stanza.get(ping_node) { to=jid };
yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
}
public override void attach(XmppStream stream) {
@ -25,8 +22,8 @@ namespace Xmpp.Xep.Ping {
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
}
public void on_iq_get(XmppStream stream, Iq.Stanza iq) {
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
public async void on_iq_get(XmppStream stream, Iq.Stanza iq) {
yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, new Iq.Stanza.result(iq));
}
public override string get_ns() { return NS_URI; }

View File

@ -7,14 +7,14 @@ public class Module : XmppStreamModule {
private ReceivedPipelineListener received_pipeline_listener = new ReceivedPipelineListener();
public void enable(XmppStream stream) {
public async void enable(XmppStream stream) {
Iq.Stanza iq = new Iq.Stanza.set(new StanzaNode.build("enable", NS_URI).add_self_xmlns());
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
}
public void disable(XmppStream stream) {
public async void disable(XmppStream stream) {
Iq.Stanza iq = new Iq.Stanza.set(new StanzaNode.build("disable", NS_URI).add_self_xmlns());
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
}
public override void attach(XmppStream stream) {

View File

@ -56,18 +56,10 @@ public class Module : XmppStreamModule {
if (stream.get_flag(Flag.IDENTITY) == null) return null;
var query_node = crate_base_query(stream, jid, query_id, start_time, end_time);
query_node.put_node(create_set_rsm_node(end_id));
Iq.Stanza iq = new Iq.Stanza.set(query_node);
Iq.Stanza? result_iq = null;
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => {
result_iq = iq;
Idle.add(query_archive.callback);
});
yield;
return result_iq;
return yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
}
public override void attach(XmppStream stream) {
@ -98,14 +90,7 @@ public class Module : XmppStreamModule {
Iq.Stanza paging_iq = new Iq.Stanza.set(query_node);
Iq.Stanza? result_iq = null;
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, paging_iq, (stream, iq) => {
result_iq = iq;
Idle.add(page_through_results.callback);
});
yield;
return result_iq;
return yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, paging_iq);
}
private async void query_availability(XmppStream stream) {