Comparison

plugins/mod_admin_shell.lua @ 13736:42367f7e6e94 13.0

mod_admin_shell: Fix simple command execution (e.g. help)
author Matthew Wild <mwild1@gmail.com>
date Mon, 17 Feb 2025 19:10:26 +0000
parent 13735:5e8a707f1dbf
child 13737:46e7cc4de5e6
comparison
equal deleted inserted replaced
13735:5e8a707f1dbf 13736:42367f7e6e94
254 session.env.output:configure(); 254 session.env.output:configure();
255 255
256 return session; 256 return session;
257 end 257 end
258 258
259 local function process_cmd_line(arg_line) 259 local function process_cmd_line(session, arg_line)
260 local chunk = load("return "..arg_line, "=shell", "t", {}); 260 local chunk = load("return "..arg_line, "=shell", "t", {});
261 local ok, args = pcall(chunk); 261 local ok, args = pcall(chunk);
262 if not ok then return nil, args; end 262 if not ok then return nil, args; end
263 263
264 local section_name, command = args[1], args[2]; 264 local section_name, command = args[1], args[2];
266 local section_mt = getmetatable(def_env[section_name]); 266 local section_mt = getmetatable(def_env[section_name]);
267 local section_help = section_mt and section_mt.help; 267 local section_help = section_mt and section_mt.help;
268 local command_help = section_help and section_help.commands[command]; 268 local command_help = section_help and section_help.commands[command];
269 269
270 if not command_help then 270 if not command_help then
271 if commands[section_name] then
272 commands[section_name](session, table.concat(args, " "));
273 return;
274 end
275 if section_help then
276 return nil, "Command not found or necessary module not loaded. Try 'help "..section_name.." for a list of available commands.";
277 end
271 return nil, "Command not found. Is the necessary module loaded?"; 278 return nil, "Command not found. Is the necessary module loaded?";
272 end 279 end
273 280
274 local fmt = { "%s"; ":%s("; ")" }; 281 local fmt = { "%s"; ":%s("; ")" };
275 282
388 395
389 local source; 396 local source;
390 if line:match("^{") then 397 if line:match("^{") then
391 -- Input is a serialized array of strings, typically from 398 -- Input is a serialized array of strings, typically from
392 -- a command-line invocation of 'prosodyctl shell something' 399 -- a command-line invocation of 'prosodyctl shell something'
393 source, flags = process_cmd_line(line); 400 source, flags = process_cmd_line(session, line);
394 if not source then 401 if not source then
395 send_result(false, flags); 402 if flags then -- err
403 send_result(false, flags);
404 else -- no err, but nothing more to do
405 -- This happens if it was a "simple" command
406 event.origin.send(result);
407 end
396 return; 408 return;
397 end 409 end
398 end 410 end
399 411
400 local chunkname = "=console"; 412 local chunkname = "=console";