Advertisement
Rolcam

Computercraft Keycard Reader System V1 [Part 1 - Reader]

Sep 15th, 2020 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.98 KB | None | 0 0
  1. --[[ Info
  2.  
  3. Save this program as: "startup". The advanced computer this will be used on should be dedicated for this program only.
  4.  
  5. This program is part of a two part keycard reading system. Instead of using a password stored on the floppy disk,
  6. the program uses the unique ID of each floppy disk. Since the computer running this program will be hooked up to a more exposed
  7. disk drive, it is not recommended to have this system hooked up to a door lock, disk drive server, or anything important due to
  8. the higher risk of the computer being hijacked (as the startup program on a floppy disk overrides the computer's startup).
  9.  
  10. Note: The reader is designed to work with my validator, however you can use your own program if you know the way it communicates.
  11. ]]--
  12.  -- Set this to the drive the system will use to read the keycard
  13. side = "bottom"
  14.  
  15. -- Ejects disks on startup to unclog itself
  16. disk.eject(side)
  17. -- Prevents program termination (to prevent easy tampering)
  18. os.pullEvent = os.pullEventRaw
  19. term.clear()
  20. term.setCursorPos(1,1)
  21. term.setTextColor(colors.blue)
  22. -- Change brackets to the name of the area the reader is located (e.g. Example Town, Lobby, Workshop, etc.)
  23. print("Welcome to <Area Name Here>")
  24. print("Please make sure validator is activated!")
  25. print("Insert Keycard Now")
  26. -- Security Variable (Don't touch this)
  27. tk = 1
  28. -- Opens Rednet communications
  29. rednet.open("left")
  30. -- Main Program
  31. while true do
  32.     -- Waits for a disk to be inserted before continuing
  33.     event,side = os.pullEvent("disk")
  34.     -- Security code - Quarantines and deletes startup programs on disks
  35.     if fs.exists("disk/startup") then
  36.         term.setTextColor(colors.red)
  37.         print("WARNING: Hijacking attempt detected!")
  38.         c = 0
  39.         while tk == 1 do
  40.             -- If a program called startup_#### doesn't exist then:
  41.             if not fs.exists("startup_" .. c) then
  42.                 -- resets security variable
  43.                 tk = 0
  44.                 -- stores a copy of the startup program to the computer as "startup_####"
  45.                 shell.run("copy disk/startup", "startup_" .. c)
  46.             else
  47.                 -- Increases the number by 1
  48.                 sleep(0.1)
  49.                 c = c + 1
  50.             end
  51.         -- Deletes the startup program on the floppy disk
  52.         shell.run("delete disk/startup")
  53.         print("Program logged and deleted!")
  54.         sleep(3)
  55.         -- Ejects disk then reboots the computer
  56.         disk.eject(side)
  57.         os.reboot()
  58.         end
  59.     end
  60.     -- Checks if a data disk was inserted or if a random object was inserted instead
  61.     if not disk.hasData(side) then
  62.         term.setTextColor(colors.red)
  63.         print("Warning: Invalid Object Detected!")
  64.         sleep(3)
  65.         disk.eject(side)
  66.         os.reboot()
  67.     end
  68.     -- Program Communications
  69.     -- Grabs disk's unique ID
  70.     IDdata = disk.getID(side)
  71.     term.setTextColor(colors.cyan)
  72.     print("Transmitting Data...")
  73.     print("Do not restart the system")
  74.     print("ID #: " ..IDdata)
  75.     sleep(2)
  76.     rednet.broadcast(IDdata)
  77.     print("Awaiting Response..")
  78.     -- Waits 5 seconds for a response
  79.     id,resp1 = rednet.receive(5)
  80.     -- Checks response from validator
  81.     if resp1 == "Break" then
  82.     -- If the ID was a maintenance card (Exits to terminal)
  83.         term.setTextColor(colors.orange)
  84.         term.setCursorPos(1,7)
  85.         print("Maintenance Card Detected!")
  86.         print("Welcome Operator!")
  87.         disk.eject(side)
  88.         break;
  89.     elseif resp1 == "Denied" then
  90.     -- If the ID was rejected
  91.         term.setTextColor(colors.red)
  92.         sleep(2)
  93.         term.setCursorPos(1,7)
  94.         print("Access Denied!        ")
  95.         sleep(2)
  96.         disk.eject(side)
  97.         os.reboot()
  98.     elseif resp1 == "Granted" then
  99.     -- If the ID was accepted
  100.         sleep(1)
  101.         rednet.broadcast("resume")
  102.         term.setCursorPos(1,7)
  103.         print("Response Recieved      ")
  104.     else
  105.     -- Timed out or invalid response
  106.         term.setTextColor(colors.red)
  107.         sleep(1)
  108.         term.setCursorPos(1,7)
  109.         print("Error: Invalid Response/Timed Out")
  110.         print("Please try again later")
  111.         sleep(3)
  112.         disk.eject(side)
  113.         os.reboot()
  114.     end
  115.     id,name = rednet.receive()
  116.     term.setTextColor(colors.green)
  117.     print("Access Granted! Welcome " ..name.. "!")
  118.     sleep(2)
  119.     disk.eject(side)
  120.     os.reboot()
  121. end    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement