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