Advertisement
Guest User

Untitled

a guest
Jul 9th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local event = require("event")
  4. local term = require("term")
  5. local data = component.data
  6. local modem = component.modem
  7. modem.open(340)
  8. local ok, config = pcall(dofile, "/usr/etc/osu_config.lua")
  9. if not ok or not type(config) == "table" then
  10.   print("Error, failed to load config because '" .. config .. "'. Exiting!")
  11.   os.exit()
  12. end
  13. local args = require 'shell'.parse(...)
  14. local function containsValue(table, element)
  15.   for _, value in pairs(table) do
  16.     if value == element then
  17.       return true
  18.     end
  19.   end
  20.   return false
  21. end
  22. function containsKey(table, element)
  23.   for key, _ in pairs(table) do
  24.     if key == element then
  25.       return true
  26.     end
  27.   end
  28.   return false
  29. end
  30. local function onReceive(_, _, _, port, _, program, module, command, password, text, args1, args2, args3)
  31.   if port == 340 then
  32.     if program == "OSU" then
  33.       password = data.decrypt(password, config.modules.network.encryption.key, config.modules.network.encryption.iv)
  34.       text = data.decrypt(text, config.modules.network.encryption.key, config.modules.network.encryption.iv)
  35.       if password == config.modules.network.password then
  36.         if module == "DOOR" then
  37.           if command == "UNLOCKALL" then
  38.             if text == config.modules.door.override then
  39.               for addr in component.list("os_doorcontroller") do
  40.                 component.invoke(addr, "open")
  41.               end
  42.             else
  43.               modem.broadcast(340, "OSU", "NETWORK", "RESPONSE", data.encrypt(config.modules.network.password, config.modules.network.encryption.key, config.modules.network.encryption.iv), "INVALID_OVERRIDE_CODE")
  44.             end
  45.           elseif command == "LOCKALL" then
  46.             if text == config.modules.door.override then
  47.               for addr in component.list("os_doorcontroller") do
  48.                 component.invoke(addr, "close")
  49.               end
  50.             else
  51.               modem.broadcast(340, "OSU", "NETWORK", "RESPONSE", data.encrypt(config.modules.network.password, config.modules.network.encryption.key, config.modules.network.encryption.iv), "INVALID_OVERRIDE_CODE")
  52.             end
  53.           end
  54.         elseif module == "LOCKDOWN" then
  55.           if command == "INIT" then
  56.             if text == config.modules.lockdown.password then
  57.               if component.isAvailable("os_doorcontroller") then
  58.                 for addr in component.list("os_doorcontroller") do
  59.                   component.invoke(addr, "close")
  60.                 end
  61.               else
  62.                 modem.broadcast(340, "OSU", "NETWORK", "RESPONSE", data.encrypt(config.modules.network.password, config.modules.network.encryption.key, config.modules.network.encryption.iv), "INVALID_LOCKDOWN_CODE")
  63.               end
  64.               if component.isAvailable("os_alarm") then
  65.                 for addr in component.list("os_alarm") do
  66.                   component.invoke(addr, "activate")
  67.                 end
  68.               end
  69.               if component.isAvailable("os_rolldoorcontroller") then
  70.                 for addr in component.list("os_rolldoorcontroller") do
  71.                   component.invoke(addr, "close")
  72.                 end
  73.               end
  74.             end
  75.           elseif command == "STOP" then
  76.             if text == config.modules.lockdown.password then
  77.               if component.isAvailable("os_alarm") then
  78.                 for addr in component.list("os_alarm") do
  79.                   component.invoke(addr, "deactivate")
  80.                 end
  81.               else
  82.                 modem.broadcast(340, "OSU", "NETWORK", "RESPONSE", data.encrypt(config.modules.network.password, config.modules.network.encryption.key, config.modules.network.encryption.iv), "INVALID_LOCKDOWN_CODE")
  83.               end
  84.             end
  85.           end
  86.         elseif module == "NETWORK" then
  87.           if command == "REBOOT_ALL" then
  88.             if text == config.reboot_password then
  89.               computer.shutdown(true)
  90.             end
  91.           end
  92.         end
  93.       end
  94.     end
  95.   end
  96. end
  97. local function unlockDoor(_, _, username, data, _, _, _)
  98.   if data == config.modules.door.carddata then
  99.     if not containsValue(config.modules.door.blacklist, username) then
  100.       for addr, type in component.list("os_doorcontroller") do
  101.         component.invoke(addr, "open")
  102.       end
  103.     else
  104.       modem.broadcast(430, "OSU", "DOOR", "UNLOCK", data.encrypt(config.modules.network.password, config.modules.network.encryption.key, config.modules.network.encryption.iv), "Blacklisted user " .. username .. " tried to open the door attached to this computer, with correct card data!!")
  105.       return
  106.     end
  107.   else
  108.     if containsValue(config.modules.door.blacklist, username) then
  109.       modem.broadcast(430, "OSU", "DOOR", "UNLOCK", data.encrypt(config.modules.network.password, config.modules.network.encryption.key, config.modules.network.encryption.iv), "Blacklisted user " .. username .. " tried to open the door attached to this computer, with incorrect card data " .. data .. "!")
  110.       return
  111.     else
  112.       modem.broadcast(430, "OSU", "DOOR", "UNLOCK", data.encrypt(config.modules.network.password, config.modules.network.encryption.key, config.modules.network.encryption.iv), "User " .. username .. " tried to open the door attached to this computer, with incorrect card data " .. data .. "!")
  113.       return
  114.     end
  115.   end
  116.   os.sleep(config.modules.door.unlocktime)
  117.   for addr, type in component.list("os_doorcontroller") do
  118.     component.invoke(addr, "close")
  119.   end
  120. end
  121. local function activateAlarms(_, _, _, _, _, username)
  122.   if not containsValue(config.modules.alert.whitelist, username) then
  123.     for addr, type in component.list("os_alarm") do
  124.       component.invoke(addr, "activate")
  125.     end
  126.   else
  127.     return
  128.   end
  129. end
  130. if args[1] == "modules" then
  131.   if args[2] == "cardwriter" then
  132.     if args[3] == "write" then
  133.       if args[4] == nil then
  134.         io.write("Data: ")
  135.         text = io.read()
  136.       else
  137.         text = args[4]
  138.       end
  139.       if args[5] == nil then
  140.         io.write("Name: ")
  141.         name = io.read()
  142.       else
  143.         name = args[5]
  144.       end
  145.       ok, why = pcall(component.os_cardwriter.write, text, name, true)
  146.       if not ok then
  147.         print("Error, operation failed. Reason: " .. why)
  148.         return
  149.       end
  150.       modem.broadcast(430, "OSU", "CARDWRITER", "WRITE", data.encrypt(config.modules.network.password, config.modules.network.encryption.key, config.modules.network.encryption.iv), "A user wrote card with data " .. text .. " and name " .. name .. " on the cardwriter conected to this computer!")
  151.     end
  152.   elseif args[2] == "door" then
  153.     if not component.isAvailable("os_doorcontroller") then
  154.       print("Error, door controller not available. Exiting.")
  155.       os.exit()
  156.     end
  157.     if args[3] == "override" then
  158.       if args[4] == nil then
  159.         io.write("Password: ")
  160.         options = {}
  161.         options.pwchar = " "
  162.         pass = term.read(options)
  163.       else
  164.         pass = args[4]
  165.       end
  166.       if pass == config.modules.door.override then
  167.         modem.broadcast(430, "OSU", "DOOR", "UNLOCKALL", data.encrypt(config.modules.network.password, config.modules.network.encryption.key, config.modules.network.encryption.iv), data.encrypt(config.modules.door.override, config.modules.network.encryption.key, config.modules.network.encryption.iv))
  168.         for addr in component.list("os_doorcontroller") do
  169.           component.invoke(addr, "open")
  170.         end
  171.       else
  172.         print("Incorrect password!")
  173.       end
  174.     elseif args[3] == "override_lock" then
  175.       if args[4] == nil then
  176.         io.write("Password: ")
  177.         options = {}
  178.         options.pwchar = " "
  179.         pass = term.read(options)
  180.       else
  181.         pass = args[4]
  182.       end
  183.       if pass == config.modules.door.override then
  184.         for addr, type in component.list("os_doorcontroller") do
  185.           component.invoke(addr, "close")
  186.         end
  187.       else
  188.         print("Incorrect password!")
  189.       end
  190.     elseif args[3] == "start_magnetic" then
  191.       if not component.isAvailable("os_magreader") then
  192.         print("Error, magnetic card reader not available. Exiting.")
  193.         os.exit()
  194.       end
  195.       id_door, _, _, username, data, _, _, _ = event.listen("magData", unlockDoor)
  196.     elseif args[3] == "stop" then
  197.       if id_door == nil then
  198.         print("Error, not started.")
  199.         os.exit()
  200.       end
  201.       event.cancel(id_door)
  202.     end
  203.   elseif args[2] == nil then
  204.     print("Usage: osu modules [MODULE] [COMMAND]")
  205.   elseif args[2] == "alert" then
  206.     if args[3] == "start" then
  207.       if not component.isAvailable("motion_sensor") then
  208.         print("Error, motion sensor not available.")
  209.         os.exit()
  210.       end
  211.       if not component.isAvailable("os_alarm") then
  212.         print("Error, alarm not available.")
  213.         os.exit()
  214.       end
  215.       id_alert, _, _, _, _, _, username = event.listen("motion", activateAlarms)
  216.     elseif args[3] == "stop" then
  217.       event.cancel(id_alert)
  218.       for addr in component.list("os_alarm") do
  219.         component.proxy(addr).deactivate()
  220.       end
  221.     end
  222.   elseif args[2] == "network" then
  223.     if args[3] == "start_client" then
  224.       id_network, _, _, sender, port, _, program, module, command, password, data, args1, args2, args3 = event.listen("modem_message", onReceive)
  225.     elseif args[3] == "stop_client" then
  226.       event.cancel(id_network)
  227.     elseif args[3] == "send_message" then
  228.       io.write("Message: ")
  229.       message = io.read()
  230.       modem.broadcast(340, "OSU", "NETWORK", "SEND_MESSAGE", data.encrypt(config.modules.network.password, config.modules.network.encryption.key, config.modules.network.encryption.iv), message)
  231.     elseif args[3] == "display" then
  232.       term.clear()
  233.       function printOutput()
  234.         _, _, sender, port, _, program, module, command, password, data, args1, args2, args3 = event.pull("modem_message")
  235.         if port == 430 then
  236.           if program == "OSU" then
  237.             password = data.decrypt(password, config.modules.network.encryption.key, config.modules.network.encryption.iv)
  238.             if password == config.modules.network.password then
  239.               print("Received message from " .. sender .. "'s module '" .. module .. "'. Data: " .. data)
  240.             end
  241.           end
  242.         end
  243.       end
  244.     else
  245.       print("Usage: osu modules network [COMMAND].")
  246.       print("Commands:")
  247.       print(" osu modules network start_client - listens for network messages from other clients / the main server")
  248.       print(" osu modules network stop_client - stops listening for messages")
  249.       print(" osu modules network send_message - sends a network message")
  250.       print(" osu modules network display - displays all received OSU network messages")
  251.     end
  252.   else
  253.     print("Error, not a valid module.")
  254.     print("Valid modules: ")
  255.     for k, v in pairs(config.modules) do
  256.       print(k)
  257.     end
  258.   end
  259. elseif args[1] == "components" then
  260.   if args[2] == "list" then
  261.     for address, type in component.list() do
  262.       if type:match("os_") == "os_" then
  263.         print(type:match("^.+_(.+)$"))
  264.       end
  265.     end
  266.   else
  267.     print("Usage: osu components [COMMAND]")
  268.     print("Commands:")
  269.     print("  list - lists all connected OpenSecurity components")
  270.   end
  271. else
  272.   print("Usage: osu [COMMAND]")
  273.   print("Commands:")
  274.   print("  osu modules - module-related commands")
  275.   print("  osu components - component-related commands")
  276. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement