Files
computercraft/lns.lua
2023-10-08 01:52:14 -04:00

76 lines
1.6 KiB
Lua

args = { ... }
cache = {}
settings.define("lns_client.allow_dynamic_lns_hosts", {
description = "Allow dynamic LNS hosts",
type = "boolean",
default = false
})
settings.define("lns_client.lns_server", {
description = "LNS Server Rednet ID",
type = "number",
default = 8
})
allow_dynamic_lns_hosts = settings.get("lns_client.allow_dynamic_lns_hosts")
lns_server_id = settings.get("lns_client.lns_server")
function lns_lookup(remote_hostname)
local data = {
["action"] = "lookup",
["hostname"] = remote_hostname
}
if allow_dynamic_lns_hosts then
lns_server_id = rednet.lookup("lns", lns_server)
if lns_server_id == nil then
io.write("LNS Server not found!\n")
return nil
end
end
rednet.send(lns_server_id, data)
while true do
id, msg = rednet.receive()
if id == lns_server_id then
if msg == nil then
return nil
else
return msg
end
end
end
end
local action = args[1]
if action == nil then
io.write("Usage: lns <action>\n")
return
end
if action == "lookup" then
local hostname = args[2]
if hostname == nil then
io.write("Usage: lns lookup <hostname>\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
end
elseif action == "clear" then
cache = {}
print("Cleared cache!")
end