Comparison

plugins/mod_admin_telnet.lua @ 10507:4d3549e64489

mod_admin_telnet: Refactor internal function for listing hosts Splits out a function that doesn't deal with modules for reuse elsewhere
author Kim Alvefur <zash@zash.se>
date Sun, 15 Dec 2019 22:07:24 +0100
parent 10506:af214e2739f5
child 10508:edd323b30de1
comparison
equal deleted inserted replaced
10506:af214e2739f5 10507:4d3549e64489
362 return true, "OK"; 362 return true, "OK";
363 end 363 end
364 364
365 def_env.module = {}; 365 def_env.module = {};
366 366
367 local function get_hosts_set(hosts, module) 367 local function get_hosts_set(hosts)
368 if type(hosts) == "table" then 368 if type(hosts) == "table" then
369 if hosts[1] then 369 if hosts[1] then
370 return set.new(hosts); 370 return set.new(hosts);
371 elseif hosts._items then 371 elseif hosts._items then
372 return hosts; 372 return hosts;
373 end 373 end
374 elseif type(hosts) == "string" then 374 elseif type(hosts) == "string" then
375 return set.new { hosts }; 375 return set.new { hosts };
376 elseif hosts == nil then 376 elseif hosts == nil then
377 local hosts_set = set.new(array.collect(keys(prosody.hosts))) 377 return set.new(array.collect(keys(prosody.hosts)));
378 / function (host) return (prosody.hosts[host].type == "local" or module and modulemanager.is_loaded(host, module)) and host or nil; end; 378 end
379 if module and modulemanager.get_module("*", module) then 379 end
380 hosts_set:add("*"); 380
381 end 381 -- Hosts with a module or all virtualhosts if no module given
382 return hosts_set; 382 -- matching modules_enabled in the global section
383 end 383 local function get_hosts_with_module(hosts, module)
384 local hosts_set = get_hosts_set(hosts)
385 / function (host) return (prosody.hosts[host].type == "local" or module and modulemanager.is_loaded(host, module)) and host or nil; end;
386 if module and modulemanager.get_module("*", module) then
387 hosts_set:add("*");
388 end
389 return hosts_set;
384 end 390 end
385 391
386 function def_env.module:load(name, hosts, config) 392 function def_env.module:load(name, hosts, config)
387 hosts = get_hosts_set(hosts); 393 hosts = get_hosts_with_module(hosts);
388 394
389 -- Load the module for each host 395 -- Load the module for each host
390 local ok, err, count, mod = true, nil, 0; 396 local ok, err, count, mod = true, nil, 0;
391 for host in hosts do 397 for host in hosts do
392 if (not modulemanager.is_loaded(host, name)) then 398 if (not modulemanager.is_loaded(host, name)) then
409 415
410 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); 416 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
411 end 417 end
412 418
413 function def_env.module:unload(name, hosts) 419 function def_env.module:unload(name, hosts)
414 hosts = get_hosts_set(hosts, name); 420 hosts = get_hosts_with_module(hosts, name);
415 421
416 -- Unload the module for each host 422 -- Unload the module for each host
417 local ok, err, count = true, nil, 0; 423 local ok, err, count = true, nil, 0;
418 for host in hosts do 424 for host in hosts do
419 if modulemanager.is_loaded(host, name) then 425 if modulemanager.is_loaded(host, name) then
435 elseif b == "*" then return false 441 elseif b == "*" then return false
436 else return a:gsub("[^.]+", string.reverse):reverse() < b:gsub("[^.]+", string.reverse):reverse(); end 442 else return a:gsub("[^.]+", string.reverse):reverse() < b:gsub("[^.]+", string.reverse):reverse(); end
437 end 443 end
438 444
439 function def_env.module:reload(name, hosts) 445 function def_env.module:reload(name, hosts)
440 hosts = array.collect(get_hosts_set(hosts, name)):sort(_sort_hosts) 446 hosts = array.collect(get_hosts_with_module(hosts, name)):sort(_sort_hosts)
441 447
442 -- Reload the module for each host 448 -- Reload the module for each host
443 local ok, err, count = true, nil, 0; 449 local ok, err, count = true, nil, 0;
444 for _, host in ipairs(hosts) do 450 for _, host in ipairs(hosts) do
445 if modulemanager.is_loaded(host, name) then 451 if modulemanager.is_loaded(host, name) then