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 void cmd_status(std::wostringstream &os,
23 ServerCommandContext *ctx)
25 os<<ctx->server->getStatusString();
28 void cmd_me(std::wostringstream &os,
29 ServerCommandContext *ctx)
31 std::wstring name = narrow_to_wide(ctx->player->getName());
32 os << L"* " << name << L" " << ctx->paramstring;
33 ctx->flags |= SEND_TO_OTHERS | SEND_NO_PREFIX;
36 void cmd_privs(std::wostringstream &os,
37 ServerCommandContext *ctx)
39 if(ctx->parms.size() == 1)
41 // Show our own real privs, without any adjustments
42 // made for admin status
43 os<<L"-!- " + narrow_to_wide(privsToString(
44 ctx->server->getPlayerAuthPrivs(ctx->player->getName())));
48 if((ctx->privs & PRIV_PRIVS) == 0)
50 os<<L"-!- You don't have permission to do that";
54 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
57 os<<L"-!- No such player";
61 os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
64 void cmd_grantrevoke(std::wostringstream &os,
65 ServerCommandContext *ctx)
67 if(ctx->parms.size() != 3)
69 os<<L"-!- Missing parameter";
73 if((ctx->privs & PRIV_PRIVS) == 0)
75 os<<L"-!- You don't have permission to do that";
79 u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
80 if(newprivs == PRIV_INVALID)
82 os<<L"-!- Invalid privileges specified";
86 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
89 os<<L"-!- No such player";
93 std::string playername = wide_to_narrow(ctx->parms[1]);
94 u64 privs = ctx->server->getPlayerAuthPrivs(playername);
96 if(ctx->parms[0] == L"grant")
101 ctx->server->setPlayerAuthPrivs(playername, privs);
103 os<<L"-!- Privileges change to ";
104 os<<narrow_to_wide(privsToString(privs));
107 void cmd_time(std::wostringstream &os,
108 ServerCommandContext *ctx)
110 if(ctx->parms.size() != 2)
112 os<<L"-!- Missing parameter";
116 if((ctx->privs & PRIV_SETTIME) ==0)
118 os<<L"-!- You don't have permission to do that";
122 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
123 ctx->server->setTimeOfDay(time);
124 os<<L"-!- time_of_day changed.";
127 void cmd_shutdown(std::wostringstream &os,
128 ServerCommandContext *ctx)
130 if((ctx->privs & PRIV_SERVER) ==0)
132 os<<L"-!- You don't have permission to do that";
136 dstream<<DTIME<<" Server: Operator requested shutdown."
138 ctx->server->requestShutdown();
140 os<<L"*** Server shutting down (operator request)";
141 ctx->flags |= SEND_TO_OTHERS;
144 void cmd_setting(std::wostringstream &os,
145 ServerCommandContext *ctx)
147 if((ctx->privs & PRIV_SERVER) ==0)
149 os<<L"-!- You don't have permission to do that";
153 /*std::string confline = wide_to_narrow(
154 ctx->parms[1] + L" = " + ctx->params[2]);*/
156 std::string confline = wide_to_narrow(ctx->paramstring);
158 g_settings.parseConfigLine(confline);
160 ctx->server->saveConfig();
162 os<< L"-!- Setting changed and configuration saved.";
165 void cmd_teleport(std::wostringstream &os,
166 ServerCommandContext *ctx)
168 if((ctx->privs & PRIV_TELEPORT) ==0)
170 os<<L"-!- You don't have permission to do that";
174 if(ctx->parms.size() != 2)
176 os<<L"-!- Missing parameter";
180 std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
181 if(coords.size() != 3)
183 os<<L"-!- You can only specify coordinates currently";
187 v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
188 ctx->player->setPosition(dest);
189 ctx->server->SendMovePlayer(ctx->player);
191 os<< L"-!- Teleported.";
194 void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
196 if((ctx->privs & PRIV_BAN) == 0)
198 os<<L"-!- You don't have permission to do that";
202 if(ctx->parms.size() < 2)
204 std::string desc = ctx->server->getBanDescription("");
205 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
208 if(ctx->parms[0] == L"ban")
210 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
214 os<<L"-!- No such player";
218 con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
221 dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
224 std::string ip_string = peer->address.serializeString();
225 ctx->server->setIpBanned(ip_string, player->getName());
226 os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
227 <<narrow_to_wide(player->getName());
231 std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
232 std::string desc = ctx->server->getBanDescription(ip_or_name);
233 ctx->server->unsetIpBanned(ip_or_name);
234 os<<L"-!- Unbanned "<<narrow_to_wide(desc);
239 std::wstring processServerCommand(ServerCommandContext *ctx)
242 std::wostringstream os(std::ios_base::binary);
243 ctx->flags = SEND_TO_SENDER; // Default, unless we change it.
245 u64 privs = ctx->privs;
247 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
249 os<<L"-!- Available commands: ";
250 os<<L"status privs ";
251 if(privs & PRIV_SERVER)
252 os<<L"shutdown setting ";
253 if(privs & PRIV_SETTIME)
255 if(privs & PRIV_TELEPORT)
257 if(privs & PRIV_PRIVS)
258 os<<L" grant revoke";
262 else if(ctx->parms[0] == L"status")
266 else if(ctx->parms[0] == L"privs")
270 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
272 cmd_grantrevoke(os, ctx);
274 else if(ctx->parms[0] == L"time")
278 else if(ctx->parms[0] == L"shutdown")
280 cmd_shutdown(os, ctx);
282 else if(ctx->parms[0] == L"setting")
284 cmd_setting(os, ctx);
286 else if(ctx->parms[0] == L"teleport")
288 cmd_teleport(os, ctx);
290 else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
292 cmd_banunban(os, ctx);
294 else if(ctx->parms[0] == L"me")
300 os<<L"-!- Invalid command: " + ctx->parms[0];