]> git.lizzy.rs Git - minetest.git/blob - src/servercommand.cpp
Fixed/extended/modified ban stuff to be good for inclusion
[minetest.git] / src / servercommand.cpp
1 /*
2 Part of Minetest-c55
3 Copyright (C) 2010-11 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
5
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.
9
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.
17 */
18
19 #include "servercommand.h"
20 #include "utility.h"
21
22 void cmd_status(std::wostringstream &os,
23         ServerCommandContext *ctx)
24 {
25         os<<ctx->server->getStatusString();
26 }
27
28 void cmd_privs(std::wostringstream &os,
29         ServerCommandContext *ctx)
30 {
31         if(ctx->parms.size() == 1)
32         {
33                 // Show our own real privs, without any adjustments
34                 // made for admin status
35                 os<<L"-!- " + narrow_to_wide(privsToString(
36                                 ctx->server->getPlayerAuthPrivs(ctx->player->getName())));
37                 return;
38         }
39
40         if((ctx->privs & PRIV_PRIVS) == 0)
41         {
42                 os<<L"-!- You don't have permission to do that";
43                 return;
44         }
45                 
46         Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
47         if(tp == NULL)
48         {
49                 os<<L"-!- No such player";
50                 return;
51         }
52         
53         os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
54 }
55
56 void cmd_grantrevoke(std::wostringstream &os,
57         ServerCommandContext *ctx)
58 {
59         if(ctx->parms.size() != 3)
60         {
61                 os<<L"-!- Missing parameter";
62                 return;
63         }
64
65         if((ctx->privs & PRIV_PRIVS) == 0)
66         {
67                 os<<L"-!- You don't have permission to do that";
68                 return;
69         }
70
71         u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
72         if(newprivs == PRIV_INVALID)
73         {
74                 os<<L"-!- Invalid privileges specified";
75                 return;
76         }
77
78         Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
79         if(tp == NULL)
80         {
81                 os<<L"-!- No such player";
82                 return;
83         }
84         
85         std::string playername = wide_to_narrow(ctx->parms[1]);
86         u64 privs = ctx->server->getPlayerAuthPrivs(playername);
87
88         if(ctx->parms[0] == L"grant")
89                 privs |= newprivs;
90         else
91                 privs &= ~newprivs;
92         
93         ctx->server->setPlayerAuthPrivs(playername, privs);
94         
95         os<<L"-!- Privileges change to ";
96         os<<narrow_to_wide(privsToString(privs));
97 }
98
99 void cmd_time(std::wostringstream &os,
100         ServerCommandContext *ctx)
101 {
102         if(ctx->parms.size() != 2)
103         {
104                 os<<L"-!- Missing parameter";
105                 return;
106         }
107
108         if((ctx->privs & PRIV_SETTIME) ==0)
109         {
110                 os<<L"-!- You don't have permission to do that";
111                 return;
112         }
113
114         u32 time = stoi(wide_to_narrow(ctx->parms[1]));
115         ctx->server->setTimeOfDay(time);
116         os<<L"-!- time_of_day changed.";
117 }
118
119 void cmd_shutdown(std::wostringstream &os,
120         ServerCommandContext *ctx)
121 {
122         if((ctx->privs & PRIV_SERVER) ==0)
123         {
124                 os<<L"-!- You don't have permission to do that";
125                 return;
126         }
127
128         dstream<<DTIME<<" Server: Operator requested shutdown."
129                 <<std::endl;
130         ctx->server->requestShutdown();
131                                         
132         os<<L"*** Server shutting down (operator request)";
133         ctx->flags |= 2;
134 }
135
136 void cmd_setting(std::wostringstream &os,
137         ServerCommandContext *ctx)
138 {
139         if((ctx->privs & PRIV_SERVER) ==0)
140         {
141                 os<<L"-!- You don't have permission to do that";
142                 return;
143         }
144
145         /*std::string confline = wide_to_narrow(
146                         ctx->parms[1] + L" = " + ctx->params[2]);*/
147
148         std::string confline = wide_to_narrow(ctx->paramstring);
149         
150         g_settings.parseConfigLine(confline);
151         
152         ctx->server->saveConfig();
153
154         os<< L"-!- Setting changed and configuration saved.";
155 }
156
157 void cmd_teleport(std::wostringstream &os,
158         ServerCommandContext *ctx)
159 {
160         if((ctx->privs & PRIV_TELEPORT) ==0)
161         {
162                 os<<L"-!- You don't have permission to do that";
163                 return;
164         }
165
166         if(ctx->parms.size() != 2)
167         {
168                 os<<L"-!- Missing parameter";
169                 return;
170         }
171
172         std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
173         if(coords.size() != 3)
174         {
175                 os<<L"-!- You can only specify coordinates currently";
176                 return;
177         }
178
179         v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
180         ctx->player->setPosition(dest);
181         ctx->server->SendMovePlayer(ctx->player);
182
183         os<< L"-!- Teleported.";
184 }
185
186 void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
187 {
188         if((ctx->privs && PRIV_BAN) == 0)
189         {
190                 os<<L"-!- You don't have permission to do that";
191                 return;
192         }
193
194         if(ctx->parms.size() < 2)
195         {
196                 std::string desc = ctx->server->getBanDescription("");
197                 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
198                 return;
199         }
200         if(ctx->parms[0] == L"ban")
201         {
202                 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
203
204                 if(player == NULL)
205                 {
206                         os<<L"-!- No such player";
207                         return;
208                 }
209
210                 con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
211                 if(peer == NULL)
212                 {
213                         dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
214                         return;
215                 }
216                 std::string ip_string = peer->address.serializeString();
217                 ctx->server->setIpBanned(ip_string, player->getName());
218                 os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
219                                 <<narrow_to_wide(player->getName());
220         }
221         else
222         {
223                 std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
224                 std::string desc = ctx->server->getBanDescription(ip_or_name);
225                 ctx->server->unsetIpBanned(ip_or_name);
226                 os<<L"-!- Unbanned "<<narrow_to_wide(desc);
227         }
228 }
229
230
231 std::wstring processServerCommand(ServerCommandContext *ctx)
232 {
233
234         std::wostringstream os(std::ios_base::binary);
235         ctx->flags = 1; // Default, unless we change it.
236
237         u64 privs = ctx->privs;
238
239         if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
240         {
241                 os<<L"-!- Available commands: ";
242                 os<<L"status privs ";
243                 if(privs & PRIV_SERVER)
244                         os<<L"shutdown setting ";
245                 if(privs & PRIV_SETTIME)
246                         os<<L" time";
247                 if(privs & PRIV_TELEPORT)
248                         os<<L" teleport";
249                 if(privs & PRIV_PRIVS)
250                         os<<L" grant revoke";
251                 if(privs & PRIV_BAN)
252                         os<<L" ban unban";
253         }
254         else if(ctx->parms[0] == L"status")
255         {
256                 cmd_status(os, ctx);
257         }
258         else if(ctx->parms[0] == L"privs")
259         {
260                 cmd_privs(os, ctx);
261         }
262         else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
263         {
264                 cmd_grantrevoke(os, ctx);
265         }
266         else if(ctx->parms[0] == L"time")
267         {
268                 cmd_time(os, ctx);
269         }
270         else if(ctx->parms[0] == L"shutdown")
271         {
272                 cmd_shutdown(os, ctx);
273         }
274         else if(ctx->parms[0] == L"setting")
275         {
276                 cmd_setting(os, ctx);
277         }
278         else if(ctx->parms[0] == L"teleport")
279         {
280                 cmd_teleport(os, ctx);
281         }
282         else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
283         {
284                 cmd_banunban(os, ctx);
285         }
286         else
287         {
288                 os<<L"-!- Invalid command: " + ctx->parms[0];
289         }
290         return os.str();
291 }
292
293