From 248d7c8469f8cb37406ea0ce56d0945e38334cfb Mon Sep 17 00:00:00 2001 From: Ciaran Gultnieks Date: Mon, 16 May 2011 10:41:19 +0100 Subject: Improved server commands and added player permissions. --HG-- extra : rebase_source : 178fe08f10b7de3ebaba088bd24faad795114216 --- src/server.cpp | 91 +++++++++++++++++----------------------------------------- 1 file changed, 26 insertions(+), 65 deletions(-) (limited to 'src/server.cpp') diff --git a/src/server.cpp b/src/server.cpp index b5a38aa06..f77b4f3c6 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "materials.h" #include "mineral.h" #include "config.h" +#include "servercommand.h" #define BLOCK_EMERGE_FLAG_FROMDISK (1<<0) @@ -1994,6 +1995,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) if(datasize < 13) return; + if((player->privs & PRIV_BUILD) == 0) + return; + /* [0] u16 command [2] u8 button (0=left, 1=right) @@ -2075,6 +2079,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) if(datasize < 7) return; + if((player->privs & PRIV_BUILD) == 0) + return; + /* length: 7 [0] u16 command @@ -2167,6 +2174,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) { if(datasize < 17) return; + if((player->privs & PRIV_BUILD) == 0) + return; /* length: 17 [0] u16 command @@ -2615,6 +2624,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) #endif else if(command == TOSERVER_SIGNTEXT) { + if((player->privs & PRIV_BUILD) == 0) + return; /* u16 command v3s16 blockpos @@ -2672,6 +2683,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) } else if(command == TOSERVER_SIGNNODETEXT) { + if((player->privs & PRIV_BUILD) == 0) + return; /* u16 command v3s16 p @@ -2853,71 +2866,19 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) line += L"Server: "; message = message.substr(commandprefix.size()); - // Get player name as narrow string - std::string name_s = player->getName(); - // Convert message to narrow string - std::string message_s = wide_to_narrow(message); - // Operator is the single name defined in config. - std::string operator_name = g_settings.get("name"); - bool is_operator = (operator_name != "" && - wide_to_narrow(name) == operator_name); - bool valid_command = false; - if(message_s == "help") - { - line += L"-!- Available commands: "; - line += L"status "; - if(is_operator) - { - line += L"shutdown setting time "; - } - else - { - } - send_to_sender = true; - valid_command = true; - } - else if(message_s == "status") - { - line = getStatusString(); - send_to_sender = true; - valid_command = true; - } - else if(is_operator) - { - if(message_s == "shutdown") - { - dstream<flags & 1; + send_to_others = ctx->flags & 2; + delete ctx; + } else { -- cgit v1.2.3 From 7cdd988f88ae3fc5b1ca342c3c5e176eec0ba8f9 Mon Sep 17 00:00:00 2001 From: Ciaran Gultnieks Date: Mon, 16 May 2011 11:34:06 +0100 Subject: Better synchronisation of build/mine attempts when the player isn't allowed to --- src/server.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/server.cpp') diff --git a/src/server.cpp b/src/server.cpp index f77b4f3c6..338b528e7 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2174,8 +2174,6 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) { if(datasize < 17) return; - if((player->privs & PRIV_BUILD) == 0) - return; /* length: 17 [0] u16 command @@ -2281,6 +2279,10 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) cannot_remove_node = true; } + // Make sure the player is allowed to do it + if((player->privs & PRIV_BUILD) == 0) + cannot_remove_node = true; + /* If node can't be removed, set block to be re-sent to client and quit. @@ -2427,7 +2429,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) try{ // Don't add a node if this is not a free space MapNode n2 = m_env.getMap().getNode(p_over); - if(content_buildable_to(n2.d) == false) + if(content_buildable_to(n2.d) == false + || (player->privs & PRIV_BUILD) ==0) { // Client probably has wrong data. // Set block not sent, so that client will get -- cgit v1.2.3 From b3268ff3896097abdd9199e4bb8ee826afda8388 Mon Sep 17 00:00:00 2001 From: Ciaran Gultnieks Date: Mon, 16 May 2011 17:13:33 +0100 Subject: Server commands without classes --- src/server.cpp | 2 +- src/servercommand.cpp | 132 +++++++++++++++++++++++++------------------------- src/servercommand.h | 28 ++--------- 3 files changed, 71 insertions(+), 91 deletions(-) (limited to 'src/server.cpp') diff --git a/src/server.cpp b/src/server.cpp index 338b528e7..d3ca32ac7 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2877,7 +2877,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) player ); - line += ServerCommand::processCommand(ctx); + line += processServerCommand(ctx); send_to_sender = ctx->flags & 1; send_to_others = ctx->flags & 2; delete ctx; diff --git a/src/servercommand.cpp b/src/servercommand.cpp index 21483b548..215dc0d27 100644 --- a/src/servercommand.cpp +++ b/src/servercommand.cpp @@ -22,73 +22,13 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "servercommand.h" #include "utility.h" -// Process a command sent from a client. The environment and connection -// should be locked when this is called. -// Returns a response message, to be dealt with according to the flags set -// in the context. -std::wstring ServerCommand::processCommand(ServerCommandContext *ctx) -{ - - std::wostringstream os(std::ios_base::binary); - ctx->flags = 1; // Default, unless we change it. - - u64 privs = ctx->player->privs; - - if(ctx->parms.size() == 0 || ctx->parms[0] == L"help") - { - os<parms[0] == L"status") - { - cmd_status(os, ctx); - } - else if(ctx->parms[0] == L"privs") - { - cmd_privs(os, ctx); - } - else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke") - { - cmd_grantrevoke(os, ctx); - } - else if(ctx->parms[0] == L"time") - { - cmd_time(os, ctx); - } - else if(ctx->parms[0] == L"shutdown") - { - cmd_shutdown(os, ctx); - } - else if(ctx->parms[0] == L"setting") - { - cmd_setting(os, ctx); - } - else if(ctx->parms[0] == L"teleport") - { - cmd_teleport(os, ctx); - } - else - { - os<parms[0]; - } - return os.str(); -} - -void ServerCommand::cmd_status(std::wostringstream &os, +void cmd_status(std::wostringstream &os, ServerCommandContext *ctx) { os<server->getStatusString(); } -void ServerCommand::cmd_privs(std::wostringstream &os, +void cmd_privs(std::wostringstream &os, ServerCommandContext *ctx) { if(ctx->parms.size() == 1) @@ -113,7 +53,7 @@ void ServerCommand::cmd_privs(std::wostringstream &os, os<privs); } -void ServerCommand::cmd_grantrevoke(std::wostringstream &os, +void cmd_grantrevoke(std::wostringstream &os, ServerCommandContext *ctx) { if(ctx->parms.size() != 3) @@ -151,7 +91,7 @@ void ServerCommand::cmd_grantrevoke(std::wostringstream &os, os<privs); } -void ServerCommand::cmd_time(std::wostringstream &os, +void cmd_time(std::wostringstream &os, ServerCommandContext *ctx) { if(ctx->parms.size() != 2) @@ -171,7 +111,7 @@ void ServerCommand::cmd_time(std::wostringstream &os, os<player->privs & PRIV_SERVER) ==0) @@ -188,7 +128,7 @@ void ServerCommand::cmd_shutdown(std::wostringstream &os, ctx->flags |= 2; } -void ServerCommand::cmd_setting(std::wostringstream &os, +void cmd_setting(std::wostringstream &os, ServerCommandContext *ctx) { if((ctx->player->privs & PRIV_SERVER) ==0) @@ -202,7 +142,7 @@ void ServerCommand::cmd_setting(std::wostringstream &os, os<< L"-!- Setting changed."; } -void ServerCommand::cmd_teleport(std::wostringstream &os, +void cmd_teleport(std::wostringstream &os, ServerCommandContext *ctx) { if((ctx->player->privs & PRIV_TELEPORT) ==0) @@ -231,3 +171,61 @@ void ServerCommand::cmd_teleport(std::wostringstream &os, os<< L"-!- Teleported."; } + +std::wstring processServerCommand(ServerCommandContext *ctx) +{ + + std::wostringstream os(std::ios_base::binary); + ctx->flags = 1; // Default, unless we change it. + + u64 privs = ctx->player->privs; + + if(ctx->parms.size() == 0 || ctx->parms[0] == L"help") + { + os<parms[0] == L"status") + { + cmd_status(os, ctx); + } + else if(ctx->parms[0] == L"privs") + { + cmd_privs(os, ctx); + } + else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke") + { + cmd_grantrevoke(os, ctx); + } + else if(ctx->parms[0] == L"time") + { + cmd_time(os, ctx); + } + else if(ctx->parms[0] == L"shutdown") + { + cmd_shutdown(os, ctx); + } + else if(ctx->parms[0] == L"setting") + { + cmd_setting(os, ctx); + } + else if(ctx->parms[0] == L"teleport") + { + cmd_teleport(os, ctx); + } + else + { + os<parms[0]; + } + return os.str(); +} + + diff --git a/src/servercommand.h b/src/servercommand.h index 01efcae06..bc7823c66 100644 --- a/src/servercommand.h +++ b/src/servercommand.h @@ -47,29 +47,11 @@ struct ServerCommandContext }; -class ServerCommand -{ -public: - - static std::wstring processCommand(ServerCommandContext *ctx); - -private: - - static void cmd_status(std::wostringstream &os, - ServerCommandContext *ctx); - static void cmd_privs(std::wostringstream &os, - ServerCommandContext *ctx); - static void cmd_grantrevoke(std::wostringstream &os, - ServerCommandContext *ctx); - static void cmd_time(std::wostringstream &os, - ServerCommandContext *ctx); - static void cmd_shutdown(std::wostringstream &os, - ServerCommandContext *ctx); - static void cmd_setting(std::wostringstream &os, - ServerCommandContext *ctx); - static void cmd_teleport(std::wostringstream &os, - ServerCommandContext *ctx); -}; +// Process a command sent from a client. The environment and connection +// should be locked when this is called. +// Returns a response message, to be dealt with according to the flags set +// in the context. +std::wstring processServerCommand(ServerCommandContext *ctx); #endif -- cgit v1.2.3