mirror of
https://github.com/SmallJoker/names_per_ip.git
synced 2025-06-30 14:40:20 +02:00
Big mod renewal: License CC0
Use minetest.safe_file_write Pass chat answers by return value Close file handle in load_whitelist (bugfix)
This commit is contained in:
@ -1,49 +1,57 @@
|
||||
-- WARNING: ipnames.command_* are reserved for chat commands!
|
||||
|
||||
function ipnames.command_list(name)
|
||||
local names = ""
|
||||
local names = {} -- faster than string concat
|
||||
for k, v in pairs(ipnames.whitelist) do
|
||||
names = names.." "..k
|
||||
if v then
|
||||
names[#names + 1] = k
|
||||
end
|
||||
end
|
||||
minetest.chat_send_player(name, "All exceptions: "..names)
|
||||
return true, "All exceptions: " .. table.concat(names, ", ")
|
||||
end
|
||||
|
||||
function ipnames.command_whois(name, param)
|
||||
if not ipnames.data[param] then
|
||||
minetest.chat_send_player(name, "The player '"..param.."' did not join yet.")
|
||||
return
|
||||
return false, "The player '" .. param .. "' did not join yet."
|
||||
end
|
||||
|
||||
local ip = ipnames.data[param][1]
|
||||
local names = ""
|
||||
for k, v in pairs(ipnames.data) do
|
||||
if v[1] == ip then
|
||||
names = names.." "..k
|
||||
names = names .. " " .. k
|
||||
end
|
||||
end
|
||||
minetest.chat_send_player(name, "Following players share an IP: "..names)
|
||||
return true, "Following players share an IP: " .. names
|
||||
end
|
||||
|
||||
function ipnames.command_ignore(name, param)
|
||||
if not ipnames.data[param] then
|
||||
minetest.chat_send_player(name, "The player '"..param.."' did not join yet.")
|
||||
return
|
||||
return false, "The player '" .. param .. "' did not join yet."
|
||||
end
|
||||
|
||||
ipnames.whitelist[param] = true
|
||||
minetest.chat_send_player(name, "Added an exception!")
|
||||
ipnames.save_whitelist()
|
||||
return true, "Added '" .. param .. "' to the name whitelist."
|
||||
end
|
||||
|
||||
function ipnames.command_unignore(name, param)
|
||||
if not ipnames.whitelist[param] then
|
||||
minetest.chat_send_player(name, "The player '"..param.."' is not on the whitelist.")
|
||||
return
|
||||
return false, "The player '" .. param .. "' is not on the whitelist."
|
||||
end
|
||||
|
||||
ipnames.whitelist[param] = nil
|
||||
minetest.chat_send_player(name, "Removed an exception!")
|
||||
ipnames.save_whitelist()
|
||||
return true, "Removed '" .. param .. "' from the name whitelist."
|
||||
end
|
||||
|
||||
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
|
||||
function ipnames.load_data()
|
||||
local file = io.open(ipnames.file, "r")
|
||||
if not file then
|
||||
@ -51,25 +59,21 @@ function ipnames.load_data()
|
||||
end
|
||||
local t = os.time()
|
||||
for line in file:lines() do
|
||||
if line ~= "" then
|
||||
local data = line:split("|")
|
||||
if #data >= 2 then
|
||||
-- Special stuff - only save if player has not been deleted yet
|
||||
local ignore = false
|
||||
if not minetest.auth_table[data[1]] then
|
||||
ignore = true
|
||||
end
|
||||
if not ignore then
|
||||
data[3] = tonumber(data[3]) or 0
|
||||
-- Remove IP after 2 weeks
|
||||
if data[3] > 0 and t - data[3] > (3600 * 24 * 14) then
|
||||
ignore = true
|
||||
end
|
||||
end
|
||||
if not ignore then
|
||||
ipnames.data[data[1]] = {data[2], data[3]}
|
||||
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
|
||||
end
|
||||
end
|
||||
if player_exists then
|
||||
ipnames.data[data[1]] = {data[2], data[3]}
|
||||
end
|
||||
end
|
||||
end
|
||||
io.close(file)
|
||||
@ -80,12 +84,13 @@ function ipnames.save_data()
|
||||
return
|
||||
end
|
||||
ipnames.changes = false
|
||||
local file = io.open(ipnames.file, "w")
|
||||
|
||||
local contents = {} -- faster than string concat
|
||||
for k, v in pairs(ipnames.data) do
|
||||
v[2] = v[2] or os.time()
|
||||
file:write(k.."|"..v[1].."|"..v[2].."\n")
|
||||
contents[#contents + 1] = k.."|"..v[1].."|"..v[2]
|
||||
end
|
||||
io.close(file)
|
||||
minetest.safe_file_write(ipnames.file, table.concat(contents, "\n"))
|
||||
end
|
||||
|
||||
function ipnames.load_whitelist()
|
||||
@ -98,14 +103,15 @@ function ipnames.load_whitelist()
|
||||
ipnames.whitelist[line] = true
|
||||
end
|
||||
end
|
||||
io.close(file)
|
||||
end
|
||||
|
||||
function ipnames.save_whitelist()
|
||||
local file = io.open(ipnames.whitelist_file, "w")
|
||||
local names = {} -- faster than string concat
|
||||
for k, v in pairs(ipnames.whitelist) do
|
||||
if v ~= nil then
|
||||
file:write(k.."\n")
|
||||
if v then
|
||||
names[#names + 1] = k
|
||||
end
|
||||
end
|
||||
io.close(file)
|
||||
minetest.safe_file_write(ipnames.whitelist_file, table.concat(names, "\n"))
|
||||
end
|
||||
|
Reference in New Issue
Block a user