Advertisement
xXm0dzXx

rexConsole v1.0

Nov 8th, 2012
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.88 KB | None | 0 0
  1. ----Configuration----
  2.  
  3. function config() --Don't modify this line
  4.     newURL = "test" --Change this to the website URL, do not add rdnt://
  5.     filePath = "." ..newURL --Default save path, not recommended to change, if you do change this, modify lines 33 and 34
  6.     homePage = filePath.. "/index" --Page returned when requesting a unspecific path, not recommended to change.
  7. end --Don't modify this line
  8.  
  9. ----Don't modify anything below----
  10.  
  11. function stopBlinking( lolk )
  12.     local x,y = term.getSize()
  13.     if #lolk > y then
  14.         oldLolk = lolk
  15.         lolk = {}
  16.         for i=#oldLolk-y,#oldLolk do
  17.             lolk[#lolk+1] = oldLolk[i]
  18.         end
  19.     end
  20.     return lolk
  21. end
  22.  
  23. --How the installer works:
  24. --1) Looks for a file called DONTDELETE
  25. --2) Looks for a file called install, if found it skips to step 5
  26. --3) Scans /serverFiles and puts all the pages inside the host folder
  27. --4) Replaces the server with /serverSoftware
  28. --5) Reruns install/server
  29.  
  30. function install( iPath )
  31.     if fs.exists( iPath ) and fs.isDir( iPath ) and fs.exists( iPath.. "/DONTDELETE" ) then
  32.         if fs.exists( iPath.. "/install" ) then
  33.             shell.run( iPath.. "/install" )
  34.         else
  35.             if fs.exists( iPath.. "/serverFiles" ) and fs.isDir( iPath.. "/serverFiles" ) then
  36.                 local files = fs.list( iPath.. "/serverFiles" )
  37.                 for i=1,#files do
  38.                     if fs.exists( filePath.. "/" ..files[i] ) then
  39.                         messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Failed to install " ..iPath.. "/" ..files[i].. " : Already Exists")
  40.                     else
  41.                         fs.copy( iPath.. "/serverFiles/" ..files[i] , filePath.. "/" ..files[i] )
  42.                     end
  43.                 end
  44.             end
  45.            
  46.             if fs.exists( iPath.. "/serverSoftware" ) and fs.isDir( iPath.. "/serverSoftware" ) ~= true then
  47.                 if fs.isReadOnly( shell.getRunningProgram() ) then
  48.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Failed to install " ..iPath.. "/serverSoftware : Server is in ROM")
  49.                 else
  50.                     fs.delete(shell.getRunningProgram())
  51.                     fs.copy( iPath.. "/serverSoftware", shell.getRunningProgram() )
  52.                     shell.run( shell.getRunningProgram() )
  53.                     error()
  54.                 end
  55.             end
  56.         end
  57.        
  58.         return true
  59.     else
  60.         return false
  61.     end
  62. end
  63.  
  64. function newServer()
  65.     messages = {}
  66.     local x,y = term.getSize()
  67.     local blocked = {}
  68.     local sText = ""
  69.     local downloads = 0
  70.     config()
  71.     function drawFooter()
  72.         term.setCursorPos(1,1)
  73.         term.setTextColour( colors.gray )
  74.         term.clearLine()
  75.         print("Console (rdnt://" ..newURL..")")
  76.         for i=1,x do
  77.             term.setCursorPos(i, 2)
  78.             write("-")
  79.         end
  80.     end
  81.    
  82.     messages[1] = "Starting server..."
  83.     rednet.broadcast( newURL )
  84.     local id,message = rednet.receive( 0.5 )
  85.     if message then
  86.         messages[2] = "<WARNING> A server with this hostname has been found (" ..id.. ")"
  87.         messages[3] = "<WARNING> type 'stop' to cancel."
  88.     end
  89.    
  90.     if not fs.exists( filePath ) then
  91.         fs.makeDir( filePath )
  92.         fs.open( homePage, "w" ).close()
  93.     end
  94.     messages[#messages+1] = "Server started."
  95.    
  96.     while true do
  97.         term.clear()
  98.         term.setCursorPos(1,3)
  99.         messages = stopBlinking( messages )
  100.         term.setTextColour( colors.green )
  101.         for i=1,#messages do
  102.             print(messages[i])
  103.         end
  104.         print()
  105.         print()
  106.         drawFooter()
  107.         for i=1,x do
  108.             term.setCursorPos(i, y-1)
  109.             write("-")
  110.         end
  111.        
  112.         if messages[#messages] == ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Stopped") then
  113.             error()
  114.         end
  115.         term.setCursorPos(1,y)
  116.         term.clearLine()
  117.         term.setTextColour( colors.yellow )
  118.         write("host> ")
  119.         term.setTextColour( colors.white )
  120.         write(sText)
  121.         term.setCursorBlink( true )
  122.         local event, key, message = os.pullEventRaw()
  123.         if event == "rednet_message" then
  124.             if blocked[tostring(key)] then else
  125.                 if message == newURL then
  126.                     downloads = downloads +1
  127.                     file = fs.open( homePage, "r" )
  128.                     rednet.send( key, file.readAll() )
  129.                     file.close()
  130.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <" ..key.."> Index downloaded.")
  131.                 elseif fs.exists( "." ..message ) then
  132.                     downloads = downloads +1
  133.                     file = fs.open( "." ..message, "r" )
  134.                     if file then
  135.                         rednet.send( key, file.readAll() )
  136.                         file.close()
  137.                     end
  138.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <" ..key.."> Subpage downloaded.")
  139.                 elseif message == "rednet.api.ping.searchengine" then
  140.                     rednet.send( key, newURL )
  141.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <" ..key.."> Searched for server")
  142.                 else
  143.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <" ..key.."> Junk received")
  144.                 end
  145.             end
  146.         elseif event == "terminate" then
  147.             messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Stopping...")
  148.             messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Stopped")
  149.         elseif event == "char" then
  150.             sText = sText..key
  151.         elseif event == "key" then
  152.             if key == keys.backspace then
  153.                 sText = string.sub(sText, 1, #sText-1)
  154.             elseif key == keys.enter then
  155.                 local tWords = {}
  156.                 for match in string.gmatch( sText, "[^ \t]+" ) do
  157.                     table.insert( tWords, match )
  158.                 end
  159.                
  160.                 local sCommand = tWords[1]
  161.                 if sCommand == "stop" then
  162.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Stopping...")
  163.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Stopped")
  164.                 elseif sCommand == "install" then
  165.                     if tWords[2] then
  166.                         messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Installing " ..tWords[2])
  167.                         if install( tWords[2] ) then
  168.                             messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> " ..tWords[2].. " successfully installed!")
  169.                         else
  170.                             messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> " ..tWords[2].. " failed to install :c")
  171.                         end
  172.                     else
  173.                         messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Missing 1 parameter(s)")
  174.                     end
  175.                 elseif sCommand == "help" or sCommand == "?" then
  176.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Commands: stop,install,help(?),stats")
  177.                 elseif sCommand == "stats" then
  178.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Downloads this session: " ..downloads)
  179.                 elseif sCommand == "time" then
  180.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> <-- Over there")
  181.                 elseif sCommand == "block" then
  182.                     if tWords[2] then
  183.                         blocked[#tWords[2]] = true
  184.                     end
  185.                 elseif sCommand == "unblock" then
  186.                     if tWords[2] then
  187.                         blocked[#tWords[2]] = false
  188.                     end
  189.                 elseif sText ~= "" then
  190.                     messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Unknown command.")
  191.                 end
  192.                
  193.                 sText = ""
  194.             end
  195.         end
  196.         term.setCursorBlink( false )
  197.     end
  198. end
  199.  
  200. for i,v in pairs( rs.getSides() ) do
  201.     rednet.open( v )
  202. end
  203.  
  204. newServer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement