diff --git a/get-all.lua b/get-all.lua index 51b4f81..19c23c4 100755 --- a/get-all.lua +++ b/get-all.lua @@ -1,11 +1,22 @@ local shell = require("shell") local fs = require("filesystem") local internet = require("internet") +-- local ll = require("liblua") local args = {...} --- Split string based on delimiter -function split(s, sep) +-- Error checking +if args[1] == nil then + print("URL must exist") + return +end + +-- Finish this off +local function strip(s, p) + return string.gsub(s, string.format("(^(%s+))|((%s+)$)", p, p)) +end + +local function split(s, sep) if sep == nil then sep = "%s" end @@ -17,57 +28,68 @@ function split(s, sep) return t end --- Slice table -function slice(tbl, first, last, step) - local sliced = {} - - for i = first or 1, last or #tbl, step or 1 do - sliced[#sliced+1] = tbl[i] +local function fixURL(url) + if not string.match(url, "/$") and not string.match(url, "\\.") then + url = url.."/" end - - return sliced + return url end --- Error checking -if args[1] == nil then - print("URL must exist") - return -end - -local handle = internet.request(args[1]) -local result = "" -for chunk in handle do result = result..chunk end --- Print the body of the HTTP response --- print(result) +local function makeRequest(url) + local handle = internet.request(url) + local result = "" + for chunk in handle do result = result..chunk end + -- Grab the metatable for the handle. This contains the + -- internal HTTPRequest object. + local mt = getmetatable(handle) --- Grab the metatable for the handle. This contains the --- internal HTTPRequest object. -local mt = getmetatable(handle) + -- The response method grabs the information for + -- the HTTP response code, the response message, and the + -- response headers. + local code, message, headers = mt.__index.response() --- The response method grabs the information for --- the HTTP response code, the response message, and the --- response headers. -local code, message, headers = mt.__index.response() -print("code = "..tostring(code)) -print("message = "..tostring(message)) + if code ~=200 then + return nil, "Bad code received" + end + return url, result +end + +local function getFiles(url, result) + for i in string.gmatch(result, "href=\".-\"") do + local dir = split(i, "\"")[2] + + if string.match(dir, ".lua$") then + shell.execute("wget -f "..url..dir) + goto continue + end + if string.match(dir, "/$") + and not string.match(dir, ".git") + and not string.match(dir, ".vscode") + then + dir = dir + -- Remove any weird files + if fs.exists(dir) and not fs.isDirectory(dir) then + fs.remove(dir) + end --- Sandbox so we don't write to the screen -sandbox_env = { - assert = assert, - require = require, - pairs = pairs, - pcall = pcall, - tostring = tostring, - string = string, - io = {write = function(...) end, read = io.read, open = io.open, stderr = io.stderr} -} + -- Make missing dir + if not fs.exists(dir) then + fs.makeDirectory(dir) + end + -- Recurse + shell.setWorkingDirectory(dir) + print(shell.getWorkingDirectory()) + getFiles(makeRequest(url..dir)) + end + ::continue:: + end +end --- pcall(fs.remove, fileName) --- shell.execute("wget -f "..args[1].." "..fileName, sandbox_env) +-- Grab all files on the server +local path = shell.getWorkingDirectory() +getFiles(makeRequest(fixURL(args[1]))) +fs.setDirectory(path) --- -- Run downloaded file --- local c = fileName --- for _, i in ipairs(newArgs) do c = c.." "..i end --- shell.execute(c)function split(s, sep) \ No newline at end of file +-- wget -f http://mc.bashed.rocks:13699/get-all.lua \ No newline at end of file diff --git a/grab-run.lua b/grab-run.lua index fb8bae2..6bb52d2 100755 --- a/grab-run.lua +++ b/grab-run.lua @@ -1,33 +1,10 @@ local shell = require("shell") local internet = require("internet") local fs = require("filesystem") +local ll = require("liblua") local args = {...} --- Split string based on delimiter -function split(s, sep) - if sep == nil then - sep = "%s" - end - - local t = {} - for str in string.gmatch(s, "([^"..sep.."]+)") do - table.insert(t, str) - end - return t -end - --- Slice table -function slice(tbl, first, last, step) - local sliced = {} - - for i = first or 1, last or #tbl, step or 1 do - sliced[#sliced+1] = tbl[i] - end - - return sliced -end - -- Error checking if args[1] == nil then print("URL must exist") @@ -38,12 +15,12 @@ args[1] = "http://mc.bashed.rocks:13699/"..args[1] -- Get local filename local fileName = "" if args[2] == nil then - local t = split(args[1], "/") + local t = ll.split(args[1], "/") fileName = t[#t] else fileName = args[2] end -local newArgs = slice(args, 3, #args-2) +local newArgs = ll.slice(args, 3, #args-2) -- Sandbox so we don't write to the screen sandbox_env = { @@ -62,4 +39,6 @@ shell.execute("wget -f "..args[1].." "..fileName, sandbox_env) -- Run downloaded file local c = fileName for _, i in ipairs(newArgs) do c = c.." "..i end -shell.execute(c) \ No newline at end of file +shell.execute(c) + +-- wget -f http://mc.bashed.rocks:13699/grab-run.lua \ No newline at end of file diff --git a/lib/async.lua b/lib/async.lua index 05e1463..0f7192d 100644 --- a/lib/async.lua +++ b/lib/async.lua @@ -7,15 +7,21 @@ local table = require("table") local async = {} --- TODO FINISH THIS -- An async wrapper for event.pullFiltered function async.pullFiltered(...) - -- Check if we're in a coroutine or not - _, status = coroutine.running() local start = computer.uptime() local args = {...} + + local blocking = nil + -- Check if we're in a coroutine or not + if type(args[#args]) == "boolean" and args[#args] then + blocking = true + else + blocking = false + end + -------------------- -- Break on interrupts function f(name, ...) -- if args[1] happens to be "interrupted" already the second won't trigger @@ -23,11 +29,11 @@ function async.pullFiltered(...) return true end end + -------------------- if type(args[1]) == "string" then -- Check if not in a coroutine - if not status then return event.pullFiltered(f, ...) end - print("Testing") + if blocking then return event.pullFiltered(f, ...) end repeat coroutine.yield() @@ -38,8 +44,10 @@ function async.pullFiltered(...) if type(args[1]) == "number" then timeout = args[1] args = ll.slice({...}, 2) + end -- For speed purposes check up here even though it's more repeated code - elseif type(args[1]) == "function" then + if type(args[1]) == "function" then + -------------------- -- Wrap other function so this will handle interrupts function g(name, ...) local a = ll.slice(args, 2) @@ -50,10 +58,11 @@ function async.pullFiltered(...) -- Otherwise run other function return args[1](name, ...) end + -------------------- if timeout ~= nil then -- Check if not in a coroutine - if not status then + if blocking then return event.pullFiltered(timeout, g, table.unpack(args)) end @@ -63,7 +72,7 @@ function async.pullFiltered(...) until #tmp > 0 or computer.uptime() - start >= timeout else -- Check if not in a coroutine - if not status then + if blocking then return event.pullFiltered(g, ...) end @@ -73,9 +82,10 @@ function async.pullFiltered(...) until #tmp > 0 end else + print(timeout, blocking) if timeout ~= nil then -- Check if not in a coroutine - if not status then + if blocking then return event.pullFiltered(timeout, f, table.unpack(args)) end @@ -85,7 +95,7 @@ function async.pullFiltered(...) until #tmp > 0 or computer.uptime() - start >= timeout else -- Check if not in a coroutine - if not status then + if blocking then return event.pullFiltered(f, table.unpack(args)) end @@ -96,6 +106,9 @@ function async.pullFiltered(...) end end end + if tmp == nil then + return nil + end return table.unpack(tmp) end @@ -103,10 +116,9 @@ end async.pull = async.pullFiltered -- Sleep in a coroutine friendly way -function async.sleep(s) - local _, status = coroutine.running() +function async.sleep(s, blocking) -- If not a coroutine just regular sleep - if status then + if blocking then os.sleep(s) return end @@ -136,7 +148,7 @@ local loop = { function async.bundle(f, ...) return { coro = coroutine.create(f), - args = {...}, + args = {...} or {}, results = {} } end @@ -156,19 +168,20 @@ function async.roundRobin(loop, ignoreError) local i = 1 repeat local b = loop.live[i] + print(b) local r = {coroutine.resume(b.coro, table.unpack(b.args))} - local status, results = r[1], ll.slice(r, 2) + local blocking, results = r[1], ll.slice(r, 2) -- Fail and exit if a function is bad - if not status and not ignoreError then + if blocking and not ignoreError then io.stderr:write(results[1]) return -- Don't exit but print the bad message - elseif not status then + elseif blocking then print(results[1]) end -- Stop running dead coroutines and add the results - if coroutine.status(b.coro) == "dead" then + if coroutine.blocking(b.coro) == "dead" then b.results = results table.insert(loop.dead, #loop.dead+1, table.remove(loop.live, i)) else diff --git a/lib/dhcp.lua b/lib/dhcp.lua index eb50cbc..3d20e0a 100755 --- a/lib/dhcp.lua +++ b/lib/dhcp.lua @@ -67,7 +67,7 @@ function dhcp.getServerConfig(fileName) end end -function dhcp.startServer(servPort, clientPort, cacheConfig) +function dhcp.startServer(servPort, clientPort, nonblocking, cacheConfig) -- Make sure ports aren't invalid if type(servPort) ~= "number" then servPort = dhcp.SERV_PORT @@ -102,7 +102,7 @@ function dhcp.startServer(servPort, clientPort, cacheConfig) local config = dhcp.getServerConfig() while true do - local pkt, err = net.recv{servPort, filter = dhcp.serverMsg} + local pkt, err = net.recv{servPort, filter = dhcp.serverMsg, nonblocking = nonblocking} if pkt == nil and err == "interrupted" then print("Interrupted") return @@ -117,7 +117,7 @@ function dhcp.startServer(servPort, clientPort, cacheConfig) end end -function dhcp.query(servPort, clientPort) +function dhcp.query(servPort, clientPort, nonblocking) -- Make sure ports aren't invalid if type(servPort) ~= "number" then servPort = dhcp.SERV_PORT @@ -143,8 +143,13 @@ function dhcp.query(servPort, clientPort) end net.broadcast(servPort, "dhcp", clientPort, "get") - pkt = net.recv{clientPort, timeout = 3, filter = dhcp.clientMsg} - return pkt + pkt, err = net.recv{ + clientPort, + timeout = 3, + filter = dhcp.clientMsg, + nonblocking = nonblocking + } + return pkt, err end -- dhcp.startServer() diff --git a/lib/dns.lua b/lib/dns.lua index 7c194f7..202036b 100755 --- a/lib/dns.lua +++ b/lib/dns.lua @@ -1,10 +1,9 @@ local component = require("component") -local event = require("event") +local async = require("async") local fs = require("filesystem") local io = require("io") local serial = require("serialization") -local split = require("split") -local slice = require("slice") +local ll = require("liblua") local modem = component.proxy(component.list("modem")()) local dns = { @@ -58,7 +57,7 @@ function dns.getRecords(fileName) while true do local line = f:read() if line ~= nil then - local t = split(line) + local t = ll.split(line) -- Records file format: