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);
226 ctx->server->SendMovePlayer(ctx->player);
228 os<< L"-!- Teleported.";
231 void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
233 if((ctx->privs & PRIV_BAN) == 0)
235 os<<L"-!- You don't have permission to do that";
239 if(ctx->parms.size() < 2)
241 std::string desc = ctx->server->getBanDescription("");
242 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
245 if(ctx->parms[0] == L"ban")
247 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
251 os<<L"-!- No such player";
256 Address address = ctx->server->getPeerAddress(player->peer_id);
257 std::string ip_string = address.serializeString();
258 ctx->server->setIpBanned(ip_string, player->getName());
259 os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
260 <<narrow_to_wide(player->getName());
262 actionstream<<ctx->player->getName()<<" bans "
263 <<player->getName()<<" / "<<ip_string<<std::endl;
264 } catch(con::PeerNotFoundException){
265 dstream<<__FUNCTION_NAME<<": peer was not found"<<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];