SHARE
TWEET

Door control speech edition 1.10.2

Omni772 Jul 9th, 2017 139 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local accessLevel = 1
  2. local cryptKey = {1, 2, 3, 4, 5}
  3. local modemPort = 199
  4.  
  5.  
  6.  
  7. local component = require("component")
  8. local gpu = component.gpu
  9. local event = require("event")
  10. local ser = require("serialization")
  11. local term = require("term")
  12. local computer = component.computer
  13.  
  14. local speechBox = component.speech_box
  15. local door = component.os_doorcontroller
  16. local magReader = component.os_magreader
  17.  
  18. local modem = component.modem
  19. -- time to have door remain open in seconds (default 2)
  20. local timeout = 2
  21.  
  22. local function convert( chars, dist, inv )
  23.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  24. end
  25.  
  26. local function crypt(str,k,inv)
  27.   local enc= "";
  28.   for i=1,#str do
  29.     if(#str-k[5] >= i or not inv)then
  30.       for inc=0,3 do
  31.     if(i%4 == inc)then
  32.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  33.       break;
  34.     end
  35.       end
  36.     end
  37.   end
  38.   if(not inv)then
  39.     for i=1,k[5] do
  40.       enc = enc .. string.char(math.random(32,126));
  41.     end
  42.   end
  43.   return enc;
  44. end
  45.  
  46.  
  47. function splitString(str, sep)
  48.         local sep, fields = sep or ":", {}
  49.         local pattern = string.format("([^%s]+)", sep)
  50.         str:gsub(pattern, function(c) fields[#fields+1] = c end)
  51.         return fields
  52. end
  53.  
  54. term.clear()
  55. print("Security door   Access level : " .. tostring(accessLevel))
  56. print("---------------------------------------------------------------------------")
  57.  
  58. if modem.isOpen(modemPort) == false then
  59.   modem.open(modemPort)
  60. end
  61. data = crypt(tostring(accessLevel), cryptKey)
  62. modem.broadcast(modemPort, "setlevel", data)
  63.  
  64. while true do
  65.   ev, _, user, str, uuid = event.pull("magData")
  66.   local data = crypt(str, cryptKey, true)
  67.   if ev then
  68.     local tmpTable = splitString(data," ")
  69.     term.write(tmpTable[3] .. ":")
  70.     if modem.isOpen(modemPort) == false then
  71.       modem.open(modemPort)
  72.     end
  73.     data = crypt(tmpTable[3], cryptKey)
  74.     modem.broadcast(modemPort, "checkuser", data)
  75.     local e, _, from, port, _, msg = event.pull(1, "modem_message")
  76.     if e then
  77.       data = crypt(msg, cryptKey, true)
  78. --    print(data)
  79.       if data == "true" then
  80.     term.write("Access granted\n")
  81.     computer.beep()
  82. speechBox.say("Access Granted")
  83.     door.toggle()
  84.     os.sleep(timeout)
  85.     door.toggle()
  86.       else
  87.     term.write("Access denied\n")
  88. speechBox.say("Access Denied")
  89.       end
  90.     else
  91.       term.write("server timeout\n")
  92. speechBox.say("Error. Server Timeout")
  93.     end
  94.   end
  95. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top