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