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 config = require "core.configmanager"
35 -- TODO: Check for other formats when we add support for them
36 -- Use lfs? Make a new conf/ dir?
37 local ok, level, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
40 print("**************************");
41 if level == "parser" then
42 print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
43 local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
44 print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
46 elseif level == "file" then
47 print("Prosody was unable to find the configuration file.");
48 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
49 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
50 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
52 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
54 print("**************************");
60 require "core.loggingmanager"
62 if not require "util.dependencies".check_dependencies() then
66 prosody = { hosts = {}, events = events, platform = "posix" };
68 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
69 require "util.datamanager".set_data_path(data_path);
71 -- Switch away from root and into the prosody user --
72 local switched_user, current_uid;
74 local want_pposix_version = "0.3.3";
75 local ok, pposix = pcall(require, "util.pposix");
78 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
79 current_uid = pposix.getuid();
80 if current_uid == 0 then
82 local desired_user = config.get("*", "core", "prosody_user") or "prosody";
83 local desired_group = config.get("*", "core", "prosody_group") or desired_user;
84 local ok, err = pposix.setgid(desired_group);
86 ok, err = pposix.setuid(desired_user);
92 if not switched_user then
94 print("Warning: Couldn't switch to Prosody user/group '"..tostring(desired_user).."'/'"..tostring(desired_group).."': "..tostring(err));
98 -- Set our umask to protect data files
99 pposix.umask(config.get("*", "core", "umask") or "027");
101 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
102 print("For more help send the below error to us through http://prosody.im/discuss");
103 print(tostring(pposix))
106 local error_messages = setmetatable({
107 ["invalid-username"] = "The given username is invalid in a Jabber ID";
108 ["invalid-hostname"] = "The given hostname is invalid";
109 ["no-password"] = "No password was supplied";
110 ["no-such-user"] = "The given user does not exist on the server";
111 ["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
112 ["no-pidfile"] = "There is no 'pidfile' option in the configuration file, see http://prosody.im/doc/prosodyctl#pidfile for help";
113 ["no-such-method"] = "This module has no commands";
114 ["not-running"] = "Prosody is not running";
115 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
117 local events = require "util.events".new();
119 hosts = prosody.hosts;
121 for hostname, config in pairs(config.getconfig()) do
122 hosts[hostname] = { events = events };
125 require "core.modulemanager"
127 require "util.prosodyctl"
129 -----------------------
131 function show_message(msg, ...)
132 print(msg:format(...));
135 function show_warning(msg, ...)
136 print(msg:format(...));
139 function show_usage(usage, desc)
140 print("Usage: "..arg[0].." "..usage);
146 local function getchar(n)
147 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
149 if stty_ret == 0 then
150 ok, char = pcall(io.read, n or 1);
151 os.execute("stty sane");
153 ok, char = pcall(io.read, "*l");
155 char = char:sub(1, n or 1);
163 local function getpass()
164 local stty_ret = os.execute("stty -echo 2>/dev/null");
165 if stty_ret ~= 0 then
166 io.write("\027[08m"); -- ANSI 'hidden' text attribute
168 local ok, pass = pcall(io.read, "*l");
169 if stty_ret == 0 then
170 os.execute("stty sane");
172 io.write("\027[00m");
180 function show_yesno(prompt)
181 io.write(prompt, " ");
182 local choice = getchar():lower();
184 if not choice:match("%a") then
185 choice = prompt:match("%[.-(%U).-%]$");
186 if not choice then return nil; end
188 return (choice == "y");
191 local function read_password()
194 io.write("Enter new password: ");
195 password = getpass();
197 show_message("No password - cancelled");
200 io.write("Retype new password: ");
201 if getpass() ~= password then
202 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
212 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
213 -----------------------
215 local command = arg[1];
217 function commands.adduser(arg)
218 if not arg[1] or arg[1] == "--help" then
219 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
222 local user, host = arg[1]:match("([^@]+)@(.+)");
223 if not user and host then
224 show_message [[Failed to understand JID, please supply the JID you want to create]]
225 show_usage [[adduser user@host]]
230 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
234 if prosodyctl.user_exists{ user = user, host = host } then
235 show_message [[That user already exists]];
239 if not hosts[host] then
240 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
241 show_warning("The user will not be able to log in until this is changed.");
244 local password = read_password();
245 if not password then return 1; end
247 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
249 if ok then return 0; end
251 show_message(error_messages[msg])
255 function commands.passwd(arg)
256 if not arg[1] or arg[1] == "--help" then
257 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
260 local user, host = arg[1]:match("([^@]+)@(.+)");
261 if not user and host then
262 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
263 show_usage [[passwd user@host]]
268 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
272 if not prosodyctl.user_exists { user = user, host = host } then
273 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
277 local password = read_password();
278 if not password then return 1; end
280 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
282 if ok then return 0; end
284 show_message(error_messages[msg])
288 function commands.deluser(arg)
289 if not arg[1] or arg[1] == "--help" then
290 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
293 local user, host = arg[1]:match("([^@]+)@(.+)");
294 if not user and host then
295 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
296 show_usage [[passwd user@host]]
301 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
305 if not prosodyctl.user_exists { user = user, host = host } then
306 show_message [[That user does not exist on this server]]
310 local ok, msg = prosodyctl.passwd { user = user, host = host };
312 if ok then return 0; end
314 show_message(error_messages[msg])
318 function commands.start(arg)
319 if arg[1] == "--help" then
320 show_usage([[start]], [[Start Prosody]]);
323 local ok, ret = prosodyctl.isrunning();
325 show_message(error_messages[ret]);
330 local ok, ret = prosodyctl.getpid();
332 show_message("Couldn't get running Prosody's PID");
333 show_message(error_messages[ret]);
336 show_message("Prosody is already running with PID %s", ret or "(unknown)");
340 local ok, ret = prosodyctl.start();
342 if config.get("*", "core", "daemonize") ~= false then
345 local ok, running = prosodyctl.isrunning();
346 if ok and running then
349 show_message("Still waiting...");
350 elseif i >= prosodyctl_timeout then
351 show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
357 show_message("Started");
362 show_message("Failed to start Prosody");
363 show_message(error_messages[ret])
367 function commands.status(arg)
368 if arg[1] == "--help" then
369 show_usage([[status]], [[Reports the running status of Prosody]]);
373 local ok, ret = prosodyctl.isrunning();
375 show_message(error_messages[ret]);
380 local ok, ret = prosodyctl.getpid();
382 show_message("Couldn't get running Prosody's PID");
383 show_message(error_messages[ret]);
386 show_message("Prosody is running with PID %s", ret or "(unknown)");
389 show_message("Prosody is not running");
390 if not switched_user and current_uid ~= 0 then
392 print(" You will also see this if prosodyctl is not running under");
393 print(" the same user account as Prosody. Try running as root (e.g. ");
394 print(" with 'sudo' in front) to gain access to Prosody's real status.");
401 function commands.stop(arg)
402 if arg[1] == "--help" then
403 show_usage([[stop]], [[Stop a running Prosody server]]);
407 if not prosodyctl.isrunning() then
408 show_message("Prosody is not running");
412 local ok, ret = prosodyctl.stop();
416 local ok, running = prosodyctl.isrunning();
417 if ok and not running then
420 show_message("Still waiting...");
421 elseif i >= prosodyctl_timeout then
422 show_message("Prosody is still running. Please give it some time or check your log files for errors.");
428 show_message("Stopped");
432 show_message(error_messages[ret]);
436 function commands.restart(arg)
437 if arg[1] == "--help" then
438 show_usage([[restart]], [[Restart a running Prosody server]]);
442 local ret = commands.stop(arg);
444 ret = commands.start(arg);
449 -- ejabberdctl compatibility
451 function commands.register(arg)
452 local user, host, password = unpack(arg);
453 if (not (user and host)) or arg[1] == "--help" then
454 if user ~= "--help" then
456 show_message [[No username specified]]
458 show_message [[Please specify which host you want to register the user on]];
461 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
465 password = read_password();
467 show_message [[Unable to register user with no password]];
472 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
474 if ok then return 0; end
476 show_message(error_messages[msg])
480 function commands.unregister(arg)
481 local user, host = unpack(arg);
482 if (not (user and host)) or arg[1] == "--help" then
483 if user ~= "--help" then
485 show_message [[No username specified]]
487 show_message [[Please specify which host you want to unregister the user from]];
490 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
494 local ok, msg = prosodyctl.deluser { user = user, host = host };
496 if ok then return 0; end
498 show_message(error_messages[msg])
502 local http_errors = {
503 [404] = "Plugin not found, did you type the address correctly?"
506 function commands.addplugin(arg)
507 if not arg[1] or arg[1] == "--help" then
508 show_usage("addplugin URL", "Download and install a plugin from a URL");
512 if url:match("^http://") then
513 local http = require "socket.http";
514 show_message("Fetching...");
515 local code, err = http.request(url);
516 if not code or not tostring(err):match("^[23]") then
517 show_message("Failed: "..(http_errors[err] or ("HTTP error "..err)));
520 if url:match("%.lua$") then
521 local ok, err = datamanager.store(url:match("/mod_([^/]+)$"), "*", "plugins", {code});
523 show_message("Failed to save to data store: "..err);
527 show_message("Saved. Don't forget to load the module using the config file or admin console!");
529 show_message("Sorry, I don't understand how to fetch plugins from there.");
533 ---------------------
535 if command and command:match("^mod_") then -- Is a command in a module
536 local module_name = command:match("^mod_(.+)");
537 local ret, err = modulemanager.load("*", module_name);
539 show_message("Failed to load module '"..module_name.."': "..err);
543 table.remove(arg, 1);
545 local module = modulemanager.get_module("*", module_name);
547 show_message("Failed to load module '"..module_name.."': Unknown error");
551 if not modulemanager.module_has_method(module, "command") then
552 show_message("Fail: mod_"..module_name.." does not support any commands");
556 local ok, ret = modulemanager.call_module_method(module, "command", arg);
558 if type(ret) == "number" then
560 elseif type(ret) == "string" then
565 show_message("Failed to execute command: "..error_messages[ret]);
570 if not commands[command] then -- Show help for all commands
571 function show_usage(usage, desc)
576 print("prosodyctl - Manage a Prosody server");
578 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
580 print("Where COMMAND may be one of:\n");
582 local hidden_commands = require "util.set".new{ "register", "unregister", "addplugin" };
583 local commands_order = { "adduser", "passwd", "deluser", "start", "stop", "restart" };
587 for _, command_name in ipairs(commands_order) do
588 local command = commands[command_name];
592 done[command_name] = true;
596 for command_name, command in pairs(commands) do
597 if not done[command_name] and not hidden_commands:contains(command_name) then
600 done[command_name] = true;
608 os.exit(commands[command]({ select(2, unpack(arg)) }));