Master_crab

Computer Craft Door locking program

Aug 24th, 2015
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.04 KB | None | 0 0
  1.  
  2. -- My Security program i wish to embedd into the door locking program w/counter
  3. local counter = 0
  4. if counter == 0 then
  5. os.pullEvent = os.pullEventRaw
  6. term.clear()
  7. term.setCursorPos(1,1)
  8. print("Crab Computer Security 1.1")
  9. print("Username: Master Crab")
  10. write("Password: ")
  11. function pass()
  12. t= io.read()
  13. if t == "Monopoly" then
  14. print ("Access Granted.")
  15. sleep(2)
  16. term.clear()
  17. term.setCursorPos(1,1)
  18. else
  19. print("Incorrect Login Details.")
  20. sleep(1)
  21. term.clear()
  22. term.setCursorPos(1,1)
  23. print("Crab Computer Security 1.1")
  24. print("Username: Master Crab")
  25. write("Password: ")
  26. pass()
  27. end
  28. end
  29. pass()
  30.  
  31. end --end if
  32.  
  33. -- Second door program not written by myself was given to me from a mate who has since left the lua world.
  34. local oside=""
  35. local pass=""
  36. local config="config"
  37. local sides={"front","back","left","right","top","bottom"}
  38. local info={}
  39. local default_state=nil
  40. local delay=0
  41. local event={}
  42. local event2={}
  43. local state=nil
  44. term.clear()
  45. term.setCursorPos(1,1)
  46.  
  47. local function lockout()
  48.     os.pullEvent=os.pullEventRaw
  49.     print("hacking attempt detected!")
  50.     print("deadlocked for 30 seconds")
  51.     for i=30,0,-1 do
  52.         sleep(1)
  53.         print(tostring(i).."...")
  54.     end
  55.     print("unlocking..")
  56.     sleep(3)
  57.     for i,side in ipairs(sides) do
  58.         disk.eject(side)
  59.     end
  60. end
  61.  
  62. local function setstr(file,input) --writes to file from a var
  63.     local f=fs.open(file,"a")
  64.     f.writeLine(input)
  65.     f.close()
  66. end
  67.  
  68. local function mkfile(file) --creates a new file, deletes original first
  69.     shell.run("delete "..file)
  70.     local f=fs.open(file,"w")
  71.     f.close()
  72. end
  73.  
  74. local function sidevalid(str) --checks a string to ensure that a correct side name was used
  75.         for i2 = 1, 6 do
  76.             if str:lower() == sides[i2] then return true end
  77.         end
  78.  
  79.         return false
  80. end
  81.  
  82. local function sideerror() --self explanatory
  83.     print([[side entered incorrect, sides can only be:
  84.     top,bottom,left,right,front or back
  85.     please enter the correct side]])
  86. end
  87.  
  88. local function conf()
  89.     mkfile(config)
  90.     print("please enter a name for this lock")
  91.     local name=read()
  92.     shell.run("label set "..name)
  93.     print("enter redstone output side for this lock")
  94.     local rside=read()
  95.     if not sidevalid(rside) then
  96.         repeat
  97.             sideerror()
  98.             rside = read()
  99.         until sidevalid(rside)
  100.         setstr(config,rside)
  101.     elseif sidevalid(rside) then
  102.         setstr(config,rside)
  103.     end
  104.     print("by default. On:true or Off:false, type true or false")
  105.     state=read()
  106.     if state== "true" then
  107.         state=true
  108.         setstr(config,state)
  109.     elseif state =="false" then
  110.         state=false
  111.         setstr(config,state)
  112.     else repeat
  113.         print("invalid response, please only type true or false!")
  114.         state=read()
  115.         until (state=="true" or "false")
  116.         setstr(config,state)
  117.     end
  118.     print("press space to enter password")
  119.     print("or simply insert preconfigured passkey into disc slot")
  120.     event2= {os.pullEvent()}
  121.     if event2[1]=="key" and event2[2]==57 then
  122.         print("password entry: please enter your password")
  123.         local pass=read("*")
  124.     elseif event2[1]=="disk" then
  125.         if disk.hasData(event2[2]) then
  126.             sFile = "disk/pass"
  127.             hRead = assert(fs.open(sFile,"r"))
  128.             sPass = hRead.readLine()
  129.             hRead.close()
  130.             setstr(config,sPass)       
  131.             disk.eject(event2[2])
  132.             os.reboot()
  133.             counter = counter + 1
  134.         end
  135.     end
  136. end
  137.        
  138. local function getvars(file) --// Localize the function too, define it before we use it in 'startup' function
  139.     local f = fs.open(file,"r")
  140.     for i = 1,10 do
  141.         info[i] = f.readLine()
  142.     end
  143.     f.close()
  144. end
  145.  
  146. local function startup()
  147.     getvars(config)
  148.     pass=info[3]
  149.     oside=info[1]
  150.     default_state=info[2]
  151. end
  152.  
  153. local function state_invert()
  154.     return not default_state
  155. end
  156.  
  157. local function state_check()
  158.     if default_state=="true" then
  159.         default_state=true
  160.         return true
  161.     else
  162.         default_state=false
  163.         return false
  164.     end
  165. end
  166.  
  167. if not fs.exists(config) then
  168.     conf()
  169. else
  170.     startup()
  171. end
  172.  
  173. rs.setOutput(oside,state_check())
  174.  
  175. while true do
  176.     event = {os.pullEvent()}
  177.     if event[1]=="disk" then
  178.         if disk.hasData(event[2]) then
  179.             sFile = "disk/pass"
  180.             hRead = assert(fs.open(sFile,"r"))
  181.             sPass = hRead.readLine()
  182.             hRead.close()
  183.             if sPass == pass then
  184.                 rs.setOutput(oside,state_invert())
  185.                 disk.eject(event[2])
  186.                 sleep(3)
  187.                 os.reboot()
  188.                 counter = counter + 1
  189.             else
  190.                 disk.eject(event[2])
  191.                 os.reboot()
  192.                 counter = counter + 1
  193.             end
  194.         else
  195.             disk.eject(event[2])
  196.             os.reboot()
  197.             counter = counter + 1
  198.         end
  199.     end
  200.     if event[1]=="key" then
  201.         if event[2]==29 or event[2]==157 then
  202.             lockout()
  203.             os.reboot()
  204.             counter = counter + 1
  205.         elseif event[2]==22 then
  206.             print("lock Killer initiated, press 1 to confirm or 2 to reboot")
  207.             event={os.pullEvent("key")}
  208.             if event[2]==2 then
  209.                 print("Killing Lock..")
  210.                 sleep(1)
  211.                 shell.run("label set Broken Lock")
  212.                 shell.run("delete startup")
  213.                 shell.run("delete config")
  214.                 os.reboot()
  215.                 counter = counter + 1
  216.             elseif event[2]==3 then
  217.                 os.reboot()
  218.                 counter = counter + 1
  219.             elseif event[2]==29 or event[2]==157 then
  220.                 lockout()
  221.                 os.reboot()
  222.                 counter = counter + 1
  223.             end
  224.         end
  225.     end
  226. end
Advertisement
Add Comment
Please, Sign In to add comment