local component = require("component") local computer = require("computer") local event = require("event") local fs = require("filesystem") local ser = require("serialization") local term = require("term") local modem = component.modem local bPort = 20 -- INIT FUNCTION -- function init() if not fs.exists("/home/.eConfig.cfg") then local file = io.open(".eConfig.cfg", "w") file:write("{}") file:close() end end -- READ FUNCTION -- function read() local file = io.open(".eConfig.cfg", "r") local stream = file:read() file:close() return ser.unserialize(stream) end -- WHO FUNCTION -- function who() modem.open(bPort) modem.broadcast(bPort, "WHO") local doSearch = true local routerList = {} while doSearch do _, _, addr, _, _, flag, alias = event.pull(1, "modem_message") if flag == nil then doSearch = false modem.close(bPort) return routerList elseif flag == "WHO" then routerList[alias] = addr end end modem.close(bPort) return routerList end -- WRITE FUNCTION -- function write(data) local stream = ser.serialize(data) local file = io.open(".eConfig.cfg", "w") file:write(stream) file:close() end -- HOSTNAME FUNCTION -- function hostname() term.clear() print("New hostname:") term.write("> ") data["hostname"] = io.read() write(data) end -- ADD RELAY FUNCTION -- function addRelay() term.clear() print("Searching...") local routerList = who() if not data["relays"] then data["relays"] = {} write(data) end while true do term.clear() print("Select a relay to add:\n") for k, v in pairs(routerList) do if not data["relays"][k] then print(k, v) end end print("QUIT", "To the main menu") term.write("\n> ") gatewayAlias = io.read() if gatewayAlias == "QUIT" then break elseif routerList[gatewayAlias] then data["relays"][gatewayAlias] = routerList[gatewayAlias] write(data) else print("Wrong selection.") os.sleep(0.5) end end end -- REMOVE RELAY FUNCTION -- function removeRelay() while true do term.clear() print("Select a relay to remove:\n") if data["relays"] then for k, v in pairs(data["relays"]) do print(k, v) end end print("QUIT", "To the main menu") term.write("\n> ") gatewayAlias = io.read() if gatewayAlias == "QUIT" then break elseif data["relays"][gatewayAlias] then data["relays"][gatewayAlias] = nil write(data) else print("Wrong selection.") os.sleep(0.5) end end end init() data = read() while true do term.clear() if data["hostname"] then print("HOSTNAME: "..data["hostname"]) else print("HOSTNAME: ") end if data["relays"] then for k, v in pairs(data["relays"]) do print("RELAY: "..k, v) end else print("RELAY: ") end print() print([[ 1. Change hostname 2. Add relay 3. Remove relay 4. Quit ]]) term.write("> ") choice = io.read() if choice == "1" then hostname() elseif choice == "2" then addRelay() elseif choice == "3" then removeRelay() elseif choice == "4" then break end end