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"
22 #include "main.h" // For g_settings
24 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
26 void cmd_status(std::wostringstream &os,
27 ServerCommandContext *ctx)
29 os<<ctx->server->getStatusString();
32 void cmd_me(std::wostringstream &os,
33 ServerCommandContext *ctx)
35 std::wstring name = narrow_to_wide(ctx->player->getName());
36 os << L"* " << name << L" " << ctx->paramstring;
37 ctx->flags |= SEND_TO_OTHERS | SEND_NO_PREFIX;
40 void cmd_privs(std::wostringstream &os,
41 ServerCommandContext *ctx)
43 if(ctx->parms.size() == 1)
45 // Show our own real privs, without any adjustments
46 // made for admin status
47 os<<L"-!- " + narrow_to_wide(privsToString(
48 ctx->server->getPlayerAuthPrivs(ctx->player->getName())));
52 if((ctx->privs & PRIV_PRIVS) == 0)
54 os<<L"-!- You don't have permission to do that";
58 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
61 os<<L"-!- No such player";
65 os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
68 void cmd_grantrevoke(std::wostringstream &os,
69 ServerCommandContext *ctx)
71 if(ctx->parms.size() != 3)
73 os<<L"-!- Missing parameter";
77 if((ctx->privs & PRIV_PRIVS) == 0)
79 os<<L"-!- You don't have permission to do that";
83 u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
84 if(newprivs == PRIV_INVALID)
86 os<<L"-!- Invalid privileges specified";
90 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
93 os<<L"-!- No such player";
97 std::string playername = wide_to_narrow(ctx->parms[1]);
98 u64 privs = ctx->server->getPlayerAuthPrivs(playername);
100 if(ctx->parms[0] == L"grant"){
102 actionstream<<ctx->player->getName()<<" grants "
103 <<wide_to_narrow(ctx->parms[2])<<" to "
104 <<playername<<std::endl;
107 msg += narrow_to_wide(ctx->player->getName());
108 msg += L" granted you the privilege \"";
109 msg += ctx->parms[2];
111 ctx->server->notifyPlayer(playername.c_str(), msg);
114 actionstream<<ctx->player->getName()<<" revokes "
115 <<wide_to_narrow(ctx->parms[2])<<" from "
116 <<playername<<std::endl;
119 msg += narrow_to_wide(ctx->player->getName());
120 msg += L" revoked from you the privilege \"";
121 msg += ctx->parms[2];
123 ctx->server->notifyPlayer(playername.c_str(), msg);
126 ctx->server->setPlayerAuthPrivs(playername, privs);
128 os<<L"-!- Privileges change to ";
129 os<<narrow_to_wide(privsToString(privs));
132 void cmd_time(std::wostringstream &os,
133 ServerCommandContext *ctx)
135 if(ctx->parms.size() != 2)
137 os<<L"-!- Missing parameter";
141 if((ctx->privs & PRIV_SETTIME) ==0)
143 os<<L"-!- You don't have permission to do that";
147 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
148 ctx->server->setTimeOfDay(time);
149 os<<L"-!- time_of_day changed.";
151 actionstream<<ctx->player->getName()<<" sets time "
155 void cmd_shutdown(std::wostringstream &os,
156 ServerCommandContext *ctx)
158 if((ctx->privs & PRIV_SERVER) ==0)
160 os<<L"-!- You don't have permission to do that";
164 actionstream<<ctx->player->getName()
165 <<" shuts down server"<<std::endl;
167 ctx->server->requestShutdown();
169 os<<L"*** Server shutting down (operator request)";
170 ctx->flags |= SEND_TO_OTHERS;
173 void cmd_setting(std::wostringstream &os,
174 ServerCommandContext *ctx)
176 if((ctx->privs & PRIV_SERVER) ==0)
178 os<<L"-!- You don't have permission to do that";
182 /*std::string confline = wide_to_narrow(
183 ctx->parms[1] + L" = " + ctx->params[2]);*/
185 std::string confline = wide_to_narrow(ctx->paramstring);
187 actionstream<<ctx->player->getName()
188 <<" sets: "<<confline<<std::endl;
190 g_settings->parseConfigLine(confline);
192 ctx->server->saveConfig();
194 os<< L"-!- Setting changed and configuration saved.";
197 void cmd_teleport(std::wostringstream &os,
198 ServerCommandContext *ctx)
200 if((ctx->privs & PRIV_TELEPORT) ==0)
202 os<<L"-!- You don't have permission to do that";
206 if(ctx->parms.size() != 2)
208 os<<L"-!- Missing parameter";
212 std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
213 if(coords.size() != 3)
215 os<<L"-!- You can only specify coordinates currently";
219 v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
221 actionstream<<ctx->player->getName()<<" teleports from "
222 <<PP(ctx->player->getPosition()/BS)<<" to "
223 <<PP(dest/BS)<<std::endl;
225 //ctx->player->setPosition(dest);
227 // Use the ServerActiveObject interface of ServerRemotePlayer
228 ServerRemotePlayer *srp = (ServerRemotePlayer*)ctx->player;
230 ctx->server->SendMovePlayer(ctx->player);
232 os<< L"-!- Teleported.";
235 void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
237 if((ctx->privs & PRIV_BAN) == 0)
239 os<<L"-!- You don't have permission to do that";
243 if(ctx->parms.size() < 2)
245 std::string desc = ctx->server->getBanDescription("");
246 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
249 if(ctx->parms[0] == L"ban")
251 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
255 os<<L"-!- No such player";
260 Address address = ctx->server->getPeerAddress(player->peer_id);
261 std::string ip_string = address.serializeString();
262 ctx->server->setIpBanned(ip_string, player->getName());
263 os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
264 <<narrow_to_wide(player->getName());
266 actionstream<<ctx->player->getName()<<" bans "
267 <<player->getName()<<" / "<<ip_string<<std::endl;
268 } catch(con::PeerNotFoundException){
269 dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
274 std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
275 std::string desc = ctx->server->getBanDescription(ip_or_name);
276 ctx->server->unsetIpBanned(ip_or_name);
277 os<<L"-!- Unbanned "<<narrow_to_wide(desc);
279 actionstream<<ctx->player->getName()<<" unbans "
280 <<ip_or_name<<std::endl;
284 void cmd_clearobjects(std::wostringstream &os,
285 ServerCommandContext *ctx)
287 if((ctx->privs & PRIV_SERVER) ==0)
289 os<<L"-!- You don't have permission to do that";
293 actionstream<<ctx->player->getName()
294 <<" clears all objects"<<std::endl;
298 msg += L"Clearing all objects. This may take long.";
299 msg += L" You may experience a timeout. (by ";
300 msg += narrow_to_wide(ctx->player->getName());
302 ctx->server->notifyPlayers(msg);
305 ctx->env->clearAllObjects();
307 actionstream<<"object clearing done"<<std::endl;
309 os<<L"*** cleared all objects";
310 ctx->flags |= SEND_TO_OTHERS;
314 std::wstring processServerCommand(ServerCommandContext *ctx)
317 std::wostringstream os(std::ios_base::binary);
318 ctx->flags = SEND_TO_SENDER; // Default, unless we change it.
320 u64 privs = ctx->privs;
322 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
324 os<<L"-!- Available commands: ";
325 os<<L"status privs ";
326 if(privs & PRIV_SERVER)
327 os<<L"shutdown setting ";
328 if(privs & PRIV_SETTIME)
330 if(privs & PRIV_TELEPORT)
332 if(privs & PRIV_PRIVS)
333 os<<L" grant revoke";
337 else if(ctx->parms[0] == L"status")
339 else if(ctx->parms[0] == L"privs")
341 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
342 cmd_grantrevoke(os, ctx);
343 else if(ctx->parms[0] == L"time")
345 else if(ctx->parms[0] == L"shutdown")
346 cmd_shutdown(os, ctx);
347 else if(ctx->parms[0] == L"setting")
348 cmd_setting(os, ctx);
349 else if(ctx->parms[0] == L"teleport")
350 cmd_teleport(os, ctx);
351 else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
352 cmd_banunban(os, ctx);
353 else if(ctx->parms[0] == L"me")
355 else if(ctx->parms[0] == L"clearobjects")
356 cmd_clearobjects(os, ctx);
358 os<<L"-!- Invalid command: " + ctx->parms[0];