2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
5 * Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason)
30 if (this->BanHash->find(ip) != this->BanHash->end()) // can't have two cache entries on the same IP, sorry..
33 b = new BanCacheHit(ip, type, reason);
35 this->BanHash->insert(std::make_pair(ip, b));
39 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
43 if (this->BanHash->find(ip) != this->BanHash->end()) // can't have two cache entries on the same IP, sorry..
46 b = new BanCacheHit(ip, type, reason, seconds);
48 this->BanHash->insert(std::make_pair(ip, b));
52 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
54 BanCacheHash::iterator i = this->BanHash->find(ip);
56 if (i == this->BanHash->end())
57 return NULL; // free and safe
60 if (ServerInstance->Time() > i->second->Expiry)
62 ServerInstance->Logs->Log("BANCACHE", DEBUG, "Hit on " + ip + " is out of date, removing!");
64 return NULL; // out of date
67 return i->second; // hit.
71 bool BanCacheManager::RemoveHit(BanCacheHit *b)
73 BanCacheHash::iterator i;
76 return false; // I don't think so.
78 i = this->BanHash->find(b->IP);
80 if (i == this->BanHash->end())
83 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveHit(): I got asked to remove a hit that wasn't in the hash(?)");
87 this->BanHash->erase(i);
94 unsigned int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
98 BanCacheHash::iterator safei;
101 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type);
103 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing negative hits for " + type);
105 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
110 BanCacheHit *b = n->second;
112 /* Safe to delete items here through iterator 'n' */
113 if (b->Type == type || !positive) // if removing negative hits, ignore type..
115 if ((positive && !b->Reason.empty()) || b->Reason.empty())
117 /* we need to remove this one. */
118 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + b->IP);
120 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
125 /* End of safe section */
133 void BanCacheManager::RehashCache()
135 BanCacheHash* NewHash = new BanCacheHash();
137 BanCacheHash::iterator safei;
138 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
143 /* Safe to delete items here through iterator 'n' */
144 BanCacheHit *b = n->second;
146 if (ServerInstance->Time() > b->Expiry)
148 /* we need to remove this one. */
150 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
154 /* Actually inserts a std::pair */
158 /* End of safe section */
167 BanCacheManager::~BanCacheManager()
169 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); ++n)