util.prosodyctl: Import CFG_SOURCEDIR from the global environment (thanks macaronyde!)
[prosody.git] / util / prosodyctl.lua
1
2 local config = require "core.configmanager";
3 local encodings = require "util.encodings";
4 local stringprep = encodings.stringprep;
5 local usermanager = require "core.usermanager";
6 local signal = require "util.signal";
7
8 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
9
10 local io, os = io, os;
11 local tostring, tonumber = tostring, tonumber;
12
13 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
14
15 module "prosodyctl"
16
17 function adduser(params)
18         local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
19         if not user then
20                 return false, "invalid-username";
21         elseif not host then
22                 return false, "invalid-hostname";
23         end
24         
25         local ok = usermanager.create_user(user, password, host);
26         if not ok then
27                 return false, "unable-to-save-data";
28         end
29         return true;
30 end
31
32 function user_exists(params)
33         return usermanager.user_exists(params.user, params.host);
34 end
35
36 function passwd(params)
37         if not _M.user_exists(params) then
38                 return false, "no-such-user";
39         end
40         
41         return _M.adduser(params);
42 end
43
44 function deluser(params)
45         if not _M.user_exists(params) then
46                 return false, "no-such-user";
47         end
48         params.password = nil;
49         
50         return _M.adduser(params);
51 end
52
53 function getpid()
54         local pidfile = config.get("*", "core", "pidfile");
55         if not pidfile then
56                 return false, "no-pidfile";
57         end
58         
59         local file, err = io.open(pidfile);
60         if not file then
61                 return false, "pidfile-read-failed", ret;
62         end
63         
64         local pid = tonumber(file:read("*a"));
65         file:close();
66         
67         if not pid then
68                 return false, "invalid-pid";
69         end
70         
71         return true, pid;
72 end
73
74 function isrunning()
75         local ok, pid, err = _M.getpid();
76         if not ok then
77                 if pid == "pidfile-read-failed" then
78                         -- Report as not running, since we can't open the pidfile
79                         -- (it probably doesn't exist)
80                         return true, false;
81                 end
82                 return ok, pid;
83         end
84         return true, signal.kill(pid, 0) == 0;
85 end
86
87 function start()
88         local ok, ret = _M.isrunning();
89         if not ok then
90                 return ok, ret;
91         end
92         if ret then
93                 return false, "already-running";
94         end
95         if not CFG_SOURCEDIR then
96                 os.execute("./prosody");
97         elseif CFG_SOURCEDIR:match("^/usr/local") then
98                 os.execute("/usr/local/bin/prosody");
99         else
100                 os.execute("prosody");
101         end
102         return true;
103 end
104
105 function stop()
106         local ok, ret = _M.isrunning();
107         if not ok then
108                 return ok, ret;
109         end
110         if not ret then
111                 return false, "not-running";
112         end
113         
114         local ok, pid = _M.getpid()
115         if not ok then return false, pid; end
116         
117         signal.kill(pid, signal.SIGTERM);
118         return true;
119 end