Fix sound direction and add experimental:soundblock alias sb in minimal for testing
[oweals/minetest.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_banunban(std::wostringstream &os, ServerCommandContext *ctx)
83 {
84         if(!ctx->server->checkPriv(ctx->player->getName(), "ban"))
85         {
86                 os<<L"-!- You don't have permission to do that";
87                 return;
88         }
89
90         if(ctx->parms.size() < 2)
91         {
92                 std::string desc = ctx->server->getBanDescription("");
93                 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
94                 return;
95         }
96         if(ctx->parms[0] == L"ban")
97         {
98                 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
99
100                 if(player == NULL)
101                 {
102                         os<<L"-!- No such player";
103                         return;
104                 }
105                 
106                 try{
107                         Address address = ctx->server->getPeerAddress(player->peer_id);
108                         std::string ip_string = address.serializeString();
109                         ctx->server->setIpBanned(ip_string, player->getName());
110                         os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
111                                         <<narrow_to_wide(player->getName());
112
113                         actionstream<<ctx->player->getName()<<" bans "
114                                         <<player->getName()<<" / "<<ip_string<<std::endl;
115                 } catch(con::PeerNotFoundException){
116                         dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
117                 }
118         }
119         else
120         {
121                 std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
122                 std::string desc = ctx->server->getBanDescription(ip_or_name);
123                 ctx->server->unsetIpBanned(ip_or_name);
124                 os<<L"-!- Unbanned "<<narrow_to_wide(desc);
125
126                 actionstream<<ctx->player->getName()<<" unbans "
127                                 <<ip_or_name<<std::endl;
128         }
129 }
130
131 void cmd_clearobjects(std::wostringstream &os,
132         ServerCommandContext *ctx)
133 {
134         if(!ctx->server->checkPriv(ctx->player->getName(), "server"))
135         {
136                 os<<L"-!- You don't have permission to do that";
137                 return;
138         }
139
140         actionstream<<ctx->player->getName()
141                         <<" clears all objects"<<std::endl;
142         
143         {
144                 std::wstring msg;
145                 msg += L"Clearing all objects. This may take long.";
146                 msg += L" You may experience a timeout. (by ";
147                 msg += narrow_to_wide(ctx->player->getName());
148                 msg += L")";
149                 ctx->server->notifyPlayers(msg);
150         }
151
152         ctx->env->clearAllObjects();
153                                         
154         actionstream<<"object clearing done"<<std::endl;
155         
156         os<<L"*** cleared all objects";
157         ctx->flags |= SEND_TO_OTHERS;
158 }
159
160
161 std::wstring processServerCommand(ServerCommandContext *ctx)
162 {
163
164         std::wostringstream os(std::ios_base::binary);
165         ctx->flags = SEND_TO_SENDER;    // Default, unless we change it.
166
167         if(ctx->parms[0] == L"status")
168                 cmd_status(os, ctx);
169         else if(ctx->parms[0] == L"time")
170                 cmd_time(os, ctx);
171         else if(ctx->parms[0] == L"shutdown")
172                 cmd_shutdown(os, ctx);
173         else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
174                 cmd_banunban(os, ctx);
175         else if(ctx->parms[0] == L"me")
176                 cmd_me(os, ctx);
177         else if(ctx->parms[0] == L"clearobjects")
178                 cmd_clearobjects(os, ctx);
179         else
180                 os<<L"-!- Invalid command: " + ctx->parms[0];
181         
182         return os.str();
183 }
184
185