2023-10-07 22:57:23 -04:00

65 lines
1.3 KiB
Lua

modem_location = "back"
rednet.host("lns", "default")
rednet.open(modem_location)
local data = {}
function save_data()
db = fs.open("lns.db", "w")
db.write(textutils.serialize(data))
db.close()
end
function load_data()
if fs.exists("lns.db") then
db = fs.open("lns.db", "r")
db_contents = db.readAll()
db.close()
data = textutils.unserialize(db_contents)
end
end
function split (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
load_data()
while true do
client_id, msg = rednet.receive()
-- split string msg into action and hostname with ':' as a delimiter using string.unpack
local action, hostname = split(msg, ":")
if action == "lookup" then
-- check if hostname in data
if data[hostname] == nil then
rednet.send(client_id, "not found")
else
rednet.send(client_id, data[hostname])
end
end
if action == "reload" then
load_data()
rednet.send(client_id, "ok")
end
if action == "register" then
data[hostname] = client_id
save_data()
rednet.send(client_id, "ok")
end
end