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"){
100 actionstream<<ctx->player->getName()<<" grants "
101 <<wide_to_narrow(ctx->parms[2])<<" to "
102 <<playername<<std::endl;
105 actionstream<<ctx->player->getName()<<" revokes "
106 <<wide_to_narrow(ctx->parms[2])<<" from "
107 <<playername<<std::endl;
111 ctx->server->setPlayerAuthPrivs(playername, privs);
113 os<<L"-!- Privileges change to ";
114 os<<narrow_to_wide(privsToString(privs));
117 void cmd_time(std::wostringstream &os,
118 ServerCommandContext *ctx)
120 if(ctx->parms.size() != 2)
122 os<<L"-!- Missing parameter";
126 if((ctx->privs & PRIV_SETTIME) ==0)
128 os<<L"-!- You don't have permission to do that";
132 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
133 ctx->server->setTimeOfDay(time);
134 os<<L"-!- time_of_day changed.";
136 actionstream<<ctx->player->getName()<<" sets time "
140 void cmd_shutdown(std::wostringstream &os,
141 ServerCommandContext *ctx)
143 if((ctx->privs & PRIV_SERVER) ==0)
145 os<<L"-!- You don't have permission to do that";
149 actionstream<<ctx->player->getName()
150 <<" shuts down server"<<std::endl;
152 ctx->server->requestShutdown();
154 os<<L"*** Server shutting down (operator request)";
155 ctx->flags |= SEND_TO_OTHERS;
158 void cmd_setting(std::wostringstream &os,
159 ServerCommandContext *ctx)
161 if((ctx->privs & PRIV_SERVER) ==0)
163 os<<L"-!- You don't have permission to do that";
167 /*std::string confline = wide_to_narrow(
168 ctx->parms[1] + L" = " + ctx->params[2]);*/
170 std::string confline = wide_to_narrow(ctx->paramstring);
172 actionstream<<ctx->player->getName()
173 <<" sets: "<<confline<<std::endl;
175 g_settings->parseConfigLine(confline);
177 ctx->server->saveConfig();
179 os<< L"-!- Setting changed and configuration saved.";
182 void cmd_teleport(std::wostringstream &os,
183 ServerCommandContext *ctx)
185 if((ctx->privs & PRIV_TELEPORT) ==0)
187 os<<L"-!- You don't have permission to do that";
191 if(ctx->parms.size() != 2)
193 os<<L"-!- Missing parameter";
197 std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
198 if(coords.size() != 3)
200 os<<L"-!- You can only specify coordinates currently";
204 v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
206 actionstream<<ctx->player->getName()<<" teleports from "
207 <<PP(ctx->player->getPosition()/BS)<<" to "
208 <<PP(dest/BS)<<std::endl;
210 ctx->player->setPosition(dest);
211 ctx->server->SendMovePlayer(ctx->player);
213 os<< L"-!- Teleported.";
216 void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
218 if((ctx->privs & PRIV_BAN) == 0)
220 os<<L"-!- You don't have permission to do that";
224 if(ctx->parms.size() < 2)
226 std::string desc = ctx->server->getBanDescription("");
227 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
230 if(ctx->parms[0] == L"ban")
232 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
236 os<<L"-!- No such player";
240 con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
243 dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
246 std::string ip_string = peer->address.serializeString();
247 ctx->server->setIpBanned(ip_string, player->getName());
248 os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
249 <<narrow_to_wide(player->getName());
251 actionstream<<ctx->player->getName()<<" bans "
252 <<player->getName()<<" / "<<ip_string<<std::endl;
256 std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
257 std::string desc = ctx->server->getBanDescription(ip_or_name);
258 ctx->server->unsetIpBanned(ip_or_name);
259 os<<L"-!- Unbanned "<<narrow_to_wide(desc);
261 actionstream<<ctx->player->getName()<<" unbans "
262 <<ip_or_name<<std::endl;
267 std::wstring processServerCommand(ServerCommandContext *ctx)
270 std::wostringstream os(std::ios_base::binary);
271 ctx->flags = SEND_TO_SENDER; // Default, unless we change it.
273 u64 privs = ctx->privs;
275 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
277 os<<L"-!- Available commands: ";
278 os<<L"status privs ";
279 if(privs & PRIV_SERVER)
280 os<<L"shutdown setting ";
281 if(privs & PRIV_SETTIME)
283 if(privs & PRIV_TELEPORT)
285 if(privs & PRIV_PRIVS)
286 os<<L" grant revoke";
290 else if(ctx->parms[0] == L"status")
294 else if(ctx->parms[0] == L"privs")
298 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
300 cmd_grantrevoke(os, ctx);
302 else if(ctx->parms[0] == L"time")
306 else if(ctx->parms[0] == L"shutdown")
308 cmd_shutdown(os, ctx);
310 else if(ctx->parms[0] == L"setting")
312 cmd_setting(os, ctx);
314 else if(ctx->parms[0] == L"teleport")
316 cmd_teleport(os, ctx);
318 else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
320 cmd_banunban(os, ctx);
322 else if(ctx->parms[0] == L"me")
328 os<<L"-!- Invalid command: " + ctx->parms[0];