LCOV - code coverage report
Current view: top level - src - ban.cpp (source / functions) Hit Total Coverage
Test: report Lines: 0 72 0.0 %
Date: 2015-07-11 18:23:49 Functions: 0 10 0.0 %

          Line data    Source code
       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           0 : BanManager::BanManager(const std::string &banfilepath):
      31             :                 m_banfilepath(banfilepath),
      32           0 :                 m_modified(false)
      33             : {
      34             :         try{
      35           0 :                 load();
      36             :         }
      37           0 :         catch(SerializationError &e)
      38             :         {
      39           0 :                 infostream<<"WARNING: BanManager: creating "
      40           0 :                                 <<m_banfilepath<<std::endl;
      41             :         }
      42           0 : }
      43             : 
      44           0 : BanManager::~BanManager()
      45             : {
      46           0 :         save();
      47           0 : }
      48             : 
      49           0 : void BanManager::load()
      50             : {
      51           0 :         JMutexAutoLock lock(m_mutex);
      52           0 :         infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
      53           0 :         std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
      54           0 :         if(is.good() == false)
      55             :         {
      56           0 :                 infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
      57           0 :                 throw SerializationError("BanManager::load(): Couldn't open file");
      58             :         }
      59             : 
      60           0 :         while(!is.eof() && is.good())
      61             :         {
      62           0 :                 std::string line;
      63           0 :                 std::getline(is, line, '\n');
      64           0 :                 Strfnd f(line);
      65           0 :                 std::string ip = trim(f.next("|"));
      66           0 :                 std::string name = trim(f.next("|"));
      67           0 :                 if(!ip.empty()) {
      68           0 :                         m_ips[ip] = name;
      69             :                 }
      70             :         }
      71           0 :         m_modified = false;
      72           0 : }
      73             : 
      74           0 : void BanManager::save()
      75             : {
      76           0 :         JMutexAutoLock lock(m_mutex);
      77           0 :         infostream << "BanManager: saving to " << m_banfilepath << std::endl;
      78           0 :         std::ostringstream ss(std::ios_base::binary);
      79             : 
      80           0 :         for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it)
      81           0 :                 ss << it->first << "|" << it->second << "\n";
      82             : 
      83           0 :         if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
      84           0 :                 infostream << "BanManager: failed saving to " << m_banfilepath << std::endl;
      85           0 :                 throw SerializationError("BanManager::save(): Couldn't write file");
      86             :         }
      87             : 
      88           0 :         m_modified = false;
      89           0 : }
      90             : 
      91           0 : bool BanManager::isIpBanned(const std::string &ip)
      92             : {
      93           0 :         JMutexAutoLock lock(m_mutex);
      94           0 :         return m_ips.find(ip) != m_ips.end();
      95             : }
      96             : 
      97           0 : std::string BanManager::getBanDescription(const std::string &ip_or_name)
      98             : {
      99           0 :         JMutexAutoLock lock(m_mutex);
     100           0 :         std::string s = "";
     101           0 :         for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it) {
     102           0 :                 if (it->first  == ip_or_name || it->second == ip_or_name
     103           0 :                                 || ip_or_name == "") {
     104           0 :                         s += it->first + "|" + it->second + ", ";
     105             :                 }
     106             :         }
     107           0 :         s = s.substr(0, s.size() - 2);
     108           0 :         return s;
     109             : }
     110             : 
     111           0 : std::string BanManager::getBanName(const std::string &ip)
     112             : {
     113           0 :         JMutexAutoLock lock(m_mutex);
     114           0 :         StringMap::iterator it = m_ips.find(ip);
     115           0 :         if (it == m_ips.end())
     116           0 :                 return "";
     117           0 :         return it->second;
     118             : }
     119             : 
     120           0 : void BanManager::add(const std::string &ip, const std::string &name)
     121             : {
     122           0 :         JMutexAutoLock lock(m_mutex);
     123           0 :         m_ips[ip] = name;
     124           0 :         m_modified = true;
     125           0 : }
     126             : 
     127           0 : void BanManager::remove(const std::string &ip_or_name)
     128             : {
     129           0 :         JMutexAutoLock lock(m_mutex);
     130           0 :         for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
     131           0 :                 if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
     132           0 :                         m_ips.erase(it++);
     133             :                 } else {
     134           0 :                         ++it;
     135             :                 }
     136             :         }
     137           0 :         m_modified = true;
     138           0 : }
     139             : 
     140             : 
     141           0 : bool BanManager::isModified()
     142             : {
     143           0 :         JMutexAutoLock lock(m_mutex);
     144           0 :         return m_modified;
     145             : }
     146             : 

Generated by: LCOV version 1.11