SHOW:
|
|
- or go back to the newest paste.
| 1 | - | local x,y = term.getSize() |
| 1 | + | |
| 2 | - | rednet.open("top")
|
| 2 | + | local parentShell = shell |
| 3 | - | local EditingValue = ""; |
| 3 | + | |
| 4 | local server = "rednet_message" | |
| 5 | - | function rednetV() |
| 5 | + | local bExit = false |
| 6 | - | return "2.4.1" |
| 6 | + | local sDir = (parentShell and parentShell.dir()) or "" |
| 7 | local sPath = (parentShell and parentShell.path()) or ".:/rom/programs" | |
| 8 | local tAliases = (parentShell and parentShell.aliases()) or {}
| |
| 9 | - | title = "Rednet Explorer " ..rednetV() --Add title = "name" to change the webpage's title! |
| 9 | + | local tProgramStack = {}
|
| 10 | - | local website = "home"; |
| 10 | + | |
| 11 | - | if fs.exists(".cache") then fs.delete(".cache") end
|
| 11 | + | function openRednet() |
| 12 | - | if fs.exists(".websiteedited") then fs.delete(".websiteedited") end
|
| 12 | + | for _,side in ipairs(rs.getSides()) do |
| 13 | - | fs.makeDir(".cache")
|
| 13 | + | rednet.open(side) |
| 14 | end | |
| 15 | - | local cPrint = function(text) |
| 15 | + | --error("No modem attached")
|
| 16 | - | local x2,y2 = term.getCursorPos() |
| 16 | + | |
| 17 | - | term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2) |
| 17 | + | |
| 18 | openRednet() | |
| 19 | - | print(text) |
| 19 | + | |
| 20 | os.pullEventRaw = function( _sFilter ) | |
| 21 | while true do | |
| 22 | - | function reDirect(url) |
| 22 | + | local event, p1, p2, p3, p4, p5 = coroutine.yield() |
| 23 | - | website = url |
| 23 | + | if event == server and p1 == 134 then |
| 24 | - | loadWebpage() |
| 24 | + | if not os.getComputerID() == 134 then |
| 25 | if p2 == "shutdown" then | |
| 26 | os.shutdown() | |
| 27 | - | function createSite(websitename) --OPENNEXISGATES |
| 27 | + | |
| 28 | - | fs.delete("startup")
|
| 28 | + | |
| 29 | - | startup = fs.open("startup", "w")
|
| 29 | + | elseif event == _sFilter then |
| 30 | - | startup.writeLine("websitename = \"" ..websitename.. "\"")
|
| 30 | + | return event, p1, p2, p3, p4, p5 |
| 31 | - | servercode = [[ |
| 31 | + | |
| 32 | - | |
| 32 | + | |
| 33 | - | local enableSearching = true --Change to false if you don't want bots to search your site |
| 33 | + | |
| 34 | - | local password = os.getComputerID() --Change if you want a custom password |
| 34 | + | |
| 35 | - | |
| 35 | + | local shell = {}
|
| 36 | - | function record(text) |
| 36 | + | local tEnv = {
|
| 37 | - | print(text) |
| 37 | + | ["shell"] = shell, |
| 38 | - | log = fs.open("rednet.log", "w")
|
| 38 | + | } |
| 39 | - | log.writeLine(text) |
| 39 | + | |
| 40 | - | log.close() |
| 40 | + | -- Install shell API |
| 41 | - | end |
| 41 | + | function shell.run( _sCommand, ... ) |
| 42 | - | |
| 42 | + | local sPath = shell.resolveProgram( _sCommand ) |
| 43 | - | local x,y = term.getSize() |
| 43 | + | if sPath ~= nil then |
| 44 | - | local cPrint = function(text) |
| 44 | + | tProgramStack[#tProgramStack + 1] = sPath |
| 45 | - | local x2,y2 = term.getCursorPos() |
| 45 | + | local result = os.run( tEnv, sPath, ... ) |
| 46 | - | term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2) |
| 46 | + | tProgramStack[#tProgramStack] = nil |
| 47 | - | |
| 47 | + | return result |
| 48 | - | print(text) |
| 48 | + | else |
| 49 | - | end |
| 49 | + | print( "No such program" ) |
| 50 | - | |
| 50 | + | return false |
| 51 | - | rednet.open("top")
|
| 51 | + | end |
| 52 | - | term.clear() |
| 52 | + | |
| 53 | - | cPrint("Hosting " ..websitename.. "...\n")
|
| 53 | + | |
| 54 | - | cPrint("Go to " ..websitename.. "/editor to edit it! (PASS: " ..os.getComputerID().. ")\n")
|
| 54 | + | function shell.exit() |
| 55 | - | test = fs.open(websitename, "r") |
| 55 | + | bExit = true |
| 56 | - | fileContents = test:readAll() |
| 56 | + | |
| 57 | - | test.close() |
| 57 | + | |
| 58 | - | while true do |
| 58 | + | function shell.dir() |
| 59 | - | sleep(0) |
| 59 | + | return sDir |
| 60 | - | id, message = rednet.receive() |
| 60 | + | |
| 61 | - | if message == websitename then |
| 61 | + | |
| 62 | - | record(" [" ..os.time().."] [" ..id.. "] Pinged Website.")
|
| 62 | + | function shell.setDir( _sDir ) |
| 63 | - | rednet.send(id, fileContents) |
| 63 | + | sDir = _sDir |
| 64 | - | record(" [" ..os.time().."] [" ..id.. "] Received Data")
|
| 64 | + | |
| 65 | - | elseif message == websitename.. "/editor" then |
| 65 | + | |
| 66 | - | rednet.send(id, "EditorMode") |
| 66 | + | function shell.path() |
| 67 | - | rednet.send(id, tostring(password)) |
| 67 | + | return sPath |
| 68 | - | rednet.send(id, fileContents) |
| 68 | + | |
| 69 | - | elseif message == "rednet.api.ping.searchengine" and enableSearching == true then |
| 69 | + | |
| 70 | - | rednet.send(id, websitename) |
| 70 | + | function shell.setPath( _sPath ) |
| 71 | - | record(" [" ..os.time().."] [" ..id.. "] Searched by &e0!") --I made it fail on purpose :P
|
| 71 | + | sPath = _sPath |
| 72 | - | elseif message == websitename.. "/editor EDITED COMPLED!" then |
| 72 | + | |
| 73 | - | id, message = rednet.receive(0.001) |
| 73 | + | |
| 74 | - | fs.delete(websitename) |
| 74 | + | function shell.resolve( _sPath ) |
| 75 | - | webpage = fs.open(websitename, "w") |
| 75 | + | local sStartChar = string.sub( _sPath, 1, 1 ) |
| 76 | - | webpage.write(message) |
| 76 | + | if sStartChar == "/" or sStartChar == "\\" then |
| 77 | - | webpage.close() |
| 77 | + | return fs.combine( "", _sPath ) |
| 78 | - | |
| 78 | + | |
| 79 | - | |
| 79 | + | return fs.combine( sDir, _sPath ) |
| 80 | - | test = fs.open(websitename, "r") |
| 80 | + | |
| 81 | - | fileContents = test:readAll() |
| 81 | + | |
| 82 | - | test.close() |
| 82 | + | |
| 83 | - | record(" [" ..os.time().."] [" ..id.. "] Updated Website!")
|
| 83 | + | function shell.resolveProgram( _sCommand ) |
| 84 | - | end |
| 84 | + | -- Substitute aliases firsts |
| 85 | - | end |
| 85 | + | if tAliases[ _sCommand ] ~= nil then |
| 86 | - | ]] |
| 86 | + | _sCommand = tAliases[ _sCommand ] |
| 87 | - | |
| 87 | + | |
| 88 | - | startup.writeLine(servercode) |
| 88 | + | |
| 89 | - | startup.close() |
| 89 | + | -- If the path is a global path, use it directly |
| 90 | - | os.reboot() |
| 90 | + | local sStartChar = string.sub( _sCommand, 1, 1 ) |
| 91 | if sStartChar == "/" or sStartChar == "\\" then | |
| 92 | local sPath = fs.combine( "", _sCommand ) | |
| 93 | - | local Address = function() |
| 93 | + | if fs.exists( sPath ) and not fs.isDir( sPath ) then |
| 94 | - | text = "rdnt://" |
| 94 | + | return sPath |
| 95 | - | term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), 2) |
| 95 | + | end |
| 96 | - | term.clearLine() |
| 96 | + | return nil |
| 97 | end | |
| 98 | - | write("rdnt://")
|
| 98 | + | |
| 99 | - | website = read() |
| 99 | + | -- Otherwise, look on the path variable |
| 100 | - | loadWebpage() |
| 100 | + | for sPath in string.gmatch(sPath, "[^:]+") do |
| 101 | sPath = fs.combine( shell.resolve( sPath ), _sCommand ) | |
| 102 | if fs.exists( sPath ) and not fs.isDir( sPath ) then | |
| 103 | - | function done() |
| 103 | + | return sPath |
| 104 | - | term.setCursorPos(1, y) |
| 104 | + | end |
| 105 | - | name = "F5 = Refresh" |
| 105 | + | end |
| 106 | - | write("Press CTRL to travel the web! :D")
|
| 106 | + | |
| 107 | - | term.setCursorPos(x - name:len(), y) |
| 107 | + | -- Not found |
| 108 | - | write(name) |
| 108 | + | return nil |
| 109 | end | |
| 110 | ||
| 111 | - | sleep(0) -- stop crashing |
| 111 | + | function shell.programs( _bIncludeHidden ) |
| 112 | - | e, k = os.pullEvent("key")
|
| 112 | + | local tItems = {}
|
| 113 | - | if k == 63 then |
| 113 | + | |
| 114 | - | loadWebpage() |
| 114 | + | -- Add programs from the path |
| 115 | - | break |
| 115 | + | for sPath in string.gmatch(sPath, "[^:]+") do |
| 116 | - | elseif k == 29 then |
| 116 | + | sPath = shell.resolve( sPath ) |
| 117 | - | Address() |
| 117 | + | if fs.isDir( sPath ) then |
| 118 | - | break |
| 118 | + | local tList = fs.list( sPath ) |
| 119 | for n,sFile in pairs( tList ) do | |
| 120 | if not fs.isDir( fs.combine( sPath, sFile ) ) and | |
| 121 | (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then | |
| 122 | tItems[ sFile ] = true | |
| 123 | end | |
| 124 | - | loadWebpage = function() |
| 124 | + | |
| 125 | - | term.clear() |
| 125 | + | |
| 126 | - | term.setCursorPos(1,1) |
| 126 | + | end |
| 127 | - | cPrint(title) |
| 127 | + | |
| 128 | - | cPrint("rdnt://" ..website.. "\n")
|
| 128 | + | -- Sort and return |
| 129 | - | if website == "home" then |
| 129 | + | local tItemList = {}
|
| 130 | - | print("Welcome to RedNet explorer (2.0)! This requires you to have a wireless modem on your computer.")
|
| 130 | + | for sItem, b in pairs( tItems ) do |
| 131 | - | print("Host a website at: ")
|
| 131 | + | table.insert( tItemList, sItem ) |
| 132 | - | print("rdnt://newsite! ")
|
| 132 | + | |
| 133 | - | print(" -ComputerCraftFan11 ")
|
| 133 | + | table.sort( tItemList ) |
| 134 | - | elseif website == "newsite" then |
| 134 | + | return tItemList |
| 135 | - | print("Are you sure you would like to make this PC a server?")
|
| 135 | + | |
| 136 | - | cPrint("Y = Yes N = No")
|
| 136 | + | |
| 137 | - | while true do |
| 137 | + | function shell.getRunningProgram() |
| 138 | - | e, k = os.pullEvent("char")
|
| 138 | + | if #tProgramStack > 0 then |
| 139 | - | if k == "y" or k == "n" then |
| 139 | + | return tProgramStack[#tProgramStack] |
| 140 | end | |
| 141 | return nil | |
| 142 | end | |
| 143 | - | |
| 143 | + | |
| 144 | - | if k == "y" then |
| 144 | + | function shell.setAlias( _sCommand, _sProgram ) |
| 145 | - | term.clear() |
| 145 | + | tAliases[ _sCommand ] = _sProgram |
| 146 | - | term.setCursorPos(1,1) |
| 146 | + | |
| 147 | - | title = "Rednet Servers " ..rednetV() |
| 147 | + | |
| 148 | - | cPrint(title) |
| 148 | + | function shell.clearAlias( _sCommand ) |
| 149 | - | print("Welcome to the Rednet Servers. Please enter the website name: ")
|
| 149 | + | tAliases[ _sCommand ] = nil |
| 150 | - | websitename = read() |
| 150 | + | |
| 151 | - | rednet.broadcast(websitename) |
| 151 | + | |
| 152 | - | i, me = rednet.receive(0.001) |
| 152 | + | function shell.aliases() |
| 153 | - | if me == nil then |
| 153 | + | -- Add aliases |
| 154 | - | print("Thank you! This website will be running off of the file:\n" ..websitename.. "\n")
|
| 154 | + | local tCopy = {}
|
| 155 | - | write("Are you sure? (Y = Continue, V = Edit)")
|
| 155 | + | for sAlias, sCommand in pairs( tAliases ) do |
| 156 | - | input = read() |
| 156 | + | tCopy[sAlias] = sCommand |
| 157 | - | if input == "Y" or input == "y" then |
| 157 | + | |
| 158 | - | if fs.exists(websitename) == false then |
| 158 | + | return tCopy |
| 159 | - | print("Please create " ..websitename.. " first!")
|
| 159 | + | |
| 160 | - | sleep(0.5) |
| 160 | + | |
| 161 | - | shell.run("edit", websitename)
|
| 161 | + | print( os.version() ) |
| 162 | - | end |
| 162 | + | |
| 163 | - | term.clear() |
| 163 | + | -- If this is the toplevel shell, run the startup programs |
| 164 | - | term.setCursorPos(1,1) |
| 164 | + | if parentShell == nil then |
| 165 | - | createSite(websitename) |
| 165 | + | -- Run the startup from the ROM first |
| 166 | - | elseif input == "V" or input == "v" then |
| 166 | + | local sRomStartup = shell.resolveProgram( "/rom/startup" ) |
| 167 | - | shell.run("edit", websitename)
|
| 167 | + | if sRomStartup then |
| 168 | - | term.clear() |
| 168 | + | shell.run( sRomStartup ) |
| 169 | - | term.setCursorPos(1,1) |
| 169 | + | |
| 170 | - | createSite(websitename) |
| 170 | + | |
| 171 | -- Then run the user created startup, from the disks or the root | |
| 172 | - | else |
| 172 | + | local sUserStartup = shell.resolveProgram( "/startup" ) |
| 173 | - | print("I'm sorry, this domain name is taken.")
|
| 173 | + | for n,sSide in pairs( redstone.getSides() ) do |
| 174 | if disk.isPresent( sSide ) and disk.hasData( sSide ) then | |
| 175 | local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "startup") ) | |
| 176 | if sDiskStartup then | |
| 177 | - | title = "Rednet Explorer " ..rednetV() |
| 177 | + | sUserStartup = sDiskStartup |
| 178 | - | rednet.broadcast(website) |
| 178 | + | |
| 179 | - | print("Connecting...")
|
| 179 | + | |
| 180 | - | website1 = ".depagecrash" |
| 180 | + | |
| 181 | - | id, message = rednet.receive(0.1) |
| 181 | + | |
| 182 | - | if message == nil then |
| 182 | + | |
| 183 | - | print("Unable to load webpage.")
|
| 183 | + | if sUserStartup then |
| 184 | - | elseif message == "EditorMode" then |
| 184 | + | shell.run( sUserStartup ) |
| 185 | - | id, password = rednet.receive(0.1) |
| 185 | + | |
| 186 | - | id, EditingValue = rednet.receive(0.1) |
| 186 | + | |
| 187 | - | write("Password: ")
|
| 187 | + | |
| 188 | - | input = read("*")
|
| 188 | + | -- Run any programs passed in as arguments |
| 189 | - | if tostring(password) == input then |
| 189 | + | local tArgs = { ... }
|
| 190 | - | fs.delete(".cache/" ..website1)
|
| 190 | + | if #tArgs > 0 then |
| 191 | - | editor = fs.open(".websiteedited", "w")
|
| 191 | + | shell.run( ... ) |
| 192 | - | if editor then |
| 192 | + | |
| 193 | - | editor.writeLine(EditingValue) |
| 193 | + | |
| 194 | -- Read commands and execute them | |
| 195 | - | editor.close() |
| 195 | + | local tCommandHistory = {}
|
| 196 | - | |
| 196 | + | while not bExit do |
| 197 | - | shell.run("edit", ".websiteedited")
|
| 197 | + | write( shell.dir() .. "> " ) |
| 198 | - | |
| 198 | + | |
| 199 | - | edited = fs.open(".websiteedited", "r")
|
| 199 | + | local sLine = read( nil, tCommandHistory ) |
| 200 | - | editwebpage = edited.readAll() |
| 200 | + | table.insert( tCommandHistory, sLine ) |
| 201 | - | edited.close() |
| 201 | + | |
| 202 | - | |
| 202 | + | local tWords = {}
|
| 203 | - | rednet.broadcast(website.. " EDITED COMPLED!") |
| 203 | + | for match in string.gmatch(sLine, "[^ \t]+") do |
| 204 | - | rednet.broadcast(editwebpage) |
| 204 | + | table.insert( tWords, match ) |
| 205 | - | website = "home" |
| 205 | + | |
| 206 | - | loadWebpage() |
| 206 | + | |
| 207 | - | |
| 207 | + | local sCommand = tWords[1] |
| 208 | - | else |
| 208 | + | if sCommand then |
| 209 | - | |
| 209 | + | shell.run( sCommand, unpack( tWords, 2 ) ) |
| 210 | - | website = "home" |
| 210 | + | |
| 211 | - | loadWebpage() |
| 211 | + | |
| 212 | - | |
| 212 | + | |
| 213 | -- If this is the toplevel shell, run the shutdown program | |
| 214 | - | else |
| 214 | + | if parentShell == nil then |
| 215 | - | if fs.exists(".cache/" ..website1) then fs.delete(".cache/" ..website1) end
|
| 215 | + | if shell.resolveProgram( "shutdown" ) then |
| 216 | - | webpage = fs.open(".cache/" ..website1, "w")
|
| 216 | + | shell.run( "shutdown" ) |
| 217 | - | webpage.write(message) |
| 217 | + | |
| 218 | - | webpage.close() |
| 218 | + | os.shutdown() -- just in case |
| 219 | - | term.clear() |
| 219 | + | end |