]> git.lizzy.rs Git - minetest.git/blob - src/servercommand.cpp
Privileges to/from string conversion functions standalone, not static members
[minetest.git] / src / servercommand.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
5
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.
10
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.
15
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.
19 */
20
21
22 #include "servercommand.h"
23 #include "utility.h"
24
25 // Process a command sent from a client. The environment and connection
26 // should be locked when this is called.
27 // Returns a response message, to be dealt with according to the flags set
28 // in the context.
29 std::wstring ServerCommand::processCommand(ServerCommandContext *ctx)
30 {
31
32         std::wostringstream os(std::ios_base::binary);
33         ctx->flags = 1; // Default, unless we change it.
34
35         u64 privs = ctx->player->privs;
36
37         if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
38         {
39                 os<<L"-!- Available commands: ";
40                 os<<L"status privs ";
41                 if(privs & PRIV_SERVER)
42                         os<<L"shutdown setting ";
43                 if(privs & PRIV_SETTIME)
44                         os<<L" time";
45                 if(privs & PRIV_TELEPORT)
46                         os<<L" teleport";
47                 if(privs & PRIV_PRIVS)
48                         os<<L" grant revoke";
49         }
50         else if(ctx->parms[0] == L"status")
51         {
52                 cmd_status(os, ctx);
53         }
54         else if(ctx->parms[0] == L"privs")
55         {
56                 cmd_privs(os, ctx);
57         }
58         else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
59         {
60                 cmd_grantrevoke(os, ctx);
61         }
62         else if(ctx->parms[0] == L"time")
63         {
64                 cmd_time(os, ctx);
65         }
66         else if(ctx->parms[0] == L"shutdown")
67         {
68                 cmd_shutdown(os, ctx);
69         }
70         else if(ctx->parms[0] == L"setting")
71         {
72                 cmd_setting(os, ctx);
73         }
74         else if(ctx->parms[0] == L"teleport")
75         {
76                 cmd_teleport(os, ctx);
77         }
78         else
79         {
80                 os<<L"-!- Invalid command: " + ctx->parms[0];
81         }
82         return os.str();
83 }
84
85 void ServerCommand::cmd_status(std::wostringstream &os,
86         ServerCommandContext *ctx)
87 {
88         os<<ctx->server->getStatusString();
89 }
90
91 void ServerCommand::cmd_privs(std::wostringstream &os,
92         ServerCommandContext *ctx)
93 {
94         if(ctx->parms.size() == 1)
95         {
96                 os<<L"-!- " + privsToString(ctx->player->privs);
97                 return;
98         }
99
100         if((ctx->player->privs & PRIV_PRIVS) == 0)
101         {
102                 os<<L"-!- You don't have permission to do that";
103                 return;
104         }
105                 
106         Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
107         if(tp == NULL)
108         {
109                 os<<L"-!- No such player";
110                 return;
111         }
112         
113         os<<L"-!- " + privsToString(tp->privs);
114 }
115
116 void ServerCommand::cmd_grantrevoke(std::wostringstream &os,
117         ServerCommandContext *ctx)
118 {
119         if(ctx->parms.size() != 3)
120         {
121                 os<<L"-!- Missing parameter";
122                 return;
123         }
124
125         if((ctx->player->privs & PRIV_PRIVS) == 0)
126         {
127                 os<<L"-!- You don't have permission to do that";
128                 return;
129         }
130
131         u64 newprivs = stringToPrivs(ctx->parms[2]);
132         if(newprivs == PRIV_INVALID)
133         {
134                 os<<L"-!- Invalid privileges specified";
135                 return;
136         }
137
138         Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
139         if(tp == NULL)
140         {
141                 os<<L"-!- No such player";
142                 return;
143         }
144
145         if(ctx->parms[0] == L"grant")
146                 tp->privs |= newprivs;
147         else
148                 tp->privs &= ~newprivs;
149         
150         os<<L"-!- Privileges change to ";
151         os<<privsToString(tp->privs);
152 }
153
154 void ServerCommand::cmd_time(std::wostringstream &os,
155         ServerCommandContext *ctx)
156 {
157         if(ctx->parms.size() != 2)
158         {
159                 os<<L"-!- Missing parameter";
160                 return;
161         }
162
163         if((ctx->player->privs & PRIV_SETTIME) ==0)
164         {
165                 os<<L"-!- You don't have permission to do that";
166                 return;
167         }
168
169         u32 time = stoi(wide_to_narrow(ctx->parms[1]));
170         ctx->server->setTimeOfDay(time);
171         os<<L"-!- time_of_day changed.";
172 }
173
174 void ServerCommand::cmd_shutdown(std::wostringstream &os,
175         ServerCommandContext *ctx)
176 {
177         if((ctx->player->privs & PRIV_SERVER) ==0)
178         {
179                 os<<L"-!- You don't have permission to do that";
180                 return;
181         }
182
183         dstream<<DTIME<<" Server: Operator requested shutdown."
184                 <<std::endl;
185         ctx->server->requestShutdown();
186                                         
187         os<<L"*** Server shutting down (operator request)";
188         ctx->flags |= 2;
189 }
190
191 void ServerCommand::cmd_setting(std::wostringstream &os,
192         ServerCommandContext *ctx)
193 {
194         if((ctx->player->privs & PRIV_SERVER) ==0)
195         {
196                 os<<L"-!- You don't have permission to do that";
197                 return;
198         }
199
200         std::string confline = wide_to_narrow(ctx->parms[1] + L" = " + ctx->parms[2]);
201         g_settings.parseConfigLine(confline);
202         os<< L"-!- Setting changed.";
203 }
204
205 void ServerCommand::cmd_teleport(std::wostringstream &os,
206         ServerCommandContext *ctx)
207 {
208         if((ctx->player->privs & PRIV_TELEPORT) ==0)
209         {
210                 os<<L"-!- You don't have permission to do that";
211                 return;
212         }
213
214         if(ctx->parms.size() != 2)
215         {
216                 os<<L"-!- Missing parameter";
217                 return;
218         }
219
220         std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
221         if(coords.size() != 3)
222         {
223                 os<<L"-!- You can only specify coordinates currently";
224                 return;
225         }
226
227         v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
228         ctx->player->setPosition(dest);
229         ctx->server->SendMovePlayer(ctx->player);
230
231         os<< L"-!- Teleported.";
232 }
233