git » sdk » commit b88c0da

Allow handling incoming iq requests

author Stephen Paul Weber
2023-09-12 18:44:27 UTC
committer Stephen Paul Weber
2023-09-13 18:53:11 UTC
parent f0f7afcf30a0cc258cee51db99799595ca258ee0

Allow handling incoming iq requests

Return service-unavailable if it goes unhandled.  Assume the handler
replied otherwise.

xmpp/EventEmitter.hx +1 -1
xmpp/GenericStream.hx +13 -1

diff --git a/xmpp/EventEmitter.hx b/xmpp/EventEmitter.hx
index dfeb9c7..7165d5c 100644
--- a/xmpp/EventEmitter.hx
+++ b/xmpp/EventEmitter.hx
@@ -38,6 +38,6 @@ class EventEmitter {
 				case EventStop | EventValue(_): return ret;
 			}
 		}
-		return EventHandled;
+		return handled ? EventHandled : EventUnhandled;
 	}
 }
diff --git a/xmpp/GenericStream.hx b/xmpp/GenericStream.hx
index a824e33..a1e8aea 100644
--- a/xmpp/GenericStream.hx
+++ b/xmpp/GenericStream.hx
@@ -30,12 +30,24 @@ abstract class GenericStream extends EventEmitter {
 		final xmlns = stanza.attr.get("xmlns");
 		if(xmlns == "jabber:client") {
 			final name = stanza.name;
-			if(stanza.name == "iq") {
+			if(name == "iq") {
 				var type = stanza.attr.get("type");
 				trace('type: $type');
 				if(type == "result" || type == "error") {
 					var id = stanza.attr.get("id");
 					trigger('iq-response/$id', { stanza: stanza });
+				} else {
+					if (trigger('iq', { stanza: stanza }) == EventUnhandled) {
+						var reply = new Stanza("iq", {
+							type: "error",
+							id: stanza.attr.get("id"),
+							to: stanza.attr.get("from")
+						})
+							.tag("error", { type: "cancel" })
+							.tag("service-unavailable", { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" })
+							.up().up();
+						sendStanza(reply);
+					}
 				}
 			} else if (name == "message" || name == "presence") {
 				trigger(name, { stanza: stanza });