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