diff options
-rw-r--r-- | include/bancache.h | 4 | ||||
-rw-r--r-- | src/bancache.cpp | 48 |
2 files changed, 25 insertions, 27 deletions
diff --git a/include/bancache.h b/include/bancache.h index e975bb68a..70ec09f02 100644 --- a/include/bancache.h +++ b/include/bancache.h @@ -45,6 +45,8 @@ class CoreExport BanCacheHit : Type(type), Reason(reason), Expiry(ServerInstance->Time() + seconds) { } + + bool IsPositive() const { return (!Reason.empty()); } }; /* A container of ban cache items. @@ -73,7 +75,7 @@ class CoreExport BanCacheManager * @param type The type of bancache entries to remove (e.g. 'G') * @param positive Remove either positive (true) or negative (false) hits. */ - unsigned int RemoveEntries(const std::string &type, bool positive); + void RemoveEntries(const std::string& type, bool positive); BanCacheManager() { diff --git a/src/bancache.cpp b/src/bancache.cpp index c38a31b4d..244ffd758 100644 --- a/src/bancache.cpp +++ b/src/bancache.cpp @@ -53,43 +53,39 @@ BanCacheHit *BanCacheManager::GetHit(const std::string &ip) } } -unsigned int BanCacheManager::RemoveEntries(const std::string &type, bool positive) +void BanCacheManager::RemoveEntries(const std::string& type, bool positive) { - int removed = 0; - - BanCacheHash::iterator safei; - if (positive) ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type); else - ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing negative hits for " + type); + ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing all negative hits"); - for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); ) + for (BanCacheHash::iterator i = BanHash->begin(); i != BanHash->end(); ) { - safei = n; - safei++; + BanCacheHit* b = i->second; + bool remove = false; - BanCacheHit *b = n->second; - - /* Safe to delete items here through iterator 'n' */ - if (b->Type == type || !positive) // if removing negative hits, ignore type.. + if (positive) { - if ((positive && !b->Reason.empty()) || b->Reason.empty()) - { - /* we need to remove this one. */ - ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + n->first); - delete b; - BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way. - removed++; - } + // when removing positive hits, remove only if the type matches + remove = b->IsPositive() && (b->Type == type); + } + else + { + // when removing negative hits, remove all of them + remove = !b->IsPositive(); } - /* End of safe section */ - n = safei; + if (remove) + { + /* we need to remove this one. */ + ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + i->first); + delete b; + i = BanHash->erase(i); + } + else + ++i; } - - - return removed; } void BanCacheManager::RehashCache() |