Advertisement
Guest User

server

a guest
Jun 8th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.65 KB | None | 0 0
  1. users = { }
  2. inhibitors = { }
  3. keyComputers = { }
  4. rsSwitches = { }
  5.  
  6.  
  7. function update()
  8.   file = fs.open("data/users","w")
  9.   file.write(textutils.serialise(users))
  10.   file.close()
  11.  
  12.   file = fs.open("data/inhibitors","w")
  13.   file.write(textutils.serialise(inhibitors))
  14.   file.close()
  15.  
  16.   file = fs.open("data/keyComputers","w")
  17.   file.write(textutils.serialise(keyComputers))
  18.   file.close()
  19.  
  20.   file = fs.open("data/rsSwitches","w")
  21.   file.write(textutils.serialise(rsSwitches))
  22.   file.close()
  23.  
  24.   d = {requestClearance,creationClearance,adminClearance}
  25.   file = fs.open("data/stats","w")
  26.   file.write(textutils.serialise(d))
  27.   file.close()
  28. end
  29.  
  30. function clear()
  31.   term.clear()
  32.   term.setCursorPos(1,1)
  33. end
  34.  
  35. function randomString(length)
  36.   allLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV0123456789"
  37.   e = ""
  38.  
  39.   for i = 1, length do
  40.     r = math.random(#allLetters)
  41.     e = e..string.sub(allLetters,r,r)
  42.   end
  43.  
  44.   return e
  45. end
  46.  
  47. if not fs.exists("data") then
  48.   clear()
  49.   print("SETTING UP SERVER")
  50.   cID = os.getComputerID()
  51.   print("SERVER ID IS "..cID)
  52.   print("REMEMBER THAT")
  53.   print()
  54.   print("PRESS ENTER")
  55.   read()
  56.   clear()
  57.   print("NEED TO SET CLEARANCES")
  58.   print()
  59.   term.write("HIGHEST SECURITY CLEARANCE? ")
  60.   adminClearance = tonumber(read())
  61.   print()
  62.   term.write("CLEARANCE REQUIRED TO REQUEST LOG IN? ")
  63.   requestClearance = tonumber(read())
  64.   print()
  65.   term.write("CLEARANCE REQUIRED TO CREATE USER? ")
  66.   creationClearance = tonumber(read())
  67.   clear()
  68.   print("NEED TO CREATE ADMIN ACCOUNT")
  69.   print()
  70.   term.write("USERNAME: ")
  71.   username = read()
  72.   print()
  73.   term.write("PASSWORD: ")
  74.   password = read("*")
  75.   users = { {username,randomString(6),adminClearance,password} }
  76.  
  77.   file = fs.open("data/users","w")
  78.   file.write(textutils.serialise(users))
  79.   file.close()
  80.  
  81.   file = fs.open("data/inhibitors","w")
  82.   file.close()
  83.  
  84.   file = fs.open("data/keyComputers","w")
  85.   file.close()
  86.  
  87.   file = fs.open("data/rsSwitches","w")
  88.   file.close()
  89.  
  90.   d = { requestClearance, creationClearance, adminClearance}
  91.   file = fs.open("data/stats","w")
  92.   file.write(textutils.serialise(d))
  93.   file.close()
  94. else
  95.   file = fs.open("data/users","r")
  96.   data = file.readAll()
  97.   file.close()
  98.   users = textutils.unserialise(data)
  99.  
  100.   file = fs.open("data/inhibitors","r")
  101.   data = file.readAll()
  102.   file.close()
  103.   inhibitors = textutils.unserialise(data)
  104.  
  105.   file = fs.open("data/keyComputers","r")
  106.   data = file.readAll()
  107.   file.close()
  108.   keyComputers = textutils.unserialise(data)
  109.  
  110.   file = fs.open("data/rsSwitches","r")
  111.   data = file.readAll()
  112.   file.close()
  113.   rsSwitches = textutils.unserialise(data)
  114.  
  115.   file = fs.open("data/stats","r")
  116.   data = file.readAll()
  117.   file.close()
  118.   d = textutils.unserialise(data)
  119.   requestClearance = d[1]
  120.   creationClearance = d[2]
  121.   adminClearance = d[3]
  122. end
  123.  
  124. for i,v in ipairs(peripheral.getNames()) do
  125.   if peripheral.getType(v) == "modem" then
  126.     modemSide = v
  127.     break
  128.   end
  129. end
  130.    
  131. rednet.open(modemSide)
  132. clear()
  133.  
  134. for i,v in ipairs(inhibitors) do
  135.   rednet.send(v[1], "GETSTATE")
  136.   id,msg,d = rednet.receive(.5)
  137.   if msg ~= nil then
  138.     if msg then
  139.       v[2] = "ACTIVE"
  140.     else
  141.       v[2] = "DEACTIVE"
  142.     end
  143.   end
  144. end
  145.  
  146. for i,v in ipairs(keyComputers) do
  147.   rednet.send(v[1], "GETSTATE")
  148.   id,msg,d = rednet.receive(.5)
  149.   if msg ~= nil then
  150.     if msg then
  151.       v[2] = "ACTIVE"
  152.     else
  153.       v[2] = "DEACTIVE"
  154.     end
  155.   end
  156. end
  157.  
  158. for i,v in ipairs(rsSwitches) do
  159.   rednet.send(v[1], "GETSTATE")
  160.   id,msg,d = rednet.receive(.5)
  161.   if msg ~= nil then
  162.     if msg then
  163.       v[2] = "ACTIVE"
  164.     else
  165.       v[2] = "DEACTIVE"
  166.     end
  167.   end
  168. end
  169.  
  170. function printStats()
  171.   clear()
  172.   print("INHIBITORS")
  173.   for i,v in ipairs(inhibitors) do
  174.     print(v[4]..": "..v[2])
  175.   end
  176.   print()
  177.   print("KEY COMPUTER")
  178.   for i,v in ipairs(keyComputers) do
  179.     print(v[4]..": "..v[2])
  180.   end
  181.   print()
  182.   print("RS SWITCHES")
  183.   for i,v in ipairs(rsSwitches) do
  184.     print(v[4]..": "..v[2])
  185.   end
  186. end
  187.  
  188. printStats()
  189.  
  190. while true do
  191.   id,msg,d = rednet.receive()
  192.   if type(msg) == "table" then
  193.     sender = msg[1]
  194.     cmd = msg[2]
  195.    
  196.     if type(sender) ~= "table" and sender == "DEVICE" then
  197.       deviceType = msg[2]
  198.       cmd = msg[3]
  199.      
  200.       if deviceType == "KEY_LOCK" then
  201.         for i,v in ipairs(keyComputers) do
  202.           if v[1] == id then
  203.             if cmd == "GET_APPROVED_IDS" then
  204.               approvedIDs = {}
  205.               for j,k in ipairs(users) do
  206.                 if k[3] >= v[5] then
  207.                   table.insert(approvedIDs, k[2])
  208.                 end
  209.               end
  210.               rednet.send(id,approvedIDs)
  211.               break
  212.             end
  213.           end
  214.         end
  215.       end
  216.      
  217.     else
  218.      
  219.       username = sender[1]
  220.       password = sender[4]
  221.       clearance = sender[3]
  222.       accountID = sender[2]
  223.      
  224.       if cmd == "LOG_IN" then
  225.        
  226.         for i,v in ipairs(users) do
  227.           if v[1] == username and v[4] == password then
  228.             if v[3] >= requestClearance then
  229.               rednet.send(id,v)
  230.             end
  231.           end
  232.         end
  233.        
  234.       else
  235.        
  236.         for i,v in ipairs(users) do
  237.           if v[1] == username then
  238.             if v[4] == password and v[3] == clearance and v[2] == accountID then
  239.          
  240.               if cmd == "CHANGE_INHIBITOR" then
  241.                 inhibitorID = msg[3]
  242.                 for i,v in ipairs(inhibitors) do
  243.                   if inhibitorID == v[1] then
  244.                     if sender[3] >= v[3] then
  245.                       rednet.send(inhibitorID, "CHANGE")
  246.                       id,msg,d = rednet.receive(.3)
  247.                       if msg ~= nil then
  248.                         v[2] = msg
  249.                       else
  250.                         v[2] = "OFFLINE"
  251.                       end
  252.                       printStats()
  253.                       break
  254.                     end
  255.                   end
  256.                 end
  257.               elseif cmd == "CHANGE_KEY_LOCK" then
  258.                 keyID = msg[3]
  259.                 for i,v in ipairs(keyComputers) do
  260.                   if keyID == v[1] then
  261.                     if sender[3] >= v[3] then
  262.                       rednet.send(keyID, "CHANGE")
  263.                       id,msg,d = rednet.receive(.3)
  264.                       if msg ~= nil then
  265.                         v[2] = msg
  266.                       else
  267.                         v[2] = "OFFLINE"
  268.                       end
  269.                       printStats()
  270.                       break
  271.                     end
  272.                   end
  273.                 end
  274.               elseif cmd == "CHANGE_SWITCH" then
  275.                 switchID = msg[3]
  276.                 for i,v in ipairs(rsSwitches) do
  277.                   if switchID == v[1] then
  278.                     if sender[3] >= v[3] then
  279.                       rednet.send(switchID, "CHANGE")
  280.                       id,msg,d = rednet.receive(.3)
  281.                       if msg ~= nil then
  282.                         v[2] = msg
  283.                       else
  284.                         v[2] = "OFFLINE"
  285.                       end
  286.                       printStats()
  287.                     end
  288.                   end
  289.                 end
  290.               elseif cmd == "GET_INHIBITORS " then
  291.                 if sender[3] >=requestClearance then
  292.                   rednet.send(id,inhibitors)
  293.                 end
  294.               elseif cmd == "GET_SWITCHES" then
  295.                 if sender[3] >= requestClearance then
  296.                   rednet.send(id,rsSwitches)
  297.                 end
  298.               elseif cmd == "GET_KEY_LOCKS " then
  299.                 if sender[3] >= requestClearance then
  300.                   rednet.send(id,keyComputers)
  301.                 end
  302.               elseif cmd == "RESET_CARD" then
  303.                 v[2] = msg[3]
  304.                 update()
  305.               elseif cmd == "RESET_PASSWORD" then
  306.                 v[4] = msg[3]
  307.                 update()
  308.               elseif cmd == "ADD_ACCOUNT" then
  309.                 if clearance >= creationClearance then
  310.                   newPass = randomString(4)
  311.                   table.insert(users,{msg[3],randomString(6),1,newPass})
  312.                   rednet.send(id,newPass)
  313.                   update()
  314.                 end
  315.               elseif cmd == "CHANGE_KEY_LOCK_CLEARANCE" then
  316.                 if clearance >= adminClearance then
  317.                   for i,v in ipairs(keyComputers) do
  318.                     if v[1] == msg[3][1] then
  319.                       v[5] = msg[4]
  320.                       update()
  321.                     end
  322.                   end
  323.                 end
  324.               elseif cmd == "GET_USERS" then
  325.                 if clearance >= adminClearance then
  326.                   sendArray = {}
  327.                   for i,v in ipairs(users) do
  328.                     table.insert(sendArray,{v[1],v[2],v[3]})
  329.                   end
  330.                  
  331.                   rednet.send(id,sendArray)
  332.                 end
  333.               elseif cmd == "CHANGE_USER_CLEARANCE" then
  334.                
  335.                 if clearance >= adminClearance then
  336.                   for i,v in ipairs(users) do
  337.                     if v[1] == msg[3][1] and not (msg[4] >= adminClearance + 1) and not (v[1] == username) and not(v[3] >= adminClearance + 1 )then
  338.                       v[3] = msg[4]
  339.                       update()
  340.                     end
  341.                   end
  342.                 end
  343.               elseif cmd == "REMOVE_USER" then
  344.                 if clearance >= adminClearance then
  345.                   for i,v in ipairs(users) do
  346.                     if v[1] == msg[3][1] and not (v[1] == username) then
  347.                       table.remove(users,i)
  348.                       update()
  349.                     end
  350.                   end
  351.                 end
  352.               elseif cmd == "REGISTER_INHIBITOR" then
  353.                 if clearance >= adminClearance then
  354.                   table.insert(inhibitors,{msg[3],msg[4],msg[5],msg[6]})
  355.                   rednet.send(id,"CREATED")
  356.                   update()
  357.                 end
  358.               elseif cmd == "REGISTER_KEY_LOCK" then
  359.                 if clearance >= adminClearance then
  360.                   table.insert(keyComputers,{msg[3],msg[4],msg[5],msg[6],msg[7]})
  361.                   rednet.send(id,"CREATED")
  362.                   update()
  363.                 end
  364.               elseif cmd == "REGISTER_RS_SWITCH" then
  365.                 if clearance >= adminClearance then
  366.                   table.insert(rsSwitches,{msg[3],msg[4],msg[5],msg[6]})
  367.                   rednet.send(id,"CREATED")
  368.                   update()
  369.                 end
  370.               end
  371.               break
  372.             end
  373.           end
  374.         end
  375.       end
  376.     end
  377.   end
  378. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement