3 Copyright (C) 2010-11 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
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.
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.
19 #include "servercommand.h"
22 #include "main.h" // For g_settings
23 #include "content_sao.h"
25 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
27 void cmd_status(std::wostringstream &os,
28 ServerCommandContext *ctx)
30 os<<ctx->server->getStatusString();
33 void cmd_me(std::wostringstream &os,
34 ServerCommandContext *ctx)
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;
41 void cmd_time(std::wostringstream &os,
42 ServerCommandContext *ctx)
44 if(ctx->parms.size() != 2)
46 os<<L"-!- Missing parameter";
50 if(!ctx->server->checkPriv(ctx->player->getName(), "settime"))
52 os<<L"-!- You don't have permission to do that";
56 u32 time = stoi(wide_to_narrow(ctx->parms[1]));
57 ctx->server->setTimeOfDay(time);
58 os<<L"-!- time_of_day changed.";
60 actionstream<<ctx->player->getName()<<" sets time "
64 void cmd_shutdown(std::wostringstream &os,
65 ServerCommandContext *ctx)
67 if(!ctx->server->checkPriv(ctx->player->getName(), "server"))
69 os<<L"-!- You don't have permission to do that";
73 actionstream<<ctx->player->getName()
74 <<" shuts down server"<<std::endl;
76 ctx->server->requestShutdown();
78 os<<L"*** Server shutting down (operator request)";
79 ctx->flags |= SEND_TO_OTHERS;
82 void cmd_setting(std::wostringstream &os,
83 ServerCommandContext *ctx)
85 if(!ctx->server->checkPriv(ctx->player->getName(), "server"))
87 os<<L"-!- You don't have permission to do that";
91 /*std::string confline = wide_to_narrow(
92 ctx->parms[1] + L" = " + ctx->params[2]);*/
94 std::string confline = wide_to_narrow(ctx->paramstring);
96 actionstream<<ctx->player->getName()
97 <<" sets: "<<confline<<std::endl;
99 g_settings->parseConfigLine(confline);
101 ctx->server->saveConfig();
103 os<< L"-!- Setting changed and configuration saved.";
106 void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
108 if(!ctx->server->checkPriv(ctx->player->getName(), "ban"))
110 os<<L"-!- You don't have permission to do that";
114 if(ctx->parms.size() < 2)
116 std::string desc = ctx->server->getBanDescription("");
117 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
120 if(ctx->parms[0] == L"ban")
122 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
126 os<<L"-!- No such player";
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());
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;
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);
150 actionstream<<ctx->player->getName()<<" unbans "
151 <<ip_or_name<<std::endl;
155 void cmd_clearobjects(std::wostringstream &os,
156 ServerCommandContext *ctx)
158 if(!ctx->server->checkPriv(ctx->player->getName(), "server"))
160 os<<L"-!- You don't have permission to do that";
164 actionstream<<ctx->player->getName()
165 <<" clears all objects"<<std::endl;
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());
173 ctx->server->notifyPlayers(msg);
176 ctx->env->clearAllObjects();
178 actionstream<<"object clearing done"<<std::endl;
180 os<<L"*** cleared all objects";
181 ctx->flags |= SEND_TO_OTHERS;
185 std::wstring processServerCommand(ServerCommandContext *ctx)
188 std::wostringstream os(std::ios_base::binary);
189 ctx->flags = SEND_TO_SENDER; // Default, unless we change it.
191 if(ctx->parms[0] == L"status")
193 else if(ctx->parms[0] == L"time")
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")
203 else if(ctx->parms[0] == L"clearobjects")
204 cmd_clearobjects(os, ctx);
206 os<<L"-!- Invalid command: " + ctx->parms[0];