diff --git a/dig.lua b/dig.lua deleted file mode 100644 index fe585ed..0000000 --- a/dig.lua +++ /dev/null @@ -1,29 +0,0 @@ -local args = { ... } - -modem_location = "right" -lns_server = "default" - -rednet.open(modem_location) -receiverID = rednet.lookup("lns", lns_server) -current_time = os.time() - - -data = { - ["action"] = "lookup", - ["hostname"] = args[1] -} - -rednet.send(receiverID, data) - -while true do - id, msg = rednet.receive() - if id == receiverID then - if msg == nil then - print("Hostname not found!") - break - else - io.write(msg .. "\n") - break - end - end -end \ No newline at end of file diff --git a/install.lua b/install.lua new file mode 100644 index 0000000..7a8fa28 --- /dev/null +++ b/install.lua @@ -0,0 +1,45 @@ +local args = { ... } + +local target_branch = "main" +local bin_path = "/bin/" +local lib_path = "/lib/" +local startup_path = "/startup/" + + +function download_lua(remote_path, local_path) + io.write("Downloading " .. remote_path .. " to " .. local_path .. "\n") + response = http.get("https://gitea.layla.gg/layla/computercraft/raw/branch/" .. target_branch .. "/" .. program_name .. ".lua", nil) + if response.getResponseCode() == 200 then + file = fs.open(local_path, "w") + file.write(response.readAll()) + file.close() + end + else + io.write("Failed to update " .. program_name .. ".lua\n") + io.write(response) + end +end + + +-- Startup +download_lua("utils/lsh.lua", startup_path .. "lsh.lua") + +-- Lib +download_lua("lib/lns.lua", lib_path .. "lns.lua") +download_lua("lib/lum.lua", lib_path .. "lum.lua") + +-- Bin +download_lua("lum.lua", bin_path .. "lum.lua") + +-- Run lsh +ok = shell.run(startup_path .. "lsh.lua") +if not ok then + io.write("Failed to run lsh!\n") + return +end + +-- Packages +lum.install("dig") +lum.install("auth") + +io.write("Done!\n") \ No newline at end of file diff --git a/lns.lua b/lib/lns.lua similarity index 58% rename from lns.lua rename to lib/lns.lua index 5db3573..307d40c 100644 --- a/lns.lua +++ b/lib/lns.lua @@ -1,7 +1,4 @@ -args = { ... } - -cache = {} - +local cache = {} settings.define("lns_client.allow_dynamic_lns_hosts", { description = "Allow dynamic LNS hosts", @@ -15,10 +12,8 @@ settings.define("lns_client.lns_server", { default = 8 }) -allow_dynamic_lns_hosts = settings.get("lns_client.allow_dynamic_lns_hosts") -lns_server_id = settings.get("lns_client.lns_server") -function load_cache() +function _load_cache() if fs.exists("lns_cache.db") then db = fs.open("lns_cache.db", "r") db_contents = db.readAll() @@ -28,17 +23,15 @@ function load_cache() end end -function save_cache() +function _save_cache() db = fs.open("lns_cache.db", "w") db.write(textutils.serialize(cache)) db.close() end -function lns_lookup(remote_hostname) - local data = { - ["action"] = "lookup", - ["hostname"] = remote_hostname - } +function server() + local lns_server_id = settings.get("lns_client.lns_server") + local allow_dynamic_lns_hosts = settings.get("lns_client.allow_dynamic_lns_hosts") if allow_dynamic_lns_hosts then lns_server_id = rednet.lookup("lns", lns_server) @@ -48,7 +41,22 @@ function lns_lookup(remote_hostname) end end - rednet.send(lns_server_id, data, "lns") + return lns_server_id +end + +function lookup(remote_hostname) + + _load_cache() + if cache[remote_hostname] ~= nil then + return cache[remote_hostname] + end + + local data = { + ["action"] = "lookup", + ["hostname"] = remote_hostname + } + + rednet.send(server(), data, "lns") while true do id, msg = rednet.receive("lns") @@ -56,43 +64,16 @@ function lns_lookup(remote_hostname) if msg == nil then return nil else + cache[remote_hostname] = msg + _save_cache() return msg end end end end - - -local action = args[1] -if action == nil then - io.write("Usage: lns \n") - return -end - -if action == "lookup" then - load_cache() - - local hostname = args[2] - if hostname == nil then - io.write("Usage: lns lookup \n") - return - end - - if cache[hostname] ~= nil then - io.write(cache[hostname]) - return - end - - local result = lns_lookup(hostname) - if result ~= nil then - io.write(result) - cache[hostname] = result - save_cache() - end - -elseif action == "clear" then +function clear_cache() cache = {} - save_cache() + _save_cache() print("Cleared cache!") end \ No newline at end of file diff --git a/lib/lum.lua b/lib/lum.lua new file mode 100644 index 0000000..5e7bd06 --- /dev/null +++ b/lib/lum.lua @@ -0,0 +1,22 @@ +local target_branch = "main" +local bin_path = "/bin/" +local lib_path = "/lib/" +local startup_path = "/startup/" + +function download_lua(remote_path, local_path) + response = http.get("https://gitea.layla.gg/layla/computercraft/raw/branch/" .. target_branch .. "/" .. program_name .. ".lua", nil) + if response.getResponseCode() == 200 then + file = fs.open(local_path, "w") + file.write(response.readAll()) + file.close() + end + else + io.write("Failed to update " .. program_name .. ".lua\n") + io.write(response) + end +end + +function install(package) + io.write("Installing " .. package .. "\n") + download_lua("packages/" .. package .. ".lua", bin_path .. package .. ".lua") +end \ No newline at end of file diff --git a/lum.lua b/lum.lua new file mode 100644 index 0000000..2cefe55 --- /dev/null +++ b/lum.lua @@ -0,0 +1,19 @@ +local args = { ... } + +local action = args[1] +if action == nil then + print("Usage: lum ") + return +end + + +if action == "install" then + package = args[2] + if package == nil then + print("Usage: lum install ") + return + end + + lum.install(package) + io.write("Done!\n") + diff --git a/lupdate.lua b/lupdate.lua deleted file mode 100644 index 0aea7f2..0000000 --- a/lupdate.lua +++ /dev/null @@ -1,27 +0,0 @@ -local args = { ... } - -local target_branch = "main" -local bin_path = "/bin/" - -local program_name = args[1] -if program_name == nil then - print("Usage: lupdate ") - return -end - -response = http.get("https://gitea.layla.gg/layla/computercraft/raw/branch/" .. target_branch .. "/" .. program_name .. ".lua", nil) - -io.write("Updating " .. program_name .. ".lua\n") - -if response.getResponseCode() == 200 then - - file = fs.open(bin_path .. program_name .. ".lua", "w") - file.write(response.readAll()) - file.close() - - io.write("Updated " .. program_name .. ".lua\n") -else - io.write("Failed to update " .. program_name .. ".lua\n") - io.write(response) -end - diff --git a/auth.lua b/packages/auth.lua similarity index 100% rename from auth.lua rename to packages/auth.lua diff --git a/auth_server.lua b/packages/auth_server.lua similarity index 100% rename from auth_server.lua rename to packages/auth_server.lua diff --git a/clown.lua b/packages/clown.lua similarity index 100% rename from clown.lua rename to packages/clown.lua diff --git a/packages/dig.lua b/packages/dig.lua new file mode 100644 index 0000000..c2fa0d6 --- /dev/null +++ b/packages/dig.lua @@ -0,0 +1,8 @@ +local args = { ... } + +if args[1] == nil then + print("Usage: dig ") + return +end + +os.write(lns.lookup(args[1]) .. "\n") \ No newline at end of file diff --git a/lns_server.lua b/packages/lns_server.lua similarity index 100% rename from lns_server.lua rename to packages/lns_server.lua diff --git a/register_lns.lua b/packages/set_lns.lua similarity index 80% rename from register_lns.lua rename to packages/set_lns.lua index 2fa48ea..615fa53 100644 --- a/register_lns.lua +++ b/packages/set_lns.lua @@ -5,15 +5,6 @@ hostname = io.read() io.write("Enter the modem location: ") modem_location = io.read() -io.write("Use default LNS Server? (y/n): ") -use_default = io.read() - -lns_server = "default" -if use_default == "n" then - io.write("Enter the LNS Server Rednet Hostname: ") - lns_server = io.read() -end - rednet.open(modem_location) receiverID = rednet.lookup("lns", lns_server) current_time = os.time() diff --git a/utils/lsh.lua b/utils/lsh.lua index fe23f5f..2c79e4a 100644 --- a/utils/lsh.lua +++ b/utils/lsh.lua @@ -1,3 +1,12 @@ local bin_path = "/bin/" -shell.setPath(shell.path() .. ":" .. bin_path) \ No newline at end of file +local lib_path = "/lib/" + +shell.setPath(shell.path() .. ":" .. bin_path) + +-- Load libraries +for _, file in ipairs(fs.list(lib_path)) do + if not fs.isDir(lib_path .. file) then + os.loadAPI(lib_path .. file) + end +end \ No newline at end of file