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"-!- " + narrow_to_wide(privsToString(
39 ctx->server->getPlayerAuthPrivs(ctx->player->getName())));
43 if((ctx->privs & PRIV_PRIVS) == 0)
45 os<<L"-!- You don't have permission to do that";
49 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
52 os<<L"-!- No such player";
56 os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
59 void cmd_grantrevoke(std::wostringstream &os,
60 ServerCommandContext *ctx)
62 if(ctx->parms.size() != 3)
64 os<<L"-!- Missing parameter";
68 if((ctx->privs & PRIV_PRIVS) == 0)
70 os<<L"-!- You don't have permission to do that";
74 u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
75 if(newprivs == PRIV_INVALID)
77 os<<L"-!- Invalid privileges specified";
81 Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
84 os<<L"-!- No such player";
88 std::string playername = wide_to_narrow(ctx->parms[1]);
89 u64 privs = ctx->server->getPlayerAuthPrivs(playername);
91 if(ctx->parms[0] == L"grant")
96 ctx->server->setPlayerAuthPrivs(playername, privs);
98 os<<L"-!- Privileges change to ";
99 os<<narrow_to_wide(privsToString(privs));
102 void cmd_time(std::wostringstream &os,
103 ServerCommandContext *ctx)
105 if(ctx->parms.size() != 2)
107 os<<L"-!- Missing parameter";
111 if((ctx->privs & PRIV_SETTIME) ==0)
113 os<<L"-!- You don't have permission to do that";
117 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
118 ctx->server->setTimeOfDay(time);
119 os<<L"-!- time_of_day changed.";
122 void cmd_shutdown(std::wostringstream &os,
123 ServerCommandContext *ctx)
125 if((ctx->privs & PRIV_SERVER) ==0)
127 os<<L"-!- You don't have permission to do that";
131 dstream<<DTIME<<" Server: Operator requested shutdown."
133 ctx->server->requestShutdown();
135 os<<L"*** Server shutting down (operator request)";
139 void cmd_setting(std::wostringstream &os,
140 ServerCommandContext *ctx)
142 if((ctx->privs & PRIV_SERVER) ==0)
144 os<<L"-!- You don't have permission to do that";
148 std::string confline = wide_to_narrow(ctx->parms[1] + L" = " + ctx->parms[2]);
149 g_settings.parseConfigLine(confline);
150 os<< L"-!- Setting changed.";
153 void cmd_teleport(std::wostringstream &os,
154 ServerCommandContext *ctx)
156 if((ctx->privs & PRIV_TELEPORT) ==0)
158 os<<L"-!- You don't have permission to do that";
162 if(ctx->parms.size() != 2)
164 os<<L"-!- Missing parameter";
168 std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
169 if(coords.size() != 3)
171 os<<L"-!- You can only specify coordinates currently";
175 v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
176 ctx->player->setPosition(dest);
177 ctx->server->SendMovePlayer(ctx->player);
179 os<< L"-!- Teleported.";
183 std::wstring processServerCommand(ServerCommandContext *ctx)
186 std::wostringstream os(std::ios_base::binary);
187 ctx->flags = 1; // Default, unless we change it.
189 u64 privs = ctx->privs;
191 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
193 os<<L"-!- Available commands: ";
194 os<<L"status privs ";
195 if(privs & PRIV_SERVER)
196 os<<L"shutdown setting ";
197 if(privs & PRIV_SETTIME)
199 if(privs & PRIV_TELEPORT)
201 if(privs & PRIV_PRIVS)
202 os<<L" grant revoke";
204 else if(ctx->parms[0] == L"status")
208 else if(ctx->parms[0] == L"privs")
212 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
214 cmd_grantrevoke(os, ctx);
216 else if(ctx->parms[0] == L"time")
220 else if(ctx->parms[0] == L"shutdown")
222 cmd_shutdown(os, ctx);
224 else if(ctx->parms[0] == L"setting")
226 cmd_setting(os, ctx);
228 else if(ctx->parms[0] == L"teleport")
230 cmd_teleport(os, ctx);
234 os<<L"-!- Invalid command: " + ctx->parms[0];