forked from mtcontrib/names_per_ip
Compare commits
No commits in common. "master" and "master" have entirely different histories.
23
README.md
Normal file
23
README.md
Normal file
@ -0,0 +1,23 @@
|
||||
names_per_ip
|
||||
============
|
||||
|
||||
A mod for Minetest to stop annoyed kids
|
||||
|
||||
It will limit the accounts to 2 (+3 if the player is whitelisted) per IP and delete IP history.
|
||||
|
||||
Initial mod creator: Krock
|
||||
|
||||
License: CC0
|
||||
|
||||
Depends: nothing
|
||||
|
||||
Chat commands
|
||||
-------------
|
||||
|
||||
```
|
||||
/ipnames
|
||||
whois <name> -> Gets all accounts of <name>
|
||||
list -> Lists all exceptions/whitelist entries (players which can have "unlimited" accounts)
|
||||
ignore <name> -> Adds an exception/whitelist entry for <name>
|
||||
unignore <name> -> Removes an exception/whitelist entry for <name>
|
||||
```
|
117
functions.lua
Normal file
117
functions.lua
Normal file
@ -0,0 +1,117 @@
|
||||
-- WARNING: ipnames.command_* are reserved for chat commands!
|
||||
|
||||
function ipnames.command_list(name)
|
||||
local names = {} -- faster than string concat
|
||||
for k, v in pairs(ipnames.whitelist) do
|
||||
if v then
|
||||
names[#names + 1] = k
|
||||
end
|
||||
end
|
||||
return true, "All exceptions: " .. table.concat(names, ", ")
|
||||
end
|
||||
|
||||
function ipnames.command_whois(name, param)
|
||||
if not ipnames.data[param] then
|
||||
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
|
||||
end
|
||||
end
|
||||
return true, "Following players share an IP: " .. names
|
||||
end
|
||||
|
||||
function ipnames.command_ignore(name, param)
|
||||
if not ipnames.data[param] then
|
||||
return false, "The player '" .. param .. "' did not join yet."
|
||||
end
|
||||
|
||||
ipnames.whitelist[param] = true
|
||||
ipnames.save_whitelist()
|
||||
return true, "Added '" .. param .. "' to the name whitelist."
|
||||
end
|
||||
|
||||
function ipnames.command_unignore(name, param)
|
||||
if not ipnames.whitelist[param] then
|
||||
return false, "The player '" .. param .. "' is not on the whitelist."
|
||||
end
|
||||
|
||||
ipnames.whitelist[param] = nil
|
||||
ipnames.save_whitelist()
|
||||
return true, "Removed '" .. param .. "' from the name whitelist."
|
||||
end
|
||||
|
||||
ipnames.is_registered = 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
|
||||
return
|
||||
end
|
||||
local t = os.time()
|
||||
for line in file:lines() do
|
||||
local data = line:split("|")
|
||||
if #data >= 2 then
|
||||
-- Ignore players which were removed (according to auth)
|
||||
local player_exists = ipnames.is_registered(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)
|
||||
end
|
||||
|
||||
function ipnames.save_data()
|
||||
if not ipnames.changes then
|
||||
return
|
||||
end
|
||||
ipnames.changes = false
|
||||
|
||||
local contents = {} -- faster than string concat
|
||||
for k, v in pairs(ipnames.data) do
|
||||
v[2] = v[2] or os.time()
|
||||
contents[#contents + 1] = k.."|"..v[1].."|"..v[2]
|
||||
end
|
||||
minetest.safe_file_write(ipnames.file, table.concat(contents, "\n"))
|
||||
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
|
||||
io.close(file)
|
||||
end
|
||||
|
||||
function ipnames.save_whitelist()
|
||||
local names = {} -- faster than string concat
|
||||
for k, v in pairs(ipnames.whitelist) do
|
||||
if v then
|
||||
names[#names + 1] = k
|
||||
end
|
||||
end
|
||||
minetest.safe_file_write(ipnames.whitelist_file, table.concat(names, "\n"))
|
||||
end
|
179
init.lua
Executable file → Normal file
179
init.lua
Executable file → Normal file
@ -1,110 +1,123 @@
|
||||
minetest.register_privilege("whois", {
|
||||
description = "Allows player to see other player IPs"})
|
||||
|
||||
-- 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.data = {}
|
||||
ipnames.tmp_data = {}
|
||||
ipnames.whitelist = {}
|
||||
ipnames.changes = false
|
||||
ipnames.file = minetest.get_worldpath().."/ipnames.txt"
|
||||
ipnames.file = minetest.get_worldpath().."/ipnames.data"
|
||||
ipnames.whitelist_file = minetest.get_worldpath().."/ipnames_whitelist.data"
|
||||
|
||||
ipnames.name_per_ip_limit = tonumber(minetest.setting_get("max_names_per_ip")) or 5
|
||||
-- Limit 2 = maximal 2 accounts, the 3rd under the same IP gets blocked
|
||||
ipnames.name_per_ip_limit = tonumber(minetest.settings:get("max_names_per_ip")) or 2
|
||||
-- 2 + 3 = 5 accounts as limit for "ignored" players
|
||||
ipnames.extended_limit = 3
|
||||
|
||||
-- Get accounts self:
|
||||
minetest.register_chatcommand("whois", {
|
||||
description = "Gets all players who have the same IP as the specified player",
|
||||
privs = {whois = true},
|
||||
-- Interval where the IP list gets saved/updated
|
||||
ipnames.save_interval = 240
|
||||
|
||||
dofile(minetest.get_modpath("names_per_ip").."/functions.lua")
|
||||
|
||||
minetest.register_chatcommand("ipnames", {
|
||||
description = "Get the features of names_per_ip",
|
||||
privs = {ban=true},
|
||||
func = function(name, param)
|
||||
if not ipnames.data[param] then
|
||||
minetest.chat_send_player(name, "The player \"" .. param .. "\" did not join yet.")
|
||||
return
|
||||
if param == "" then
|
||||
return true,
|
||||
"Available commands:\n" ..
|
||||
"Get all accounts of <name>: /ipnames whois <name>\n" ..
|
||||
"Show all whitelisted names: /ipnames list\n" ..
|
||||
"Add/remove whitelist entry: /ipnames (un)ignore <name>"
|
||||
end
|
||||
if param == "list" then
|
||||
return ipnames.command_list(name)
|
||||
end
|
||||
|
||||
local ip = ipnames.data[param]
|
||||
local names = "";
|
||||
for k, v in pairs(ipnames.data) do
|
||||
if v == ip then
|
||||
if names ~= "" then
|
||||
names = names .. ", " .. k
|
||||
else
|
||||
names = names .. " " .. k
|
||||
end
|
||||
end
|
||||
-- Commands with two arguments
|
||||
local args = param:split(" ")
|
||||
if #args < 2 then
|
||||
return false, "Error: Too few command arguments."
|
||||
end
|
||||
minetest.chat_send_player(name, "Players for IP address " .. ip .. ": " .. names)
|
||||
end,
|
||||
|
||||
local func = ipnames["command_" .. args[1]]
|
||||
if func then
|
||||
return func(name, args[2])
|
||||
end
|
||||
|
||||
return false, "Error: No known action for argument #1 ('"..args[1].."')"
|
||||
end
|
||||
})
|
||||
|
||||
-- Get IP if player tries to join, ban if there are too much names per IP:
|
||||
-- Get IP if player tries to join, ban if there are too much names per IP
|
||||
minetest.register_on_prejoinplayer(function(name, ip)
|
||||
-- Only stop new accounts:
|
||||
ipnames.tmp_data[name] = ip
|
||||
if not ipnames.data[name] then
|
||||
local count = 1
|
||||
local names = ""
|
||||
for k, v in pairs(ipnames.data) do
|
||||
if v == ip then
|
||||
count = count + 1
|
||||
names = names .. k .. ", "
|
||||
end
|
||||
end
|
||||
-- Only stop new accounts
|
||||
if ipnames.data[name] or ipnames.is_registered(name) then
|
||||
return
|
||||
end
|
||||
|
||||
if count <= ipnames.name_per_ip_limit and count > 1 then
|
||||
minetest.log("action", name .. " now has " .. count .. " accounts. Other accounts: " .. names)
|
||||
end
|
||||
|
||||
if count > ipnames.name_per_ip_limit then
|
||||
ipnames.tmp_data[name] = nil
|
||||
if tostring(ip) ~= "127.0.0.1" then
|
||||
return ("\nYou exceeded the limit of accounts (" .. ipnames.name_per_ip_limit ..
|
||||
").\nYou already have the following accounts:\n" .. names)
|
||||
local names = {} -- faster than string concat
|
||||
local count_bonus = nil
|
||||
for k, v in pairs(ipnames.data) do
|
||||
if v[1] == ip then
|
||||
if not count_bonus and ipnames.whitelist[k] then
|
||||
count_bonus = ipnames.extended_limit
|
||||
end
|
||||
names[#names + 1] = k
|
||||
end
|
||||
end
|
||||
|
||||
-- Return error message if too many accounts have been created
|
||||
if #names > ipnames.name_per_ip_limit + (count_bonus or 0) then
|
||||
return "\nYou exceeded the limit of accounts.\n" ..
|
||||
"You already own the following accounts:\n" .. table.concat(names, ", ")
|
||||
end
|
||||
end)
|
||||
|
||||
-- Save IP if player joined:
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
local name = player:get_player_name()
|
||||
ipnames.data[name] = ipnames.tmp_data[name]
|
||||
ipnames.tmp_data[name] = nil
|
||||
-- Save IP if player joined
|
||||
local function update_player_address(name, recursive)
|
||||
local info = minetest.get_player_information(name)
|
||||
local address = info and info.address
|
||||
|
||||
if not address then
|
||||
minetest.log("warning", "[names_per_ip] minetest.get_player_information(\"" ..
|
||||
name .. "\"): " .. dump(info) .. ". This is probably an engine bug.")
|
||||
if not recursive then
|
||||
-- Delay, hope it works next time
|
||||
minetest.after(0.5, update_player_address, name, true)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
ipnames.data[name] = {
|
||||
address or "??",
|
||||
os.time()
|
||||
}
|
||||
|
||||
ipnames.changes = true
|
||||
ipnames.save_data()
|
||||
end
|
||||
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
update_player_address(player:get_player_name())
|
||||
end)
|
||||
|
||||
function ipnames.load_data()
|
||||
local file = io.open(ipnames.file, "r")
|
||||
if not file then
|
||||
return
|
||||
end
|
||||
for line in file:lines() do
|
||||
if line ~= "" then
|
||||
local data = line:split("|")
|
||||
if #data >= 2 then
|
||||
ipnames.data[data[1]] = data[2]
|
||||
end
|
||||
end
|
||||
end
|
||||
io.close(file)
|
||||
-- Data saving routine
|
||||
-- Save changes at a fixed interval
|
||||
local function save_data_job()
|
||||
ipnames.save_data()
|
||||
minetest.after(ipnames.save_interval, save_data_job)
|
||||
end
|
||||
minetest.after(ipnames.save_interval, save_data_job)
|
||||
minetest.register_on_shutdown(ipnames.save_data)
|
||||
|
||||
function ipnames.save_data()
|
||||
if not ipnames.changes then
|
||||
return
|
||||
end
|
||||
ipnames.changes = false
|
||||
local file = io.open(ipnames.file, "w")
|
||||
for i, v in pairs(ipnames.data) do
|
||||
if v ~= nil then
|
||||
file:write(i .. "|" .. v .. "\n")
|
||||
end
|
||||
end
|
||||
io.close(file)
|
||||
end
|
||||
-- Due to use of minetest.player_exists, the data loading must be delayed
|
||||
-- until ServerEnvironment is set up. register_on_mods_loaded is still too early.
|
||||
minetest.after(0, ipnames.load_data)
|
||||
|
||||
ipnames.load_whitelist()
|
||||
|
||||
minetest.register_on_shutdown(function() ipnames.save_data() end)
|
||||
|
||||
minetest.after(3, function() ipnames.load_data() end)
|
||||
minetest.log("action", "[names_per_ip] loaded.")
|
||||
|
Loading…
Reference in New Issue
Block a user