]> git.lizzy.rs Git - dragonfireclient.git/blob - src/ban.cpp
Make Lint Happy
[dragonfireclient.git] / src / ban.cpp
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>
5
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.
10
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.
15
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.
19 */
20
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"
30
31 BanManager::BanManager(const std::string &banfilepath) : m_banfilepath(banfilepath)
32 {
33         try {
34                 load();
35         } catch (SerializationError &e) {
36                 infostream << "BanManager: creating " << m_banfilepath << std::endl;
37         }
38 }
39
40 BanManager::~BanManager()
41 {
42         save();
43 }
44
45 void BanManager::load()
46 {
47         MutexAutoLock lock(m_mutex);
48         infostream << "BanManager: loading from " << m_banfilepath << std::endl;
49         std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
50         if (!is.good()) {
51                 infostream << "BanManager: failed loading from " << m_banfilepath
52                            << std::endl;
53                 throw SerializationError("BanManager::load(): Couldn't open file");
54         }
55
56         while (!is.eof() && is.good()) {
57                 std::string line;
58                 std::getline(is, line, '\n');
59                 Strfnd f(line);
60                 std::string ip = trim(f.next("|"));
61                 std::string name = trim(f.next("|"));
62                 if (!ip.empty()) {
63                         m_ips[ip] = name;
64                 }
65         }
66         m_modified = false;
67 }
68
69 void BanManager::save()
70 {
71         MutexAutoLock lock(m_mutex);
72         infostream << "BanManager: saving to " << m_banfilepath << std::endl;
73         std::ostringstream ss(std::ios_base::binary);
74
75         for (const auto &ip : m_ips)
76                 ss << ip.first << "|" << ip.second << "\n";
77
78         if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
79                 infostream << "BanManager: failed saving to " << m_banfilepath
80                            << std::endl;
81                 throw SerializationError("BanManager::save(): Couldn't write file");
82         }
83
84         m_modified = false;
85 }
86
87 bool BanManager::isIpBanned(const std::string &ip)
88 {
89         MutexAutoLock lock(m_mutex);
90         return m_ips.find(ip) != m_ips.end();
91 }
92
93 std::string BanManager::getBanDescription(const std::string &ip_or_name)
94 {
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 + ", ";
101                 }
102         }
103         s = s.substr(0, s.size() - 2);
104         return s;
105 }
106
107 std::string BanManager::getBanName(const std::string &ip)
108 {
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;
114 }
115
116 void BanManager::add(const std::string &ip, const std::string &name)
117 {
118         MutexAutoLock lock(m_mutex);
119         m_ips[ip] = name;
120         m_modified = true;
121 }
122
123 void BanManager::remove(const std::string &ip_or_name)
124 {
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;
132                 }
133         }
134 }
135
136 bool BanManager::isModified()
137 {
138         MutexAutoLock lock(m_mutex);
139         return m_modified;
140 }