2019-02-09 16:48:34 +01:00
|
|
|
-- WARNING: ipnames.command_* are reserved for chat commands!
|
|
|
|
|
2014-08-16 09:39:55 +02:00
|
|
|
function ipnames.command_list(name)
|
2019-02-09 16:48:34 +01:00
|
|
|
local names = {} -- faster than string concat
|
2014-08-16 09:39:55 +02:00
|
|
|
for k, v in pairs(ipnames.whitelist) do
|
2019-02-09 16:48:34 +01:00
|
|
|
if v then
|
|
|
|
names[#names + 1] = k
|
|
|
|
end
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
2019-02-09 16:48:34 +01:00
|
|
|
return true, "All exceptions: " .. table.concat(names, ", ")
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function ipnames.command_whois(name, param)
|
|
|
|
if not ipnames.data[param] then
|
2019-02-09 16:48:34 +01:00
|
|
|
return false, "The player '" .. param .. "' did not join yet."
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
|
2014-08-16 09:53:41 +02:00
|
|
|
local ip = ipnames.data[param][1]
|
2014-08-16 09:39:55 +02:00
|
|
|
local names = ""
|
|
|
|
for k, v in pairs(ipnames.data) do
|
2014-08-16 09:53:41 +02:00
|
|
|
if v[1] == ip then
|
2019-02-09 16:48:34 +01:00
|
|
|
names = names .. " " .. k
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
end
|
2019-02-09 16:48:34 +01:00
|
|
|
return true, "Following players share an IP: " .. names
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function ipnames.command_ignore(name, param)
|
|
|
|
if not ipnames.data[param] then
|
2019-02-09 16:48:34 +01:00
|
|
|
return false, "The player '" .. param .. "' did not join yet."
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
ipnames.whitelist[param] = true
|
|
|
|
ipnames.save_whitelist()
|
2019-02-09 16:48:34 +01:00
|
|
|
return true, "Added '" .. param .. "' to the name whitelist."
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function ipnames.command_unignore(name, param)
|
|
|
|
if not ipnames.whitelist[param] then
|
2019-02-09 16:48:34 +01:00
|
|
|
return false, "The player '" .. param .. "' is not on the whitelist."
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
ipnames.whitelist[param] = nil
|
|
|
|
ipnames.save_whitelist()
|
2019-02-09 16:48:34 +01:00
|
|
|
return true, "Removed '" .. param .. "' from the name whitelist."
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
|
2019-02-09 16:48:34 +01:00
|
|
|
local player_auth_exists = minetest.player_exists
|
|
|
|
or function(name)
|
|
|
|
-- 0.4.x support: If you get a nil error here -> update Minetest
|
|
|
|
return minetest.auth_table[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: Use mod storage
|
2014-08-16 09:39:55 +02:00
|
|
|
function ipnames.load_data()
|
|
|
|
local file = io.open(ipnames.file, "r")
|
|
|
|
if not file then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local t = os.time()
|
|
|
|
for line in file:lines() do
|
2019-02-09 16:48:34 +01:00
|
|
|
local data = line:split("|")
|
|
|
|
if #data >= 2 then
|
|
|
|
-- Ignore players which were removed (according to auth)
|
|
|
|
local player_exists = player_auth_exists(data[1])
|
|
|
|
|
|
|
|
if player_exists then
|
|
|
|
data[3] = tonumber(data[3]) or 0
|
|
|
|
-- Remove IP after 2 weeks: Expired
|
|
|
|
if data[3] > 0 and t - data[3] > (3600 * 24 * 14) then
|
|
|
|
player_exists = false
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
end
|
2019-02-09 16:48:34 +01:00
|
|
|
if player_exists then
|
|
|
|
ipnames.data[data[1]] = {data[2], data[3]}
|
|
|
|
end
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
io.close(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ipnames.save_data()
|
|
|
|
if not ipnames.changes then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
ipnames.changes = false
|
2019-02-09 16:48:34 +01:00
|
|
|
|
|
|
|
local contents = {} -- faster than string concat
|
2014-08-16 09:39:55 +02:00
|
|
|
for k, v in pairs(ipnames.data) do
|
2016-05-21 14:01:39 +02:00
|
|
|
v[2] = v[2] or os.time()
|
2019-02-09 16:48:34 +01:00
|
|
|
contents[#contents + 1] = k.."|"..v[1].."|"..v[2]
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
2019-02-09 16:48:34 +01:00
|
|
|
minetest.safe_file_write(ipnames.file, table.concat(contents, "\n"))
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function ipnames.load_whitelist()
|
|
|
|
local file = io.open(ipnames.whitelist_file, "r")
|
|
|
|
if not file then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for line in file:lines() do
|
|
|
|
if line ~= "" then
|
|
|
|
ipnames.whitelist[line] = true
|
|
|
|
end
|
|
|
|
end
|
2019-02-09 16:48:34 +01:00
|
|
|
io.close(file)
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function ipnames.save_whitelist()
|
2019-02-09 16:48:34 +01:00
|
|
|
local names = {} -- faster than string concat
|
2014-08-16 09:39:55 +02:00
|
|
|
for k, v in pairs(ipnames.whitelist) do
|
2019-02-09 16:48:34 +01:00
|
|
|
if v then
|
|
|
|
names[#names + 1] = k
|
2014-08-16 09:39:55 +02:00
|
|
|
end
|
|
|
|
end
|
2019-02-09 16:48:34 +01:00
|
|
|
minetest.safe_file_write(ipnames.whitelist_file, table.concat(names, "\n"))
|
2016-05-21 14:01:39 +02:00
|
|
|
end
|