utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/minetest.git] / src / ban.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 <jmutexautolock.h>
23 #include <sstream>
24 #include <set>
25 #include "strfnd.h"
26 #include "debug.h"
27
28 BanManager::BanManager(const std::string &banfilepath):
29                 m_banfilepath(banfilepath),
30                 m_modified(false)
31 {
32         m_mutex.Init();
33         try{
34                 load();
35         }
36         catch(SerializationError &e)
37         {
38                 dstream<<"WARNING: BanManager: creating "
39                                 <<m_banfilepath<<std::endl;
40         }
41 }
42
43 BanManager::~BanManager()
44 {
45         save();
46 }
47
48 void BanManager::load()
49 {
50         JMutexAutoLock lock(m_mutex);
51         dstream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
52         std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
53         if(is.good() == false)
54         {
55                 dstream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
56                 throw SerializationError("BanManager::load(): Couldn't open file");
57         }
58         
59         for(;;)
60         {
61                 if(is.eof() || is.good() == false)
62                         break;
63                 std::string line;
64                 std::getline(is, line, '\n');
65                 Strfnd f(line);
66                 std::string ip = trim(f.next("|"));
67                 std::string name = trim(f.next("|"));
68                 if(ip.empty())
69                         continue;
70                 m_ips[ip] = name;
71         }
72         m_modified = false;
73 }
74
75 void BanManager::save()
76 {
77         JMutexAutoLock lock(m_mutex);
78         dstream<<"BanManager: saving to "<<m_banfilepath<<std::endl;
79         std::ofstream os(m_banfilepath.c_str(), std::ios::binary);
80         
81         if(os.good() == false)
82         {
83                 dstream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
84                 throw SerializationError("BanManager::load(): Couldn't open file");
85         }
86
87         for(std::map<std::string, std::string>::iterator
88                         i = m_ips.begin();
89                         i != m_ips.end(); i++)
90         {
91                 os<<i->first<<"|"<<i->second<<"\n";
92         }
93         m_modified = false;
94 }
95
96 bool BanManager::isIpBanned(const std::string &ip)
97 {
98         JMutexAutoLock lock(m_mutex);
99         return m_ips.find(ip) != m_ips.end();
100 }
101
102 std::string BanManager::getBanDescription(const std::string &ip_or_name)
103 {
104         JMutexAutoLock lock(m_mutex);
105         std::string s = "";
106         for(std::map<std::string, std::string>::iterator
107                         i = m_ips.begin();
108                         i != m_ips.end(); i++)
109         {
110                 if(i->first == ip_or_name || i->second == ip_or_name
111                                 || ip_or_name == "")
112                         s += i->first + "|" + i->second + ", ";
113         }
114         s = s.substr(0, s.size()-2);
115         return s;
116 }
117
118 std::string BanManager::getBanName(const std::string &ip)
119 {
120         JMutexAutoLock lock(m_mutex);
121         std::map<std::string, std::string>::iterator i = m_ips.find(ip);
122         if(i == m_ips.end())
123                 return "";
124         return i->second;
125 }
126
127 void BanManager::add(const std::string &ip, const std::string &name)
128 {
129         JMutexAutoLock lock(m_mutex);
130         m_ips[ip] = name;
131         m_modified = true;
132 }
133
134 void BanManager::remove(const std::string &ip_or_name)
135 {
136         JMutexAutoLock lock(m_mutex);
137         //m_ips.erase(m_ips.find(ip));
138         // Find out all ip-name pairs that match the ip or name
139         std::set<std::string> ips_to_delete;
140         for(std::map<std::string, std::string>::iterator
141                         i = m_ips.begin();
142                         i != m_ips.end(); i++)
143         {
144                 if(i->first == ip_or_name || i->second == ip_or_name)
145                         ips_to_delete.insert(i->first);
146         }
147         // Erase them
148         for(std::set<std::string>::iterator
149                         i = ips_to_delete.begin();
150                         i != ips_to_delete.end(); i++)
151         {
152                 m_ips.erase(*i);
153         }
154         m_modified = true;
155 }
156         
157
158 bool BanManager::isModified()
159 {
160         JMutexAutoLock lock(m_mutex);
161         return m_modified;
162 }
163