From 7aec733ad6ceef3a9dfdf9eacef5e92baa4ed3c0 Mon Sep 17 00:00:00 2001 From: Dorian Wouters Date: Tue, 23 Aug 2016 11:28:52 +0200 Subject: [PATCH] Add non-pattern banned name list; ability to reload it & patterns --- init.lua | 133 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 109 insertions(+), 24 deletions(-) diff --git a/init.lua b/init.lua index 94abfd2..289fbdd 100644 --- a/init.lua +++ b/init.lua @@ -17,50 +17,126 @@ temp = nil exemptions[minetest.setting_get("name")] = true exemptions["singleplayer"] = true +local disallowed_names + +local function load_forbidden_names() + disallowed_names = {} + local path = minetest.setting_get("name_restrictions.forbidden_names_list_path") or + minetest.get_worldpath("name_restrictions") .. "/forbidden_names.txt" + local file = io.open(path, 'r') + if file then + local count = 0 + for line in file:lines() do + local low_line = line:lower() + disallowed_names[low_line] = true + count = count + 1 + end + file:close() + return true, count .. " forbidden names loaded", 'verbose' + else + disallowed_names = {} + return true, path .. " doesn't exist, no forbidden names have been loaded", 'warning' + end +end + +do + local ret, msg, lvl = load_forbidden_names() + if msg and lvl then + minetest.log(lvl, msg) + end +end --------------------- -- Simple matching -- --------------------- -local msg_guest = "Guest accounts are disallowed on this server. ".. - "Please choose a proper name and try again." -local msg_misleading = "Your player name is misleading. ".. - "Please choose a more appropriate name." -local disallowed = { - ["^guest[0-9]+"] = msg_guest, - ["^squeakecrafter[0-9]+"] = msg_guest, - ["adm[1il]n"] = msg_misleading, - ["[0o]wn[e3]r"] = msg_misleading, - ["^[0-9]+$"] = "All-numeric usernames are disallowed on this server.", -} +local disallowed + +local function load_disallowed() + local path = minetest.setting_get("name_restrictions.forbidden_name_patterns_list_path") or + minetest.get_worldpath("name_restrictions") .. "/forbidden_names_patterns.txt" + local file = io.open(path, 'r') + if file then + local content = file:read('*all') + local imported = minetest.deserialize(content) + local ret, msg, lvl = true, nil, nil + if imported == nil then + disallowed = disallowed or {} + ret, msg, lvl = false, "Failed to parse " .. path .. "; patterns not imported", 'error' + elseif type(imported) ~= 'table' then + disallowed = disallowed or {} + ret, msg, lvl = false, "Parsing " .. path .. " returned a " .. type(imported) .. + " where a table was expected; patterns not imported", 'error' + else + disallowed = imported + local count = 0 + for _ in pairs(disallowed) do + count = count + 1 + end + msg, lvl = count .. " forbidden name patterns loaded", 'verbose' + end + file:close() + return ret, msg, lvl + else + disallowed = {} + return true, path .. " doesn't exist, no forbidden name patterns have been loaded", 'warning' + end +end + +do + local ret, msg, lvl = load_disallowed() + if msg and lvl then + minetest.log(lvl, msg) + end +end minetest.register_on_prejoinplayer(function(name, ip) - if exemptions[name] then return end - - -- Check for disallowed names local lname = name:lower() for re, reason in pairs(disallowed) do if lname:find(re) then return reason end end + if disallowed_names[lname] then + return "Sorry. This name is forbidden." + end end) +-------------------------------------- +-- Simple matching config reloading -- +-------------------------------------- + +minetest.register_chatcommand("forbidden_names_reload", { + params = "", + description = "Reloads forbidden_names match lists", + privs = {ban= true}, + func = function(name) + local ret1, msg, lvl = load_forbidden_names() + if msg and lvl then + minetest.log(lvl, msg) + minetest.chat_send_player(name, msg) + end + local ret2 + ret2, msg, lvl = load_disallowed() + if msg and lvl then + minetest.log(lvl, msg) + minetest.chat_send_player(name, msg) + end + return ret1 and ret2 + end +}) ------------------------ -- Case-insensitivity -- ------------------------ minetest.register_on_prejoinplayer(function(name, ip) - if exemptions[name] then return end - - -- Check for used names local lname = name:lower() for iname, data in pairs(minetest.auth_table) do if iname:lower() == lname and iname ~= name then return "Sorry, someone else is already using this" .." name. Please pick another name." - .." Annother possibility is that you used the" + .." Another possibility is that you used the" .." wrong case for your name." end end @@ -68,9 +144,9 @@ end) -- Compatability, for old servers with conflicting players minetest.register_chatcommand("choosecase", { - description = "Choose the casing that a player name should have.", + description = "Choose the casing that a player name should have", params = "", - privs = {server=true}, + privs = {server = true}, func = function(name, params) local lname = params:lower() local worldpath = minetest.get_worldpath() @@ -78,7 +154,7 @@ minetest.register_chatcommand("choosecase", { if iname:lower() == lname and iname ~= params then minetest.auth_table[iname] = nil assert(not iname:find("[/\\]")) - os.remove(worldpath.."/players/"..iname) + os.remove(worldpath .. "/players/" .. iname) end end return true, "Done." @@ -92,7 +168,7 @@ minetest.register_chatcommand("choosecase", { -- Prevents names that are too similar to another player's name. local similar_chars = { - -- Only A-Z, a-z, 1-9, dash, and underscore are allowed in player names + -- Only A-Z, a-z, 1-9, dash, and underscore are allowed in playernames "A4", "B8", "COco0", @@ -148,13 +224,22 @@ end) -- Name length -- ----------------- -local min_name_len = tonumber(minetest.setting_get("name_restrictions.minimum_name_length")) or 3 +local min_name_len = tonumber(minetest.setting_get("name_restrictions.minimum_name_length")) or 2 +local max_name_len = tonumber(minetest.setting_get("name_restrictions.maximum_name_length")) or 17 minetest.register_on_prejoinplayer(function(name, ip) if exemptions[name] then return end if #name < min_name_len then - return "Your player name is too short, please try a longer name." + return "Your player name is too short" + .. " (" .. #name .. " characters, must be " .. min_name_len .. " characters at least)." + .. " Please try a longer name." + end + + if #name > max_name_len then + return "Your player name is too long" + .. " (" .. #name .. " characters, must be " .. max_name_len .. " characters at most)." + .. " Please try a shorter name." end end)