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