3 Copyright (C) 2010-11 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "servercommand.h"
23 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
25 void cmd_status(std::wostringstream &os,
26 ServerCommandContext *ctx)
28 os<<ctx->server->getStatusString();
31 void cmd_me(std::wostringstream &os,
32 ServerCommandContext *ctx)
34 std::wstring name = narrow_to_wide(ctx->player->getName());
35 os << L"* " << name << L" " << ctx->paramstring;
36 ctx->flags |= SEND_TO_OTHERS | SEND_NO_PREFIX;
39 void cmd_privs(std::wostringstream &os,
40 ServerCommandContext *ctx)
42 if(ctx->parms.size() == 1)
44 // Show our own real privs, without any adjustments
45 // made for admin status
46 os<<L"-!- " + narrow_to_wide(privsToString(
47 ctx->server->getPlayerAuthPrivs(ctx->player->getName())));
51 if((ctx->privs & PRIV_PRIVS) == 0)
53 os<<L"-!- You don't have permission to do that";
57 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
60 os<<L"-!- No such player";
64 os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
67 void cmd_grantrevoke(std::wostringstream &os,
68 ServerCommandContext *ctx)
70 if(ctx->parms.size() != 3)
72 os<<L"-!- Missing parameter";
76 if((ctx->privs & PRIV_PRIVS) == 0)
78 os<<L"-!- You don't have permission to do that";
82 u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
83 if(newprivs == PRIV_INVALID)
85 os<<L"-!- Invalid privileges specified";
89 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
92 os<<L"-!- No such player";
96 std::string playername = wide_to_narrow(ctx->parms[1]);
97 u64 privs = ctx->server->getPlayerAuthPrivs(playername);
99 if(ctx->parms[0] == L"grant"){
101 actionstream<<ctx->player->getName()<<" grants "
102 <<wide_to_narrow(ctx->parms[2])<<" to "
103 <<playername<<std::endl;
106 msg += narrow_to_wide(ctx->player->getName());
107 msg += L" granted you the privilege \"";
108 msg += ctx->parms[2];
110 ctx->server->notifyPlayer(playername.c_str(), msg);
113 actionstream<<ctx->player->getName()<<" revokes "
114 <<wide_to_narrow(ctx->parms[2])<<" from "
115 <<playername<<std::endl;
118 msg += narrow_to_wide(ctx->player->getName());
119 msg += L" revoked from you the privilege \"";
120 msg += ctx->parms[2];
122 ctx->server->notifyPlayer(playername.c_str(), msg);
125 ctx->server->setPlayerAuthPrivs(playername, privs);
127 os<<L"-!- Privileges change to ";
128 os<<narrow_to_wide(privsToString(privs));
131 void cmd_time(std::wostringstream &os,
132 ServerCommandContext *ctx)
134 if(ctx->parms.size() != 2)
136 os<<L"-!- Missing parameter";
140 if((ctx->privs & PRIV_SETTIME) ==0)
142 os<<L"-!- You don't have permission to do that";
146 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
147 ctx->server->setTimeOfDay(time);
148 os<<L"-!- time_of_day changed.";
150 actionstream<<ctx->player->getName()<<" sets time "
154 void cmd_shutdown(std::wostringstream &os,
155 ServerCommandContext *ctx)
157 if((ctx->privs & PRIV_SERVER) ==0)
159 os<<L"-!- You don't have permission to do that";
163 actionstream<<ctx->player->getName()
164 <<" shuts down server"<<std::endl;
166 ctx->server->requestShutdown();
168 os<<L"*** Server shutting down (operator request)";
169 ctx->flags |= SEND_TO_OTHERS;
172 void cmd_setting(std::wostringstream &os,
173 ServerCommandContext *ctx)
175 if((ctx->privs & PRIV_SERVER) ==0)
177 os<<L"-!- You don't have permission to do that";
181 /*std::string confline = wide_to_narrow(
182 ctx->parms[1] + L" = " + ctx->params[2]);*/
184 std::string confline = wide_to_narrow(ctx->paramstring);
186 actionstream<<ctx->player->getName()
187 <<" sets: "<<confline<<std::endl;
189 g_settings->parseConfigLine(confline);
191 ctx->server->saveConfig();
193 os<< L"-!- Setting changed and configuration saved.";
196 void cmd_teleport(std::wostringstream &os,
197 ServerCommandContext *ctx)
199 if((ctx->privs & PRIV_TELEPORT) ==0)
201 os<<L"-!- You don't have permission to do that";
205 if(ctx->parms.size() != 2)
207 os<<L"-!- Missing parameter";
211 std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
212 if(coords.size() != 3)
214 os<<L"-!- You can only specify coordinates currently";
218 v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
220 actionstream<<ctx->player->getName()<<" teleports from "
221 <<PP(ctx->player->getPosition()/BS)<<" to "
222 <<PP(dest/BS)<<std::endl;
224 ctx->player->setPosition(dest);
225 ctx->server->SendMovePlayer(ctx->player);
227 os<< L"-!- Teleported.";
230 void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
232 if((ctx->privs & PRIV_BAN) == 0)
234 os<<L"-!- You don't have permission to do that";
238 if(ctx->parms.size() < 2)
240 std::string desc = ctx->server->getBanDescription("");
241 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
244 if(ctx->parms[0] == L"ban")
246 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
250 os<<L"-!- No such player";
254 con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
257 dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
260 std::string ip_string = peer->address.serializeString();
261 ctx->server->setIpBanned(ip_string, player->getName());
262 os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
263 <<narrow_to_wide(player->getName());
265 actionstream<<ctx->player->getName()<<" bans "
266 <<player->getName()<<" / "<<ip_string<<std::endl;
270 std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
271 std::string desc = ctx->server->getBanDescription(ip_or_name);
272 ctx->server->unsetIpBanned(ip_or_name);
273 os<<L"-!- Unbanned "<<narrow_to_wide(desc);
275 actionstream<<ctx->player->getName()<<" unbans "
276 <<ip_or_name<<std::endl;
280 void cmd_clearobjects(std::wostringstream &os,
281 ServerCommandContext *ctx)
283 if((ctx->privs & PRIV_SERVER) ==0)
285 os<<L"-!- You don't have permission to do that";
289 actionstream<<ctx->player->getName()
290 <<" clears all objects"<<std::endl;
294 msg += L"Clearing all objects. This may take long.";
295 msg += L" You may experience a timeout. (by ";
296 msg += narrow_to_wide(ctx->player->getName());
298 ctx->server->notifyPlayers(msg);
301 ctx->env->clearAllObjects();
303 actionstream<<"object clearing done"<<std::endl;
305 os<<L"*** cleared all objects";
306 ctx->flags |= SEND_TO_OTHERS;
310 std::wstring processServerCommand(ServerCommandContext *ctx)
313 std::wostringstream os(std::ios_base::binary);
314 ctx->flags = SEND_TO_SENDER; // Default, unless we change it.
316 u64 privs = ctx->privs;
318 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
320 os<<L"-!- Available commands: ";
321 os<<L"status privs ";
322 if(privs & PRIV_SERVER)
323 os<<L"shutdown setting ";
324 if(privs & PRIV_SETTIME)
326 if(privs & PRIV_TELEPORT)
328 if(privs & PRIV_PRIVS)
329 os<<L" grant revoke";
333 else if(ctx->parms[0] == L"status")
335 else if(ctx->parms[0] == L"privs")
337 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
338 cmd_grantrevoke(os, ctx);
339 else if(ctx->parms[0] == L"time")
341 else if(ctx->parms[0] == L"shutdown")
342 cmd_shutdown(os, ctx);
343 else if(ctx->parms[0] == L"setting")
344 cmd_setting(os, ctx);
345 else if(ctx->parms[0] == L"teleport")
346 cmd_teleport(os, ctx);
347 else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
348 cmd_banunban(os, ctx);
349 else if(ctx->parms[0] == L"me")
351 else if(ctx->parms[0] == L"clearobjects")
352 cmd_clearobjects(os, ctx);
354 os<<L"-!- Invalid command: " + ctx->parms[0];