3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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>
28 BanManager::BanManager(const std::string &banfilepath):
29 m_banfilepath(banfilepath),
36 catch(SerializationError &e)
38 infostream<<"WARNING: BanManager: creating "
39 <<m_banfilepath<<std::endl;
43 BanManager::~BanManager()
48 void BanManager::load()
50 JMutexAutoLock lock(m_mutex);
51 infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
52 std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
53 if(is.good() == false)
55 infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
56 throw SerializationError("BanManager::load(): Couldn't open file");
61 if(is.eof() || is.good() == false)
64 std::getline(is, line, '\n');
66 std::string ip = trim(f.next("|"));
67 std::string name = trim(f.next("|"));
75 void BanManager::save()
77 JMutexAutoLock lock(m_mutex);
78 infostream<<"BanManager: saving to "<<m_banfilepath<<std::endl;
79 std::ofstream os(m_banfilepath.c_str(), std::ios::binary);
81 if(os.good() == false)
83 infostream<<"BanManager: failed saving to "<<m_banfilepath<<std::endl;
84 throw SerializationError("BanManager::load(): Couldn't open file");
87 for(std::map<std::string, std::string>::iterator
89 i != m_ips.end(); i++)
91 os<<i->first<<"|"<<i->second<<"\n";
96 bool BanManager::isIpBanned(const std::string &ip)
98 JMutexAutoLock lock(m_mutex);
99 return m_ips.find(ip) != m_ips.end();
102 std::string BanManager::getBanDescription(const std::string &ip_or_name)
104 JMutexAutoLock lock(m_mutex);
106 for(std::map<std::string, std::string>::iterator
108 i != m_ips.end(); i++)
110 if(i->first == ip_or_name || i->second == ip_or_name
112 s += i->first + "|" + i->second + ", ";
114 s = s.substr(0, s.size()-2);
118 std::string BanManager::getBanName(const std::string &ip)
120 JMutexAutoLock lock(m_mutex);
121 std::map<std::string, std::string>::iterator i = m_ips.find(ip);
127 void BanManager::add(const std::string &ip, const std::string &name)
129 JMutexAutoLock lock(m_mutex);
134 void BanManager::remove(const std::string &ip_or_name)
136 JMutexAutoLock lock(m_mutex);
137 //m_ips.erase(m_ips.find(ip));
138 // Find out all ip-name pairs that match the ip or name
139 std::set<std::string> ips_to_delete;
140 for(std::map<std::string, std::string>::iterator
142 i != m_ips.end(); i++)
144 if(i->first == ip_or_name || i->second == ip_or_name)
145 ips_to_delete.insert(i->first);
148 for(std::set<std::string>::iterator
149 i = ips_to_delete.begin();
150 i != ips_to_delete.end(); i++)
158 bool BanManager::isModified()
160 JMutexAutoLock lock(m_mutex);