Advertisement
PaymentOption

Apache

Aug 26th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.47 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. -- THE FOLLOWING CODE WAS WRITTEN BY XXM0DZXX --
  21. local WebsiteDatabase = "http://www.ccnet.netau.net/php/folder/" --Folder with all the sites
  22. local ApiPath = "http://www.ccnet.netau.net/php/mcmain.php" --Path API is stored
  23.  
  24. function upload(path, domain, debug, user, pass)
  25.     if user and pass then else
  26.         user = "guest"
  27.         pass = "guest"
  28.     end
  29.    
  30.     if fs.exists(path) then
  31.         file = fs.open(path, "r")
  32.         if file then
  33.             data = file.readAll()
  34.             file.close()
  35.             if not data then
  36.                 print("Failed to read " ..path)
  37.                 return
  38.             end
  39.             local response = http.post(
  40.             ApiPath,
  41.             "type=upload&"..
  42.             "user=guest&"..
  43.             "pass=guest&"..
  44.             "name=".. textutils.urlEncode(domain) .. "&"..
  45.             "data=".. textutils.urlEncode(data)
  46.             )
  47.                        
  48.             if response then
  49.                 local sResponse = response.readAll()
  50.                 response.close()
  51.                 print("Done!")
  52.                 print("Log: ")
  53.                 sleep(0.5)
  54.                 if sResponse == "success" then
  55.                     if debug then
  56.                         print("Uploading " ..shell.resolve(path).. " complete!")
  57.                         print("Errors: 0")
  58.                         print("\nGo to " ..domain.. " to check the site!")
  59.                     end
  60.                     return sResponse
  61.                 else
  62.                     if debug then
  63.                         print("Failed : " ..sResponse)
  64.                     end
  65.                     return sResponse
  66.                 end
  67.             else
  68.                 if debug then
  69.                     print("Failed to connect to database.")
  70.                 end
  71.             end
  72.         else
  73.             if debug then
  74.                 print("Failed to open " ..path)
  75.             end
  76.         end
  77.     else
  78.         if debug then
  79.             print(path.. " doesn't exist.")
  80.         end
  81.     end
  82. end
  83.  
  84. function delete(path, user, pass)
  85.     local response = http.post(
  86.     ApiPath,
  87.     "type=delete&"..
  88.     "user=" ..user.. "&"..
  89.     "pass=" ..pass.. "&"..
  90.     "name=".. textutils.urlEncode(path)
  91.     )
  92.    
  93.     return response.readAll()
  94. end
  95.  
  96. function lister()
  97.     local response = http.post(
  98.     ApiPath,
  99.     "type=list&"..
  100.     "user=guest&"..
  101.     "pass=guest"
  102.     )
  103.    
  104.     return response.readAll()
  105. end
  106. ------------------------------------------------
  107.  
  108. -- CODE WRITTEN BY PAYMENTOPTION --
  109. local tMenuItems = { [1] = { Title = "Upload" }, [2] = { Title = "Delete" } } -- These are the extra menu options to be added.
  110. -- ^^ The above table holds a small menu that can be accessed anytime while connected.
  111. local nMenuSelection = 1 -- This is the current selection in the sub menu that contains the 'upload' and 'delete' options.
  112. local ScreenWidth, ScreenHeight = term.getSize() -- The x and y dimensions of the scree we're operating on.
  113.  
  114. function DrawMenuItems() -- Draws the 'upload' and 'delete' menu.
  115.     -- Draw the menu in the lower left hand corner of the screen.
  116.     term.setCursorPos( 1, ScreenHeight )
  117.    
  118.     for i=1, #tMenuItems do -- Iterate through the menu that is stored in the tMenuItems table.
  119.         if nMenuSelection == i then -- If the current item of this iteration is selected.
  120.             term.write( "[" .. tMenuItems[i].Title .. "] " ) -- Draw the menu item that is currently selected with brackets around it.
  121.         else -- If the current item of this iteration is not selected.
  122.             term.write( " " .. tMenuItems[i].Title .. "  " ) -- Draw the menu item that is not currently selection without brackets around it.
  123.         end
  124.     end
  125. end
  126.  
  127. function DrawUploadMenu() -- Draws the menu presented when the upload option is activated.
  128.     term.setCursorPos( 1, ScreenHeight )
  129.     term.write( string.rep( " ", string.len( "[Upload] Delete  " ) ) ) -- Clear the line of all previous selections.
  130.     -- Reposition the cursor after clearing the line.
  131.     term.setCursorPos( 1, ScreenHeight )
  132.     term.write( "File to upload: " )
  133.    
  134.     local UploadPath = shell.resolve( tostring( read() ) ) -- Get the absolute path of the file selected.
  135.    
  136.     if fs.exists( UploadPath ) then -- If the file path entered exists.
  137.         -- Clear the line of the previously entered information.
  138.         term.setCursorPos( 1, ScreenHeight )
  139.         term.write( string.rep( " ", string.len( "File to upload: " ) + string.len( UploadPath ) ) )
  140.         -- Get the username for the apache server.
  141.         term.setCursorPos( 1, ScreenHeight )
  142.         term.write( "Username: " )
  143.         local UserName = tostring( read() )
  144.         -- Reclear the line to get the password.
  145.         term.setCursorPos( 1, ScreenHeight )
  146.         term.write( string.rep( " ", string.len( "Username: " ) + string.len( UserName ) ) )
  147.         -- Get the password for the apache server.
  148.         term.setCursorPos( 1, ScreenHeight )
  149.         term.write( "Password: " )
  150.         local PassWord = tostring( read() )
  151.         -- Reclear the line for later usage.
  152.         term.setCursorPos( 1, ScreenHeight )
  153.         term.write( string.rep( " ", string.len( "Password: " ) + string.len( PassWord ) ) )
  154.        
  155.         -- If the uploading was succesfull.
  156.         if upload( UploadPath, WebsiteDatabase, false, UserName, Password ) == "succes" then -- Upload the selected file to the current directory.
  157.             term.setCursorPos( 1, ScreenHeight )
  158.             term.write( "Success!" )
  159.             sleep( 0.8 )
  160.             -- Clear the line of the success message.
  161.             term.setCursorPos( 1, ScreenHeight )
  162.             term.write( string.rep( " ", string.len( "Success!" ) ) )
  163.         -- If the uploading was unsuccessfull.
  164.         else
  165.             term.setCursorPos( 1, ScreenHeight )
  166.             term.write( "Failure!" )
  167.             sleep( 0.8 )
  168.             -- Clear the line of the failure message.
  169.             term.setCursorPos( 1, ScreenHeight )
  170.             term.write( string.rep( " ", string.len( "Failure!" ) ) )
  171.         end
  172.     else -- If the file path entered does not exist.
  173.         -- Clear the line of the previously entered information.
  174.         term.setCursorPos( 1, ScreenHeight )
  175.         term.write( string.rep( " ", string.len( "File to upload: " ) + string.len( UploadPath ) ) )
  176.         -- Draw the failure message due to an invalid file path.
  177.         term.setCursorPos( 1, ScreenHeight )
  178.         term.write( "File does not exist." )
  179.         sleep( 0.8 )
  180.         -- Clear the line of the failure message.
  181.         term.setCursorPos( 1, ScreenHeight )
  182.         term.write( string.rep( " ", string.len( "File does not exist." ) ) )
  183.     end
  184. end
  185.  
  186. function DrawDeleteMenu()
  187.     term.setCursorPos( 1, ScreenHeight )
  188.     term.write( string.rep( " ", string.len( "[Upload] Delete  " ) ) ) -- Clear the line of all previous selections.
  189.     -- Reposition the cursor after clearing the line.
  190.     term.write( "File to delete: " )
  191.    
  192.     local DeletionPath = read() -- Get the filename that we'll be deleting.
  193.    
  194.     -- Clear the line.
  195.     term.setCursorPos( 1, ScreenHeight )
  196.     term.write( string.rep( " ", string.len( "File to delete: " ) + string.len( DeletionPath ) ) )
  197.     -- Get the username for the apache server.
  198.     term.write( "Username: " )
  199.     local UserName = read()
  200.     -- Clear the line.
  201.     term.setCursorPos( 1, ScreenHeight )
  202.     term.write( string.rep( " ", string.len( "Username: " ) + string.len( UserName ) ) )
  203.     -- Get the password for the apache server.
  204.     term.write( "Password: " )
  205.     local PassWord = read()
  206.    
  207.     -- Attempt to delete the file.
  208.     -- Clear the line.
  209.     term.setCursorPos( 1, ScreenHeight )
  210.     term.write( string.rep( " ", string.len( "Password: " ) + string.len( PassWord ) ) )
  211.     -- Inform the user of the attempt.
  212.     term.setCursorPos( 1, ScreenHeight )
  213.     term.write( "Deletion attempted." )
  214.     sleep( 0.8 )
  215.     -- Clear the line.
  216.     term.setCursorPos( 1, ScreenHeight )
  217.     term.write( string.rep( " ", string.len( "Deletion attempted." ) ) )
  218. end
  219.  
  220. function HandleKeyPress( Key )
  221.     if Key == 205 then -- Right key.
  222.         if nMenuSelection < #tMenuItems then
  223.             nMenuSelection = nMenuSelection + 1
  224.         end
  225.     elseif Key == 203 then -- Left key.
  226.         if nMenuSelection > 1 then
  227.             nMenuSelection = nMenuSelection - 1
  228.         end
  229.     elseif Key == 43 then -- Enter key.
  230.         if nMenuSelection == 1 then -- If the selection is on 'upload'.
  231.             DrawUploadMenu()
  232.         elseif nMenuSelection == 2 then -- If the selection is on 'delete'.
  233.             DrawDeleteMenu()
  234.         end
  235.     end
  236. end
  237. -----------------------------------
  238.  
  239. -- THE REST WAS WRITTEN BY 1LANN AND EPIC CODERZ --
  240. function welcomeSelect(wSel)
  241.     if wSel == 1 then
  242.         term.setCursorPos(5, 8)
  243.         term.clearLine()
  244.         write(welcome1)
  245.         event, key = os.pullEvent("key")
  246.         if key == 205 then
  247.             return welcomeSelect(2)
  248.         elseif key == 28 then
  249.             return 1
  250.         else
  251.             return welcomeSelect(1)
  252.         end
  253.     elseif wSel == 2 then
  254.         term.setCursorPos(5, 8)
  255.         term.clearLine()
  256.         write(welcome2)
  257.         event, key = os.pullEvent("key")
  258.         if key == 203 then
  259.             return welcomeSelect(1)
  260.         elseif key == 205 then
  261.             return welcomeSelect(3)
  262.         elseif key == 28 then
  263.             return 2
  264.         else
  265.             return welcomeSelect(2)
  266.         end
  267.     elseif wSel == 3 then
  268.         term.setCursorPos(5, 8)
  269.         term.clearLine()
  270.         write(welcome3)
  271.         event, key = os.pullEvent("key")
  272.         if key == 203 then
  273.             return welcomeSelect(2)
  274.         elseif key == 205 then
  275.             return welcomeSelect(4)
  276.         elseif key == 28 then
  277.             return 3
  278.         else
  279.             return welcomeSelect(3)
  280.         end
  281.     elseif wSel == 4 then
  282.         term.setCursorPos(5, 8)
  283.         term.clearLine()
  284.         write(welcome4)
  285.         event, key = os.pullEvent("key")
  286.         if key == 203 then
  287.             return welcomeSelect(3)
  288.         elseif key == 28 then
  289.             return 4
  290.         else
  291.             return welcomeSelect(4)
  292.         end
  293.     end
  294. end
  295.  
  296.  
  297.  
  298. function debug(msg)
  299.     term.setCursorPos(1,1)
  300.     term.clearLine()
  301.     write(msg)
  302.     sleep(2)
  303. end
  304.  
  305. local function contains(string, pattern)
  306.     local a = string.find(string, pattern)
  307.     if a ~= nil then
  308.         return true
  309.     else
  310.         return false
  311.     end
  312. end
  313.  
  314. local function downloadrun(side)
  315. term.setCursorPos(1, 1)
  316. term.clearLine()
  317. if side == "state1" then
  318.     write(state1)
  319.     event, key = os.pullEvent("key")
  320.     if key == 205 then
  321.         return downloadrun("state2")
  322.     elseif key == 28 then
  323.         return "download"
  324.     else
  325.         return downloadrun("state1")
  326.     end
  327. elseif side == "state2" then
  328.     write(state2)
  329.     event, key = os.pullEvent("key")
  330.     if key == 203 then
  331.         return downloadrun("state1")
  332.     elseif key == 205 then
  333.         return downloadrun("state3")
  334.     elseif key == 28 then
  335.         return "run"
  336.     else
  337.         return downloadrun("state2")
  338.     end
  339. elseif side == "state3" then
  340.     write(state3)
  341.     event, key = os.pullEvent("key")
  342.     if key == 203 then
  343.         return downloadrun("state2")
  344.     elseif key == 28 then
  345.         return "cancel"
  346.     else
  347.         return downloadrun("state3")
  348.     end
  349. end
  350. end
  351.  
  352. local function drawGUI(items, scroll, selection, title, types)
  353.     while true do
  354.         term.clear()
  355.         term.setCursorPos(1,1)
  356.         write(title)
  357.         term.setCursorPos(1,2)
  358.         write("=================================================")
  359.         for i = 1+scroll, #types do
  360.             term.setCursorPos(45, (i+2)-scroll)
  361.             write(types[i])
  362.         end
  363.         for d = 1+scroll, #items do
  364.             term.setCursorPos(3, (d+2)-scroll)
  365.             write(items[d])
  366.         end
  367.         local length = string.len(items[selection])
  368.         term.setCursorPos(2, (selection+2)-scroll)
  369.         term.write("[")
  370.         term.setCursorPos(3+length, (selection+2)-scroll)
  371.         term.write("]")
  372.        
  373.         -- PAYMENT'S CODE --
  374.         DrawMenuItems()
  375.         --------------------
  376.        
  377. event, key = os.pullEvent("key")
  378.  
  379. -- PAYMENT'S CODE --
  380. HandleKeyPress( key )
  381. --------------------
  382.  
  383. if key == 208 then
  384. term.setCursorPos(2, (selection+2)-scroll)
  385. term.write(" ")
  386. term.setCursorPos(3+length, (selection+2)-scroll)
  387. term.write(" ")
  388.     if selection == #items then
  389.     elseif selection == scroll+16 then
  390.         selection = selection+1
  391.         scroll = scroll+1
  392.     else
  393.         selection = selection+1
  394.     end
  395. elseif key == 200 then
  396. term.setCursorPos(2, (selection+2)-scroll)
  397. term.write(" ")
  398. term.setCursorPos(3+length, (selection+2)-scroll)
  399. term.write(" ")
  400.     if selection == 1 then
  401.     elseif selection == scroll+1 then
  402.         selection = selection-1
  403.         scroll = scroll-1
  404.     else
  405.         selection = selection-1
  406.     end
  407. elseif key == 28 then  
  408.     return(items[selection])
  409. elseif key == 16 then
  410.     sleep(0.2)
  411.     return("Press Q to Exit")
  412. else
  413. end
  414. end
  415. end
  416.  
  417. local function serverDrawGUI(items, scroll, selection, title)
  418.     while true do
  419.         term.clear()
  420.         term.setCursorPos(1,1)
  421.         write(title)
  422.         term.setCursorPos(1,2)
  423.         write("=================================================")
  424.         for d = 1+scroll, #items do
  425.             term.setCursorPos(3, (d+2)-scroll)
  426.             write(items[d])
  427.         end
  428.         local length = string.len(items[selection])
  429.         term.setCursorPos(2, (selection+2)-scroll)
  430.         term.write("[")
  431.         term.setCursorPos(3+length, (selection+2)-scroll)
  432.         term.write("]")
  433.         event, key = os.pullEvent("key")   
  434. if key == 208 then
  435. term.setCursorPos(2, (selection+2)-scroll)
  436. term.write(" ")
  437. term.setCursorPos(3+length, (selection+2)-scroll)
  438. term.write(" ")
  439.     if selection == #items then
  440.     elseif selection == scroll+16 then
  441.         selection = selection+1
  442.         scroll = scroll+1
  443.     else
  444.         selection = selection+1
  445.     end
  446. elseif key == 200 then
  447. term.setCursorPos(2, (selection+2)-scroll)
  448. term.write(" ")
  449. term.setCursorPos(3+length, (selection+2)-scroll)
  450. term.write(" ")
  451.     if selection == 1 then
  452.     elseif selection == scroll+1 then
  453.         selection = selection-1
  454.         scroll = scroll-1
  455.     else
  456.         selection = selection-1
  457.     end
  458. elseif key == 28 then  
  459.     return(items[selection])
  460. elseif key == 16 then
  461.     sleep(0.2)
  462.     return("exit")
  463. elseif key == 19 and selection ~= #items then
  464.     return "remove", selection
  465. end
  466. end
  467. end
  468.  
  469. local function process()
  470.     local f= io.open("/.tmpdata", "r")
  471.     f:read("*l")
  472.     f:read("*l")
  473.     f:read("*l")
  474.     f:read("*l")
  475.     f:read("*l")
  476.     f:read("*l")
  477.     local data = f:read("*l")
  478.     if data == nil then
  479.     term.setCursorPos(1,1)
  480.     term.clearLine()
  481.     print("Not an Apache File Server! [Ok]")
  482.     os.pullEvent("key")
  483.     return "error"
  484.     end
  485.     local titlestart = nil
  486.     local derp = nil
  487.     if string.find(data, "<h1>") == nil then
  488.     term.setCursorPos(1,1)
  489.     term.clearLine()
  490.     print("Not an Apache File Server! [Ok]")
  491.     os.pullEvent("key")
  492.     return "error"
  493.     end
  494.     titlestart = (string.find(data, "<h1>")+4)
  495.     local titleend = (string.find(data, "</h1>")-1)
  496.     title = string.sub(data, titlestart, titleend)
  497.     items = {}
  498.     types = {}
  499.     href = {}
  500.     local line = f:read("*l")
  501.     apachev = 1
  502.     if contains(line, "<table><tr><th>") then
  503.         apachev = 2
  504.         line = f:read("*l")
  505.     end
  506.     while contains(line, "<li>") or contains(line, "</td><td>") do
  507.         rStart = (string.find(line, "<a href=")-4)
  508.         lStart = string.find(line, "\"> ", rStart)
  509.         if apachev == 2 then
  510.             lStart = (string.find(line, "\">", rStart) + 2)
  511.         else
  512.             lStart = (string.find(line, "\"> ", rStart)+3)
  513.         end
  514.         lEnd = (string.find(line, "</a>", rStart) - 1)
  515.         lData = string.sub(line, lStart, lEnd)
  516.         table.insert(items, lData)
  517.         hStart = (string.find(line, "<a href=\"", rStart) + 9)
  518.         hEnd = (string.find(line, "\">", rStart) - 1)
  519.         hData = string.sub(line, hStart, hEnd)
  520.         table.insert(href, hData)
  521.         if contains(lData, "Parent Directory") then
  522.             lPStart = (string.find(line, "<a href=\"", rStart) + 9)
  523.             lPEnd = (string.find(line, "\">", rStart) - 1)
  524.             lPData = string.sub(line, lPStart, lPEnd)
  525.             table.insert(types, "back")
  526.             elseif contains(lData, "/") then
  527.                 table.insert(types, "dir")
  528.             else
  529.                 table.insert(types, "file")
  530.             end
  531.             line = f:read("*l")
  532.         end
  533.         return title, items, types
  534.     end
  535.  
  536. local function isFile(filename, items, types)
  537.     for i = 1, #items do
  538.         if filename == items[i] and types[i] == "file" then
  539.             return true
  540.         end
  541.     end
  542.     return false
  543. end
  544.  
  545. local function gethref(ref)
  546.     for i = 1, #items do
  547.         if items[i] == ref then
  548.             return href[i]
  549.         end
  550.     end
  551. end
  552.  
  553.  
  554. local function loadPage(url)
  555.     term.setCursorBlink(false)
  556.     term.setCursorPos(1, 1)
  557.     term.clearLine()
  558.     write("Connecting...")
  559.     http.request(url)
  560.     while true do
  561.     event, a, code = os.pullEvent()
  562.     if event == "http_failure" then
  563.     term.setCursorPos(1,1)
  564.     term.clearLine()
  565.     print("Connection failed! [Ok]")
  566.         os.pullEvent("key")
  567.         return "error"
  568.     elseif event == "http_success" then
  569.     break
  570.     elseif event == "key" and a == 16 then
  571.     return "error"
  572.     end
  573.     end
  574.     if code == nil then
  575.         term.setCursorPos(1, 1)
  576.         term.clearLine()
  577.         print("Page not found! [Ok]")
  578.         os.pullEvent("key")
  579.         return "error"
  580.     end
  581.     f = io.open("/.tmpdata", "w")
  582.     f:write(code:readAll())
  583.     f:close()
  584.     code:close()
  585.     title, items, types = process()
  586.     if title == "error" then
  587.     return "error"
  588.     end
  589.     newpage = drawGUI(items, 0, 1, title, types)
  590.     if isFile(newpage, items, types) then
  591.         npage = gethref(newpage)
  592.         dr = downloadrun("state1")
  593.         if dr == "download" then
  594.             term.setCursorPos(1, 1)
  595.             term.clearLine()
  596.             write("Download as: ")
  597.             saveas = read()
  598.             if contains(saveas, " ") or saveas == "" then
  599.                 return(url)
  600.             else
  601.                 if contains(saveas, "/") then
  602.                     local reverse = string.reverse(saveas)
  603.                     local fDir = (string.len(saveas) - string.find(reverse, "/"))
  604.                     local cDir = string.sub(saveas, 1, fDir)
  605.                     if fs.exists(cDir) then
  606.                         local tmpF = http.get(url .. npage)
  607.                         local code = tmpF:readAll()
  608.                         tmpF:close()
  609.                         f = io.open(saveas , "w")
  610.                         f:write(code)
  611.                         f:close()
  612.                         return(url)
  613.                     else
  614.                         term.setCursorPos(1,1)
  615.                         term.clearLine()
  616.                         write("Directory does not exist! Press any key...")
  617.                         os.pullEvent("key")
  618.                         return(url)
  619.                     end
  620.                 end
  621.                 local tmpF = http.get(url .. npage)
  622.                 local code = tmpF:readAll()
  623.                 tmpF:close()
  624.                 f = io.open(saveas , "w")
  625.                 f:write(code)
  626.                 f:close()
  627.                 return(url)
  628.             end
  629.             elseif dr == "run" then
  630.                 local tmpF = http.get(url .. npage)
  631.                 local code = tmpF:readAll()
  632.                 tmpF:close()
  633.                 f = io.open("/.tmprun" , "w")
  634.                 f:write(code)
  635.                 f:close()
  636.                 term.clear()
  637.                 term.setCursorPos(1, 1)
  638.                 shell.run("/.tmprun")
  639.                 x, y = term.getCursorPos()
  640.                 if x > 6 then
  641.                     if y == 18 then
  642.                         term.setCursorPos(1, y)
  643.                         term.clearLine()
  644.                     else
  645.                         term.setCursorPos(1, y+1)
  646.                     end
  647.                 end
  648.                 print("Program ended. Press any key to continue")
  649.                 os.pullEvent("key")
  650.                 return(url)
  651.             else
  652.                 return url
  653.             end
  654.         else
  655.             if newpage == "Parent Directory" then
  656.                 return(start .. lPData)
  657.                 elseif newpage == "Press Q to Exit" then
  658.                     return("exitcode")
  659.                 else
  660.                     npage = gethref(newpage)
  661.                     return(url .. npage)
  662.                 end
  663.             end
  664.         end
  665.  
  666.  
  667. function main()
  668.     start = string.sub(home, 1, string.len(home)-1)
  669.     f = io.open("/.previous", "w")
  670.     f:write(home)
  671.     f:close()
  672.     rhome = home
  673.     hTest = string.gsub(home, "http://", "")
  674.     a, b = string.find(hTest, "/")
  675.     if b ~= nil then home = string.sub(home, 1, a+7)
  676.     start = string.sub(home, 1, string.len(home)-1)
  677.     end
  678.     finish = loadPage(rhome)
  679.     if finish == "error" then
  680.         return "error"
  681.     end
  682.     while true do
  683.         if finish == "exitcode" then
  684.             term.clear()
  685.             welcome()
  686.             break
  687.         end
  688.         finish = loadPage(finish)
  689. if finish == "error" then
  690.         return "error"
  691.     end
  692.     end
  693. end
  694.  
  695. function removeSel(rSel)
  696. if rSel == 1 then
  697.     term.setCursorPos(1,1)
  698.     term.clearLine()
  699.     write(remove1)
  700.     event, key = os.pullEvent("key")
  701.     if key == 205 then
  702.         return removeSel(2)
  703.     elseif key == 28 then
  704.         return true
  705.     else
  706.         return removeSel(1)
  707.     end
  708. elseif rSel == 2 then
  709.     term.setCursorPos(1,1)
  710.     term.clearLine()
  711.     write(remove2)
  712.     event, key = os.pullEvent("key")
  713.     if key == 203 then
  714.         return removeSel(1)
  715.     elseif key == 28 then
  716.         return false
  717.     else
  718.         return removeSel(2)
  719.     end
  720. end
  721. end
  722.  
  723. function saveList(fsList)
  724. f = io.open("/.serverlist", "w")
  725. toWrite = textutils.serialize(fsList)
  726. f:write(toWrite)
  727. f:close()
  728. end
  729.  
  730. function serverList()
  731. if not fs.exists("/.serverlist") then
  732. f = io.open("/.serverlist", "w")
  733. serTable = textutils.serialize({"Add new server"})
  734. serverNew = true
  735. f:write(serTable)
  736. f:close()
  737. end
  738. f = io.open("/.serverlist", "r")
  739. serStart = f:read("*l")
  740. f:close()
  741. serList = textutils.unserialize(serStart)
  742. if type(serList) == "table" then
  743. local toRemove = nil
  744. serReturn, toRemove = serverDrawGUI(serList, 0, 1, "Server List")
  745. if serReturn == "Add new server" then
  746.     term.setCursorPos(1, 1)
  747.     write("Add: http://")
  748.     local newAddress = read()
  749.     start = ("http://" .. newAddress)
  750.     if string.sub(string.len(start), string.len(start)) == "/" then
  751.         home = start
  752.     else
  753.         home = (start .. "/")
  754.     end
  755.     table.insert(serList, 1, home)
  756.     saveList(serList)
  757.     return serverList()
  758. elseif serReturn == "remove" then
  759.     rRemove = removeSel(1)
  760.     if rRemove then table.remove(serList, toRemove)
  761.     saveList(serList)
  762.     end
  763.     return serverList()
  764. elseif serReturn == "exit" then
  765. term.clear()
  766. welcome()
  767. else
  768.     home = serReturn
  769.     if main() == "error" then
  770.     return "error"
  771.     end
  772.        
  773. end
  774. else
  775. term.setCursorPos(1, 1)
  776. term.clearLine()
  777. print("Server list is corrupted! [Ok]")
  778. os.pullEvent("key")
  779. return "error"
  780. end
  781. end
  782.  
  783. function welcome()
  784. term.clear()
  785. term.setCursorPos(1, 1)
  786. print("Welcome!")
  787. print("=================================================")
  788. print("Use the arrow keys to navigate")
  789. print("Press enter to download/run files or open folders")
  790. print("Press \"Q\" to exit the browser")
  791. print("Press \"R\" to remove servers in the server list")
  792. print(" ")
  793. welRe = welcomeSelect(1)
  794. if welRe == 1 then
  795.     if not fs.exists("/.previous") then
  796.     f = io.open("/.previous", "w")
  797.     f:write("http://previous")
  798.     f:close()
  799.     end
  800.     f = io.open("/.previous", "r")
  801.     home = f:read("*l")
  802.     f:close()
  803.     if main() == "error" then return
  804. welcome() end
  805. elseif welRe == 2 then
  806.     term.setCursorPos(1, 8)
  807.     term.clearLine()
  808.     print("Server Address:")
  809. write("http://")
  810. local address = read()
  811. start = ("http://" .. address)
  812. if string.sub(string.len(start), string.len(start)) == "/" then
  813.     home = start
  814. else
  815.     home = (start .. "/")
  816. end
  817. f = io.open("/.previous", "w")
  818. f:write(home)
  819. f:close()
  820. if main() == "error" then return welcome() end
  821. elseif welRe == 3 then
  822. if serverList() == "error" then return welcome() end
  823. else
  824.     term.clear()
  825.     term.setCursorPos(1, 1)
  826.     return "end"
  827. end
  828. end
  829.  
  830. welcome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement