Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The server computer id
- local server_id = 2
- os.pullEvent = os.pullEventRaw
- -- Function to check if a disk is inserted into any of the computer drives
- function checkDisk(diskSide)
- return disk.isPresent(diskSide)
- end
- -- Function to print to monitors connected via modem to a door client computer
- function printToMonitor(message)
- nRate = 5
- local nSleep = 1 / nRate
- local p = peripheral.getNames()
- for var=0,5,1 do
- local monitors = string.match(tostring(p[var]), "monitor_(%w+)")
- if tostring(monitors) ~= "nil" then
- monitor = peripheral.wrap("monitor_" .. tostring(monitors))
- monitor.clear()
- local width, height = monitor.getSize()
- local cursorX, cursorY = monitor.getCursorPos()
- monitor.setCursorPos(1,cursorY)
- sText = ""
- for n = 1, #message do
- sText = sText .. tostring(message[n])
- sText = sText .. " | "
- end
- sString = "| "
- if width / string.len(sText) < 1 then nStringRepeat = 3
- else
- nStringRepeat = math.ceil(width / string.len(sText) * 3)
- end
- for n = 1, nStringRepeat do
- sString = sString .. sText
- end
- while true do
- for n = 1, string.len(sText) do
- sDisplay = string.sub(sString, n, n + width -1)
- monitor.clearLine()
- monitor.setCursorPos(1, cursorY)
- monitor.write(sDisplay)
- sleep(nSleep)
- end
- end
- else
- --Do nothing
- end
- end
- end
- -- Main program loop
- while true do
- -- Prompt the user to insert a disk into a drive
- print("Insert an id disk into the drive.")
- -- Wait for a disk to be inserted into a drive
- local event, diskSide = os.pullEvent("disk")
- rednet.open("top")
- while not checkDisk(diskSide) do
- sleep(1)
- end
- local mountPath = disk.getMountPath(diskSide)
- 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 = "door"
- -- 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
- print("Access Granted")
- -- Eject both disks
- disk.eject("left")
- disk.eject("right")
- redstone.setOutput("back", true)
- sleep(4)
- redstone.setOutput("back", false)
- 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")
- end
Advertisement
Add Comment
Please, Sign In to add comment