Cleanup ban.cpp/h (#15496)

Make BanManager more const correctly
Delete unused includes
This commit is contained in:
wrrrzr 2024-12-01 22:52:13 +03:00 committed by GitHub
parent 7cc5a6ec68
commit e9080f91f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 15 deletions

@ -7,11 +7,11 @@
#include <fstream> #include <fstream>
#include "threading/mutex_auto_lock.h" #include "threading/mutex_auto_lock.h"
#include <sstream> #include <sstream>
#include <set>
#include "util/strfnd.h" #include "util/strfnd.h"
#include "util/string.h" #include "util/string.h"
#include "log.h" #include "log.h"
#include "filesys.h" #include "filesys.h"
#include "exceptions.h"
BanManager::BanManager(const std::string &banfilepath): BanManager::BanManager(const std::string &banfilepath):
m_banfilepath(banfilepath) m_banfilepath(banfilepath)
@ -68,13 +68,13 @@ void BanManager::save()
m_modified = false; m_modified = false;
} }
bool BanManager::isIpBanned(const std::string &ip) bool BanManager::isIpBanned(const std::string &ip) const
{ {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
return m_ips.find(ip) != m_ips.end(); return m_ips.find(ip) != m_ips.end();
} }
std::string BanManager::getBanDescription(const std::string &ip_or_name) std::string BanManager::getBanDescription(const std::string &ip_or_name) const
{ {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
std::string s; std::string s;
@ -88,10 +88,10 @@ std::string BanManager::getBanDescription(const std::string &ip_or_name)
return s; return s;
} }
std::string BanManager::getBanName(const std::string &ip) std::string BanManager::getBanName(const std::string &ip) const
{ {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
StringMap::iterator it = m_ips.find(ip); StringMap::const_iterator it = m_ips.find(ip);
if (it == m_ips.end()) if (it == m_ips.end())
return ""; return "";
return it->second; return it->second;
@ -118,9 +118,8 @@ void BanManager::remove(const std::string &ip_or_name)
} }
bool BanManager::isModified() bool BanManager::isModified() const
{ {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
return m_modified; return m_modified;
} }

@ -5,9 +5,6 @@
#pragma once #pragma once
#include "util/string.h" #include "util/string.h"
#include "threading/thread.h"
#include "exceptions.h"
#include <map>
#include <string> #include <string>
#include <mutex> #include <mutex>
@ -18,16 +15,16 @@ public:
~BanManager(); ~BanManager();
void load(); void load();
void save(); void save();
bool isIpBanned(const std::string &ip); bool isIpBanned(const std::string &ip) const;
// Supplying ip_or_name = "" lists all bans. // Supplying ip_or_name = "" lists all bans.
std::string getBanDescription(const std::string &ip_or_name); std::string getBanDescription(const std::string &ip_or_name) const;
std::string getBanName(const std::string &ip); std::string getBanName(const std::string &ip) const;
void add(const std::string &ip, const std::string &name); void add(const std::string &ip, const std::string &name);
void remove(const std::string &ip_or_name); void remove(const std::string &ip_or_name);
bool isModified(); bool isModified() const;
private: private:
std::mutex m_mutex; mutable std::mutex m_mutex;
std::string m_banfilepath = ""; std::string m_banfilepath = "";
StringMap m_ips; StringMap m_ips;
bool m_modified = false; bool m_modified = false;