Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("top")
- -- The server computer id
- local server_id = 2
- -- Function to check if a disk is inserted into any of the computer drives
- function checkDisk()
- local result = false
- if disk.isPresent("left") and disk.isPresent("right") then
- result = true
- end
- return result
- end
- -- Main program loop
- while true do
- -- Prompt the user to insert a blank disk into the left drive and an admin disk into the right drive
- print("Please insert a blank disk into the left drive and an Administrator disk into the right drive.")
- -- Wait for a disk to be inserted into the left drive
- while not checkDisk() do
- sleep(1)
- end
- local mountPath = disk.getMountPath("right")
- if fs.exists(mountPath .. "/user_id.txt") and fs.exists(mountPath .. "/user_pass.txt") then
- print("user id file path is: " .. mountPath .. "/user_id.txt")
- local access_reason = "add"
- -- Open the "id" file on the left disk
- local file = fs.open(mountPath .. "/user_id.txt", "r")
- -- Read the contents of the file
- local admin_user_id = file.readAll()
- admin_user_id = admin_user_id:gsub("[^%w ]", "")
- print("user id is: " .. admin_user_id)
- -- Close the file
- file.close()
- local file = fs.open(mountPath .. "/user_pass.txt", "r")
- -- Read the contents of the file
- local admin_user_pass = file.readAll()
- admin_user_pass = admin_user_pass:gsub("[^%w ]", "")
- -- Close the file
- file.close()
- print("user pass is: " .. admin_user_pass)
- print("sending to server: " .. "<ar>" .. access_reason .. "</ar>" .. "<cui>" .. admin_user_id .. "</cui>" .. "<cup>" .. admin_user_pass .. "</cup>")
- rednet.send(server_id, "<ar>" .. access_reason .. "</ar>" .. "<cui>" .. admin_user_id .. "</cui>" .. "<cup>" .. admin_user_pass .. "</cup>")
- local _, response = rednet.receive()
- local server_user_id_result = string.match(response, "<suir>(%w+)</suir>")
- local server_user_pass_result = string.match(response, "<supr>(%w+)</supr>")
- local server_user_new_pass = string.match(response, "<sunp>(%w+)</sunp>")
- local server_user_access_result = string.match(response, "<suar>(%w+)</suar>")
- print("server response is: " .. response)
- if (server_user_id_result == "recognized") then
- if (server_user_pass_result == "verified") then
- -- Receive the new user pass and write it to the file
- local file = fs.open(mountPath .. "/user_pass.txt", "w")
- file.write(server_user_new_pass)
- file.close()
- if (server_user_access_result == "granted") then
- -- Prompt the user for a unique user id
- local client_user_id = read("Please enter a unique user name for this card: ")
- rednet.send(server_id, client_user_id)
- local _, response = rednet.receive()
- if (response == "user added") then
- local mountPath = disk.getMountPath("left")
- local _, client_user_pass = rednet.receive()
- local file = fs.open(mountPath .. "/user_id.txt", "w")
- file.write(client_user_id)
- file.close()
- local file = fs.open(mountPath .. "/user_pass.txt", "w")
- file.write(client_user_pass)
- file.close()
- 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: ")
- rednet.send(server_id, client_user_level)
- print("The new user card has been properly set up. Thank you and goodbye.")
- sleep(3)
- else
- -- Inform the user that the chosen user id already exists
- print("The user name entered for this card already exists")
- sleep(3)
- end
- else
- -- Inform the user that the card in the right slot is not an admin account
- print("The inserted disk does not have sufficient permissions for this door")
- sleep(3)
- end
- else
- print("The inserted disk has a mismatched user password. Please contact a site administrator.")
- sleep(3)
- end
- else
- print("The inserted disk is not a recognized user.")
- sleep(3)
- end
- else
- print("The inserted disk is not a valid id card.")
- sleep(3)
- end
- rednet.close("top")
- -- Eject both disks
- disk.eject("left")
- disk.eject("right")
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement