3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <jmutexautolock.h>
27 BanManager::BanManager(const std::string &banfilepath):
28 m_banfilepath(banfilepath),
35 catch(SerializationError &e)
37 dstream<<"WARNING: BanManager: creating "
38 <<m_banfilepath<<std::endl;
42 BanManager::~BanManager()
47 void BanManager::load()
49 JMutexAutoLock lock(m_mutex);
50 dstream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
51 std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
52 if(is.good() == false)
54 dstream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
55 throw SerializationError("BanManager::load(): Couldn't open file");
60 if(is.eof() || is.good() == false)
63 std::getline(is, ip, '\n');
69 void BanManager::save()
71 JMutexAutoLock lock(m_mutex);
72 dstream<<"BanManager: saving to "<<m_banfilepath<<std::endl;
73 std::ofstream os(m_banfilepath.c_str(), std::ios::binary);
75 if(os.good() == false)
77 dstream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
78 throw SerializationError("BanManager::load(): Couldn't open file");
81 for(std::set<std::string>::iterator
83 i != m_ips.end(); i++)
92 bool BanManager::isIpBanned(std::string ip)
94 JMutexAutoLock lock(m_mutex);
95 return m_ips.find(ip) != m_ips.end();
98 void BanManager::add(std::string ip)
100 JMutexAutoLock lock(m_mutex);
105 void BanManager::remove(std::string ip)
107 JMutexAutoLock lock(m_mutex);
108 m_ips.erase(m_ips.find(ip));
113 bool BanManager::isModified()
115 JMutexAutoLock lock(m_mutex);