Advertisement
onwardprogress

adminTest

Jan 2nd, 2023 (edited)
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.06 KB | None | 0 0
  1. rednet.open("top")
  2.  
  3. -- The server computer id
  4. local server_id = 2
  5.  
  6. -- Function to check if a disk is inserted into any of the computer drives
  7. function checkDisk()
  8.     local result = false
  9.     if disk.isPresent("left") and disk.isPresent("right") then
  10.         result = true
  11.     end
  12.     return result
  13. end
  14.  
  15. -- Main program loop
  16. while true do
  17.     -- Prompt the user to insert a blank disk into the left drive and an admin disk into the right drive
  18.     print("Please insert a blank disk into the left drive and an Administrator disk into the right drive.")
  19.     -- Wait for a disk to be inserted into the left drive
  20.     while not checkDisk() do
  21.         sleep(1)
  22.     end
  23.     local mountPath = disk.getMountPath("right")
  24.     if fs.exists(mountPath .. "/user_id.txt") and fs.exists(mountPath .. "/user_pass.txt") then
  25.         print("user id file path is: " .. mountPath .. "/user_id.txt")
  26.         local access_reason = "add"
  27.         -- Open the "id" file on the left disk
  28.         local file = fs.open(mountPath .. "/user_id.txt", "r")
  29.         -- Read the contents of the file
  30.         local admin_user_id = file.readAll()
  31.         admin_user_id = admin_user_id:gsub("[^%w ]", "")
  32.         print("user id is: " .. admin_user_id)
  33.         -- Close the file
  34.         file.close()
  35.         local file = fs.open(mountPath .. "/user_pass.txt", "r")
  36.         -- Read the contents of the file
  37.         local admin_user_pass = file.readAll()
  38.         admin_user_pass = admin_user_pass:gsub("[^%w ]", "")
  39.         -- Close the file
  40.         file.close()
  41.         print("user pass is: " .. admin_user_pass)
  42.         print("sending to server: " .. "<ar>" .. access_reason .. "</ar>" .. "<cui>" .. admin_user_id .. "</cui>" .. "<cup>" .. admin_user_pass .. "</cup>")
  43.         rednet.send(server_id, "<ar>" .. access_reason .. "</ar>" .. "<cui>" .. admin_user_id .. "</cui>" .. "<cup>" .. admin_user_pass .. "</cup>")
  44.         local _, response = rednet.receive()
  45.         local server_user_id_result = string.match(response, "<suir>(%w+)</suir>")
  46.         local server_user_pass_result = string.match(response, "<supr>(%w+)</supr>")
  47.         local server_user_new_pass = string.match(response, "<sunp>(%w+)</sunp>")
  48.         local server_user_access_result = string.match(response, "<suar>(%w+)</suar>")
  49.         print("server response is: " .. response)
  50.         if (server_user_id_result == "recognized") then
  51.             if (server_user_pass_result == "verified") then
  52.                 -- Receive the new user pass and write it to the file
  53.                 local file = fs.open(mountPath .. "/user_pass.txt", "w")
  54.                 file.write(server_user_new_pass)
  55.                 file.close()
  56.                 if (server_user_access_result == "granted") then
  57.                     -- Prompt the user for a unique user id
  58.                     local client_user_id = read("Please enter a unique user name for this card: ")
  59.                     rednet.send(server_id, client_user_id)
  60.                     local _, response = rednet.receive()
  61.                     if (response == "user added") then
  62.                         local mountPath = disk.getMountPath("left")
  63.                         local _, client_user_pass = rednet.receive()
  64.                         local file = fs.open(mountPath .. "/user_id.txt", "w")
  65.                         file.write(client_user_id)
  66.                         file.close()
  67.                         local file = fs.open(mountPath .. "/user_pass.txt", "w")
  68.                         file.write(client_user_pass)
  69.                         file.close()
  70.                         local client_user_level = read("Please enter the access level to assign to this card. 1 for guest, 2 for employee or 3 for Administrator: ")
  71.                         rednet.send(server_id, client_user_level)
  72.                         print("The new user card has been properly set up. Thank you and goodbye.")
  73.                         sleep(3)
  74.                     else
  75.                         -- Inform the user that the chosen user id already exists
  76.                         print("The user name entered for this card already exists")
  77.                         sleep(3)
  78.                     end
  79.                 else
  80.                     -- Inform the user that the card in the right slot is not an admin account
  81.                     print("The inserted disk does not have sufficient permissions for this door")
  82.                     sleep(3)
  83.                 end
  84.             else
  85.                 print("The inserted disk has a mismatched user password. Please contact a site administrator.")
  86.                 sleep(3)
  87.             end
  88.         else
  89.             print("The inserted disk is not a recognized user.")
  90.             sleep(3)
  91.         end
  92.     else
  93.         print("The inserted disk is not a valid id card.")
  94.         sleep(3)
  95.     end
  96.     rednet.close("top")
  97.     -- Eject both disks
  98.     disk.eject("left")
  99.     disk.eject("right")
  100.     sleep(1)
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement