]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/bancache.cpp
244ffd758206f1965674e7ebcc807c700d7874e2
[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 /* $Core */
22
23 #include "inspircd.h"
24 #include "bancache.h"
25
26 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
27 {
28         BanCacheHit*& b = (*BanHash)[ip];
29         if (b != NULL) // can't have two cache entries on the same IP, sorry..
30                 return NULL;
31
32         b = new BanCacheHit(type, reason, (seconds ? seconds : 86400));
33         return b;
34 }
35
36 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
37 {
38         BanCacheHash::iterator i = this->BanHash->find(ip);
39
40         if (i == this->BanHash->end())
41                 return NULL; // free and safe
42         else
43         {
44                 if (ServerInstance->Time() > i->second->Expiry)
45                 {
46                         ServerInstance->Logs->Log("BANCACHE", DEBUG, "Hit on " + ip + " is out of date, removing!");
47                         delete i->second;
48                         BanHash->erase(i);
49                         return NULL; // out of date
50                 }
51
52                 return i->second; // hit.
53         }
54 }
55
56 void BanCacheManager::RemoveEntries(const std::string& type, bool positive)
57 {
58         if (positive)
59                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type);
60         else
61                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing all negative hits");
62
63         for (BanCacheHash::iterator i = BanHash->begin(); i != BanHash->end(); )
64         {
65                 BanCacheHit* b = i->second;
66                 bool remove = false;
67
68                 if (positive)
69                 {
70                         // when removing positive hits, remove only if the type matches
71                         remove = b->IsPositive() && (b->Type == type);
72                 }
73                 else
74                 {
75                         // when removing negative hits, remove all of them
76                         remove = !b->IsPositive();
77                 }
78
79                 if (remove)
80                 {
81                         /* we need to remove this one. */
82                         ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + i->first);
83                         delete b;
84                         i = BanHash->erase(i);
85                 }
86                 else
87                         ++i;
88         }
89 }
90
91 void BanCacheManager::RehashCache()
92 {
93         BanCacheHash* NewHash = new BanCacheHash();
94
95         BanCacheHash::iterator safei;
96         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
97         {
98                 safei = n;
99                 safei++;
100
101                 /* Safe to delete items here through iterator 'n' */
102                 BanCacheHit *b = n->second;
103
104                 if (ServerInstance->Time() > b->Expiry)
105                 {
106                         /* we need to remove this one. */
107                         delete b;
108                         BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
109                 }
110                 else
111                 {
112                         /* Actually inserts a std::pair */
113                         NewHash->insert(*n);
114                 }
115
116                 /* End of safe section */
117
118                 n = safei;
119         }
120
121         delete BanHash;
122         BanHash = NewHash;
123 }
124
125 BanCacheManager::~BanCacheManager()
126 {
127         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); ++n)
128                 delete n->second;
129         delete BanHash;
130 }