From 1c81e40b20e1aa1e81352a8268f42e245a829717 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 30 Aug 2021 20:19:15 +0200 Subject: 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. --- plugins/mod_external_services.lua | 49 +++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 28 deletions(-) (limited to 'plugins') 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; -- cgit v1.2.3