]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Add 'on_prejoinplayer' callback
authorkaeza <kaeza@users.sf.net>
Thu, 12 Dec 2013 06:51:35 +0000 (04:51 -0200)
committerShadowNinja <shadowninja@minetest.net>
Thu, 12 Dec 2013 18:42:14 +0000 (13:42 -0500)
builtin/misc_register.lua
doc/lua_api.txt
src/script/cpp_api/s_player.cpp
src/script/cpp_api/s_player.h
src/server.cpp

index 249d272eb579aa663b6502b9f276560555cc6d2a..6c50ff02e53e7d3426c979dcf8ef4840d0341a34 100644 (file)
@@ -380,6 +380,7 @@ minetest.registered_on_generateds, minetest.register_on_generated = make_registr
 minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
 minetest.registered_on_dieplayers, minetest.register_on_dieplayer = make_registration()
 minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()
+minetest.registered_on_prejoinplayers, minetest.register_on_prejoinplayer = make_registration()
 minetest.registered_on_joinplayers, minetest.register_on_joinplayer = make_registration()
 minetest.registered_on_leaveplayers, minetest.register_on_leaveplayer = make_registration()
 minetest.registered_on_player_receive_fields, minetest.register_on_player_receive_fields = make_registration_reverse()
index cc8044fa931b8dfc5c5f7d59c868646b172edcf5..ae714444212d1eaa2984ce7d90825461eea54896 100644 (file)
@@ -1175,6 +1175,9 @@ minetest.register_on_respawnplayer(func(ObjectRef))
 ^ Called when player is to be respawned
 ^ Called _before_ repositioning of player occurs
 ^ return true in func to disable regular player placement
+minetest.register_on_prejoinplayer(func(name, ip))
+^ Called before a player joins the game
+^ If it returns a string, the player is disconnected with that string as reason
 minetest.register_on_joinplayer(func(ObjectRef))
 ^ Called when a player joins the game
 minetest.register_on_leaveplayer(func(ObjectRef))
index 215a34d534578dfcb8bd6d4f677a40739e3148eb..d357689f204bd289c41624688ec30c04db19a98a 100644 (file)
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "cpp_api/s_player.h"
 #include "cpp_api/s_internal.h"
+#include "util/string.h"
 
 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
 {
@@ -58,6 +59,23 @@ bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
        return positioning_handled_by_some;
 }
 
+bool ScriptApiPlayer::on_prejoinplayer(std::string name, std::string ip, std::string &reason)
+{
+       SCRIPTAPI_PRECHECKHEADER
+
+       // Get minetest.registered_on_prejoinplayers
+       lua_getglobal(L, "minetest");
+       lua_getfield(L, -1, "registered_on_prejoinplayers");
+       lua_pushstring(L, name.c_str());
+       lua_pushstring(L, ip.c_str());
+       script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
+       if (lua_isstring(L, -1)) {
+               reason.assign(lua_tostring(L, -1));
+               return true;
+       }
+       return false;
+}
+
 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
 {
        SCRIPTAPI_PRECHECKHEADER
index 88221f4869431a62635e9424ae6eb212c09a2217..c77d397c408fd28139e98c6bcca4a24472bd7252 100644 (file)
@@ -34,6 +34,7 @@ class ScriptApiPlayer
        void on_newplayer(ServerActiveObject *player);
        void on_dieplayer(ServerActiveObject *player);
        bool on_respawnplayer(ServerActiveObject *player);
+       bool on_prejoinplayer(std::string name, std::string ip, std::string &reason);
        void on_joinplayer(ServerActiveObject *player);
        void on_leaveplayer(ServerActiveObject *player);
        void on_cheat(ServerActiveObject *player, const std::string &cheat_type);
index 13b59e7f5c1b41e686bc8e575ba7ba9015aa96ce..2c38c66d3f71a453a7ce0c6f2640783aba46443a 100644 (file)
@@ -1970,6 +1970,19 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        return;
                }
 
+               {
+                       std::string reason;
+                       if(m_script->on_prejoinplayer(playername, addr_s, reason))
+                       {
+                               actionstream<<"Server: Player with the name \""<<playername<<"\" "
+                                               <<"tried to connect from "<<addr_s<<" "
+                                               <<"but it was disallowed for the following reason: "
+                                               <<reason<<std::endl;
+                               DenyAccess(peer_id, narrow_to_wide(reason.c_str()));
+                               return;
+                       }
+               }
+
                infostream<<"Server: New connection: \""<<playername<<"\" from "
                                <<addr_s<<" (peer_id="<<peer_id<<")"<<std::endl;