Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. --[[
  2. 420-login script
  3. by killerbyte12
  4. for use on OpenComputers mod in minecraft
  5. --]]
  6.  
  7. -- GLOBAL vars
  8. ATTEMPTS = 3
  9. LOGGED_IN = false
  10.  
  11. -- Ask for username
  12. function userInput()
  13. -- ask for username, covert input to all uppercase.
  14. print("Username: ")
  15. local input = string.upper(io.read())
  16.  
  17. -- compare input to user files. Does this user exist?
  18. local user_exists = userExists(input)
  19. if user_exists == nil then
  20. print("Username invalid.")
  21. -- if user exists, lets ask for their password
  22. else
  23. print("Username found.")
  24. passInput(input)
  25. end
  26. end
  27.  
  28. -- checks if a user file exists, returns nil if it doesnt.
  29. function userExists(user)
  30. local file = io.open(".dat/.usr/."..user..".txt", "r")
  31. if file ~= nil then
  32. file:close()
  33. end
  34. return file
  35. end
  36.  
  37. -- Compare input to password on user file
  38. function getPass(user, pass)
  39. local file_contents = {}
  40. local pass_valid = false
  41. local file = io.open(".dat/.usr/."..user..".txt", "r")
  42. for line in file:lines() do
  43. file_contents[#file_contents+1] = line
  44. end
  45. file:close()
  46. -- password is on 3rd line
  47. if file_contents[3] == pass then
  48. pass_valid = true
  49. end
  50. return pass_valid
  51. end
  52.  
  53. -- Ask for password
  54. function passInput(user)
  55. while ATTEMPTS > 0 do
  56. print("Password: ")
  57. local input = io.read()
  58. -- TODO md5 hash passwords
  59.  
  60. local pass_valid = getPass(user, input)
  61. if pass_valid == true then
  62. print("Password accepted, logging in.")
  63. LOGGED_IN = true
  64. break
  65. else
  66. ATTEMPTS = ATTEMPTS - 1
  67. print("Password invalid. You have "..ATTEMPTS.." attempts left.")
  68. end
  69. end
  70. if ATTEMPTS <= 0 then
  71. print("You tried to many times, shutting down the system!")
  72. -- TODO, shutdown pc
  73. end
  74. end
  75.  
  76. while LOGGED_IN == false do
  77. userInput()
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement