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