Advertisement
xuma202

Chrome

Aug 5th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.56 KB | None | 0 0
  1. local x,y = term.getSize()
  2. local version = "v 1.1"
  3. local website = "home";
  4. if fs.exists(".cache") then fs.delete(".cache") end
  5. fs.makeDir(".cache")
  6. local history = {"", "", "", "", "", "", "", "", "", ""}
  7.  
  8. rednet.open("top")
  9. rednet.open("bottom")
  10. rednet.open("left")
  11. rednet.open("right")
  12. rednet.open("back")
  13. rednet.open("right")
  14.  
  15. function getPastebin(pCode, pLocation)
  16. local sCode = pCode
  17.         local sFile = pLocation
  18. local sPath = pLocation
  19.        
  20.         -- GET the contents from pastebin
  21.         local response = http.get(
  22.                 "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  23.                 )
  24.                
  25.         if response then
  26.                
  27.                 local sResponse = response.readAll()
  28.                 response.close()
  29.                
  30.                 local file = fs.open( sPath, "w" )
  31.                 file.write( sResponse )
  32.                 file.close()
  33.                
  34.         else
  35.         end    
  36. end
  37.  
  38. function isUpdate()
  39.   local response = http.get("http://pastebin.com/raw.php?i=XFRj6NhD")
  40.                
  41.         if response then
  42.             local sResponse = response.readAll()
  43.             response.close()
  44.             if sResponse == version then
  45.                 return false
  46.             end
  47.                 return true
  48.            
  49.         end
  50. end
  51.  
  52. function addHistory(val)
  53.   table.remove(history)
  54.   table.insert(history, 1, val)
  55. end
  56.  
  57. function listHistory()
  58.   for i = 1, 10 do
  59.     print(tostring(i)..". "..history[i])
  60.   end
  61. end
  62.  
  63. function visitHistory(num)
  64.   numI = tonumber(num)
  65.   website = history[numI]
  66.   loadWebpage()
  67. end
  68.  
  69. function isDangerous(msg)
  70.  a = string.find(msg, "startup")
  71.  if a == nil then return false end
  72.  return true
  73. end
  74.  
  75. function savePageToFile(addr, file, check)
  76.     rednet.broadcast(addr)
  77.  
  78.     id, message = rednet.receive(0.8)
  79.          if message == nil then
  80.            return
  81.          end
  82.       if isDangerous(message) == true and check == true then
  83.         print("The webpage will meight modify your startup")
  84.         print("continue anyways")
  85.         print("Y = YES N = NO")
  86.        
  87.         while true do
  88.             e, k = os.pullEvent("char")
  89.             if k == "y" or k == "n" then
  90.                 break
  91.             end
  92.         end
  93.        
  94.           if k == "n" then return end
  95.     end
  96.    
  97.    
  98.     f = fs.open(file, "w")
  99.     f.write(message)
  100.     f.close()
  101.    
  102. end
  103.  
  104. local cPrint = function(text)
  105.     local x2,y2 = term.getCursorPos()
  106.     term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  107.    
  108.     print(text)
  109. end
  110.  
  111. function reDirect(url)
  112.     website = url
  113.     loadWebpage()
  114. end
  115.  
  116. function createSite(websitename, crawl, keys) --OPENNEXISGATES
  117.                     print("Warning this will overwrite your startup")
  118.                     print("continue anyways")
  119.                     print("Y = YES N = NO")
  120.        
  121.                     while true do
  122.                         e, k = os.pullEvent("char")
  123.                         if k == "y" or k == "n" then
  124.                             break
  125.                         end
  126.                     end
  127.        
  128.                     if k == "n" then return end
  129.  
  130.                     fs.delete("startup")
  131.                     startup = fs.open("startup", "w")
  132.                     startup.writeLine("websitename = \"" ..websitename.. "\"")
  133.                     servercode = [[
  134.                        
  135.                         local enableSearching = "]]..tostring(crawl)..[["
  136.                         local keys = "]]..keys.. [["
  137.                         function record(text)
  138.                             print(text)
  139.                             log = fs.open("rednet.log", "w")
  140.                             log.writeLine(text)
  141.                             log.close()
  142.                         end
  143.                        
  144.                         local x,y = term.getSize()
  145.                         local cPrint = function(text)
  146.                             local x2,y2 = term.getCursorPos()
  147.                             term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  148.                            
  149.                             print(text)
  150.                         end
  151.                        
  152.                         rednet.open("top")
  153. rednet.open("left")
  154. rednet.open("right")
  155. rednet.open("back")
  156. rednet.open("front")
  157. rednet.open("bottom")
  158.                         term.clear()
  159.                         term.setCursorPos(1,1)
  160.                         print("Hosting " ..websitename.. "...")
  161.                         print("Press \"E\" to edit site")
  162.                         while true do
  163.                         test = io.open(websitename, "r")
  164.                         fileContents = test:read("*a")
  165.                         test:close()
  166.                         term.setCursorPos(1,1)
  167.                         print("Hosting " ..websitename.. "...")
  168.                         print("Press \"E\" to edit site")
  169.                             event, id, message = os.pullEvent()
  170. if event == "key" and id == 18 then
  171. sleep(0.2)
  172. shell.run("/rom/programs/edit", "/" .. websitename)
  173. elseif event == "rednet_message" then
  174.                             if message == websitename then
  175.                                 record("   [" ..os.time().."] [" ..id.. "] Pinged Website.")
  176. for i = 1, 10 do
  177.                                 rednet.send(id, fileContents)
  178. end
  179.                                 record("   [" ..os.time().."] [" ..id.. "] Received Data")
  180.                             elseif message == "rednet.api.ping.searchengine" and enableSearching == true then
  181.                                 rednet.send(id, websitename)
  182.                                 record("   [" ..os.time().."] [" ..id.. "] Searched by " .. id)
  183.                             elseif message == "rednet.api.website.keys"  and enableSearching == true then
  184.                                 rednet.send(id, keys)
  185.                                 record("   [" ..os.time().."] [" ..id.. "] Keywords req by " .. id)
  186.                             end
  187. end
  188.                         end
  189.                     ]]
  190.                    
  191.                     startup.writeLine(servercode)
  192.                     startup.close()
  193.                     os.reboot()
  194. end
  195.  
  196. local Address = function()
  197.     text = "rdnt://"
  198.     term.setCursorPos(1,2)
  199.     term.clearLine()
  200.    
  201.     write("rdnt://")
  202.     website = read()
  203.     loadWebpage()
  204. end
  205.  
  206. function done()
  207.     term.setCursorPos(1, y)
  208.     name = "F5 = Refresh"
  209.     write("Press CTRL to travel the web! :D")
  210.     term.setCursorPos(x - name:len(), y)
  211.     write(name)
  212.    
  213.     while true do
  214.         sleep(0) -- stop crashing
  215.         e, k = os.pullEvent("key")
  216.         if k == 63 then
  217.             loadWebpage()
  218.             break
  219.         elseif k == 29 then
  220.             Address()
  221.             break
  222.         end
  223.     end
  224. end
  225.    
  226.  
  227. loadWebpage = function()
  228.     addHistory(website)
  229.     local title = "Google Chrome "..version
  230.     term.clear()
  231.     term.setCursorPos(1,1)
  232.     cPrint(title)
  233.     cPrint("rdnt://" ..website.. "\n")
  234.     if website == "home" then
  235.     term.clear()
  236.     term.setCursorPos(1,1)
  237.     cPrint(title)
  238.     cPrint("rdnt://" ..website.. "\n")
  239.     print("Welcome to Google Chrome!")
  240.     print("by xuma")
  241.     print("visit rdnt://chrome:help to find out about Chrome")
  242.     print("you can go to rdnt://chrome:exit to close Chrome")
  243.     print("To host a website visit rdnt://chrome:new")
  244.    
  245.     elseif website == "chrome:exit" then
  246.     term.clear()
  247.     term.setCursorPos(1,1)
  248.     print("Thanks for using Chrome!")
  249.     error()
  250.    
  251.     elseif website == "chrome:help" then
  252.     print("List of special pages:")
  253.     print("home ,shows the main page")
  254.     print("chrome:help ,shows this page")
  255.     print("chrome:exit ,will exit chrome")
  256.     print("chrome:new ,allows you to setup a new webpage")
  257.     print("chrome:settings ,allows you to make settings")
  258.     print("chrome:history ,shows your history")
  259.     print("chrome:save ,allows you to save a webpage to a file")
  260.     print("chrome:version ,information about the verion of Chorme you use")
  261.    
  262.     elseif website == "chrome:new" then
  263.         print("Are you sure you would like to make this PC a server?")
  264.         cPrint("Y = Yes N = No")
  265.         while true do
  266.             e, k = os.pullEvent("char")
  267.             if k == "y" or k == "n" then
  268.                 break
  269.             end
  270.         end
  271.        
  272.         if k == "y" then
  273.             term.clear()
  274.             term.setCursorPos(1,1)
  275.             title = "Chrome Sever " ..version
  276.             cPrint(title)
  277.             print("Welcome to Chrome for servers. Please enter the website name: ")
  278.             websitename = read()
  279.             rednet.broadcast(websitename)
  280.             i, me = rednet.receive(0.1)
  281.             if me == nil then
  282.             else print("WARNING: Another server possibly using this domain")
  283.             end
  284.                 print("Thank you! Do you want your website crawled by search engines?")
  285.                 cPrint("Y = YES N = NO")
  286.                 while true do
  287.                     e, k = os.pullEvent("char")
  288.                     if k == "y" or k == "n" then
  289.                         break
  290.                     end
  291.                 end
  292.                
  293.                   crawl = false
  294.                   keywords = ""
  295.                
  296.                 if k == "y" then
  297.                   print("Great. You can now enter keywords for your site seperated by a ,")
  298.                   keyswords = read()
  299.                   crawl = true
  300.                 end
  301.                            
  302.        
  303.                 print("This website will be running off of the file:\n" ..websitename.. "\n")
  304.                 write("Are you sure? (Y = Continue, E = Edit)")
  305.                 while true do
  306.                 event, key = os.pullEvent("key")
  307.                 if key == 21 or key == 18 then
  308.                 break
  309.                 end
  310.                 end
  311.                 if key == 21 then
  312.                     if fs.exists(websitename) == false then
  313.                         print("Please create " ..websitename.. " first!")
  314.                         sleep(3)
  315.                         shell.run("/rom/programs/edit", "/" .. websitename)
  316.                     end
  317.                     term.clear()
  318.                     term.setCursorPos(1,1)
  319.                     createSite(websitename, crawl, keywords)
  320.                 elseif key == 18 then
  321.                     sleep(0.2)
  322.                     shell.run("/rom/programs/edit", "/" .. websitename)
  323.                     term.clear()
  324.                     term.setCursorPos(1,1)
  325.                     createSite(websitename, crawl, keywords)
  326.                 end
  327.         end
  328.     elseif website == "chrome:settings" then
  329.         term.clear()
  330.         term.setCursorPos(1,1)
  331.         cPrint(title)
  332.         cPrint("rdnt://" ..website.. "\n")
  333.        
  334.         print("No settings to make yet")
  335.        
  336.     elseif website == "chrome:history" then  
  337.         term.clear()
  338.         term.setCursorPos(1,1)
  339.         cPrint(title)
  340.         cPrint("rdnt://" ..website.. "\n")
  341.         print("Last ten pages you've visited: ")
  342.         listHistory()
  343.         print("Enter the number of the page to visit it now or press enter")
  344.         num = read()
  345.         if num ~= "" then
  346.           visitHistory(num)
  347.         end
  348.        
  349.     elseif website == "chrome:save" then
  350.         term.clear()
  351.         term.setCursorPos(1,1)
  352.         cPrint(title)
  353.         cPrint("rdnt://" ..website.. "\n")
  354.         print("Enter the adress of the website: ")
  355.         addr = read()
  356.         savePageToFile(addr, "site_"..addr , false)
  357.         if fs.exists("site_"..addr) == true then
  358.             print("")
  359.             print("The webpage has been saved to the file site_"..addr)
  360.             print("")
  361.             print("Do you want to open the file with the editor now?")
  362.             cPrint("Y = YES N = NO")
  363.             while true do
  364.             e, k = os.pullEvent("char")
  365.             if k == "y" or k == "n" then
  366.                 break
  367.             end
  368.             end
  369.        
  370.             if k == "y" then shell.run("/rom/programs/edit", "/site_" .. addr) end
  371.         term.clear()
  372.                 term.setCursorPos(1,1)
  373.                 cPrint(title)
  374.                 cPrint("rdnt://" ..website.. "\n")
  375.         print("Finished.")
  376.         else
  377.             print("Unable to load website")
  378.         end
  379.        
  380.                
  381.        
  382.     elseif website == "chrome:version" then
  383.         term.clear()
  384.         term.setCursorPos(1,1)
  385.         cPrint(title)
  386.         cPrint("rdnt://" ..website.. "\n")
  387.         print("You are currently using Gogle Chrome"..version)
  388.        
  389.     else
  390.         title = "Google Chrome" ..version
  391.         savePageToFile(website, ".cache/"..website, true)
  392.         if fs.exists(".cache/" ..website) == true then
  393.             term.clear()
  394.             term.setCursorPos(1,1)
  395.             cPrint(title)
  396.             cPrint("rdnt://" ..website.. "\n")
  397.             shell.run(".cache/" ..website)
  398.         else
  399.             term.clear()
  400.             term.setCursorPos(1,1)
  401.             cPrint(title)
  402.             cPrint("rdnt://" ..website.. "\n")
  403.             print("Website unreachable.")
  404.         end
  405.     end
  406.    
  407.     done()
  408. end
  409.  
  410. if isUpdate() then
  411.   cPrint("There is an update available")
  412.   cPrint("Updating...")
  413.   getPastebin("pcanbrn2", "/chrome")
  414.   cPrint("update saved to chrome please restart chrome now.")
  415.   error()  
  416. end
  417. loadWebpage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement