Comparison

plugins/mod_admin_shell.lua @ 12865:e6324117f124

mod_admin_shell: Add muc:occupants(room) command to list occupants Easier than going trough muc:room():each_occupant() since you have to do fiddly things to reach the print() function.
author Kim Alvefur <zash@zash.se>
date Sun, 29 Jan 2023 17:41:08 +0100
parent 12789:517b5702c5a1
child 12866:54aea2622459
comparison
equal deleted inserted replaced
12864:9f9633364044 12865:e6324117f124
23 local prosody = _G.prosody; 23 local prosody = _G.prosody;
24 24
25 local unpack = table.unpack; 25 local unpack = table.unpack;
26 local iterators = require "util.iterators"; 26 local iterators = require "util.iterators";
27 local keys, values = iterators.keys, iterators.values; 27 local keys, values = iterators.keys, iterators.values;
28 local jid_bare, jid_split, jid_join = import("util.jid", "bare", "prepped_split", "join"); 28 local jid_bare, jid_split, jid_join, jid_resource = import("util.jid", "bare", "prepped_split", "join", "resource");
29 local set, array = require "util.set", require "util.array"; 29 local set, array = require "util.set", require "util.array";
30 local cert_verify_identity = require "util.x509".verify_identity; 30 local cert_verify_identity = require "util.x509".verify_identity;
31 local envload = require "util.envload".envload; 31 local envload = require "util.envload".envload;
32 local envloadfile = require "util.envload".envloadfile; 32 local envloadfile = require "util.envload".envloadfile;
33 local has_pposix, pposix = pcall(require, "util.pposix"); 33 local has_pposix, pposix = pcall(require, "util.pposix");
287 elseif section == "muc" then 287 elseif section == "muc" then
288 -- TODO `muc:room():foo()` commands 288 -- TODO `muc:room():foo()` commands
289 print [[muc:create(roomjid, { config }) - Create the specified MUC room with the given config]] 289 print [[muc:create(roomjid, { config }) - Create the specified MUC room with the given config]]
290 print [[muc:list(host) - List rooms on the specified MUC component]] 290 print [[muc:list(host) - List rooms on the specified MUC component]]
291 print [[muc:room(roomjid) - Reference the specified MUC room to access MUC API methods]] 291 print [[muc:room(roomjid) - Reference the specified MUC room to access MUC API methods]]
292 print [[muc:occupants(roomjid, filter) - List room occupants, optionally filtered on substring or role]]
292 elseif section == "server" then 293 elseif section == "server" then
293 print [[server:version() - Show the server's version number]] 294 print [[server:version() - Show the server's version number]]
294 print [[server:uptime() - Show how long the server has been running]] 295 print [[server:uptime() - Show how long the server has been running]]
295 print [[server:memory() - Show details about the server's memory usage]] 296 print [[server:memory() - Show details about the server's memory usage]]
296 print [[server:shutdown(reason) - Shut down the server, with an optional reason to be broadcast to all connections]] 297 print [[server:shutdown(reason) - Shut down the server, with an optional reason to be broadcast to all connections]]
1382 c = c + 1; 1383 c = c + 1;
1383 end 1384 end
1384 return true, c.." rooms"; 1385 return true, c.." rooms";
1385 end 1386 end
1386 1387
1388 function def_env.muc:occupants(room_jid, filter)
1389 local room_name, host = check_muc(room_jid);
1390 if not room_name then
1391 return room_name, host;
1392 end
1393 local room_obj = prosody.hosts[host].modules.muc.get_room_from_jid(room_jid);
1394 if not room_obj then
1395 return nil, "No such room: " .. room_jid;
1396 end
1397
1398 local print = self.session.print;
1399 local total, displayed = 0, 0;
1400 for nick_jid, occupant in room_obj:each_occupant() do
1401 local nick = jid_resource(nick_jid);
1402 if filter == nil or occupant.role == filter or nick:find(filter, 1, true) then
1403 print(occupant.role, nick);
1404 displayed = displayed + 1;
1405 end
1406 total = total + 1
1407 end
1408
1409 if total == displayed then
1410 return true, ("%d occupant%s listed"):format(total, total ~= 1 and "s" or "")
1411 else
1412 return true, ("%d out of %d occupant%s listed"):format(displayed, total, total ~= 1 and "s" or "")
1413 end
1414 end
1415
1387 local um = require"core.usermanager"; 1416 local um = require"core.usermanager";
1388 1417
1389 def_env.user = {}; 1418 def_env.user = {};
1390 function def_env.user:create(jid, password, role) 1419 function def_env.user:create(jid, password, role)
1391 local username, host = jid_split(jid); 1420 local username, host = jid_split(jid);