1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $Core: libIRCDbancache */
19 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason)
23 if (this->BanHash->find(ip) != this->BanHash->end()) // can't have two cache entries on the same IP, sorry..
26 b = new BanCacheHit(ServerInstance, ip, type, reason);
28 this->BanHash->insert(std::make_pair(ip, b));
32 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
34 BanCacheHash::iterator i = this->BanHash->find(ip);
36 if (i == this->BanHash->end())
37 return NULL; // free and safe
40 if (time(NULL) > i->second->Expiry)
42 ServerInstance->Log(DEBUG, "Hit on " + ip + " is out of date, removing!");
44 return NULL; // out of date
47 return i->second; // hit.
51 bool BanCacheManager::RemoveHit(BanCacheHit *b)
53 BanCacheHash::iterator i;
56 return false; // I don't think so.
58 i = this->BanHash->find(b->IP);
60 if (i == this->BanHash->end())
63 ServerInstance->Log(DEBUG, "BanCacheManager::RemoveHit(): I got asked to remove a hit that wasn't in the hash(?)");
67 this->BanHash->erase(i);
74 unsigned int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
78 BanCacheHash::iterator safei;
81 ServerInstance->Log(DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type);
83 ServerInstance->Log(DEBUG, "BanCacheManager::RemoveEntries(): Removing negative hits for " + type);
85 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
90 BanCacheHit *b = n->second;
92 /* Safe to delete items here through iterator 'n' */
93 if (b->Type == type || !positive) // if removing negative hits, ignore type..
95 if ((positive && !b->Reason.empty()) || b->Reason.empty())
97 /* we need to remove this one. */
98 ServerInstance->Log(DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + b->IP);
101 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
106 /* End of safe section */
114 void BanCacheManager::RehashCache()
116 BanCacheHash* NewHash = new BanCacheHash();
118 BanCacheHash::iterator safei;
119 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
124 /* Safe to delete items here through iterator 'n' */
125 BanCacheHit *b = n->second;
127 if (time(NULL) > b->Expiry)
129 /* we need to remove this one. */
132 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
136 /* Actually inserts a std::pair */
140 /* End of safe section */