1 /*****************************************************************************
2  * PokerTH - The open source texas holdem engine                             *
3  * Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May          *
4  *                                                                           *
5  * This program is free software: you can redistribute it and/or modify      *
6  * it under the terms of the GNU Affero General Public License as            *
7  * published by the Free Software Foundation, either version 3 of the        *
8  * License, or (at your option) any later version.                           *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU Affero General Public License for more details.                       *
14  *                                                                           *
15  * You should have received a copy of the GNU Affero General Public License  *
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
17  *                                                                           *
18  *                                                                           *
19  * Additional permission under GNU AGPL version 3 section 7                  *
20  *                                                                           *
21  * If you modify this program, or any covered work, by linking or            *
22  * combining it with the OpenSSL project's OpenSSL library (or a             *
23  * modified version of that library), containing parts covered by the        *
24  * terms of the OpenSSL or SSLeay licenses, the authors of PokerTH           *
25  * (Felix Hammer, Florian Thauer, Lothar May) grant you additional           *
26  * permission to convey the resulting work.                                  *
27  * Corresponding Source for a non-source form of such a combination          *
28  * shall include the source code for the parts of OpenSSL used as well       *
29  * as that of the covered work.                                              *
30  *****************************************************************************/
31 /* Manager for server bans. */
32 
33 #ifndef _SERVERBANMANAGER_H_
34 #define _SERVERBANMANAGER_H_
35 
36 #include <db/dbdefs.h>
37 #include <boost/asio.hpp>
38 #include <boost/asio/steady_timer.hpp>
39 #include <boost/regex.hpp>
40 #include <boost/thread.hpp>
41 #include <boost/enable_shared_from_this.hpp>
42 #include <map>
43 #include <list>
44 #include <string>
45 
46 class ServerBanManager : public boost::enable_shared_from_this<ServerBanManager>
47 {
48 public:
49 	ServerBanManager(boost::shared_ptr<boost::asio::io_service> ioService);
50 	virtual ~ServerBanManager();
51 
52 	void SetAdminPlayerIds(const std::list<DB_id> &adminList);
53 
54 	void BanPlayerName(const std::string &playerName, unsigned durationHours = 0);
55 	void BanPlayerRegex(const std::string &playerRegex, unsigned durationHours = 0);
56 	void BanIPAddress(const std::string &ipAddress, unsigned durationHours);
57 	bool UnBan(unsigned banId);
58 	void GetBanList(std::list<std::string> &list) const;
59 	void ClearBanList();
60 
61 	bool IsAdminPlayer(DB_id playerId) const;
62 	bool IsPlayerBanned(const std::string &name) const;
63 	bool IsIPAddressBanned(const std::string &ipAddress) const;
64 
65 	void InitGameNameBadWordList(const std::list<std::string> &badWordList);
66 	bool IsBadGameName(const std::string &name) const;
67 
68 protected:
69 
70 	struct TimedPlayerBan {
71 		boost::shared_ptr<boost::asio::steady_timer> timer;
72 		std::string nameStr;
73 		boost::regex nameRegex;
74 	};
75 	struct TimedIPBan {
76 		boost::shared_ptr<boost::asio::steady_timer> timer;
77 		std::string ipAddress;
78 	};
79 
80 	typedef std::map<unsigned, TimedPlayerBan> RegexMap;
81 	typedef std::map<unsigned, TimedIPBan> IPAddressMap;
82 	typedef std::list<boost::regex> RegexList;
83 	typedef std::vector<DB_id> DBPlayerIdList;
84 
85 	boost::shared_ptr<boost::asio::steady_timer> InternalRegisterTimedBan(unsigned timerId, unsigned durationHours);
86 	void TimerRemoveBan(const boost::system::error_code &ec, unsigned banId, boost::shared_ptr<boost::asio::steady_timer> timer);
87 
88 	boost::shared_ptr<boost::asio::io_service> m_ioService;
89 
90 	unsigned GetNextBanId();
91 
92 private:
93 	RegexMap m_banPlayerNameMap;
94 	RegexList m_gameNameBadWordFilter;
95 	IPAddressMap m_banIPAddressMap;
96 	DBPlayerIdList m_adminPlayers;
97 	unsigned m_curBanId;
98 	mutable boost::mutex m_banMutex;
99 };
100 
101 #endif
102