Grauly

CC_IEC-OS host v3

May 8th, 2021 (edited)
509
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local modemSide = "back"
  2. local credentials = {}
  3. credentials["Grauly"] = {pass="f09513d6da1023d369a37bbad13638865c1d0f7aae3b157395fa7bdc3d2b2e89",access=3}
  4. credentials["Marten"] = {pass="53643a3b756ad2ff75b5e63d865c1fffdd8b93796a85eb7b54ea93c157b6ab64",access=2}
  5.  
  6. -- end of config
  7. sha256 = require("sha256").sha256
  8.  
  9. function startup()
  10.     if (peripheral.isPresent(modemSide)) then
  11.         if (peripheral.getType(modemSide) == "modem") then
  12.             print("Modem found present - starting!")
  13.             rednet.open(modemSide)
  14.             return true
  15.         else
  16.             print("Modem was not found - aborting.")
  17.             return false
  18.         end
  19.     else
  20.         print("Modem was not found - aborting.")
  21.         return false
  22.     end
  23. end
  24.  
  25. function hostnet(net)
  26.     print("Starting " .. net .. "...")
  27.     rednet.host(net, "IEC-Host")
  28.     print(net .. " Successfully hosted!")
  29. end
  30.  
  31. function hostnets()
  32.     hostnet("IECNET")
  33.     hostnet("Turtlenet")
  34.     hostnet("Energynet")
  35.     hostnet("Turretnet")
  36. end
  37.  
  38. function unhostnet(net)
  39.     rednet.unhost(net, "IEC-Host")
  40.     print(net .. " Offline.")
  41. end
  42.  
  43. function unhostnets()
  44.     unhostnet("IECNET")
  45.     unhostnet("Turtlenet")
  46.     unhostnet("Energynet")
  47.     unhostnet("Turretnet")
  48. end
  49.  
  50. function tableContainsValue(table,value)
  51.     if (type(table) == "table") then
  52.         for k,v in pairs(table) do
  53.             if (v == value) then
  54.                 return true
  55.             end
  56.         end
  57.         return false
  58.     else
  59.         return false
  60.     end
  61. end
  62.  
  63. -- rednet handeling and main functionality
  64. local lastTurtleTransmissions = {}
  65. local lastTurtleTransmissionsTimer = {}
  66. local lastEnergyReadouts = {}
  67. local lastEnergyNetTransmissionTimer = {}
  68. local turretRegisters = {}
  69. local lastTurretCommands = {}
  70. local loggedIn = {}
  71.  
  72. function rednetHandeling(sender, message, protocol)
  73.     if (message == "ping") then -- ping
  74.         rednet.send(sender, "pong", protocol)
  75.     elseif (protocol == "Turtlenet") then -- Turtlenet handeling
  76.         if (type(message) == "string") then
  77.             lastTurtleTransmissions[sender] = message;
  78.         end
  79.         lastEnergyNetTransmissionTimer[sender] = 0
  80.     elseif (protocol == "Energynet") then -- Energynet handeling
  81.         lastEnergyReadouts[sender] = message
  82.         lastEnergyNetTransmissionTimer[sender] = 0
  83.     elseif (protocol == "Turretnet") then
  84.         if (type(message) == "table") then
  85.             if (message["command"] == "register") then
  86.                 turretsRegistered = turretRegisters[message["input"]]
  87.                 if (turretsRegistered == nil) then
  88.                     turretsRegistered = {}
  89.                 end
  90.                 if (not tableContainsValue(turretsRegistered,sender)) then
  91.                     table.insert(turretsRegistered,sender)
  92.                     turretRegisters[message["input"]] = turretsRegistered
  93.                     rednet.send(sender,"registered",protocol)
  94.                     turretMessage = {}
  95.                     turretMessage["n"] = false
  96.                     turretMessage["m"] = false
  97.                     turretMessage["p"] = false
  98.                     rednet.send(sender,turretMessage,protocol)
  99.                     lastTurretCommands[message["input"]] = turretMessage
  100.                 end
  101.             end
  102.         end
  103.     elseif (protocol == "IECNET") then
  104.         -- actual protocol handeling
  105.         if (message == "Turtlenet querry") then
  106.             returnMessage = {}
  107.             returnMessage["message"] = lastTurtleTransmissions
  108.             returnMessage["format"] = "turtle"
  109.             rednet.send(sender, returnMessage, protocol)
  110.         elseif (message == "Energynet querry") then
  111.             returnMessage = {}
  112.             returnMessage["message"] = lastEnergyReadouts
  113.             returnMessage["format"] = "energy"
  114.             rednet.send(sender, returnMessage, protocol)
  115.         elseif (message == "Turtlenet Harvest") then
  116.             print("Turtlenet Harvest Initiated")
  117.             returnMessage = {}
  118.             returnMessage["message"] = "Turtlenet Harvest"
  119.             returnMessage["format"] = "Turtlenet"
  120.             rednet.broadcast(returnMessage,"Turtlenet")
  121.         elseif (message == "Turtlenet Reboot") then
  122.             print("Turtlenet Reboot Initiated")
  123.             returnMessage = {}
  124.             returnMessage["message"] = "Turtlenet Reboot"
  125.             returnMessage["format"] = "Turtlenet"
  126.             rednet.broadcast(returnMessage,"Turtlenet")
  127.         elseif (message == "Turretnet querry") then
  128.             if (not (loggedIn[sender] == nil)) then --check if the user is logged in
  129.                 returnMessage = {}
  130.                 returnMessage["message"] = lastTurretCommands
  131.                 returnMessage["format"] = "Securitynet-Data"
  132.                 rednet.send(sender,returnMessage,protocol)
  133.             else
  134.                 rednet.send(sender,"Logout",protocol)
  135.             end
  136.         elseif (type(message) == "table") then
  137.             if (message["format"] == "Securitynet") then
  138.                 message = message["message"]
  139.                 if (message["action"] == "login") then
  140.                     returnMessage = {}
  141.                     returnMessage["message"] = "Denied"
  142.                     if (type(message["user"]) == "string" and type(message["pass"]) == "string") then
  143.                         if (not (credentials[message["user"]] == nil)) then
  144.                             if (credentials[message["user"]]["pass"] == sha256(message["pass"])) then --password check
  145.                                 loggedIn[sender] = {access=credentials[message["user"]]["access"],timeout=0}
  146.                                 returnMessage["message"] = "Success"
  147.                             end
  148.                         end
  149.                     end
  150.                     returnMessage["format"] = "Securitynet-Login"
  151.                     rednet.send(sender, returnMessage, protocol)
  152.                 elseif (message["action"] == "control") then
  153.                     if (not (loggedIn[sender] == nil)) then --check if the user is logged in
  154.                         turretsToRecieve = turretRegisters[message["label"]]
  155.                         turretMessage = {}
  156.                         turretMessage["n"] = lastTurretCommands[message["label"]]["n"]
  157.                         turretMessage["m"] = lastTurretCommands[message["label"]]["m"]
  158.                         turretMessage["p"] = lastTurretCommands[message["label"]]["p"]
  159.                         if (type(message["n"]) == "boolean") then
  160.                             turretMessage["n"] = message["n"]
  161.                         end
  162.                         if (type(message["m"]) == "boolean") then
  163.                             turretMessage["m"] = message["m"]
  164.                         end
  165.                         if (type(message["p"]) == "boolean") then
  166.                             turretMessage["p"] = message["p"]
  167.                         end
  168.                         for k,v in pairs(turretsToRecieve) do
  169.                             rednet.send(v,turretMessage,"Turretnet")
  170.                         end
  171.                         lastTurretCommands[message["label"]] = turretMessage
  172.                     else
  173.                         rednet.send(sender,"Logout",protocol)
  174.                     end
  175.                 end
  176.             end
  177.         end
  178.     end
  179. end
  180.  
  181. function routine()
  182.     for k, v in pairs(lastEnergyNetTransmissionTimer) do
  183.         if (v > 20) then
  184.             readout = lastEnergyReadouts[k]
  185.             if (not (readout == nil)) then
  186.                 readout["online"] = false
  187.                 lastEnergyReadouts[k] = readout
  188.             end
  189.         end
  190.         lastEnergyNetTransmissionTimer[k] = v + 1
  191.     end
  192.     for k, v in pairs(lastTurtleTransmissionsTimer) do
  193.         if (v > 500) then
  194.             lastEnergyReadouts[k] = nil
  195.         end
  196.         lastTurtleTransmissionsTimer[k] = v + 1
  197.     end
  198.     for k, v in pairs(loggedIn) do
  199.         if (v["timeout"] > 300) then
  200.             loggedIn[k] = nil
  201.             rednet.send(k,"Logout","IECNET")
  202.         end
  203.         v["timeout"] = v["timeout"] - 1
  204.     end
  205. end
  206.  
  207. if (not startup()) then
  208.     print("Unable to Open Networking.")
  209.     return
  210. end
  211. hostnets()
  212. print("IEC-Host Active!")
  213.  
  214. timer = os.startTimer(1)
  215. while true do
  216.     event, a1, a2, a3 = os.pullEventRaw()
  217.     if (event == "timer" and a1 == timer) then
  218.         routine()
  219.         timer = os.startTimer(1)
  220.     elseif (event == "rednet_message") then
  221.         rednetHandeling(a1, a2, a3)
  222.         routine()
  223.     elseif (event == "terminate") then
  224.         term.setTextColor(colors.red)
  225.         print("Shutting down...")
  226.         term.setTextColor(colors.white)
  227.         unhostnets()
  228.         rednet.close(modemSide)
  229.         return
  230.     end
  231. end
  232.  
RAW Paste Data