changeset 4735:58a20d5ac6e9

mod_rest: Fire pre-events Let event handlers on pre-events have effect.
author Kim Alvefur <zash@zash.se>
date Mon, 01 Nov 2021 15:51:59 +0100
parents e58ec4b3cf90
children a938df1f60de
files mod_rest/mod_rest.lua
diffstat 1 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/mod_rest.lua	Mon Nov 01 15:08:21 2021 +0100
+++ b/mod_rest/mod_rest.lua	Mon Nov 01 15:51:59 2021 +0100
@@ -68,6 +68,41 @@
 	end
 end
 
+-- TODO This ought to be handled some way other than duplicating this
+-- core.stanza_router code here.
+local function compat_preevents(origin, stanza) --> boolean : handled
+	local to = stanza.attr.to;
+	local node, host, resource = jid.split(to);
+	local to_bare = node and (node .. "@" .. host) or host; -- bare JID
+
+	local to_type, to_self;
+	if node then
+		if resource then
+			to_type = '/full';
+		else
+			to_type = '/bare';
+			if node == origin.username and host == origin.host then
+				stanza.attr.to = nil;
+				to_self = true;
+			end
+		end
+	else
+		if host then
+			to_type = '/host';
+		else
+			to_type = '/bare';
+			to_self = true;
+		end
+	end
+
+	local event_data = { origin = origin; stanza = stanza; to_self = to_self };
+
+	local result = module:fire_event("pre-stanza", event_data);
+	if result ~= nil then return true end
+	if module:fire_event('pre-' .. stanza.name .. to_type, event_data) then return true; end -- do preprocessing
+	return false
+end
+
 -- (table, string) -> table
 local function amend_from_path(data, path)
 	local st_kind, st_type, st_to = path:match("^([mpi]%w+)/(%w+)/(.*)$");
@@ -308,6 +343,7 @@
 		function origin.send(stanza)
 			responses:add_direct_child(stanza);
 		end
+		if compat_preevents(origin, payload) then return 202; end
 
 		if payload.attr.type ~= "get" and payload.attr.type ~= "set" then
 			return post_errors.new("iq_type");
@@ -343,6 +379,7 @@
 			response:send(encode(send_type, stanza));
 			return true;
 		end
+		if compat_preevents(origin, payload) then return 202; end
 
 		module:send(payload, origin);
 		return 202;