Left4Cake2

firebox

Jan 2nd, 2013
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.55 KB | None | 0 0
  1. local tArgs = { ... }
  2. local favorits = { }
  3.  
  4. --configrable variables
  5. Firebox = shell.getRunningProgram()
  6. edit = "edit" -- textviewing programe for http;
  7. server = nil -- for redbook
  8. install = "" -- for all downloads
  9. dns = 202 -- id of DNS server
  10. DNSids = {201}
  11. DNSdomains = {"www.test.com"}
  12. -- end
  13.  
  14. if #tArgs < 1 then
  15.    print( "Do you want to 1 enter a url, 2:select from 'firebox_fave'" )
  16.    input = read()
  17.    shell.run( "clear" )
  18.    if input == "2" then
  19.     if(fs.exists("firebox_fave")) then
  20.         local fave = fs.open("firebox_fave", "r")
  21.            max = 0
  22.            while true do
  23.          local line = fave.readLine()
  24.          if line == nil then --if reached end of file
  25.   break end
  26.            table.insert(favorits, line)
  27.            max = max + 1
  28.   end
  29. fave.close()
  30.                 i = 0
  31.            print( "Use the arrows and enter to chose.  " )
  32.            while true do
  33.            i = i + 1
  34.            if favorits[i] == nil then break end
  35.            print( "  " .. i .. ": " .. favorits[i])
  36.            end
  37.            pointer = 1
  38.            term.setCursorPos(1, pointer + 1)
  39.            print( "->" )
  40.            while true do
  41.                    local evt, arg1, arg2 = os.pullEvent()
  42.                    term.setCursorPos(1, pointer + 1)
  43.                    print( "  " )
  44.                    if arg1 == 200 and pointer > 1 then
  45.                            pointer = pointer - 1
  46.                    elseif arg1 == 208 and pointer < max then
  47.                            pointer = pointer + 1
  48.                    end
  49.                    term.setCursorPos(1, pointer + 1)
  50.                    print( "->" )
  51.                    if arg1 == 28 then break end
  52.            end
  53.            url = favorits[pointer]
  54.          
  55.    else
  56.    print("'firebox_fave' dose not exist")
  57.    print("To create a favorits list then store each url on its own line in firebox/fave")
  58.    print("")
  59.    print( "Enter a url or type host to host a server." )
  60.    url = read()
  61.    end
  62.    else
  63.            print( "Enter a url or type host to host a server." )
  64.            url = read()
  65.    end
  66. else
  67.   url = tArgs[1]
  68. end
  69. if string.find(url, "192.168.1.") then
  70. -- Connect to rednet
  71. rednetip = string.gsub(url, "192.168.1.", "") * 1
  72. rednet.open("right")
  73. shell.run( "clear" )
  74. rednet.send(rednetip, "http/index")
  75. file = io.open("temp", "w")
  76. id, msg = rednet.receive(20)
  77. if msg == nil then
  78.         print( "The server did not sent a message back. It must not like you." )
  79. else
  80.         file:write(msg)
  81.         file:close()
  82.         response = nil
  83.         shell.run( "temp" )
  84. end
  85. elseif url == "host" then
  86. shell.run( "clear" )
  87. print ( "Server ready to serve." )
  88. rednet.open("right")
  89. print ( "Opening the portholes" )
  90. while true do
  91.   id, message  = rednet.receive()
  92.   if fs.exists(message) then
  93.           file = io.open( message )
  94.           filedata = file:read()
  95.           file:close()
  96.           rednet.send(id, filedata)
  97.           print ( "Send that old trash to 192.168.1." .. id )
  98.   elseif fs.exists("http/error") then
  99.           file = io.open("http/error")
  100.           filedata = file:read()
  101.           file:close()
  102.           rednet.send(id, filedata)
  103.           print ( "Send that old trash to 192.168.1." .. id )
  104.   else
  105.           rednet.send(id, "print( \"Error, This server (192.168.1." .. id .. ") could not sent you your request.\")")
  106.           print ( "Failed to give 192.168.1." .. id  .. " his present." )
  107.   end
  108. end
  109. elseif string.find(url, "http://") then
  110. --Connect to internet
  111.                 local response = http.get(url)
  112.             if response then
  113.                   while response do
  114.                    local sResponse = response.readAll()
  115.                    response.close()
  116.                    shell.run( "clear" )
  117.                    cache = shell.resolve( "temp" )
  118.                    local file = fs.open( cache, "w" )
  119.                    file.write( sResponse )
  120.                    file.close()
  121.                    response = nil
  122.                    shell.run( edit, "temp" )
  123.                   end
  124.                 else
  125.              -- if page is not reseved
  126.                print( "Error, Could not load page." )
  127.                 end
  128. elseif string.find(url, "rdnt://") then
  129. --Connect to Rednet DNS
  130. rednetDomain = string.gsub(url, "rdnt://", "")
  131. rednet.open("right")
  132. shell.run( "clear" )
  133.     rednet.send(dns, rednetDomain)
  134.     file = io.open("temp", "w")
  135.     id, msg = rednet.receive(20)
  136.     if msg == nil then
  137.             print( "The Domain Name Server did not sent a message back. It must have got it self in a fight with the website you are trying to connect to." )
  138.     else
  139.             file:write(msg)
  140.             file:close()
  141.             response = nil
  142.             shell.run( "temp" )
  143.     end
  144. elseif string.find(url, "hostDNS") then
  145. --Host a DNS server
  146.     rednet.open("right")
  147.     print ( "DNS ready to serve." )
  148.     while true do
  149.     client, message  = rednet.receive()
  150.         print( "192.168.1." .. client .. " whats to connect to " .. message )
  151.     i = 1
  152.    
  153.     devidingCharater = string.find(message, "/")
  154.     length  = string.len(message)
  155.     message = string.lower(message)
  156.     Domainname = string.sub(message, devidingCharater, length - devidingCharater)
  157.     subDomain = string.sub(message, devidingCharater, length)
  158.    
  159.     print("request:" .. message)
  160.     print("Domainname:" .. Domainname)
  161.    
  162.     print("subDomain:" .. subDomain)
  163.     while DNSids[i] do
  164.         if(message == DNSdomains[i]) then
  165.             rednet.send(DNSids[i], "http/" .. subDomain)
  166.                 print( "Trying to find 192.168.1." .. client .. " to 192.168.1." .. DNSids[i])
  167.                 id, msg = rednet.receive(20)
  168.                 if msg == nil then
  169.                         print( "All as been lost." )
  170.                 else
  171.                         rednet.send(client, msg)  
  172.                         print( "Found him. Matching up." )
  173.                 end
  174.         end
  175.     i = i + 1
  176.     end
  177.     end
  178.  
  179. else
  180. -- Connect to Craftnet
  181.          --  root = "http://localhost/minecraft/"
  182.            root = "http://www.lozengia.com/tmas/Downloads/Minecraft/firebox/"
  183.         local response = http.get(root .. url)
  184.         if response then
  185.         -- if page is reseved
  186.            while response do
  187.                    local sResponse = response.readAll()
  188.                    response.close()
  189.                    shell.run( "clear" )
  190.                    cache = shell.resolve( "temp" )
  191.                    local file = fs.open( cache, "w" )
  192.                    file.write( sResponse )
  193.                    file.close()
  194.                    response = nil
  195.                    shell.run( "temp" )
  196.            end
  197.            shell.run( "clear" )
  198.         else
  199.         -- if page is not reseved
  200.            print( "Error, Could not load page." )
  201.         end
  202. end
Advertisement
Add Comment
Please, Sign In to add comment