Add copyright header to those files missing one
[prosody.git] / util / prosodyctl.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 usermanager = require "core.usermanager";
14 local signal = require "util.signal";
15
16 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
17
18 local io, os = io, os;
19 local tostring, tonumber = tostring, tonumber;
20
21 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
22
23 module "prosodyctl"
24
25 function adduser(params)
26         local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
27         if not user then
28                 return false, "invalid-username";
29         elseif not host then
30                 return false, "invalid-hostname";
31         end
32         
33         local ok = usermanager.create_user(user, password, host);
34         if not ok then
35                 return false, "unable-to-save-data";
36         end
37         return true;
38 end
39
40 function user_exists(params)
41         return usermanager.user_exists(params.user, params.host);
42 end
43
44 function passwd(params)
45         if not _M.user_exists(params) then
46                 return false, "no-such-user";
47         end
48         
49         return _M.adduser(params);
50 end
51
52 function deluser(params)
53         if not _M.user_exists(params) then
54                 return false, "no-such-user";
55         end
56         params.password = nil;
57         
58         return _M.adduser(params);
59 end
60
61 function getpid()
62         local pidfile = config.get("*", "core", "pidfile");
63         if not pidfile then
64                 return false, "no-pidfile";
65         end
66         
67         local file, err = io.open(pidfile);
68         if not file then
69                 return false, "pidfile-read-failed", err;
70         end
71         
72         local pid = tonumber(file:read("*a"));
73         file:close();
74         
75         if not pid then
76                 return false, "invalid-pid";
77         end
78         
79         return true, pid;
80 end
81
82 function isrunning()
83         local ok, pid, err = _M.getpid();
84         if not ok then
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)
88                         return true, false;
89                 end
90                 return ok, pid;
91         end
92         return true, signal.kill(pid, 0) == 0;
93 end
94
95 function start()
96         local ok, ret = _M.isrunning();
97         if not ok then
98                 return ok, ret;
99         end
100         if ret then
101                 return false, "already-running";
102         end
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");
107         else
108                 os.execute("prosody");
109         end
110         return true;
111 end
112
113 function stop()
114         local ok, ret = _M.isrunning();
115         if not ok then
116                 return ok, ret;
117         end
118         if not ret then
119                 return false, "not-running";
120         end
121         
122         local ok, pid = _M.getpid()
123         if not ok then return false, pid; end
124         
125         signal.kill(pid, signal.SIGTERM);
126         return true;
127 end