jille_Jr icon

CC: Security #2 username and password

jille_Jr | PRO | 10/09/12 06:02:59 PM UTC | 0 ⭐ | 659 👁️ | Never ⏰ | []
Lua |

1.62 KB

|

None

|

0 👍

/

0 👎

-- Security #2
-- Username and password lock
-- Code written by jag_e_nummer_ett
 
local oldPull = os.pullEvent;
os.pullEvent = os.pullEventRaw;
 
function printPos(stringMessage, x, y)
  local unused = 0
  if not stringMessage then
    error("Needs a string to function!")
  else
    if not x then
      x, unused = term.getCursorPos()
    end
    if not y then
      unused, y = term.getCursorPos()
    end
    term.setCursorPos(x,y)
    print(stringMessage)
  end
end
 
function readPos(x, y, readChar)
  local unused = 0
  if not x then
    x, unused = term.getCursorPos()
  end
  if not y then
    unused, y = term.getCursorPos()
  end
  term.setCursorPos(x,y)
  if readChar then return read(readChar)
  else return read() end
end
 
function login()
  local username = "admin"
  local password = "password"
  local inputUser = ""
  local inputPass = ""
  local tries = 5
  while inputPass ~= password and inputUser ~= username do
    term.clear()
    if tries < 5 then
      term.setCursorPos(3,6)
      print("Incorrect username or password!")
    end
    printPos("Please enter login",3,2)
    printPos("Username: ",3,3)
    printPos("Password: ",3,4)
    printPos("Tries left: "..tries,3,5)
    inputUser = readPos(13,3)
    inputPass = readPos(13,4,"*")
    tries = tries - 1
    if tries == 0 then
      return false
    end
  end
  return true
end
 
if login() then
  term.clear()
  term.setCursorPos(3,2)
  print("Login successful!")
  sleep(1.5)
else
  term.clear()
  term.setCursorPos(3,2)
  print("Login failed!")
  sleep(1.5)
  os.shutdown()
end
 
os.pullEvent = oldPull;

Comments