Files
computercraft/lns.lua
2023-10-08 01:26:35 -04:00

63 lines
1.2 KiB
Lua

args = { ... }
cache = {}
allow_dynamic_lns_hosts = false
function lns_lookup(remote_hostname)
local data = {
["action"] = "lookup",
["hostname"] = remote_hostname
}
local lns_server_id = 7
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(id)
cache[hostname] = result
end
elseif action == "clear" then
cache = {}
print("Cleared cache!")
end