Advertisement
Frekvens1

[ComputerCraft] Basic Computer Locker

Jul 19th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 KB | None | 0 0
  1. local diskLabel = "CC:Unlock"
  2. local fileName = "default.code"
  3. local password = "admin"
  4.  
  5. local useDefault = ...
  6.  
  7. term.setBackgroundColor(colors.cyan)
  8. term.setTextColor(colors.black)
  9.  
  10. term.clear()
  11. term.setCursorPos(1,1)
  12.  
  13. if not useDefault then
  14.     print("Type disk label: (Default: '"..diskLabel.."')")
  15.  
  16.     local input = read()
  17.     if (input == "") then input = diskLabel end
  18.  
  19.     diskLabel = input
  20.  
  21.     term.clear()
  22.     term.setCursorPos(1,1)
  23.     print("(Default: '"..fileName.."')")
  24.     print("Type filename containing password:")
  25.  
  26.     local input = read()
  27.     if (input == "") then input = fileName end
  28.  
  29.     fileName = input
  30.  
  31.     term.clear()
  32.     term.setCursorPos(1,1)
  33.     print("(Default: '"..password.."')")
  34.     print("Type password to unlock computer:")
  35.  
  36.     local input = read()
  37.     if (input == "") then input = password end
  38.  
  39.     password = input
  40. end
  41.  
  42. local startup = [[
  43.     local fileName = "]]..fileName..[["
  44.     local password = "]]..password..[["
  45.    
  46.     local oldPullEvent = os.pullEvent
  47.     os.pullEvent = os.pullEventRaw
  48.    
  49.     term.setBackgroundColor(colors.cyan)
  50.     term.setTextColor(colors.black)
  51.    
  52.     term.clear()
  53.     term.setCursorPos(1,1)
  54.    
  55.     print("This computer have been locked.")
  56.    
  57.     local drive
  58.     while true do
  59.        
  60.        
  61.         for _, device in pairs(peripheral.getNames()) do
  62.             if peripheral.getType(device) == "monitor" then
  63.                 local monitor = peripheral.wrap(device)
  64.                 monitor.setBackgroundColor(colors.cyan)
  65.                 monitor.setTextColor(colors.black)
  66.                 monitor.setTextScale(0.5)
  67.                
  68.                 monitor.clear()
  69.                 monitor.setCursorPos(1,1)
  70.                
  71.                 monitor.write("Lockdown")
  72.             end
  73.            
  74.             if peripheral.getType(device) == "drive" then
  75.                 drive = peripheral.wrap(device)
  76.                
  77.                 if (drive.getDiskLabel() == "]]..diskLabel..[[") then
  78.                     local file = drive.getMountPath().."/"..fileName
  79.                    
  80.                     if fs.exists(file) then
  81.                         local reader = fs.open(file, "r")
  82.                         if reader then
  83.                             local content = reader.readAll()
  84.                             if content == password then
  85.                                 -- Unlock device
  86.                            
  87.                                 if fs.exists("startup") then fs.delete("startup") end
  88.  
  89.                                 if fs.exists("startup_old.lua") then
  90.                                     if fs.exists("startup.lua") then fs.delete("startup.lua") end
  91.                                     fs.move("startup_old.lua", "startup.lua")
  92.                                 end
  93.                                
  94.                                 if fs.exists("diskName") then
  95.                                     local reader = fs.open("diskName", "r")
  96.                                     local content = reader.readAll()
  97.                                     reader.close()
  98.                                    
  99.                                     os.setComputerLabel(content)
  100.                                    
  101.                                     fs.delete("diskName")
  102.                                 else
  103.                                     os.setComputerLabel()
  104.                                 end
  105.                                
  106.                                 os.pullEvent = oldPullEvent
  107.                                
  108.                                 for _, device in pairs(peripheral.getNames()) do
  109.                                     if peripheral.getType(device) == "monitor" then
  110.                                         local monitor = peripheral.wrap(device)
  111.  
  112.                                         monitor.setBackgroundColor(colors.black)
  113.                                         monitor.setTextColor(colors.white)
  114.                                         monitor.setTextScale(1)
  115.                                        
  116.                                         monitor.clear()
  117.                                         monitor.setCursorPos(1,1)
  118.                                     end
  119.                                 end
  120.                                
  121.                                 os.reboot()
  122.                                
  123.                             end
  124.                         end
  125.                     end
  126.                 end
  127.                
  128.             end
  129.         end
  130.        
  131.         os.sleep(1)
  132.     end
  133. ]]
  134.  
  135. if fs.exists("startup") then
  136.     if fs.exists("startup_old") then fs.delete("startup_old") end
  137.     fs.move("startup", "startup_old.lua")
  138. end
  139.  
  140. if fs.exists("startup.lua") then
  141.     if fs.exists("startup_old.lua") then fs.delete("startup_old.lua") end
  142.     fs.move("startup.lua", "startup_old.lua")
  143. end
  144.  
  145. if fs.exists("diskName") then
  146.     fs.delete("diskName")
  147. end
  148.  
  149. local h
  150. local label = os.getComputerLabel()
  151.  
  152. if label then
  153.     h = fs.open("diskName", "w")
  154.     h.write(label)
  155.     h.close()
  156. end
  157.  
  158. h = fs.open("startup", "w")
  159. h.write(startup)
  160. h.close()
  161.  
  162. os.setComputerLabel("Lockdown") -- Makes sure you keep the files
  163. os.reboot() -- Reboots into lockdown
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement