Skip to content

Commit

Permalink
ban + unban + whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
fogWraith committed Nov 28, 2010
1 parent 8f61149 commit ca7f8c9
Showing 1 changed file with 70 additions and 27 deletions.
97 changes: 70 additions & 27 deletions src/commands.cpp
Expand Up @@ -110,7 +110,7 @@ void home(User *user, std::string command, std::deque<std::string> args)

void kit(User *user, std::string command, std::deque<std::string> args)
{
if(args.size() == 1)
if(!args.empty())
{
std::vector<int> kitItems = Conf::get().vValue("kit_" + args[0]);
// If kit is found
Expand All @@ -132,8 +132,6 @@ void kit(User *user, std::string command, std::deque<std::string> args)
else
reportError(user, "Kit " + args[0] + " not found");
}
else
reportError(user, "Usage: /kit name");
}

void saveMap(User *user, std::string command, std::deque<std::string> args)
Expand All @@ -143,6 +141,65 @@ void saveMap(User *user, std::string command, std::deque<std::string> args)
Chat::USER);
}

void ban(User *user, std::string command, std::deque<std::string> args)
{
std::string victim = args[0];
User *tUser = getUserByNick(victim);

std::fstream bannedf;
bannedf.open("banned.txt",std::fstream::app);
bannedf << victim << std::endl;
bannedf.close();

if(tUser != NULL)
{
args.pop_front();
std::string kickMsg;
if(args.empty())
kickMsg = Conf::get().sValue("default_banned_message");
else
{
while(!args.empty())
{
kickMsg += args[0] + " ";
args.pop_front();
}
}

tUser->kick(kickMsg);
}
else
Chat::get().sendMsg(user, COLOR_DARK_MAGENTA + victim +" was banned in his absence!", Chat::USER);

// Reload list with banned users
Chat::get().loadBanned(BANNEDFILE);
}

void unban(User *user, std::string command, std::deque<std::string> args)
{
std::string victim = args[0];
User *tUser = getUserByNick(victim);

std::string line;
std::ifstream in("banned.txt");

std::ofstream out("banned.tmp");

while( getline(in,line) )
{
if(line != victim)
out << line << "\n";
}
in.close();
out.close();

remove("banned.txt");
rename("banned.tmp","banned.txt");

// Reload list with banned users
Chat::get().loadBanned(BANNEDFILE);
}

void kick(User *user, std::string command, std::deque<std::string> args)
{
if(!args.empty())
Expand Down Expand Up @@ -190,28 +247,16 @@ void setTime(User *user, std::string command, std::deque<std::string> args)
reportError(user, "Usage: /settime time (time = 0-24000)");
}

void setHealth(User *user, std::string command, std::deque<std::string> args)
{
if(args.size() == 2)
{
user->sethealth(atoi(args[1].c_str()));
}
else
reportError(user, "Usage: /sethealth [player] health (health = 0-20)");
}

void coordinateTeleport(User *user, std::string command, std::deque<std::string> args)
{
if(args.size() == 3)
if(args.size() > 2)
{
LOG(user->nick + " teleport to: " + args[0] + " " + args[1] + " " + args[2]);
double x = atof(args[0].c_str());
double y = atof(args[1].c_str());
double z = atof(args[2].c_str());
user->teleport(x, y, z);
}
else
reportError(user, "Usage: /ctp x y z");
}

void userTeleport(User *user, std::string command, std::deque<std::string> args)
Expand Down Expand Up @@ -243,8 +288,6 @@ void userTeleport(User *user, std::string command, std::deque<std::string> args)
" not found (see /players");
}
}
else
reportError(user, "Usage: /tp [player] targetplayer");
}

void showPosition(User *user, std::string command, std::deque<std::string> args)
Expand All @@ -261,16 +304,14 @@ void showPosition(User *user, std::string command, std::deque<std::string> args)
else
reportError(user, "User " + args[0] + " not found (see /players)");
}
else if(args.size() == 0)
else
{
Chat::get().sendMsg(user, COLOR_MAGENTA + "You are at: " + dtos(user->pos.x)
+ " "
+ dtos(user->pos.y)
+ " "
+ dtos(user->pos.z), Chat::USER);
}
else
reportError(user, "Usage: /gps [player]");
}

void regenerateLighting(User *user, std::string command, std::deque<std::string> args)
Expand All @@ -287,6 +328,8 @@ void regenerateLighting(User *user, std::string command, std::deque<std::string>
void reloadConfiguration(User *user, std::string command, std::deque<std::string> args)
{
Chat::get().loadAdmins(ADMINFILE);
Chat::get().loadBanned(BANNEDFILE);
Chat::get().loadWhitelist(WHITELISTFILE);
Conf::get().load(CONFIGFILE);

// Set physics enable state based on config
Expand Down Expand Up @@ -331,7 +374,7 @@ void giveItems(User *user, std::string command, std::deque<std::string> args)
User *tUser = NULL;
int itemId = 0, itemCount = 1, itemStacks = 1;

if(args.size() == 2 || args.size() == 3)
if(args.size() > 1)
{
tUser = getUserByNick(args[0]);

Expand All @@ -349,7 +392,7 @@ void giveItems(User *user, std::string command, std::deque<std::string> args)
return;
}

if(args.size() == 3)
if(args.size() > 2)
{
itemCount = atoi(args[2].c_str());
// If multiple stacks
Expand All @@ -359,7 +402,7 @@ void giveItems(User *user, std::string command, std::deque<std::string> args)
}
else
{
reportError(user, "Usage: /give player item [count]");
reportError(user, "Too few parameters.");
return;
}

Expand Down Expand Up @@ -396,6 +439,8 @@ void Chat::registerStandardCommands()
registerCommand("about", about, false);
registerCommand("rules", rules, false);
registerCommand("home", home, false);
registerCommand("ban", ban, true);
registerCommand("unban", unban, true);
registerCommand("kit", kit, false);
registerCommand("save", saveMap, true);
registerCommand("kick", kick, true);
Expand All @@ -404,7 +449,5 @@ void Chat::registerStandardCommands()
registerCommand("reload", reloadConfiguration, true);
registerCommand("give", giveItems, true);
registerCommand("gps", showPosition, true);
registerCommand("settime", setTime, true);
registerCommand("regen", regenerateLighting, true);
registerCommand("sethealth", setHealth, true);
registerCommand("settime", setTime, true); registerCommand("regen", regenerateLighting, true);
}

0 comments on commit ca7f8c9

Please sign in to comment.