1
0
mirror of https://github.com/SmallJoker/names_per_ip.git synced 2024-11-10 20:30:27 +01: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:
SmallJoker 2019-02-09 16:48:34 +01:00
parent 353e651454
commit 16e1fa84c2
3 changed files with 75 additions and 68 deletions

View File

@ -7,7 +7,7 @@ It will limit the accounts to 2 (+3 if the player is whitelisted) per IP and del
Initial mod creator: Krock Initial mod creator: Krock
License: WTFPL License: CC0
Depends: nothing Depends: nothing

View File

@ -1,49 +1,57 @@
-- WARNING: ipnames.command_* are reserved for chat commands!
function ipnames.command_list(name) function ipnames.command_list(name)
local names = "" local names = {} -- faster than string concat
for k, v in pairs(ipnames.whitelist) do 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) end
return true, "All exceptions: " .. table.concat(names, ", ")
end end
function ipnames.command_whois(name, param) function ipnames.command_whois(name, param)
if not ipnames.data[param] then if not ipnames.data[param] then
minetest.chat_send_player(name, "The player '"..param.."' did not join yet.") return false, "The player '" .. param .. "' did not join yet."
return
end end
local ip = ipnames.data[param][1] local ip = ipnames.data[param][1]
local names = "" local names = ""
for k, v in pairs(ipnames.data) do for k, v in pairs(ipnames.data) do
if v[1] == ip then if v[1] == ip then
names = names.." "..k names = names .. " " .. k
end end
end end
minetest.chat_send_player(name, "Following players share an IP: "..names) return true, "Following players share an IP: " .. names
end end
function ipnames.command_ignore(name, param) function ipnames.command_ignore(name, param)
if not ipnames.data[param] then if not ipnames.data[param] then
minetest.chat_send_player(name, "The player '"..param.."' did not join yet.") return false, "The player '" .. param .. "' did not join yet."
return
end end
ipnames.whitelist[param] = true ipnames.whitelist[param] = true
minetest.chat_send_player(name, "Added an exception!")
ipnames.save_whitelist() ipnames.save_whitelist()
return true, "Added '" .. param .. "' to the name whitelist."
end end
function ipnames.command_unignore(name, param) function ipnames.command_unignore(name, param)
if not ipnames.whitelist[param] then if not ipnames.whitelist[param] then
minetest.chat_send_player(name, "The player '"..param.."' is not on the whitelist.") return false, "The player '" .. param .. "' is not on the whitelist."
return
end end
ipnames.whitelist[param] = nil ipnames.whitelist[param] = nil
minetest.chat_send_player(name, "Removed an exception!")
ipnames.save_whitelist() ipnames.save_whitelist()
return true, "Removed '" .. param .. "' from the name whitelist."
end 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() function ipnames.load_data()
local file = io.open(ipnames.file, "r") local file = io.open(ipnames.file, "r")
if not file then if not file then
@ -51,27 +59,23 @@ function ipnames.load_data()
end end
local t = os.time() local t = os.time()
for line in file:lines() do for line in file:lines() do
if line ~= "" then
local data = line:split("|") local data = line:split("|")
if #data >= 2 then if #data >= 2 then
-- Special stuff - only save if player has not been deleted yet -- Ignore players which were removed (according to auth)
local ignore = false local player_exists = player_auth_exists(data[1])
if not minetest.auth_table[data[1]] then
ignore = true if player_exists then
end
if not ignore then
data[3] = tonumber(data[3]) or 0 data[3] = tonumber(data[3]) or 0
-- Remove IP after 2 weeks -- Remove IP after 2 weeks: Expired
if data[3] > 0 and t - data[3] > (3600 * 24 * 14) then if data[3] > 0 and t - data[3] > (3600 * 24 * 14) then
ignore = true player_exists = false
end end
end end
if not ignore then if player_exists then
ipnames.data[data[1]] = {data[2], data[3]} ipnames.data[data[1]] = {data[2], data[3]}
end end
end end
end end
end
io.close(file) io.close(file)
end end
@ -80,12 +84,13 @@ function ipnames.save_data()
return return
end end
ipnames.changes = false ipnames.changes = false
local file = io.open(ipnames.file, "w")
local contents = {} -- faster than string concat
for k, v in pairs(ipnames.data) do for k, v in pairs(ipnames.data) do
v[2] = v[2] or os.time() v[2] = v[2] or os.time()
file:write(k.."|"..v[1].."|"..v[2].."\n") contents[#contents + 1] = k.."|"..v[1].."|"..v[2]
end end
io.close(file) minetest.safe_file_write(ipnames.file, table.concat(contents, "\n"))
end end
function ipnames.load_whitelist() function ipnames.load_whitelist()
@ -98,14 +103,15 @@ function ipnames.load_whitelist()
ipnames.whitelist[line] = true ipnames.whitelist[line] = true
end end
end end
io.close(file)
end end
function ipnames.save_whitelist() 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 for k, v in pairs(ipnames.whitelist) do
if v ~= nil then if v then
file:write(k.."\n") names[#names + 1] = k
end end
end end
io.close(file) minetest.safe_file_write(ipnames.whitelist_file, table.concat(names, "\n"))
end end

View File

@ -1,5 +1,11 @@
-- Created by Krock to stop mass-account-creators -- Created by Krock to stop mass-account-creators
-- License: WTFPL -- License: CC0
if not minetest.safe_file_write then
error("[simple_protection] Your Minetest version is no longer supported."
.. " (version < 0.4.17)")
end
ipnames = {} ipnames = {}
ipnames.data = {} ipnames.data = {}
ipnames.tmp_data = {} ipnames.tmp_data = {}
@ -24,32 +30,28 @@ minetest.register_chatcommand("ipnames", {
privs = {ban=true}, privs = {ban=true},
func = function(name, param) func = function(name, param)
if param == "" then if param == "" then
minetest.chat_send_player(name, "Available commands: ") return true,
minetest.chat_send_player(name, "Get all accounts of <name>: /ipnames whois <name>") "Available commands:\n" ..
minetest.chat_send_player(name, "List all exceptions: /ipnames list") "Get all accounts of <name>: /ipnames whois <name>\n" ..
minetest.chat_send_player(name, "Remove/add an exception: /ipnames (un)ignore <name>") "Show all whitelisted names: /ipnames list\n" ..
return "Add/remove whitelist entry: /ipnames (un)ignore <name>"
end end
if param == "list" then if param == "list" then
ipnames.command_list(name) return ipnames.command_list(name)
return
end end
-- Commands with two arguments
local args = param:split(" ") local args = param:split(" ")
if #args < 2 then if #args < 2 then
minetest.chat_send_player(name, "Error: Please check again '/ipnames' for correct usage.") return false, "Error: Too few command arguments."
return
end end
if args[1] == "whois" then local func = ipnames["command_" .. args[1]]
ipnames.command_whois(name, args[2]) if func then
elseif args[1] == "ignore" then return func(name, args[2])
ipnames.command_ignore(name, args[2])
elseif args[1] == "unignore" then
ipnames.command_unignore(name, args[2])
else
minetest.chat_send_player(name, "Error: No known argument for #1 '"..args[1].."'")
end end
return false, "Error: No known action for argument #1 ('"..args[1].."')"
end end
}) })
@ -62,8 +64,7 @@ minetest.register_on_prejoinplayer(function(name, ip)
return return
end end
local count = 1 local names = {} -- faster than string concat
local names = ""
local limit_ext = false local limit_ext = false
for k, v in pairs(ipnames.data) do for k, v in pairs(ipnames.data) do
if v[1] == ip then if v[1] == ip then
@ -71,14 +72,14 @@ minetest.register_on_prejoinplayer(function(name, ip)
count = count - ipnames.extended_limit count = count - ipnames.extended_limit
limit_ext = true limit_ext = true
end end
count = count + 1 names[#names + 1] = k
names = names..k..", "
end end
end end
-- Return error message if too many accounts have been created -- Return error message if too many accounts have been created
if count > ipnames.name_per_ip_limit then if #names > ipnames.name_per_ip_limit then
ipnames.tmp_data[name] = nil ipnames.tmp_data[name] = nil
return ("\nYou exceeded the limit of accounts.\nYou already own the following accounts:\n"..names) return "\nYou exceeded the limit of accounts.\n" ..
"You already own the following accounts:\n" .. table.concat(names, ", ")
end end
end) end)
@ -100,7 +101,7 @@ minetest.register_globalstep(function(t)
ipnames.save_data() ipnames.save_data()
end) end)
minetest.register_on_shutdown(function() ipnames.save_data() end) minetest.register_on_shutdown(ipnames.save_data)
minetest.after(3, function() ipnames.load_data() end) minetest.after(3, ipnames.load_data)
minetest.after(3, function() ipnames.load_whitelist() end) minetest.after(3, ipnames.load_whitelist)