Advertisement
Guest User

osu.lua

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