Advertisement
1lann

browser-unstable

Jul 21st, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.81 KB | None | 0 0
  1. state1 = "Actions: [Download] Run  Cancel"
  2. state2 = "Actions:  Download [Run] Cancel"
  3. state3 = "Actions:  Download  Run [Cancel]"
  4. welcome1 = "[Previous] Enter URL  Server List  Exit"
  5. welcome2 = " Previous [Enter URL] Server List  Exit"
  6. welcome3 = " Previous  Enter URL [Server List] Exit"
  7. welcome4 = " Previous  Enter URL  Server List [Exit]"
  8. remove1 = "Remove server? [Yes] No"
  9. remove2 = "Remove server?  Yes [No]"
  10. local items = {}
  11. local types = {}
  12. local href = {}
  13. local selection = nil
  14. local title = nil
  15. local url = nil
  16. local scroll = nil
  17. local home = nil
  18. local start = nil
  19.  
  20. function welcomeSelect(wSel)
  21.     if wSel == 1 then
  22.         term.setCursorPos(5, 8)
  23.         term.clearLine()
  24.         write(welcome1)
  25.         event, key = os.pullEvent("key")
  26.         if key == 205 then
  27.             return welcomeSelect(2)
  28.         elseif key == 28 then
  29.             return 1
  30.         else
  31.             return welcomeSelect(1)
  32.         end
  33.     elseif wSel == 2 then
  34.         term.setCursorPos(5, 8)
  35.         term.clearLine()
  36.         write(welcome2)
  37.         event, key = os.pullEvent("key")
  38.         if key == 203 then
  39.             return welcomeSelect(1)
  40.         elseif key == 205 then
  41.             return welcomeSelect(3)
  42.         elseif key == 28 then
  43.             return 2
  44.         else
  45.             return welcomeSelect(2)
  46.         end
  47.     elseif wSel == 3 then
  48.         term.setCursorPos(5, 8)
  49.         term.clearLine()
  50.         write(welcome3)
  51.         event, key = os.pullEvent("key")
  52.         if key == 203 then
  53.             return welcomeSelect(2)
  54.         elseif key == 205 then
  55.             return welcomeSelect(4)
  56.         elseif key == 28 then
  57.             return 3
  58.         else
  59.             return welcomeSelect(3)
  60.         end
  61.     elseif wSel == 4 then
  62.         term.setCursorPos(5, 8)
  63.         term.clearLine()
  64.         write(welcome4)
  65.         event, key = os.pullEvent("key")
  66.         if key == 203 then
  67.             return welcomeSelect(3)
  68.         elseif key == 28 then
  69.             return 4
  70.         else
  71.             return welcomeSelect(4)
  72.         end
  73.     end
  74. end
  75.  
  76.  
  77.  
  78. function debug(msg)
  79.     term.setCursorPos(1,1)
  80.     term.clearLine()
  81.     write(msg)
  82.     sleep(2)
  83. end
  84.  
  85. local function contains(string, pattern)
  86.     local a = string.find(string, pattern)
  87.     if a ~= nil then
  88.         return true
  89.     else
  90.         return false
  91.     end
  92. end
  93.  
  94. local function downloadrun(side)
  95. term.setCursorPos(1, 1)
  96. term.clearLine()
  97. if side == "state1" then
  98.     write(state1)
  99.     event, key = os.pullEvent("key")
  100.     if key == 205 then
  101.         return downloadrun("state2")
  102.     elseif key == 28 then
  103.         return "download"
  104.     else
  105.         return downloadrun("state1")
  106.     end
  107. elseif side == "state2" then
  108.     write(state2)
  109.     event, key = os.pullEvent("key")
  110.     if key == 203 then
  111.         return downloadrun("state1")
  112.     elseif key == 205 then
  113.         return downloadrun("state3")
  114.     elseif key == 28 then
  115.         return "run"
  116.     else
  117.         return downloadrun("state2")
  118.     end
  119. elseif side == "state3" then
  120.     write(state3)
  121.     event, key = os.pullEvent("key")
  122.     if key == 203 then
  123.         return downloadrun("state2")
  124.     elseif key == 28 then
  125.         return "cancel"
  126.     else
  127.         return downloadrun("state3")
  128.     end
  129. end
  130. end
  131.  
  132. local function drawGUI(items, scroll, selection, title, types)
  133.     while true do
  134.         term.clear()
  135.         term.setCursorPos(1,1)
  136.         write(title)
  137.         term.setCursorPos(1,2)
  138.         write("=================================================")
  139.         for i = 1+scroll, #types do
  140.             term.setCursorPos(45, (i+2)-scroll)
  141.             write(types[i])
  142.         end
  143.         for d = 1+scroll, #items do
  144.             term.setCursorPos(3, (d+2)-scroll)
  145.             write(items[d])
  146.         end
  147.         local length = string.len(items[selection])
  148.         term.setCursorPos(2, (selection+2)-scroll)
  149.         term.write("[")
  150.         term.setCursorPos(3+length, (selection+2)-scroll)
  151.         term.write("]")
  152. event, key = os.pullEvent("key")
  153. if key == 208 then
  154. term.setCursorPos(2, (selection+2)-scroll)
  155. term.write(" ")
  156. term.setCursorPos(3+length, (selection+2)-scroll)
  157. term.write(" ")
  158.     if selection == #items then
  159.     elseif selection == scroll+16 then
  160.         selection = selection+1
  161.         scroll = scroll+1
  162.     else
  163.         selection = selection+1
  164.     end
  165. elseif key == 200 then
  166. term.setCursorPos(2, (selection+2)-scroll)
  167. term.write(" ")
  168. term.setCursorPos(3+length, (selection+2)-scroll)
  169. term.write(" ")
  170.     if selection == 1 then
  171.     elseif selection == scroll+1 then
  172.         selection = selection-1
  173.         scroll = scroll-1
  174.     else
  175.         selection = selection-1
  176.     end
  177. elseif key == 28 then  
  178.     return(items[selection])
  179. elseif key == 16 then
  180.     sleep(0.2)
  181.     return("Press Q to Exit")
  182. else
  183. end
  184. end
  185. end
  186.  
  187. local function serverDrawGUI(items, scroll, selection, title)
  188.     while true do
  189.         term.clear()
  190.         term.setCursorPos(1,1)
  191.         write(title)
  192.         term.setCursorPos(1,2)
  193.         write("=================================================")
  194.         for d = 1+scroll, #items do
  195.             term.setCursorPos(3, (d+2)-scroll)
  196.             write(items[d])
  197.         end
  198.         local length = string.len(items[selection])
  199.         term.setCursorPos(2, (selection+2)-scroll)
  200.         term.write("[")
  201.         term.setCursorPos(3+length, (selection+2)-scroll)
  202.         term.write("]")
  203. event, key = os.pullEvent("key")
  204. if key == 208 then
  205. term.setCursorPos(2, (selection+2)-scroll)
  206. term.write(" ")
  207. term.setCursorPos(3+length, (selection+2)-scroll)
  208. term.write(" ")
  209.     if selection == #items then
  210.     elseif selection == scroll+16 then
  211.         selection = selection+1
  212.         scroll = scroll+1
  213.     else
  214.         selection = selection+1
  215.     end
  216. elseif key == 200 then
  217. term.setCursorPos(2, (selection+2)-scroll)
  218. term.write(" ")
  219. term.setCursorPos(3+length, (selection+2)-scroll)
  220. term.write(" ")
  221.     if selection == 1 then
  222.     elseif selection == scroll+1 then
  223.         selection = selection-1
  224.         scroll = scroll-1
  225.     else
  226.         selection = selection-1
  227.     end
  228. elseif key == 28 then  
  229.     return(items[selection])
  230. elseif key == 16 then
  231.     sleep(0.2)
  232.     return("exit")
  233. elseif key == 19 and selection ~= #items then
  234.     return "remove", selection
  235. end
  236. end
  237. end
  238.  
  239. local function process()
  240.     local f= io.open("/.tmpdata", "r")
  241.     f:read("*l")
  242.     f:read("*l")
  243.     f:read("*l")
  244.     f:read("*l")
  245.     f:read("*l")
  246.     f:read("*l")
  247.     local data = f:read("*l")
  248.     if data == nil then
  249.     term.setCursorPos(1,1)
  250.     term.clearLine()
  251.     print("Not an Apache File Server! [Ok]")
  252.     os.pullEvent("key")
  253.     return "error"
  254.     end
  255.     local titlestart = nil
  256.     local derp = nil
  257.     if string.find(data, "<h1>") == nil then
  258.     term.setCursorPos(1,1)
  259.     term.clearLine()
  260.     print("Not an Apache File Server! [Ok]")
  261.     os.pullEvent("key")
  262.     return "error"
  263.     end
  264.     titlestart = (string.find(data, "<h1>")+4)
  265.     local titleend = (string.find(data, "</h1>")-1)
  266.     title = string.sub(data, titlestart, titleend)
  267.     items = {}
  268.     types = {}
  269.     href = {}
  270.     local line = f:read("*l")
  271.     apachev = 1
  272.     if contains(line, "<table><tr><th>") then
  273.         apachev = 2
  274.         line = f:read("*l")
  275.     end
  276.     while contains(line, "<li>") or contains(line, "</td><td>") do
  277.         rStart = (string.find(line, "<a href=")-4)
  278.         lStart = string.find(line, "\"> ", rStart)
  279.         if apachev == 2 then
  280.             lStart = (string.find(line, "\">", rStart) + 2)
  281.         else
  282.             lStart = (string.find(line, "\"> ", rStart)+3)
  283.         end
  284.         lEnd = (string.find(line, "</a>", rStart) - 1)
  285.         lData = string.sub(line, lStart, lEnd)
  286.         table.insert(items, lData)
  287.         hStart = (string.find(line, "<a href=\"", rStart) + 9)
  288.         hEnd = (string.find(line, "\">", rStart) - 1)
  289.         hData = string.sub(line, hStart, hEnd)
  290.         table.insert(href, hData)
  291.         if contains(lData, "Parent Directory") then
  292.             lPStart = (string.find(line, "<a href=\"", rStart) + 9)
  293.             lPEnd = (string.find(line, "\">", rStart) - 1)
  294.             lPData = string.sub(line, lPStart, lPEnd)
  295.             table.insert(types, "back")
  296.             elseif contains(lData, "/") then
  297.                 table.insert(types, "dir")
  298.             else
  299.                 table.insert(types, "file")
  300.             end
  301.             line = f:read("*l")
  302.         end
  303.         return title, items, types
  304.     end
  305.  
  306. local function isFile(filename, items, types)
  307.     for i = 1, #items do
  308.         if filename == items[i] and types[i] == "file" then
  309.             return true
  310.         end
  311.     end
  312.     return false
  313. end
  314.  
  315. local function gethref(ref)
  316.     for i = 1, #items do
  317.         if items[i] == ref then
  318.             return href[i]
  319.         end
  320.     end
  321. end
  322.  
  323.  
  324. local function loadPage(url)
  325.     term.setCursorBlink(false)
  326.     term.setCursorPos(1, 1)
  327.     term.clearLine()
  328.     write("Connecting...")
  329.     http.request(url)
  330.     while true do
  331.     event, a, code = os.pullEvent()
  332.     if event == "http_failure" then
  333.     term.setCursorPos(1,1)
  334.     term.clearLine()
  335.     print("Connection failed! [Ok]")
  336.         os.pullEvent("key")
  337.         return "error"
  338.     elseif event == "http_success" then
  339.     break
  340.     elseif event == "key" and a == 16 then
  341.     return "error"
  342.     end
  343.     end
  344.     if code == nil then
  345.         term.setCursorPos(1, 1)
  346.         term.clearLine()
  347.         print("Page not found! [Ok]")
  348.         os.pullEvent("key")
  349.         return "error"
  350.     end
  351.     f = io.open("/.tmpdata", "w")
  352.     f:write(code:readAll())
  353.     f:close()
  354.     code:close()
  355.     title, items, types = process()
  356.     if title == "error" then
  357.     return "error"
  358.     end
  359.     newpage = drawGUI(items, 0, 1, title, types)
  360.     if isFile(newpage, items, types) then
  361.         npage = gethref(newpage)
  362.         dr = downloadrun("state1")
  363.         if dr == "download" then
  364.             term.setCursorPos(1, 1)
  365.             term.clearLine()
  366.             write("Download as: ")
  367.             saveas = read()
  368.             if contains(saveas, " ") or saveas == "" then
  369.                 return(url)
  370.             else
  371.                 if contains(saveas, "/") then
  372.                     local reverse = string.reverse(saveas)
  373.                     local fDir = (string.len(saveas) - string.find(reverse, "/"))
  374.                     local cDir = string.sub(saveas, 1, fDir)
  375.                     if fs.exists(cDir) then
  376.                         local tmpF = http.get(url .. npage)
  377.                         local code = tmpF:readAll()
  378.                         tmpF:close()
  379.                         f = io.open(saveas , "w")
  380.                         f:write(code)
  381.                         f:close()
  382.                         return(url)
  383.                     else
  384.                         term.setCursorPos(1,1)
  385.                         term.clearLine()
  386.                         write("Directory does not exist! Press any key...")
  387.                         os.pullEvent("key")
  388.                         return(url)
  389.                     end
  390.                 end
  391.                 local tmpF = http.get(url .. npage)
  392.                 local code = tmpF:readAll()
  393.                 tmpF:close()
  394.                 f = io.open(saveas , "w")
  395.                 f:write(code)
  396.                 f:close()
  397.                 return(url)
  398.             end
  399.             elseif dr == "run" then
  400.                 local tmpF = http.get(url .. npage)
  401.                 local code = tmpF:readAll()
  402.                 tmpF:close()
  403.                 f = io.open("/.tmprun" , "w")
  404.                 f:write(code)
  405.                 f:close()
  406.                 term.clear()
  407.                 term.setCursorPos(1, 1)
  408.                 shell.run("/.tmprun")
  409.                 x, y = term.getCursorPos()
  410.                 if x > 6 then
  411.                     if y == 18 then
  412.                         term.setCursorPos(1, y)
  413.                         term.clearLine()
  414.                     else
  415.                         term.setCursorPos(1, y+1)
  416.                     end
  417.                 end
  418.                 print("Program ended. Press any key to continue")
  419.                 os.pullEvent("key")
  420.                 return(url)
  421.             else
  422.                 return url
  423.             end
  424.         else
  425.             if newpage == "Parent Directory" then
  426.                 return(start .. lPData)
  427.                 elseif newpage == "Press Q to Exit" then
  428.                     return("exitcode")
  429.                 else
  430.                     npage = gethref(newpage)
  431.                     return(url .. npage)
  432.                 end
  433.             end
  434.         end
  435.  
  436.  
  437. function main()
  438.     start = string.sub(home, 1, string.len(home)-1)
  439.     f = io.open("/.previous", "w")
  440.     f:write(home)
  441.     f:close()
  442.     rhome = home
  443.     hTest = string.gsub(home, "http://", "")
  444.     a, b = string.find(hTest, "/")
  445.     if b ~= nil then home = string.sub(home, 1, a+7)
  446.     start = string.sub(home, 1, string.len(home)-1)
  447.     end
  448.     finish = loadPage(rhome)
  449.     if finish == "error" then
  450.         return "error"
  451.     end
  452.     while true do
  453.         if finish == "exitcode" then
  454.             term.clear()
  455.             welcome()
  456.             break
  457.         end
  458.         finish = loadPage(finish)
  459. if finish == "error" then
  460.         return "error"
  461.     end
  462.     end
  463. end
  464.  
  465. function removeSel(rSel)
  466. if rSel == 1 then
  467.     term.setCursorPos(1,1)
  468.     term.clearLine()
  469.     write(remove1)
  470.     event, key = os.pullEvent("key")
  471.     if key == 205 then
  472.         return removeSel(2)
  473.     elseif key == 28 then
  474.         return true
  475.     else
  476.         return removeSel(1)
  477.     end
  478. elseif rSel == 2 then
  479.     term.setCursorPos(1,1)
  480.     term.clearLine()
  481.     write(remove2)
  482.     event, key = os.pullEvent("key")
  483.     if key == 203 then
  484.         return removeSel(1)
  485.     elseif key == 28 then
  486.         return false
  487.     else
  488.         return removeSel(2)
  489.     end
  490. end
  491. end
  492.  
  493. function saveList(fsList)
  494. f = io.open("/.serverlist", "w")
  495. toWrite = textutils.serialize(fsList)
  496. f:write(toWrite)
  497. f:close()
  498. end
  499.  
  500. function serverList()
  501. if not fs.exists("/.serverlist") then
  502. f = io.open("/.serverlist", "w")
  503. serTable = textutils.serialize({"Add new server"})
  504. serverNew = true
  505. f:write(serTable)
  506. f:close()
  507. end
  508. f = io.open("/.serverlist", "r")
  509. serStart = f:read("*l")
  510. f:close()
  511. serList = textutils.unserialize(serStart)
  512. if type(serList) == "table" then
  513. local toRemove = nil
  514. serReturn, toRemove = serverDrawGUI(serList, 0, 1, "Server List")
  515. if serReturn == "Add new server" then
  516.     term.setCursorPos(1, 1)
  517.     write("Add: http://")
  518.     local newAddress = read()
  519.     start = ("http://" .. newAddress)
  520.     if string.sub(string.len(start), string.len(start)) == "/" then
  521.         home = start
  522.     else
  523.         home = (start .. "/")
  524.     end
  525.     table.insert(serList, 1, home)
  526.     saveList(serList)
  527.     return serverList()
  528. elseif serReturn == "remove" then
  529.     rRemove = removeSel(1)
  530.     if rRemove then table.remove(serList, toRemove)
  531.     saveList(serList)
  532.     end
  533.     return serverList()
  534. elseif serReturn == "exit" then
  535. term.clear()
  536. welcome()
  537. else
  538.     home = serReturn
  539.     if main() == "error" then
  540.     return "error"
  541.     end
  542.        
  543. end
  544. else
  545. term.setCursorPos(1, 1)
  546. term.clearLine()
  547. print("Server list is corrupted! [Ok]")
  548. os.pullEvent("key")
  549. return "error"
  550. end
  551. end
  552.  
  553. function welcome()
  554. term.clear()
  555. term.setCursorPos(1, 1)
  556. print("Welcome!")
  557. print("=================================================")
  558. print("Use the arrow keys to navigate")
  559. print("Press enter to download/run files or open folders")
  560. print("Press \"Q\" to exit the browser")
  561. print("Press \"R\" to remove servers in the server list")
  562. print(" ")
  563. welRe = welcomeSelect(1)
  564. if welRe == 1 then
  565.     if not fs.exists("/.previous") then
  566.     f = io.open("/.previous", "w")
  567.     f:write("http://previous")
  568.     f:close()
  569.     end
  570.     f = io.open("/.previous", "r")
  571.     home = f:read("*l")
  572.     f:close()
  573.     if main() == "error" then return
  574. welcome() end
  575. elseif welRe == 2 then
  576.     term.setCursorPos(1, 8)
  577.     term.clearLine()
  578.     print("Server Address:")
  579. write("http://")
  580. local address = read()
  581. start = ("http://" .. address)
  582. if string.sub(string.len(start), string.len(start)) == "/" then
  583.     home = start
  584. else
  585.     home = (start .. "/")
  586. end
  587. f = io.open("/.previous", "w")
  588. f:write(home)
  589. f:close()
  590. if main() == "error" then return welcome() end
  591. elseif welRe == 3 then
  592. if serverList() == "error" then return welcome() end
  593. else
  594.     term.clear()
  595.     term.setCursorPos(1, 1)
  596.     return "end"
  597. end
  598. end
  599.  
  600. welcome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement