Advertisement
jille_Jr

CC: Security #2 username and password

Oct 9th, 2012
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Security #2
  2. -- Username and password lock
  3. -- Code written by jag_e_nummer_ett
  4.  
  5. local oldPull = os.pullEvent;
  6. os.pullEvent = os.pullEventRaw;
  7.  
  8. function printPos(stringMessage, x, y)
  9.   local unused = 0
  10.   if not stringMessage then
  11.     error("Needs a string to function!")
  12.   else
  13.     if not x then
  14.       x, unused = term.getCursorPos()
  15.     end
  16.     if not y then
  17.       unused, y = term.getCursorPos()
  18.     end
  19.     term.setCursorPos(x,y)
  20.     print(stringMessage)
  21.   end
  22. end
  23.  
  24. function readPos(x, y, readChar)
  25.   local unused = 0
  26.   if not x then
  27.     x, unused = term.getCursorPos()
  28.   end
  29.   if not y then
  30.     unused, y = term.getCursorPos()
  31.   end
  32.   term.setCursorPos(x,y)
  33.   if readChar then return read(readChar)
  34.   else return read() end
  35. end
  36.  
  37. function login()
  38.   local username = "admin"
  39.   local password = "password"
  40.   local inputUser = ""
  41.   local inputPass = ""
  42.   local tries = 5
  43.   while inputPass ~= password and inputUser ~= username do
  44.     term.clear()
  45.     if tries < 5 then
  46.       term.setCursorPos(3,6)
  47.       print("Incorrect username or password!")
  48.     end
  49.     printPos("Please enter login",3,2)
  50.     printPos("Username: ",3,3)
  51.     printPos("Password: ",3,4)
  52.     printPos("Tries left: "..tries,3,5)
  53.     inputUser = readPos(13,3)
  54.     inputPass = readPos(13,4,"*")
  55.     tries = tries - 1
  56.     if tries == 0 then
  57.       return false
  58.     end
  59.   end
  60.   return true
  61. end
  62.  
  63. if login() then
  64.   term.clear()
  65.   term.setCursorPos(3,2)
  66.   print("Login successful!")
  67.   sleep(1.5)
  68. else
  69.   term.clear()
  70.   term.setCursorPos(3,2)
  71.   print("Login failed!")
  72.   sleep(1.5)
  73.   os.shutdown()
  74. end
  75.  
  76. os.pullEvent = oldPull;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement