Comparison

prosodyctl @ 4142:caa78589598f

prosodyctl, util.prosodyctl: Move UI functions to util.prosodyctl so they can be used outside of prosodyctl itself
author Matthew Wild <mwild1@gmail.com>
date Sun, 13 Feb 2011 19:28:29 +0000
parent 4095:6ad7ed619d37
child 4158:14581c3f33bd
comparison
equal deleted inserted replaced
4141:ef94984c44a0 4142:caa78589598f
224 224
225 require "util.prosodyctl" 225 require "util.prosodyctl"
226 require "socket" 226 require "socket"
227 ----------------------- 227 -----------------------
228 228
229 function show_message(msg, ...) 229 local show_message, show_warning = prosodyctl.show_message, prosodyctl.show_warning;
230 print(msg:format(...)); 230 local show_usage = prosodyctl.show_usage;
231 end 231 local getchar, getpass = prosodyctl.getchar, prosodyctl.getpass;
232 232 local show_yesno = prosodyctl.show_yesno;
233 function show_warning(msg, ...) 233 local read_password = prosodyctl.read_password;
234 print(msg:format(...));
235 end
236
237 function show_usage(usage, desc)
238 print("Usage: "..arg[0].." "..usage);
239 if desc then
240 print(" "..desc);
241 end
242 end
243
244 local function getchar(n)
245 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
246 local ok, char;
247 if stty_ret == 0 then
248 ok, char = pcall(io.read, n or 1);
249 os.execute("stty sane");
250 else
251 ok, char = pcall(io.read, "*l");
252 if ok then
253 char = char:sub(1, n or 1);
254 end
255 end
256 if ok then
257 return char;
258 end
259 end
260
261 local function getpass()
262 local stty_ret = os.execute("stty -echo 2>/dev/null");
263 if stty_ret ~= 0 then
264 io.write("\027[08m"); -- ANSI 'hidden' text attribute
265 end
266 local ok, pass = pcall(io.read, "*l");
267 if stty_ret == 0 then
268 os.execute("stty sane");
269 else
270 io.write("\027[00m");
271 end
272 io.write("\n");
273 if ok then
274 return pass;
275 end
276 end
277
278 function show_yesno(prompt)
279 io.write(prompt, " ");
280 local choice = getchar():lower();
281 io.write("\n");
282 if not choice:match("%a") then
283 choice = prompt:match("%[.-(%U).-%]$");
284 if not choice then return nil; end
285 end
286 return (choice == "y");
287 end
288
289 local function read_password()
290 local password;
291 while true do
292 io.write("Enter new password: ");
293 password = getpass();
294 if not password then
295 show_message("No password - cancelled");
296 return;
297 end
298 io.write("Retype new password: ");
299 if getpass() ~= password then
300 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
301 return;
302 end
303 else
304 break;
305 end
306 end
307 return password;
308 end
309 234
310 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2; 235 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
311 ----------------------- 236 -----------------------
312 local commands = {}; 237 local commands = {};
313 local command = arg[1]; 238 local command = arg[1];