Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Lockette - Computer access limiter. v1.1
- --# Made By Wojbie
- --# http://pastebin.com/cGM4iF1d
- -- Copyright (c) 2015-2021 Wojbie (wojbie@wojbie.net)
- -- Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
- -- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- -- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- -- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
- -- 4. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
- -- 5. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
- -- NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. YOU ACKNOWLEDGE THAT THIS SOFTWARE IS NOT DESIGNED, LICENSED OR INTENDED FOR USE IN THE DESIGN, CONSTRUCTION, OPERATION OR MAINTENANCE OF ANY NUCLEAR FACILITY.
- --Basic settings
- local pass = "unlock" --Password used to unlock.
- local passtime = 30 --Time of inactivity that causes system to autolock.
- local sProgram = "/rom/programs/shell" --Path to program to run
- local tArguments = {} --Table of arguments for Program
- local ShutdownOnExit = true --If set to true computer will turn off after the program exits.
- local lockscreen = true --Display lockscreen. If set to false computer will not display System Locked screen but still lockdown inputs.
- local scrap = false --Changes lockscreen from basic one to messed up one. Basicly fills screen with random letters and cllors.
- --Advanced settings
- local lockkey = keys.scollLock --Key for manual locking. Set to scroolLock
- local message="System Locked" --Message on lockscreen
- local bor={"+","-","|"} --Border of Messege window
- local Events={char=true,key=true,key_up=true,paste=true,monitor_touch=false,mouse_click=true,mouse_up=true,mouse_scroll=true,mouse_drag=true,terminate=true} --Events that are not allowed to pass in locked mode. To stop monitor clicks event change false to true next to monitor_touch.
- local ActionEvents={char=true,key=true,key_up=true,paste=true,mouse_click=true,mouse_up=true,mouse_scroll=true,mouse_drag=true} --Events that cause inactivity timer to restart. Only change if you know what you are doing.
- --Program setup.
- local parentTerm = term.current()
- local shutdown = os.shutdown
- --Initializing coorutine
- local co = coroutine.create( function()
- local run=os.run
- local tEnv={[ "shell" ] = shell,[ "multishell" ] = multishell,}
- run(tEnv, sProgram, unpack( tArguments ) )
- end )
- local function resume( ... )
- local ok, param = coroutine.resume( co, ... )
- if not ok then
- printError( param )
- end
- return param
- end
- --terminal stuff
- local passon=true
- local passer=setmetatable({},{["__index"]=function(t,k) if parentTerm[k] then if passon then return parentTerm[k] else return function() end end else return nil end end,["__newindex"]=function(t,k,v) end,})
- local saveWindow
- local lockWindow
- do
- local w,h = parentTerm.getSize()
- saveWindow=window.create(parentTerm,1,1,w,h,false)
- lockWindow=window.create(parentTerm,1,1,w,h,false)
- end
- local saveWindowsetVisible = saveWindow.setVisible
- local saveWindowreposition = saveWindow.reposition
- saveWindow.setVisible = function() end
- saveWindow.reposition = function() end
- --saveWindow.redraw = function() end
- --saveWindow.restoreCursor = function() end
- --saveWindow.getPosition = function() end
- local function createMultitable(...)
- local tab = {...}
- if #tab==1 and tab[1] and type(tab[1])=="table" then tab = tab[1] end
- if #tab==0 then error("Expected table of tables or any tables to table. I know it makes no sense.", 2) end
- local manymeta={ --Anytime index is requested fist table is used as refference.
- ["__index"]=function (parent , key)
- if tab and tab[1] and tab[1][key] then --If it has value it tested then
- if type(tab[1][key]) =="function" then --If its function then a function that calls all tables in row is made
- return function(...)
- local ret={}
- local tArgs={...}
- for i,k in ipairs(tab) do
- if k[key] then
- if #ret==0 then ret={k[key](table.unpack(tArgs))} --ret contains returns from first table that returned anything.
- else k[key](table.unpack(tArgs)) end
- end
- end
- return table.unpack(ret)
- end
- else
- return tab[1][key] --If its not a function then its just given out.
- end
- else
- return nil --Of it not exist in first table give nothing
- end
- end,
- ["__newindex"]=function (parent, key, value) --If someone wants to add anything to the table
- --do nothing.
- end,
- ["__call"]=function (parent, key) --If someone calls table like function give him direct acces to table list.
- --if key then tab = key end changing of content disalowed in static mode
- return tab
- end,
- ["__len"]=function (parent, key) --Not sure if it works but this is giving the leanght of first table or 0 if there is no first table.
- return (tab[1] and #tab[1]) or 0
- end,
- ["__metatable"]=false,--No touching the metatable.
- --["__type"]="WojbieManyMeta",--Custom type? Not in current version and not sure if wise. Commented out for now.
- }
- local out = {}
- for key,fun in pairs(tab[1]) do --create static table of multitable functions using first one as template
- out[key] = function(...)
- local ret={}
- local tArgs={...}
- for i,k in ipairs(tab) do
- if k[key] then
- if #ret==0 then ret={k[key](table.unpack(tArgs))} --ret contains returns from first table that returned anything.
- else k[key](table.unpack(tArgs)) end
- end
- end
- return table.unpack(ret)
- end
- end
- return setmetatable(out,manymeta) --create acctual manymeta table and return it
- end
- local multiplex=createMultitable(saveWindow,passer)
- local function drawLock()
- local w,h = lockWindow.getSize()
- local messagesize=message:len()
- if scrap then --fill the screan with (s)crap.
- if parentTerm.isColor() then
- for i = 1 , h do
- lockWindow.setCursorPos(1,i)
- for j = 1,w do
- lockWindow.setTextColor(2^math.random(0,15))
- lockWindow.setBackgroundColor(2^math.random(0,15))
- lockWindow.write(string.char(math.random(32,126)))
- end
- end
- else
- local col = {colors.black , colors.white , colors.gray , colors.lightGray}
- for i = 1 , h do
- lockWindow.setCursorPos(1,i)
- for j = 1,w do
- lockWindow.setTextColor(col[math.random(#col)])
- lockWindow.setBackgroundColor(col[math.random(#col)])
- lockWindow.write(string.char(math.random(32,126)))
- end
- end
- end
- elseif h < 5 or w < messagesize+4 then --write in middle
- lockWindow.setBackgroundColor(colors.black)
- lockWindow.clear()
- lockWindow.setTextColor(colors.white)
- lockWindow.setBackgroundColor(colors.black)
- lockWindow.setCursorPos(math.floor(w / 2 - messagesize / 2 )+1, math.floor(h / 2 )+1)
- lockWindow.write(message)
- else --write window in middle
- local offsetLeft = math.floor(w / 2 - (messagesize+4) / 2)+1
- local offsetTop = math.floor(h / 2 - 5/2)+1
- local filler = string.rep (" ", messagesize+2)
- local line = string.rep (bor[2], messagesize+2)
- lockWindow.setBackgroundColor(colors.black)
- lockWindow.clear()
- lockWindow.setTextColor(colors.white)
- lockWindow.setBackgroundColor(colors.black)
- lockWindow.setCursorPos(offsetLeft, offsetTop)
- lockWindow.write(bor[1]..line..bor[1])
- lockWindow.setCursorPos(offsetLeft, offsetTop+1)
- lockWindow.write(bor[3]..filler..bor[3])
- lockWindow.setCursorPos(offsetLeft, offsetTop+2)
- lockWindow.write(bor[3].." "..message.." "..bor[3])
- lockWindow.setCursorPos(offsetLeft, offsetTop+3)
- lockWindow.write(bor[3]..filler..bor[3])
- lockWindow.setCursorPos(offsetLeft, offsetTop+4)
- lockWindow.write(bor[1]..line..bor[1])
- end
- end
- --state stuff
- local curr
- local locked
- local lockdown
- local function lock()
- locked=true
- curr=""
- if lockdown then os.cancelTimer(lockdown) end
- lockdown=nil
- if lockscreen then
- passon=false
- drawLock()
- lockWindow.setVisible(true)
- end
- end
- local function unlock()
- locked=false
- curr=""
- if lockdown then os.cancelTimer(lockdown) end
- lockdown=os.startTimer(passtime)
- if lockscreen then
- lockWindow.setVisible(false)
- saveWindowsetVisible(true) --this restores contents of terminal before passon lets program write on it again.
- saveWindowsetVisible(false)
- passon=true
- end
- end
- term.redirect(multiplex)
- term.setCursorPos(1,1)
- term.clear()
- local ok, param = pcall( function()
- lock()
- local sFilter = resume()
- while coroutine.status( co ) ~= "dead" do
- local tEvent = { os.pullEventRaw() }
- if tEvent[1]=="term_resize" then
- -- Resize event
- local w,h = parentTerm.getSize()
- saveWindowreposition(1,1,w,h)
- lockWindow.setVisible(false)
- lockWindow.reposition(1,1,w,h)
- drawLock()
- lockWindow.setVisible(lockscreen and locked)
- end
- if locked then
- --See if chars or paste fit password
- if tEvent[1]=="char" then
- curr=curr..tEvent[2]
- if #curr>#pass then curr=curr:sub(2,#curr) end
- if curr==pass then unlock() end
- elseif tEvent[1]=="paste" then
- if tEvent[2]==pass then unlock() end
- end
- --Pass event if its allowed to go on.
- if not Events[tEvent[1]] then
- if sFilter == nil or tEvent[1] == sFilter or tEvent[1] == "terminate" then
- sFilter = resume( unpack( tEvent ) )
- end
- end
- else
- --See if you dont need to lockdown
- if tEvent[1]=="timer" and tEvent[2]==lockdown then
- lock()
- elseif tEvent[1]=="key" and tEvent[2]==lockkey then
- lock()
- end
- --Update lockdown counter if one of ActionEvents
- if ActionEvents[tEvent[1]] then
- if lockdown then os.cancelTimer(lockdown) end
- lockdown=os.startTimer(passtime)
- end
- --Pass event on
- if sFilter == nil or tEvent[1] == sFilter or tEvent[1] == "terminate" then
- sFilter = resume( unpack( tEvent ) )
- end
- end
- end
- end )
- --redirect back to start one
- term.redirect( parentTerm )
- --print errors
- if not ok then
- printError( param )
- end
- --if Shutdown then shutdown
- if ShutdownOnExit then shutdown() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement