]> git.lizzy.rs Git - minetest.git/blobdiff - src/servercommand.cpp
Clients inform server on wielded item
[minetest.git] / src / servercommand.cpp
index 21483b548f4c68ab2a4d3ed2be7e08c2c0448da0..7d6d7bb73e6dbfdf967bead0e39c59e796cb1ff5 100644 (file)
 /*
-Minetest-c55
-Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+Part of Minetest-c55
+Copyright (C) 2010-11 celeron55, Perttu Ahola <celeron55@gmail.com>
 Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
 
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License along
-with this program; if not, write to the Free Software Foundation, Inc.,
-51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
 
-
 #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<<L"-!- Available commands: ";
-               os<<L"status privs ";
-               if(privs & PRIV_SERVER)
-                       os<<L"shutdown setting ";
-               if(privs & PRIV_SETTIME)
-                       os<<L" time";
-               if(privs & PRIV_TELEPORT)
-                       os<<L" teleport";
-               if(privs & PRIV_PRIVS)
-                       os<<L" grant revoke";
-       }
-       else if(ctx->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<<L"-!- Invalid command: " + ctx->parms[0];
-       }
-       return os.str();
-}
-
-void ServerCommand::cmd_status(std::wostringstream &os,
+void cmd_status(std::wostringstream &os,
        ServerCommandContext *ctx)
 {
        os<<ctx->server->getStatusString();
 }
 
-void ServerCommand::cmd_privs(std::wostringstream &os,
+void cmd_privs(std::wostringstream &os,
        ServerCommandContext *ctx)
 {
        if(ctx->parms.size() == 1)
        {
-               os<<L"-!- " + privsToString(ctx->player->privs);
+               // Show our own real privs, without any adjustments
+               // made for admin status
+               os<<L"-!- " + narrow_to_wide(privsToString(
+                               ctx->server->getPlayerAuthPrivs(ctx->player->getName())));
                return;
        }
 
-       if((ctx->player->privs & PRIV_PRIVS) == 0)
+       if((ctx->privs & PRIV_PRIVS) == 0)
        {
                os<<L"-!- You don't have permission to do that";
                return;
@@ -110,10 +50,10 @@ void ServerCommand::cmd_privs(std::wostringstream &os,
                return;
        }
        
-       os<<L"-!- " + privsToString(tp->privs);
+       os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
 }
 
-void ServerCommand::cmd_grantrevoke(std::wostringstream &os,
+void cmd_grantrevoke(std::wostringstream &os,
        ServerCommandContext *ctx)
 {
        if(ctx->parms.size() != 3)
@@ -122,13 +62,13 @@ void ServerCommand::cmd_grantrevoke(std::wostringstream &os,
                return;
        }
 
-       if((ctx->player->privs & PRIV_PRIVS) == 0)
+       if((ctx->privs & PRIV_PRIVS) == 0)
        {
                os<<L"-!- You don't have permission to do that";
                return;
        }
 
-       u64 newprivs = stringToPrivs(ctx->parms[2]);
+       u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
        if(newprivs == PRIV_INVALID)
        {
                os<<L"-!- Invalid privileges specified";
@@ -141,17 +81,22 @@ void ServerCommand::cmd_grantrevoke(std::wostringstream &os,
                os<<L"-!- No such player";
                return;
        }
+       
+       std::string playername = wide_to_narrow(ctx->parms[1]);
+       u64 privs = ctx->server->getPlayerAuthPrivs(playername);
 
        if(ctx->parms[0] == L"grant")
-               tp->privs |= newprivs;
+               privs |= newprivs;
        else
-               tp->privs &= ~newprivs;
+               privs &= ~newprivs;
+       
+       ctx->server->setPlayerAuthPrivs(playername, privs);
        
        os<<L"-!- Privileges change to ";
-       os<<privsToString(tp->privs);
+       os<<narrow_to_wide(privsToString(privs));
 }
 
-void ServerCommand::cmd_time(std::wostringstream &os,
+void cmd_time(std::wostringstream &os,
        ServerCommandContext *ctx)
 {
        if(ctx->parms.size() != 2)
@@ -160,7 +105,7 @@ void ServerCommand::cmd_time(std::wostringstream &os,
                return;
        }
 
-       if((ctx->player->privs & PRIV_SETTIME) ==0)
+       if((ctx->privs & PRIV_SETTIME) ==0)
        {
                os<<L"-!- You don't have permission to do that";
                return;
@@ -171,10 +116,10 @@ void ServerCommand::cmd_time(std::wostringstream &os,
        os<<L"-!- time_of_day changed.";
 }
 
-void ServerCommand::cmd_shutdown(std::wostringstream &os,
+void cmd_shutdown(std::wostringstream &os,
        ServerCommandContext *ctx)
 {
-       if((ctx->player->privs & PRIV_SERVER) ==0)
+       if((ctx->privs & PRIV_SERVER) ==0)
        {
                os<<L"-!- You don't have permission to do that";
                return;
@@ -188,24 +133,31 @@ 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)
+       if((ctx->privs & PRIV_SERVER) ==0)
        {
                os<<L"-!- You don't have permission to do that";
                return;
        }
 
-       std::string confline = wide_to_narrow(ctx->parms[1] + L" = " + ctx->parms[2]);
+       /*std::string confline = wide_to_narrow(
+                       ctx->parms[1] + L" = " + ctx->params[2]);*/
+
+       std::string confline = wide_to_narrow(ctx->paramstring);
+       
        g_settings.parseConfigLine(confline);
-       os<< L"-!- Setting changed.";
+       
+       ctx->server->saveConfig();
+
+       os<< L"-!- Setting changed and configuration saved.";
 }
 
-void ServerCommand::cmd_teleport(std::wostringstream &os,
+void cmd_teleport(std::wostringstream &os,
        ServerCommandContext *ctx)
 {
-       if((ctx->player->privs & PRIV_TELEPORT) ==0)
+       if((ctx->privs & PRIV_TELEPORT) ==0)
        {
                os<<L"-!- You don't have permission to do that";
                return;
@@ -231,3 +183,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->privs;
+
+       if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
+       {
+               os<<L"-!- Available commands: ";
+               os<<L"status privs ";
+               if(privs & PRIV_SERVER)
+                       os<<L"shutdown setting ";
+               if(privs & PRIV_SETTIME)
+                       os<<L" time";
+               if(privs & PRIV_TELEPORT)
+                       os<<L" teleport";
+               if(privs & PRIV_PRIVS)
+                       os<<L" grant revoke";
+       }
+       else if(ctx->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<<L"-!- Invalid command: " + ctx->parms[0];
+       }
+       return os.str();
+}
+
+