From e59e50c10b165007cc3709e130f3cdf7e6559677 Mon Sep 17 00:00:00 2001 From: lotodore Date: Tue, 7 Apr 2009 22:04:38 +0000 Subject: [PATCH] Changes to allow regex bans. Might need updating of pro-files due to boost::regex dependency. --- src/net/common/serverlobbythread.cpp | 41 +++++++++++++++++++++++++++- src/net/serverlobbythread.h | 9 +++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 354e66ad..ab9bc6c2 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -257,6 +257,20 @@ ServerLobbyThread::KickPlayerByName(const std::string &playerName) return retVal; } +void +ServerLobbyThread::BanPlayerRegex(const std::string &playerRegex) +{ + boost::mutex::scoped_lock lock(m_banPlayerNameListMutex); + m_banPlayerNameList.push_back(boost::regex(playerRegex)); +} + +void +ServerLobbyThread::ClearBanList() +{ + boost::mutex::scoped_lock lock(m_banPlayerNameListMutex); + m_banPlayerNameList.clear(); +} + void ServerLobbyThread::RemovePlayer(unsigned playerId, unsigned errorCode) { @@ -511,6 +525,13 @@ ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const NetPacketIn return; } + // Check whether the player name is banned. + if (IsPlayerBanned(initData.playerName)) + { + SessionError(session, ERR_NET_PLAYER_BANNED); + return; + } + // Create player data object. boost::shared_ptr tmpPlayerData( new PlayerData(GetNextUniquePlayerId(), 0, PLAYER_TYPE_HUMAN, PLAYER_RIGHTS_NORMAL)); @@ -1344,7 +1365,7 @@ ServerLobbyThread::GetGui() } bool -ServerLobbyThread::IsPlayerConnected(const string &name) +ServerLobbyThread::IsPlayerConnected(const string &name) const { bool retVal = false; @@ -1356,6 +1377,24 @@ ServerLobbyThread::IsPlayerConnected(const string &name) return retVal; } +bool +ServerLobbyThread::IsPlayerBanned(const std::string &name) const +{ + bool retVal = false; + boost::mutex::scoped_lock lock(m_banPlayerNameListMutex); + RegExList::const_iterator i = m_banPlayerNameList.begin(); + RegExList::const_iterator end = m_banPlayerNameList.end(); + while (i != end) + { + if (regex_match(name, *i)) + { + retVal = true; + break; + } + ++i; + } +} + boost::shared_ptr ServerLobbyThread::CreateNetPacketGameListNew(const ServerGameThread &game) { diff --git a/src/net/serverlobbythread.h b/src/net/serverlobbythread.h index 3a081dbc..8ac52e35 100644 --- a/src/net/serverlobbythread.h +++ b/src/net/serverlobbythread.h @@ -29,6 +29,7 @@ #include #include +#include #include #define NET_LOBBY_THREAD_TERMINATE_TIMEOUT_MSEC 20000 @@ -68,6 +69,8 @@ public: void HandleGameRetrieveAvatar(SessionWrapper session, const NetPacketRetrieveAvatar &tmpPacket); bool KickPlayerByName(const std::string &playerName); + void BanPlayerRegex(const std::string &playerRegex); + void ClearBanList(); void RemovePlayer(unsigned playerId, unsigned errorCode); void SendGlobalChat(const std::string &message); @@ -156,7 +159,8 @@ protected: ServerSenderCallback &GetSenderCallback(); GuiInterface &GetGui(); - bool IsPlayerConnected(const std::string &name); + bool IsPlayerConnected(const std::string &name) const; + bool IsPlayerBanned(const std::string &name) const; static boost::shared_ptr CreateNetPacketGameListNew(const ServerGameThread &game); static boost::shared_ptr CreateNetPacketGameListUpdate(unsigned gameId, GameMode mode); @@ -187,6 +191,9 @@ private: SessionIdList m_resubscribeList; mutable boost::mutex m_resubscribeListMutex; + RegExList m_banPlayerNameList; + mutable boost::mutex m_banPlayerNameListMutex; + GameMap m_gameMap; boost::shared_ptr m_sender;