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 void cmd_status(std::wostringstream &os,
24 ServerCommandContext *ctx)
26 os<<ctx->server->getStatusString();
29 void cmd_me(std::wostringstream &os,
30 ServerCommandContext *ctx)
32 std::wstring name = narrow_to_wide(ctx->player->getName());
33 os << L"* " << name << L" " << ctx->paramstring;
34 ctx->flags |= SEND_TO_OTHERS | SEND_NO_PREFIX;
37 void cmd_privs(std::wostringstream &os,
38 ServerCommandContext *ctx)
40 if(ctx->parms.size() == 1)
42 // Show our own real privs, without any adjustments
43 // made for admin status
44 os<<L"-!- " + narrow_to_wide(privsToString(
45 ctx->server->getPlayerAuthPrivs(ctx->player->getName())));
49 if((ctx->privs & PRIV_PRIVS) == 0)
51 os<<L"-!- You don't have permission to do that";
55 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
58 os<<L"-!- No such player";
62 os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
65 void cmd_grantrevoke(std::wostringstream &os,
66 ServerCommandContext *ctx)
68 if(ctx->parms.size() != 3)
70 os<<L"-!- Missing parameter";
74 if((ctx->privs & PRIV_PRIVS) == 0)
76 os<<L"-!- You don't have permission to do that";
80 u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
81 if(newprivs == PRIV_INVALID)
83 os<<L"-!- Invalid privileges specified";
87 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
90 os<<L"-!- No such player";
94 std::string playername = wide_to_narrow(ctx->parms[1]);
95 u64 privs = ctx->server->getPlayerAuthPrivs(playername);
97 if(ctx->parms[0] == L"grant")
102 ctx->server->setPlayerAuthPrivs(playername, privs);
104 os<<L"-!- Privileges change to ";
105 os<<narrow_to_wide(privsToString(privs));
108 void cmd_time(std::wostringstream &os,
109 ServerCommandContext *ctx)
111 if(ctx->parms.size() != 2)
113 os<<L"-!- Missing parameter";
117 if((ctx->privs & PRIV_SETTIME) ==0)
119 os<<L"-!- You don't have permission to do that";
123 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
124 ctx->server->setTimeOfDay(time);
125 os<<L"-!- time_of_day changed.";
128 void cmd_shutdown(std::wostringstream &os,
129 ServerCommandContext *ctx)
131 if((ctx->privs & PRIV_SERVER) ==0)
133 os<<L"-!- You don't have permission to do that";
137 dstream<<DTIME<<" Server: Operator requested shutdown."
139 ctx->server->requestShutdown();
141 os<<L"*** Server shutting down (operator request)";
142 ctx->flags |= SEND_TO_OTHERS;
145 void cmd_setting(std::wostringstream &os,
146 ServerCommandContext *ctx)
148 if((ctx->privs & PRIV_SERVER) ==0)
150 os<<L"-!- You don't have permission to do that";
154 /*std::string confline = wide_to_narrow(
155 ctx->parms[1] + L" = " + ctx->params[2]);*/
157 std::string confline = wide_to_narrow(ctx->paramstring);
159 g_settings->parseConfigLine(confline);
161 ctx->server->saveConfig();
163 os<< L"-!- Setting changed and configuration saved.";
166 void cmd_teleport(std::wostringstream &os,
167 ServerCommandContext *ctx)
169 if((ctx->privs & PRIV_TELEPORT) ==0)
171 os<<L"-!- You don't have permission to do that";
175 if(ctx->parms.size() != 2)
177 os<<L"-!- Missing parameter";
181 std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
182 if(coords.size() != 3)
184 os<<L"-!- You can only specify coordinates currently";
188 v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
189 ctx->player->setPosition(dest);
190 ctx->server->SendMovePlayer(ctx->player);
192 os<< L"-!- Teleported.";
195 void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
197 if((ctx->privs & PRIV_BAN) == 0)
199 os<<L"-!- You don't have permission to do that";
203 if(ctx->parms.size() < 2)
205 std::string desc = ctx->server->getBanDescription("");
206 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
209 if(ctx->parms[0] == L"ban")
211 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
215 os<<L"-!- No such player";
219 con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
222 dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
225 std::string ip_string = peer->address.serializeString();
226 ctx->server->setIpBanned(ip_string, player->getName());
227 os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
228 <<narrow_to_wide(player->getName());
232 std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
233 std::string desc = ctx->server->getBanDescription(ip_or_name);
234 ctx->server->unsetIpBanned(ip_or_name);
235 os<<L"-!- Unbanned "<<narrow_to_wide(desc);
240 std::wstring processServerCommand(ServerCommandContext *ctx)
243 std::wostringstream os(std::ios_base::binary);
244 ctx->flags = SEND_TO_SENDER; // Default, unless we change it.
246 u64 privs = ctx->privs;
248 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
250 os<<L"-!- Available commands: ";
251 os<<L"status privs ";
252 if(privs & PRIV_SERVER)
253 os<<L"shutdown setting ";
254 if(privs & PRIV_SETTIME)
256 if(privs & PRIV_TELEPORT)
258 if(privs & PRIV_PRIVS)
259 os<<L" grant revoke";
263 else if(ctx->parms[0] == L"status")
267 else if(ctx->parms[0] == L"privs")
271 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
273 cmd_grantrevoke(os, ctx);
275 else if(ctx->parms[0] == L"time")
279 else if(ctx->parms[0] == L"shutdown")
281 cmd_shutdown(os, ctx);
283 else if(ctx->parms[0] == L"setting")
285 cmd_setting(os, ctx);
287 else if(ctx->parms[0] == L"teleport")
289 cmd_teleport(os, ctx);
291 else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
293 cmd_banunban(os, ctx);
295 else if(ctx->parms[0] == L"me")
301 os<<L"-!- Invalid command: " + ctx->parms[0];