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 prosody = { hosts = {}, events = events, platform = "posix" };
65 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
66 require "util.datamanager".set_data_path(data_path);
68 -- Switch away from root and into the prosody user --
69 local switched_user, current_uid;
71 local want_pposix_version = "0.3.3";
72 local ok, pposix = pcall(require, "util.pposix");
75 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
76 current_uid = pposix.getuid();
77 if current_uid == 0 then
79 local desired_user = config.get("*", "core", "prosody_user") or "prosody";
80 local desired_group = config.get("*", "core", "prosody_group") or desired_user;
81 local ok, err = pposix.setgid(desired_group);
83 ok, err = pposix.setuid(desired_user);
89 if not switched_user then
91 print("Warning: Couldn't switch to Prosody user/group '"..tostring(desired_user).."'/'"..tostring(desired_group).."': "..tostring(err));
95 -- Set our umask to protect data files
96 pposix.umask(config.get("*", "core", "umask") or "027");
98 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
99 print("For more help send the below error to us through http://prosody.im/discuss");
100 print(tostring(pposix))
103 local error_messages = setmetatable({
104 ["invalid-username"] = "The given username is invalid in a Jabber ID";
105 ["invalid-hostname"] = "The given hostname is invalid";
106 ["no-password"] = "No password was supplied";
107 ["no-such-user"] = "The given user does not exist on the server";
108 ["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
109 ["no-pidfile"] = "There is no 'pidfile' option in the configuration file, see http://prosody.im/doc/prosodyctl#pidfile for help";
110 ["no-such-method"] = "This module has no commands";
111 ["not-running"] = "Prosody is not running";
112 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
114 local events = require "util.events".new();
116 hosts = prosody.hosts;
118 for hostname, config in pairs(config.getconfig()) do
119 hosts[hostname] = { events = events };
122 require "core.modulemanager"
124 require "util.prosodyctl"
126 -----------------------
128 function show_message(msg, ...)
129 print(msg:format(...));
132 function show_warning(msg, ...)
133 print(msg:format(...));
136 function show_usage(usage, desc)
137 print("Usage: "..arg[0].." "..usage);
143 local function getchar(n)
144 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
146 if stty_ret == 0 then
147 ok, char = pcall(io.read, n or 1);
148 os.execute("stty sane");
150 ok, char = pcall(io.read, "*l");
152 char = char:sub(1, n or 1);
160 local function getpass()
161 local stty_ret = os.execute("stty -echo 2>/dev/null");
162 if stty_ret ~= 0 then
163 io.write("\027[08m"); -- ANSI 'hidden' text attribute
165 local ok, pass = pcall(io.read, "*l");
166 if stty_ret == 0 then
167 os.execute("stty sane");
169 io.write("\027[00m");
177 function show_yesno(prompt)
178 io.write(prompt, " ");
179 local choice = getchar():lower();
181 if not choice:match("%a") then
182 choice = prompt:match("%[.-(%U).-%]$");
183 if not choice then return nil; end
185 return (choice == "y");
188 local function read_password()
191 io.write("Enter new password: ");
192 password = getpass();
194 show_message("No password - cancelled");
197 io.write("Retype new password: ");
198 if getpass() ~= password then
199 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
209 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
210 -----------------------
212 local command = arg[1];
214 function commands.adduser(arg)
215 if not arg[1] or arg[1] == "--help" then
216 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
219 local user, host = arg[1]:match("([^@]+)@(.+)");
220 if not user and host then
221 show_message [[Failed to understand JID, please supply the JID you want to create]]
222 show_usage [[adduser user@host]]
227 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
231 if prosodyctl.user_exists{ user = user, host = host } then
232 show_message [[That user already exists]];
236 if not hosts[host] then
237 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
238 show_warning("The user will not be able to log in until this is changed.");
241 local password = read_password();
242 if not password then return 1; end
244 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
246 if ok then return 0; end
248 show_message(error_messages[msg])
252 function commands.passwd(arg)
253 if not arg[1] or arg[1] == "--help" then
254 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
257 local user, host = arg[1]:match("([^@]+)@(.+)");
258 if not user and host then
259 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
260 show_usage [[passwd user@host]]
265 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
269 if not prosodyctl.user_exists { user = user, host = host } then
270 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
274 local password = read_password();
275 if not password then return 1; end
277 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
279 if ok then return 0; end
281 show_message(error_messages[msg])
285 function commands.deluser(arg)
286 if not arg[1] or arg[1] == "--help" then
287 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
290 local user, host = arg[1]:match("([^@]+)@(.+)");
291 if not user and host then
292 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
293 show_usage [[passwd user@host]]
298 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
302 if not prosodyctl.user_exists { user = user, host = host } then
303 show_message [[That user does not exist on this server]]
307 local ok, msg = prosodyctl.passwd { user = user, host = host };
309 if ok then return 0; end
311 show_message(error_messages[msg])
315 function commands.start(arg)
316 if arg[1] == "--help" then
317 show_usage([[start]], [[Start Prosody]]);
320 local ok, ret = prosodyctl.isrunning();
322 show_message(error_messages[ret]);
327 local ok, ret = prosodyctl.getpid();
329 show_message("Couldn't get running Prosody's PID");
330 show_message(error_messages[ret]);
333 show_message("Prosody is already running with PID %s", ret or "(unknown)");
337 local ok, ret = prosodyctl.start();
341 local ok, running = prosodyctl.isrunning();
342 if ok and running then
345 show_message("Still waiting...");
346 elseif i >= prosodyctl_timeout then
347 show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
353 show_message("Started");
357 show_message("Failed to start Prosody");
358 show_message(error_messages[ret])
362 function commands.status(arg)
363 if arg[1] == "--help" then
364 show_usage([[status]], [[Reports the running status of Prosody]]);
368 local ok, ret = prosodyctl.isrunning();
370 show_message(error_messages[ret]);
375 local ok, ret = prosodyctl.getpid();
377 show_message("Couldn't get running Prosody's PID");
378 show_message(error_messages[ret]);
381 show_message("Prosody is running with PID %s", ret or "(unknown)");
384 show_message("Prosody is not running");
385 if not switched_user and current_uid ~= 0 then
387 print(" You will also see this if prosodyctl is not running under");
388 print(" the same user account as Prosody. Try running as root (e.g. ");
389 print(" with 'sudo' in front) to gain access to Prosody's real status.");
396 function commands.stop(arg)
397 if arg[1] == "--help" then
398 show_usage([[stop]], [[Stop a running Prosody server]]);
402 if not prosodyctl.isrunning() then
403 show_message("Prosody is not running");
407 local ok, ret = prosodyctl.stop();
411 local ok, running = prosodyctl.isrunning();
412 if ok and not running then
415 show_message("Still waiting...");
416 elseif i >= prosodyctl_timeout then
417 show_message("Prosody is still running. Please give it some time or check your log files for errors.");
423 show_message("Stopped");
427 show_message(error_messages[ret]);
431 -- ejabberdctl compatibility
433 function commands.register(arg)
434 local user, host, password = unpack(arg);
435 if (not (user and host)) or arg[1] == "--help" then
436 if user ~= "--help" then
438 show_message [[No username specified]]
440 show_message [[Please specify which host you want to register the user on]];
443 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
447 password = read_password();
449 show_message [[Unable to register user with no password]];
454 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
456 if ok then return 0; end
458 show_message(error_messages[msg])
462 function commands.unregister(arg)
463 local user, host = unpack(arg);
464 if (not (user and host)) or arg[1] == "--help" then
465 if user ~= "--help" then
467 show_message [[No username specified]]
469 show_message [[Please specify which host you want to unregister the user from]];
472 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
476 local ok, msg = prosodyctl.deluser { user = user, host = host };
478 if ok then return 0; end
480 show_message(error_messages[msg])
484 local http_errors = {
485 [404] = "Plugin not found, did you type the address correctly?"
488 function commands.addplugin(arg)
490 if url:match("^http://") then
491 local http = require "socket.http";
492 show_message("Fetching...");
493 local code, err = http.request(url);
494 if not code or not tostring(err):match("^[23]") then
495 show_message("Failed: "..(http_errors[err] or ("HTTP error "..err)));
498 if url:match("%.lua$") then
499 local ok, err = datamanager.store(url:match("/mod_([^/]+)$"), "*", "plugins", {code});
501 show_message("Failed to save to data store: "..err);
505 show_message("Saved. Don't forget to load the module using the config file or admin console!");
507 show_message("Sorry, I don't understand how to fetch plugins from there.");
511 ---------------------
513 if command and command:match("^mod_") then -- Is a command in a module
514 local module_name = command:match("^mod_(.+)");
515 local ret, err = modulemanager.load("*", module_name);
517 show_message("Failed to load module '"..module_name.."': "..err);
521 table.remove(arg, 1);
523 local module = modulemanager.get_module("*", module_name);
525 show_message("Failed to load module '"..module_name.."': Unknown error");
529 if not modulemanager.module_has_method(module, "command") then
530 show_message("Fail: mod_"..module_name.." does not support any commands");
534 local ok, ret = modulemanager.call_module_method(module, "command", arg);
536 if type(ret) == "number" then
538 elseif type(ret) == "string" then
543 show_message("Failed to execute command: "..error_messages[ret]);
548 if not commands[command] then -- Show help for all commands
549 function show_usage(usage, desc)
554 print("prosodyctl - Manage a Prosody server");
556 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
558 print("Where COMMAND may be one of:\n");
560 local hidden_commands = require "util.set".new{ "register", "unregister" };
561 local commands_order = { "adduser", "passwd", "deluser" };
565 for _, command_name in ipairs(commands_order) do
566 local command = commands[command_name];
570 done[command_name] = true;
574 for command_name, command in pairs(commands) do
575 if not done[command_name] and not hidden_commands:contains(command_name) then
578 done[command_name] = true;
586 os.exit(commands[command]({ select(2, unpack(arg)) }));