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_banunban(std::wostringstream &os, ServerCommandContext *ctx)
84 if(!ctx->server->checkPriv(ctx->player->getName(), "ban"))
86 os<<L"-!- You don't have permission to do that";
90 if(ctx->parms.size() < 2)
92 std::string desc = ctx->server->getBanDescription("");
93 os<<L"-!- Ban list: "<<narrow_to_wide(desc);
96 if(ctx->parms[0] == L"ban")
98 Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
102 os<<L"-!- No such player";
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());
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;
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);
126 actionstream<<ctx->player->getName()<<" unbans "
127 <<ip_or_name<<std::endl;
131 void cmd_clearobjects(std::wostringstream &os,
132 ServerCommandContext *ctx)
134 if(!ctx->server->checkPriv(ctx->player->getName(), "server"))
136 os<<L"-!- You don't have permission to do that";
140 actionstream<<ctx->player->getName()
141 <<" clears all objects"<<std::endl;
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());
149 ctx->server->notifyPlayers(msg);
152 ctx->env->clearAllObjects();
154 actionstream<<"object clearing done"<<std::endl;
156 os<<L"*** cleared all objects";
157 ctx->flags |= SEND_TO_OTHERS;
161 std::wstring processServerCommand(ServerCommandContext *ctx)
164 std::wostringstream os(std::ios_base::binary);
165 ctx->flags = SEND_TO_SENDER; // Default, unless we change it.
167 if(ctx->parms[0] == L"status")
169 else if(ctx->parms[0] == L"time")
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")
177 else if(ctx->parms[0] == L"clearobjects")
178 cmd_clearobjects(os, ctx);
180 os<<L"-!- Invalid command: " + ctx->parms[0];