Software /
code /
prosody
Comparison
util/prosodyctl.lua @ 6777:5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 21 Feb 2015 10:36:37 +0100 |
parent | 6771:60957dd5b41b |
child | 7259:d8300985f2bb |
comparison
equal
deleted
inserted
replaced
6774:3965662ae091 | 6777:5de6b93d0190 |
---|---|
27 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR; | 27 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR; |
28 | 28 |
29 local _G = _G; | 29 local _G = _G; |
30 local prosody = prosody; | 30 local prosody = prosody; |
31 | 31 |
32 module "prosodyctl" | |
33 | |
34 -- UI helpers | 32 -- UI helpers |
35 function show_message(msg, ...) | 33 local function show_message(msg, ...) |
36 print(msg:format(...)); | 34 print(msg:format(...)); |
37 end | 35 end |
38 | 36 |
39 function show_warning(msg, ...) | 37 local function show_usage(usage, desc) |
40 print(msg:format(...)); | |
41 end | |
42 | |
43 function show_usage(usage, desc) | |
44 print("Usage: ".._G.arg[0].." "..usage); | 38 print("Usage: ".._G.arg[0].." "..usage); |
45 if desc then | 39 if desc then |
46 print(" "..desc); | 40 print(" "..desc); |
47 end | 41 end |
48 end | 42 end |
49 | 43 |
50 function getchar(n) | 44 local function getchar(n) |
51 local stty_ret = os.execute("stty raw -echo 2>/dev/null"); | 45 local stty_ret = os.execute("stty raw -echo 2>/dev/null"); |
52 local ok, char; | 46 local ok, char; |
53 if stty_ret == 0 then | 47 if stty_ret == 0 then |
54 ok, char = pcall(io.read, n or 1); | 48 ok, char = pcall(io.read, n or 1); |
55 os.execute("stty sane"); | 49 os.execute("stty sane"); |
62 if ok then | 56 if ok then |
63 return char; | 57 return char; |
64 end | 58 end |
65 end | 59 end |
66 | 60 |
67 function getline() | 61 local function getline() |
68 local ok, line = pcall(io.read, "*l"); | 62 local ok, line = pcall(io.read, "*l"); |
69 if ok then | 63 if ok then |
70 return line; | 64 return line; |
71 end | 65 end |
72 end | 66 end |
73 | 67 |
74 function getpass() | 68 local function getpass() |
75 local stty_ret = os.execute("stty -echo 2>/dev/null"); | 69 local stty_ret = os.execute("stty -echo 2>/dev/null"); |
76 if stty_ret ~= 0 then | 70 if stty_ret ~= 0 then |
77 io.write("\027[08m"); -- ANSI 'hidden' text attribute | 71 io.write("\027[08m"); -- ANSI 'hidden' text attribute |
78 end | 72 end |
79 local ok, pass = pcall(io.read, "*l"); | 73 local ok, pass = pcall(io.read, "*l"); |
86 if ok then | 80 if ok then |
87 return pass; | 81 return pass; |
88 end | 82 end |
89 end | 83 end |
90 | 84 |
91 function show_yesno(prompt) | 85 local function show_yesno(prompt) |
92 io.write(prompt, " "); | 86 io.write(prompt, " "); |
93 local choice = getchar():lower(); | 87 local choice = getchar():lower(); |
94 io.write("\n"); | 88 io.write("\n"); |
95 if not choice:match("%a") then | 89 if not choice:match("%a") then |
96 choice = prompt:match("%[.-(%U).-%]$"); | 90 choice = prompt:match("%[.-(%U).-%]$"); |
97 if not choice then return nil; end | 91 if not choice then return nil; end |
98 end | 92 end |
99 return (choice == "y"); | 93 return (choice == "y"); |
100 end | 94 end |
101 | 95 |
102 function read_password() | 96 local function read_password() |
103 local password; | 97 local password; |
104 while true do | 98 while true do |
105 io.write("Enter new password: "); | 99 io.write("Enter new password: "); |
106 password = getpass(); | 100 password = getpass(); |
107 if not password then | 101 if not password then |
118 end | 112 end |
119 end | 113 end |
120 return password; | 114 return password; |
121 end | 115 end |
122 | 116 |
123 function show_prompt(prompt) | 117 local function show_prompt(prompt) |
124 io.write(prompt, " "); | 118 io.write(prompt, " "); |
125 local line = getline(); | 119 local line = getline(); |
126 line = line and line:gsub("\n$",""); | 120 line = line and line:gsub("\n$",""); |
127 return (line and #line > 0) and line or nil; | 121 return (line and #line > 0) and line or nil; |
128 end | 122 end |
129 | 123 |
130 -- Server control | 124 -- Server control |
131 function adduser(params) | 125 local function adduser(params) |
132 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password; | 126 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password; |
133 if not user then | 127 if not user then |
134 return false, "invalid-username"; | 128 return false, "invalid-username"; |
135 elseif not host then | 129 elseif not host then |
136 return false, "invalid-hostname"; | 130 return false, "invalid-hostname"; |
147 usermanager.initialize_host(host); | 141 usermanager.initialize_host(host); |
148 end | 142 end |
149 | 143 |
150 local ok, errmsg = usermanager.create_user(user, password, host); | 144 local ok, errmsg = usermanager.create_user(user, password, host); |
151 if not ok then | 145 if not ok then |
152 return false, errmsg; | 146 return false, errmsg or "creating-user-failed"; |
153 end | 147 end |
154 return true; | 148 return true; |
155 end | 149 end |
156 | 150 |
157 function user_exists(params) | 151 local function user_exists(params) |
158 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password; | 152 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password; |
159 | 153 |
160 storagemanager.initialize_host(host); | 154 storagemanager.initialize_host(host); |
161 local provider = prosody.hosts[host].users; | 155 local provider = prosody.hosts[host].users; |
162 if not(provider) or provider.name == "null" then | 156 if not(provider) or provider.name == "null" then |
164 end | 158 end |
165 | 159 |
166 return usermanager.user_exists(user, host); | 160 return usermanager.user_exists(user, host); |
167 end | 161 end |
168 | 162 |
169 function passwd(params) | 163 local function passwd(params) |
170 if not _M.user_exists(params) then | 164 if not user_exists(params) then |
171 return false, "no-such-user"; | 165 return false, "no-such-user"; |
172 end | 166 end |
173 | 167 |
174 return _M.adduser(params); | 168 return adduser(params); |
175 end | 169 end |
176 | 170 |
177 function deluser(params) | 171 local function deluser(params) |
178 if not _M.user_exists(params) then | 172 if not user_exists(params) then |
179 return false, "no-such-user"; | 173 return false, "no-such-user"; |
180 end | 174 end |
181 local user, host = nodeprep(params.user), nameprep(params.host); | 175 local user, host = nodeprep(params.user), nameprep(params.host); |
182 | 176 |
183 return usermanager.delete_user(user, host); | 177 return usermanager.delete_user(user, host); |
184 end | 178 end |
185 | 179 |
186 function getpid() | 180 local function getpid() |
187 local pidfile = config.get("*", "pidfile"); | 181 local pidfile = config.get("*", "pidfile"); |
188 if not pidfile then | 182 if not pidfile then |
189 return false, "no-pidfile"; | 183 return false, "no-pidfile"; |
190 end | 184 end |
191 | 185 |
217 end | 211 end |
218 | 212 |
219 return true, pid; | 213 return true, pid; |
220 end | 214 end |
221 | 215 |
222 function isrunning() | 216 local function isrunning() |
223 local ok, pid, err = _M.getpid(); | 217 local ok, pid, err = getpid(); |
224 if not ok then | 218 if not ok then |
225 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then | 219 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then |
226 -- Report as not running, since we can't open the pidfile | 220 -- Report as not running, since we can't open the pidfile |
227 -- (it probably doesn't exist) | 221 -- (it probably doesn't exist) |
228 return true, false; | 222 return true, false; |
230 return ok, pid; | 224 return ok, pid; |
231 end | 225 end |
232 return true, signal.kill(pid, 0) == 0; | 226 return true, signal.kill(pid, 0) == 0; |
233 end | 227 end |
234 | 228 |
235 function start() | 229 local function start() |
236 local ok, ret = _M.isrunning(); | 230 local ok, ret = isrunning(); |
237 if not ok then | 231 if not ok then |
238 return ok, ret; | 232 return ok, ret; |
239 end | 233 end |
240 if ret then | 234 if ret then |
241 return false, "already-running"; | 235 return false, "already-running"; |
246 os.execute(CFG_SOURCEDIR.."/../../bin/prosody"); | 240 os.execute(CFG_SOURCEDIR.."/../../bin/prosody"); |
247 end | 241 end |
248 return true; | 242 return true; |
249 end | 243 end |
250 | 244 |
251 function stop() | 245 local function stop() |
252 local ok, ret = _M.isrunning(); | 246 local ok, ret = isrunning(); |
253 if not ok then | 247 if not ok then |
254 return ok, ret; | 248 return ok, ret; |
255 end | 249 end |
256 if not ret then | 250 if not ret then |
257 return false, "not-running"; | 251 return false, "not-running"; |
258 end | 252 end |
259 | 253 |
260 local ok, pid = _M.getpid() | 254 local ok, pid = getpid() |
261 if not ok then return false, pid; end | 255 if not ok then return false, pid; end |
262 | 256 |
263 signal.kill(pid, signal.SIGTERM); | 257 signal.kill(pid, signal.SIGTERM); |
264 return true; | 258 return true; |
265 end | 259 end |
266 | 260 |
267 function reload() | 261 local function reload() |
268 local ok, ret = _M.isrunning(); | 262 local ok, ret = isrunning(); |
269 if not ok then | 263 if not ok then |
270 return ok, ret; | 264 return ok, ret; |
271 end | 265 end |
272 if not ret then | 266 if not ret then |
273 return false, "not-running"; | 267 return false, "not-running"; |
274 end | 268 end |
275 | 269 |
276 local ok, pid = _M.getpid() | 270 local ok, pid = getpid() |
277 if not ok then return false, pid; end | 271 if not ok then return false, pid; end |
278 | 272 |
279 signal.kill(pid, signal.SIGHUP); | 273 signal.kill(pid, signal.SIGHUP); |
280 return true; | 274 return true; |
281 end | 275 end |
282 | 276 |
283 return _M; | 277 return { |
278 show_message = show_message; | |
279 show_warning = show_message; | |
280 show_usage = show_usage; | |
281 getchar = getchar; | |
282 getline = getline; | |
283 getpass = getpass; | |
284 show_yesno = show_yesno; | |
285 read_password = read_password; | |
286 show_prompt = show_prompt; | |
287 adduser = adduser; | |
288 user_exists = user_exists; | |
289 passwd = passwd; | |
290 deluser = deluser; | |
291 getpid = getpid; | |
292 isrunning = isrunning; | |
293 start = start; | |
294 stop = stop; | |
295 reload = reload; | |
296 }; |