3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "servercommand.h"
25 void cmd_status(std::wostringstream &os,
26 ServerCommandContext *ctx)
28 os<<ctx->server->getStatusString();
31 void cmd_privs(std::wostringstream &os,
32 ServerCommandContext *ctx)
34 if(ctx->parms.size() == 1)
36 // Show our own real privs, without any adjustments
37 // made for admin status
38 os<<L"-!- " + privsToString(ctx->player->privs);
42 if((ctx->privs & PRIV_PRIVS) == 0)
44 os<<L"-!- You don't have permission to do that";
48 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
51 os<<L"-!- No such player";
55 os<<L"-!- " + privsToString(tp->privs);
58 void cmd_grantrevoke(std::wostringstream &os,
59 ServerCommandContext *ctx)
61 if(ctx->parms.size() != 3)
63 os<<L"-!- Missing parameter";
67 if((ctx->privs & PRIV_PRIVS) == 0)
69 os<<L"-!- You don't have permission to do that";
73 u64 newprivs = stringToPrivs(ctx->parms[2]);
74 if(newprivs == PRIV_INVALID)
76 os<<L"-!- Invalid privileges specified";
80 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
83 os<<L"-!- No such player";
87 if(ctx->parms[0] == L"grant")
88 tp->privs |= newprivs;
90 tp->privs &= ~newprivs;
92 os<<L"-!- Privileges change to ";
93 os<<privsToString(tp->privs);
96 void cmd_time(std::wostringstream &os,
97 ServerCommandContext *ctx)
99 if(ctx->parms.size() != 2)
101 os<<L"-!- Missing parameter";
105 if((ctx->privs & PRIV_SETTIME) ==0)
107 os<<L"-!- You don't have permission to do that";
111 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
112 ctx->server->setTimeOfDay(time);
113 os<<L"-!- time_of_day changed.";
116 void cmd_shutdown(std::wostringstream &os,
117 ServerCommandContext *ctx)
119 if((ctx->privs & PRIV_SERVER) ==0)
121 os<<L"-!- You don't have permission to do that";
125 dstream<<DTIME<<" Server: Operator requested shutdown."
127 ctx->server->requestShutdown();
129 os<<L"*** Server shutting down (operator request)";
133 void cmd_setting(std::wostringstream &os,
134 ServerCommandContext *ctx)
136 if((ctx->privs & PRIV_SERVER) ==0)
138 os<<L"-!- You don't have permission to do that";
142 std::string confline = wide_to_narrow(ctx->parms[1] + L" = " + ctx->parms[2]);
143 g_settings.parseConfigLine(confline);
144 os<< L"-!- Setting changed.";
147 void cmd_teleport(std::wostringstream &os,
148 ServerCommandContext *ctx)
150 if((ctx->privs & PRIV_TELEPORT) ==0)
152 os<<L"-!- You don't have permission to do that";
156 if(ctx->parms.size() != 2)
158 os<<L"-!- Missing parameter";
162 std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
163 if(coords.size() != 3)
165 os<<L"-!- You can only specify coordinates currently";
169 v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
170 ctx->player->setPosition(dest);
171 ctx->server->SendMovePlayer(ctx->player);
173 os<< L"-!- Teleported.";
177 std::wstring processServerCommand(ServerCommandContext *ctx)
180 std::wostringstream os(std::ios_base::binary);
181 ctx->flags = 1; // Default, unless we change it.
183 u64 privs = ctx->privs;
185 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
187 os<<L"-!- Available commands: ";
188 os<<L"status privs ";
189 if(privs & PRIV_SERVER)
190 os<<L"shutdown setting ";
191 if(privs & PRIV_SETTIME)
193 if(privs & PRIV_TELEPORT)
195 if(privs & PRIV_PRIVS)
196 os<<L" grant revoke";
198 else if(ctx->parms[0] == L"status")
202 else if(ctx->parms[0] == L"privs")
206 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
208 cmd_grantrevoke(os, ctx);
210 else if(ctx->parms[0] == L"time")
214 else if(ctx->parms[0] == L"shutdown")
216 cmd_shutdown(os, ctx);
218 else if(ctx->parms[0] == L"setting")
220 cmd_setting(os, ctx);
222 else if(ctx->parms[0] == L"teleport")
224 cmd_teleport(os, ctx);
228 os<<L"-!- Invalid command: " + ctx->parms[0];