]> git.lizzy.rs Git - dragonfireclient.git/blob - src/ban.cpp
Build and link gmp correctly on MSVC
[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 "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         while(!is.eof() && is.good())
61         {
62                 std::string line;
63                 std::getline(is, line, '\n');
64                 Strfnd f(line);
65                 std::string ip = trim(f.next("|"));
66                 std::string name = trim(f.next("|"));
67                 if(!ip.empty()) {
68                         m_ips[ip] = name;
69                 }
70         }
71         m_modified = false;
72 }
73
74 void BanManager::save()
75 {
76         JMutexAutoLock lock(m_mutex);
77         infostream<<"BanManager: saving to "<<m_banfilepath<<std::endl;
78         std::ostringstream ss(std::ios_base::binary);
79
80         for(std::map<std::string, std::string>::iterator
81                         i = m_ips.begin();
82                         i != m_ips.end(); i++)
83         {
84                 ss << i->first << "|" << i->second << "\n";
85         }
86
87         if(!fs::safeWriteToFile(m_banfilepath, ss.str())) {
88                 infostream<<"BanManager: failed saving to "<<m_banfilepath<<std::endl;
89                 throw SerializationError("BanManager::save(): Couldn't write file");
90         }
91
92         m_modified = false;
93 }
94
95 bool BanManager::isIpBanned(const std::string &ip)
96 {
97         JMutexAutoLock lock(m_mutex);
98         return m_ips.find(ip) != m_ips.end();
99 }
100
101 std::string BanManager::getBanDescription(const std::string &ip_or_name)
102 {
103         JMutexAutoLock lock(m_mutex);
104         std::string s = "";
105         for(std::map<std::string, std::string>::iterator
106                         i = m_ips.begin();
107                         i != m_ips.end(); i++)
108         {
109                 if(i->first == ip_or_name || i->second == ip_or_name
110                                 || ip_or_name == "")
111                         s += i->first + "|" + i->second + ", ";
112         }
113         s = s.substr(0, s.size()-2);
114         return s;
115 }
116
117 std::string BanManager::getBanName(const std::string &ip)
118 {
119         JMutexAutoLock lock(m_mutex);
120         std::map<std::string, std::string>::iterator i = m_ips.find(ip);
121         if(i == m_ips.end())
122                 return "";
123         return i->second;
124 }
125
126 void BanManager::add(const std::string &ip, const std::string &name)
127 {
128         JMutexAutoLock lock(m_mutex);
129         m_ips[ip] = name;
130         m_modified = true;
131 }
132
133 void BanManager::remove(const std::string &ip_or_name)
134 {
135         JMutexAutoLock lock(m_mutex);
136         for(std::map<std::string, std::string>::iterator
137                         i = m_ips.begin();
138                         i != m_ips.end();)
139         {
140                 if((i->first == ip_or_name) || (i->second == ip_or_name)) {
141                         m_ips.erase(i++);
142                 } else {
143                         ++i;
144                 }
145         }
146         m_modified = true;
147 }
148         
149
150 bool BanManager::isModified()
151 {
152         JMutexAutoLock lock(m_mutex);
153         return m_modified;
154 }
155