Merge 0.9->0.10
[prosody.git] / util / prosodyctl.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local config = require "core.configmanager";
11 local encodings = require "util.encodings";
12 local stringprep = encodings.stringprep;
13 local storagemanager = require "core.storagemanager";
14 local usermanager = require "core.usermanager";
15 local signal = require "util.signal";
16 local set = require "util.set";
17 local lfs = require "lfs";
18 local pcall = pcall;
19 local type = type;
20
21 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
22
23 local io, os = io, os;
24 local print = print;
25 local tostring, tonumber = tostring, tonumber;
26
27 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
28
29 local _G = _G;
30 local prosody = prosody;
31
32 module "prosodyctl"
33
34 -- UI helpers
35 function show_message(msg, ...)
36         print(msg:format(...));
37 end
38
39 function show_warning(msg, ...)
40         print(msg:format(...));
41 end
42
43 function show_usage(usage, desc)
44         print("Usage: ".._G.arg[0].." "..usage);
45         if desc then
46                 print(" "..desc);
47         end
48 end
49
50 function getchar(n)
51         local stty_ret = os.execute("stty raw -echo 2>/dev/null");
52         local ok, char;
53         if stty_ret == 0 then
54                 ok, char = pcall(io.read, n or 1);
55                 os.execute("stty sane");
56         else
57                 ok, char = pcall(io.read, "*l");
58                 if ok then
59                         char = char:sub(1, n or 1);
60                 end
61         end
62         if ok then
63                 return char;
64         end
65 end
66
67 function getline()
68         local ok, line = pcall(io.read, "*l");
69         if ok then
70                 return line;
71         end
72 end
73
74 function getpass()
75         local stty_ret = os.execute("stty -echo 2>/dev/null");
76         if stty_ret ~= 0 then
77                 io.write("\027[08m"); -- ANSI 'hidden' text attribute
78         end
79         local ok, pass = pcall(io.read, "*l");
80         if stty_ret == 0 then
81                 os.execute("stty sane");
82         else
83                 io.write("\027[00m");
84         end
85         io.write("\n");
86         if ok then
87                 return pass;
88         end
89 end
90
91 function show_yesno(prompt)
92         io.write(prompt, " ");
93         local choice = getchar():lower();
94         io.write("\n");
95         if not choice:match("%a") then
96                 choice = prompt:match("%[.-(%U).-%]$");
97                 if not choice then return nil; end
98         end
99         return (choice == "y");
100 end
101
102 function read_password()
103         local password;
104         while true do
105                 io.write("Enter new password: ");
106                 password = getpass();
107                 if not password then
108                         show_message("No password - cancelled");
109                         return;
110                 end
111                 io.write("Retype new password: ");
112                 if getpass() ~= password then
113                         if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
114                                 return;
115                         end
116                 else
117                         break;
118                 end
119         end
120         return password;
121 end
122
123 function show_prompt(prompt)
124         io.write(prompt, " ");
125         local line = getline();
126         line = line and line:gsub("\n$","");
127         return (line and #line > 0) and line or nil;
128 end
129
130 -- Server control
131 function adduser(params)
132         local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
133         if not user then
134                 return false, "invalid-username";
135         elseif not host then
136                 return false, "invalid-hostname";
137         end
138
139         local host_session = prosody.hosts[host];
140         if not host_session then
141                 return false, "no-such-host";
142         end
143
144         storagemanager.initialize_host(host);
145         local provider = host_session.users;
146         if not(provider) or provider.name == "null" then
147                 usermanager.initialize_host(host);
148         end
149         
150         local ok, errmsg = usermanager.create_user(user, password, host);
151         if not ok then
152                 return false, errmsg;
153         end
154         return true;
155 end
156
157 function user_exists(params)
158         local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
159
160         storagemanager.initialize_host(host);
161         local provider = prosody.hosts[host].users;
162         if not(provider) or provider.name == "null" then
163                 usermanager.initialize_host(host);
164         end
165         
166         return usermanager.user_exists(user, host);
167 end
168
169 function passwd(params)
170         if not _M.user_exists(params) then
171                 return false, "no-such-user";
172         end
173         
174         return _M.adduser(params);
175 end
176
177 function deluser(params)
178         if not _M.user_exists(params) then
179                 return false, "no-such-user";
180         end
181         local user, host = nodeprep(params.user), nameprep(params.host);
182         
183         return usermanager.delete_user(user, host);
184 end
185
186 function getpid()
187         local pidfile = config.get("*", "pidfile");
188         if not pidfile then
189                 return false, "no-pidfile";
190         end
191
192         if type(pidfile) ~= "string" then
193                 return false, "invalid-pidfile";
194         end
195         
196         local modules_enabled = set.new(config.get("*", "modules_disabled"));
197         if prosody.platform ~= "posix" or modules_enabled:contains("posix") then
198                 return false, "no-posix";
199         end
200         
201         local file, err = io.open(pidfile, "r+");
202         if not file then
203                 return false, "pidfile-read-failed", err;
204         end
205         
206         local locked, err = lfs.lock(file, "w");
207         if locked then
208                 file:close();
209                 return false, "pidfile-not-locked";
210         end
211         
212         local pid = tonumber(file:read("*a"));
213         file:close();
214         
215         if not pid then
216                 return false, "invalid-pid";
217         end
218         
219         return true, pid;
220 end
221
222 function isrunning()
223         local ok, pid, err = _M.getpid();
224         if not ok then
225                 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then
226                         -- Report as not running, since we can't open the pidfile
227                         -- (it probably doesn't exist)
228                         return true, false;
229                 end
230                 return ok, pid;
231         end
232         return true, signal.kill(pid, 0) == 0;
233 end
234
235 function start()
236         local ok, ret = _M.isrunning();
237         if not ok then
238                 return ok, ret;
239         end
240         if ret then
241                 return false, "already-running";
242         end
243         if not CFG_SOURCEDIR then
244                 os.execute("./prosody");
245         else
246                 os.execute(CFG_SOURCEDIR.."/../../bin/prosody");
247         end
248         return true;
249 end
250
251 function stop()
252         local ok, ret = _M.isrunning();
253         if not ok then
254                 return ok, ret;
255         end
256         if not ret then
257                 return false, "not-running";
258         end
259         
260         local ok, pid = _M.getpid()
261         if not ok then return false, pid; end
262         
263         signal.kill(pid, signal.SIGTERM);
264         return true;
265 end
266
267 function reload()
268         local ok, ret = _M.isrunning();
269         if not ok then
270                 return ok, ret;
271         end
272         if not ret then
273                 return false, "not-running";
274         end
275         
276         local ok, pid = _M.getpid()
277         if not ok then return false, pid; end
278         
279         signal.kill(pid, signal.SIGHUP);
280         return true;
281 end
282
283 return _M;