clientobject: add null checks
[waspsaliva.git] / src / ban.cpp
blob3decc9666117274c4a7e21d490b4ce567edca655
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "ban.h"
22 #include <fstream>
23 #include "threading/mutex_auto_lock.h"
24 #include <sstream>
25 #include <set>
26 #include "util/strfnd.h"
27 #include "util/string.h"
28 #include "log.h"
29 #include "filesys.h"
31 BanManager::BanManager(const std::string &banfilepath):
32 m_banfilepath(banfilepath)
34 try {
35 load();
36 } catch(SerializationError &e) {
37 infostream << "BanManager: creating "
38 << m_banfilepath << std::endl;
42 BanManager::~BanManager()
44 save();
47 void BanManager::load()
49 MutexAutoLock lock(m_mutex);
50 infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
51 std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
52 if (!is.good()) {
53 infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
54 throw SerializationError("BanManager::load(): Couldn't open file");
57 while (!is.eof() && is.good()) {
58 std::string line;
59 std::getline(is, line, '\n');
60 Strfnd f(line);
61 std::string ip = trim(f.next("|"));
62 std::string name = trim(f.next("|"));
63 if(!ip.empty()) {
64 m_ips[ip] = name;
67 m_modified = false;
70 void BanManager::save()
72 MutexAutoLock lock(m_mutex);
73 infostream << "BanManager: saving to " << m_banfilepath << std::endl;
74 std::ostringstream ss(std::ios_base::binary);
76 for (const auto &ip : m_ips)
77 ss << ip.first << "|" << ip.second << "\n";
79 if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
80 infostream << "BanManager: failed saving to " << m_banfilepath << std::endl;
81 throw SerializationError("BanManager::save(): Couldn't write file");
84 m_modified = false;
87 bool BanManager::isIpBanned(const std::string &ip)
89 MutexAutoLock lock(m_mutex);
90 return m_ips.find(ip) != m_ips.end();
93 std::string BanManager::getBanDescription(const std::string &ip_or_name)
95 MutexAutoLock lock(m_mutex);
96 std::string s;
97 for (const auto &ip : m_ips) {
98 if (ip.first == ip_or_name || ip.second == ip_or_name
99 || ip_or_name.empty()) {
100 s += ip.first + "|" + ip.second + ", ";
103 s = s.substr(0, s.size() - 2);
104 return s;
107 std::string BanManager::getBanName(const std::string &ip)
109 MutexAutoLock lock(m_mutex);
110 StringMap::iterator it = m_ips.find(ip);
111 if (it == m_ips.end())
112 return "";
113 return it->second;
116 void BanManager::add(const std::string &ip, const std::string &name)
118 MutexAutoLock lock(m_mutex);
119 m_ips[ip] = name;
120 m_modified = true;
123 void BanManager::remove(const std::string &ip_or_name)
125 MutexAutoLock lock(m_mutex);
126 for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
127 if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
128 m_ips.erase(it++);
129 m_modified = true;
130 } else {
131 ++it;
137 bool BanManager::isModified()
139 MutexAutoLock lock(m_mutex);
140 return m_modified;