Software / code / prosody
Comparison
util/prosodyctl.lua @ 4178:760f644c0ca3
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 | 3895:84f8153d06ce |
| child | 4335:3a2a01432b5c |
comparison
equal
deleted
inserted
replaced
| 4177:623e6d5f30b7 | 4178:760f644c0ca3 |
|---|---|
| 13 local storagemanager = require "core.storagemanager"; | 13 local storagemanager = require "core.storagemanager"; |
| 14 local usermanager = require "core.usermanager"; | 14 local usermanager = require "core.usermanager"; |
| 15 local signal = require "util.signal"; | 15 local signal = require "util.signal"; |
| 16 local set = require "util.set"; | 16 local set = require "util.set"; |
| 17 local lfs = require "lfs"; | 17 local lfs = require "lfs"; |
| 18 local pcall = pcall; | |
| 18 | 19 |
| 19 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep; | 20 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep; |
| 20 | 21 |
| 21 local io, os = io, os; | 22 local io, os = io, os; |
| 23 local print = print; | |
| 22 local tostring, tonumber = tostring, tonumber; | 24 local tostring, tonumber = tostring, tonumber; |
| 23 | 25 |
| 24 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR; | 26 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR; |
| 25 | 27 |
| 28 local _G = _G; | |
| 26 local prosody = prosody; | 29 local prosody = prosody; |
| 27 | 30 |
| 28 module "prosodyctl" | 31 module "prosodyctl" |
| 29 | 32 |
| 33 -- UI helpers | |
| 34 function show_message(msg, ...) | |
| 35 print(msg:format(...)); | |
| 36 end | |
| 37 | |
| 38 function show_warning(msg, ...) | |
| 39 print(msg:format(...)); | |
| 40 end | |
| 41 | |
| 42 function show_usage(usage, desc) | |
| 43 print("Usage: ".._G.arg[0].." "..usage); | |
| 44 if desc then | |
| 45 print(" "..desc); | |
| 46 end | |
| 47 end | |
| 48 | |
| 49 function getchar(n) | |
| 50 local stty_ret = os.execute("stty raw -echo 2>/dev/null"); | |
| 51 local ok, char; | |
| 52 if stty_ret == 0 then | |
| 53 ok, char = pcall(io.read, n or 1); | |
| 54 os.execute("stty sane"); | |
| 55 else | |
| 56 ok, char = pcall(io.read, "*l"); | |
| 57 if ok then | |
| 58 char = char:sub(1, n or 1); | |
| 59 end | |
| 60 end | |
| 61 if ok then | |
| 62 return char; | |
| 63 end | |
| 64 end | |
| 65 | |
| 66 function getpass() | |
| 67 local stty_ret = os.execute("stty -echo 2>/dev/null"); | |
| 68 if stty_ret ~= 0 then | |
| 69 io.write("\027[08m"); -- ANSI 'hidden' text attribute | |
| 70 end | |
| 71 local ok, pass = pcall(io.read, "*l"); | |
| 72 if stty_ret == 0 then | |
| 73 os.execute("stty sane"); | |
| 74 else | |
| 75 io.write("\027[00m"); | |
| 76 end | |
| 77 io.write("\n"); | |
| 78 if ok then | |
| 79 return pass; | |
| 80 end | |
| 81 end | |
| 82 | |
| 83 function show_yesno(prompt) | |
| 84 io.write(prompt, " "); | |
| 85 local choice = getchar():lower(); | |
| 86 io.write("\n"); | |
| 87 if not choice:match("%a") then | |
| 88 choice = prompt:match("%[.-(%U).-%]$"); | |
| 89 if not choice then return nil; end | |
| 90 end | |
| 91 return (choice == "y"); | |
| 92 end | |
| 93 | |
| 94 function read_password() | |
| 95 local password; | |
| 96 while true do | |
| 97 io.write("Enter new password: "); | |
| 98 password = getpass(); | |
| 99 if not password then | |
| 100 show_message("No password - cancelled"); | |
| 101 return; | |
| 102 end | |
| 103 io.write("Retype new password: "); | |
| 104 if getpass() ~= password then | |
| 105 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then | |
| 106 return; | |
| 107 end | |
| 108 else | |
| 109 break; | |
| 110 end | |
| 111 end | |
| 112 return password; | |
| 113 end | |
| 114 | |
| 115 -- Server control | |
| 30 function adduser(params) | 116 function adduser(params) |
| 31 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password; | 117 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password; |
| 32 if not user then | 118 if not user then |
| 33 return false, "invalid-username"; | 119 return false, "invalid-username"; |
| 34 elseif not host then | 120 elseif not host then |