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