Mercurial > prosody-modules
changeset 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 | df3d521e3c39 |
files | mod_rest/README.markdown mod_rest/mod_rest.lua |
diffstat | 2 files changed, 48 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/mod_rest/README.markdown Sat Apr 09 00:43:18 2022 +0200 +++ b/mod_rest/README.markdown Sat Apr 09 01:04:25 2022 +0200 @@ -169,6 +169,41 @@ } ``` +### Which stanzas + +The set of stanzas routed to the callback is determined by these two +settings: + +`rest_callback_stanzas` +: The stanza kinds to handle, defaults to `{ "message", "presence", "iq" }` + +`rest_callback_events` +: For the selected stanza kinds, which events to handle. When loaded +on a Component, this defaults to `{ "bare", "full", "host" }`, while on +a VirtualHost the default is `{ "host" }`. + +Events correspond to which form of address was used in the `to` +attribute of the stanza. + +bare +: `localpart@hostpart` + +full +: `localpart@hostpart/resourcepart` + +host +: `hostpart` + +The following example would handle only stanzas like `<message +to="anything@hello.example"/>` + +```lua +Component "hello.example" "rest" +rest_callback_url = "http://hello.internal.example:9003/api" +rest_callback_stanzas = { "message" } +rest_callback_events = { "bare" } +``` + ### Replying To accept the stanza without returning a reply, respond with HTTP status
--- 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