Noise: Prevent unittest crash caused by division by zero
[oweals/minetest.git] / src / ban.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "ban.h"
21 #include <fstream>
22 #include "threading/mutex_auto_lock.h"
23 #include <sstream>
24 #include <set>
25 #include "util/strfnd.h"
26 #include "util/string.h"
27 #include "log.h"
28 #include "filesys.h"
29
30 BanManager::BanManager(const std::string &banfilepath):
31                 m_banfilepath(banfilepath)
32 {
33         try {
34                 load();
35         } catch(SerializationError &e) {
36                 warningstream<<"BanManager: creating "
37                                 <<m_banfilepath<<std::endl;
38         }
39 }
40
41 BanManager::~BanManager()
42 {
43         save();
44 }
45
46 void BanManager::load()
47 {
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);
51         if(is.good() == false)
52         {
53                 infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
54                 throw SerializationError("BanManager::load(): Couldn't open file");
55         }
56
57         while(!is.eof() && is.good())
58         {
59                 std::string line;
60                 std::getline(is, line, '\n');
61                 Strfnd f(line);
62                 std::string ip = trim(f.next("|"));
63                 std::string name = trim(f.next("|"));
64                 if(!ip.empty()) {
65                         m_ips[ip] = name;
66                 }
67         }
68         m_modified = false;
69 }
70
71 void BanManager::save()
72 {
73         MutexAutoLock lock(m_mutex);
74         infostream << "BanManager: saving to " << m_banfilepath << std::endl;
75         std::ostringstream ss(std::ios_base::binary);
76
77         for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it)
78                 ss << it->first << "|" << it->second << "\n";
79
80         if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
81                 infostream << "BanManager: failed saving to " << m_banfilepath << std::endl;
82                 throw SerializationError("BanManager::save(): Couldn't write file");
83         }
84
85         m_modified = false;
86 }
87
88 bool BanManager::isIpBanned(const std::string &ip)
89 {
90         MutexAutoLock lock(m_mutex);
91         return m_ips.find(ip) != m_ips.end();
92 }
93
94 std::string BanManager::getBanDescription(const std::string &ip_or_name)
95 {
96         MutexAutoLock lock(m_mutex);
97         std::string s = "";
98         for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it) {
99                 if (it->first  == ip_or_name || it->second == ip_or_name
100                                 || ip_or_name == "") {
101                         s += it->first + "|" + it->second + ", ";
102                 }
103         }
104         s = s.substr(0, s.size() - 2);
105         return s;
106 }
107
108 std::string BanManager::getBanName(const std::string &ip)
109 {
110         MutexAutoLock lock(m_mutex);
111         StringMap::iterator it = m_ips.find(ip);
112         if (it == m_ips.end())
113                 return "";
114         return it->second;
115 }
116
117 void BanManager::add(const std::string &ip, const std::string &name)
118 {
119         MutexAutoLock lock(m_mutex);
120         m_ips[ip] = name;
121         m_modified = true;
122 }
123
124 void BanManager::remove(const std::string &ip_or_name)
125 {
126         MutexAutoLock lock(m_mutex);
127         for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
128                 if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
129                         m_ips.erase(it++);
130                 } else {
131                         ++it;
132                 }
133         }
134         m_modified = true;
135 }
136
137
138 bool BanManager::isModified()
139 {
140         MutexAutoLock lock(m_mutex);
141         return m_modified;
142 }
143