Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.pullEvent = os.pullEventRaw
- -- By: 1lann. Written in five to six hours
- --local stargate = peripheral.wrap("stargate_base_1")
- local stargate = peripheral.wrap("back")
- local glass = peripheral.wrap("bottom")
- if not fs.exists("/stargatedata") then
- local f = io.open("/stargatedata", "w")
- local save = {{["password"]="trains",["timeout"]=0,["autoreject"]=true}, {}, {}}
- f:write(textutils.serialize(save))
- f:close()
- end
- local unknowns = {}
- local addresses = {}
- local config = {}
- local skipPrint = false
- local startingTime = nil
- local logs = {}
- local readerColor = colors.red
- local glassLines = {}
- local previousConnection = nil
- local readerTimer = nil
- local gColors = {}
- gColors.red = 0xff3333
- gColors.blue = 0x7dd2e4
- gColors.yellow = 0xffff4d
- gColors.green = 0x4dff4d
- gColors.gray = 0xe0e0e0
- gColors.textGray = 0x818181
- gColors.text = 0x5a5a5a
- gColors.rain = 0x2e679f
- local function loadEverything()
- local f = io.open("/stargatedata", "r")
- local everything = textutils.unserialize(f:read("*a"))
- f:close()
- config = everything[1]
- unknowns = everything[2]
- addresses = everything[3]
- end
- local function saveEverything()
- local everything = {config, unknowns, addresses}
- local f = io.open("/stargatedata", "w")
- f:write(textutils.serialize(everything))
- f:close()
- end
- local function readTimeout(e, p1)
- if (e == "timer") and (p1 == readerTimer) then
- return true
- end
- end
- local function reader(replaceChar, handler)
- local posX, posY = term.getCursorPos()
- local length = term.getSize()
- local max = posX + length
- local min = posX
- local hScroll = 1
- local textPos = 1
- local data = curData
- if not data then data = "" else textPos = data:len()+1 end
- local previousFull = false
- while true do
- term.setCursorBlink(true)
- term.setCursorPos(posX, posY)
- term.write(string.rep(" ", length))
- term.setCursorPos(posX, posY)
- if not replaceChar then
- term.write(data:sub(hScroll, hScroll+length-1))
- else
- term.write(string.rep("*",#(data:sub(hScroll, hScroll+length-1))))
- end
- --logScreen("textPos: " .. textPos .. " hScroll: " .. hScroll .. " Relationship: " .. length-(textPos-hScroll)+1 .. "\n" .. data)
- term.setCursorPos(posX+textPos-hScroll, posY)
- local e, p1, p2, p3, p4, p5 = os.pullEvent()
- if handler then
- local handleResponse, h1 = handler(e, p1, p2, p3, p4, p5)
- if handleResponse then return "handler", h1, data, e, p1, p2, p3, p4, p5 end
- end
- if (e == "char") then
- if term.getCursorPos() >= posX+length-1 then
- hScroll=hScroll+1
- if true then
- data = data:sub(1, textPos-1) .. p1 .. data:sub(textPos, -1)
- else
- textPos = textPos1
- data = data:sub(1, textPos-1) .. p1 .. data:sub(textPos, -1)
- end
- textPos = textPos+1
- previousFull = true
- else
- previousFull = false
- data = data:sub(1, textPos-1) .. p1 .. data:sub(textPos, -1)
- textPos = textPos+1
- end
- elseif (e == "key") then
- if p1 == 28 then
- term.setCursorBlink(false)
- return "enter", data
- elseif p1 == 203 then
- if textPos > 1 then
- textPos = textPos-1
- if textPos <= hScroll then
- if hScroll > 1 then
- hScroll = hScroll-((hScroll-textPos)+1)
- end
- end
- end
- elseif p1 == 205 then
- if textPos <= data:len() then
- textPos = textPos+1
- if 1 >= length-(textPos-hScroll)+1 then
- hScroll = hScroll+1
- end
- end
- elseif p1 == 14 then
- if textPos > 1 then
- data = data:sub(1, textPos-2) .. data:sub(textPos, -1)
- textPos = textPos-1
- if textPos <= hScroll then
- if hScroll > 1 then
- hScroll = hScroll-((hScroll-textPos)+1)
- end
- end
- end
- end
- end
- end
- end
- local function trimText(s)
- if s then
- return s:match("^%s*(.-)%s*$")
- else
- return nil
- end
- end
- local function isAddressRecorded(address)
- if address == "" then return true end
- local address = trimText(address):upper()
- for k,v in pairs(unknowns) do
- if v == address then
- return true
- end
- end
- for k,v in pairs(addresses) do
- if v == address then
- return true
- end
- end
- end
- local function isInAddressbook(address)
- local address = trimText(address):upper()
- for k,v in pairs(addresses) do
- if v == address then
- return true, k
- elseif k:upper() == address then
- return true, v
- end
- end
- end
- local function getID(address)
- local address = trimText(address):upper()
- for k,v in pairs(addresses) do
- if v == address then
- return k
- end
- end
- return false
- end
- local function getAddress(id)
- local id = trimText(id):lower()
- for k,v in pairs(addresses) do
- if k == id then
- return v
- end
- end
- return false
- end
- local function autofill(address)
- if getID(address) or getAddress(address) then
- return address
- end
- if address:upper() == address then
- for k,v in pairs(addresses) do
- if v:sub(1,#address):lower() == address then
- return v
- end
- end
- for k,v in pairs(unknowns) do
- if v:sub(1,#address):lower() == address then
- return v
- end
- end
- else
- local address = address:lower()
- for k,v in pairs(addresses) do
- if k:sub(1,#address) == address then
- return k
- end
- end
- for k,v in pairs(addresses) do
- if k:find(address) then
- return k
- end
- end
- for k,v in pairs(addresses) do
- if v:sub(1,#address):lower() == address then
- return v
- end
- end
- for k,v in pairs(unknowns) do
- if v:sub(1,#address):lower() == address then
- return v
- end
- end
- end
- return false
- end
- local function autofillUnknown(address)
- local address = trimText(address):upper()
- for k,v in pairs(unknowns) do
- if v:sub(1,#address) == address then
- return v
- end
- end
- return false
- end
- local width, height = term.getSize()
- local function push(data, color)
- local oldX, oldY = term.getCursorPos()
- logs[#logs+1] = {data, color}
- for i = 1, height-2 do
- term.setCursorPos(1, i)
- term.clearLine()
- end
- if #logs-(height-3) > 0 then
- term.setCursorPos(1, 1)
- for i = #logs-(height-3), #logs do
- term.setTextColor(logs[i][2])
- term.write(logs[i][1])
- print("")
- end
- else
- term.setCursorPos(1, height-1-#logs)
- for i = 1, #logs do
- term.setTextColor(logs[i][2])
- term.write(logs[i][1])
- print("")
- end
- end
- term.setTextColor(readerColor)
- term.setCursorPos(oldX, oldY)
- if glass then
- for i = 1, 10 do
- glassLines[i].setText("")
- end
- if #logs > 9 then
- for i = #logs, #logs-9, -1 do
- local setCol = nil
- if logs[i][2] == colors.red then
- setCol = gColors.red
- elseif logs[i][2] == colors.lightBlue then
- setCol = gColors.blue
- elseif logs[i][2] == colors.white then
- setCol = gColors.text
- elseif logs[i][2] == colors.yellow then
- setCol = gColors.yellow
- else
- setCol = 0
- end
- glassLines[(#logs-i)+1].setColor(setCol)
- glassLines[(#logs-i)+1].setText(logs[i][1])
- end
- else
- for i = #logs, 1, -1 do
- local setCol = nil
- if logs[i][2] == colors.red then
- setCol = gColors.red
- elseif logs[i][2] == colors.lightBlue then
- setCol = gColors.blue
- elseif logs[i][2] == colors.white then
- setCol = gColors.text
- elseif logs[i][2] == colors.yellow then
- setCol = gColors.yellow
- else
- setCol = 0
- end
- glassLines[(#logs-i)+1].setColor(setCol)
- glassLines[(#logs-i)+1].setText(logs[i][1])
- end
- end
- end
- end
- local function disconnect()
- if (stargate.getDialledAddress() ~= "") then
- skipPrint = true
- if stargate.isConnected() == "true" then
- push("Connection Closed by Controller [" .. tostring(math.ceil(os.clock()-startingTime-0.5)) .."]", colors.lightBlue)
- stargate.disconnect()
- else
- push("Connection Cancelled by Controller", colors.lightBlue)
- stargate.disconnect()
- end
- end
- end
- local function performAction(data, prompt)
- if (trimText(data) ~= "") then
- if prompt then
- term.setCursorPos(1, height)
- term.clearLine()
- term.write("> ")
- end
- push("> " .. data:sub(1,width-1), colors.white)
- data = trimText(data:lower())
- if data:sub(1,9) == "register " then
- local args = data:sub(10,-1)
- local address = args:match("(%a+)%s.+")
- local name = args:match("%a+%s(.+)")
- if not(address and name) then
- push("Missing arguments!", colors.red)
- end
- address = trimText(address):upper()
- name = trimText(name):lower()
- local aResp, aState = isInAddressbook(address)
- local nResp, rState = isInAddressbook(name)
- if aResp and nResp then
- push("Address and name is already in address book!", colors.red)
- elseif aResp then
- push("Address is already in address book!", colors.red)
- elseif nResp then
- push("Name is already in address book!", colors.red)
- else
- if autofillUnknown(address) then
- address = autofillUnknown(address)
- for k,v in pairs(unknowns) do
- if v == address then
- table.remove(unknowns, k)
- break
- end
- end
- end
- if #address ~= 7 then
- push("Invalid address!", colors.red)
- else
- addresses[name] = address
- saveEverything()
- push("Registered: " .. name .. " - " .. address, colors.yellow)
- end
- end
- elseif data:sub(1,11) == "unregister " then
- local args = autofill(data:sub(12,-1))
- if args then
- args = trimText(args):upper()
- if getAddress(args) then
- push("Unregistered: " .. args:lower() .. " - " .. getAddress(args), colors.yellow)
- addresses[args:lower()] = nil
- elseif getID(args) then
- local index = getID(args)
- push("Unregistered: " .. index .. " - " .. args, colors.yellow)
- addresses[index] = nil
- else
- for k,v in pairs(unknowns) do
- if v == args then
- table.remove(unknowns, k)
- push("Unregistered unknown: "..args:upper(), colors.yellow)
- break
- end
- end
- end
- saveEverything()
- else
- push("Could not find entry!", colors.red)
- end
- elseif data == "dc" or (data == "disconnect") then
- disconnect()
- elseif data:sub(1,8) == "timeout " then
- local args = data:sub(9,-1)
- if tonumber(args) and (tonumber(args) >= 0) then
- config["timeout"] = tonumber(args)
- saveEverything()
- push("Timeout set to " .. tostring(tonumber(args)), colors.yellow)
- else
- push("Invalid number!", colors.red)
- end
- elseif data == "autoreject" then
- config["autoreject"] = not(config["autoreject"])
- saveEverything()
- push("Auto-reject: " .. tostring(config["autoreject"]), colors.yellow)
- elseif data == "redial" then
- if previousConnection then
- disconnect()
- local resp, err = pcall(stargate.connect, previousConnection)
- if resp then
- push("Re-dialling...", colors.yellow)
- else
- local proccess = err:match(".+:%d+: (.+)")
- if err:find("java.") then
- proccess = proccess:match("java.%S+ (.+)")
- end
- push(proccess, colors.red)
- end
- else
- push("No gate to redial!", colors.red)
- end
- elseif data:sub(1,5) == "dial " then
- local args = data:sub(6,-1)
- if autofill(args) then
- if trimText(args):upper() ~= trimText(autofill(args)):upper() then
- args = autofill(args)
- end
- if getAddress(args) then
- disconnect()
- local resp, err = pcall(stargate.connect, getAddress(args))
- if resp then
- push("Dialling...", colors.yellow)
- else
- local proccess = err:match(".+:%d+: (.+)")
- if err:find("java.") then
- proccess = proccess:match("java.%S+ (.+)")
- end
- push(proccess, colors.red)
- end
- else
- args = args:upper()
- disconnect()
- local resp, err = pcall(stargate.connect, args)
- if resp then
- push("Dialling...", colors.yellow)
- else
- local proccess = err:match(".+:%d+: (.+)")
- if err:find("java.") then
- proccess = proccess:match("java.%S+ (.+)")
- end
- push(proccess, colors.red)
- end
- end
- else
- args = args:upper()
- disconnect()
- local resp, err = pcall(stargate.connect, args)
- if resp then
- push("Dialling...", colors.yellow)
- else
- local proccess = err:match(".+:%d+: (.+)")
- if err:find("java.") then
- proccess = proccess:match("java.%S+ (.+)")
- end
- push(proccess, colors.red)
- end
- end
- elseif data:sub(1,6) == "whois " then
- local args = autofill(data:sub(7,-1))
- if args then
- if getAddress(args) then
- push(args:lower() .. "'s address is " .. getAddress(args):upper(), colors.yellow)
- elseif getID(args) then
- push(args:upper() .. " is " .. getID(args), colors.yellow)
- else
- push("Unknown address!", colors.red)
- end
- else
- push("Unknown address!", colors.red)
- end
- elseif data == "list unknown" or (data == "unknown") then
- local list = {"Unknown Address List (" .. tostring(#unknowns) .. "):"}
- local nextLine = ""
- local previousLine = ""
- for k,v in pairs(unknowns) do
- previousLine = nextLine
- nextLine = nextLine .. v..", "
- if #nextLine > width then
- table.insert(list, previousLine)
- nextLine = v..", "
- end
- end
- table.insert(list, nextLine)
- for k,v in ipairs(list) do
- push(v, colors.yellow)
- end
- elseif data == "list book" or (data == "list addresses") or (data == "list address") or (data == "addresses") then
- local count = 0
- for k,v in pairs(addresses) do
- count = count+1
- end
- local list = {"Address List (" .. tostring(count) .. "):"}
- local nextLine = ""
- local previousLine = ""
- for k,v in pairs(addresses) do
- previousLine = nextLine
- nextLine = nextLine .. k..": "..v..", "
- if #nextLine > width then
- table.insert(list, previousLine)
- nextLine = k..": "..v..", "
- end
- end
- table.insert(list,nextLine)
- for k,v in ipairs(list) do
- push(v, colors.yellow)
- end
- elseif data == "me" or (data == "address") or (data == "info") then
- push("Autoreject: " .. tostring(config["autoreject"]), colors.yellow)
- push("Timeout: " .. config["timeout"], colors.yellow)
- push("This stargate address: " .. stargate.getHomeAddress(), colors.yellow)
- if stargate.getDialledAddress() ~= "" then
- local otherSide = stargate.getDialledAddress()
- local printAddress = ""
- if getID(otherSide) then
- printAddress = getID(otherSide) .. " - " .. otherSide
- else
- printAddress = otherSide
- end
- if stargate.isConnected() == "false" then
- push("Dialling: " .. printAddress, colors.yellow)
- else
- push("Connected to: " .. printAddress, colors.yellow)
- end
- else
- push("Not connected", colors.yellow)
- end
- elseif data == "clear" then
- logs = {}
- push("Logs cleared!",colors.red)
- elseif (data == "logout") or (data == "help") or (data == "lock") or (data == "shell") then
- return false
- elseif data == "immafiringmailazor" then
- return true
- else
- push("Unknown Command!", colors.red)
- end
- end
- end
- local function startPanel()
- --local function textField(posX, posY, length, curData, handler)
- while true do
- term.setCursorPos(1, height)
- term.setTextColor(colors.white)
- write("> ")
- readerColor = colors.white
- readerTimer = os.startTimer(300)
- local respType, data = reader(false, readTimeout)
- if respType == "enter" then
- local resp = performAction(data, true)
- if resp == true then
- return true
- elseif resp == false then
- return false
- end
- elseif respType == "handler" then
- push("Inactivity Timeout!", colors.red)
- return false
- end
- end
- end
- local function start()
- term.clear()
- term.setCursorPos(1, 1)
- push("Stargate control panel", colors.yellow)
- push("Pure-text like a bawss", colors.yellow)
- push("Autoreject: " .. tostring(config["autoreject"]), colors.yellow)
- push("Timeout: " .. tostring(config["timeout"]), colors.yellow)
- push("This stargate address: " .. stargate.getHomeAddress(), colors.yellow)
- while true do
- term.setCursorBlink(false)
- term.setCursorPos(1,height-1)
- term.setTextColor(colors.lightGray)
- term.write(string.rep("-", width))
- term.setCursorPos(1,height)
- term.setTextColor(colors.red)
- term.write("-[This control panel is locked]-")
- while true do
- local e, key = os.pullEvent()
- if e == "key" and (key == 15) then
- break
- end
- end
- term.setTextColor(colors.red)
- term.setCursorPos(1,height)
- term.clearLine()
- term.write("> ")
- readerColor = colors.red
- local _, pass = reader("*")
- if pass == tostring(config["password"]) then
- term.setCursorPos(1,height)
- term.clearLine()
- term.write("Password accepted")
- sleep(1)
- term.clearLine()
- if startPanel() then
- term.clear()
- term.setCursorPos(1,1)
- return "quit"
- end
- else
- term.setCursorPos(1,height)
- term.write("Password incorrect!")
- sleep(3)
- end
- end
- end
- local function glasses()
- if glass then
- glass.clear()
- local mainBox = glass.addBox(10, 10, 150, 55, gColors.gray, 0.7)
- local outlineT = glass.addBox(8,8,154,2,gColors.blue,0.7)
- local outlineB = glass.addBox(8,65,154,2,gColors.blue,0.7)
- for i = 1, 10 do
- glassLines[#glassLines+1] = glass.addText(15,62-(i*5),"",0)
- glassLines[#glassLines].setScale(0.5)
- end
- while true do
- local e, msg, user = os.pullEvent()
- if (e == "chat_command") and (user == "1lann") then
- performAction(msg)
- end
- end
- else
- while true do os.pullEvent() end
- end
- end
- local function monitor()
- while true do
- local timer = 0
- if (stargate.getDialledAddress() ~= "") then
- local skipAll = false
- local otherSide = stargate.getDialledAddress()
- if not(isAddressRecorded(otherSide)) then
- table.insert(unknowns, otherSide)
- saveEverything()
- end
- if otherSide ~= "" then
- previousConnection = otherSide
- if stargate.isInitiator() == "true" then
- if getID(otherSide) then
- push("Outgoing: " .. getID(otherSide) .. " - " .. otherSide, colors.lightBlue)
- else
- push("Outgoing: " .. otherSide, colors.lightBlue)
- end
- elseif stargate.isInitiator() == "false" then
- if getID(otherSide) then
- push("Incoming: " .. getID(otherSide) .. " - " .. otherSide, colors.lightBlue)
- else
- push("Incoming: " .. otherSide, colors.lightBlue)
- end
- if config["autoreject"] then
- disconnect()
- push("Auto-rejected!", colors.lightBlue)
- skipAll = true
- end
- else
- skipAll = true
- end
- end
- if not(skipAll) then
- sleep(1)
- if stargate.getDialledAddress() == "" then
- push("Connection Rejected by Remote!", colors.red)
- skipAll = true
- end
- if not(skipAll) then
- while stargate.isConnected() == "false" and (stargate.getDialledAddress() ~= "") do
- sleep(0.5)
- end
- if stargate.getDialledAddress() == "" then
- if not(skipPrint) then
- push("Connection Cancelled by Remote", colors.lightBlue)
- else
- skipPrint = false
- end
- else
- push("Connection Established", colors.lightBlue)
- startingTime = os.clock()
- while stargate.isConnected() == "true" do
- if not(timer > 0) then
- if config["timeout"] > 0 then
- timer = os.clock()+config["timeout"]
- end
- elseif os.clock() >= timer then
- push("Stargate Timeout", colors.lightBlue)
- disconnect()
- break
- end
- sleep(0.5)
- end
- if not(skipPrint) then
- push("Connection Closed by Remote ["..tostring(math.ceil(os.clock()-startingTime-0.5)).."]", colors.lightBlue)
- else
- skipPrint = false
- end
- end
- end
- end
- end
- sleep(0.5)
- end
- end
- loadEverything()
- parallel.waitForAny(glasses, start, monitor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement