Adding support for listing and removing single bans.

This commit is contained in:
lotodore
2009-04-10 21:04:08 +00:00
parent bf8b5b84a6
commit 69f3b49ab2
3 changed files with 161 additions and 101 deletions
+41 -11
View File
@@ -29,6 +29,7 @@
#include <core/openssl_wrapper.h> #include <core/openssl_wrapper.h>
#include <fstream> #include <fstream>
#include <sstream>
#include <algorithm> #include <algorithm>
#include <boost/lambda/lambda.hpp> #include <boost/lambda/lambda.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
@@ -80,7 +81,7 @@ private:
ServerLobbyThread::ServerLobbyThread(GuiInterface &gui, ConfigFile *playerConfig, AvatarManager &avatarManager) ServerLobbyThread::ServerLobbyThread(GuiInterface &gui, ConfigFile *playerConfig, AvatarManager &avatarManager)
: m_gui(gui), m_avatarManager(avatarManager), m_playerConfig(playerConfig), : m_curBanId(0), m_gui(gui), m_avatarManager(avatarManager), m_playerConfig(playerConfig),
m_curGameId(0), m_curUniquePlayerId(0), m_curSessionId(INVALID_SESSION + 1), m_curGameId(0), m_curUniquePlayerId(0), m_curSessionId(INVALID_SESSION + 1),
m_statDataChanged(false), m_startTime(boost::posix_time::second_clock::local_time()) m_statDataChanged(false), m_startTime(boost::posix_time::second_clock::local_time())
{ {
@@ -258,17 +259,46 @@ ServerLobbyThread::KickPlayerByName(const std::string &playerName)
} }
void void
ServerLobbyThread::BanPlayerRegex(const std::string &playerRegex) ServerLobbyThread::BanPlayerRegex(const string &playerRegex)
{ {
boost::mutex::scoped_lock lock(m_banPlayerNameListMutex); boost::mutex::scoped_lock lock(m_banMutex);
m_banPlayerNameList.push_back(boost::regex(playerRegex, boost::regex_constants::no_except)); m_banPlayerNameMap[++m_curBanId] = boost::regex(playerRegex, boost::regex_constants::no_except);
}
bool
ServerLobbyThread::UnBanPlayerRegex(unsigned banId)
{
bool retVal = false;
boost::mutex::scoped_lock lock(m_banMutex);
RegexMap::iterator pos = m_banPlayerNameMap.find(banId);
if (pos != m_banPlayerNameMap.end())
{
m_banPlayerNameMap.erase(pos);
retVal = true;
}
return retVal;
} }
void void
ServerLobbyThread::ClearBanList() ServerLobbyThread::GetBanPlayerList(list<string> &list) const
{ {
boost::mutex::scoped_lock lock(m_banPlayerNameListMutex); boost::mutex::scoped_lock lock(m_banMutex);
m_banPlayerNameList.clear(); RegexMap::const_iterator i = m_banPlayerNameMap.begin();
RegexMap::const_iterator end = m_banPlayerNameMap.end();
while (i != end)
{
ostringstream banText;
banText << (*i).first << ": " << (*i).second.str();
list.push_back(banText.str());
++i;
}
}
void
ServerLobbyThread::ClearBanPlayerList()
{
boost::mutex::scoped_lock lock(m_banMutex);
m_banPlayerNameMap.clear();
} }
void void
@@ -1381,12 +1411,12 @@ bool
ServerLobbyThread::IsPlayerBanned(const std::string &name) const ServerLobbyThread::IsPlayerBanned(const std::string &name) const
{ {
bool retVal = false; bool retVal = false;
boost::mutex::scoped_lock lock(m_banPlayerNameListMutex); boost::mutex::scoped_lock lock(m_banMutex);
RegexList::const_iterator i = m_banPlayerNameList.begin(); RegexMap::const_iterator i = m_banPlayerNameMap.begin();
RegexList::const_iterator end = m_banPlayerNameList.end(); RegexMap::const_iterator end = m_banPlayerNameMap.end();
while (i != end) while (i != end)
{ {
if (regex_match(name, *i)) if (regex_match(name, (*i).second))
{ {
retVal = true; retVal = true;
break; break;
+113 -86
View File
@@ -90,107 +90,134 @@ ServerManager::SignalIrcChatMsg(const std::string &nickName, const std::string &
{ {
if (m_ircThread) if (m_ircThread)
{ {
istringstream msgStream(msg); try
string target;
msgStream >> target;
if (boost::algorithm::iequals(target, m_ircNick + ":"))
{ {
string command; istringstream msgStream(msg);
msgStream >> command; string target;
if (command == "kick") msgStream >> target;
if (boost::algorithm::iequals(target, m_ircNick + ":"))
{ {
while (msgStream.peek() == ' ') string command;
msgStream.get(); msgStream >> command;
string playerName(msgStream.str().substr(msgStream.tellg())); if (command == "kick")
if (!playerName.empty())
{ {
if (GetLobbyThread().KickPlayerByName(playerName)) while (msgStream.peek() == ' ')
m_ircThread->SendChatMessage(nickName + ": Successfully kicked player \"" + playerName + "\" from the server."); msgStream.get();
string playerName(msgStream.str().substr(msgStream.tellg()));
if (!playerName.empty())
{
if (GetLobbyThread().KickPlayerByName(playerName))
m_ircThread->SendChatMessage(nickName + ": Successfully kicked player \"" + playerName + "\" from the server.");
else
m_ircThread->SendChatMessage(nickName + ": Player \"" + playerName + "\" was not found on the server.");
}
}
else if (command == "nickban")
{
while (msgStream.peek() == ' ')
msgStream.get();
string playerRegex(msgStream.str().substr(msgStream.tellg()));
if (!playerRegex.empty())
{
GetLobbyThread().BanPlayerRegex(playerRegex);
m_ircThread->SendChatMessage(nickName + ": The regex \"" + playerRegex + "\" was added to the player ban list.");
}
}
else if (command == "listnickban")
{
list<string> banList;
GetLobbyThread().GetBanPlayerList(banList);
list<string>::const_iterator i = banList.begin();
list<string>::const_iterator end = banList.end();
while (i != end)
{
m_ircThread->SendChatMessage(*i);
++i;
}
}
else if (command == "removenickban")
{
unsigned banId = 0;
msgStream >> banId;
if (GetLobbyThread().UnBanPlayerRegex(banId))
m_ircThread->SendChatMessage(nickName + ": The nick ban was successfully removed.");
else else
m_ircThread->SendChatMessage(nickName + ": Player \"" + playerName + "\" was not found on the server."); m_ircThread->SendChatMessage(nickName + ": This nick ban does not exist.");
} }
} else if (command == "clearnickban")
else if (command == "ban")
{
while (msgStream.peek() == ' ')
msgStream.get();
string playerRegex(msgStream.str().substr(msgStream.tellg()));
if (!playerRegex.empty())
{ {
GetLobbyThread().BanPlayerRegex(playerRegex); GetLobbyThread().ClearBanPlayerList();
m_ircThread->SendChatMessage(nickName + ": The regex \"" + playerRegex + "\" was added to the player ban list."); m_ircThread->SendChatMessage(nickName + ": The player ban list was cleared.");
} }
} else if (command == "stat")
else if (command == "clearban")
{
GetLobbyThread().ClearBanList();
m_ircThread->SendChatMessage(nickName + ": The player ban list was cleared.");
}
else if (command == "stat")
{
ServerStats tmpStats = GetLobbyThread().GetStats();
{ {
boost::posix_time::time_duration timeDiff(boost::posix_time::second_clock::local_time() - GetLobbyThread().GetStartTime()); ServerStats tmpStats = GetLobbyThread().GetStats();
ostringstream statStream; {
statStream boost::posix_time::time_duration timeDiff(boost::posix_time::second_clock::local_time() - GetLobbyThread().GetStartTime());
<< "Server uptime................ " << timeDiff.hours() / 24 << " days " << timeDiff.hours() % 24 << " hours " << timeDiff.minutes() << " minutes " << timeDiff.seconds() << " seconds"; ostringstream statStream;
m_ircThread->SendChatMessage(statStream.str()); statStream
<< "Server uptime................ " << timeDiff.hours() / 24 << " days " << timeDiff.hours() % 24 << " hours " << timeDiff.minutes() << " minutes " << timeDiff.seconds() << " seconds";
m_ircThread->SendChatMessage(statStream.str());
}
{
ostringstream statStream;
statStream
<< "Players currently on Server.. " << tmpStats.numberOfPlayersOnServer;
m_ircThread->SendChatMessage(statStream.str());
}
{
ostringstream statStream;
statStream
<< "Games currently open......... " << tmpStats.numberOfGamesOpen;
m_ircThread->SendChatMessage(statStream.str());
}
{
ostringstream statStream;
statStream
<< "Total players ever logged in. " << tmpStats.totalPlayersEverLoggedIn
<< " (Max at a time: " << tmpStats.maxPlayersLoggedIn << ")";
m_ircThread->SendChatMessage(statStream.str());
}
{
ostringstream statStream;
statStream
<< "Total games ever open........ " << tmpStats.totalGamesEverCreated
<< " (Max at a time: " << tmpStats.maxGamesOpen << ")";
m_ircThread->SendChatMessage(statStream.str());
}
} }
else if (command == "chat")
{ {
ostringstream statStream; while (msgStream.peek() == ' ')
statStream msgStream.get();
<< "Players currently on Server.. " << tmpStats.numberOfPlayersOnServer; string chat(msgStream.str().substr(msgStream.tellg()));
m_ircThread->SendChatMessage(statStream.str()); if (!chat.empty() && chat.size() < MAX_CHAT_TEXT_SIZE)
{
GetLobbyThread().SendGlobalChat(chat);
m_ircThread->SendChatMessage(nickName + ": Global chat message sent.");
}
else
m_ircThread->SendChatMessage(nickName + ": Invalid message.");
} }
else if (command == "msg")
{ {
ostringstream statStream; while (msgStream.peek() == ' ')
statStream msgStream.get();
<< "Games currently open......... " << tmpStats.numberOfGamesOpen; string message(msgStream.str().substr(msgStream.tellg()));
m_ircThread->SendChatMessage(statStream.str()); if (!message.empty() && message.size() < MAX_CHAT_TEXT_SIZE)
} {
{ GetLobbyThread().SendGlobalMsgBox(message);
ostringstream statStream; m_ircThread->SendChatMessage(nickName + ": Global message box sent.");
statStream }
<< "Total players ever logged in. " << tmpStats.totalPlayersEverLoggedIn else
<< " (Max at a time: " << tmpStats.maxPlayersLoggedIn << ")"; m_ircThread->SendChatMessage(nickName + ": Invalid message.");
m_ircThread->SendChatMessage(statStream.str());
}
{
ostringstream statStream;
statStream
<< "Total games ever open........ " << tmpStats.totalGamesEverCreated
<< " (Max at a time: " << tmpStats.maxGamesOpen << ")";
m_ircThread->SendChatMessage(statStream.str());
}
}
else if (command == "chat")
{
while (msgStream.peek() == ' ')
msgStream.get();
string chat(msgStream.str().substr(msgStream.tellg()));
if (!chat.empty() && chat.size() < MAX_CHAT_TEXT_SIZE)
{
GetLobbyThread().SendGlobalChat(chat);
m_ircThread->SendChatMessage(nickName + ": Global chat message sent.");
} }
else else
m_ircThread->SendChatMessage(nickName + ": Invalid message."); m_ircThread->SendChatMessage(nickName + ": Invalid command \"" + command + "\".");
} }
else if (command == "msg") } catch (...)
{ {
while (msgStream.peek() == ' ') m_ircThread->SendChatMessage(nickName + ": Syntax error. Please check the command.");
msgStream.get();
string message(msgStream.str().substr(msgStream.tellg()));
if (!message.empty() && message.size() < MAX_CHAT_TEXT_SIZE)
{
GetLobbyThread().SendGlobalMsgBox(message);
m_ircThread->SendChatMessage(nickName + ": Global message box sent.");
}
else
m_ircThread->SendChatMessage(nickName + ": Invalid message.");
}
else
m_ircThread->SendChatMessage(nickName + ": Invalid command \"" + command + "\".");
} }
} }
} }
+7 -4
View File
@@ -70,7 +70,9 @@ public:
bool KickPlayerByName(const std::string &playerName); bool KickPlayerByName(const std::string &playerName);
void BanPlayerRegex(const std::string &playerRegex); void BanPlayerRegex(const std::string &playerRegex);
void ClearBanList(); bool UnBanPlayerRegex(unsigned banId);
void GetBanPlayerList(std::list<std::string> &list) const;
void ClearBanPlayerList();
void RemovePlayer(unsigned playerId, unsigned errorCode); void RemovePlayer(unsigned playerId, unsigned errorCode);
void SendGlobalChat(const std::string &message); void SendGlobalChat(const std::string &message);
@@ -102,7 +104,7 @@ protected:
typedef std::map<unsigned, boost::shared_ptr<ServerGameThread> > GameMap; typedef std::map<unsigned, boost::shared_ptr<ServerGameThread> > GameMap;
typedef std::map<std::string, boost::timers::portable::microsec_timer> TimerClientAddressMap; typedef std::map<std::string, boost::timers::portable::microsec_timer> TimerClientAddressMap;
typedef std::list<unsigned> RemoveGameList; typedef std::list<unsigned> RemoveGameList;
typedef std::list<boost::regex> RegexList; typedef std::map<unsigned, boost::regex> RegexMap;
// Main function of the thread. // Main function of the thread.
virtual void Main(); virtual void Main();
@@ -192,8 +194,9 @@ private:
SessionIdList m_resubscribeList; SessionIdList m_resubscribeList;
mutable boost::mutex m_resubscribeListMutex; mutable boost::mutex m_resubscribeListMutex;
RegexList m_banPlayerNameList; RegexMap m_banPlayerNameMap;
mutable boost::mutex m_banPlayerNameListMutex; unsigned m_curBanId;
mutable boost::mutex m_banMutex;
GameMap m_gameMap; GameMap m_gameMap;