Advertisement
theinsekt

detector lock 2

Sep 17th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.64 KB | None | 0 0
  1. --pastebin get 1NSzVUq4 startup
  2. local permsPath="perms"
  3. local testing=false
  4.  
  5.  
  6.  
  7. --function to set to enable or disable ctrl-t
  8. --http://www.computercraft.info/forums2/index.php?/topic/2732-disable-ctrlt/
  9. local pullEventCopy = os.pullEvent
  10. function setCtrlT(value)
  11.  if value then
  12.   os.pullEvent = pullEventCopy
  13.  else
  14.   os.pullEvent = os.pullEventRaw
  15.  end
  16. end
  17.  
  18. function clear()
  19.  term.clear()
  20.  term.setCursorPos(1, 1)
  21. end
  22.  
  23.  
  24.  
  25.  
  26. function loadValue(path)
  27.  local succes=true
  28.  if  not fs.exists(path) then
  29.   return nil
  30.  end
  31.  local h=fs.open(path, "r")
  32.  if h==nil then
  33.   return nil
  34.  end
  35.  local value=textutils.unserialize(h.readAll())
  36.  h.close()
  37.  return value
  38. end
  39.  
  40.  
  41.  
  42.  
  43. function saveValue(path,value)
  44.  local h = fs.open(path, "w")
  45.  h.write(textutils.serialize(value))
  46.  h.close()
  47.  return true
  48. end
  49.  
  50.  
  51.  
  52.  
  53. function loadIntoTable(path)
  54. if  not fs.exists(path) then
  55.   return nil
  56.  end
  57.  local h=fs.open(path, "r")
  58.  if h==nil then
  59.   return nil
  60.  end
  61.  local res={}
  62.  while(true) do
  63.   local value=h.readLine()
  64.   if value==nil then break end
  65.   res[value]=true
  66.  end
  67.  h.close()
  68.  return res
  69. end
  70.  
  71.  
  72.  
  73.  
  74. function appendString(path,str)
  75.  --open code from wiki
  76.  --Opens path in either append or write mode, depending on whether it already exists or not.
  77.  local h = fs.open(path, fs.exists(path) and "a" or "w")
  78.  if h==nil then
  79.   return false
  80.  end
  81.  h.writeLine(str)
  82.  h.close()
  83.  return true
  84. end
  85.  
  86.  
  87.  
  88.  
  89. function appendUser(userName,permission)
  90.   if(userName==nil) then
  91.    print("UserName is nil...")
  92.    return
  93.   end
  94.   local append=""
  95.   if type(permission)=="string" and #permission>0 then
  96.    append=" "..permission
  97.   end
  98.   appendString(permsPath,userName..append)
  99. end
  100.  
  101.  
  102.  
  103.  
  104. function extractAdminsAndOpenersFrom(aTable)
  105.  
  106.  
  107.   local users={}
  108.   local admins={}
  109.   local openers={}
  110.   if aTable~=nil then
  111.   for line,_ in pairs(aTable) do
  112.      local userName=nil
  113.      --http://stackoverflow.com/questions/2779700/lua-split-into-words
  114.      for w in line:gmatch("%S+") do
  115.        if userName==nil then
  116.         userName=w
  117.         users[userName]=true
  118.        else
  119.         if w=="admin" then
  120.          admins[userName]=true
  121.         end
  122.         if w=="open" then
  123.          openers[userName]=true
  124.         end
  125.  
  126.        end
  127.      end
  128.   end
  129.   end
  130.   return admins,openers,users
  131. end
  132.  
  133.  
  134.  
  135.  
  136.  
  137. function getSides()
  138.  local adminPath="adminSide"
  139.  local openPath="openSide"
  140.  local doorPath="doorSide"
  141.  
  142.  local adminSide= loadValue(adminPath)
  143.  local openSide= loadValue(openPath)
  144.  local doorSide= loadValue(doorPath)
  145.  
  146.  
  147.  if adminSide==nil or openSide==nil or doorSide==nil then
  148.   clear()
  149.   print("you can now use ctrl-t")
  150.   setCtrlT(true)--enable ctrl-t
  151.   --configure sides
  152.   print("Configuring sides...")
  153.     -- get a side
  154.   local sides=redstone.getSides()
  155.   while(true) do
  156.    for k,v in ipairs(sides) do
  157.     print(k,":",v)
  158.    end
  159.    print("New door side (number):")
  160.    doorSide=read()
  161.    if not sides[tonumber(doorSide)] then
  162.     print("Invalid side")
  163.    else
  164.     doorSide=sides[tonumber(doorSide)]
  165.     break
  166.    end
  167.   end
  168.  
  169.   print("Right click admin player detector")
  170.   local event, side, player=os.pullEvent("player")
  171.   adminSide=side
  172.  
  173.   print("Right click open player detector")
  174.   local event, side, player=os.pullEvent("player")
  175.   openSide=side
  176.  
  177.   print("saving side configuretions")
  178.   setCtrlT(false)--enable ctrl-t
  179.   saveValue(adminPath,adminSide)
  180.   saveValue(openPath,openSide)
  181.   saveValue(doorPath,doorSide)
  182.   sleep(1)
  183.  end
  184.  
  185.  return adminSide,openSide,doorSide
  186. end
  187.  
  188.  
  189. function about()
  190.  print("theinsekt's player detector lock 1.0")
  191.  print("pastebin get 1NSzVUq4 startup")
  192.  print()
  193. end
  194.  
  195.  
  196. function main()
  197. --load permissions from file
  198. local admins,openers,users=extractAdminsAndOpenersFrom(loadIntoTable(permsPath))
  199.  
  200. --count number of admins
  201. local nrAdmins=0
  202. for _,_ in pairs(admins) do
  203.  nrAdmins=nrAdmins+1
  204. end
  205.  
  206. --exit if no admins
  207. if nrAdmins==0 then
  208.  print("No admins, right click any player detector to gain root acces,")
  209.  print("then add admin, after your user name in the file called perms")
  210.  local event, side, player=os.pullEvent("player")
  211.  if users[player]==nil then
  212.    appendUser(player)
  213.    users[player]=true
  214.  end
  215.  setCtrlT(true)--enable ctrl-t
  216.  error("You now have root")
  217. end
  218.  
  219.  
  220. --get  or configure the sides
  221. local adminSide, openSide, doorSide=getSides()
  222. local openTime=3
  223.  
  224.  
  225. while(true) do
  226.  clear()
  227.  if testing then
  228.   print("testing, ctrl-t enabled")
  229.   setCtrlT(true)--enable ctrl-t
  230.  end
  231.  about()
  232.  print("AdminSide: ",adminSide)
  233.  print("DoorSide: ",doorSide)
  234.  print("OpenSide: ",openSide)
  235.  print()
  236.  print("restricted mode:")
  237.  --for k,v in pairs(users) do
  238.  -- print(k," ",v)
  239.  --end
  240.  print("Waiting for player detector...")
  241.  local event, side, player=os.pullEvent("player")
  242.  
  243.  
  244.  if event=="player" then
  245.   if side==adminSide then
  246.    if admins[player]~=nil then
  247.     --enter admin mode
  248.     setCtrlT(true)--enable ctrl-t
  249.     error("You now have root")
  250.    else
  251.     print("Access denied! (not an admin)")
  252.     sleep(2)
  253.    end
  254.   elseif side==openSide and (openers[player]~=nil or admins[player]~=nil) then
  255.    --open door
  256.    print("Opening!")
  257.    redstone.setOutput(doorSide, true)
  258.    sleep(openTime)
  259.    redstone.setOutput(doorSide, false)
  260.   else
  261.    print("Access denied!")
  262.    if users[player]==nil then
  263.     print("Appended user to perms file, with no permissions.")
  264.     appendUser(player)
  265.     users[player]=true
  266.    end
  267.    sleep(2)
  268.   end
  269.  else
  270.   print("Denied!")
  271.   sleep(1)
  272.  end
  273.  sleep(0.15)
  274. end
  275.  
  276.  
  277.  
  278.  
  279. end
  280.  
  281. setCtrlT(false)--disable ctrl-t
  282.  
  283. if testing then
  284.  print("testing, ctrl-t enabled")
  285.  setCtrlT(true)--enable ctrl-t
  286. end
  287.  
  288. main()
  289.  
  290. setCtrlT(true)--enable ctrl-t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement