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 os<<L"-!- " + privsToString(ctx->player->privs);
40 if((ctx->player->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"-!- " + privsToString(tp->privs);
56 void cmd_grantrevoke(std::wostringstream &os,
57 ServerCommandContext *ctx)
59 if(ctx->parms.size() != 3)
61 os<<L"-!- Missing parameter";
65 if((ctx->player->privs & PRIV_PRIVS) == 0)
67 os<<L"-!- You don't have permission to do that";
71 u64 newprivs = stringToPrivs(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 if(ctx->parms[0] == L"grant")
86 tp->privs |= newprivs;
88 tp->privs &= ~newprivs;
90 os<<L"-!- Privileges change to ";
91 os<<privsToString(tp->privs);
94 void cmd_time(std::wostringstream &os,
95 ServerCommandContext *ctx)
97 if(ctx->parms.size() != 2)
99 os<<L"-!- Missing parameter";
103 if((ctx->player->privs & PRIV_SETTIME) ==0)
105 os<<L"-!- You don't have permission to do that";
109 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
110 ctx->server->setTimeOfDay(time);
111 os<<L"-!- time_of_day changed.";
114 void cmd_shutdown(std::wostringstream &os,
115 ServerCommandContext *ctx)
117 if((ctx->player->privs & PRIV_SERVER) ==0)
119 os<<L"-!- You don't have permission to do that";
123 dstream<<DTIME<<" Server: Operator requested shutdown."
125 ctx->server->requestShutdown();
127 os<<L"*** Server shutting down (operator request)";
131 void cmd_setting(std::wostringstream &os,
132 ServerCommandContext *ctx)
134 if((ctx->player->privs & PRIV_SERVER) ==0)
136 os<<L"-!- You don't have permission to do that";
140 std::string confline = wide_to_narrow(ctx->parms[1] + L" = " + ctx->parms[2]);
141 g_settings.parseConfigLine(confline);
142 os<< L"-!- Setting changed.";
145 void cmd_teleport(std::wostringstream &os,
146 ServerCommandContext *ctx)
148 if((ctx->player->privs & PRIV_TELEPORT) ==0)
150 os<<L"-!- You don't have permission to do that";
154 if(ctx->parms.size() != 2)
156 os<<L"-!- Missing parameter";
160 std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
161 if(coords.size() != 3)
163 os<<L"-!- You can only specify coordinates currently";
167 v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
168 ctx->player->setPosition(dest);
169 ctx->server->SendMovePlayer(ctx->player);
171 os<< L"-!- Teleported.";
175 std::wstring processServerCommand(ServerCommandContext *ctx)
178 std::wostringstream os(std::ios_base::binary);
179 ctx->flags = 1; // Default, unless we change it.
181 u64 privs = ctx->player->privs;
183 if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
185 os<<L"-!- Available commands: ";
186 os<<L"status privs ";
187 if(privs & PRIV_SERVER)
188 os<<L"shutdown setting ";
189 if(privs & PRIV_SETTIME)
191 if(privs & PRIV_TELEPORT)
193 if(privs & PRIV_PRIVS)
194 os<<L" grant revoke";
196 else if(ctx->parms[0] == L"status")
200 else if(ctx->parms[0] == L"privs")
204 else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
206 cmd_grantrevoke(os, ctx);
208 else if(ctx->parms[0] == L"time")
212 else if(ctx->parms[0] == L"shutdown")
214 cmd_shutdown(os, ctx);
216 else if(ctx->parms[0] == L"setting")
218 cmd_setting(os, ctx);
220 else if(ctx->parms[0] == L"teleport")
222 cmd_teleport(os, ctx);
226 os<<L"-!- Invalid command: " + ctx->parms[0];