Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --pastebin get 1NSzVUq4 startup
- local permsPath="perms"
- local testing=false
- --function to set to enable or disable ctrl-t
- --http://www.computercraft.info/forums2/index.php?/topic/2732-disable-ctrlt/
- local pullEventCopy = os.pullEvent
- function setCtrlT(value)
- if value then
- os.pullEvent = pullEventCopy
- else
- os.pullEvent = os.pullEventRaw
- end
- end
- function clear()
- term.clear()
- term.setCursorPos(1, 1)
- end
- function loadValue(path)
- local succes=true
- if not fs.exists(path) then
- return nil
- end
- local h=fs.open(path, "r")
- if h==nil then
- return nil
- end
- local value=textutils.unserialize(h.readAll())
- h.close()
- return value
- end
- function saveValue(path,value)
- local h = fs.open(path, "w")
- h.write(textutils.serialize(value))
- h.close()
- return true
- end
- function loadIntoTable(path)
- if not fs.exists(path) then
- return nil
- end
- local h=fs.open(path, "r")
- if h==nil then
- return nil
- end
- local res={}
- while(true) do
- local value=h.readLine()
- if value==nil then break end
- res[value]=true
- end
- h.close()
- return res
- end
- function appendString(path,str)
- --open code from wiki
- --Opens path in either append or write mode, depending on whether it already exists or not.
- local h = fs.open(path, fs.exists(path) and "a" or "w")
- if h==nil then
- return false
- end
- h.writeLine(str)
- h.close()
- return true
- end
- function appendUser(userName,permission)
- if(userName==nil) then
- print("UserName is nil...")
- return
- end
- local append=""
- if type(permission)=="string" and #permission>0 then
- append=" "..permission
- end
- appendString(permsPath,userName..append)
- end
- function extractAdminsAndOpenersFrom(aTable)
- local users={}
- local admins={}
- local openers={}
- if aTable~=nil then
- for line,_ in pairs(aTable) do
- local userName=nil
- --http://stackoverflow.com/questions/2779700/lua-split-into-words
- for w in line:gmatch("%S+") do
- if userName==nil then
- userName=w
- users[userName]=true
- else
- if w=="admin" then
- admins[userName]=true
- end
- if w=="open" then
- openers[userName]=true
- end
- end
- end
- end
- end
- return admins,openers,users
- end
- function getSides()
- local adminPath="adminSide"
- local openPath="openSide"
- local doorPath="doorSide"
- local adminSide= loadValue(adminPath)
- local openSide= loadValue(openPath)
- local doorSide= loadValue(doorPath)
- if adminSide==nil or openSide==nil or doorSide==nil then
- clear()
- print("you can now use ctrl-t")
- setCtrlT(true)--enable ctrl-t
- --configure sides
- print("Configuring sides...")
- -- get a side
- local sides=redstone.getSides()
- while(true) do
- for k,v in ipairs(sides) do
- print(k,":",v)
- end
- print("New door side (number):")
- doorSide=read()
- if not sides[tonumber(doorSide)] then
- print("Invalid side")
- else
- doorSide=sides[tonumber(doorSide)]
- break
- end
- end
- print("Right click admin player detector")
- local event, side, player=os.pullEvent("player")
- adminSide=side
- print("Right click open player detector")
- local event, side, player=os.pullEvent("player")
- openSide=side
- print("saving side configuretions")
- setCtrlT(false)--enable ctrl-t
- saveValue(adminPath,adminSide)
- saveValue(openPath,openSide)
- saveValue(doorPath,doorSide)
- sleep(1)
- end
- return adminSide,openSide,doorSide
- end
- function about()
- print("theinsekt's player detector lock 1.0")
- print("pastebin get 1NSzVUq4 startup")
- print()
- end
- function main()
- --load permissions from file
- local admins,openers,users=extractAdminsAndOpenersFrom(loadIntoTable(permsPath))
- --count number of admins
- local nrAdmins=0
- for _,_ in pairs(admins) do
- nrAdmins=nrAdmins+1
- end
- --exit if no admins
- if nrAdmins==0 then
- print("No admins, right click any player detector to gain root acces,")
- print("then add admin, after your user name in the file called perms")
- local event, side, player=os.pullEvent("player")
- if users[player]==nil then
- appendUser(player)
- users[player]=true
- end
- setCtrlT(true)--enable ctrl-t
- error("You now have root")
- end
- --get or configure the sides
- local adminSide, openSide, doorSide=getSides()
- local openTime=3
- while(true) do
- clear()
- if testing then
- print("testing, ctrl-t enabled")
- setCtrlT(true)--enable ctrl-t
- end
- about()
- print("AdminSide: ",adminSide)
- print("DoorSide: ",doorSide)
- print("OpenSide: ",openSide)
- print()
- print("restricted mode:")
- --for k,v in pairs(users) do
- -- print(k," ",v)
- --end
- print("Waiting for player detector...")
- local event, side, player=os.pullEvent("player")
- if event=="player" then
- if side==adminSide then
- if admins[player]~=nil then
- --enter admin mode
- setCtrlT(true)--enable ctrl-t
- error("You now have root")
- else
- print("Access denied! (not an admin)")
- sleep(2)
- end
- elseif side==openSide and (openers[player]~=nil or admins[player]~=nil) then
- --open door
- print("Opening!")
- redstone.setOutput(doorSide, true)
- sleep(openTime)
- redstone.setOutput(doorSide, false)
- else
- print("Access denied!")
- if users[player]==nil then
- print("Appended user to perms file, with no permissions.")
- appendUser(player)
- users[player]=true
- end
- sleep(2)
- end
- else
- print("Denied!")
- sleep(1)
- end
- sleep(0.15)
- end
- end
- setCtrlT(false)--disable ctrl-t
- if testing then
- print("testing, ctrl-t enabled")
- setCtrlT(true)--enable ctrl-t
- end
- main()
- setCtrlT(true)--enable ctrl-t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement