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