Advertisement
KevinW1998

browser v1.11

Jul 22nd, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.24 KB | None | 0 0
  1. state1 = "Actions: [Download] Run  Cancel"
  2. state2 = "Actions:  Download [Run] Cancel"
  3. state3 = "Actions:  Download  Run [Cancel]"
  4. local items = {}
  5. local types = {}
  6. local href = {}
  7. local selection = nil
  8. local title = nil
  9. local url = nil
  10. local scroll = nil
  11.  
  12. term.clear()
  13. term.setCursorPos(1, 1)
  14. print("Welcome!")
  15. print("=================================================")
  16. print("Use the arrow keys to navigate")
  17. print("Press enter to download/run files or open folders")
  18. print("Press \"Q\" to exit the browser")
  19. print(" ")
  20. print("Server address:")
  21. write("http://")
  22. local address = read()
  23. start = ("http://" .. address)
  24. if string.sub(string.len(start), string.len(start)) == "/" then
  25. home = start
  26. else
  27. home = (start .. "/")
  28. end
  29.  
  30. function debug(msg)
  31. term.setCursorPos(1,1)
  32.     term.clearLine()
  33.     write(msg)
  34.     sleep(2)
  35. end
  36.  
  37. local function contains(string, pattern)
  38. local a = string.find(string, pattern)
  39. if a ~= nil then
  40. return true
  41. else
  42. return false
  43. end
  44. end
  45.  
  46. local function downloadrun(side)
  47. term.setCursorPos(1, 1)
  48. term.clearLine()
  49. if side == "state1" then
  50.     write(state1)
  51.     event, key = os.pullEvent("key")
  52.     if key == 205 then
  53.         return downloadrun("state2")
  54.     elseif key == 28 then
  55.         return "download"
  56.     else
  57.         return downloadrun("state1")
  58.     end
  59. elseif side == "state2" then
  60.     write(state2)
  61.     event, key = os.pullEvent("key")
  62.     if key == 203 then
  63.         return downloadrun("state1")
  64.     elseif key == 205 then
  65.         return downloadrun("state3")
  66.     elseif key == 28 then
  67.         return "run"
  68.     else
  69.         return downloadrun("state2")
  70.     end
  71. elseif side == "state3" then
  72.     write(state3)
  73.     event, key = os.pullEvent("key")
  74.     if key == 203 then
  75.         return downloadrun("state2")
  76.     elseif key == 28 then
  77.         return "cancel"
  78.     else
  79.         return downloadrun("state3")
  80.     end
  81. end
  82. end
  83.  
  84. local function drawGUI(items, scroll, selection, title, types)
  85. while true do
  86. term.clear()
  87. term.setCursorPos(1,1)
  88. write(title)
  89. term.setCursorPos(1,2)
  90. write("=================================================")
  91. for i = 1+scroll, #types do
  92. term.setCursorPos(45, (i+2)-scroll)
  93. write(types[i])
  94. end
  95. for d = 1+scroll, #items do
  96. term.setCursorPos(3, (d+2)-scroll)
  97. write(items[d])
  98. end
  99. local length = string.len(items[selection])
  100. term.setCursorPos(2, (selection+2)-scroll)
  101. term.write("[")
  102. term.setCursorPos(3+length, (selection+2)-scroll)
  103. term.write("]")
  104. event, key = os.pullEvent("key")
  105. if key == 208 then
  106. term.setCursorPos(2, (selection+2)-scroll)
  107. term.write(" ")
  108. term.setCursorPos(3+length, (selection+2)-scroll)
  109. term.write(" ")
  110.     if selection == #items then
  111.     elseif selection == scroll+16 then
  112.         selection = selection+1
  113.         scroll = scroll+1
  114.     else
  115.         selection = selection+1
  116.     end
  117. elseif key == 200 then
  118. term.setCursorPos(2, (selection+2)-scroll)
  119. term.write(" ")
  120. term.setCursorPos(3+length, (selection+2)-scroll)
  121. term.write(" ")
  122.     if selection == 1 then
  123.     elseif selection == scroll+1 then
  124.         selection = selection-1
  125.         scroll = scroll-1
  126.     else
  127.         selection = selection-1
  128.     end
  129. elseif key == 28 then  
  130.     return(items[selection])
  131. elseif key == 16 then
  132.     sleep(0.2)
  133.     return("Press Q to Exit")
  134. else
  135. end
  136. end
  137. end
  138.  
  139. local function process()
  140. local f= io.open("/.tmpdata", "r")
  141. f:read("*l")
  142. f:read("*l")
  143. f:read("*l")
  144. f:read("*l")
  145. f:read("*l")
  146. f:read("*l")
  147. local data = f:read("*l")
  148. local titlestart = nil
  149. local derp = nil
  150. if string.find(data, "Index of /") == nil then
  151. term.setCursorPos(1, 1)
  152. term.clearLine()
  153. error("Webaddress is not an Apache Server!")
  154. end
  155. titlestart = (string.find(data, "<h1>")+4)
  156. local titleend = (string.find(data, "</h1>")-1)
  157. title = string.sub(data, titlestart, titleend)
  158. items = {}
  159. types = {}
  160. href = {}
  161. local line = f:read("*l")
  162. apachev = 1
  163. if contains(line, "<table><tr><th>") then
  164. apachev = 2
  165. line = f:read("*l")
  166. end
  167. while contains(line, "<li>") or contains(line, "</td><td>") do
  168. rStart = (string.find(line, "<a href=")-4)
  169.     lStart = string.find(line, "\"> ", rStart)
  170.     if apachev == 2 then
  171.     lStart = (string.find(line, "\">", rStart) + 2)
  172.     else
  173.     lStart = (string.find(line, "\"> ", rStart)+3)
  174.     end
  175.     lEnd = (string.find(line, "</a>", rStart) - 1)
  176.     lData = string.sub(line, lStart, lEnd)
  177.     table.insert(items, lData)
  178.     hStart = (string.find(line, "<a href=\"", rStart) + 9)
  179.     hEnd = (string.find(line, "\">", rStart) - 1)
  180.     hData = string.sub(line, hStart, hEnd)
  181.     table.insert(href, hData)
  182.     if contains(lData, "Parent Directory") then
  183.         lPStart = (string.find(line, "<a href=\"", rStart) + 9)
  184.         lPEnd = (string.find(line, "\">", rStart) - 1)
  185.         lPData = string.sub(line, lPStart, lPEnd)
  186.         table.insert(types, "back")
  187.     elseif contains(lData, "/") then
  188.         table.insert(types, "dir")
  189.     else
  190.         table.insert(types, "file")
  191.     end
  192.     line = f:read("*l")
  193. end
  194. return title, items, types
  195. end
  196.  
  197. local function isFile(filename, items, types)
  198. for i = 1, #items do
  199. if filename == items[i] and types[i] == "file" then
  200. return true
  201. end
  202. end
  203. return false
  204. end
  205.  
  206. local function gethref(ref)
  207. for i = 1, #items do
  208. if items[i] == ref then
  209. return href[i]
  210. end
  211. end
  212. end
  213.  
  214.  
  215. local function loadPage(url)
  216. term.setCursorBlink(false)
  217. term.setCursorPos(1, 1)
  218. term.clearLine()
  219. write("Connecting...")
  220. local tmpF = http.get(url)
  221. if tmpF == nil then
  222. term.setCursorPos(1, 1)
  223. term.clear()
  224. error("Connection failed!")
  225. end
  226. local code = tmpF:readAll()
  227. tmpF:close()
  228. f = io.open("/.tmpdata", "w")
  229. f:write(code)
  230. f:close()
  231. title, items, types = process()
  232. newpage = drawGUI(items, 0, 1, title, types)
  233. if isFile(newpage, items, types) then
  234.         npage = gethref(newpage)
  235.         dr = downloadrun("state1")
  236.         if dr == "download" then
  237.         term.setCursorPos(1, 1)
  238.         term.clearLine()
  239.         write("Download as: ")
  240.         saveas = read()
  241.         if contains(saveas, " ") or saveas == "" then
  242.             return(url)
  243.         else
  244.             if contains(saveas, "/") then
  245.                 local reverse = string.reverse(saveas)
  246.                 local fDir = (string.len(saveas) - string.find(reverse, "/"))
  247.                 local cDir = string.sub(saveas, 1, fDir)
  248.                 if fs.exists(cDir) then
  249.                     local tmpF = http.get(url .. npage)
  250.                     local code = tmpF:readAll()
  251.                     tmpF:close()
  252.                     f = io.open(saveas , "w")
  253.                     f:write(code)
  254.                     f:close()
  255.                     return(url)
  256.                 else
  257.                     term.setCursorPos(1,1)
  258.                     term.clearLine()
  259.                     write("Directory does not exist! Press any key...")
  260.                     os.pullEvent("key")
  261.                     return(url)
  262.                 end
  263.             end
  264.             local tmpF = http.get(url .. npage)
  265.             local code = tmpF:readAll()
  266.             tmpF:close()
  267.             f = io.open(saveas , "w")
  268.             f:write(code)
  269.             f:close()
  270.             return(url)
  271.         end
  272.     elseif dr == "run" then
  273.         local tmpF = http.get(url .. npage)
  274.         local code = tmpF:readAll()
  275.         tmpF:close()
  276.         f = io.open("/.tmprun" , "w")
  277.         f:write(code)
  278.         f:close()
  279.         term.clear()
  280.         term.setCursorPos(1, 1)
  281.         shell.run("/.tmprun")
  282.         x, y = term.getCursorPos()
  283.         if x > 6 then
  284.             if y == 18 then
  285.                 term.setCursorPos(1, y)
  286.                 term.clearLine()
  287.             else
  288.                 term.setCursorPos(1, y+1)
  289.             end
  290.         end
  291.         print("Program ended. Press any key to continue")
  292.         os.pullEvent("key")
  293.         return(url)
  294.     else
  295.     return url
  296.     end
  297. else
  298. if newpage == "Parent Directory" then
  299. return(start .. lPData)
  300. elseif newpage == "Press Q to Exit" then
  301. return("exitcode")
  302. else
  303. npage = gethref(newpage)
  304. return(url .. npage)
  305. end
  306. end
  307. end
  308.  
  309. finish = loadPage(home)
  310. while true do
  311. if finish == "exitcode" then
  312. term.clear()
  313. term.setCursorPos(1,1)
  314. break
  315. end
  316. finish = loadPage(finish)
  317. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement