1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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;
80 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
85 BanCacheHit *b = n->second;
87 /* Safe to delete items here through iterator 'n' */
90 if ((positive && !b->Reason.empty()) || b->Reason.empty())
92 /* we need to remove this one. */
95 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
100 /* End of safe section */
108 void BanCacheManager::RehashCache()
110 BanCacheHash* NewHash = new BanCacheHash();
112 BanCacheHash::iterator safei;
113 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
118 /* Safe to delete items here through iterator 'n' */
119 BanCacheHit *b = n->second;
121 if (time(NULL) > b->Expiry)
123 /* we need to remove this one. */
126 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
130 /* Actually inserts a std::pair */
134 /* End of safe section */