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