Advertisement
Wojbie

Lockette

Oct 29th, 2015 (edited)
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.21 KB | None | 0 0
  1. --# Lockette - Computer access limiter. v1.1
  2. --# Made By Wojbie
  3. --# http://pastebin.com/cGM4iF1d
  4.  
  5. --   Copyright (c) 2015-2021 Wojbie (wojbie@wojbie.net)
  6. --   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:
  7. --   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. --   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.
  9. --   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.
  10. --   4. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. --   5. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  12. --   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.
  13.  
  14. --Basic settings
  15.  
  16. local pass = "unlock"   --Password used to unlock.
  17. local passtime = 30 --Time of inactivity that causes system to autolock.
  18.  
  19. local sProgram = "/rom/programs/shell" --Path to program to run
  20. local tArguments = {} --Table of arguments for Program
  21. local ShutdownOnExit = true --If set to true computer will turn off after the program exits.
  22.  
  23. local lockscreen = true --Display lockscreen. If set to false computer will not display System Locked screen but still lockdown inputs.
  24. local scrap = false --Changes lockscreen from basic one to messed up one. Basicly fills screen with random letters and cllors.
  25.  
  26.  
  27. --Advanced settings
  28.  
  29. local lockkey = keys.scollLock  --Key for manual locking. Set to scroolLock
  30. local message="System Locked" --Message on lockscreen
  31. local bor={"+","-","|"} --Border of Messege window
  32.  
  33. 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.
  34. 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.
  35.  
  36.  
  37. --Program setup.
  38.  
  39. local parentTerm = term.current()
  40. local shutdown = os.shutdown
  41.  
  42. --Initializing coorutine
  43.  
  44. local co = coroutine.create( function()
  45.     local run=os.run
  46.     local tEnv={[ "shell" ] = shell,[ "multishell" ] = multishell,}
  47.     run(tEnv, sProgram, unpack( tArguments ) )
  48. end )
  49.  
  50. local function resume( ... )
  51.     local ok, param = coroutine.resume( co, ... )
  52.     if not ok then
  53.         printError( param )
  54.     end
  55.     return param
  56. end
  57.  
  58. --terminal stuff
  59.  
  60. local passon=true
  61. 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,})
  62. local saveWindow
  63. local lockWindow
  64.  
  65.  
  66. do
  67.     local w,h = parentTerm.getSize()
  68.     saveWindow=window.create(parentTerm,1,1,w,h,false)
  69.     lockWindow=window.create(parentTerm,1,1,w,h,false)
  70. end
  71.  
  72. local saveWindowsetVisible = saveWindow.setVisible
  73. local saveWindowreposition = saveWindow.reposition
  74. saveWindow.setVisible = function() end
  75. saveWindow.reposition = function() end
  76. --saveWindow.redraw = function() end
  77. --saveWindow.restoreCursor = function() end
  78. --saveWindow.getPosition = function() end
  79.  
  80. local function createMultitable(...)
  81.  
  82.     local tab = {...}
  83.     if #tab==1 and tab[1] and type(tab[1])=="table" then tab = tab[1] end
  84.     if #tab==0 then error("Expected table of tables or any tables to table. I know it makes no sense.", 2) end
  85.  
  86.     local manymeta={ --Anytime index is requested fist table is used as refference.
  87.     ["__index"]=function (parent , key)
  88.         if tab and tab[1] and tab[1][key] then --If it has value it tested then
  89.             if type(tab[1][key]) =="function" then --If its function then a function that calls all tables in row is made
  90.                 return function(...)
  91.                     local ret={}
  92.                     local tArgs={...}
  93.                     for i,k in ipairs(tab) do
  94.                         if k[key] then
  95.                             if #ret==0 then ret={k[key](table.unpack(tArgs))} --ret contains returns from first table that returned anything.
  96.                             else k[key](table.unpack(tArgs)) end
  97.                         end
  98.                     end
  99.                     return table.unpack(ret)
  100.                 end
  101.             else
  102.                 return tab[1][key]  --If its not a function then its just given out.
  103.             end
  104.         else
  105.             return nil --Of it not exist in first table give nothing
  106.         end
  107.     end,
  108.     ["__newindex"]=function (parent, key, value) --If someone wants to add anything to the table
  109.         --do nothing.
  110.     end,
  111.     ["__call"]=function (parent, key) --If someone calls table like function give him direct acces to table list.
  112.         --if key then tab = key end changing of content disalowed in static mode
  113.         return tab
  114.     end,
  115.     ["__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.
  116.         return (tab[1] and #tab[1]) or 0
  117.     end,
  118.     ["__metatable"]=false,--No touching the metatable.
  119.     --["__type"]="WojbieManyMeta",--Custom type? Not in current version and not sure if wise. Commented out for now.
  120.     }
  121.  
  122.     local out = {}
  123.     for key,fun in pairs(tab[1]) do --create static table of multitable functions using first one as template
  124.         out[key] = function(...)
  125.             local ret={}
  126.             local tArgs={...}
  127.             for i,k in ipairs(tab) do
  128.                 if k[key] then
  129.                     if #ret==0 then ret={k[key](table.unpack(tArgs))} --ret contains returns from first table that returned anything.
  130.                     else k[key](table.unpack(tArgs)) end
  131.                 end
  132.             end
  133.             return table.unpack(ret)
  134.         end
  135.     end
  136.     return setmetatable(out,manymeta) --create acctual manymeta table and return it
  137.    
  138. end
  139.  
  140. local multiplex=createMultitable(saveWindow,passer)
  141.  
  142. local function drawLock()
  143.     local w,h = lockWindow.getSize()
  144.     local messagesize=message:len()
  145.     if scrap then --fill the screan with (s)crap.
  146.         if parentTerm.isColor() then
  147.             for i = 1 , h do
  148.                 lockWindow.setCursorPos(1,i)
  149.                 for j = 1,w do
  150.                     lockWindow.setTextColor(2^math.random(0,15))
  151.                     lockWindow.setBackgroundColor(2^math.random(0,15))
  152.                     lockWindow.write(string.char(math.random(32,126)))
  153.                 end
  154.             end
  155.         else
  156.             local col = {colors.black , colors.white , colors.gray , colors.lightGray}
  157.             for i = 1 , h do
  158.                 lockWindow.setCursorPos(1,i)
  159.                 for j = 1,w do
  160.                     lockWindow.setTextColor(col[math.random(#col)])
  161.                     lockWindow.setBackgroundColor(col[math.random(#col)])
  162.                     lockWindow.write(string.char(math.random(32,126)))
  163.                 end
  164.             end
  165.         end
  166.     elseif h < 5 or w < messagesize+4 then --write in middle
  167.         lockWindow.setBackgroundColor(colors.black)
  168.         lockWindow.clear()
  169.         lockWindow.setTextColor(colors.white)
  170.         lockWindow.setBackgroundColor(colors.black)
  171.         lockWindow.setCursorPos(math.floor(w / 2 - messagesize / 2 )+1, math.floor(h / 2 )+1)
  172.         lockWindow.write(message)
  173.        
  174.     else --write window in middle
  175.         local offsetLeft = math.floor(w / 2 - (messagesize+4) / 2)+1
  176.         local offsetTop = math.floor(h / 2 - 5/2)+1
  177.         local filler = string.rep (" ", messagesize+2)
  178.         local line = string.rep (bor[2], messagesize+2)
  179.         lockWindow.setBackgroundColor(colors.black)
  180.         lockWindow.clear()
  181.         lockWindow.setTextColor(colors.white)
  182.         lockWindow.setBackgroundColor(colors.black)
  183.         lockWindow.setCursorPos(offsetLeft, offsetTop)
  184.         lockWindow.write(bor[1]..line..bor[1])
  185.         lockWindow.setCursorPos(offsetLeft, offsetTop+1)
  186.         lockWindow.write(bor[3]..filler..bor[3])
  187.         lockWindow.setCursorPos(offsetLeft, offsetTop+2)
  188.         lockWindow.write(bor[3].." "..message.." "..bor[3])
  189.         lockWindow.setCursorPos(offsetLeft, offsetTop+3)
  190.         lockWindow.write(bor[3]..filler..bor[3])
  191.         lockWindow.setCursorPos(offsetLeft, offsetTop+4)
  192.         lockWindow.write(bor[1]..line..bor[1])
  193.     end
  194. end
  195.  
  196. --state stuff
  197.  
  198. local curr
  199. local locked
  200. local lockdown
  201.  
  202. local function lock()
  203.     locked=true
  204.     curr=""
  205.     if lockdown then os.cancelTimer(lockdown) end
  206.     lockdown=nil
  207.     if lockscreen then
  208.         passon=false
  209.         drawLock()
  210.         lockWindow.setVisible(true)
  211.     end
  212. end
  213.  
  214. local function unlock()
  215.     locked=false
  216.     curr=""
  217.     if lockdown then os.cancelTimer(lockdown) end
  218.     lockdown=os.startTimer(passtime)
  219.     if lockscreen then
  220.         lockWindow.setVisible(false)
  221.         saveWindowsetVisible(true) --this restores contents of terminal before passon lets program write on it again.
  222.         saveWindowsetVisible(false)
  223.         passon=true
  224.     end
  225. end
  226.  
  227. term.redirect(multiplex)
  228. term.setCursorPos(1,1)
  229. term.clear()
  230. local ok, param = pcall( function()
  231.     lock()
  232.     local sFilter = resume()
  233.     while coroutine.status( co ) ~= "dead" do
  234.         local tEvent = { os.pullEventRaw() }
  235.         if tEvent[1]=="term_resize" then
  236.             -- Resize event
  237.             local w,h = parentTerm.getSize()
  238.             saveWindowreposition(1,1,w,h)
  239.             lockWindow.setVisible(false)
  240.             lockWindow.reposition(1,1,w,h)
  241.             drawLock()
  242.             lockWindow.setVisible(lockscreen and locked)
  243.         end
  244.         if locked then
  245.             --See if chars or paste fit password
  246.             if tEvent[1]=="char" then
  247.                 curr=curr..tEvent[2]
  248.                 if #curr>#pass then curr=curr:sub(2,#curr) end
  249.                 if curr==pass then unlock() end
  250.             elseif tEvent[1]=="paste" then
  251.                 if tEvent[2]==pass then unlock() end
  252.             end
  253.             --Pass event if its allowed to go on.
  254.             if not Events[tEvent[1]] then
  255.                 if sFilter == nil or tEvent[1] == sFilter or tEvent[1] == "terminate" then
  256.                     sFilter = resume( unpack( tEvent ) )
  257.                 end
  258.             end
  259.         else
  260.             --See if you dont need to lockdown
  261.             if tEvent[1]=="timer" and tEvent[2]==lockdown then
  262.                 lock()
  263.             elseif tEvent[1]=="key" and tEvent[2]==lockkey then
  264.                 lock()
  265.             end
  266.             --Update lockdown counter if one of ActionEvents
  267.             if ActionEvents[tEvent[1]] then
  268.                 if lockdown then os.cancelTimer(lockdown) end
  269.                 lockdown=os.startTimer(passtime)
  270.             end
  271.             --Pass event on
  272.             if sFilter == nil or tEvent[1] == sFilter or tEvent[1] == "terminate" then
  273.                 sFilter = resume( unpack( tEvent ) )
  274.             end
  275.         end
  276.     end
  277. end )
  278. --redirect back to start one
  279. term.redirect( parentTerm )
  280. --print errors
  281. if not ok then
  282.     printError( param )
  283. end
  284. --if Shutdown then shutdown
  285. if ShutdownOnExit then shutdown() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement