-- Variables --
dir = shell.dir().."/"
settings = {}
ID = os.getComputerID()
os.loadAPI("/loc/browser/includes/inzernet")
inzernet.startup()
-- Top Bar --
function topBar()
term.setCursorPos(6, 1)
term.setBackgroundColor(colors.white)
local URL = read()
modemMes(URL)
end
-- Draw Main Function --
function drawMain()
term.setBackgroundColor(colors.lightGray)
term.clear()
paintutils.drawLine(1, 1, 51, 1, colors.gray)
term.setCursorPos(1, 1)
write("URL: ")
paintutils.drawLine(6, 1, 50, 1, colors.white)
paintutils.drawPixel(51, 1, colors.red)
term.setCursorPos(51, 1)
write("X")
term.setCursorPos(6, 1)
term.setBackgroundColor(colors.white)
webDis = window.create(term.current(), 1, 2, 51, 18)
term.setTextColor(colors.gray)
topBar()
end
-- Web Display --
function display(file, url)
term.redirect(webDis)
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1, 1)
shell.run("/loc/browser/pages/webDis", file, url)
term.redirect(term.native())
paintutils.drawLine(6, 1, 50, 1, colors.white)
term.setTextColor(colors.black)
topBar()
end
-- Modem Message Function --
function modemMes(msg)
IP = inzernet.lookup(msg)
if IP == "error" then
display(dir.."pages/error-DNS", "error-DNS")
else
page = inzernet.GET(IP, "index")
if page == "error" then
else
local web = fs.open(dir.."cache/izt-"..msg, "w")
web.write(page)
web.close()
display(dir.."cache/izt-"..msg, msg)
end
end
end
-- Load Settings --
function sLoad()
local file = fs.open(dir.."settings", "r")
local data = file.readAll()
file.close()
settings = textutils.unserialize(data)
end
-- Save Settings --
function sSave(table)
local file = fs.open(dir.."settings", "w")
file.write(textutils.serialize(table))
file.close()
end
-- Main Loop --
function mainLoop(RF)
while true do
event = {os.pullEvent()}
if event[1] == "modem_message" then
local side = event[2]
local freq = event[3]
local rfreq = event[4]
local msg = event[5]
local dis = event[6]
modemMes(msg, RF)
elseif event[1] == "mouse_click" then
local button = event[2]
local x = event[3]
local y = event[4]
if x==51 and y==1 then
term.setCursorPos(1, 1)
term.setBackgroundColor(colors.black)
term.clear()
break
end
end
end
end
-- Startup --
if fs.exists("/loc/browser/settings") then
sLoad()
parallel.waitForAll(drawMain(freq), mainLoop(freq))
else
write("What side is your main modem on? [top, bottom, right, left, back, front] ")
modem = read()
write("What side is your network modem on? [na] for none [top, bottom, right, left, back, front] ")
nmodem = read()
write("Do you have an update server? [yes, no] ")
updateServer = read()
write("If [yes] what update type? If [no] put [na] ")
updateType = read()
tSettings = {
mainSide = modem,
networkSide = nmodem,
updateServer = updateServer,
updateType = updateType,
}
sSave(tSettings)
end
webDis code:
local arg = { ... }
os.loadAPI("/loc/browser/includes/inzernet")
os.loadAPI(arg[1])
inzernet API code:
-- Variables --
ID = os.getComputerID()
settings = {}
msg = ""
-- Lookup --
function lookup(website)
pkt = {"lookup", website, "DNS", "DNS", "na", ID}
sendPacket(pkt)
return(msg[2])
end
function GET(IP, page)
gIP = parser(IP)
if tonumber(gIP[1]) then
pkt = {"GET", page, gIP[1], gIP[2], "na", ID}
sendPacket(pkt)
else
msg[2] = "error"
end
return(msg[2])
end
function PING(IP)
pIP = parser(IP)
if tonumber(pIP[1]) then
pkt = {"PING", "na", pIP[1], pIP[2], "na", ID}
sendPacket(pkt)
else
msg[2] = "error"
end
return(msg[2])
end
function otherPacket(packetName, data, desRouter, desClient)
pkt = {packetName, data, desRouter, desClient, "na", os.getComputerID }
sendPacket(pkt)
end
function wait()
local timeout = os.startTimer(3)
while true do
event = {os.pullEvent()}
if event[1] == "modem_message" then
local data = event[5]
return(data)
elseif event[1] == "timer" and event[2] == timeout then
local data = "timeout"
return(data)
end
end
end
-- IP Parser --
function parser(ip)
line = {}
for word in string.gmatch(ip, "[^.]+") do
table.insert(line, word)
end
return line
end
function sendPacket(pkt)
main.transmit(100, 100, pkt)
msg = wait()
end
function version()
ver = {protocol = "1.0", API = "1.0"}
return(ver)
end
function sLoad()
local file = fs.open("/loc/browser/settings", "r")
settings = textutils.unserialize(file.readAll())
file.close()
end
local function sSave(table)
local file = fs.open("settings","w")
file.write(textutils.serialize(table))
file.close()
end
-- Startup --
function startup()
sLoad()
main = peripheral.wrap(settings.mainSide)
main.open(100)
end
At line 64 I get the error:
inzernet:64: attempt to index ? (a nil value)
I think this is because the API isn't being unloded, but I'm not sure. Any help and code to fix it is much appreciated!