Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- runningProgram = shell.getRunningProgram()
- LogFile.i('OpenPDetect Program Successfully Started', runningProgram)
- current = {
- sensor = {},
- settings = {
- whiteList = {}
- }
- }
- Events ={}
- elements = {}
- current.config = false
- termX, termY = term.getSize()
- function doClick(event, btn, x, y)
- functionToRun = element.tryClick(elements, x, y)
- if functionToRun then --Check click location
- functionToRun()
- end
- end
- function checkClick(event, btn, x, y)
- if not isSettingUp then
- doClick(event, btn, x, y)
- end
- end
- function btnInit(btnText, btnWidth, btnHeight, btnX, btnY, btnTC, btnBG, oTC, oBG, onClick, toggle, secBG, secTC, secText) --Function to create button
- local btn = element.create(btnText, btnWidth, btnHeight, btnX, btnY, btnTC, btnBG, oTC, oBG, onClick, toggle, secBG, secTC, secText) --Calls API to generate button
- table.insert(elements, btn) --Inserts into table so it can be scanned later
- element.opacity(btn, true) --Sets visibility to true
- return btn
- end
- function drawTitle()
- term.clear()
- titleBar.draw('HbombOS Security Suite', 'OpenP Detector Lock', colors.cyan, 256, 128, 256, 1)
- end
- function setup()
- LogFile.i('OpenPDetect Setup Started', runningProgram)
- local function setupWelcome()
- titleBar.draw('HbombOS Security Suite', 'OpenP Detector Lock Setup', colors.cyan, 256, 128, 256, 1)
- end
- local function Intro()
- LogFile.i('OpenPDetect Setup Intro', runningProgram)
- setupWelcome()
- printer.centered('Welcome To OpenP Detector Setup', 6)
- printer.centered('This Program Uses Open Peripherals', 8)
- printer.centered('Sensor Block', 9)
- printer.centered('Therefore To Use This Program You Need', 11)
- printer.centered('Open Peripheral Addons Installed', 12)
- printer.centered('Click Start To Setup', 19)
- 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)
- while true do
- local event, btn, x, y = os.pullEvent()
- if event == "mouse_click" then
- doClick(event, btn, x, y)
- elseif event == "start" then
- element.opacity(start, false)
- break
- end
- end
- return
- end
- local function whitelistAdd()
- LogFile.i('OpenPDetect whitelist Add Start', runningProgram)
- 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)
- while true do
- local function addNames()
- while true do
- inList = false
- setupWelcome()
- element.opacity(continue, true)
- printer.centered('Enter The Names You Would Like To', 6)
- printer.centered('Add To The Detector WhiteList', 7)
- printer.centered('These Are The Players Who Will Have Access', 9)
- printer.centered('Enter The Username, And Hit Enter To Add', 10)
- term.setCursorPos(termX/2-#'Username: ', 13)
- write('Username: ')
- local input = read()
- if input and input ~= "" then
- for _, entry in ipairs(current.settings.whiteList) do
- if entry == input then
- inList = true
- printer.centered('Username Already In Whitelist', 19)
- LogFile.w('User Tried To Add Username To Whitelist That Already Existed', runningProgram)
- sleep(1)
- end
- end
- if inList == false then
- printer.centered('Adding Username To whitelist', 19)
- LogFile.i('Adding Username: '..input..' Into Whitelist', runningProgram)
- if current.settings.whiteList then
- table.insert(current.settings.whiteList, input)
- printer.centered('Username Inserted Into Whitelist', 19)
- sleep(0.5)
- else
- LogFile.e('Whitelist Table Is Missing.. Fatal', runningProgram)
- error'Table Appears To Be Missing'
- end
- end
- end
- end
- end
- local function buttonPress()
- while true do
- local event, btn, x, y = os.pullEvent('mouse_click')
- doClick(event, btn, x, y)
- end
- end
- local function nextStep()
- while true do
- local event = os.pullEvent('Continue')
- if #current.settings.whiteList <= 0 then
- term.setCursorBlink(false)
- printer.centered('Please Register At Least ONE Username', 19)
- LogFile.w('User Tried To Skip Whitelist When 0 Usernames Are In Table', runningProgram)
- sleep(0.5)
- lessThanOne = true
- return
- else
- lessThanOne = false
- return key
- end
- end
- end
- parallel.waitForAny(nextStep, addNames, buttonPress)
- if not lessThanOne then LogFile.i('Whitelist Complete', runningProgram) break end
- end
- return
- end
- local function ouputOption()
- element.opacity(continue, false)
- 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)
- 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)
- term.setCursorBlink(false)
- setupWelcome()
- element.opacity(yes, true)
- element.opacity(no, true)
- printer.centered('Do You Want The Computer To Ouput', 6)
- printer.centered('A Redstone Signal When The Players Is', 7)
- printer.centered('On The Whitelist', 8)
- printer.centered('You Pick What Side It Outputs From Next Step', 19)
- while true do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "mouse_click" then
- doClick(event, p1, p2, p3)
- elseif event == 'Yes' then
- return true
- elseif event == "No" then
- return false
- end
- end
- end
- local function outputSide()
- for _, elem in ipairs(elements) do
- elem.toggleState = 1
- element.opacity(elem, false)
- end
- setupWelcome()
- right = btnInit('Right', nil, nil, 5, 10, 1, colors.cyan, 1, 256, function()current.settings.redstoneSide = 'right' end, true, colors.green, 1, 'Right', 'rsOut')
- right.toggleState = 2
- element.opacity(right, true)
- current.settings.redstoneSide = 'right'
- 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')
- 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')
- 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')
- 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')
- element.opacity(continue, true)
- while true do
- printer.centered("Redstone Output Side", 6)
- printer.centered("Click A Button To Decide Your", termY-1)
- printer.centered("Redstone Output Side", termY)
- element.opacity(continue, true)
- local event, btn, x, y = os.pullEvent()
- if event == "mouse_click" then
- doClick(event, btn, x, y)
- elseif event == "Continue" then
- break
- end
- end
- for _, elem in ipairs(elements) do
- if elem.parent == "rsOut" then
- element.opacity(elem, false)
- end
- end
- element.opacity(continue ,false)
- return
- end
- local function alarmOption()
- element.opacity(continue, false)
- yes.func = function() current.settings.alarmOption = true os.queueEvent('Yes') end
- no.func = function() current.settings.alarmOption = false os.queueEvent('No') end
- term.setCursorBlink(false)
- setupWelcome()
- element.opacity(yes, true)
- element.opacity(no, true)
- printer.centered('Do You Want The Computer To Ouput', 6)
- printer.centered('A Redstone Signal When The Players Is', 7)
- printer.centered('NOT On The Whitelist', 8)
- printer.centered('You Pick What Side It Outputs From Next Step', 19)
- while true do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "mouse_click" then
- doClick(event, p1, p2, p3)
- elseif event == 'Yes' then
- return true
- elseif event == "No" then
- return false
- end
- end
- end
- local function alarmOutput()
- for _, elem in ipairs(elements) do
- elem.toggleState = 1
- element.opacity(elem, false)
- end
- setupWelcome()
- continue.func = function() os.queueEvent('Continue') end
- printer.centered("Redstone Alarm Output Side", 6)
- printer.centered("Click A Button To Decide Your", termY-1)
- printer.centered("Output Side", termY)
- element.opacity(continue, true)
- current.settings.alarmRedstoneSide = 'right'
- right.func = function() current.settings.alarmRedstoneSide = 'right' end
- right.parent = 'rsAlarm'
- right.toggleState = 2
- left.func = function() current.settings.alarmRedstoneSide = 'left' end
- left.parent = 'rsAlarm'
- top.func = function() current.settings.alarmRedstoneSide = 'top' end
- top.parent = 'rsAlarm'
- bottom.func = function() current.settings.alarmRedstoneSide = 'bottom' end
- bottom.parent = 'rsAlarm'
- back.func = function() current.settings.alarmRedstoneSide = 'back' end
- back.parent = 'rsAlarm'
- for _, elem in ipairs(elements) do
- if elem.parent == "rsAlarm" then
- element.opacity(elem, true)
- end
- end
- while true do
- element.opacity(nextBtn, true)
- local event, btn, x, y = os.pullEvent()
- if event == "mouse_click" then
- doClick(event, btn, x, y)
- elseif event == "Continue" then
- break
- end
- end
- for _, elem in ipairs(elements) do
- if elem.parent == "rsAlarm" then
- element.opacity(elem, false)
- end
- end
- element.opacity(nextBtn ,false)
- end
- local function setupSave()
- LogFile.i('Setup Complete', runningProgram)
- setupWelcome()
- printer.centered('Thanks For Completing Setup', 6)
- printer.centered('Please Wait While Your Preferences', 8)
- printer.centered('Are Saved To File', 9)
- printer.centered('Saving Settings', 19)
- SaveSettings()
- printer.centered('Settings Saved, Click To Continue', 19)
- os.pullEvent('mouse_click')
- loadSettings()
- current.config = false
- end
- Intro()
- whitelistAdd()
- if ouputOption() == true then outputSide() end
- if alarmOption() == true then alarmOutput() end
- setupSave()
- term.clear()
- drawTitle()
- scanSensor()
- eventLoop()
- end
- function loadSettings()
- LogFile.i('Loading Settings', runningProgram)
- if fs.exists('systemFiles/Programs/detectorConfig') then
- local f = fs.open('systemFiles/Programs/detectorConfig', 'r')
- if f then
- current.settings = textutils.unserialize(f.readAll())
- end
- f.close()
- else
- setup()
- end
- end
- function SaveSettings()
- LogFile.i('Saving Settings', runningProgram)
- current.settings = current.settings or {}
- local f = fs.open('systemFiles/Programs/detectorConfig', 'w')
- if f then
- f.write(textutils.serialize(current.settings))
- end
- f.close()
- end
- --Sensor Peripheral: openperipheral_sensor
- function scanSensor()
- LogFile.i('Searching For OpenP Scanner', runningProgram)
- localPeripherals = peripheral.getNames()
- for i = 1, #localPeripherals do
- if peripheral.getType(localPeripherals[i]) == 'openperipheral_sensor' then
- Sensor = peripheral.find('openperipheral_sensor')
- LogFile.i('Sensor Found', runningProgram)
- current.sensor.isPresent = true
- return true
- end
- end
- LogFile.w('Sensor Missing', runningProgram)
- current.sensor.isPresent = false
- noSensor()
- end
- function noSensor()
- LogFile.w('There Is No Sensor Present', runningProgram)
- drawTitle()
- if current.settings.redstoneSide then rs.setOutput(current.settings.redstoneSide, false) end
- if current.settings.alarmRedstoneSide then rs.setOutput(current.settings.alarmRedstoneSide, false) end
- printer.centered('It Seems There Is No OpenPeripheral Sensor', 6)
- printer.centered('Attached To This Computer', 7)
- printer.centered('Please Attach One', 8)
- printer.centered('Well Look Automatically When You Attach One', 10)
- printer.centered('Attach A Sensor To Continue', 19)
- while true do
- local event = os.pullEvent()
- if event == 'peripheral' or event == 'peripheral_detach' then
- printer.centered('Scanning For Sensor', 19)
- sleep(2)
- scanSensor()
- drawTitle()
- closeAllSides()
- checkScanner()
- break
- end
- end
- end
- function checkScanner()
- allowPlayer = false
- denyPlayer = false
- nearbyPlayers = Sensor.getPlayerNames()
- if nearbyPlayers then
- for _, entry in ipairs(nearbyPlayers) do
- for _, v in ipairs (current.settings.whiteList) do
- if entry == v then
- openSide()
- allowPlayer = true
- else
- if not allowPlayer then
- openAlarmSide()
- denyPlayer = true
- end
- end
- end
- end
- end
- scanPlayers = os.startTimer(0.5)
- if denyPlayer or allowPlayer then return true else return false end
- end
- function eventRegister(event, functionToRun)
- if not Events[event] then
- Events[event] = {}
- end
- table.insert(Events[event], functionToRun)
- end
- configBtn = btnInit('Enter Config', nil, nil, 19, termY-1, 1, colors.cyan, 1, 256, function() os.queueEvent('config') end, false, nil, nil, nil, nil)
- backBtn = btnInit('Return', nil, nil, 22, termY-1, 1, colors.cyan, 1, 256, function() os.queueEvent('back') end, false, nil, nil, nil, nil)
- element.opacity(backBtn, false)
- function drawScreen()
- if current.status == 'System Ready' then term.setTextColor(colors.blue)
- elseif current.status == 'Access Granted ' then term.setTextColor(colors.lime)
- elseif current.status == 'Access Denied' then term.setTextColor(colors.red) end
- printer.centered(current.status, 8)
- term.setTextColor(1)
- term.setBackgroundColor(256)
- element.opacity(configBtn, true)
- printer.centered('System Monitoring Nearby Entities', 19)
- printer.centered('Detector System', 6)
- end
- function eventLoop()
- drawTitle()
- current.status = 'System Ready'
- allowPlayer = false
- scanSensor()
- checkScanner()
- while true do
- drawScreen()
- local event, arg1, arg2, arg3, arg4, arg5, arg6 = os.pullEventRaw()
- if Events[event] then
- for i, e in ipairs(Events[event]) do
- LogFile.i("Event Called: "..tostring(event)..", "..tostring(arg1).." "..tostring(arg2).." "..tostring(arg3).." "..tostring(arg4).." "..tostring(arg5).." "..tostring(arg6), runningProgram)
- e(event, arg1, arg2, arg3, arg4, arg5, arg6)
- end
- end
- end
- end
- function openSide()
- current.status = 'Access Granted '
- if current.settings.redstoneSide then rs.setOutput(current.settings.redstoneSide, true) end
- end
- function openAlarmSide()
- current.status = 'Access Denied'
- if current.settings.alarmRedstoneSide then rs.setOutput(current.settings.alarmRedstoneSide, true) end
- end
- function closeAllSides()
- if not checkScanner() then
- allowPlayer = false
- denyPlayer = false
- current.status = 'System Ready'
- if current.settings.redstoneSide then rs.setOutput(current.settings.redstoneSide, false) end
- if current.settings.alarmRedstoneSide then rs.setOutput(current.settings.alarmRedstoneSide, false) end
- end
- end
- function timerHandle(event, timer)
- if timer == closeSide then
- closeAllSides()
- elseif timer == scanPlayers then
- closeAllSides()
- checkScanner()
- end
- end
- function whitelistAdd()
- LogFile.i('Whitelist Add Called', runningProgram)
- while true do
- local function addNames()
- while true do
- titleBar.draw('HbombOS Security Suite', 'OpenP Detector Settings', colors.cyan, 256, 128, 256, 1)
- inList = false
- printer.centered('Enter The Names You Would Like To', 6)
- printer.centered('The Detector WhiteList', 7)
- printer.centered('These Are The Players Who Will Have Access', 9)
- printer.centered('Enter The Username, And Hit Enter To Add', 10)
- printer.centered('Then Press [alt] To Save And Return',19)
- term.setCursorPos(termX/2-#'Username: ', 13)
- write('Username: ')
- local input = read()
- if input and input ~= "" then
- for _, entry in ipairs(current.settings.whiteList) do
- if entry == input then
- inList = true
- printer.centered('Username Already In Whitelist', 19)
- LogFile.w('Username: '..input..' Already In Whitelist', runningProgram)
- sleep(1)
- end
- end
- if inList == false then
- printer.centered('Adding Username To whitelist', 19)
- LogFile.i('Adding: '..input.. 'Into Whitelist', runningProgram)
- if current.settings.whiteList then
- table.insert(current.settings.whiteList, input)
- printer.centered('Username Inserted Into Whitelist', 19)
- sleep(0.5)
- else
- LogFile.i('Whitelist Table Missing!', runningProgram)
- error'Table Appears To Be Missing'
- end
- end
- end
- end
- end
- local function nextStep()
- while true do
- local event, key = os.pullEvent('key')
- if key == keys.leftAlt or key == keys.rightAlt then
- if #current.settings.whiteList <= 0 then
- term.setCursorBlink(false)
- printer.centered('Please Register At Least ONE Username', 19)
- LogFile.i('User Tried To Add 0 Usernames', runningProgram)
- sleep(0.5)
- lessThanOne = true
- return
- else
- lessThanOne = false
- return key
- end
- end
- end
- end
- parallel.waitForAny(nextStep, addNames)
- if not lessThanOne then break end
- end
- SaveSettings()
- configMenu()
- end
- function whitelistRemove()
- LogFile.i('Remove Whitelist Username Open', runningProgram)
- local function removeNames()
- while true do
- inList = false
- titleBar.draw('HbombOS Security Suite', 'OpenP Detector Lock Settings', colors.cyan, 256, 128, 256, 1)
- printer.centered('Enter The Names You Would Like To', 6)
- printer.centered('Remove From The Detector WhiteList', 7)
- printer.centered('These Are The Players Who Will Not Have Access', 9)
- printer.centered('Enter The Username, And Hit Enter To Remove', 10)
- printer.centered('Then Press [alt] To Save And Return',19)
- term.setCursorPos(termX/2-#'Username: ', 13)
- write('Username: ')
- local input = read()
- if input and input ~= "" then
- for _, entry in ipairs(current.settings.whiteList) do
- if entry == input then
- if #current.settings.whiteList - 1 >0 then
- printer.centered('Removing Username From whitelist', 19)
- table.remove(current.settings.whiteList, _)
- SaveSettings()
- printer.centered('Removed Username From Whitelist', 19)
- sleep(0.5)
- break
- else
- LogFile.i('User Tried To Remove LAST Username From Whitelist', runningProgram)
- printer.centered('There Has To Be Atleast One Username', 18)
- printer.centered('In Whitelist', 19)
- sleep(1)
- end
- end
- end
- end
- end
- end
- local function completeStage()
- while true do
- local event, key = os.pullEvent('key')
- if key == keys.leftAlt or key == keys.rightAlt then
- return key
- end
- end
- end
- parallel.waitForAny(completeStage, removeNames)
- SaveSettings()
- configMenu()
- end
- function configMenu()
- LogFile.i('Config Menu Open', runningProgram)
- term.setCursorBlink(false)
- titleBar.draw('HbombOS Security Suite', 'OpenP Detector Settings', colors.cyan, 256, 128, 256, 1)
- printer.centered('Pick An Option Below', 6)
- local continue = false
- current.config = true
- local options = {
- {'Edit Settings', function()
- setup()
- end},
- {'Whitelist Users', function()
- whitelistAdd()
- end},
- {'Un-Whitelist Users', function()
- whitelistRemove()
- end},
- {'Reset Configuration', function()
- LogFile.i('Config Reset', runningProgram)
- fs.delete('systemFiles/Programs/detectorConfig')
- os.reboot()
- end},
- {'Cancel And Return', function()
- current.config = false
- eventLoop()
- end}
- }
- local selected = 1
- local function draw()
- for i, v in ipairs(options) do
- local bg = 256
- local tc = colours.white
- if i == selected then
- bg = 256
- tc = colours.blue
- end
- term.setTextColor(tc)
- term.setBackgroundColor(bg)
- PrintCentered(' '..v[1]..' ', math.floor(#options/2) + i + 6)
- end
- end
- local wait = true
- draw()
- while true do
- local event, key = os.pullEvent('key')
- if key == keys.up then
- selected = selected - 1
- if selected < 1 then
- selected = 1
- end
- elseif key == keys.down then
- selected = selected + 1
- if selected > #options then
- selected = #options
- end
- elseif key == keys.enter then
- options[selected][2]()
- wait = false
- break
- end
- draw()
- end
- end
- function ConfigLogin()
- LogFile.i('Config Login', runningProgram)
- closeAllSides()
- current.config = true
- local f = fs.open('systemFiles/Security/adminPass', 'r')
- adminPassLoaded = f.readLine()
- f.close()
- titleBar.draw('HbombOS Security Suite', 'OpenP Detector Settings', colors.cyan, 256, 128, 256, 1)
- printer.centered('Please Enter Your Admin Password', 4)
- printer.centered('Linked To Your Security Suite To Access', 5)
- printer.centered('The Settings Menu', 6)
- element.opacity(configBtn, false)
- element.opacity(backBtn, true)
- backBtn.func = function() current.config = false os.queueEvent('back') end
- local function LoginInput()
- while true do
- term.setCursorPos(termX/2-#'Password: ', 10)
- term.clearLine()
- write('Password: ')
- local input = read('*')
- if input == adminPassLoaded then
- LogFile.i('Admin Password Correct', runningProgram)
- current.config = true
- break
- end
- end
- end
- local function eventListen()
- while true do
- local event, p1, p2, p3 = os.pullEvent()
- if event == 'mouse_click' then doClick(event, p1, p2, p3)
- elseif event == "back" then
- term.setCursorBlink(false)
- current.config = false
- break
- end
- end
- return
- end
- parallel.waitForAny(LoginInput, eventListen)
- if current.config then configMenu() else eventLoop() end
- end
- function keyHandler(event, key)
- if event == 'key' and key == keys.leftAlt or key == keys.rightAlt then
- if current.config == false then ConfigLogin() else return end
- end
- end
- function init()
- LogFile.i('main Init function started', runningProgram)
- eventRegister('mouse_click', checkClick)
- eventRegister('timer', timerHandle)
- --eventRegister('key', keyHandler)
- eventRegister('terminate', function(event) errora.err('Termination Detected', 'You Attempted To Terminate The Program', true, false) end)
- eventRegister('peripheral', scanSensor)
- eventRegister('peripheral_detach', scanSensor)
- eventRegister('config', function() if current.config == false then ConfigLogin() end end)
- loadSettings()
- eventLoop()
- end
- local _, err = pcall(init) --Error Catching, All Functions Are Called From initialise() at some point...
- if err then
- term.setCursorBlink(false)
- LogFile.e('Error Detected: '..err, runningProgram)
- 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
- end
Add Comment
Please, Sign In to add comment