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";
255 Address address = ctx->server->getPeerAddress(player->peer_id);
256 std::string ip_string = address.serializeString();
257 ctx->server->setIpBanned(ip_string, player->getName());
258 os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
259 <<narrow_to_wide(player->getName());
261 actionstream<<ctx->player->getName()<<" bans "
262 <<player->getName()<<" / "<<ip_string<<std::endl;
263 } catch(con::PeerNotFoundException){
264 dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
269 std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
270 std::string desc = ctx->server->getBanDescription(ip_or_name);
271 ctx->server->unsetIpBanned(ip_or_name);
272 os<<L"-!- Unbanned "<<narrow_to_wide(desc);
274 actionstream<<ctx->player->getName()<<" unbans "
275 <<ip_or_name<<std::endl;
279 void cmd_clearobjects(std::wostringstream &os,
280 ServerCommandContext *ctx)
282 if((ctx->privs & PRIV_SERVER) ==0)
284 os<<L"-!- You don't have permission to do that";
288 actionstream<<ctx->player->getName()
289 <<" clears all objects"<<std::endl;
293 msg += L"Clearing all objects. This may take long.";
294 msg += L" You may experience a timeout. (by ";
295 msg += narrow_to_wide(ctx->player->getName());
297 ctx->server->notifyPlayers(msg);
300 ctx->env->clearAllObjects();
302 actionstream<<"object clearing done"<<std::endl;
304 os<<L"*** cleared all objects";
305 ctx->flags |= SEND_TO_OTHERS;
309 std::wstring processServerCommand(ServerCommandContext *ctx)
312 std::wostringstream os(std::ios_base::binary);
313 ctx->flags = SEND_TO_SENDER; // Default, unless we change it.
315 u64 privs = ctx->privs;
317 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
319 os<<L"-!- Available commands: ";
320 os<<L"status privs ";
321 if(privs & PRIV_SERVER)
322 os<<L"shutdown setting ";
323 if(privs & PRIV_SETTIME)
325 if(privs & PRIV_TELEPORT)
327 if(privs & PRIV_PRIVS)
328 os<<L" grant revoke";
332 else if(ctx->parms[0] == L"status")
334 else if(ctx->parms[0] == L"privs")
336 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
337 cmd_grantrevoke(os, ctx);
338 else if(ctx->parms[0] == L"time")
340 else if(ctx->parms[0] == L"shutdown")
341 cmd_shutdown(os, ctx);
342 else if(ctx->parms[0] == L"setting")
343 cmd_setting(os, ctx);
344 else if(ctx->parms[0] == L"teleport")
345 cmd_teleport(os, ctx);
346 else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
347 cmd_banunban(os, ctx);
348 else if(ctx->parms[0] == L"me")
350 else if(ctx->parms[0] == L"clearobjects")
351 cmd_clearobjects(os, ctx);
353 os<<L"-!- Invalid command: " + ctx->parms[0];