3 -- Copyright (C) 2008-2009 Matthew Wild
4 -- Copyright (C) 2008-2009 Waqas Hussain
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 -- prosodyctl - command-line controller for Prosody XMPP server
12 -- Will be modified by configure script if run --
15 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
17 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
19 -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- --
22 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
23 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
27 if os.getenv("HOME") then
28 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
32 if not require "util.dependencies".check_dependencies() then
36 config = require "core.configmanager"
39 -- TODO: Check for other formats when we add support for them
40 -- Use lfs? Make a new conf/ dir?
41 local ok, level, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
44 print("**************************");
45 if level == "parser" then
46 print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
47 local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
48 print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
50 elseif level == "file" then
51 print("Prosody was unable to find the configuration file.");
52 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
53 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
54 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
56 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
58 print("**************************");
64 prosody = { hosts = {}, events = events, platform = "posix" };
66 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
67 require "util.datamanager".set_data_path(data_path);
69 -- Switch away from root and into the prosody user --
70 local switched_user, current_uid;
72 local want_pposix_version = "0.3.3";
73 local ok, pposix = pcall(require, "util.pposix");
76 if pposix._VERSION ~= want_pposix_version then print(string.format("Unknown version (%s) of binary pposix module, expected %s", tostring(pposix._VERSION), want_pposix_version)); return; end
77 current_uid = pposix.getuid();
78 if current_uid == 0 then
80 local desired_user = config.get("*", "core", "prosody_user") or "prosody";
81 local desired_group = config.get("*", "core", "prosody_group") or desired_user;
82 local ok, err = pposix.setgid(desired_group);
84 ok, err = pposix.setuid(desired_user);
90 if not switched_user then
92 print("Warning: Couldn't switch to Prosody user/group '"..tostring(desired_user).."'/'"..tostring(desired_group).."': "..tostring(err));
96 -- Set our umask to protect data files
97 pposix.umask(config.get("*", "core", "umask") or "027");
99 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
100 print("For more help send the below error to us through http://prosody.im/discuss");
101 print(tostring(pposix))
104 local error_messages = setmetatable({
105 ["invalid-username"] = "The given username is invalid in a Jabber ID";
106 ["invalid-hostname"] = "The given hostname is invalid";
107 ["no-password"] = "No password was supplied";
108 ["no-such-user"] = "The given user does not exist on the server";
109 ["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
110 ["no-pidfile"] = "There is no 'pidfile' option in the configuration file, see http://prosody.im/doc/prosodyctl#pidfile for help";
111 ["no-such-method"] = "This module has no commands";
112 ["not-running"] = "Prosody is not running";
113 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
115 local events = require "util.events".new();
117 hosts = prosody.hosts;
119 for hostname, config in pairs(config.getconfig()) do
120 hosts[hostname] = { events = events };
123 require "core.modulemanager"
125 require "util.prosodyctl"
127 -----------------------
129 function show_message(msg, ...)
130 print(msg:format(...));
133 function show_warning(msg, ...)
134 print(msg:format(...));
137 function show_usage(usage, desc)
138 print("Usage: "..arg[0].." "..usage);
144 local function getchar(n)
145 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
147 if stty_ret == 0 then
148 ok, char = pcall(io.read, n or 1);
149 os.execute("stty sane");
151 ok, char = pcall(io.read, "*l");
153 char = char:sub(1, n or 1);
161 local function getpass()
162 local stty_ret = os.execute("stty -echo 2>/dev/null");
163 if stty_ret ~= 0 then
164 io.write("\027[08m"); -- ANSI 'hidden' text attribute
166 local ok, pass = pcall(io.read, "*l");
167 if stty_ret == 0 then
168 os.execute("stty sane");
170 io.write("\027[00m");
178 function show_yesno(prompt)
179 io.write(prompt, " ");
180 local choice = getchar():lower();
182 if not choice:match("%a") then
183 choice = prompt:match("%[.-(%U).-%]$");
184 if not choice then return nil; end
186 return (choice == "y");
189 local function read_password()
192 io.write("Enter new password: ");
193 password = getpass();
195 show_message("No password - cancelled");
198 io.write("Retype new password: ");
199 if getpass() ~= password then
200 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
210 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
211 -----------------------
213 local command = arg[1];
215 function commands.adduser(arg)
216 if not arg[1] or arg[1] == "--help" then
217 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
220 local user, host = arg[1]:match("([^@]+)@(.+)");
221 if not user and host then
222 show_message [[Failed to understand JID, please supply the JID you want to create]]
223 show_usage [[adduser user@host]]
228 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
232 if prosodyctl.user_exists{ user = user, host = host } then
233 show_message [[That user already exists]];
237 if not hosts[host] then
238 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
239 show_warning("The user will not be able to log in until this is changed.");
242 local password = read_password();
243 if not password then return 1; end
245 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
247 if ok then return 0; end
249 show_message(error_messages[msg])
253 function commands.passwd(arg)
254 if not arg[1] or arg[1] == "--help" then
255 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
258 local user, host = arg[1]:match("([^@]+)@(.+)");
259 if not user and host then
260 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
261 show_usage [[passwd user@host]]
266 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
270 if not prosodyctl.user_exists { user = user, host = host } then
271 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
275 local password = read_password();
276 if not password then return 1; end
278 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
280 if ok then return 0; end
282 show_message(error_messages[msg])
286 function commands.deluser(arg)
287 if not arg[1] or arg[1] == "--help" then
288 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
291 local user, host = arg[1]:match("([^@]+)@(.+)");
292 if not user and host then
293 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
294 show_usage [[passwd user@host]]
299 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
303 if not prosodyctl.user_exists { user = user, host = host } then
304 show_message [[That user does not exist on this server]]
308 local ok, msg = prosodyctl.passwd { user = user, host = host };
310 if ok then return 0; end
312 show_message(error_messages[msg])
316 function commands.start(arg)
317 if arg[1] == "--help" then
318 show_usage([[start]], [[Start Prosody]]);
321 local ok, ret = prosodyctl.isrunning();
323 show_message(error_messages[ret]);
328 local ok, ret = prosodyctl.getpid();
330 show_message("Couldn't get running Prosody's PID");
331 show_message(error_messages[ret]);
334 show_message("Prosody is already running with PID %s", ret or "(unknown)");
338 local ok, ret = prosodyctl.start();
340 if config.get("*", "core", "daemonize") ~= false then
343 local ok, running = prosodyctl.isrunning();
344 if ok and running then
347 show_message("Still waiting...");
348 elseif i >= prosodyctl_timeout then
349 show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
355 show_message("Started");
360 show_message("Failed to start Prosody");
361 show_message(error_messages[ret])
365 function commands.status(arg)
366 if arg[1] == "--help" then
367 show_usage([[status]], [[Reports the running status of Prosody]]);
371 local ok, ret = prosodyctl.isrunning();
373 show_message(error_messages[ret]);
378 local ok, ret = prosodyctl.getpid();
380 show_message("Couldn't get running Prosody's PID");
381 show_message(error_messages[ret]);
384 show_message("Prosody is running with PID %s", ret or "(unknown)");
387 show_message("Prosody is not running");
388 if not switched_user and current_uid ~= 0 then
390 print(" You will also see this if prosodyctl is not running under");
391 print(" the same user account as Prosody. Try running as root (e.g. ");
392 print(" with 'sudo' in front) to gain access to Prosody's real status.");
399 function commands.stop(arg)
400 if arg[1] == "--help" then
401 show_usage([[stop]], [[Stop a running Prosody server]]);
405 if not prosodyctl.isrunning() then
406 show_message("Prosody is not running");
410 local ok, ret = prosodyctl.stop();
414 local ok, running = prosodyctl.isrunning();
415 if ok and not running then
418 show_message("Still waiting...");
419 elseif i >= prosodyctl_timeout then
420 show_message("Prosody is still running. Please give it some time or check your log files for errors.");
426 show_message("Stopped");
430 show_message(error_messages[ret]);
434 -- ejabberdctl compatibility
436 function commands.register(arg)
437 local user, host, password = unpack(arg);
438 if (not (user and host)) or arg[1] == "--help" then
439 if user ~= "--help" then
441 show_message [[No username specified]]
443 show_message [[Please specify which host you want to register the user on]];
446 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
450 password = read_password();
452 show_message [[Unable to register user with no password]];
457 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
459 if ok then return 0; end
461 show_message(error_messages[msg])
465 function commands.unregister(arg)
466 local user, host = unpack(arg);
467 if (not (user and host)) or arg[1] == "--help" then
468 if user ~= "--help" then
470 show_message [[No username specified]]
472 show_message [[Please specify which host you want to unregister the user from]];
475 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
479 local ok, msg = prosodyctl.deluser { user = user, host = host };
481 if ok then return 0; end
483 show_message(error_messages[msg])
487 local http_errors = {
488 [404] = "Plugin not found, did you type the address correctly?"
491 function commands.addplugin(arg)
493 if url:match("^http://") then
494 local http = require "socket.http";
495 show_message("Fetching...");
496 local code, err = http.request(url);
497 if not code or not tostring(err):match("^[23]") then
498 show_message("Failed: "..(http_errors[err] or ("HTTP error "..err)));
501 if url:match("%.lua$") then
502 local ok, err = datamanager.store(url:match("/mod_([^/]+)$"), "*", "plugins", {code});
504 show_message("Failed to save to data store: "..err);
508 show_message("Saved. Don't forget to load the module using the config file or admin console!");
510 show_message("Sorry, I don't understand how to fetch plugins from there.");
514 ---------------------
516 if command and command:match("^mod_") then -- Is a command in a module
517 local module_name = command:match("^mod_(.+)");
518 local ret, err = modulemanager.load("*", module_name);
520 show_message("Failed to load module '"..module_name.."': "..err);
524 table.remove(arg, 1);
526 local module = modulemanager.get_module("*", module_name);
528 show_message("Failed to load module '"..module_name.."': Unknown error");
532 if not modulemanager.module_has_method(module, "command") then
533 show_message("Fail: mod_"..module_name.." does not support any commands");
537 local ok, ret = modulemanager.call_module_method(module, "command", arg);
539 if type(ret) == "number" then
541 elseif type(ret) == "string" then
546 show_message("Failed to execute command: "..error_messages[ret]);
551 if not commands[command] then -- Show help for all commands
552 function show_usage(usage, desc)
557 print("prosodyctl - Manage a Prosody server");
559 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
561 print("Where COMMAND may be one of:\n");
563 local hidden_commands = require "util.set".new{ "register", "unregister" };
564 local commands_order = { "adduser", "passwd", "deluser" };
568 for _, command_name in ipairs(commands_order) do
569 local command = commands[command_name];
573 done[command_name] = true;
577 for command_name, command in pairs(commands) do
578 if not done[command_name] and not hidden_commands:contains(command_name) then
581 done[command_name] = true;
589 os.exit(commands[command]({ select(2, unpack(arg)) }));