From 62aa8aa74a63e67247cd80d20ccc27ae52882b9e Mon Sep 17 00:00:00 2001 From: hrxi Date: Thu, 20 Jun 2019 16:01:58 +0200 Subject: [PATCH 1/2] Add a couple of error primitives for easier error handling --- xmpp-vala/src/module/iq/module.vala | 2 +- xmpp-vala/src/module/iq/stanza.vala | 9 ++---- xmpp-vala/src/module/stanza_error.vala | 43 +++++++++++++++++++++----- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/xmpp-vala/src/module/iq/module.vala b/xmpp-vala/src/module/iq/module.vala index 47f6e764..abcc271c 100644 --- a/xmpp-vala/src/module/iq/module.vala +++ b/xmpp-vala/src/module/iq/module.vala @@ -67,7 +67,7 @@ namespace Xmpp.Iq { } } } else { - Iq.Stanza unavailable_error = new Iq.Stanza.error(iq, new StanzaNode.build("service-unavailable", "urn:ietf:params:xml:ns:xmpp-stanzas").add_self_xmlns()); + Iq.Stanza unavailable_error = new Iq.Stanza.error(iq, new ErrorStanza.service_unavailable()); send_iq(stream, unavailable_error); } } diff --git a/xmpp-vala/src/module/iq/stanza.vala b/xmpp-vala/src/module/iq/stanza.vala index 3c938883..825d6a5d 100644 --- a/xmpp-vala/src/module/iq/stanza.vala +++ b/xmpp-vala/src/module/iq/stanza.vala @@ -30,17 +30,14 @@ public class Stanza : Xmpp.Stanza { public Stanza.set(StanzaNode stanza_node, string? id = null) { this(id); - type_ = TYPE_SET; + this.type_ = TYPE_SET; stanza.put_node(stanza_node); } - public Stanza.error(Stanza request, StanzaNode error_stanza, StanzaNode? associated_child = null) { + public Stanza.error(Stanza request, ErrorStanza error_stanza) { this(request.id); this.type_ = TYPE_ERROR; - stanza.put_node(error_stanza); - if (associated_child != null) { - stanza.put_node(associated_child); - } + stanza.put_node(error_stanza.error_node); } public Stanza.from_stanza(StanzaNode stanza_node, Jid? my_jid) { base.incoming(stanza_node, my_jid); diff --git a/xmpp-vala/src/module/stanza_error.vala b/xmpp-vala/src/module/stanza_error.vala index c45ff4e3..651e8d2b 100644 --- a/xmpp-vala/src/module/stanza_error.vala +++ b/xmpp-vala/src/module/stanza_error.vala @@ -2,6 +2,8 @@ using Gee; namespace Xmpp { + private const string ERROR_NS_URI = "urn:ietf:params:xml:ns:xmpp-stanzas"; + public class ErrorStanza { public const string CONDITION_BAD_REQUEST = "bad-request"; public const string CONDITION_CONFLICT = "conflict"; @@ -37,14 +39,14 @@ namespace Xmpp { } public string? text { - get { return error_node.get_deep_string_content("urn:ietf:params:xml:ns:xmpp-stanzas:text"); } + get { return error_node.get_deep_string_content(ERROR_NS_URI + ":text"); } } public string condition { get { Gee.List subnodes = error_node.sub_nodes; foreach (StanzaNode subnode in subnodes) { // TODO get subnode by ns - if (subnode.ns_uri == "urn:ietf:params:xml:ns:xmpp-stanzas") { + if (subnode.ns_uri == ERROR_NS_URI) { return subnode.name; } } @@ -52,20 +54,45 @@ namespace Xmpp { } } - public string? original_id { - get { return stanza.get_attribute("id"); } - } - public string type_ { get { return error_node.get_attribute("type"); } } - public StanzaNode stanza; public StanzaNode error_node; public ErrorStanza.from_stanza(StanzaNode stanza) { - this.stanza = stanza; error_node = stanza.get_subnode("error"); } + + public ErrorStanza.build(string type, string condition, string? human_readable, StanzaNode? application_condition) { + error_node = new StanzaNode.build("error") + .put_attribute("type", type) + .put_node(new StanzaNode.build(condition, ERROR_NS_URI).add_self_xmlns()); + if (application_condition != null) { + error_node.put_node(application_condition); + } + if (human_readable != null) { + error_node.put_node(new StanzaNode.build("text", ERROR_NS_URI) + .add_self_xmlns() + .put_attribute("xml:lang", "en") + .put_node(new StanzaNode.text(text)) + ); + } + } + public ErrorStanza.bad_request(string? human_readable = null) { + this.build(TYPE_MODIFY, CONDITION_BAD_REQUEST, human_readable, null); + } + public ErrorStanza.feature_not_implemented(StanzaNode? application_condition = null) { + this.build(TYPE_MODIFY, CONDITION_FEATURE_NOT_IMPLEMENTED, null, application_condition); + } + public ErrorStanza.item_not_found(StanzaNode? application_condition = null) { + this.build(TYPE_CANCEL, CONDITION_ITEM_NOT_FOUND, null, application_condition); + } + public ErrorStanza.not_acceptable(string? human_readable = null) { + this.build(TYPE_MODIFY, CONDITION_NOT_ACCEPTABLE, human_readable, null); + } + public ErrorStanza.service_unavailable() { + this.build(TYPE_CANCEL, CONDITION_SERVICE_UNAVAILABLE, null, null); + } } } From 57a7d165e91cbfa1b2b4c10e42958cdc0854e961 Mon Sep 17 00:00:00 2001 From: hrxi Date: Thu, 20 Jun 2019 16:08:05 +0200 Subject: [PATCH 2/2] Send bad-request error for unknown IQ set/gets --- xmpp-vala/src/module/iq/module.vala | 10 ++++++++-- xmpp-vala/src/module/roster/module.vala | 2 -- .../src/module/xep/0030_service_discovery/module.vala | 2 -- xmpp-vala/src/module/xep/0191_blocking_command.vala | 1 - xmpp-vala/src/module/xep/0199_ping.vala | 1 - 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/xmpp-vala/src/module/iq/module.vala b/xmpp-vala/src/module/iq/module.vala index abcc271c..f73f5459 100644 --- a/xmpp-vala/src/module/iq/module.vala +++ b/xmpp-vala/src/module/iq/module.vala @@ -83,8 +83,14 @@ namespace Xmpp.Iq { } public interface Handler : Object { - public abstract void on_iq_get(XmppStream stream, Iq.Stanza iq); - public abstract void on_iq_set(XmppStream stream, Iq.Stanza iq); + public 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) { + 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); + } } } diff --git a/xmpp-vala/src/module/roster/module.vala b/xmpp-vala/src/module/roster/module.vala index 2d36211d..7a69abe9 100644 --- a/xmpp-vala/src/module/roster/module.vala +++ b/xmpp-vala/src/module/roster/module.vala @@ -66,8 +66,6 @@ public class Module : XmppStreamModule, Iq.Handler { } } - public void on_iq_get(XmppStream stream, Iq.Stanza iq) { } - public override void attach(XmppStream stream) { stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI, this); stream.get_module(Presence.Module.IDENTITY).initial_presence_sent.connect(roster_get); diff --git a/xmpp-vala/src/module/xep/0030_service_discovery/module.vala b/xmpp-vala/src/module/xep/0030_service_discovery/module.vala index 2b955738..e2bb46f8 100644 --- a/xmpp-vala/src/module/xep/0030_service_discovery/module.vala +++ b/xmpp-vala/src/module/xep/0030_service_discovery/module.vala @@ -66,8 +66,6 @@ public class Module : XmppStreamModule, Iq.Handler { } } - public void on_iq_set(XmppStream stream, Iq.Stanza iq) { } - public override void attach(XmppStream stream) { stream.add_flag(new Flag()); stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI_INFO, this); diff --git a/xmpp-vala/src/module/xep/0191_blocking_command.vala b/xmpp-vala/src/module/xep/0191_blocking_command.vala index 4b78ed33..ee8a1fed 100644 --- a/xmpp-vala/src/module/xep/0191_blocking_command.vala +++ b/xmpp-vala/src/module/xep/0191_blocking_command.vala @@ -45,7 +45,6 @@ public class Module : XmppStreamModule, Iq.Handler { return stream.has_flag(Flag.IDENTITY); } - private void on_iq_get(XmppStream stream, Iq.Stanza iq) { } private 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); diff --git a/xmpp-vala/src/module/xep/0199_ping.vala b/xmpp-vala/src/module/xep/0199_ping.vala index 661ad253..3458fa9d 100644 --- a/xmpp-vala/src/module/xep/0199_ping.vala +++ b/xmpp-vala/src/module/xep/0199_ping.vala @@ -27,7 +27,6 @@ namespace Xmpp.Xep.Ping { 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 void on_iq_set(XmppStream stream, Iq.Stanza iq) { } public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; }