Merge 0.9->trunk
[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         local provider = host_session.users;
144         if not(provider) or provider.name == "null" then
145                 usermanager.initialize_host(host);
146         end
147         storagemanager.initialize_host(host);
148         
149         local ok, errmsg = usermanager.create_user(user, password, host);
150         if not ok then
151                 return false, errmsg;
152         end
153         return true;
154 end
155
156 function user_exists(params)
157         local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
158         local provider = prosody.hosts[host].users;
159         if not(provider) or provider.name == "null" then
160                 usermanager.initialize_host(host);
161         end
162         storagemanager.initialize_host(host);
163         
164         return usermanager.user_exists(user, host);
165 end
166
167 function passwd(params)
168         if not _M.user_exists(params) then
169                 return false, "no-such-user";
170         end
171         
172         return _M.adduser(params);
173 end
174
175 function deluser(params)
176         if not _M.user_exists(params) then
177                 return false, "no-such-user";
178         end
179         local user, host = nodeprep(params.user), nameprep(params.host);
180         
181         return usermanager.delete_user(user, host);
182 end
183
184 function getpid()
185         local pidfile = config.get("*", "pidfile");
186         if not pidfile then
187                 return false, "no-pidfile";
188         end
189         
190         local modules_enabled = set.new(config.get("*", "modules_enabled"));
191         if not modules_enabled:contains("posix") then
192                 return false, "no-posix";
193         end
194         
195         local file, err = io.open(pidfile, "r+");
196         if not file then
197                 return false, "pidfile-read-failed", err;
198         end
199         
200         local locked, err = lfs.lock(file, "w");
201         if locked then
202                 file:close();
203                 return false, "pidfile-not-locked";
204         end
205         
206         local pid = tonumber(file:read("*a"));
207         file:close();
208         
209         if not pid then
210                 return false, "invalid-pid";
211         end
212         
213         return true, pid;
214 end
215
216 function isrunning()
217         local ok, pid, err = _M.getpid();
218         if not ok then
219                 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then
220                         -- Report as not running, since we can't open the pidfile
221                         -- (it probably doesn't exist)
222                         return true, false;
223                 end
224                 return ok, pid;
225         end
226         return true, signal.kill(pid, 0) == 0;
227 end
228
229 function start()
230         local ok, ret = _M.isrunning();
231         if not ok then
232                 return ok, ret;
233         end
234         if ret then
235                 return false, "already-running";
236         end
237         if not CFG_SOURCEDIR then
238                 os.execute("./prosody");
239         else
240                 os.execute(CFG_SOURCEDIR.."/../../bin/prosody");
241         end
242         return true;
243 end
244
245 function stop()
246         local ok, ret = _M.isrunning();
247         if not ok then
248                 return ok, ret;
249         end
250         if not ret then
251                 return false, "not-running";
252         end
253         
254         local ok, pid = _M.getpid()
255         if not ok then return false, pid; end
256         
257         signal.kill(pid, signal.SIGTERM);
258         return true;
259 end
260
261 function reload()
262         local ok, ret = _M.isrunning();
263         if not ok then
264                 return ok, ret;
265         end
266         if not ret then
267                 return false, "not-running";
268         end
269         
270         local ok, pid = _M.getpid()
271         if not ok then return false, pid; end
272         
273         signal.kill(pid, signal.SIGHUP);
274         return true;
275 end
276
277 return _M;