Merge remote-tracking branch 'speedprog/banByIp'
[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 "strfnd.h"
25 #include "debug.h"
26
27 BanManager::BanManager(const std::string &banfilepath):
28                 m_banfilepath(banfilepath),
29                 m_modified(false)
30 {
31         m_mutex.Init();
32         try{
33                 load();
34         }
35         catch(SerializationError &e)
36         {
37                 dstream<<"WARNING: BanManager: creating "
38                                 <<m_banfilepath<<std::endl;
39         }
40 }
41
42 BanManager::~BanManager()
43 {
44         save();
45 }
46
47 void BanManager::load()
48 {
49         JMutexAutoLock lock(m_mutex);
50         dstream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
51         std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
52         if(is.good() == false)
53         {
54                 dstream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
55                 throw SerializationError("BanManager::load(): Couldn't open file");
56         }
57         
58         for(;;)
59         {
60                 if(is.eof() || is.good() == false)
61                         break;
62                 std::string ip;
63                 std::getline(is, ip, '\n');
64                 m_ips.insert(ip);
65         }
66         m_modified = false;
67 }
68
69 void BanManager::save()
70 {
71         JMutexAutoLock lock(m_mutex);
72         dstream<<"BanManager: saving to "<<m_banfilepath<<std::endl;
73         std::ofstream os(m_banfilepath.c_str(), std::ios::binary);
74         
75         if(os.good() == false)
76         {
77                 dstream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
78                 throw SerializationError("BanManager::load(): Couldn't open file");
79         }
80
81         for(std::set<std::string>::iterator
82                         i = m_ips.begin();
83                         i != m_ips.end(); i++)
84         {
85                 if(*i == "")
86                         continue;
87                 os<<*i<<"\n";
88         }
89         m_modified = false;
90 }
91
92 bool BanManager::isIpBanned(std::string ip)
93 {
94         JMutexAutoLock lock(m_mutex);
95         return m_ips.find(ip) != m_ips.end();
96 }
97
98 void BanManager::add(std::string ip)
99 {
100         JMutexAutoLock lock(m_mutex);
101         m_ips.insert(ip);
102         m_modified = true;
103 }
104
105 void BanManager::remove(std::string ip)
106 {
107         JMutexAutoLock lock(m_mutex);
108         m_ips.erase(m_ips.find(ip));
109         m_modified = true;
110 }
111         
112
113 bool BanManager::isModified()
114 {
115         JMutexAutoLock lock(m_mutex);
116         return m_modified;
117 }
118