Guest User

Untitled

a guest
Dec 12th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1. local sides = {"top", "bottom", "left", "right", "back", "front"}
  2. local serverID = os.computerID()
  3. local users = {}
  4. local passwords = {}
  5.  
  6. function clear()
  7.    term.clear()
  8.    term.setCursorPos(1,1)
  9. end
  10.  
  11. function updateAdminText()
  12.       print("Welcome, admin.")
  13.       print("What would you like to do?")
  14.       print("[1] Make a new account.")
  15.       print("[2] Quit the server.")
  16.       print("[3] Return to normal server mode.")
  17.       print("[4] Change the admin password.")
  18.       print("[5] Print out the current users database.")
  19.       write("> ")
  20. end
  21.  
  22. function saveUsersVar()
  23.    local usersFile = fs.open("users", "w")
  24.    usersFile.write(textutils.serialize(users))
  25.    usersFile.close()
  26. end
  27.  
  28. function getUsersVar()
  29.    local usersFile = fs.open("users", "r")
  30.    users = textutils.unserialize(usersFile.readAll())
  31.    usersFile.close()
  32. end
  33.  
  34. function savePasswordsVar()
  35.    local passwordsFile = fs.open("passwords", "w")
  36.    passwordsFile.write(textutils.serialize(passwords))
  37.    passwordsFile.close()
  38. end
  39.  
  40. function getPasswordsVar()
  41.    local passwordsFile = fs.open("passwords", "r")
  42.    passwords = textutils.unserialize(passwordsFile.readAll())
  43.    passwordsFile.close()
  44. end
  45.  
  46. function bootUp()
  47.    local rnetSuccess = false
  48.    for i = 1, #sides do
  49.       rednet.open(sides[i])
  50.       if peripheral.getType(sides[i]) == "modem" then
  51.          rnetSuccess = true
  52.       end
  53.    end
  54.    if not rnetSuccess then
  55.       print("Error: No wireless modem attached.")
  56.       error()
  57.    end
  58.    if not fs.exists("adminPassword") then
  59.       local adminPass = ""
  60.       repeat
  61.          clear()
  62.          textutils.slowPrint("No admin password detected.")
  63.          textutils.slowWrite("Enter a new password: ")
  64.          adminPass = read()
  65.       until adminPass ~= ""
  66.       adminPassFile = fs.open("adminPassword", "w")
  67.       adminPassFile.write(adminPass)
  68.       adminPassFile.close()
  69.    end
  70.    if not fs.exists("users") then
  71.       saveUsersVar()
  72.    else
  73.       getUsersVar()
  74.    end
  75.    if not fs.exists("passwords") then
  76.       savePasswordsVar()
  77.    else
  78.       getPasswordsVar()
  79.    end
  80. end
  81.  
  82. function newAccount(newUsername, newPassword)
  83.    local usernameKey = usernameToKey(newUsername)
  84.    if usernameKey == nil then
  85.       table.insert(users, 1, newUsername)
  86.       table.insert(passwords, 1, newPassword)
  87.       print("New account created:")
  88.       print("New username: "..username)
  89.       print("New password: "..password)
  90.       sleep(2)
  91.    else
  92.       print("Username "..newUsername.." already in use:")
  93.       print("Setting "..newUsername.."\'s password as "..newPassword..".")
  94.       passwords[usernameKey] = newPassword
  95.       sleep(3)
  96.    end
  97.    saveUsersVar()
  98.    savePasswordsVar()
  99. end
  100.  
  101. function usernameToKey(username)
  102.    for i = 1, #users do
  103.       if users[i] == username then
  104.          return i
  105.       end
  106.    end
  107.    return nil
  108. end
  109.  
  110. function adminMode()
  111.    local admin = true
  112.    while admin == true do
  113.       clear()
  114.       updateAdminText()
  115.       option = read()
  116.       if option == "1" then
  117.          repeat
  118.             write("Enter the new account's username: ")
  119.             username = read()
  120.             write("Enter the new account's password: ")
  121.             password = read()
  122.          until username ~= "" and password ~= ""
  123.          newAccount(username, password)
  124.       elseif option == "2" then
  125.          clear()
  126.          write("Entering normal command-line mode")
  127.          os.sleep(0.2)
  128.          textutils.slowPrint("...", 3)
  129.          os.sleep(0.2)
  130.          error()
  131.       elseif option == "3" then
  132.          clear()
  133.          write("Returning to normal server mode")
  134.          os.sleep(0.2)
  135.          textutils.slowPrint("...", 3)
  136.          os.sleep(0.2)
  137.          return
  138.       elseif option == "4" then
  139.          local newPassword = ""
  140.          repeat
  141.             write("Enter the new password: ")
  142.             newPassword = read()
  143.          until newPassword ~= ""
  144.          adminPassFile = fs.open("adminPassword", "w")
  145.          adminPassFile.write(newPassword)
  146.          adminPassFile.close()
  147.          print("Successfully changed the password to "..newPassword..".")
  148.          sleep(1.5)
  149.       elseif option == "5" then
  150.          clear()
  151.          print("Username(Password)")
  152.          for key, user in ipairs(users) do
  153.             print(users[key].."("..passwords[key]..")")
  154.             sleep(0.5)
  155.          end
  156.          print("Press any key to continue...")
  157.          os.pullEvent("key")
  158.          sleep(0.2)
  159.       else
  160.          print("Not a valid option.")
  161.          os.sleep(1.5)
  162.       end
  163.    end
  164. end
  165.  
  166. function updateText()
  167.    print("This is the login server with ID "..serverID..".")
  168.    print("Only the administrator can login here.")
  169.    print("Enter the password to be granted access.")
  170.    write("> ")
  171. end
  172.  
  173. function normalMode()
  174.    while true do
  175.       clear()
  176.       updateText()
  177.       local password = read("*")
  178.       local adminPassFile = fs.open("adminPassword", "r")
  179.       adminPass = adminPassFile.readLine()
  180.       adminPassFile.close()
  181.       if password == adminPass then
  182.          adminMode()
  183.       else
  184.          print("Password invalid!")
  185.          sleep(2)
  186.       end
  187.    end
  188. end
  189.  
  190. function serverSub()
  191.    while true do
  192.      
  193.    end
  194. end
  195.  
  196. bootUp()
  197. parallel.waitForAll(normalMode(), serverSub())
Add Comment
Please, Sign In to add comment