Page 1 of 1

[MOD]Whitelist[whitelist]

PostPosted: Thu Jan 02, 2014 20:04
by Pitriss
Simple whitelist mod.
You must specify admin name in minetest.conf to be able join server and setup allowed players.
For changing this setup use /allow <nickname> command. If you want someone to refuse playing on server just use /disallow <nickname> or delete him from allow.conf in your world directory. For these commands basic_privs are required.

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local config_file = minetest.get_worldpath().."/allow.conf"
--in case of not existant config file, it
--will create it
local file_desc = io.open(config_file, "a")
file_desc:close()

local config = Settings(config_file)

local admin_name = minetest.setting_get("name")
if admin_name ~= nil then
    config:set(admin_name, "true")
    config:write()
end


minetest.register_chatcommand("allow", {
    param = "allow <nickname>",
    privs = {basic_privs=true},
    description = "Allows joining for specified nickname.",
    func = function(name, param)
        local player = minetest.get_player_by_name(name)
        if not player then
            return
        end
        -- Handling parameter
        if param == "" then
            minetest.chat_send_player(name, "You must provide nickname to allow.");
            return
        else
            config:set(param, "true")
            config:write()
            minetest.chat_send_player(name, param.." is now allowed to join server.");
            return
        end
    end
})

minetest.register_chatcommand("disallow", {
    param = "disallow <nickname>",
    privs = {basic_privs=true},
    description = "Disallows joining for specified nickname.",
    func = function(name, param)
        local player = minetest.get_player_by_name(name)
        if not player then
            return
        end
        -- Handling parameter
        if param == "" then
            minetest.chat_send_player(name, "You must provide nickname to disallow.");
            return
        else
            config:set(param, "false")
            config:write()
            minetest.chat_send_player(name, param.." is now not allowed to join server.");
            return
        end
    end
})



minetest.register_on_prejoinplayer(function(name, ip)
    local joining = config:get_bool(name)
    if joining == nil or joining == false then
        minetest.chat_send_all("-!- Server "..name.." tried to join server.")
        return "Sorry you need permission to join this server."
    end
end)


Licence: WTFPL
Dependency: default

This mod was written in 10 minutes, so coding style is not perfect.

EDIT: Fixed privs handling. TY Krock

PostPosted: Thu Jan 02, 2014 21:02
by fairiestoy
Nice work. Though i have a suggestion. Maybe transform this line
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.chat_send_all("-!- Server "..name.." tried to join server.")

into a log file or something. Since this current setup would allow people to flood your server with tons of
"-!- Server trololo tried to join server."
"-!- Server trololo tried to join server."
"-!- Server trololo tried to join server."
"-!- Server trololo tried to join server."
"-!- Server trololo tried to join server."
Saw something like that in another game, and it was more than annoying ;)

PostPosted: Thu Jan 02, 2014 21:17
by Pitriss
Thanks. I'm aware of this.. But for our purpose it is better as it is now.. When someone will be abusing this, then I have no problem to handle this.. BTW logging is done automagically.

PostPosted: Thu Jan 02, 2014 21:23
by Krock
Pitriss wrote:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
..
        if not minetest.check_player_privs(name, {basic_privs}) then
            minetest.chat_send_player(name, "Hey "..name..", you are not allowed to use that command. Privs needed: basic_privs");
            return
        end

...
        if not minetest.check_player_privs(name, {basic_privs}) then
            minetest.chat_send_player(name, "Hey "..name..", you are not allowed to use that command. Privs needed: basic_privs");
            return
        end
...


This mod was written in 10 minutes, so coding style is not perfect.

Yes, im my view are the codes abrove totally useless, just add this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
privs = {basic_privs=true},

and it should say that itself.

PostPosted: Thu Jan 02, 2014 21:47
by Pitriss
Ok fixed, TY.