]> git.lizzy.rs Git - minetest.git/blob - src/ban.cpp
Add setting for tooltips show delay.
[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 "jthread/jmutexautolock.h"
23 #include <sstream>
24 #include <set>
25 #include "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                 m_modified(false)
33 {
34         try{
35                 load();
36         }
37         catch(SerializationError &e)
38         {
39                 infostream<<"WARNING: BanManager: creating "
40                                 <<m_banfilepath<<std::endl;
41         }
42 }
43
44 BanManager::~BanManager()
45 {
46         save();
47 }
48
49 void BanManager::load()
50 {
51         JMutexAutoLock lock(m_mutex);
52         infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
53         std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
54         if(is.good() == false)
55         {
56                 infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
57                 throw SerializationError("BanManager::load(): Couldn't open file");
58         }
59         
60         for(;;)
61         {
62                 if(is.eof() || is.good() == false)
63                         break;
64                 std::string line;
65                 std::getline(is, line, '\n');
66                 Strfnd f(line);
67                 std::string ip = trim(f.next("|"));
68                 std::string name = trim(f.next("|"));
69                 if(ip.empty())
70                         continue;
71                 m_ips[ip] = name;
72         }
73         m_modified = false;
74 }
75
76 void BanManager::save()
77 {
78         JMutexAutoLock lock(m_mutex);
79         infostream<<"BanManager: saving to "<<m_banfilepath<<std::endl;
80         std::ostringstream ss(std::ios_base::binary);
81
82         for(std::map<std::string, std::string>::iterator
83                         i = m_ips.begin();
84                         i != m_ips.end(); i++)
85         {
86                 ss << i->first << "|" << i->second << "\n";
87         }
88
89         if(!fs::safeWriteToFile(m_banfilepath, ss.str())) {
90                 infostream<<"BanManager: failed saving to "<<m_banfilepath<<std::endl;
91                 throw SerializationError("BanManager::save(): Couldn't write file");
92         }
93
94         m_modified = false;
95 }
96
97 bool BanManager::isIpBanned(const std::string &ip)
98 {
99         JMutexAutoLock lock(m_mutex);
100         return m_ips.find(ip) != m_ips.end();
101 }
102
103 std::string BanManager::getBanDescription(const std::string &ip_or_name)
104 {
105         JMutexAutoLock lock(m_mutex);
106         std::string s = "";
107         for(std::map<std::string, std::string>::iterator
108                         i = m_ips.begin();
109                         i != m_ips.end(); i++)
110         {
111                 if(i->first == ip_or_name || i->second == ip_or_name
112                                 || ip_or_name == "")
113                         s += i->first + "|" + i->second + ", ";
114         }
115         s = s.substr(0, s.size()-2);
116         return s;
117 }
118
119 std::string BanManager::getBanName(const std::string &ip)
120 {
121         JMutexAutoLock lock(m_mutex);
122         std::map<std::string, std::string>::iterator i = m_ips.find(ip);
123         if(i == m_ips.end())
124                 return "";
125         return i->second;
126 }
127
128 void BanManager::add(const std::string &ip, const std::string &name)
129 {
130         JMutexAutoLock lock(m_mutex);
131         m_ips[ip] = name;
132         m_modified = true;
133 }
134
135 void BanManager::remove(const std::string &ip_or_name)
136 {
137         JMutexAutoLock lock(m_mutex);
138         //m_ips.erase(m_ips.find(ip));
139         // Find out all ip-name pairs that match the ip or name
140         std::set<std::string> ips_to_delete;
141         for(std::map<std::string, std::string>::iterator
142                         i = m_ips.begin();
143                         i != m_ips.end(); i++)
144         {
145                 if(i->first == ip_or_name || i->second == ip_or_name)
146                         ips_to_delete.insert(i->first);
147         }
148         // Erase them
149         for(std::set<std::string>::iterator
150                         i = ips_to_delete.begin();
151                         i != ips_to_delete.end(); i++)
152         {
153                 m_ips.erase(*i);
154         }
155         m_modified = true;
156 }
157         
158
159 bool BanManager::isModified()
160 {
161         JMutexAutoLock lock(m_mutex);
162         return m_modified;
163 }
164