forked from Mirrorlandia_minetest/minetest
Server commands without classes
This commit is contained in:
parent
1520d49310
commit
b3268ff389
@ -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;
|
||||
|
@ -22,11 +22,157 @@ 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)
|
||||
void cmd_status(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
os<<ctx->server->getStatusString();
|
||||
}
|
||||
|
||||
void cmd_privs(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if(ctx->parms.size() == 1)
|
||||
{
|
||||
os<<L"-!- " + privsToString(ctx->player->privs);
|
||||
return;
|
||||
}
|
||||
|
||||
if((ctx->player->privs & PRIV_PRIVS) == 0)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
os<<L"-!- " + privsToString(tp->privs);
|
||||
}
|
||||
|
||||
void cmd_grantrevoke(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if(ctx->parms.size() != 3)
|
||||
{
|
||||
os<<L"-!- Missing parameter";
|
||||
return;
|
||||
}
|
||||
|
||||
if((ctx->player->privs & PRIV_PRIVS) == 0)
|
||||
{
|
||||
os<<L"-!- You don't have permission to do that";
|
||||
return;
|
||||
}
|
||||
|
||||
u64 newprivs = stringToPrivs(ctx->parms[2]);
|
||||
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;
|
||||
}
|
||||
|
||||
if(ctx->parms[0] == L"grant")
|
||||
tp->privs |= newprivs;
|
||||
else
|
||||
tp->privs &= ~newprivs;
|
||||
|
||||
os<<L"-!- Privileges change to ";
|
||||
os<<privsToString(tp->privs);
|
||||
}
|
||||
|
||||
void cmd_time(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if(ctx->parms.size() != 2)
|
||||
{
|
||||
os<<L"-!- Missing parameter";
|
||||
return;
|
||||
}
|
||||
|
||||
if((ctx->player->privs & PRIV_SETTIME) ==0)
|
||||
{
|
||||
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.";
|
||||
}
|
||||
|
||||
void cmd_shutdown(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if((ctx->player->privs & PRIV_SERVER) ==0)
|
||||
{
|
||||
os<<L"-!- You don't have permission to do that";
|
||||
return;
|
||||
}
|
||||
|
||||
dstream<<DTIME<<" Server: Operator requested shutdown."
|
||||
<<std::endl;
|
||||
ctx->server->requestShutdown();
|
||||
|
||||
os<<L"*** Server shutting down (operator request)";
|
||||
ctx->flags |= 2;
|
||||
}
|
||||
|
||||
void cmd_setting(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if((ctx->player->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]);
|
||||
g_settings.parseConfigLine(confline);
|
||||
os<< L"-!- Setting changed.";
|
||||
}
|
||||
|
||||
void cmd_teleport(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if((ctx->player->privs & PRIV_TELEPORT) ==0)
|
||||
{
|
||||
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);
|
||||
ctx->player->setPosition(dest);
|
||||
ctx->server->SendMovePlayer(ctx->player);
|
||||
|
||||
os<< L"-!- Teleported.";
|
||||
}
|
||||
|
||||
|
||||
std::wstring processServerCommand(ServerCommandContext *ctx)
|
||||
{
|
||||
|
||||
std::wostringstream os(std::ios_base::binary);
|
||||
@ -82,152 +228,4 @@ std::wstring ServerCommand::processCommand(ServerCommandContext *ctx)
|
||||
return os.str();
|
||||
}
|
||||
|
||||
void ServerCommand::cmd_status(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
os<<ctx->server->getStatusString();
|
||||
}
|
||||
|
||||
void ServerCommand::cmd_privs(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if(ctx->parms.size() == 1)
|
||||
{
|
||||
os<<L"-!- " + privsToString(ctx->player->privs);
|
||||
return;
|
||||
}
|
||||
|
||||
if((ctx->player->privs & PRIV_PRIVS) == 0)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
os<<L"-!- " + privsToString(tp->privs);
|
||||
}
|
||||
|
||||
void ServerCommand::cmd_grantrevoke(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if(ctx->parms.size() != 3)
|
||||
{
|
||||
os<<L"-!- Missing parameter";
|
||||
return;
|
||||
}
|
||||
|
||||
if((ctx->player->privs & PRIV_PRIVS) == 0)
|
||||
{
|
||||
os<<L"-!- You don't have permission to do that";
|
||||
return;
|
||||
}
|
||||
|
||||
u64 newprivs = stringToPrivs(ctx->parms[2]);
|
||||
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;
|
||||
}
|
||||
|
||||
if(ctx->parms[0] == L"grant")
|
||||
tp->privs |= newprivs;
|
||||
else
|
||||
tp->privs &= ~newprivs;
|
||||
|
||||
os<<L"-!- Privileges change to ";
|
||||
os<<privsToString(tp->privs);
|
||||
}
|
||||
|
||||
void ServerCommand::cmd_time(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if(ctx->parms.size() != 2)
|
||||
{
|
||||
os<<L"-!- Missing parameter";
|
||||
return;
|
||||
}
|
||||
|
||||
if((ctx->player->privs & PRIV_SETTIME) ==0)
|
||||
{
|
||||
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.";
|
||||
}
|
||||
|
||||
void ServerCommand::cmd_shutdown(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if((ctx->player->privs & PRIV_SERVER) ==0)
|
||||
{
|
||||
os<<L"-!- You don't have permission to do that";
|
||||
return;
|
||||
}
|
||||
|
||||
dstream<<DTIME<<" Server: Operator requested shutdown."
|
||||
<<std::endl;
|
||||
ctx->server->requestShutdown();
|
||||
|
||||
os<<L"*** Server shutting down (operator request)";
|
||||
ctx->flags |= 2;
|
||||
}
|
||||
|
||||
void ServerCommand::cmd_setting(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if((ctx->player->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]);
|
||||
g_settings.parseConfigLine(confline);
|
||||
os<< L"-!- Setting changed.";
|
||||
}
|
||||
|
||||
void ServerCommand::cmd_teleport(std::wostringstream &os,
|
||||
ServerCommandContext *ctx)
|
||||
{
|
||||
if((ctx->player->privs & PRIV_TELEPORT) ==0)
|
||||
{
|
||||
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);
|
||||
ctx->player->setPosition(dest);
|
||||
ctx->server->SendMovePlayer(ctx->player);
|
||||
|
||||
os<< L"-!- Teleported.";
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user