X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fban.cpp;h=3decc9666117274c4a7e21d490b4ce567edca655;hb=ca5c2dbefab3676514e48b445b36de50993de9f1;hp=7c1a68d458af2d330150198c8d812dc1d79df0c9;hpb=da34a2b33e1f600ec11172f599384b9a92835403;p=minetest.git diff --git a/src/ban.cpp b/src/ban.cpp index 7c1a68d45..3decc9666 100644 --- a/src/ban.cpp +++ b/src/ban.cpp @@ -1,6 +1,7 @@ /* Minetest Copyright (C) 2013 celeron55, Perttu Ahola +Copyright (C) 2018 nerzhul, Loic BLOT This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -19,25 +20,22 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "ban.h" #include -#include "jthread/jmutexautolock.h" +#include "threading/mutex_auto_lock.h" #include #include -#include "strfnd.h" +#include "util/strfnd.h" #include "util/string.h" #include "log.h" #include "filesys.h" BanManager::BanManager(const std::string &banfilepath): - m_banfilepath(banfilepath), - m_modified(false) + m_banfilepath(banfilepath) { - try{ + try { load(); - } - catch(SerializationError &e) - { - infostream<<"WARNING: BanManager: creating " - <first << "|" << it->second << "\n"; + for (const auto &ip : m_ips) + ss << ip.first << "|" << ip.second << "\n"; if (!fs::safeWriteToFile(m_banfilepath, ss.str())) { infostream << "BanManager: failed saving to " << m_banfilepath << std::endl; @@ -90,18 +86,18 @@ void BanManager::save() bool BanManager::isIpBanned(const std::string &ip) { - JMutexAutoLock lock(m_mutex); + MutexAutoLock lock(m_mutex); return m_ips.find(ip) != m_ips.end(); } std::string BanManager::getBanDescription(const std::string &ip_or_name) { - JMutexAutoLock lock(m_mutex); - std::string s = ""; - for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it) { - if (it->first == ip_or_name || it->second == ip_or_name - || ip_or_name == "") { - s += it->first + "|" + it->second + ", "; + MutexAutoLock lock(m_mutex); + std::string s; + for (const auto &ip : m_ips) { + if (ip.first == ip_or_name || ip.second == ip_or_name + || ip_or_name.empty()) { + s += ip.first + "|" + ip.second + ", "; } } s = s.substr(0, s.size() - 2); @@ -110,7 +106,7 @@ std::string BanManager::getBanDescription(const std::string &ip_or_name) std::string BanManager::getBanName(const std::string &ip) { - JMutexAutoLock lock(m_mutex); + MutexAutoLock lock(m_mutex); StringMap::iterator it = m_ips.find(ip); if (it == m_ips.end()) return ""; @@ -119,28 +115,28 @@ std::string BanManager::getBanName(const std::string &ip) void BanManager::add(const std::string &ip, const std::string &name) { - JMutexAutoLock lock(m_mutex); + MutexAutoLock lock(m_mutex); m_ips[ip] = name; m_modified = true; } void BanManager::remove(const std::string &ip_or_name) { - JMutexAutoLock lock(m_mutex); + MutexAutoLock lock(m_mutex); for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) { if ((it->first == ip_or_name) || (it->second == ip_or_name)) { m_ips.erase(it++); + m_modified = true; } else { ++it; } } - m_modified = true; } bool BanManager::isModified() { - JMutexAutoLock lock(m_mutex); + MutexAutoLock lock(m_mutex); return m_modified; }