Guest User

OpenPDetect

a guest
Aug 26th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.70 KB | None | 0 0
  1. runningProgram = shell.getRunningProgram()
  2.  
  3. LogFile.i('OpenPDetect Program Successfully Started', runningProgram)
  4. current = {
  5.   sensor = {},
  6.   settings = {
  7.     whiteList = {}
  8.   }
  9. }
  10. Events ={}
  11. elements = {}
  12. current.config = false
  13. termX, termY = term.getSize()
  14.  
  15.  
  16. function doClick(event, btn, x, y)
  17.     functionToRun = element.tryClick(elements, x, y)
  18.     if functionToRun then --Check click location
  19.         functionToRun()
  20.     end
  21. end
  22.  
  23. function checkClick(event, btn, x, y)
  24.     if not isSettingUp then
  25.         doClick(event, btn, x, y)
  26.     end
  27. end
  28.  
  29. function btnInit(btnText, btnWidth, btnHeight, btnX, btnY, btnTC, btnBG, oTC, oBG, onClick, toggle, secBG, secTC, secText) --Function to create button
  30.     local btn = element.create(btnText, btnWidth, btnHeight, btnX, btnY, btnTC, btnBG, oTC, oBG, onClick, toggle, secBG, secTC, secText) --Calls API to generate button
  31.     table.insert(elements, btn) --Inserts into table so it can be scanned later
  32.     element.opacity(btn, true) --Sets visibility to true
  33.     return btn
  34. end
  35.  
  36. function drawTitle()
  37.   term.clear()
  38.   titleBar.draw('HbombOS Security Suite', 'OpenP Detector Lock', colors.cyan, 256, 128, 256, 1)
  39. end
  40.  
  41. function setup()
  42. LogFile.i('OpenPDetect Setup Started', runningProgram)
  43.   local function setupWelcome()
  44.     titleBar.draw('HbombOS Security Suite', 'OpenP Detector Lock Setup', colors.cyan, 256, 128, 256, 1)
  45.   end
  46.  
  47.   local function Intro()
  48.     LogFile.i('OpenPDetect Setup Intro', runningProgram)
  49.     setupWelcome()
  50.     printer.centered('Welcome To OpenP Detector Setup', 6)
  51.     printer.centered('This Program Uses Open Peripherals', 8)
  52.     printer.centered('Sensor Block', 9)
  53.     printer.centered('Therefore To Use This Program You Need', 11)
  54.     printer.centered('Open Peripheral Addons Installed', 12)
  55.     printer.centered('Click Start To Setup', 19)
  56.     start = btnInit('Start', nil, nil, termX-2-#'Start', termY-1, 1, colors.cyan, 1, 256, function() os.queueEvent('start') end, false, nil, nil, nil, nil)
  57.         while true do
  58.             local event, btn, x, y = os.pullEvent()
  59.             if event == "mouse_click" then
  60.                 doClick(event, btn, x, y)
  61.             elseif event == "start" then
  62.                 element.opacity(start, false)
  63.                 break
  64.             end
  65.         end
  66.         return
  67.   end
  68.  
  69.   local function whitelistAdd()
  70.   LogFile.i('OpenPDetect whitelist Add Start', runningProgram)
  71.   continue = btnInit('Continue', nil, nil, termX-2-#'Continue', termY-1, 1, colors.cyan, 1, 256, function() os.queueEvent('Continue') end, false, nil, nil, nil, nil)
  72.   while true do
  73.     local function addNames()
  74.     while true do
  75.       inList = false
  76.       setupWelcome()
  77.       element.opacity(continue, true)
  78.       printer.centered('Enter The Names You Would Like To', 6)
  79.       printer.centered('Add To The Detector WhiteList', 7)
  80.       printer.centered('These Are The Players Who Will Have Access', 9)
  81.       printer.centered('Enter The Username, And Hit Enter To Add', 10)
  82.       term.setCursorPos(termX/2-#'Username: ', 13)
  83.       write('Username: ')
  84.       local input = read()
  85.         if input and input ~= "" then
  86.           for _, entry in ipairs(current.settings.whiteList) do
  87.             if entry == input then
  88.               inList = true
  89.               printer.centered('Username Already In Whitelist', 19)
  90.               LogFile.w('User Tried To Add Username To Whitelist That Already Existed', runningProgram)
  91.               sleep(1)
  92.             end
  93.           end
  94.           if inList == false then
  95.             printer.centered('Adding Username To whitelist', 19)
  96.             LogFile.i('Adding Username: '..input..' Into Whitelist', runningProgram)
  97.               if current.settings.whiteList then
  98.                 table.insert(current.settings.whiteList, input)
  99.                 printer.centered('Username Inserted Into Whitelist', 19)
  100.                 sleep(0.5)
  101.               else
  102.                 LogFile.e('Whitelist Table Is Missing.. Fatal', runningProgram)
  103.                 error'Table Appears To Be Missing'
  104.               end
  105.           end
  106.         end
  107.     end
  108.     end
  109.    
  110.     local function buttonPress()
  111.         while true do
  112.             local event, btn, x, y = os.pullEvent('mouse_click')
  113.             doClick(event, btn, x, y)
  114.         end
  115.     end
  116.    
  117.     local function nextStep()
  118.       while true do
  119.         local event = os.pullEvent('Continue')
  120.         if #current.settings.whiteList <= 0 then
  121.           term.setCursorBlink(false)
  122.           printer.centered('Please Register At Least ONE Username', 19)
  123.           LogFile.w('User Tried To Skip Whitelist When 0 Usernames Are In Table', runningProgram)
  124.           sleep(0.5)
  125.           lessThanOne = true
  126.           return
  127.         else
  128.           lessThanOne = false
  129.           return key
  130.         end
  131.       end
  132.     end
  133.    
  134.     parallel.waitForAny(nextStep, addNames, buttonPress)
  135.     if not lessThanOne then LogFile.i('Whitelist Complete', runningProgram) break end
  136.   end
  137.   return
  138.   end
  139.  
  140.   local function ouputOption()
  141.     element.opacity(continue, false)
  142.     yes = btnInit('Enable', nil, nil, termX-2-#'Enable', termY-1, 1, colors.green, 1, 256, function() current.settings.outputOption = true os.queueEvent('Yes') end, false, nil, nil, nil, nil)
  143.     no = btnInit('Dont Enable', nil, nil, termX-6-#'Enable'-#'Dont Enable', termY-1, 1, colors.red, 1, 256, function() current.settings.outputOption = false os.queueEvent('No') end, false, nil, nil, nil, nil)
  144.     term.setCursorBlink(false)
  145.     setupWelcome()
  146.     element.opacity(yes, true)
  147.     element.opacity(no, true)
  148.     printer.centered('Do You Want The Computer To Ouput', 6)
  149.     printer.centered('A Redstone Signal When The Players Is', 7)
  150.     printer.centered('On The Whitelist', 8)
  151.     printer.centered('You Pick What Side It Outputs From Next Step', 19)
  152.       while true do
  153.         local event, p1, p2, p3 = os.pullEvent()
  154.         if event == "mouse_click" then
  155.             doClick(event, p1, p2, p3)
  156.         elseif event == 'Yes' then
  157.             return true
  158.         elseif event == "No" then
  159.             return false
  160.         end
  161.       end
  162.   end
  163.  
  164.   local function outputSide()
  165.     for _, elem in ipairs(elements) do
  166.         elem.toggleState = 1
  167.         element.opacity(elem, false)
  168.     end
  169.     setupWelcome()
  170.     right = btnInit('Right', nil, nil, 5, 10, 1, colors.cyan, 1, 256, function()current.settings.redstoneSide = 'right' end, true, colors.green, 1, 'Right', 'rsOut')
  171.     right.toggleState = 2
  172.     element.opacity(right, true)
  173.     current.settings.redstoneSide = 'right'
  174.     left = btnInit('Left', nil, nil, right.x+right.width+2, 10, 1, colors.cyan, 1, 256, function() current.settings.redstoneSide = 'left' end, true, colors.green, 1, 'Left', 'rsOut')
  175.     top = btnInit('Top', nil, nil, left.x+left.width+2, 10, 1, colors.cyan, 1, 256, function() current.settings.redstoneSide = 'top' end, true, colors.green, 1, 'Top', 'rsOut')
  176.     bottom = btnInit('Bottom', nil, nil, top.x+top.width+2, 10, 1, colors.cyan, 1, 256, function() current.settings.redstoneSide = 'bottom' end, true, colors.green, 1, 'Bottom', 'rsOut')
  177.     back = btnInit('Back', nil, nil, bottom.x+bottom.width+2, 10, 1, colors.cyan, 1, 256, function() current.settings.redstoneSide = 'back' end, true, colors.green, 1, 'Back', 'rsOut')
  178.     element.opacity(continue, true)
  179.     while true do
  180.         printer.centered("Redstone Output Side", 6)
  181.         printer.centered("Click A Button To Decide Your", termY-1)
  182.         printer.centered("Redstone Output Side", termY)
  183.         element.opacity(continue, true)
  184.         local event, btn, x, y = os.pullEvent()
  185.         if event == "mouse_click" then
  186.             doClick(event, btn, x, y)
  187.         elseif event == "Continue" then
  188.             break
  189.         end
  190.     end
  191.    
  192.     for _, elem in ipairs(elements) do
  193.         if elem.parent == "rsOut" then
  194.             element.opacity(elem, false)
  195.         end
  196.     end
  197.     element.opacity(continue ,false)
  198.     return
  199.   end
  200.  
  201.     local function alarmOption()
  202.     element.opacity(continue, false)
  203.     yes.func = function() current.settings.alarmOption = true os.queueEvent('Yes') end
  204.     no.func = function() current.settings.alarmOption = false os.queueEvent('No') end
  205.     term.setCursorBlink(false)
  206.     setupWelcome() 
  207.     element.opacity(yes, true)
  208.     element.opacity(no, true)
  209.     printer.centered('Do You Want The Computer To Ouput', 6)
  210.     printer.centered('A Redstone Signal When The Players Is', 7)
  211.     printer.centered('NOT On The Whitelist', 8)
  212.     printer.centered('You Pick What Side It Outputs From Next Step', 19)
  213.       while true do
  214.         local event, p1, p2, p3 = os.pullEvent()
  215.         if event == "mouse_click" then
  216.             doClick(event, p1, p2, p3)
  217.         elseif event == 'Yes' then
  218.             return true
  219.         elseif event == "No" then
  220.             return false
  221.         end
  222.       end
  223.   end
  224.  
  225.   local function alarmOutput()
  226.         for _, elem in ipairs(elements) do
  227.             elem.toggleState = 1
  228.             element.opacity(elem, false)
  229.         end
  230.         setupWelcome()
  231.         continue.func = function() os.queueEvent('Continue') end
  232.         printer.centered("Redstone Alarm Output Side", 6)
  233.         printer.centered("Click A Button To Decide Your", termY-1)
  234.         printer.centered("Output Side", termY)
  235.         element.opacity(continue, true)
  236.         current.settings.alarmRedstoneSide = 'right'
  237.         right.func = function() current.settings.alarmRedstoneSide = 'right' end
  238.         right.parent = 'rsAlarm'       
  239.         right.toggleState = 2
  240.         left.func = function() current.settings.alarmRedstoneSide = 'left' end
  241.         left.parent = 'rsAlarm'
  242.         top.func = function() current.settings.alarmRedstoneSide = 'top' end
  243.         top.parent = 'rsAlarm'
  244.         bottom.func = function() current.settings.alarmRedstoneSide = 'bottom' end
  245.         bottom.parent = 'rsAlarm'
  246.         back.func = function() current.settings.alarmRedstoneSide = 'back' end
  247.         back.parent = 'rsAlarm'    
  248.         for _, elem in ipairs(elements) do
  249.             if elem.parent == "rsAlarm" then
  250.                 element.opacity(elem, true)
  251.             end
  252.         end
  253.         while true do
  254.             element.opacity(nextBtn, true)
  255.             local event, btn, x, y = os.pullEvent()
  256.             if event == "mouse_click" then
  257.                 doClick(event, btn, x, y)
  258.             elseif event == "Continue" then
  259.                 break
  260.             end
  261.         end
  262.         for _, elem in ipairs(elements) do
  263.             if elem.parent == "rsAlarm" then
  264.                 element.opacity(elem, false)
  265.             end
  266.         end
  267.         element.opacity(nextBtn ,false)
  268.     end
  269.  
  270.   local function setupSave()
  271.     LogFile.i('Setup Complete', runningProgram)
  272.     setupWelcome()
  273.     printer.centered('Thanks For Completing Setup', 6)
  274.     printer.centered('Please Wait While Your Preferences', 8)
  275.     printer.centered('Are Saved To File', 9)
  276.     printer.centered('Saving Settings', 19)
  277.     SaveSettings()
  278.     printer.centered('Settings Saved, Click To Continue', 19)
  279.     os.pullEvent('mouse_click')
  280.     loadSettings()
  281.     current.config = false
  282.   end
  283.  
  284. Intro()
  285. whitelistAdd()
  286. if ouputOption() == true then outputSide() end
  287. if alarmOption() == true then alarmOutput() end
  288. setupSave()
  289. term.clear()
  290. drawTitle()
  291. scanSensor()
  292. eventLoop()
  293. end
  294.  
  295. function loadSettings()
  296.   LogFile.i('Loading Settings', runningProgram)
  297.   if fs.exists('systemFiles/Programs/detectorConfig') then
  298.         local f = fs.open('systemFiles/Programs/detectorConfig', 'r')
  299.         if f then
  300.             current.settings = textutils.unserialize(f.readAll())
  301.         end
  302.         f.close()
  303.     else
  304.         setup()
  305.     end
  306. end
  307.  
  308. function SaveSettings()
  309.     LogFile.i('Saving Settings', runningProgram)
  310.     current.settings = current.settings or {}
  311.     local f = fs.open('systemFiles/Programs/detectorConfig', 'w')
  312.     if f then
  313.         f.write(textutils.serialize(current.settings))
  314.     end
  315.     f.close()  
  316. end
  317.  
  318. --Sensor Peripheral: openperipheral_sensor
  319.  
  320. function scanSensor()
  321.   LogFile.i('Searching For OpenP Scanner', runningProgram)
  322.   localPeripherals = peripheral.getNames()
  323.     for i = 1, #localPeripherals do
  324.       if peripheral.getType(localPeripherals[i]) == 'openperipheral_sensor' then
  325.         Sensor = peripheral.find('openperipheral_sensor')
  326.         LogFile.i('Sensor Found', runningProgram)
  327.         current.sensor.isPresent = true
  328.         return true
  329.       end
  330.     end
  331.     LogFile.w('Sensor Missing', runningProgram)
  332.     current.sensor.isPresent = false
  333.     noSensor()
  334. end
  335.  
  336. function noSensor()
  337.   LogFile.w('There Is No Sensor Present', runningProgram)
  338.   drawTitle()
  339.   if current.settings.redstoneSide then rs.setOutput(current.settings.redstoneSide, false) end
  340.   if current.settings.alarmRedstoneSide then rs.setOutput(current.settings.alarmRedstoneSide, false) end
  341.   printer.centered('It Seems There Is No OpenPeripheral Sensor', 6)
  342.   printer.centered('Attached To This Computer', 7)
  343.   printer.centered('Please Attach One', 8)
  344.   printer.centered('Well Look Automatically When You Attach One', 10)
  345.   printer.centered('Attach A Sensor To Continue', 19)
  346.   while true do
  347.     local event = os.pullEvent()
  348.       if event == 'peripheral' or event == 'peripheral_detach' then
  349.         printer.centered('Scanning For Sensor', 19)
  350.         sleep(2)
  351.         scanSensor()
  352.         drawTitle()
  353.         closeAllSides()
  354.         checkScanner()
  355.         break
  356.       end
  357.   end
  358. end
  359.  
  360. function checkScanner()
  361.     allowPlayer = false
  362.     denyPlayer = false
  363.     nearbyPlayers = Sensor.getPlayerNames()
  364.     if nearbyPlayers then
  365.       for _, entry in ipairs(nearbyPlayers) do
  366.         for _, v in ipairs (current.settings.whiteList) do
  367.           if entry == v then
  368.             openSide()
  369.             allowPlayer = true
  370.           else
  371.             if not allowPlayer then
  372.               openAlarmSide()
  373.               denyPlayer = true
  374.             end
  375.           end
  376.         end
  377.       end
  378.     end
  379.     scanPlayers = os.startTimer(0.5)
  380.     if denyPlayer or allowPlayer then return true else return false end
  381. end
  382.  
  383. function eventRegister(event, functionToRun)
  384.     if not Events[event] then
  385.         Events[event] = {}
  386.     end
  387.     table.insert(Events[event], functionToRun)
  388. end
  389.  
  390. configBtn = btnInit('Enter Config', nil, nil, 19, termY-1, 1, colors.cyan, 1, 256, function() os.queueEvent('config') end, false, nil, nil, nil, nil)
  391. backBtn = btnInit('Return', nil, nil, 22, termY-1, 1, colors.cyan, 1, 256, function() os.queueEvent('back') end, false, nil, nil, nil, nil)
  392. element.opacity(backBtn, false)
  393.  
  394. function drawScreen()
  395.   if current.status == 'System Ready' then term.setTextColor(colors.blue)
  396.   elseif current.status == 'Access Granted ' then term.setTextColor(colors.lime)
  397.   elseif current.status == 'Access Denied' then term.setTextColor(colors.red) end
  398.   printer.centered(current.status, 8)
  399.   term.setTextColor(1)  
  400.   term.setBackgroundColor(256)
  401.   element.opacity(configBtn, true)
  402.   printer.centered('System Monitoring Nearby Entities', 19)
  403.   printer.centered('Detector System', 6)
  404. end
  405.  
  406. function eventLoop()
  407. drawTitle()
  408. current.status = 'System Ready'        
  409. allowPlayer = false
  410. scanSensor()
  411. checkScanner()
  412.   while true do
  413.   drawScreen()
  414.     local event, arg1, arg2, arg3, arg4, arg5, arg6 = os.pullEventRaw()
  415.       if Events[event] then
  416.         for i, e in ipairs(Events[event]) do
  417.           LogFile.i("Event Called: "..tostring(event)..", "..tostring(arg1).." "..tostring(arg2).." "..tostring(arg3).." "..tostring(arg4).." "..tostring(arg5).." "..tostring(arg6), runningProgram)
  418.           e(event, arg1, arg2, arg3, arg4, arg5, arg6)
  419.         end
  420.       end
  421.    end
  422. end
  423.  
  424. function openSide()
  425.   current.status = 'Access Granted '
  426.   if current.settings.redstoneSide then rs.setOutput(current.settings.redstoneSide, true) end
  427. end
  428.  
  429. function openAlarmSide()
  430.   current.status = 'Access Denied'
  431.   if current.settings.alarmRedstoneSide then rs.setOutput(current.settings.alarmRedstoneSide, true) end
  432. end
  433.  
  434. function closeAllSides()
  435.   if not checkScanner() then
  436.     allowPlayer = false
  437.     denyPlayer = false
  438.     current.status = 'System Ready'
  439.     if current.settings.redstoneSide then rs.setOutput(current.settings.redstoneSide, false) end
  440.     if current.settings.alarmRedstoneSide then rs.setOutput(current.settings.alarmRedstoneSide, false) end
  441.   end
  442. end
  443.  
  444. function timerHandle(event, timer)
  445.   if timer == closeSide then
  446.     closeAllSides()
  447.   elseif timer == scanPlayers then
  448.     closeAllSides()
  449.     checkScanner()
  450.   end
  451. end
  452.  
  453. function whitelistAdd()
  454. LogFile.i('Whitelist Add Called', runningProgram)
  455.   while true do
  456.     local function addNames()
  457.     while true do  
  458.     titleBar.draw('HbombOS Security Suite', 'OpenP Detector Settings', colors.cyan, 256, 128, 256, 1)
  459.       inList = false
  460.       printer.centered('Enter The Names You Would Like To', 6)
  461.       printer.centered('The Detector WhiteList', 7)
  462.       printer.centered('These Are The Players Who Will Have Access', 9)
  463.       printer.centered('Enter The Username, And Hit Enter To Add', 10)
  464.       printer.centered('Then Press [alt] To Save And Return',19)
  465.       term.setCursorPos(termX/2-#'Username: ', 13)
  466.       write('Username: ')
  467.       local input = read()
  468.         if input and input ~= "" then
  469.           for _, entry in ipairs(current.settings.whiteList) do
  470.             if entry == input then
  471.               inList = true
  472.               printer.centered('Username Already In Whitelist', 19)
  473.               LogFile.w('Username: '..input..' Already In Whitelist', runningProgram)
  474.               sleep(1)
  475.             end
  476.           end
  477.           if inList == false then
  478.             printer.centered('Adding Username To whitelist', 19)
  479.             LogFile.i('Adding: '..input.. 'Into Whitelist', runningProgram)
  480.               if current.settings.whiteList then
  481.                 table.insert(current.settings.whiteList, input)
  482.                 printer.centered('Username Inserted Into Whitelist', 19)
  483.                 sleep(0.5)
  484.               else
  485.                 LogFile.i('Whitelist Table Missing!', runningProgram)
  486.                 error'Table Appears To Be Missing'
  487.               end
  488.           end
  489.         end
  490.     end
  491.     end
  492.    
  493.     local function nextStep()
  494.       while true do
  495.         local event, key = os.pullEvent('key')
  496.           if key == keys.leftAlt or key == keys.rightAlt then
  497.             if #current.settings.whiteList <= 0 then
  498.               term.setCursorBlink(false)
  499.               printer.centered('Please Register At Least ONE Username', 19)
  500.               LogFile.i('User Tried To Add 0 Usernames', runningProgram)
  501.               sleep(0.5)
  502.               lessThanOne = true
  503.               return
  504.             else
  505.               lessThanOne = false
  506.               return key
  507.             end
  508.           end
  509.       end
  510.     end
  511.    
  512.     parallel.waitForAny(nextStep, addNames)
  513.     if not lessThanOne then break end
  514.   end
  515.   SaveSettings()
  516.   configMenu()
  517. end
  518.  
  519. function whitelistRemove()
  520.  LogFile.i('Remove Whitelist Username Open', runningProgram)
  521.   local function removeNames()
  522.     while true do
  523.       inList = false
  524.       titleBar.draw('HbombOS Security Suite', 'OpenP Detector Lock Settings', colors.cyan, 256, 128, 256, 1)
  525.       printer.centered('Enter The Names You Would Like To', 6)
  526.       printer.centered('Remove From The Detector WhiteList', 7)
  527.       printer.centered('These Are The Players Who Will Not Have Access', 9)
  528.       printer.centered('Enter The Username, And Hit Enter To Remove', 10)
  529.       printer.centered('Then Press [alt] To Save And Return',19)
  530.       term.setCursorPos(termX/2-#'Username: ', 13)
  531.       write('Username: ')
  532.       local input = read()
  533.         if input and input ~= "" then
  534.           for _, entry in ipairs(current.settings.whiteList) do
  535.             if entry == input then
  536.               if #current.settings.whiteList - 1 >0 then
  537.                 printer.centered('Removing Username From whitelist', 19)
  538.                 table.remove(current.settings.whiteList, _)
  539.                 SaveSettings()
  540.                 printer.centered('Removed Username From Whitelist', 19)
  541.                 sleep(0.5)
  542.                 break
  543.               else
  544.                 LogFile.i('User Tried To Remove LAST Username From Whitelist', runningProgram)
  545.                 printer.centered('There Has To Be Atleast One Username', 18)
  546.                 printer.centered('In Whitelist', 19)
  547.                 sleep(1)
  548.               end
  549.             end
  550.           end
  551.         end
  552.     end
  553.   end
  554.    
  555.     local function completeStage()
  556.       while true do
  557.         local event, key = os.pullEvent('key')
  558.           if key == keys.leftAlt or key == keys.rightAlt then
  559.             return key
  560.           end
  561.       end
  562.     end
  563.    
  564.     parallel.waitForAny(completeStage, removeNames)
  565.     SaveSettings()
  566.     configMenu()
  567. end
  568.  
  569. function configMenu()
  570.   LogFile.i('Config Menu Open', runningProgram)
  571.   term.setCursorBlink(false)
  572.   titleBar.draw('HbombOS Security Suite', 'OpenP Detector Settings', colors.cyan, 256, 128, 256, 1)
  573.   printer.centered('Pick An Option Below', 6)
  574.   local continue = false
  575.   current.config = true
  576.   local options = {
  577.         {'Edit Settings', function()
  578.           setup()
  579.         end},
  580.         {'Whitelist Users', function()
  581.           whitelistAdd()
  582.         end},
  583.         {'Un-Whitelist Users', function()
  584.           whitelistRemove()
  585.         end},
  586.         {'Reset Configuration', function()
  587.           LogFile.i('Config Reset', runningProgram)
  588.           fs.delete('systemFiles/Programs/detectorConfig')
  589.           os.reboot()
  590.         end},
  591.         {'Cancel And Return', function()
  592.           current.config = false
  593.           eventLoop()
  594.         end}
  595.     }
  596.     local selected = 1
  597.     local function draw()
  598.         for i, v in ipairs(options) do
  599.             local bg = 256
  600.             local tc = colours.white
  601.             if i == selected then
  602.                 bg = 256
  603.                 tc = colours.blue
  604.             end
  605.             term.setTextColor(tc)
  606.             term.setBackgroundColor(bg)
  607.             PrintCentered(' '..v[1]..' ', math.floor(#options/2) + i + 6)
  608.         end
  609.     end
  610.     local wait = true
  611.     draw()
  612.     while true do
  613.         local event, key = os.pullEvent('key')
  614.         if key == keys.up then
  615.             selected = selected - 1
  616.             if selected < 1 then
  617.                 selected = 1
  618.             end
  619.         elseif key == keys.down then
  620.             selected = selected + 1
  621.             if selected > #options then
  622.                 selected = #options
  623.             end
  624.         elseif key == keys.enter then
  625.           options[selected][2]()
  626.           wait = false
  627.           break
  628.         end
  629.         draw()
  630.     end
  631. end
  632.  
  633. function ConfigLogin()
  634.   LogFile.i('Config Login', runningProgram)
  635.   closeAllSides()
  636.   current.config = true
  637.   local f = fs.open('systemFiles/Security/adminPass', 'r')
  638.   adminPassLoaded = f.readLine()
  639.   f.close()
  640.   titleBar.draw('HbombOS Security Suite', 'OpenP Detector Settings', colors.cyan, 256, 128, 256, 1)
  641.   printer.centered('Please Enter Your Admin Password', 4)
  642.   printer.centered('Linked To Your Security Suite To Access', 5)
  643.   printer.centered('The Settings Menu', 6)
  644.   element.opacity(configBtn, false)
  645.   element.opacity(backBtn, true)
  646.   backBtn.func = function() current.config = false os.queueEvent('back') end
  647.  
  648.   local function LoginInput()
  649.     while true do
  650.       term.setCursorPos(termX/2-#'Password: ', 10)
  651.       term.clearLine()
  652.       write('Password: ')
  653.       local input = read('*')
  654.         if input == adminPassLoaded then
  655.           LogFile.i('Admin Password Correct', runningProgram)
  656.           current.config = true
  657.           break
  658.         end
  659.     end
  660.   end
  661.  
  662.   local function eventListen()
  663.     while true do
  664.         local event, p1, p2, p3 = os.pullEvent()
  665.         if event == 'mouse_click' then doClick(event, p1, p2, p3)
  666.         elseif event == "back" then
  667.             term.setCursorBlink(false)
  668.             current.config = false
  669.             break
  670.         end
  671.     end
  672.     return
  673.   end
  674.     parallel.waitForAny(LoginInput, eventListen)
  675.     if current.config then configMenu() else eventLoop() end
  676.   end
  677.  
  678.  
  679.  
  680.  
  681.  
  682. function keyHandler(event, key)
  683.   if event == 'key' and key == keys.leftAlt or key == keys.rightAlt then
  684.     if current.config == false then ConfigLogin() else return end
  685.   end
  686. end
  687.  
  688. function init()
  689.     LogFile.i('main Init function started', runningProgram)
  690.     eventRegister('mouse_click', checkClick)
  691.     eventRegister('timer', timerHandle)
  692.     --eventRegister('key', keyHandler)
  693.     eventRegister('terminate', function(event) errora.err('Termination Detected', 'You Attempted To Terminate The Program', true, false) end)
  694.     eventRegister('peripheral', scanSensor)
  695.     eventRegister('peripheral_detach', scanSensor)
  696.     eventRegister('config', function() if current.config == false then ConfigLogin() end end)
  697.     loadSettings()
  698.     eventLoop()
  699. end
  700.  
  701.   local _, err = pcall(init) --Error Catching, All Functions Are Called From initialise() at some point...
  702.   if err then
  703.     term.setCursorBlink(false)
  704.     LogFile.e('Error Detected: '..err, runningProgram)
  705.     errora.err(err, 'Detector Lock Crashed, Report Error Above', true, true) --BSoD for protection, the 'true' is an auto-reboot option to prevent the door waiting for a prompt
  706.   end
Add Comment
Please, Sign In to add comment