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_privs(std::wostringstream &os,
29 ServerCommandContext *ctx)
31 if(ctx->parms.size() == 1)
33 // Show our own real privs, without any adjustments
34 // made for admin status
35 os<<L"-!- " + narrow_to_wide(privsToString(
36 ctx->server->getPlayerAuthPrivs(ctx->player->getName())));
40 if((ctx->privs & PRIV_PRIVS) == 0)
42 os<<L"-!- You don't have permission to do that";
46 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
49 os<<L"-!- No such player";
53 os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
56 void cmd_grantrevoke(std::wostringstream &os,
57 ServerCommandContext *ctx)
59 if(ctx->parms.size() != 3)
61 os<<L"-!- Missing parameter";
65 if((ctx->privs & PRIV_PRIVS) == 0)
67 os<<L"-!- You don't have permission to do that";
71 u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
72 if(newprivs == PRIV_INVALID)
74 os<<L"-!- Invalid privileges specified";
78 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
81 os<<L"-!- No such player";
85 std::string playername = wide_to_narrow(ctx->parms[1]);
86 u64 privs = ctx->server->getPlayerAuthPrivs(playername);
88 if(ctx->parms[0] == L"grant")
93 ctx->server->setPlayerAuthPrivs(playername, privs);
95 os<<L"-!- Privileges change to ";
96 os<<narrow_to_wide(privsToString(privs));
99 void cmd_time(std::wostringstream &os,
100 ServerCommandContext *ctx)
102 if(ctx->parms.size() != 2)
104 os<<L"-!- Missing parameter";
108 if((ctx->privs & PRIV_SETTIME) ==0)
110 os<<L"-!- You don't have permission to do that";
114 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
115 ctx->server->setTimeOfDay(time);
116 os<<L"-!- time_of_day changed.";
119 void cmd_shutdown(std::wostringstream &os,
120 ServerCommandContext *ctx)
122 if((ctx->privs & PRIV_SERVER) ==0)
124 os<<L"-!- You don't have permission to do that";
128 dstream<<DTIME<<" Server: Operator requested shutdown."
130 ctx->server->requestShutdown();
132 os<<L"*** Server shutting down (operator request)";
136 void cmd_setting(std::wostringstream &os,
137 ServerCommandContext *ctx)
139 if((ctx->privs & PRIV_SERVER) ==0)
141 os<<L"-!- You don't have permission to do that";
145 /*std::string confline = wide_to_narrow(
146 ctx->parms[1] + L" = " + ctx->params[2]);*/
148 std::string confline = wide_to_narrow(ctx->paramstring);
150 g_settings.parseConfigLine(confline);
152 ctx->server->saveConfig();
154 os<< L"-!- Setting changed and configuration saved.";
157 void cmd_teleport(std::wostringstream &os,
158 ServerCommandContext *ctx)
160 if((ctx->privs & PRIV_TELEPORT) ==0)
162 os<<L"-!- You don't have permission to do that";
166 if(ctx->parms.size() != 2)
168 os<<L"-!- Missing parameter";
172 std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
173 if(coords.size() != 3)
175 os<<L"-!- You can only specify coordinates currently";
179 v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
180 ctx->player->setPosition(dest);
181 ctx->server->SendMovePlayer(ctx->player);
183 os<< L"-!- Teleported.";
186 void cmd_ipbanunban(std::wostringstream &os, ServerCommandContext *ctx)
188 if((ctx->privs && PRIV_BAN) == 0)
190 os<<L"-!- You don't have permission to do that";
194 if(ctx->parms.size() != 2)
196 os<<L"-!- Missing parameter";
199 if(ctx->parms[0] == L"ipban")
201 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
205 os<<L"-!- No such Player!";
209 con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
212 dstream<<"peer was not found!"<<std::endl;
214 ctx->server->setIpBanned(peer->address.serializeString());
215 os<<L"-!- IP: "<<narrow_to_wide(peer->address.serializeString())<<L" was banned!";
219 ctx->server->unsetIpBanned(wide_to_narrow(ctx->parms[1]));
220 os<<L"-!- IP: "<<ctx->parms[1]<<L" was unbanned!";
225 std::wstring processServerCommand(ServerCommandContext *ctx)
228 std::wostringstream os(std::ios_base::binary);
229 ctx->flags = 1; // Default, unless we change it.
231 u64 privs = ctx->privs;
233 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
235 os<<L"-!- Available commands: ";
236 os<<L"status privs ";
237 if(privs & PRIV_SERVER)
238 os<<L"shutdown setting ";
239 if(privs & PRIV_SETTIME)
241 if(privs & PRIV_TELEPORT)
243 if(privs & PRIV_PRIVS)
244 os<<L" grant revoke";
246 os<<L" ipban ipunban";
248 else if(ctx->parms[0] == L"status")
252 else if(ctx->parms[0] == L"privs")
256 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
258 cmd_grantrevoke(os, ctx);
260 else if(ctx->parms[0] == L"time")
264 else if(ctx->parms[0] == L"shutdown")
266 cmd_shutdown(os, ctx);
268 else if(ctx->parms[0] == L"setting")
270 cmd_setting(os, ctx);
272 else if(ctx->parms[0] == L"teleport")
274 cmd_teleport(os, ctx);
276 else if(ctx->parms[0] == L"ipban" || ctx->parms[0] == L"ipunban")
278 cmd_ipbanunban(os, ctx);
282 os<<L"-!- Invalid command: " + ctx->parms[0];