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/>.
23 BanCacheHit::BanCacheHit(const std::string& type, const std::string& reason, time_t seconds)
26 , Expiry(ServerInstance->Time() + seconds)
30 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
32 BanCacheHit*& b = BanHash[ip];
33 if (b != NULL) // can't have two cache entries on the same IP, sorry..
36 b = new BanCacheHit(type, reason, (seconds ? seconds : 86400));
40 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
42 BanCacheHash::iterator i = this->BanHash.find(ip);
44 if (i == this->BanHash.end())
45 return NULL; // free and safe
47 if (RemoveIfExpired(i))
48 return NULL; // expired
50 return i->second; // hit.
53 bool BanCacheManager::RemoveIfExpired(BanCacheHash::iterator& it)
55 if (ServerInstance->Time() < it->second->Expiry)
58 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "Hit on " + it->first + " is out of date, removing!");
60 it = BanHash.erase(it);
64 void BanCacheManager::RemoveEntries(const std::string& type, bool positive)
67 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type);
69 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCacheManager::RemoveEntries(): Removing all negative hits");
71 for (BanCacheHash::iterator i = BanHash.begin(); i != BanHash.end(); )
73 if (RemoveIfExpired(i))
74 continue; // updates the iterator if expired
76 BanCacheHit* b = i->second;
81 // when removing positive hits, remove only if the type matches
82 remove = b->IsPositive() && (b->Type == type);
86 // when removing negative hits, remove all of them
87 remove = !b->IsPositive();
92 /* we need to remove this one. */
93 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + i->first);
102 BanCacheManager::~BanCacheManager()
104 for (BanCacheHash::iterator n = BanHash.begin(); n != BanHash.end(); ++n)