diff options
author | Kim Alvefur <zash@zash.se> | 2021-08-30 20:19:15 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2021-08-30 20:19:15 +0200 |
commit | 1c81e40b20e1aa1e81352a8268f42e245a829717 (patch) | |
tree | 54978bdef68f036f8e1f5d3c02589e9dcebe1743 /plugins | |
parent | 8d25086ac19b2f01005d03221abb538e009fa82d (diff) | |
download | prosody-1c81e40b20e1aa1e81352a8268f42e245a829717.tar.gz prosody-1c81e40b20e1aa1e81352a8268f42e245a829717.zip |
mod_external_services: Factor out public function for converting to XML
Along with the previous commit, allows building the XML thing yourself,
should you wish to send it yourself or use it in a different context than
an iq reply.
API change: The 'reply' is removed from the event.
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/mod_external_services.lua | 49 |
1 files changed, 21 insertions, 28 deletions
diff --git a/plugins/mod_external_services.lua b/plugins/mod_external_services.lua index 3a92dc2d..6b46f890 100644 --- a/plugins/mod_external_services.lua +++ b/plugins/mod_external_services.lua @@ -131,6 +131,25 @@ function get_services() return services; end +function services_xml(services, name, namespace) + local reply = st.stanza(name or "services", { xmlns = namespace or "urn:xmpp:extdisco:2" }); + + for _, srv in ipairs(services) do + reply:tag("service", { + type = srv.type; + transport = srv.transport; + host = srv.host; + port = srv.port and string.format("%d", srv.port) or nil; + username = srv.username; + password = srv.password; + expires = srv.expires and dt.datetime(srv.expires) or nil; + restricted = srv.restricted and "1" or nil; + }):up(); + end + + return reply; +end + local function handle_services(event) local origin, stanza = event.origin, event.stanza; local action = stanza.tags[1]; @@ -142,7 +161,6 @@ local function handle_services(event) return true; end - local reply = st.reply(stanza):tag("services", { xmlns = action.attr.xmlns }); local services = get_services(); local requested_type = action.attr.type; @@ -155,23 +173,11 @@ local function handle_services(event) module:fire_event("external_service/services", { origin = origin; stanza = stanza; - reply = reply; requested_type = requested_type; services = services; }); - for _, srv in ipairs(services) do - reply:tag("service", { - type = srv.type; - transport = srv.transport; - host = srv.host; - port = srv.port and string.format("%d", srv.port) or nil; - username = srv.username; - password = srv.password; - expires = srv.expires and dt.datetime(srv.expires) or nil; - restricted = srv.restricted and "1" or nil; - }):up(); - end + local reply = st.reply(stanza):add_child(services_xml(services, action.name, action.attr.xmlns)); origin.send(reply); return true; @@ -186,7 +192,6 @@ local function handle_credentials(event) return true; end - local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns }); local services = get_services(); services:filter(function (item) return item.restricted; @@ -206,7 +211,6 @@ local function handle_credentials(event) module:fire_event("external_service/credentials", { origin = origin; stanza = stanza; - reply = reply; requested_credentials = requested_credentials; services = services; }); @@ -217,18 +221,7 @@ local function handle_credentials(event) return requested_credentials:contains(port_key) or requested_credentials:contains(portless_key); end); - for _, srv in ipairs(services) do - reply:tag("service", { - type = srv.type; - transport = srv.transport; - host = srv.host; - port = srv.port and string.format("%d", srv.port) or nil; - username = srv.username; - password = srv.password; - expires = srv.expires and dt.datetime(srv.expires) or nil; - restricted = srv.restricted and "1" or nil; - }):up(); - end + local reply = st.reply(stanza):add_child(services_xml(services, action.name, action.attr.xmlns)); origin.send(reply); return true; |