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 -- Required to be able to find packages installed with luarocks
33 pcall(require, "luarocks.require")
35 config = require "core.configmanager"
38 -- TODO: Check for other formats when we add support for them
39 -- Use lfs? Make a new conf/ dir?
40 local ok, level, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
43 print("**************************");
44 if level == "parser" then
45 print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
46 local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
47 print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
49 elseif level == "file" then
50 print("Prosody was unable to find the configuration file.");
51 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
52 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
53 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
55 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
57 print("**************************");
63 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
64 require "util.datamanager".set_data_path(data_path);
66 -- Switch away from root and into the prosody user --
67 local switched_user, current_uid;
69 local want_pposix_version = "0.3.2";
70 local ok, pposix = pcall(require, "util.pposix");
73 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
74 current_uid = pposix.getuid();
75 if current_uid == 0 then
77 local desired_user = config.get("*", "core", "prosody_user") or "prosody";
78 local desired_group = config.get("*", "core", "prosody_group") or desired_user;
79 local ok, err = pposix.setgid(desired_group);
81 ok, err = pposix.setuid(desired_user);
87 if not switched_user then
89 print("Warning: Couldn't switch to Prosody user/group '"..tostring(desired_user).."'/'"..tostring(desired_group).."': "..tostring(err));
93 -- Set our umask to protect data files
94 pposix.umask(config.get("*", "core", "umask") or "027");
96 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
97 print("For more help send the below error to us through http://prosody.im/discuss");
98 print(tostring(pposix))
101 local error_messages = setmetatable({
102 ["invalid-username"] = "The given username is invalid in a Jabber ID";
103 ["invalid-hostname"] = "The given hostname is invalid";
104 ["no-password"] = "No password was supplied";
105 ["no-such-user"] = "The given user does not exist on the server";
106 ["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
107 ["no-pidfile"] = "There is no 'pidfile' option in the configuration file, see http://prosody.im/doc/prosodyctl#pidfile for help";
108 ["no-such-method"] = "This module has no commands";
109 ["not-running"] = "Prosody is not running";
110 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
112 local events = require "util.events".new();
115 prosody = { hosts = hosts, events = events };
117 for hostname, config in pairs(config.getconfig()) do
118 hosts[hostname] = { events = events };
121 require "core.modulemanager"
123 require "util.prosodyctl"
125 -----------------------
127 function show_message(msg, ...)
128 print(msg:format(...));
131 function show_warning(msg, ...)
132 print(msg:format(...));
135 function show_usage(usage, desc)
136 print("Usage: "..arg[0].." "..usage);
142 local function getchar(n)
143 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
145 if stty_ret == 0 then
146 ok, char = pcall(io.read, n or 1);
147 os.execute("stty sane");
149 ok, char = pcall(io.read, "*l");
151 char = char:sub(1, n or 1);
159 local function getpass()
160 local stty_ret = os.execute("stty -echo 2>/dev/null");
161 if stty_ret ~= 0 then
162 io.write("\027[08m"); -- ANSI 'hidden' text attribute
164 local ok, pass = pcall(io.read, "*l");
165 if stty_ret == 0 then
166 os.execute("stty sane");
168 io.write("\027[00m");
176 function show_yesno(prompt)
177 io.write(prompt, " ");
178 local choice = getchar():lower();
180 if not choice:match("%a") then
181 choice = prompt:match("%[.-(%U).-%]$");
182 if not choice then return nil; end
184 return (choice == "y");
187 local function read_password()
190 io.write("Enter new password: ");
191 password = getpass();
193 show_message("No password - cancelled");
196 io.write("Retype new password: ");
197 if getpass() ~= password then
198 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
208 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
209 -----------------------
211 local command = arg[1];
213 function commands.adduser(arg)
214 if not arg[1] or arg[1] == "--help" then
215 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
218 local user, host = arg[1]:match("([^@]+)@(.+)");
219 if not user and host then
220 show_message [[Failed to understand JID, please supply the JID you want to create]]
221 show_usage [[adduser user@host]]
226 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
230 if prosodyctl.user_exists{ user = user, host = host } then
231 show_message [[That user already exists]];
235 if not hosts[host] then
236 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
237 show_warning("The user will not be able to log in until this is changed.");
240 local password = read_password();
241 if not password then return 1; end
243 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
245 if ok then return 0; end
247 show_message(error_messages[msg])
251 function commands.passwd(arg)
252 if not arg[1] or arg[1] == "--help" then
253 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
256 local user, host = arg[1]:match("([^@]+)@(.+)");
257 if not user and host then
258 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
259 show_usage [[passwd user@host]]
264 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
268 if not prosodyctl.user_exists { user = user, host = host } then
269 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
273 local password = read_password();
274 if not password then return 1; end
276 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
278 if ok then return 0; end
280 show_message(error_messages[msg])
284 function commands.deluser(arg)
285 if not arg[1] or arg[1] == "--help" then
286 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
289 local user, host = arg[1]:match("([^@]+)@(.+)");
290 if not user and host then
291 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
292 show_usage [[passwd user@host]]
297 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
301 if not prosodyctl.user_exists { user = user, host = host } then
302 show_message [[That user does not exist on this server]]
306 local ok, msg = prosodyctl.passwd { user = user, host = host };
308 if ok then return 0; end
310 show_message(error_messages[msg])
314 function commands.start(arg)
315 if arg[1] == "--help" then
316 show_usage([[start]], [[Start Prosody]]);
319 local ok, ret = prosodyctl.isrunning();
321 show_message(error_messages[ret]);
326 local ok, ret = prosodyctl.getpid();
328 show_message("Couldn't get running Prosody's PID");
329 show_message(error_messages[ret]);
332 show_message("Prosody is already running with PID %s", ret or "(unknown)");
336 local ok, ret = prosodyctl.start();
340 local ok, running = prosodyctl.isrunning();
341 if ok and running then
344 show_message("Still waiting...");
345 elseif i >= prosodyctl_timeout then
346 show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
352 show_message("Started");
356 show_message("Failed to start Prosody");
357 show_message(error_messages[ret])
361 function commands.status(arg)
362 if arg[1] == "--help" then
363 show_usage([[status]], [[Reports the running status of Prosody]]);
367 local ok, ret = prosodyctl.isrunning();
369 show_message(error_messages[ret]);
374 local ok, ret = prosodyctl.getpid();
376 show_message("Couldn't get running Prosody's PID");
377 show_message(error_messages[ret]);
380 show_message("Prosody is running with PID %s", ret or "(unknown)");
383 show_message("Prosody is not running");
384 if not switched_user and current_uid ~= 0 then
386 print(" You will also see this if prosodyctl is not running under");
387 print(" the same user account as Prosody. Try running as root (e.g. ");
388 print(" with 'sudo' in front) to gain access to Prosody's real status.");
395 function commands.stop(arg)
396 if arg[1] == "--help" then
397 show_usage([[stop]], [[Stop a running Prosody server]]);
401 if not prosodyctl.isrunning() then
402 show_message("Prosody is not running");
406 local ok, ret = prosodyctl.stop();
410 local ok, running = prosodyctl.isrunning();
411 if ok and not running then
414 show_message("Still waiting...");
415 elseif i >= prosodyctl_timeout then
416 show_message("Prosody is still running. Please give it some time or check your log files for errors.");
422 show_message("Stopped");
426 show_message(error_messages[ret]);
430 -- ejabberdctl compatibility
432 function commands.register(arg)
433 local user, host, password = unpack(arg);
434 if (not (user and host)) or arg[1] == "--help" then
435 if user ~= "--help" then
437 show_message [[No username specified]]
439 show_message [[Please specify which host you want to register the user on]];
442 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
446 password = read_password();
448 show_message [[Unable to register user with no password]];
453 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
455 if ok then return 0; end
457 show_message(error_messages[msg])
461 function commands.unregister(arg)
462 local user, host = unpack(arg);
463 if (not (user and host)) or arg[1] == "--help" then
464 if user ~= "--help" then
466 show_message [[No username specified]]
468 show_message [[Please specify which host you want to unregister the user from]];
471 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
475 local ok, msg = prosodyctl.deluser { user = user, host = host };
477 if ok then return 0; end
479 show_message(error_messages[msg])
483 local http_errors = {
484 [404] = "Plugin not found, did you type the address correctly?"
487 function commands.addplugin(arg)
489 if url:match("^http://") then
490 local http = require "socket.http";
491 show_message("Fetching...");
492 local code, err = http.request(url);
493 if not code or not tostring(err):match("^[23]") then
494 show_message("Failed: "..(http_errors[err] or ("HTTP error "..err)));
497 if url:match("%.lua$") then
498 local ok, err = datamanager.store(url:match("/mod_([^/]+)$"), "*", "plugins", {code});
500 show_message("Failed to save to data store: "..err);
504 show_message("Saved. Don't forget to load the module using the config file or admin console!");
506 show_message("Sorry, I don't understand how to fetch plugins from there.");
510 ---------------------
512 if command and command:match("^mod_") then -- Is a command in a module
513 local module_name = command:match("^mod_(.+)");
514 local ret, err = modulemanager.load("*", module_name);
516 show_message("Failed to load module '"..module_name.."': "..err);
520 table.remove(arg, 1);
522 local module = modulemanager.get_module("*", module_name);
524 show_message("Failed to load module '"..module_name.."': Unknown error");
528 if not modulemanager.module_has_method(module, "command") then
529 show_message("Fail: mod_"..module_name.." does not support any commands");
533 local ok, ret = modulemanager.call_module_method(module, "command", arg);
535 if type(ret) == "number" then
537 elseif type(ret) == "string" then
542 show_message("Failed to execute command: "..error_messages[ret]);
547 if not commands[command] then -- Show help for all commands
548 function show_usage(usage, desc)
553 print("prosodyctl - Manage a Prosody server");
555 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
557 print("Where COMMAND may be one of:\n");
559 local hidden_commands = require "util.set".new{ "register", "unregister" };
560 local commands_order = { "adduser", "passwd", "deluser" };
564 for _, command_name in ipairs(commands_order) do
565 local command = commands[command_name];
569 done[command_name] = true;
573 for command_name, command in pairs(commands) do
574 if not done[command_name] and not hidden_commands:contains(command_name) then
577 done[command_name] = true;
585 os.exit(commands[command]({ select(2, unpack(arg)) }));