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 "threading/mutex_auto_lock.h"
25 #include "util/strfnd.h"
26 #include "util/string.h"
30 BanManager::BanManager(const std::string &banfilepath):
31 m_banfilepath(banfilepath)
35 } catch(SerializationError &e) {
36 warningstream<<"BanManager: creating "
37 <<m_banfilepath<<std::endl;
41 BanManager::~BanManager()
46 void BanManager::load()
48 MutexAutoLock lock(m_mutex);
49 infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
50 std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
52 infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
53 throw SerializationError("BanManager::load(): Couldn't open file");
56 while (!is.eof() && is.good()) {
58 std::getline(is, line, '\n');
60 std::string ip = trim(f.next("|"));
61 std::string name = trim(f.next("|"));
69 void BanManager::save()
71 MutexAutoLock lock(m_mutex);
72 infostream << "BanManager: saving to " << m_banfilepath << std::endl;
73 std::ostringstream ss(std::ios_base::binary);
75 for (const auto &ip : m_ips)
76 ss << ip.first << "|" << ip.second << "\n";
78 if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
79 infostream << "BanManager: failed saving to " << m_banfilepath << std::endl;
80 throw SerializationError("BanManager::save(): Couldn't write file");
86 bool BanManager::isIpBanned(const std::string &ip)
88 MutexAutoLock lock(m_mutex);
89 return m_ips.find(ip) != m_ips.end();
92 std::string BanManager::getBanDescription(const std::string &ip_or_name)
94 MutexAutoLock lock(m_mutex);
96 for (const auto &ip : m_ips) {
97 if (ip.first == ip_or_name || ip.second == ip_or_name
98 || ip_or_name.empty()) {
99 s += ip.first + "|" + ip.second + ", ";
102 s = s.substr(0, s.size() - 2);
106 std::string BanManager::getBanName(const std::string &ip)
108 MutexAutoLock lock(m_mutex);
109 StringMap::iterator it = m_ips.find(ip);
110 if (it == m_ips.end())
115 void BanManager::add(const std::string &ip, const std::string &name)
117 MutexAutoLock lock(m_mutex);
122 void BanManager::remove(const std::string &ip_or_name)
124 MutexAutoLock lock(m_mutex);
125 for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
126 if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
136 bool BanManager::isModified()
138 MutexAutoLock lock(m_mutex);