Implement minetest.register_can_bypass_userlimit (#6369)
authorLoïc Blot <nerzhul@users.noreply.github.com>
Mon, 4 Sep 2017 20:08:59 +0000 (22:08 +0200)
committerGitHub <noreply@github.com>
Mon, 4 Sep 2017 20:08:59 +0000 (22:08 +0200)
* Implement minetest.register_on_userlimit_check

This new callback permits to bypass the max_users parameter with new mods condition, based on player name or IP
Only one mod needs to permit it.

Move core part for builtin privileges checks to builtin

builtin/game/privileges.lua
builtin/game/register.lua
doc/lua_api.txt
src/network/serverpackethandler.cpp
src/script/cpp_api/s_player.cpp
src/script/cpp_api/s_player.h

index fb53423eb948140eb4f17eee3a2df75b8d55a2e1..e9b2df54cc556d52860a816db8f158135ffe0e68 100644 (file)
@@ -86,3 +86,8 @@ core.register_privilege("debug", {
        description = "Allows enabling various debug options that may affect gameplay",
        give_to_singleplayer = false,
 })
+
+core.register_can_bypass_userlimit(function(name, ip)
+       local privs = core.get_player_privs(name)
+       return privs["server"] or privs["ban"] or privs["privs"] or privs["password"]
+end)
index 1f355c7e56e2369e7396c16233bcb2e8bf19b583..e0073fc038a20a7c362d8eecb1a5d7d35d46b308 100644 (file)
@@ -581,6 +581,7 @@ core.registered_on_item_eats, core.register_on_item_eat = make_registration()
 core.registered_on_punchplayers, core.register_on_punchplayer = make_registration()
 core.registered_on_priv_grant, core.register_on_priv_grant = make_registration()
 core.registered_on_priv_revoke, core.register_on_priv_revoke = make_registration()
+core.registered_can_bypass_userlimit, core.register_can_bypass_userlimit = make_registration()
 
 --
 -- Compatibility for on_mapgen_init()
index a6d02ebb5ada8471aafe39dfa29f6c40b1b17063..ec5a8ff3238d6539a63ea457e2dc413e65f192c9 100644 (file)
@@ -2470,6 +2470,9 @@ Call these functions only at load time!
     * Called when `revoker` revokes the priv `priv` from `name`.
     * Note that the callback will be called twice if it's done by a player, once with revoker being the player name,
       and again with revoker being nil.
+* `minetest.register_can_bypass_userlimit(function(name, ip))`
+    * Called when `name` user connects with `ip`.
+    * Return `true` to by pass the player limit
 
 ### Other registration functions
 * `minetest.register_chatcommand(cmd, chatcommand definition)`
index 62da136ad607745aea28cb555403ad2296c7a1e9..ecf4306c8ea93691dc36cece65870e28ff415ff2 100644 (file)
@@ -209,13 +209,10 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
                        << addr_s << " (peer_id=" << pkt->getPeerId() << ")" << std::endl;
 
        // Enforce user limit.
-       // Don't enforce for users that have some admin right
+       // Don't enforce for users that have some admin right or mod permits it.
        if (m_clients.isUserLimitReached() &&
-                       !checkPriv(playername, "server") &&
-                       !checkPriv(playername, "ban") &&
-                       !checkPriv(playername, "privs") &&
-                       !checkPriv(playername, "password") &&
-                       playername != g_settings->get("name")) {
+                       playername != g_settings->get("name") &&
+                       !m_script->can_bypass_userlimit(playername, addr_s)) {
                actionstream << "Server: " << playername << " tried to join from "
                                << addr_s << ", but there" << " are already max_users="
                                << g_settings->getU16("max_users") << " players." << std::endl;
index b7f2f10f9b5e9ec6229d858df41ba6a1bcdb7aec..578c26184dc6882c2d75b4b99a19837239b0bc50 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "cpp_api/s_internal.h"
 #include "common/c_converter.h"
 #include "common/c_content.h"
+#include "debug.h"
 #include "util/string.h"
 
 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
@@ -123,6 +124,20 @@ bool ScriptApiPlayer::on_prejoinplayer(
        return false;
 }
 
+bool ScriptApiPlayer::can_bypass_userlimit(const std::string &name, const std::string &ip)
+{
+       SCRIPTAPI_PRECHECKHEADER
+
+       // Get core.registered_on_prejoinplayers
+       lua_getglobal(L, "core");
+       lua_getfield(L, -1, "registered_can_bypass_userlimit");
+       lua_pushstring(L, name.c_str());
+       lua_pushstring(L, ip.c_str());
+       runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+       FATAL_ERROR_IF(!lua_isboolean(L, -1), "on_user_limitcheck must return a boolean");
+       return lua_toboolean(L, -1);
+}
+
 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
 {
        SCRIPTAPI_PRECHECKHEADER
index faf394de53e33d7fa855502ebb1881719445be12..6b752eb69d30a8d72b80067bdea37e4179ed1d37 100644 (file)
@@ -35,6 +35,7 @@ public:
        bool on_respawnplayer(ServerActiveObject *player);
        bool on_prejoinplayer(const std::string &name, const std::string &ip,
                        std::string *reason);
+       bool can_bypass_userlimit(const std::string &name, const std::string &ip);
        void on_joinplayer(ServerActiveObject *player);
        void on_leaveplayer(ServerActiveObject *player, bool timeout);
        void on_cheat(ServerActiveObject *player, const std::string &cheat_type);