]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
BanCache: Simplify BanCacheManager::RemoveEntries()
authorattilamolnar <attilamolnar@hush.com>
Sun, 30 Sep 2012 23:00:10 +0000 (01:00 +0200)
committerattilamolnar <attilamolnar@hush.com>
Wed, 10 Apr 2013 17:27:24 +0000 (19:27 +0200)
include/bancache.h
src/bancache.cpp

index e975bb68ad59c1785891636783c8fdc06433c762..70ec09f0233ce0e5c84c05e4f0355e6a654132d3 100644 (file)
@@ -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()
        {
index c38a31b4dad7277321c672a82a697bd32e2af248..244ffd758206f1965674e7ebcc807c700d7874e2 100644 (file)
@@ -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()