1
0
mirror of https://github.com/SmallJoker/names_per_ip.git synced 2024-11-14 22:30:37 +01:00
names_per_ip/init.lua

103 lines
2.4 KiB
Lua

-- Created by Krock to stop mass-account-creators
-- License: WTFPL
ipnames = {}
ipnames.data = {}
ipnames.tmp_data = {}
ipnames.changes = false
ipnames.save_interval = 240
ipnames.save_time = 0
ipnames.file = minetest.get_worldpath().."/ipnames.data"
ipnames.name_per_ip_limit = 3
-- Get accounts self.
minetest.register_chatcommand("whois", {
description = "Gets all player names which have the same IP as <param>.",
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
end
local ip = ipnames.data[param]
local names = "";
for k, v in pairs(ipnames.data) do
if v == ip then
names = names.." "..k
end
end
minetest.chat_send_player(name, "Following players share an IP: "..names)
end,
})
-- 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
if count > ipnames.name_per_ip_limit then
ipnames.tmp_data[name] = nil
return ("\nYou exceeded the limit of accounts.\nYou already own the following accounts:\n"..names)
end
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
ipnames.changes = true
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)
end
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
minetest.register_globalstep(function(t)
ipnames.save_time = ipnames.save_time + t
if ipnames.save_time < ipnames.save_interval then
return
end
ipnames.save_time = 0
ipnames.save_data()
end)
minetest.register_on_shutdown(function() ipnames.save_data() end)
minetest.after(3, function() ipnames.load_data() end)