diff mod_rest/mod_rest.lua @ 4923:c83b009b5bc5

mod_rest: Add configuration of which stanzas to route to callback Makes it simpler to build APIs that only handle a certain kind of stanzas, letting them be handled by the unhandled stanza handler instead of having to write code to ignore certain kinds of stanzas.
author Kim Alvefur <zash@zash.se>
date Sat, 09 Apr 2022 01:04:25 +0200
parents 816b23e09c20
children e7b9bc629ecc
line wrap: on
line diff
--- a/mod_rest/mod_rest.lua	Sat Apr 09 00:43:18 2022 +0200
+++ b/mod_rest/mod_rest.lua	Sat Apr 09 01:04:25 2022 +0200
@@ -553,21 +553,19 @@
 		return true;
 	end
 
-	if module:get_host_type() == "component" then
-		module:hook("iq/bare", handle_stanza, -1);
-		module:hook("message/bare", handle_stanza, -1);
-		module:hook("presence/bare", handle_stanza, -1);
-		module:hook("iq/full", handle_stanza, -1);
-		module:hook("message/full", handle_stanza, -1);
-		module:hook("presence/full", handle_stanza, -1);
-		module:hook("iq/host", handle_stanza, -1);
-		module:hook("message/host", handle_stanza, -1);
-		module:hook("presence/host", handle_stanza, -1);
-	else
-		-- Don't override everything on normal VirtualHosts
-		module:hook("iq/host", handle_stanza, -1);
-		module:hook("message/host", handle_stanza, -1);
-		module:hook("presence/host", handle_stanza, -1);
+	local send_kinds = module:get_option_set("rest_callback_stanzas", { "message", "presence", "iq" });
+
+	local event_presets = {
+		-- Don't override everything on normal VirtualHosts by default
+		["local"] = { "host" },
+		-- Comonents get to handle all kinds of stanzas
+		["component"] = { "bare", "full", "host" },
+	};
+	local hook_events = module:get_option_set("rest_callback_events", event_presets[module:get_host_type()]);
+	for kind in send_kinds do
+		for event in hook_events do
+			module:hook(kind.."/"..event, handle_stanza, -1);
+		end
 	end
 end