Merge remote-tracking branch 'kahrl/dissector'
[oweals/minetest.git] / src / servercommand.cpp
index 215dc0d275f09ff07efb0071c5620c03812bd8da..89ba0771fe93f8ed91be720ab408b1398c9da34d 100644 (file)
@@ -1,24 +1,21 @@
 /*
-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"
 
@@ -28,16 +25,27 @@ void cmd_status(std::wostringstream &os,
        os<<ctx->server->getStatusString();
 }
 
+void cmd_me(std::wostringstream &os,
+       ServerCommandContext *ctx)
+{
+       std::wstring name = narrow_to_wide(ctx->player->getName());
+       os << L"* " << name << L" " << ctx->paramstring;
+       ctx->flags |= SEND_TO_OTHERS | SEND_NO_PREFIX;
+}
+
 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;
@@ -50,7 +58,7 @@ void cmd_privs(std::wostringstream &os,
                return;
        }
        
-       os<<L"-!- " + privsToString(tp->privs);
+       os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
 }
 
 void cmd_grantrevoke(std::wostringstream &os,
@@ -62,13 +70,13 @@ void 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";
@@ -81,14 +89,19 @@ void 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 cmd_time(std::wostringstream &os,
@@ -100,7 +113,7 @@ void 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;
@@ -114,7 +127,7 @@ void cmd_time(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;
@@ -125,27 +138,34 @@ void cmd_shutdown(std::wostringstream &os,
        ctx->server->requestShutdown();
                                        
        os<<L"*** Server shutting down (operator request)";
-       ctx->flags |= 2;
+       ctx->flags |= SEND_TO_OTHERS;
 }
 
 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 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;
@@ -171,14 +191,58 @@ void cmd_teleport(std::wostringstream &os,
        os<< L"-!- Teleported.";
 }
 
+void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
+{
+       if((ctx->privs & PRIV_BAN) == 0)
+       {
+               os<<L"-!- You don't have permission to do that";
+               return;
+       }
+
+       if(ctx->parms.size() < 2)
+       {
+               std::string desc = ctx->server->getBanDescription("");
+               os<<L"-!- Ban list: "<<narrow_to_wide(desc);
+               return;
+       }
+       if(ctx->parms[0] == L"ban")
+       {
+               Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
+
+               if(player == NULL)
+               {
+                       os<<L"-!- No such player";
+                       return;
+               }
+
+               con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
+               if(peer == NULL)
+               {
+                       dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
+                       return;
+               }
+               std::string ip_string = peer->address.serializeString();
+               ctx->server->setIpBanned(ip_string, player->getName());
+               os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
+                               <<narrow_to_wide(player->getName());
+       }
+       else
+       {
+               std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
+               std::string desc = ctx->server->getBanDescription(ip_or_name);
+               ctx->server->unsetIpBanned(ip_or_name);
+               os<<L"-!- Unbanned "<<narrow_to_wide(desc);
+       }
+}
+
 
 std::wstring processServerCommand(ServerCommandContext *ctx)
 {
 
        std::wostringstream os(std::ios_base::binary);
-       ctx->flags = 1; // Default, unless we change it.
+       ctx->flags = SEND_TO_SENDER;    // Default, unless we change it.
 
-       u64 privs = ctx->player->privs;
+       u64 privs = ctx->privs;
 
        if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
        {
@@ -192,6 +256,8 @@ std::wstring processServerCommand(ServerCommandContext *ctx)
                        os<<L" teleport";
                if(privs & PRIV_PRIVS)
                        os<<L" grant revoke";
+               if(privs & PRIV_BAN)
+                       os<<L" ban unban";
        }
        else if(ctx->parms[0] == L"status")
        {
@@ -221,6 +287,14 @@ std::wstring processServerCommand(ServerCommandContext *ctx)
        {
                cmd_teleport(os, ctx);
        }
+       else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
+       {
+               cmd_banunban(os, ctx);
+       }
+       else if(ctx->parms[0] == L"me")
+       {
+               cmd_me(os, ctx);
+       }
        else
        {
                os<<L"-!- Invalid command: " + ctx->parms[0];