2011-05-16 11:41:19 +02:00
|
|
|
/*
|
2011-05-31 10:59:51 +02:00
|
|
|
Part of Minetest-c55
|
|
|
|
Copyright (C) 2010-11 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2011-05-16 11:41:19 +02:00
|
|
|
Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
|
|
|
|
|
2011-05-31 10:59:51 +02:00
|
|
|
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.
|
2011-05-16 11:41:19 +02:00
|
|
|
|
2011-05-31 10:59:51 +02:00
|
|
|
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.
|
2011-05-16 11:41:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "servercommand.h"
|
|
|
|
#include "utility.h"
|
2011-10-12 12:53:38 +02:00
|
|
|
#include "settings.h"
|
2011-05-16 11:41:19 +02:00
|
|
|
|
2011-10-16 13:57:53 +02:00
|
|
|
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
|
|
|
|
|
2011-05-16 18:13:33 +02:00
|
|
|
void cmd_status(std::wostringstream &os,
|
2011-05-16 11:41:19 +02:00
|
|
|
ServerCommandContext *ctx)
|
|
|
|
{
|
|
|
|
os<<ctx->server->getStatusString();
|
|
|
|
}
|
|
|
|
|
2011-08-13 17:41:18 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-16 18:13:33 +02:00
|
|
|
void cmd_privs(std::wostringstream &os,
|
2011-05-16 11:41:19 +02:00
|
|
|
ServerCommandContext *ctx)
|
|
|
|
{
|
|
|
|
if(ctx->parms.size() == 1)
|
|
|
|
{
|
2011-05-16 19:26:37 +02:00
|
|
|
// Show our own real privs, without any adjustments
|
|
|
|
// made for admin status
|
2011-05-29 21:34:04 +02:00
|
|
|
os<<L"-!- " + narrow_to_wide(privsToString(
|
|
|
|
ctx->server->getPlayerAuthPrivs(ctx->player->getName())));
|
2011-05-16 11:41:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-16 19:26:37 +02:00
|
|
|
if((ctx->privs & PRIV_PRIVS) == 0)
|
2011-05-16 11:41:19 +02:00
|
|
|
{
|
|
|
|
os<<L"-!- You don't have permission to do that";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
|
|
|
|
if(tp == NULL)
|
|
|
|
{
|
|
|
|
os<<L"-!- No such player";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-29 21:34:04 +02:00
|
|
|
os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
|
2011-05-16 11:41:19 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 18:13:33 +02:00
|
|
|
void cmd_grantrevoke(std::wostringstream &os,
|
2011-05-16 11:41:19 +02:00
|
|
|
ServerCommandContext *ctx)
|
|
|
|
{
|
|
|
|
if(ctx->parms.size() != 3)
|
|
|
|
{
|
|
|
|
os<<L"-!- Missing parameter";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-16 19:26:37 +02:00
|
|
|
if((ctx->privs & PRIV_PRIVS) == 0)
|
2011-05-16 11:41:19 +02:00
|
|
|
{
|
|
|
|
os<<L"-!- You don't have permission to do that";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-29 20:11:16 +02:00
|
|
|
u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
|
2011-05-16 11:41:19 +02:00
|
|
|
if(newprivs == PRIV_INVALID)
|
|
|
|
{
|
|
|
|
os<<L"-!- Invalid privileges specified";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
|
|
|
|
if(tp == NULL)
|
|
|
|
{
|
|
|
|
os<<L"-!- No such player";
|
|
|
|
return;
|
|
|
|
}
|
2011-05-29 21:34:04 +02:00
|
|
|
|
|
|
|
std::string playername = wide_to_narrow(ctx->parms[1]);
|
|
|
|
u64 privs = ctx->server->getPlayerAuthPrivs(playername);
|
2011-05-16 11:41:19 +02:00
|
|
|
|
2011-10-16 13:57:53 +02:00
|
|
|
if(ctx->parms[0] == L"grant"){
|
2011-10-16 19:03:43 +02:00
|
|
|
privs |= newprivs;
|
2011-10-16 13:57:53 +02:00
|
|
|
actionstream<<ctx->player->getName()<<" grants "
|
|
|
|
<<wide_to_narrow(ctx->parms[2])<<" to "
|
|
|
|
<<playername<<std::endl;
|
2011-10-16 19:03:43 +02:00
|
|
|
|
|
|
|
std::wstring msg;
|
|
|
|
msg += narrow_to_wide(ctx->player->getName());
|
|
|
|
msg += L" granted you the privilege \"";
|
|
|
|
msg += ctx->parms[2];
|
|
|
|
msg += L"\"";
|
|
|
|
ctx->server->notifyPlayer(playername.c_str(), msg);
|
2011-10-16 13:57:53 +02:00
|
|
|
} else {
|
2011-10-16 19:03:43 +02:00
|
|
|
privs &= ~newprivs;
|
2011-10-16 13:57:53 +02:00
|
|
|
actionstream<<ctx->player->getName()<<" revokes "
|
|
|
|
<<wide_to_narrow(ctx->parms[2])<<" from "
|
|
|
|
<<playername<<std::endl;
|
2011-10-16 19:03:43 +02:00
|
|
|
|
|
|
|
std::wstring msg;
|
|
|
|
msg += narrow_to_wide(ctx->player->getName());
|
|
|
|
msg += L" revoked from you the privilege \"";
|
|
|
|
msg += ctx->parms[2];
|
|
|
|
msg += L"\"";
|
|
|
|
ctx->server->notifyPlayer(playername.c_str(), msg);
|
2011-10-16 13:57:53 +02:00
|
|
|
}
|
2011-05-29 21:34:04 +02:00
|
|
|
|
|
|
|
ctx->server->setPlayerAuthPrivs(playername, privs);
|
2011-05-16 11:41:19 +02:00
|
|
|
|
|
|
|
os<<L"-!- Privileges change to ";
|
2011-05-29 21:34:04 +02:00
|
|
|
os<<narrow_to_wide(privsToString(privs));
|
2011-05-16 11:41:19 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 18:13:33 +02:00
|
|
|
void cmd_time(std::wostringstream &os,
|
2011-05-16 11:41:19 +02:00
|
|
|
ServerCommandContext *ctx)
|
|
|
|
{
|
|
|
|
if(ctx->parms.size() != 2)
|
|
|
|
{
|
|
|
|
os<<L"-!- Missing parameter";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-16 19:26:37 +02:00
|
|
|
if((ctx->privs & PRIV_SETTIME) ==0)
|
2011-05-16 11:41:19 +02:00
|
|
|
{
|
|
|
|
os<<L"-!- You don't have permission to do that";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 time = stoi(wide_to_narrow(ctx->parms[1]));
|
|
|
|
ctx->server->setTimeOfDay(time);
|
|
|
|
os<<L"-!- time_of_day changed.";
|
2011-10-16 13:57:53 +02:00
|
|
|
|
|
|
|
actionstream<<ctx->player->getName()<<" sets time "
|
|
|
|
<<time<<std::endl;
|
2011-05-16 11:41:19 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 18:13:33 +02:00
|
|
|
void cmd_shutdown(std::wostringstream &os,
|
2011-05-16 11:41:19 +02:00
|
|
|
ServerCommandContext *ctx)
|
|
|
|
{
|
2011-05-16 19:26:37 +02:00
|
|
|
if((ctx->privs & PRIV_SERVER) ==0)
|
2011-05-16 11:41:19 +02:00
|
|
|
{
|
|
|
|
os<<L"-!- You don't have permission to do that";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-16 13:57:53 +02:00
|
|
|
actionstream<<ctx->player->getName()
|
|
|
|
<<" shuts down server"<<std::endl;
|
|
|
|
|
2011-05-16 11:41:19 +02:00
|
|
|
ctx->server->requestShutdown();
|
|
|
|
|
|
|
|
os<<L"*** Server shutting down (operator request)";
|
2011-08-13 17:35:10 +02:00
|
|
|
ctx->flags |= SEND_TO_OTHERS;
|
2011-05-16 11:41:19 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 18:13:33 +02:00
|
|
|
void cmd_setting(std::wostringstream &os,
|
2011-05-16 11:41:19 +02:00
|
|
|
ServerCommandContext *ctx)
|
|
|
|
{
|
2011-05-16 19:26:37 +02:00
|
|
|
if((ctx->privs & PRIV_SERVER) ==0)
|
2011-05-16 11:41:19 +02:00
|
|
|
{
|
|
|
|
os<<L"-!- You don't have permission to do that";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-30 18:49:42 +02:00
|
|
|
/*std::string confline = wide_to_narrow(
|
|
|
|
ctx->parms[1] + L" = " + ctx->params[2]);*/
|
|
|
|
|
|
|
|
std::string confline = wide_to_narrow(ctx->paramstring);
|
|
|
|
|
2011-10-16 13:57:53 +02:00
|
|
|
actionstream<<ctx->player->getName()
|
|
|
|
<<" sets: "<<confline<<std::endl;
|
|
|
|
|
2011-10-12 12:53:38 +02:00
|
|
|
g_settings->parseConfigLine(confline);
|
2011-07-30 18:49:42 +02:00
|
|
|
|
|
|
|
ctx->server->saveConfig();
|
|
|
|
|
|
|
|
os<< L"-!- Setting changed and configuration saved.";
|
2011-05-16 11:41:19 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 18:13:33 +02:00
|
|
|
void cmd_teleport(std::wostringstream &os,
|
2011-05-16 11:41:19 +02:00
|
|
|
ServerCommandContext *ctx)
|
|
|
|
{
|
2011-05-16 19:26:37 +02:00
|
|
|
if((ctx->privs & PRIV_TELEPORT) ==0)
|
2011-05-16 11:41:19 +02:00
|
|
|
{
|
|
|
|
os<<L"-!- You don't have permission to do that";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ctx->parms.size() != 2)
|
|
|
|
{
|
|
|
|
os<<L"-!- Missing parameter";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
|
|
|
|
if(coords.size() != 3)
|
|
|
|
{
|
|
|
|
os<<L"-!- You can only specify coordinates currently";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
|
2011-10-16 13:57:53 +02:00
|
|
|
|
|
|
|
actionstream<<ctx->player->getName()<<" teleports from "
|
|
|
|
<<PP(ctx->player->getPosition()/BS)<<" to "
|
|
|
|
<<PP(dest/BS)<<std::endl;
|
|
|
|
|
2011-05-16 11:41:19 +02:00
|
|
|
ctx->player->setPosition(dest);
|
|
|
|
ctx->server->SendMovePlayer(ctx->player);
|
|
|
|
|
|
|
|
os<< L"-!- Teleported.";
|
|
|
|
}
|
|
|
|
|
2011-08-12 12:11:27 +02:00
|
|
|
void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
|
2011-08-12 01:13:42 +02:00
|
|
|
{
|
2011-08-12 23:34:12 +02:00
|
|
|
if((ctx->privs & PRIV_BAN) == 0)
|
2011-08-12 01:13:42 +02:00
|
|
|
{
|
|
|
|
os<<L"-!- You don't have permission to do that";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-12 12:11:27 +02:00
|
|
|
if(ctx->parms.size() < 2)
|
2011-08-12 01:13:42 +02:00
|
|
|
{
|
2011-08-12 12:11:27 +02:00
|
|
|
std::string desc = ctx->server->getBanDescription("");
|
|
|
|
os<<L"-!- Ban list: "<<narrow_to_wide(desc);
|
2011-08-12 01:13:42 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-08-12 12:11:27 +02:00
|
|
|
if(ctx->parms[0] == L"ban")
|
2011-08-12 01:13:42 +02:00
|
|
|
{
|
|
|
|
Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
|
|
|
|
|
|
|
|
if(player == NULL)
|
|
|
|
{
|
2011-08-12 12:11:27 +02:00
|
|
|
os<<L"-!- No such player";
|
2011-08-12 01:13:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
|
|
|
|
if(peer == NULL)
|
|
|
|
{
|
2011-08-12 12:11:27 +02:00
|
|
|
dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
|
|
|
|
return;
|
2011-08-12 01:13:42 +02:00
|
|
|
}
|
2011-08-12 12:11:27 +02:00
|
|
|
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());
|
2011-10-16 13:57:53 +02:00
|
|
|
|
|
|
|
actionstream<<ctx->player->getName()<<" bans "
|
|
|
|
<<player->getName()<<" / "<<ip_string<<std::endl;
|
2011-08-12 01:13:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-12 12:11:27 +02:00
|
|
|
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);
|
2011-10-16 13:57:53 +02:00
|
|
|
|
|
|
|
actionstream<<ctx->player->getName()<<" unbans "
|
|
|
|
<<ip_or_name<<std::endl;
|
2011-08-12 01:13:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-16 18:13:33 +02:00
|
|
|
|
|
|
|
std::wstring processServerCommand(ServerCommandContext *ctx)
|
|
|
|
{
|
|
|
|
|
|
|
|
std::wostringstream os(std::ios_base::binary);
|
2011-08-13 17:35:10 +02:00
|
|
|
ctx->flags = SEND_TO_SENDER; // Default, unless we change it.
|
2011-05-16 18:13:33 +02:00
|
|
|
|
2011-05-16 19:26:37 +02:00
|
|
|
u64 privs = ctx->privs;
|
2011-05-16 18:13:33 +02:00
|
|
|
|
|
|
|
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";
|
2011-08-12 01:13:42 +02:00
|
|
|
if(privs & PRIV_BAN)
|
2011-08-12 12:11:27 +02:00
|
|
|
os<<L" ban unban";
|
2011-05-16 18:13:33 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2011-08-12 12:11:27 +02:00
|
|
|
else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
|
2011-08-12 01:13:42 +02:00
|
|
|
{
|
2011-08-12 12:11:27 +02:00
|
|
|
cmd_banunban(os, ctx);
|
2011-08-12 01:13:42 +02:00
|
|
|
}
|
2011-08-13 17:41:18 +02:00
|
|
|
else if(ctx->parms[0] == L"me")
|
|
|
|
{
|
|
|
|
cmd_me(os, ctx);
|
|
|
|
}
|
2011-05-16 18:13:33 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
os<<L"-!- Invalid command: " + ctx->parms[0];
|
|
|
|
}
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|