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 os.execute("stty raw -echo");
141 local ok, char = pcall(io.read, n or 1);
142 os.execute("stty sane");
148 local function getpass()
149 os.execute("stty -echo");
150 local ok, pass = pcall(io.read, "*l");
151 os.execute("stty sane");
158 function show_yesno(prompt)
159 io.write(prompt, " ");
160 local choice = getchar():lower();
162 if not choice:match("%a") then
163 choice = prompt:match("%[.-(%U).-%]$");
164 if not choice then return nil; end
166 return (choice == "y");
169 local function read_password()
172 io.write("Enter new password: ");
173 password = getpass();
175 show_message("No password - cancelled");
178 io.write("Retype new password: ");
179 if getpass() ~= password then
180 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
190 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
191 -----------------------
193 local command = arg[1];
195 function commands.adduser(arg)
196 if not arg[1] or arg[1] == "--help" then
197 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
200 local user, host = arg[1]:match("([^@]+)@(.+)");
201 if not user and host then
202 show_message [[Failed to understand JID, please supply the JID you want to create]]
203 show_usage [[adduser user@host]]
208 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
212 if prosodyctl.user_exists{ user = user, host = host } then
213 show_message [[That user already exists]];
217 if not hosts[host] then
218 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
219 show_warning("The user will not be able to log in until this is changed.");
222 local password = read_password();
223 if not password then return 1; end
225 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
227 if ok then return 0; end
229 show_message(error_messages[msg])
233 function commands.passwd(arg)
234 if not arg[1] or arg[1] == "--help" then
235 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
238 local user, host = arg[1]:match("([^@]+)@(.+)");
239 if not user and host then
240 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
241 show_usage [[passwd user@host]]
246 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
250 if not prosodyctl.user_exists { user = user, host = host } then
251 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
255 local password = read_password();
256 if not password then return 1; end
258 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
260 if ok then return 0; end
262 show_message(error_messages[msg])
266 function commands.deluser(arg)
267 if not arg[1] or arg[1] == "--help" then
268 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
271 local user, host = arg[1]:match("([^@]+)@(.+)");
272 if not user and host then
273 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
274 show_usage [[passwd user@host]]
279 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
283 if not prosodyctl.user_exists { user = user, host = host } then
284 show_message [[That user does not exist on this server]]
288 local ok, msg = prosodyctl.passwd { user = user, host = host };
290 if ok then return 0; end
292 show_message(error_messages[msg])
296 function commands.start(arg)
297 if arg[1] == "--help" then
298 show_usage([[start]], [[Start Prosody]]);
301 local ok, ret = prosodyctl.isrunning();
303 show_message(error_messages[ret]);
308 local ok, ret = prosodyctl.getpid();
310 show_message("Couldn't get running Prosody's PID");
311 show_message(error_messages[ret]);
314 show_message("Prosody is already running with PID %s", ret or "(unknown)");
318 local ok, ret = prosodyctl.start();
322 local ok, running = prosodyctl.isrunning();
323 if ok and running then
326 show_message("Still waiting...");
327 elseif i >= prosodyctl_timeout then
328 show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
334 show_message("Started");
338 show_message("Failed to start Prosody");
339 show_message(error_messages[ret])
343 function commands.status(arg)
344 if arg[1] == "--help" then
345 show_usage([[status]], [[Reports the running status of Prosody]]);
349 local ok, ret = prosodyctl.isrunning();
351 show_message(error_messages[ret]);
356 local ok, ret = prosodyctl.getpid();
358 show_message("Couldn't get running Prosody's PID");
359 show_message(error_messages[ret]);
362 show_message("Prosody is running with PID %s", ret or "(unknown)");
365 show_message("Prosody is not running");
366 if not switched_user and current_uid ~= 0 then
368 print(" You will also see this if prosodyctl is not running under");
369 print(" the same user account as Prosody. Try running as root (e.g. ");
370 print(" with 'sudo' in front) to gain access to Prosody's real status.");
377 function commands.stop(arg)
378 if arg[1] == "--help" then
379 show_usage([[stop]], [[Stop a running Prosody server]]);
383 if not prosodyctl.isrunning() then
384 show_message("Prosody is not running");
388 local ok, ret = prosodyctl.stop();
392 local ok, running = prosodyctl.isrunning();
393 if ok and not running then
396 show_message("Still waiting...");
397 elseif i >= prosodyctl_timeout then
398 show_message("Prosody is still running. Please give it some time or check your log files for errors.");
404 show_message("Stopped");
408 show_message(error_messages[ret]);
412 -- ejabberdctl compatibility
414 function commands.register(arg)
415 local user, host, password = unpack(arg);
416 if (not (user and host)) or arg[1] == "--help" then
417 if user ~= "--help" then
419 show_message [[No username specified]]
421 show_message [[Please specify which host you want to register the user on]];
424 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
428 password = read_password();
430 show_message [[Unable to register user with no password]];
435 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
437 if ok then return 0; end
439 show_message(error_messages[msg])
443 function commands.unregister(arg)
444 local user, host = unpack(arg);
445 if (not (user and host)) or arg[1] == "--help" then
446 if user ~= "--help" then
448 show_message [[No username specified]]
450 show_message [[Please specify which host you want to unregister the user from]];
453 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
457 local ok, msg = prosodyctl.deluser { user = user, host = host };
459 if ok then return 0; end
461 show_message(error_messages[msg])
465 function commands.addplugin(arg)
467 if url:match("^http://") then
468 local http = require "socket.http";
469 show_message("Fetching...");
470 local code, err = http.request(url);
472 show_message("Failed: "..err);
475 if url:match("%.lua$") then
476 local ok, err = datamanager.store(url:match("/mod_([^/]+)$"), "*", "plugins", {code});
478 show_message("Failed to save to data store: "..err);
482 show_message("Saved. Don't forget to load the module using the config file or admin console!");
484 show_message("Sorry, I don't understand how to fetch plugins from there.");
488 ---------------------
490 if command and command:match("^mod_") then -- Is a command in a module
491 local module_name = command:match("^mod_(.+)");
492 local ret, err = modulemanager.load("*", module_name);
494 show_message("Failed to load module '"..module_name.."': "..err);
498 table.remove(arg, 1);
500 local module = modulemanager.get_module("*", module_name);
502 show_message("Failed to load module '"..module_name.."': Unknown error");
506 if not modulemanager.module_has_method(module, "command") then
507 show_message("Fail: mod_"..module_name.." does not support any commands");
511 local ok, ret = modulemanager.call_module_method(module, "command", arg);
513 if type(ret) == "number" then
515 elseif type(ret) == "string" then
520 show_message("Failed to execute command: "..error_messages[ret]);
525 if not commands[command] then -- Show help for all commands
526 function show_usage(usage, desc)
531 print("prosodyctl - Manage a Prosody server");
533 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
535 print("Where COMMAND may be one of:\n");
537 local hidden_commands = require "util.set".new{ "register", "unregister" };
538 local commands_order = { "adduser", "passwd", "deluser" };
542 for _, command_name in ipairs(commands_order) do
543 local command = commands[command_name];
547 done[command_name] = true;
551 for command_name, command in pairs(commands) do
552 if not done[command_name] and not hidden_commands:contains(command_name) then
555 done[command_name] = true;
563 os.exit(commands[command]({ select(2, unpack(arg)) }));