Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Glasses Terminal by Totoro
- -- OpenComputers Port
- -- 16.07.2014
- local event = require('event')
- local serial = require('serialization')
- local sides = require('sides')
- local fs = require('filesystem')
- local com = require('component')
- local modem = com.modem
- glass = com.openperipheral_glassesbridge
- WIDTH = 400
- HEIGHT = 300
- USER = "Totoro"
- FIRSTTIMEVISIBLE = true
- QUEUELEN = 20
- if not glass then print("Error! No glasses available!"); return end
- textdata = "Message, e.t.c"
- visible = false
- whitelist = {}
- blacklist = {}
- ports = {}
- log = {}
- -- ================================================ L O G S ================================================== --
- function toLog(message)
- table.insert(log, message)
- end
- function saveLog()
- local num = 1
- repeat
- name = "log_"..num..".txt"
- num = num+1
- until not fs.exists(name)
- local file = io.open("doorlist.txt", "w")
- local data = serial.serialize(log)
- file:write(data)
- file:close()
- end
- -- =============================================== P O R T S ================================================= --
- function loadDoorlist()
- if fs.exists("doorlist.txt") then
- local file = io.open("doorlist.txt", "r")
- local data = file:read("*a")
- ports = serial.unserialize(data)
- file:close()
- for i=1, #ports do
- modem.open(ports[i])
- end
- end
- end
- function saveDoorlist()
- local file = io.open("doorlist.txt", "w")
- local data = serial.serialize(ports)
- file:write(data)
- file:close()
- end
- function openPort(num)
- for i=1, #ports do
- if ports[i] == num then print("Port "..num.." is already open!"); return end
- end
- modem.open(num)
- table.insert(ports, num)
- end
- function closePorts()
- for i=1, #ports do
- modem.close(ports[i])
- end
- end
- -- ============================================= W H I T E L I S T =========================================== --
- function readLists()
- if fs.exists("whitelist.txt") then
- local file = io.open("whitelist.txt", "r")
- local data = file:read("*a")
- whitelist = serial.unserialize(data)
- file:close()
- end
- if fs.exists("blacklist.txt") then
- local file = io.open("blacklist.txt", "r")
- local data = file:read("*a")
- blacklist = serial.unserialize(data)
- file:close()
- end
- end
- function saveLists()
- local file = io.open("whitelist.txt", "w")
- data = serial.serialize(whitelist)
- file:write(data)
- file:close()
- local file = io.open("blacklist.txt", "w")
- data = serial.serialize(blacklist)
- file:write(data)
- file:close()
- end
- function addToWhitelist(name)
- if checkWhitelist(name) then
- return false
- else
- table.insert(whitelist, name)
- return true
- end
- end
- function addToBlacklist(name)
- if checkBlacklist(name) then
- return false
- else
- table.insert(blacklist, name)
- return true
- end
- end
- function removeFromWhitelist(name)
- for i=#whitelist, 1, -1 do
- if whitelist[i] == name then
- table.remove(whitelist, i)
- return true
- end
- end
- return false
- end
- function removeFromBlacklist(name)
- for i=#blacklist, 1, -1 do
- if blacklist[i] == name then
- table.remove(blacklist, i)
- return true
- end
- end
- return false
- end
- function checkWhitelist(name)
- for i=1, #whitelist do
- if whitelist[i] == name then return true end
- end
- return false
- end
- function checkBlacklist(name)
- for i=1, #blacklist do
- if blacklist[i] == name then return true end
- end
- return false
- end
- -- ======================================== T E X T O B J E C T S =========================================== --
- function addText(x, y, text, color)
- obj = {}
- obj[0] = glass.addText(x+1, y+1, text, 0x000000)
- obj[1] = glass.addText(x, y, text, color)
- return obj
- end
- --[[function changeText(object, text, color)
- object[0].setText(text)
- object[1].setText(text)
- if color ~= nil then
- changeColor(object, color)
- end
- end
- function scaleText(object, scale)
- object[0].setScale(scale)
- object[1].setScale(scale)
- end
- function changeColor(object, color)
- object[1].setColor(color)
- end]]--
- -- ========================================== S P L A S H E S =============================================== --
- function info()
- x = 10+WIDTH/2-40; y = 10+HEIGHT/2-15
- glass.addBox(x, y, 80, 30, 0x398eb5, 0.5)
- addText(x+30, y+11, "INFO", 0xFFFFFF)
- end
- function warning()
- x = 10+WIDTH/2-40; y = 10+HEIGHT/2-15
- glass.addBox(x, y, 80, 30, 0xFF0000, 0.5)
- addText(x+20, y+11, "WARNING", 0xFFFFFF)
- end
- function showinfo(message)
- addMessage(message, 0xFFFFFF)
- refresh()
- info()
- os.sleep(2)
- refresh()
- end
- function showwarn(message)
- addMessage(message, 0xFF0000)
- for i=1, 3 do
- refresh()
- warning()
- os.sleep(0.4)
- refresh()
- os.sleep(0.4)
- end
- end
- -- ================================================ G U I ================================================== --
- queue = {}
- function addMessage(message, color)
- table.insert(queue, {message, color})
- if #queue > QUEUELEN then table.remove(queue, 1) end
- end
- function showMessages()
- for i=1, #queue do
- addText(20, 30+i*10, queue[i][1], queue[i][2])
- end
- end
- function clearMessages()
- queue = {}
- end
- function show()
- if not visible then
- -- background
- --glass.addBox(10, 10, WIDTH, HEIGHT, 0x508080, 0.1)
- glass.addBox(15, 14, WIDTH-10, 12, 0x508080, 0.2)
- glass.addBox(15, HEIGHT-30, WIDTH-10, 35, 0x508080, 0.2)
- -- title
- local title_offset = 20+WIDTH/2-glass.getStringWidth("Glass Control")/2
- local title = glass.addText(title_offset, 16, "Glass Control", 0xffa500)
- -- gui
- glass.addText(20, HEIGHT-25, "User: ", 0xffa500)
- glass.addText(20, HEIGHT-15, "Time: ", 0xffa500)
- -- data to display
- showMessages()
- username = addText(50, HEIGHT-25, USER, 0xFFFFFF)
- time = addText(50, HEIGHT-15, os.date(), 0xFFFFFF)
- -- change state
- visible = true
- end
- end
- function refresh()
- if not visible then
- show()
- else
- hide()
- show()
- end
- end
- function hide()
- if visible then
- glass.clear()
- visible = false
- end
- end
- -- ================================================== M A I N ============================================== --
- -- init
- glass.clear()
- if FIRSTTIMEVISIBLE then show() end
- loadDoorlist()
- openPort(27)
- readLists()
- last_door = 0
- -- cycle
- while true do
- local event, add, senderAdd,
- port, distance, message = event.pull()
- if event == "modem_message" then
- data = serial.unserialize(message)
- if data[1] == "lock" then -- door lock message
- if false and data[2] == USER then
- addMessage("Welcome to home, "..USER.."!", 0x3bd268)
- refresh()
- modem.broadcast(port, "lock_open")
- elseif checkWhitelist(data[2]) then
- modem.broadcast(port, "lock_open")
- showinfo(data[2].." enters the home!")
- toLog("["..os.date().."|"..data[2].."|whitelist]")
- elseif checkBlacklist(data[2]) then
- modem.broadcast(port, "lock_deny")
- showwarn(data[2].." tried to enter the home!")
- toLog("["..os.date().."|"..data[2].."|blacklist]")
- else
- showwarn(data[2].." wants to enter (door "..port..")!")
- last_door = port
- toLog("["..os.date().."|"..data[2].."|?]")
- end
- end
- elseif event == "chat_command" then
- if add == 'hide' and visible then -- hide gui
- hide()
- elseif add == 'show' and not visible then -- show gui
- show()
- elseif add == 'open' then -- open last request door lock
- modem.broadcast(last_door, "lock_open")
- elseif string.sub(add, 1,4) == 'open' then -- open door lock
- modem.broadcast(tonumber(string.sub(add, 6)), "lock_open")
- elseif string.sub(add, 1,4) == 'deny' then -- don't open door
- modem.broadcast(tonumber(string.sub(add, 6)), "lock_deny")
- elseif string.sub(add, 1,3) == 'add' then -- add new door to system
- port = tonumber(string.sub(add, 5))
- openPort(port)
- showinfo("Add new door on "..port.." port.")
- elseif add == 'whitelist' then
- for i=1, #whitelist do
- addMessage(whitelist[i])
- end
- refresh()
- elseif string.sub(add, 1,9) == 'whitelist' then -- add user to whitelist
- name = string.sub(add, 11)
- if addToWhitelist(name) then
- showinfo("New user in whitelist: "..name.."!")
- else
- showinfo("User "..name.." is already in white list!")
- end
- elseif add == 'blacklist' then
- for i=1, #blacklist do
- addMessage(blacklist[i])
- end
- refresh()
- elseif string.sub(add, 1,9) == 'blacklist' then -- add user to blacklist
- name = string.sub(add, 11)
- if addToBlacklist(name) then
- showinfo("New user in blacklist: "..name.."!")
- else
- showinfo("User "..name.." is already in black list!")
- end
- elseif string.sub(add, 1,6) == 'remove' then -- remove user from any list
- name = string.sub(add, 8)
- if removeFromWhitelist(name) then
- showinfo("User "..name.." removed from whitelist!")
- else
- if removeFromBlacklist(name) then
- showinfo("User "..name.." removed from blacklist!")
- end
- end
- elseif add == 'bash' then -- random bash quote
- showinfo(http.get("http://bash.im/forweb/"))
- end
- end
- -- exit on any key
- if event == 'key_down' then break end
- end
- -- the end
- closePorts()
- glass.clear()
- saveLists()
- saveDoorlist()
- saveLog()
Advertisement
Add Comment
Please, Sign In to add comment