]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/bancache.cpp
452bd998f3eb199f5ea15ba50de08af74228e2d9
[user/henk/code/inspircd.git] / src / bancache.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
5  *   Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "bancache.h"
23
24 BanCacheHit::BanCacheHit(const std::string& type, const std::string& reason, time_t seconds)
25         : Type(type)
26         , Reason(reason)
27         , Expiry(ServerInstance->Time() + seconds)
28 {
29 }
30
31 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
32 {
33         BanCacheHit*& b = BanHash[ip];
34         if (b != NULL) // can't have two cache entries on the same IP, sorry..
35                 return NULL;
36
37         b = new BanCacheHit(type, reason, (seconds ? seconds : 86400));
38         return b;
39 }
40
41 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
42 {
43         BanCacheHash::iterator i = this->BanHash.find(ip);
44
45         if (i == this->BanHash.end())
46                 return NULL; // free and safe
47
48         if (RemoveIfExpired(i))
49                 return NULL; // expired
50
51         return i->second; // hit.
52 }
53
54 bool BanCacheManager::RemoveIfExpired(BanCacheHash::iterator& it)
55 {
56         if (ServerInstance->Time() < it->second->Expiry)
57                 return false;
58
59         ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "Hit on " + it->first + " is out of date, removing!");
60         delete it->second;
61         it = BanHash.erase(it);
62         return true;
63 }
64
65 void BanCacheManager::RemoveEntries(const std::string& type, bool positive)
66 {
67         if (positive)
68                 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type);
69         else
70                 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCacheManager::RemoveEntries(): Removing all negative hits");
71
72         for (BanCacheHash::iterator i = BanHash.begin(); i != BanHash.end(); )
73         {
74                 if (RemoveIfExpired(i))
75                         continue; // updates the iterator if expired
76
77                 BanCacheHit* b = i->second;
78                 bool remove = false;
79
80                 if (positive)
81                 {
82                         // when removing positive hits, remove only if the type matches
83                         remove = b->IsPositive() && (b->Type == type);
84                 }
85                 else
86                 {
87                         // when removing negative hits, remove all of them
88                         remove = !b->IsPositive();
89                 }
90
91                 if (remove)
92                 {
93                         /* we need to remove this one. */
94                         ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + i->first);
95                         delete b;
96                         i = BanHash.erase(i);
97                 }
98                 else
99                         ++i;
100         }
101 }
102
103 BanCacheManager::~BanCacheManager()
104 {
105         for (BanCacheHash::iterator n = BanHash.begin(); n != BanHash.end(); ++n)
106                 delete n->second;
107 }