feat: Updates external-services.lua to latest.

changeset ede9682c2022
https://hg.prosody.im/prosody-modules/file/ede9682c2022/mod_external_services/mod_external_services.lua
This commit is contained in:
Дамян Минков 2022-03-29 11:59:14 -05:00
parent 3a36945c3c
commit be1828d9e9
2 changed files with 54 additions and 68 deletions

View File

@ -5,6 +5,7 @@ local hashes = require "util.hashes";
local st = require "util.stanza"; local st = require "util.stanza";
local jid = require "util.jid"; local jid = require "util.jid";
local array = require "util.array"; local array = require "util.array";
local set = require "util.set";
local default_host = module:get_option_string("external_service_host", module.host); local default_host = module:get_option_string("external_service_host", module.host);
local default_port = module:get_option_number("external_service_port"); local default_port = module:get_option_number("external_service_port");
@ -114,43 +115,32 @@ local services_mt = {
end; end;
} }
function get_services(requested_type, origin, stanza, reply) function get_services()
local extras = module:get_host_items("external_service"); local extras = module:get_host_items("external_service");
local services = ( configured_services + extras ) / prepare; 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); setmetatable(services, services_mt);
if origin and stanza and reply then return services;
module:fire_event("external_service/services", { end
origin = origin;
stanza = stanza; function services_xml(services, name, namespace)
reply = reply; local reply = st.stanza(name or "services", { xmlns = namespace or "urn:xmpp:extdisco:2" });
requested_type = requested_type;
services = services;
});
end
local res_services = {};
for _, srv in ipairs(services) do for _, srv in ipairs(services) do
table.insert(res_services, { reply:tag("service", {
type = srv.type; type = srv.type;
transport = srv.transport; transport = srv.transport;
host = srv.host; host = srv.host;
port = srv.port and string.format("%d", srv.port) or nil; port = srv.port and string.format("%d", srv.port) or nil;
username = srv.username; username = srv.username;
password = srv.password; password = srv.password;
expires = srv.expires and dt.datetime(srv.expires) or nil; expires = srv.expires and dt.datetime(srv.expires) or nil;
restricted = srv.restricted and "1" or nil; restricted = srv.restricted and "1" or nil;
}) }):up();
end end
return res_services; return reply;
end end
local function handle_services(event) local function handle_services(event)
@ -164,12 +154,24 @@ local function handle_services(event)
return true; return true;
end 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 local requested_type = action.attr.type;
reply:tag("service", srv):up(); if requested_type then
services:filter(function(item)
return item.type == requested_type;
end);
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); origin.send(reply);
return true; return true;
end end
@ -179,54 +181,40 @@ local function handle_credentials(event)
local action = stanza.tags[1]; local action = stanza.tags[1];
if origin.type ~= "c2s" then 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; return true;
end end
local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns }); local services = get_services();
local extras = module:get_host_items("external_service");
local services = ( configured_services + extras ) / prepare;
services:filter(function (item) services:filter(function (item)
return item.restricted; return item.restricted;
end) end)
local requested_credentials = {}; local requested_credentials = set.new();
for service in action:childtags("service") do for service in action:childtags("service") do
table.insert(requested_credentials, { if not service.attr.type or not service.attr.host then
type = service.attr.type; origin.send(st.error_reply(stanza, "modify", "bad-request"));
host = service.attr.host; return true;
port = tonumber(service.attr.port); end
});
end
setmetatable(services, services_mt); requested_credentials:add(string.format("%s:%s:%d", service.attr.type, service.attr.host,
setmetatable(requested_credentials, services_mt); tonumber(service.attr.port) or 0));
end
module:fire_event("external_service/credentials", { module:fire_event("external_service/credentials", {
origin = origin; origin = origin;
stanza = stanza; stanza = stanza;
reply = reply;
requested_credentials = requested_credentials; requested_credentials = requested_credentials;
services = services; services = services;
}); });
for req_srv in action:childtags("service") do services:filter(function (srv)
for _, srv in ipairs(services) do local port_key = string.format("%s:%s:%d", srv.type, srv.host, srv.port or 0);
if srv.type == req_srv.attr.type and srv.host == req_srv.attr.host local portless_key = string.format("%s:%s:%d", srv.type, srv.host, 0);
and not req_srv.attr.port or srv.port == tonumber(req_srv.attr.port) then return requested_credentials:contains(port_key) or requested_credentials:contains(portless_key);
reply:tag("service", { end);
type = srv.type;
transport = srv.transport; local reply = st.reply(stanza):add_child(services_xml(services, action.name, action.attr.xmlns));
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
origin.send(reply); origin.send(reply);
return true; return true;

View File

@ -1,5 +1,7 @@
local st = require "util.stanza"; 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 -- Jitsi Connection Optimization
-- gathers needed information and pushes it with a message to clients -- 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(); stanza:add_child(query):up();
--- get turnservers and credentials --- get turnservers and credentials
local services = get_services(); stanza:add_child(services_xml(get_services()));
stanza:tag("services");
for _, srv in ipairs(services) do
stanza:tag("service", srv):up();
end
session.send(stanza); session.send(stanza);
end); end);