]> git.lizzy.rs Git - dragonfireclient.git/blob - src/servercommand.cpp
f6088925ea851d4067c7feac43b0b84c15b1cdd6
[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 #include "main.h" // For g_settings
23 #include "content_sao.h"
24
25 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
26
27 void cmd_status(std::wostringstream &os,
28         ServerCommandContext *ctx)
29 {
30         os<<ctx->server->getStatusString();
31 }
32
33 void cmd_me(std::wostringstream &os,
34         ServerCommandContext *ctx)
35 {
36         std::wstring name = narrow_to_wide(ctx->player->getName());
37         os << L"* " << name << L" " << ctx->paramstring;
38         ctx->flags |= SEND_TO_OTHERS | SEND_NO_PREFIX;
39 }
40
41 void cmd_time(std::wostringstream &os,
42         ServerCommandContext *ctx)
43 {
44         if(ctx->parms.size() != 2)
45         {
46                 os<<L"-!- Missing parameter";
47                 return;
48         }
49         
50         if(!ctx->server->checkPriv(ctx->player->getName(), "settime"))
51         {
52                 os<<L"-!- You don't have permission to do that";
53                 return;
54         }
55
56         u32 time = stoi(wide_to_narrow(ctx->parms[1]));
57         ctx->server->setTimeOfDay(time);
58         os<<L"-!- time_of_day changed.";
59
60         actionstream<<ctx->player->getName()<<" sets time "
61                         <<time<<std::endl;
62 }
63
64 void cmd_shutdown(std::wostringstream &os,
65         ServerCommandContext *ctx)
66 {
67         if(!ctx->server->checkPriv(ctx->player->getName(), "server"))
68         {
69                 os<<L"-!- You don't have permission to do that";
70                 return;
71         }
72
73         actionstream<<ctx->player->getName()
74                         <<" shuts down server"<<std::endl;
75
76         ctx->server->requestShutdown();
77                                         
78         os<<L"*** Server shutting down (operator request)";
79         ctx->flags |= SEND_TO_OTHERS;
80 }
81
82 void cmd_setting(std::wostringstream &os,
83         ServerCommandContext *ctx)
84 {
85         if(!ctx->server->checkPriv(ctx->player->getName(), "server"))
86         {
87                 os<<L"-!- You don't have permission to do that";
88                 return;
89         }
90
91         /*std::string confline = wide_to_narrow(
92                         ctx->parms[1] + L" = " + ctx->params[2]);*/
93
94         std::string confline = wide_to_narrow(ctx->paramstring);
95         
96         actionstream<<ctx->player->getName()
97                         <<" sets: "<<confline<<std::endl;
98
99         g_settings->parseConfigLine(confline);
100         
101         ctx->server->saveConfig();
102
103         os<< L"-!- Setting changed and configuration saved.";
104 }
105
106 void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
107 {
108         if(!ctx->server->checkPriv(ctx->player->getName(), "ban"))
109         {
110                 os<<L"-!- You don't have permission to do that";
111                 return;
112         }
113
114         if(ctx->parms.size() < 2)
115         {
116                 std::string desc = ctx->server->getBanDescription("");
117                 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
118                 return;
119         }
120         if(ctx->parms[0] == L"ban")
121         {
122                 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
123
124                 if(player == NULL)
125                 {
126                         os<<L"-!- No such player";
127                         return;
128                 }
129                 
130                 try{
131                         Address address = ctx->server->getPeerAddress(player->peer_id);
132                         std::string ip_string = address.serializeString();
133                         ctx->server->setIpBanned(ip_string, player->getName());
134                         os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
135                                         <<narrow_to_wide(player->getName());
136
137                         actionstream<<ctx->player->getName()<<" bans "
138                                         <<player->getName()<<" / "<<ip_string<<std::endl;
139                 } catch(con::PeerNotFoundException){
140                         dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
141                 }
142         }
143         else
144         {
145                 std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
146                 std::string desc = ctx->server->getBanDescription(ip_or_name);
147                 ctx->server->unsetIpBanned(ip_or_name);
148                 os<<L"-!- Unbanned "<<narrow_to_wide(desc);
149
150                 actionstream<<ctx->player->getName()<<" unbans "
151                                 <<ip_or_name<<std::endl;
152         }
153 }
154
155 void cmd_clearobjects(std::wostringstream &os,
156         ServerCommandContext *ctx)
157 {
158         if(!ctx->server->checkPriv(ctx->player->getName(), "server"))
159         {
160                 os<<L"-!- You don't have permission to do that";
161                 return;
162         }
163
164         actionstream<<ctx->player->getName()
165                         <<" clears all objects"<<std::endl;
166         
167         {
168                 std::wstring msg;
169                 msg += L"Clearing all objects. This may take long.";
170                 msg += L" You may experience a timeout. (by ";
171                 msg += narrow_to_wide(ctx->player->getName());
172                 msg += L")";
173                 ctx->server->notifyPlayers(msg);
174         }
175
176         ctx->env->clearAllObjects();
177                                         
178         actionstream<<"object clearing done"<<std::endl;
179         
180         os<<L"*** cleared all objects";
181         ctx->flags |= SEND_TO_OTHERS;
182 }
183
184
185 std::wstring processServerCommand(ServerCommandContext *ctx)
186 {
187
188         std::wostringstream os(std::ios_base::binary);
189         ctx->flags = SEND_TO_SENDER;    // Default, unless we change it.
190
191         if(ctx->parms[0] == L"status")
192                 cmd_status(os, ctx);
193         else if(ctx->parms[0] == L"time")
194                 cmd_time(os, ctx);
195         else if(ctx->parms[0] == L"shutdown")
196                 cmd_shutdown(os, ctx);
197         else if(ctx->parms[0] == L"setting")
198                 cmd_setting(os, ctx);
199         else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
200                 cmd_banunban(os, ctx);
201         else if(ctx->parms[0] == L"me")
202                 cmd_me(os, ctx);
203         else if(ctx->parms[0] == L"clearobjects")
204                 cmd_clearobjects(os, ctx);
205         else
206                 os<<L"-!- Invalid command: " + ctx->parms[0];
207         
208         return os.str();
209 }
210
211