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::AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
36 if (this->BanHash->find(ip) != this->BanHash->end()) // can't have two cache entries on the same IP, sorry..
39 b = new BanCacheHit(ServerInstance, ip, type, reason, seconds);
41 this->BanHash->insert(std::make_pair(ip, b));
45 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
47 BanCacheHash::iterator i = this->BanHash->find(ip);
49 if (i == this->BanHash->end())
50 return NULL; // free and safe
53 if (time(NULL) > i->second->Expiry)
55 ServerInstance->Logs->Log("BANCACHE", DEBUG, "Hit on " + ip + " is out of date, removing!");
57 return NULL; // out of date
60 return i->second; // hit.
64 bool BanCacheManager::RemoveHit(BanCacheHit *b)
66 BanCacheHash::iterator i;
69 return false; // I don't think so.
71 i = this->BanHash->find(b->IP);
73 if (i == this->BanHash->end())
76 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveHit(): I got asked to remove a hit that wasn't in the hash(?)");
80 this->BanHash->erase(i);
87 unsigned int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
91 BanCacheHash::iterator safei;
94 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type);
96 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing negative hits for " + type);
98 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
103 BanCacheHit *b = n->second;
105 /* Safe to delete items here through iterator 'n' */
106 if (b->Type == type || !positive) // if removing negative hits, ignore type..
108 if ((positive && !b->Reason.empty()) || b->Reason.empty())
110 /* we need to remove this one. */
111 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + b->IP);
114 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
119 /* End of safe section */
127 void BanCacheManager::RehashCache()
129 BanCacheHash* NewHash = new BanCacheHash();
131 BanCacheHash::iterator safei;
132 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
137 /* Safe to delete items here through iterator 'n' */
138 BanCacheHit *b = n->second;
140 if (time(NULL) > b->Expiry)
142 /* we need to remove this one. */
145 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
149 /* Actually inserts a std::pair */
153 /* End of safe section */