Comparison

mod_rest/mod_rest.lua @ 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
parent 4734:e58ec4b3cf90
child 4737:2bc5a0c0d8c3
comparison
equal deleted inserted replaced
4734:e58ec4b3cf90 4735:58a20d5ac6e9
64 return { 64 return {
65 username = id.medium():lower(); 65 username = id.medium():lower();
66 host = module.host; 66 host = module.host;
67 } 67 }
68 end 68 end
69 end
70
71 -- TODO This ought to be handled some way other than duplicating this
72 -- core.stanza_router code here.
73 local function compat_preevents(origin, stanza) --> boolean : handled
74 local to = stanza.attr.to;
75 local node, host, resource = jid.split(to);
76 local to_bare = node and (node .. "@" .. host) or host; -- bare JID
77
78 local to_type, to_self;
79 if node then
80 if resource then
81 to_type = '/full';
82 else
83 to_type = '/bare';
84 if node == origin.username and host == origin.host then
85 stanza.attr.to = nil;
86 to_self = true;
87 end
88 end
89 else
90 if host then
91 to_type = '/host';
92 else
93 to_type = '/bare';
94 to_self = true;
95 end
96 end
97
98 local event_data = { origin = origin; stanza = stanza; to_self = to_self };
99
100 local result = module:fire_event("pre-stanza", event_data);
101 if result ~= nil then return true end
102 if module:fire_event('pre-' .. stanza.name .. to_type, event_data) then return true; end -- do preprocessing
103 return false
69 end 104 end
70 105
71 -- (table, string) -> table 106 -- (table, string) -> table
72 local function amend_from_path(data, path) 107 local function amend_from_path(data, path)
73 local st_kind, st_type, st_to = path:match("^([mpi]%w+)/(%w+)/(.*)$"); 108 local st_kind, st_type, st_to = path:match("^([mpi]%w+)/(%w+)/(.*)$");
306 if payload.name == "iq" then 341 if payload.name == "iq" then
307 local responses = st.stanza("xmpp"); 342 local responses = st.stanza("xmpp");
308 function origin.send(stanza) 343 function origin.send(stanza)
309 responses:add_direct_child(stanza); 344 responses:add_direct_child(stanza);
310 end 345 end
346 if compat_preevents(origin, payload) then return 202; end
311 347
312 if payload.attr.type ~= "get" and payload.attr.type ~= "set" then 348 if payload.attr.type ~= "get" and payload.attr.type ~= "set" then
313 return post_errors.new("iq_type"); 349 return post_errors.new("iq_type");
314 elseif #payload.tags ~= 1 then 350 elseif #payload.tags ~= 1 then
315 return post_errors.new("iq_tags"); 351 return post_errors.new("iq_tags");
341 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); 377 module:log("debug", "Sending[rest]: %s", stanza:top_tag());
342 response.headers.content_type = send_type; 378 response.headers.content_type = send_type;
343 response:send(encode(send_type, stanza)); 379 response:send(encode(send_type, stanza));
344 return true; 380 return true;
345 end 381 end
382 if compat_preevents(origin, payload) then return 202; end
346 383
347 module:send(payload, origin); 384 module:send(payload, origin);
348 return 202; 385 return 202;
349 end 386 end
350 end 387 end