2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local config = require "core.configmanager";
11 local encodings = require "util.encodings";
12 local stringprep = encodings.stringprep;
13 local usermanager = require "core.usermanager";
14 local signal = require "util.signal";
16 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
18 local io, os = io, os;
19 local tostring, tonumber = tostring, tonumber;
21 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
25 function adduser(params)
26 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
28 return false, "invalid-username";
30 return false, "invalid-hostname";
33 local ok = usermanager.create_user(user, password, host);
35 return false, "unable-to-save-data";
40 function user_exists(params)
41 return usermanager.user_exists(params.user, params.host);
44 function passwd(params)
45 if not _M.user_exists(params) then
46 return false, "no-such-user";
49 return _M.adduser(params);
52 function deluser(params)
53 if not _M.user_exists(params) then
54 return false, "no-such-user";
56 params.password = nil;
58 return _M.adduser(params);
62 local pidfile = config.get("*", "core", "pidfile");
64 return false, "no-pidfile";
67 local file, err = io.open(pidfile);
69 return false, "pidfile-read-failed", err;
72 local pid = tonumber(file:read("*a"));
76 return false, "invalid-pid";
83 local ok, pid, err = _M.getpid();
85 if pid == "pidfile-read-failed" then
86 -- Report as not running, since we can't open the pidfile
87 -- (it probably doesn't exist)
92 return true, signal.kill(pid, 0) == 0;
96 local ok, ret = _M.isrunning();
101 return false, "already-running";
103 if not CFG_SOURCEDIR then
104 os.execute("./prosody");
105 elseif CFG_SOURCEDIR:match("^/usr/local") then
106 os.execute("/usr/local/bin/prosody");
108 os.execute("prosody");
114 local ok, ret = _M.isrunning();
119 return false, "not-running";
122 local ok, pid = _M.getpid()
123 if not ok then return false, pid; end
125 signal.kill(pid, signal.SIGTERM);