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