Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- users = { }
- inhibitors = { }
- keyComputers = { }
- rsSwitches = { }
- function update()
- file = fs.open("data/users","w")
- file.write(textutils.serialise(users))
- file.close()
- file = fs.open("data/inhibitors","w")
- file.write(textutils.serialise(inhibitors))
- file.close()
- file = fs.open("data/keyComputers","w")
- file.write(textutils.serialise(keyComputers))
- file.close()
- file = fs.open("data/rsSwitches","w")
- file.write(textutils.serialise(rsSwitches))
- file.close()
- d = {requestClearance,creationClearance,adminClearance}
- file = fs.open("data/stats","w")
- file.write(textutils.serialise(d))
- file.close()
- end
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function randomString(length)
- allLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV0123456789"
- e = ""
- for i = 1, length do
- r = math.random(#allLetters)
- e = e..string.sub(allLetters,r,r)
- end
- return e
- end
- if not fs.exists("data") then
- clear()
- print("SETTING UP SERVER")
- cID = os.getComputerID()
- print("SERVER ID IS "..cID)
- print("REMEMBER THAT")
- print()
- print("PRESS ENTER")
- read()
- clear()
- print("NEED TO SET CLEARANCES")
- print()
- term.write("HIGHEST SECURITY CLEARANCE? ")
- adminClearance = tonumber(read())
- print()
- term.write("CLEARANCE REQUIRED TO REQUEST LOG IN? ")
- requestClearance = tonumber(read())
- print()
- term.write("CLEARANCE REQUIRED TO CREATE USER? ")
- creationClearance = tonumber(read())
- clear()
- print("NEED TO CREATE ADMIN ACCOUNT")
- print()
- term.write("USERNAME: ")
- username = read()
- print()
- term.write("PASSWORD: ")
- password = read("*")
- users = { {username,randomString(6),adminClearance,password} }
- file = fs.open("data/users","w")
- file.write(textutils.serialise(users))
- file.close()
- file = fs.open("data/inhibitors","w")
- file.close()
- file = fs.open("data/keyComputers","w")
- file.close()
- file = fs.open("data/rsSwitches","w")
- file.close()
- d = { requestClearance, creationClearance, adminClearance}
- file = fs.open("data/stats","w")
- file.write(textutils.serialise(d))
- file.close()
- else
- file = fs.open("data/users","r")
- data = file.readAll()
- file.close()
- users = textutils.unserialise(data)
- file = fs.open("data/inhibitors","r")
- data = file.readAll()
- file.close()
- inhibitors = textutils.unserialise(data)
- file = fs.open("data/keyComputers","r")
- data = file.readAll()
- file.close()
- keyComputers = textutils.unserialise(data)
- file = fs.open("data/rsSwitches","r")
- data = file.readAll()
- file.close()
- rsSwitches = textutils.unserialise(data)
- file = fs.open("data/stats","r")
- data = file.readAll()
- file.close()
- d = textutils.unserialise(data)
- requestClearance = d[1]
- creationClearance = d[2]
- adminClearance = d[3]
- end
- for i,v in ipairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- modemSide = v
- break
- end
- end
- rednet.open(modemSide)
- clear()
- for i,v in ipairs(inhibitors) do
- rednet.send(v[1], "GETSTATE")
- id,msg,d = rednet.receive(.5)
- if msg ~= nil then
- if msg then
- v[2] = "ACTIVE"
- else
- v[2] = "DEACTIVE"
- end
- end
- end
- for i,v in ipairs(keyComputers) do
- rednet.send(v[1], "GETSTATE")
- id,msg,d = rednet.receive(.5)
- if msg ~= nil then
- if msg then
- v[2] = "ACTIVE"
- else
- v[2] = "DEACTIVE"
- end
- end
- end
- for i,v in ipairs(rsSwitches) do
- rednet.send(v[1], "GETSTATE")
- id,msg,d = rednet.receive(.5)
- if msg ~= nil then
- if msg then
- v[2] = "ACTIVE"
- else
- v[2] = "DEACTIVE"
- end
- end
- end
- function printStats()
- clear()
- print("INHIBITORS")
- for i,v in ipairs(inhibitors) do
- print(v[4]..": "..v[2])
- end
- print()
- print("KEY COMPUTER")
- for i,v in ipairs(keyComputers) do
- print(v[4]..": "..v[2])
- end
- print()
- print("RS SWITCHES")
- for i,v in ipairs(rsSwitches) do
- print(v[4]..": "..v[2])
- end
- end
- printStats()
- while true do
- id,msg,d = rednet.receive()
- if type(msg) == "table" then
- sender = msg[1]
- cmd = msg[2]
- if type(sender) ~= "table" and sender == "DEVICE" then
- deviceType = msg[2]
- cmd = msg[3]
- if deviceType == "KEY_LOCK" then
- for i,v in ipairs(keyComputers) do
- if v[1] == id then
- if cmd == "GET_APPROVED_IDS" then
- approvedIDs = {}
- for j,k in ipairs(users) do
- if k[3] >= v[5] then
- table.insert(approvedIDs, k[2])
- end
- end
- rednet.send(id,approvedIDs)
- break
- end
- end
- end
- end
- else
- username = sender[1]
- password = sender[4]
- clearance = sender[3]
- accountID = sender[2]
- if cmd == "LOG_IN" then
- for i,v in ipairs(users) do
- if v[1] == username and v[4] == password then
- if v[3] >= requestClearance then
- rednet.send(id,v)
- end
- end
- end
- else
- for i,v in ipairs(users) do
- if v[1] == username then
- if v[4] == password and v[3] == clearance and v[2] == accountID then
- if cmd == "CHANGE_INHIBITOR" then
- inhibitorID = msg[3]
- for i,v in ipairs(inhibitors) do
- if inhibitorID == v[1] then
- if sender[3] >= v[3] then
- rednet.send(inhibitorID, "CHANGE")
- id,msg,d = rednet.receive(.3)
- if msg ~= nil then
- v[2] = msg
- else
- v[2] = "OFFLINE"
- end
- printStats()
- break
- end
- end
- end
- elseif cmd == "CHANGE_KEY_LOCK" then
- keyID = msg[3]
- for i,v in ipairs(keyComputers) do
- if keyID == v[1] then
- if sender[3] >= v[3] then
- rednet.send(keyID, "CHANGE")
- id,msg,d = rednet.receive(.3)
- if msg ~= nil then
- v[2] = msg
- else
- v[2] = "OFFLINE"
- end
- printStats()
- break
- end
- end
- end
- elseif cmd == "CHANGE_SWITCH" then
- switchID = msg[3]
- for i,v in ipairs(rsSwitches) do
- if switchID == v[1] then
- if sender[3] >= v[3] then
- rednet.send(switchID, "CHANGE")
- id,msg,d = rednet.receive(.3)
- if msg ~= nil then
- v[2] = msg
- else
- v[2] = "OFFLINE"
- end
- printStats()
- end
- end
- end
- elseif cmd == "GET_INHIBITORS " then
- if sender[3] >=requestClearance then
- rednet.send(id,inhibitors)
- end
- elseif cmd == "GET_SWITCHES" then
- if sender[3] >= requestClearance then
- rednet.send(id,rsSwitches)
- end
- elseif cmd == "GET_KEY_LOCKS " then
- if sender[3] >= requestClearance then
- rednet.send(id,keyComputers)
- end
- elseif cmd == "RESET_CARD" then
- v[2] = msg[3]
- update()
- elseif cmd == "RESET_PASSWORD" then
- v[4] = msg[3]
- update()
- elseif cmd == "ADD_ACCOUNT" then
- if clearance >= creationClearance then
- newPass = randomString(4)
- table.insert(users,{msg[3],randomString(6),1,newPass})
- rednet.send(id,newPass)
- update()
- end
- elseif cmd == "CHANGE_KEY_LOCK_CLEARANCE" then
- if clearance >= adminClearance then
- for i,v in ipairs(keyComputers) do
- if v[1] == msg[3][1] then
- v[5] = msg[4]
- update()
- end
- end
- end
- elseif cmd == "GET_USERS" then
- if clearance >= adminClearance then
- sendArray = {}
- for i,v in ipairs(users) do
- table.insert(sendArray,{v[1],v[2],v[3]})
- end
- rednet.send(id,sendArray)
- end
- elseif cmd == "CHANGE_USER_CLEARANCE" then
- if clearance >= adminClearance then
- for i,v in ipairs(users) do
- if v[1] == msg[3][1] and not (msg[4] >= adminClearance + 1) and not (v[1] == username) and not(v[3] >= adminClearance + 1 )then
- v[3] = msg[4]
- update()
- end
- end
- end
- elseif cmd == "REMOVE_USER" then
- if clearance >= adminClearance then
- for i,v in ipairs(users) do
- if v[1] == msg[3][1] and not (v[1] == username) then
- table.remove(users,i)
- update()
- end
- end
- end
- elseif cmd == "REGISTER_INHIBITOR" then
- if clearance >= adminClearance then
- table.insert(inhibitors,{msg[3],msg[4],msg[5],msg[6]})
- rednet.send(id,"CREATED")
- update()
- end
- elseif cmd == "REGISTER_KEY_LOCK" then
- if clearance >= adminClearance then
- table.insert(keyComputers,{msg[3],msg[4],msg[5],msg[6],msg[7]})
- rednet.send(id,"CREATED")
- update()
- end
- elseif cmd == "REGISTER_RS_SWITCH" then
- if clearance >= adminClearance then
- table.insert(rsSwitches,{msg[3],msg[4],msg[5],msg[6]})
- rednet.send(id,"CREATED")
- update()
- end
- end
- break
- end
- end
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement