Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- state1 = "Actions: [Download] Run Cancel"
- state2 = "Actions: Download [Run] Cancel"
- state3 = "Actions: Download Run [Cancel]"
- welcome1 = "[Previous] Enter URL Server List Exit"
- welcome2 = " Previous [Enter URL] Server List Exit"
- welcome3 = " Previous Enter URL [Server List] Exit"
- welcome4 = " Previous Enter URL Server List [Exit]"
- remove1 = "Remove server? [Yes] No"
- remove2 = "Remove server? Yes [No]"
- local items = {}
- local types = {}
- local href = {}
- local selection = nil
- local title = nil
- local url = nil
- local scroll = nil
- local home = nil
- local start = nil
- function welcomeSelect(wSel)
- if wSel == 1 then
- term.setCursorPos(5, 8)
- term.clearLine()
- write(welcome1)
- event, key = os.pullEvent("key")
- if key == 205 then
- return welcomeSelect(2)
- elseif key == 28 then
- return 1
- else
- return welcomeSelect(1)
- end
- elseif wSel == 2 then
- term.setCursorPos(5, 8)
- term.clearLine()
- write(welcome2)
- event, key = os.pullEvent("key")
- if key == 203 then
- return welcomeSelect(1)
- elseif key == 205 then
- return welcomeSelect(3)
- elseif key == 28 then
- return 2
- else
- return welcomeSelect(2)
- end
- elseif wSel == 3 then
- term.setCursorPos(5, 8)
- term.clearLine()
- write(welcome3)
- event, key = os.pullEvent("key")
- if key == 203 then
- return welcomeSelect(2)
- elseif key == 205 then
- return welcomeSelect(4)
- elseif key == 28 then
- return 3
- else
- return welcomeSelect(3)
- end
- elseif wSel == 4 then
- term.setCursorPos(5, 8)
- term.clearLine()
- write(welcome4)
- event, key = os.pullEvent("key")
- if key == 203 then
- return welcomeSelect(3)
- elseif key == 28 then
- return 4
- else
- return welcomeSelect(4)
- end
- end
- end
- function debug(msg)
- term.setCursorPos(1,1)
- term.clearLine()
- write(msg)
- sleep(2)
- end
- local function contains(string, pattern)
- local a = string.find(string, pattern)
- if a ~= nil then
- return true
- else
- return false
- end
- end
- local function downloadrun(side)
- term.setCursorPos(1, 1)
- term.clearLine()
- if side == "state1" then
- write(state1)
- event, key = os.pullEvent("key")
- if key == 205 then
- return downloadrun("state2")
- elseif key == 28 then
- return "download"
- else
- return downloadrun("state1")
- end
- elseif side == "state2" then
- write(state2)
- event, key = os.pullEvent("key")
- if key == 203 then
- return downloadrun("state1")
- elseif key == 205 then
- return downloadrun("state3")
- elseif key == 28 then
- return "run"
- else
- return downloadrun("state2")
- end
- elseif side == "state3" then
- write(state3)
- event, key = os.pullEvent("key")
- if key == 203 then
- return downloadrun("state2")
- elseif key == 28 then
- return "cancel"
- else
- return downloadrun("state3")
- end
- end
- end
- local function drawGUI(items, scroll, selection, title, types)
- while true do
- term.clear()
- term.setCursorPos(1,1)
- write(title)
- term.setCursorPos(1,2)
- write("=================================================")
- for i = 1+scroll, #types do
- term.setCursorPos(45, (i+2)-scroll)
- write(types[i])
- end
- for d = 1+scroll, #items do
- term.setCursorPos(3, (d+2)-scroll)
- write(items[d])
- end
- local length = string.len(items[selection])
- term.setCursorPos(2, (selection+2)-scroll)
- term.write("[")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write("]")
- event, key = os.pullEvent("key")
- if key == 208 then
- term.setCursorPos(2, (selection+2)-scroll)
- term.write(" ")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write(" ")
- if selection == #items then
- elseif selection == scroll+16 then
- selection = selection+1
- scroll = scroll+1
- else
- selection = selection+1
- end
- elseif key == 200 then
- term.setCursorPos(2, (selection+2)-scroll)
- term.write(" ")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write(" ")
- if selection == 1 then
- elseif selection == scroll+1 then
- selection = selection-1
- scroll = scroll-1
- else
- selection = selection-1
- end
- elseif key == 28 then
- return(items[selection])
- elseif key == 16 then
- sleep(0.2)
- return("Press Q to Exit")
- else
- end
- end
- end
- local function serverDrawGUI(items, scroll, selection, title)
- while true do
- term.clear()
- term.setCursorPos(1,1)
- write(title)
- term.setCursorPos(1,2)
- write("=================================================")
- for d = 1+scroll, #items do
- term.setCursorPos(3, (d+2)-scroll)
- write(items[d])
- end
- local length = string.len(items[selection])
- term.setCursorPos(2, (selection+2)-scroll)
- term.write("[")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write("]")
- event, key = os.pullEvent("key")
- if key == 208 then
- term.setCursorPos(2, (selection+2)-scroll)
- term.write(" ")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write(" ")
- if selection == #items then
- elseif selection == scroll+16 then
- selection = selection+1
- scroll = scroll+1
- else
- selection = selection+1
- end
- elseif key == 200 then
- term.setCursorPos(2, (selection+2)-scroll)
- term.write(" ")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write(" ")
- if selection == 1 then
- elseif selection == scroll+1 then
- selection = selection-1
- scroll = scroll-1
- else
- selection = selection-1
- end
- elseif key == 28 then
- return(items[selection])
- elseif key == 16 then
- sleep(0.2)
- return("exit")
- elseif key == 19 and selection ~= #items then
- return "remove", selection
- end
- end
- end
- local function process()
- local f= io.open("/.tmpdata", "r")
- f:read("*l")
- f:read("*l")
- f:read("*l")
- f:read("*l")
- f:read("*l")
- f:read("*l")
- local data = f:read("*l")
- if data == nil then
- term.setCursorPos(1,1)
- term.clearLine()
- print("Not an Apache File Server! [Ok]")
- os.pullEvent("key")
- return "error"
- end
- local titlestart = nil
- local derp = nil
- if string.find(data, "<h1>") == nil then
- term.setCursorPos(1,1)
- term.clearLine()
- print("Not an Apache File Server! [Ok]")
- os.pullEvent("key")
- return "error"
- end
- titlestart = (string.find(data, "<h1>")+4)
- local titleend = (string.find(data, "</h1>")-1)
- title = string.sub(data, titlestart, titleend)
- items = {}
- types = {}
- href = {}
- local line = f:read("*l")
- apachev = 1
- if contains(line, "<table><tr><th>") then
- apachev = 2
- line = f:read("*l")
- end
- while contains(line, "<li>") or contains(line, "</td><td>") do
- rStart = (string.find(line, "<a href=")-4)
- lStart = string.find(line, "\"> ", rStart)
- if apachev == 2 then
- lStart = (string.find(line, "\">", rStart) + 2)
- else
- lStart = (string.find(line, "\"> ", rStart)+3)
- end
- lEnd = (string.find(line, "</a>", rStart) - 1)
- lData = string.sub(line, lStart, lEnd)
- table.insert(items, lData)
- hStart = (string.find(line, "<a href=\"", rStart) + 9)
- hEnd = (string.find(line, "\">", rStart) - 1)
- hData = string.sub(line, hStart, hEnd)
- table.insert(href, hData)
- if contains(lData, "Parent Directory") then
- lPStart = (string.find(line, "<a href=\"", rStart) + 9)
- lPEnd = (string.find(line, "\">", rStart) - 1)
- lPData = string.sub(line, lPStart, lPEnd)
- table.insert(types, "back")
- elseif contains(lData, "/") then
- table.insert(types, "dir")
- else
- table.insert(types, "file")
- end
- line = f:read("*l")
- end
- return title, items, types
- end
- local function isFile(filename, items, types)
- for i = 1, #items do
- if filename == items[i] and types[i] == "file" then
- return true
- end
- end
- return false
- end
- local function gethref(ref)
- for i = 1, #items do
- if items[i] == ref then
- return href[i]
- end
- end
- end
- local function loadPage(url)
- term.setCursorBlink(false)
- term.setCursorPos(1, 1)
- term.clearLine()
- write("Connecting...")
- http.request(url)
- while true do
- event, a, code = os.pullEvent()
- if event == "http_failure" then
- term.setCursorPos(1,1)
- term.clearLine()
- print("Connection failed! [Ok]")
- os.pullEvent("key")
- return "error"
- elseif event == "http_success" then
- break
- elseif event == "key" and a == 16 then
- return "error"
- end
- end
- if code == nil then
- term.setCursorPos(1, 1)
- term.clearLine()
- print("Page not found! [Ok]")
- os.pullEvent("key")
- return "error"
- end
- f = io.open("/.tmpdata", "w")
- f:write(code:readAll())
- f:close()
- code:close()
- title, items, types = process()
- if title == "error" then
- return "error"
- end
- newpage = drawGUI(items, 0, 1, title, types)
- if isFile(newpage, items, types) then
- npage = gethref(newpage)
- dr = downloadrun("state1")
- if dr == "download" then
- term.setCursorPos(1, 1)
- term.clearLine()
- write("Download as: ")
- saveas = read()
- if contains(saveas, " ") or saveas == "" then
- return(url)
- else
- if contains(saveas, "/") then
- local reverse = string.reverse(saveas)
- local fDir = (string.len(saveas) - string.find(reverse, "/"))
- local cDir = string.sub(saveas, 1, fDir)
- if fs.exists(cDir) then
- local tmpF = http.get(url .. npage)
- local code = tmpF:readAll()
- tmpF:close()
- f = io.open(saveas , "w")
- f:write(code)
- f:close()
- return(url)
- else
- term.setCursorPos(1,1)
- term.clearLine()
- write("Directory does not exist! Press any key...")
- os.pullEvent("key")
- return(url)
- end
- end
- local tmpF = http.get(url .. npage)
- local code = tmpF:readAll()
- tmpF:close()
- f = io.open(saveas , "w")
- f:write(code)
- f:close()
- return(url)
- end
- elseif dr == "run" then
- local tmpF = http.get(url .. npage)
- local code = tmpF:readAll()
- tmpF:close()
- f = io.open("/.tmprun" , "w")
- f:write(code)
- f:close()
- term.clear()
- term.setCursorPos(1, 1)
- shell.run("/.tmprun")
- x, y = term.getCursorPos()
- if x > 6 then
- if y == 18 then
- term.setCursorPos(1, y)
- term.clearLine()
- else
- term.setCursorPos(1, y+1)
- end
- end
- print("Program ended. Press any key to continue")
- os.pullEvent("key")
- return(url)
- else
- return url
- end
- else
- if newpage == "Parent Directory" then
- return(start .. lPData)
- elseif newpage == "Press Q to Exit" then
- return("exitcode")
- else
- npage = gethref(newpage)
- return(url .. npage)
- end
- end
- end
- function main()
- start = string.sub(home, 1, string.len(home)-1)
- f = io.open("/.previous", "w")
- f:write(home)
- f:close()
- rhome = home
- hTest = string.gsub(home, "http://", "")
- a, b = string.find(hTest, "/")
- if b ~= nil then home = string.sub(home, 1, a+7)
- start = string.sub(home, 1, string.len(home)-1)
- end
- finish = loadPage(rhome)
- if finish == "error" then
- return "error"
- end
- while true do
- if finish == "exitcode" then
- term.clear()
- welcome()
- break
- end
- finish = loadPage(finish)
- if finish == "error" then
- return "error"
- end
- end
- end
- function removeSel(rSel)
- if rSel == 1 then
- term.setCursorPos(1,1)
- term.clearLine()
- write(remove1)
- event, key = os.pullEvent("key")
- if key == 205 then
- return removeSel(2)
- elseif key == 28 then
- return true
- else
- return removeSel(1)
- end
- elseif rSel == 2 then
- term.setCursorPos(1,1)
- term.clearLine()
- write(remove2)
- event, key = os.pullEvent("key")
- if key == 203 then
- return removeSel(1)
- elseif key == 28 then
- return false
- else
- return removeSel(2)
- end
- end
- end
- function saveList(fsList)
- f = io.open("/.serverlist", "w")
- toWrite = textutils.serialize(fsList)
- f:write(toWrite)
- f:close()
- end
- function serverList()
- if not fs.exists("/.serverlist") then
- f = io.open("/.serverlist", "w")
- serTable = textutils.serialize({"Add new server"})
- serverNew = true
- f:write(serTable)
- f:close()
- end
- f = io.open("/.serverlist", "r")
- serStart = f:read("*a")
- f:close()
- serList = textutils.unserialize(serStart)
- if type(serList) == "table" then
- local toRemove = nil
- serReturn, toRemove = serverDrawGUI(serList, 0, 1, "Server List")
- if serReturn == "Add new server" then
- term.setCursorPos(1, 1)
- write("Add: http://")
- local newAddress = read()
- start = ("http://" .. newAddress)
- if string.sub(string.len(start), string.len(start)) == "/" then
- home = start
- else
- home = (start .. "/")
- end
- table.insert(serList, 1, home)
- saveList(serList)
- return serverList()
- elseif serReturn == "remove" then
- rRemove = removeSel(1)
- if rRemove then table.remove(serList, toRemove)
- saveList(serList)
- end
- return serverList()
- elseif serReturn == "exit" then
- term.clear()
- welcome()
- else
- home = serReturn
- if main() == "error" then
- return "error"
- end
- end
- else
- term.setCursorPos(1, 1)
- term.clearLine()
- print("Server list is corrupted! [Ok]")
- os.pullEvent("key")
- return "error"
- end
- end
- function welcome()
- term.clear()
- term.setCursorPos(1, 1)
- print("Welcome!")
- print("=================================================")
- print("Use the arrow keys to navigate")
- print("Press enter to download/run files or open folders")
- print("Press \"Q\" to exit the browser")
- print("Press \"R\" to remove servers in the server list")
- print(" ")
- welRe = welcomeSelect(1)
- if welRe == 1 then
- if not fs.exists("/.previous") then
- f = io.open("/.previous", "w")
- f:write("http://previous")
- f:close()
- end
- f = io.open("/.previous", "r")
- home = f:read("*l")
- f:close()
- if main() == "error" then return
- welcome() end
- elseif welRe == 2 then
- term.setCursorPos(1, 8)
- term.clearLine()
- print("Server Address:")
- write("http://")
- local address = read()
- start = ("http://" .. address)
- if string.sub(string.len(start), string.len(start)) == "/" then
- home = start
- else
- home = (start .. "/")
- end
- f = io.open("/.previous", "w")
- f:write(home)
- f:close()
- if main() == "error" then return welcome() end
- elseif welRe == 3 then
- if serverList() == "error" then return welcome() end
- else
- term.clear()
- term.setCursorPos(1, 1)
- return "end"
- end
- end
- welcome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement