Advertisement
MoonlightOwl

Door Lock Terminal by Totoro

Aug 26th, 2014
580
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.59 KB | None
  1. -- Glasses Terminal by Totoro
  2. -- OpenComputers Port
  3. -- 16.07.2014
  4.  
  5. local event = require('event')
  6. local serial = require('serialization')
  7. local sides = require('sides')
  8. local fs = require('filesystem')
  9. local com = require('component')
  10. local modem = com.modem
  11. glass = com.openperipheral_glassesbridge
  12.  
  13. WIDTH = 400
  14. HEIGHT = 300
  15. USER = "Totoro"
  16. FIRSTTIMEVISIBLE = true
  17. QUEUELEN = 20
  18.  
  19. if not glass then print("Error! No glasses available!"); return end
  20. textdata = "Message, e.t.c"
  21. visible = false
  22. whitelist = {}
  23. blacklist = {}
  24. ports = {}
  25. log = {}
  26.  
  27.  
  28. -- ================================================ L O G S ================================================== --
  29. function toLog(message)
  30.   table.insert(log, message)
  31. end
  32. function saveLog()
  33.   local num = 1
  34.   repeat
  35.     name = "log_"..num..".txt"
  36.     num = num+1
  37.   until not fs.exists(name)
  38.   local file = io.open("doorlist.txt", "w")
  39.   local data = serial.serialize(log)
  40.   file:write(data)
  41.   file:close()
  42. end
  43.  
  44. -- =============================================== P O R T S ================================================= --
  45. function loadDoorlist()
  46.   if fs.exists("doorlist.txt") then
  47.     local file = io.open("doorlist.txt", "r")
  48.     local data = file:read("*a")
  49.     ports = serial.unserialize(data)
  50.     file:close()
  51.     for i=1, #ports do
  52.       modem.open(ports[i])
  53.     end
  54.   end
  55. end
  56. function saveDoorlist()
  57.   local file = io.open("doorlist.txt", "w")
  58.   local data = serial.serialize(ports)
  59.   file:write(data)
  60.   file:close()
  61. end
  62. function openPort(num)
  63.   for i=1, #ports do
  64.     if ports[i] == num then print("Port "..num.." is already open!"); return end
  65.   end
  66.   modem.open(num)
  67.   table.insert(ports, num)
  68. end
  69. function closePorts()
  70.   for i=1, #ports do
  71.     modem.close(ports[i])
  72.   end
  73. end
  74.  
  75. -- ============================================= W H I T E L I S T =========================================== --
  76. function readLists()
  77.   if fs.exists("whitelist.txt") then
  78.     local file = io.open("whitelist.txt", "r")
  79.     local data = file:read("*a")
  80.     whitelist = serial.unserialize(data)
  81.     file:close()
  82.   end
  83.   if fs.exists("blacklist.txt") then
  84.     local file = io.open("blacklist.txt", "r")
  85.     local data = file:read("*a")
  86.     blacklist = serial.unserialize(data)
  87.     file:close()
  88.   end
  89. end
  90. function saveLists()
  91.   local file = io.open("whitelist.txt", "w")
  92.   data = serial.serialize(whitelist)
  93.   file:write(data)
  94.   file:close()
  95.   local file = io.open("blacklist.txt", "w")
  96.   data = serial.serialize(blacklist)
  97.   file:write(data)
  98.   file:close()
  99. end
  100. function addToWhitelist(name)
  101.   if checkWhitelist(name) then
  102.     return false
  103.   else
  104.     table.insert(whitelist, name)
  105.     return true
  106.   end
  107. end
  108. function addToBlacklist(name)
  109.   if checkBlacklist(name) then
  110.     return false
  111.   else
  112.     table.insert(blacklist, name)
  113.     return true
  114.   end
  115. end
  116. function removeFromWhitelist(name)
  117.   for i=#whitelist, 1, -1 do
  118.     if whitelist[i] == name then
  119.       table.remove(whitelist, i)
  120.       return true
  121.     end
  122.   end
  123.   return false
  124. end
  125. function removeFromBlacklist(name)
  126.   for i=#blacklist, 1, -1 do
  127.     if blacklist[i] == name then
  128.       table.remove(blacklist, i)
  129.       return true
  130.     end
  131.   end
  132.   return false
  133. end
  134. function checkWhitelist(name)
  135.   for i=1, #whitelist do
  136.     if whitelist[i] == name then return true end
  137.   end
  138.   return false
  139. end
  140. function checkBlacklist(name)
  141.   for i=1, #blacklist do
  142.     if blacklist[i] == name then return true end
  143.   end
  144.   return false
  145. end
  146.  
  147. -- ======================================== T E X T  O B J E C T S =========================================== --
  148. function addText(x, y, text, color)
  149.   obj = {}
  150.   obj[0] = glass.addText(x+1, y+1, text, 0x000000)
  151.   obj[1] = glass.addText(x, y, text, color)
  152.   return obj
  153. end
  154. --[[function changeText(object, text, color)
  155.   object[0].setText(text)
  156.   object[1].setText(text)
  157.   if color ~= nil then
  158.     changeColor(object, color)
  159.   end
  160. end
  161. function scaleText(object, scale)
  162.   object[0].setScale(scale)
  163.   object[1].setScale(scale)
  164. end
  165. function changeColor(object, color)
  166.   object[1].setColor(color)
  167. end]]--
  168.  
  169. -- ========================================== S P L A S H E S =============================================== --
  170. function info()
  171.   x = 10+WIDTH/2-40; y = 10+HEIGHT/2-15
  172.   glass.addBox(x, y, 80, 30, 0x398eb5, 0.5)
  173.   addText(x+30, y+11, "INFO", 0xFFFFFF)
  174. end
  175. function warning()
  176.   x = 10+WIDTH/2-40; y = 10+HEIGHT/2-15
  177.   glass.addBox(x, y, 80, 30, 0xFF0000, 0.5)
  178.   addText(x+20, y+11, "WARNING", 0xFFFFFF)
  179. end
  180. function showinfo(message)
  181.   addMessage(message, 0xFFFFFF)
  182.   refresh()
  183.   info()
  184.   os.sleep(2)
  185.   refresh()
  186. end
  187. function showwarn(message)
  188.   addMessage(message, 0xFF0000)
  189.   for i=1, 3 do
  190.     refresh()
  191.     warning()
  192.     os.sleep(0.4)
  193.     refresh()
  194.     os.sleep(0.4)
  195.   end
  196. end
  197.  
  198.  
  199. -- ================================================ G U I ================================================== --
  200. queue = {}
  201. function addMessage(message, color)
  202.   table.insert(queue, {message, color})
  203.   if #queue > QUEUELEN then table.remove(queue, 1) end
  204. end
  205. function showMessages()
  206.   for i=1, #queue do
  207.     addText(20, 30+i*10, queue[i][1], queue[i][2])
  208.   end
  209. end
  210. function clearMessages()
  211.   queue = {}
  212. end
  213.  
  214. function show()
  215.   if not visible then
  216.     -- background
  217.     --glass.addBox(10, 10, WIDTH, HEIGHT, 0x508080, 0.1)
  218.     glass.addBox(15, 14, WIDTH-10, 12, 0x508080, 0.2)
  219.     glass.addBox(15, HEIGHT-30, WIDTH-10, 35, 0x508080, 0.2)
  220.  
  221.     -- title
  222.     local title_offset = 20+WIDTH/2-glass.getStringWidth("Glass Control")/2
  223.     local title = glass.addText(title_offset, 16, "Glass Control", 0xffa500)
  224.  
  225.     -- gui
  226.     glass.addText(20, HEIGHT-25, "User: ", 0xffa500)
  227.     glass.addText(20, HEIGHT-15, "Time: ", 0xffa500)
  228.  
  229.     -- data to display
  230.     showMessages()
  231.     username = addText(50, HEIGHT-25, USER, 0xFFFFFF)
  232.     time = addText(50, HEIGHT-15, os.date(), 0xFFFFFF)
  233.  
  234.     -- change state
  235.     visible = true
  236.   end
  237. end
  238. function refresh()
  239.   if not visible then
  240.     show()
  241.   else
  242.     hide()
  243.     show()
  244.   end
  245. end
  246. function hide()
  247.   if visible then
  248.     glass.clear()
  249.     visible = false
  250.   end
  251. end
  252.  
  253.  
  254. -- ================================================== M A I N ============================================== --
  255. -- init
  256. glass.clear()
  257. if FIRSTTIMEVISIBLE then show() end
  258. loadDoorlist()
  259. openPort(27)
  260. readLists()
  261. last_door = 0
  262.  
  263. -- cycle
  264. while true do
  265.   local event, add, senderAdd,
  266.     port, distance, message = event.pull()
  267.   if event == "modem_message" then
  268.     data = serial.unserialize(message)
  269.     if data[1] == "lock" then                             -- door lock message
  270.       if false and data[2] == USER then
  271.         addMessage("Welcome to home, "..USER.."!", 0x3bd268)
  272.         refresh()
  273.         modem.broadcast(port, "lock_open")
  274.       elseif checkWhitelist(data[2]) then
  275.         modem.broadcast(port, "lock_open")
  276.         showinfo(data[2].." enters the home!")
  277.         toLog("["..os.date().."|"..data[2].."|whitelist]")
  278.       elseif checkBlacklist(data[2]) then
  279.         modem.broadcast(port, "lock_deny")
  280.         showwarn(data[2].." tried to enter the home!")
  281.         toLog("["..os.date().."|"..data[2].."|blacklist]")
  282.       else
  283.         showwarn(data[2].." wants to enter (door "..port..")!")
  284.         last_door = port
  285.         toLog("["..os.date().."|"..data[2].."|?]")
  286.       end
  287.     end
  288.  
  289.   elseif event == "chat_command" then
  290.     if add == 'hide' and visible then                     -- hide gui
  291.       hide()
  292.     elseif add == 'show' and not visible then             -- show gui
  293.       show()
  294.     elseif add == 'open' then                             -- open last request door lock
  295.       modem.broadcast(last_door, "lock_open")
  296.     elseif string.sub(add, 1,4) == 'open' then            -- open door lock
  297.       modem.broadcast(tonumber(string.sub(add, 6)), "lock_open")
  298.     elseif string.sub(add, 1,4) == 'deny' then            -- don't open door
  299.       modem.broadcast(tonumber(string.sub(add, 6)), "lock_deny")
  300.     elseif string.sub(add, 1,3) == 'add' then             -- add new door to system
  301.       port = tonumber(string.sub(add, 5))
  302.       openPort(port)
  303.       showinfo("Add new door on "..port.." port.")
  304.     elseif add == 'whitelist' then
  305.       for i=1, #whitelist do
  306.         addMessage(whitelist[i])
  307.       end
  308.       refresh()
  309.     elseif string.sub(add, 1,9) == 'whitelist' then       -- add user to whitelist
  310.       name = string.sub(add, 11)
  311.       if addToWhitelist(name) then
  312.         showinfo("New user in whitelist: "..name.."!")
  313.       else
  314.         showinfo("User "..name.." is already in white list!")
  315.       end
  316.     elseif add == 'blacklist' then
  317.       for i=1, #blacklist do
  318.         addMessage(blacklist[i])
  319.       end
  320.       refresh()
  321.     elseif string.sub(add, 1,9) == 'blacklist' then       -- add user to blacklist
  322.       name = string.sub(add, 11)
  323.       if addToBlacklist(name) then
  324.         showinfo("New user in blacklist: "..name.."!")
  325.       else
  326.         showinfo("User "..name.." is already in black list!")
  327.       end
  328.     elseif string.sub(add, 1,6) == 'remove' then          -- remove user from any list
  329.       name = string.sub(add, 8)
  330.       if removeFromWhitelist(name) then
  331.         showinfo("User "..name.." removed from whitelist!")
  332.       else
  333.         if removeFromBlacklist(name) then
  334.           showinfo("User "..name.." removed from blacklist!")
  335.         end
  336.       end
  337.     elseif add == 'bash' then                             -- random bash quote
  338.       showinfo(http.get("http://bash.im/forweb/"))
  339.     end
  340.   end
  341.  
  342.   -- exit on any key
  343.   if event == 'key_down' then break end
  344. end
  345.  
  346. -- the end
  347. closePorts()
  348. glass.clear()
  349. saveLists()
  350. saveDoorlist()
  351. saveLog()
Advertisement
RAW Paste Data Copied
Advertisement