util.stanza: Iterate on childtags instead of all childs.
[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 usermanager = require "core.usermanager";
14 local signal = require "util.signal";
15 local set = require "util.set";
16 local lfs = require "lfs";
17
18 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
19
20 local io, os = io, os;
21 local tostring, tonumber = tostring, tonumber;
22
23 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
24
25 local prosody = prosody;
26
27 module "prosodyctl"
28
29 function adduser(params)
30         local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
31         if not user then
32                 return false, "invalid-username";
33         elseif not host then
34                 return false, "invalid-hostname";
35         end
36
37         local provider = prosody.hosts[host].users;
38         if not(provider) or provider.name == "null" then
39                 usermanager.initialize_host(host);
40         end
41         
42         local ok = usermanager.create_user(user, password, host);
43         if not ok then
44                 return false, "unable-to-save-data";
45         end
46         return true;
47 end
48
49 function user_exists(params)
50         local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
51         local provider = prosody.hosts[host].users;
52         if not(provider) or provider.name == "null" then
53                 usermanager.initialize_host(host);
54         end
55         
56         return usermanager.user_exists(user, host);
57 end
58
59 function passwd(params)
60         if not _M.user_exists(params) then
61                 return false, "no-such-user";
62         end
63         
64         return _M.adduser(params);
65 end
66
67 function deluser(params)
68         if not _M.user_exists(params) then
69                 return false, "no-such-user";
70         end
71         params.password = nil;
72         
73         return _M.adduser(params);
74 end
75
76 function getpid()
77         local pidfile = config.get("*", "core", "pidfile");
78         if not pidfile then
79                 return false, "no-pidfile";
80         end
81         
82         local modules_enabled = set.new(config.get("*", "core", "modules_enabled"));
83         if not modules_enabled:contains("posix") then
84                 return false, "no-posix";
85         end
86         
87         local file, err = io.open(pidfile, "r+");
88         if not file then
89                 return false, "pidfile-read-failed", err;
90         end
91         
92         local locked, err = lfs.lock(file, "w");
93         if locked then
94                 file:close();
95                 return false, "pidfile-not-locked";
96         end
97         
98         local pid = tonumber(file:read("*a"));
99         file:close();
100         
101         if not pid then
102                 return false, "invalid-pid";
103         end
104         
105         return true, pid;
106 end
107
108 function isrunning()
109         local ok, pid, err = _M.getpid();
110         if not ok then
111                 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then
112                         -- Report as not running, since we can't open the pidfile
113                         -- (it probably doesn't exist)
114                         return true, false;
115                 end
116                 return ok, pid;
117         end
118         return true, signal.kill(pid, 0) == 0;
119 end
120
121 function start()
122         local ok, ret = _M.isrunning();
123         if not ok then
124                 return ok, ret;
125         end
126         if ret then
127                 return false, "already-running";
128         end
129         if not CFG_SOURCEDIR then
130                 os.execute("./prosody");
131         else
132                 os.execute(CFG_SOURCEDIR.."/../../bin/prosody");
133         end
134         return true;
135 end
136
137 function stop()
138         local ok, ret = _M.isrunning();
139         if not ok then
140                 return ok, ret;
141         end
142         if not ret then
143                 return false, "not-running";
144         end
145         
146         local ok, pid = _M.getpid()
147         if not ok then return false, pid; end
148         
149         signal.kill(pid, signal.SIGTERM);
150         return true;
151 end