Advertisement
JJl77

Computer Craft Login

Dec 29th, 2016
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.35 KB | None | 0 0
  1. --This is a VERY simple login script, slightly more complex than hardcoded login scripts although
  2. --It will read a list of users from a file where a serialized list of users is stored. Before copying this
  3. --to your startup script, please do create a users file and verify it functions as you wish.
  4.  
  5. local users = {}
  6.  
  7. term.clear()
  8. term.setTextColor(colors.red)
  9. print("MTech Launch System 1.0")
  10. term.setTextColor(colors.white)
  11. print("")
  12.  
  13. local usfile = "users"
  14.  
  15. if not fs.exists(usfile)then
  16.     term.setTextColor(colors.red)
  17.     print("Core file 'users.txt' not found!")
  18.     term.setTextColor(colors.white)
  19.     print("")
  20.     sleep(1)
  21.     os.reboot()
  22. else
  23.     local file = io.open("users", "r")
  24.     local data = textutils.unserialize(file:read("*a"))
  25.     file:close()
  26.     if data == nil then
  27.         term.setTextColor(colors.red)
  28.         print("User entry data file is empty!")
  29.         term.setTextColor(colors.white)
  30.         print("")
  31.         sleep(1)
  32.         os.reboot()
  33.     else
  34.         users = data.users
  35.     end
  36. end
  37.  
  38. local function CheckUSR(name)
  39.     if name == nil then return false end
  40.     for k, v in pairs(users)do
  41.         if(name == k)then return true else return false end
  42.     end
  43. end
  44.  
  45. local function CheckPassword(name, pass)
  46.     if name == nil then return false end
  47.     if pass == nil then return false end
  48.     for k, v in pairs(users)do
  49.         if(k == name)then
  50.             if(pass == v)then
  51.                 return true
  52.             else
  53.                 return false
  54.             end
  55.         end
  56.     end
  57.     return false
  58. end
  59.  
  60. local function SetUp()
  61.     term.clear()
  62.     term.setCursorPos(1, 1)
  63.     term.setTextColor(colors.red)
  64.     textutils.slowPrint("MTech Mainframe Controller")
  65.     term.setTextColor(colors.white)
  66.     print("")
  67.     if(fs.exists("init"))then
  68.         dofile("init")
  69.     end
  70. end
  71.  
  72. term.clear()
  73. term.setCursorPos(1, 1)
  74. term.setTextColor(colors.red)
  75. print("Login Area")
  76. term.setTextColor(colors.white)
  77. print("")
  78. print("Username:")
  79. local username = read()
  80. if(CheckUSR(username) == false)then
  81.     term.setTextColor(colors.red)
  82.     print("Unknown Username!")
  83.     term.setTextColor(colors.white)
  84.     sleep(1)
  85.     os.reboot()
  86. else
  87.     print("Password:")
  88.     local password = read()
  89.     if(CheckPassword(username, password) == false)then
  90.         term.setTextColor(colors.red)
  91.         print("Incorrect Password!")
  92.         sleep(1)
  93.         os.reboot()
  94.     elseif(CheckPassword(username, password) == true)then
  95.         term.setTextColor(colors.green)
  96.         print("Welcome "..username.."!")
  97.         term.setTextColor(colors.white)
  98.         print("")
  99.         SetUp()
  100.     end
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement