From be1828d9e9eb92d8f71270fd036676e26eefc1dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D1=8F=D0=BD=20=D0=9C=D0=B8=D0=BD=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Tue, 29 Mar 2022 11:59:14 -0500 Subject: [PATCH] feat: Updates external-services.lua to latest. changeset ede9682c2022 https://hg.prosody.im/prosody-modules/file/ede9682c2022/mod_external_services/mod_external_services.lua --- .../prosody-plugins/mod_external_services.lua | 112 ++++++++---------- resources/prosody-plugins/mod_jiconop.lua | 10 +- 2 files changed, 54 insertions(+), 68 deletions(-) diff --git a/resources/prosody-plugins/mod_external_services.lua b/resources/prosody-plugins/mod_external_services.lua index 56182c8ed8..84c157f7d2 100644 --- a/resources/prosody-plugins/mod_external_services.lua +++ b/resources/prosody-plugins/mod_external_services.lua @@ -5,6 +5,7 @@ local hashes = require "util.hashes"; local st = require "util.stanza"; local jid = require "util.jid"; local array = require "util.array"; +local set = require "util.set"; local default_host = module:get_option_string("external_service_host", module.host); local default_port = module:get_option_number("external_service_port"); @@ -114,43 +115,32 @@ local services_mt = { end; } -function get_services(requested_type, origin, stanza, reply) +function get_services() local extras = module:get_host_items("external_service"); local services = ( configured_services + extras ) / prepare; - if requested_type then - services:filter(function(item) - return item.type == requested_type; - end); - end - setmetatable(services, services_mt); - if origin and stanza and reply then - module:fire_event("external_service/services", { - origin = origin; - stanza = stanza; - reply = reply; - requested_type = requested_type; - services = services; - }); - end + return services; +end + +function services_xml(services, name, namespace) + local reply = st.stanza(name or "services", { xmlns = namespace or "urn:xmpp:extdisco:2" }); - local res_services = {}; for _, srv in ipairs(services) do - table.insert(res_services, { - 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; - }) + 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 res_services; + return reply; end local function handle_services(event) @@ -164,12 +154,24 @@ local function handle_services(event) return true; end - local reply = st.reply(stanza):tag("services", { xmlns = action.attr.xmlns }); + local services = get_services(); - for _, srv in ipairs(get_services(action.attr.type, origin, stanza, reply)) do - reply:tag("service", srv):up(); + local requested_type = action.attr.type; + if requested_type then + services:filter(function(item) + return item.type == requested_type; + end); end + module:fire_event("external_service/services", { + origin = origin; + stanza = stanza; + requested_type = requested_type; + services = services; + }); + + local reply = st.reply(stanza):add_child(services_xml(services, action.name, action.attr.xmlns)); + origin.send(reply); return true; end @@ -179,54 +181,40 @@ local function handle_credentials(event) local action = stanza.tags[1]; if origin.type ~= "c2s" then - origin.send(st.error_reply(stanza, "auth", "forbidden")); + origin.send(st.error_reply(stanza, "auth", "forbidden", "The 'port' and 'type' attributes are required.")); return true; end - local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns }); - local extras = module:get_host_items("external_service"); - local services = ( configured_services + extras ) / prepare; + local services = get_services(); services:filter(function (item) return item.restricted; end) - local requested_credentials = {}; + local requested_credentials = set.new(); for service in action:childtags("service") do - table.insert(requested_credentials, { - type = service.attr.type; - host = service.attr.host; - port = tonumber(service.attr.port); - }); - end + if not service.attr.type or not service.attr.host then + origin.send(st.error_reply(stanza, "modify", "bad-request")); + return true; + end - setmetatable(services, services_mt); - setmetatable(requested_credentials, services_mt); + requested_credentials:add(string.format("%s:%s:%d", service.attr.type, service.attr.host, + tonumber(service.attr.port) or 0)); + end module:fire_event("external_service/credentials", { origin = origin; stanza = stanza; - reply = reply; requested_credentials = requested_credentials; services = services; }); - for req_srv in action:childtags("service") do - for _, srv in ipairs(services) do - if srv.type == req_srv.attr.type and srv.host == req_srv.attr.host - and not req_srv.attr.port or srv.port == tonumber(req_srv.attr.port) then - 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 - end - end + services:filter(function (srv) + local port_key = string.format("%s:%s:%d", srv.type, srv.host, srv.port or 0); + local portless_key = string.format("%s:%s:%d", srv.type, srv.host, 0); + return requested_credentials:contains(port_key) or requested_credentials:contains(portless_key); + end); + + local reply = st.reply(stanza):add_child(services_xml(services, action.name, action.attr.xmlns)); origin.send(reply); return true; diff --git a/resources/prosody-plugins/mod_jiconop.lua b/resources/prosody-plugins/mod_jiconop.lua index e63b1869ec..9988340479 100644 --- a/resources/prosody-plugins/mod_jiconop.lua +++ b/resources/prosody-plugins/mod_jiconop.lua @@ -1,5 +1,7 @@ local st = require "util.stanza"; -local get_services = module:depends("external_services").get_services; +local ext_services = module:depends("external_services"); +local get_services = ext_services.get_services; +local services_xml = ext_services.services_xml; -- Jitsi Connection Optimization -- gathers needed information and pushes it with a message to clients @@ -41,11 +43,7 @@ module:hook("resource-bind", function (event) stanza:add_child(query):up(); --- get turnservers and credentials - local services = get_services(); - stanza:tag("services"); - for _, srv in ipairs(services) do - stanza:tag("service", srv):up(); - end + stanza:add_child(services_xml(get_services())); session.send(stanza); end);