3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "servercommand.h"
25 // Process a command sent from a client. The environment and connection
26 // should be locked when this is called.
27 // Returns a response message, to be dealt with according to the flags set
29 std::wstring ServerCommand::processCommand(ServerCommandContext *ctx)
32 std::wostringstream os(std::ios_base::binary);
33 ctx->flags = 1; // Default, unless we change it.
35 u64 privs = ctx->player->privs;
37 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
39 os<<L"-!- Available commands: ";
41 if(privs & PRIV_SERVER)
42 os<<L"shutdown setting ";
43 if(privs & PRIV_SETTIME)
45 if(privs & PRIV_TELEPORT)
47 if(privs & PRIV_PRIVS)
50 else if(ctx->parms[0] == L"status")
54 else if(ctx->parms[0] == L"privs")
58 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
60 cmd_grantrevoke(os, ctx);
62 else if(ctx->parms[0] == L"time")
66 else if(ctx->parms[0] == L"shutdown")
68 cmd_shutdown(os, ctx);
70 else if(ctx->parms[0] == L"setting")
74 else if(ctx->parms[0] == L"teleport")
76 cmd_teleport(os, ctx);
80 os<<L"-!- Invalid command: " + ctx->parms[0];
85 void ServerCommand::cmd_status(std::wostringstream &os,
86 ServerCommandContext *ctx)
88 os<<ctx->server->getStatusString();
91 void ServerCommand::cmd_privs(std::wostringstream &os,
92 ServerCommandContext *ctx)
94 if(ctx->parms.size() == 1)
96 os<<L"-!- " + privsToString(ctx->player->privs);
100 if((ctx->player->privs & PRIV_PRIVS) == 0)
102 os<<L"-!- You don't have permission to do that";
106 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
109 os<<L"-!- No such player";
113 os<<L"-!- " + privsToString(tp->privs);
116 void ServerCommand::cmd_grantrevoke(std::wostringstream &os,
117 ServerCommandContext *ctx)
119 if(ctx->parms.size() != 3)
121 os<<L"-!- Missing parameter";
125 if((ctx->player->privs & PRIV_PRIVS) == 0)
127 os<<L"-!- You don't have permission to do that";
131 u64 newprivs = stringToPrivs(ctx->parms[2]);
132 if(newprivs == PRIV_INVALID)
134 os<<L"-!- Invalid privileges specified";
138 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
141 os<<L"-!- No such player";
145 if(ctx->parms[0] == L"grant")
146 tp->privs |= newprivs;
148 tp->privs &= ~newprivs;
150 os<<L"-!- Privileges change to ";
151 os<<privsToString(tp->privs);
154 void ServerCommand::cmd_time(std::wostringstream &os,
155 ServerCommandContext *ctx)
157 if(ctx->parms.size() != 2)
159 os<<L"-!- Missing parameter";
163 if((ctx->player->privs & PRIV_SETTIME) ==0)
165 os<<L"-!- You don't have permission to do that";
169 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
170 ctx->server->setTimeOfDay(time);
171 os<<L"-!- time_of_day changed.";
174 void ServerCommand::cmd_shutdown(std::wostringstream &os,
175 ServerCommandContext *ctx)
177 if((ctx->player->privs & PRIV_SERVER) ==0)
179 os<<L"-!- You don't have permission to do that";
183 dstream<<DTIME<<" Server: Operator requested shutdown."
185 ctx->server->requestShutdown();
187 os<<L"*** Server shutting down (operator request)";
191 void ServerCommand::cmd_setting(std::wostringstream &os,
192 ServerCommandContext *ctx)
194 if((ctx->player->privs & PRIV_SERVER) ==0)
196 os<<L"-!- You don't have permission to do that";
200 std::string confline = wide_to_narrow(ctx->parms[1] + L" = " + ctx->parms[2]);
201 g_settings.parseConfigLine(confline);
202 os<< L"-!- Setting changed.";
205 void ServerCommand::cmd_teleport(std::wostringstream &os,
206 ServerCommandContext *ctx)
208 if((ctx->player->privs & PRIV_TELEPORT) ==0)
210 os<<L"-!- You don't have permission to do that";
214 if(ctx->parms.size() != 2)
216 os<<L"-!- Missing parameter";
220 std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
221 if(coords.size() != 3)
223 os<<L"-!- You can only specify coordinates currently";
227 v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
228 ctx->player->setPosition(dest);
229 ctx->server->SendMovePlayer(ctx->player);
231 os<< L"-!- Teleported.";