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.1";
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 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
94 print("For more help send the below error to us through http://prosody.im/discuss");
95 print(tostring(pposix))
98 local error_messages = setmetatable({
99 ["invalid-username"] = "The given username is invalid in a Jabber ID";
100 ["invalid-hostname"] = "The given hostname is invalid";
101 ["no-password"] = "No password was supplied";
102 ["no-such-user"] = "The given user does not exist on the server";
103 ["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
104 ["no-pidfile"] = "There is no 'pidfile' option in the configuration file, see http://prosody.im/doc/prosodyctl#pidfile for help";
105 ["no-such-method"] = "This module has no commands";
106 ["not-running"] = "Prosody is not running";
107 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
109 local events = require "util.events".new();
112 prosody = { hosts = hosts, events = events };
114 for hostname, config in pairs(config.getconfig()) do
115 hosts[hostname] = { events = events };
118 require "core.modulemanager"
120 require "util.prosodyctl"
122 -----------------------
124 function show_message(msg, ...)
125 print(msg:format(...));
128 function show_warning(msg, ...)
129 print(msg:format(...));
132 function show_usage(usage, desc)
133 print("Usage: "..arg[0].." "..usage);
139 local function getchar(n)
140 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
142 if stty_ret == 0 then
143 ok, char = pcall(io.read, n or 1);
144 os.execute("stty sane");
146 ok, char = pcall(io.read, "*l");
148 char = char:sub(1, n or 1);
156 local function getpass()
157 local stty_ret = os.execute("stty -echo 2>/dev/null");
158 if stty_ret ~= 0 then
159 io.write("\027[08m"); -- ANSI 'hidden' text attribute
161 local ok, pass = pcall(io.read, "*l");
162 if stty_ret == 0 then
163 os.execute("stty sane");
165 io.write("\027[00m");
173 function show_yesno(prompt)
174 io.write(prompt, " ");
175 local choice = getchar():lower();
177 if not choice:match("%a") then
178 choice = prompt:match("%[.-(%U).-%]$");
179 if not choice then return nil; end
181 return (choice == "y");
184 local function read_password()
187 io.write("Enter new password: ");
188 password = getpass();
190 show_message("No password - cancelled");
193 io.write("Retype new password: ");
194 if getpass() ~= password then
195 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
205 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
206 -----------------------
208 local command = arg[1];
210 function commands.adduser(arg)
211 if not arg[1] or arg[1] == "--help" then
212 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
215 local user, host = arg[1]:match("([^@]+)@(.+)");
216 if not user and host then
217 show_message [[Failed to understand JID, please supply the JID you want to create]]
218 show_usage [[adduser user@host]]
223 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
227 if prosodyctl.user_exists{ user = user, host = host } then
228 show_message [[That user already exists]];
232 if not hosts[host] then
233 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
234 show_warning("The user will not be able to log in until this is changed.");
237 local password = read_password();
238 if not password then return 1; end
240 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
242 if ok then return 0; end
244 show_message(error_messages[msg])
248 function commands.passwd(arg)
249 if not arg[1] or arg[1] == "--help" then
250 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
253 local user, host = arg[1]:match("([^@]+)@(.+)");
254 if not user and host then
255 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
256 show_usage [[passwd user@host]]
261 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
265 if not prosodyctl.user_exists { user = user, host = host } then
266 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
270 local password = read_password();
271 if not password then return 1; end
273 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
275 if ok then return 0; end
277 show_message(error_messages[msg])
281 function commands.deluser(arg)
282 if not arg[1] or arg[1] == "--help" then
283 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
286 local user, host = arg[1]:match("([^@]+)@(.+)");
287 if not user and host then
288 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
289 show_usage [[passwd user@host]]
294 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
298 if not prosodyctl.user_exists { user = user, host = host } then
299 show_message [[That user does not exist on this server]]
303 local ok, msg = prosodyctl.passwd { user = user, host = host };
305 if ok then return 0; end
307 show_message(error_messages[msg])
311 function commands.start(arg)
312 if arg[1] == "--help" then
313 show_usage([[start]], [[Start Prosody]]);
316 local ok, ret = prosodyctl.isrunning();
318 show_message(error_messages[ret]);
323 local ok, ret = prosodyctl.getpid();
325 show_message("Couldn't get running Prosody's PID");
326 show_message(error_messages[ret]);
329 show_message("Prosody is already running with PID %s", ret or "(unknown)");
333 local ok, ret = prosodyctl.start();
337 local ok, running = prosodyctl.isrunning();
338 if ok and running then
341 show_message("Still waiting...");
342 elseif i >= prosodyctl_timeout then
343 show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
349 show_message("Started");
353 show_message("Failed to start Prosody");
354 show_message(error_messages[ret])
358 function commands.status(arg)
359 if arg[1] == "--help" then
360 show_usage([[status]], [[Reports the running status of Prosody]]);
364 local ok, ret = prosodyctl.isrunning();
366 show_message(error_messages[ret]);
371 local ok, ret = prosodyctl.getpid();
373 show_message("Couldn't get running Prosody's PID");
374 show_message(error_messages[ret]);
377 show_message("Prosody is running with PID %s", ret or "(unknown)");
380 show_message("Prosody is not running");
381 if not switched_user and current_uid ~= 0 then
383 print(" You will also see this if prosodyctl is not running under");
384 print(" the same user account as Prosody. Try running as root (e.g. ");
385 print(" with 'sudo' in front) to gain access to Prosody's real status.");
392 function commands.stop(arg)
393 if arg[1] == "--help" then
394 show_usage([[stop]], [[Stop a running Prosody server]]);
398 if not prosodyctl.isrunning() then
399 show_message("Prosody is not running");
403 local ok, ret = prosodyctl.stop();
407 local ok, running = prosodyctl.isrunning();
408 if ok and not running then
411 show_message("Still waiting...");
412 elseif i >= prosodyctl_timeout then
413 show_message("Prosody is still running. Please give it some time or check your log files for errors.");
419 show_message("Stopped");
423 show_message(error_messages[ret]);
427 -- ejabberdctl compatibility
429 function commands.register(arg)
430 local user, host, password = unpack(arg);
431 if (not (user and host)) or arg[1] == "--help" then
432 if user ~= "--help" then
434 show_message [[No username specified]]
436 show_message [[Please specify which host you want to register the user on]];
439 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
443 password = read_password();
445 show_message [[Unable to register user with no password]];
450 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
452 if ok then return 0; end
454 show_message(error_messages[msg])
458 function commands.unregister(arg)
459 local user, host = unpack(arg);
460 if (not (user and host)) or arg[1] == "--help" then
461 if user ~= "--help" then
463 show_message [[No username specified]]
465 show_message [[Please specify which host you want to unregister the user from]];
468 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
472 local ok, msg = prosodyctl.deluser { user = user, host = host };
474 if ok then return 0; end
476 show_message(error_messages[msg])
480 local http_errors = {
481 [404] = "Plugin not found, did you type the address correctly?"
484 function commands.addplugin(arg)
486 if url:match("^http://") then
487 local http = require "socket.http";
488 show_message("Fetching...");
489 local code, err = http.request(url);
490 if not code or not tostring(err):match("^[23]") then
491 show_message("Failed: "..(http_errors[err] or ("HTTP error "..err)));
494 if url:match("%.lua$") then
495 local ok, err = datamanager.store(url:match("/mod_([^/]+)$"), "*", "plugins", {code});
497 show_message("Failed to save to data store: "..err);
501 show_message("Saved. Don't forget to load the module using the config file or admin console!");
503 show_message("Sorry, I don't understand how to fetch plugins from there.");
507 ---------------------
509 if command and command:match("^mod_") then -- Is a command in a module
510 local module_name = command:match("^mod_(.+)");
511 local ret, err = modulemanager.load("*", module_name);
513 show_message("Failed to load module '"..module_name.."': "..err);
517 table.remove(arg, 1);
519 local module = modulemanager.get_module("*", module_name);
521 show_message("Failed to load module '"..module_name.."': Unknown error");
525 if not modulemanager.module_has_method(module, "command") then
526 show_message("Fail: mod_"..module_name.." does not support any commands");
530 local ok, ret = modulemanager.call_module_method(module, "command", arg);
532 if type(ret) == "number" then
534 elseif type(ret) == "string" then
539 show_message("Failed to execute command: "..error_messages[ret]);
544 if not commands[command] then -- Show help for all commands
545 function show_usage(usage, desc)
550 print("prosodyctl - Manage a Prosody server");
552 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
554 print("Where COMMAND may be one of:\n");
556 local hidden_commands = require "util.set".new{ "register", "unregister" };
557 local commands_order = { "adduser", "passwd", "deluser" };
561 for _, command_name in ipairs(commands_order) do
562 local command = commands[command_name];
566 done[command_name] = true;
570 for command_name, command in pairs(commands) do
571 if not done[command_name] and not hidden_commands:contains(command_name) then
574 done[command_name] = true;
582 os.exit(commands[command]({ select(2, unpack(arg)) }));