]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/bancache.cpp
524baaad2e4805471a53ccf1030259c8c4ce8113
[user/henk/code/inspircd.git] / src / bancache.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "bancache.h"
18
19 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason)
20 {
21         BanCacheHit *b;
22
23         if (this->BanHash->find(ip) != this->BanHash->end()) // can't have two cache entries on the same IP, sorry..
24                 return NULL;
25
26         b = new BanCacheHit(ServerInstance, ip, type, reason);
27
28         this->BanHash->insert(std::make_pair(ip, b));
29         return b;
30 }
31
32 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
33 {
34         BanCacheHit *b;
35
36         if (this->BanHash->find(ip) != this->BanHash->end()) // can't have two cache entries on the same IP, sorry..
37                 return NULL;
38
39         b = new BanCacheHit(ServerInstance, ip, type, reason, seconds);
40
41         this->BanHash->insert(std::make_pair(ip, b));
42         return b;
43 }
44
45 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
46 {
47         BanCacheHash::iterator i = this->BanHash->find(ip);
48
49         if (i == this->BanHash->end())
50                 return NULL; // free and safe
51         else
52         {
53                 if (ServerInstance->Time() > i->second->Expiry)
54                 {
55                         ServerInstance->Logs->Log("BANCACHE", DEBUG, "Hit on " + ip + " is out of date, removing!");
56                         RemoveHit(i->second);
57                         return NULL; // out of date
58                 }
59
60                 return i->second; // hit.
61         }
62 }
63
64 bool BanCacheManager::RemoveHit(BanCacheHit *b)
65 {
66         BanCacheHash::iterator i;
67
68         if (!b)
69                 return false; // I don't think so.
70
71         i = this->BanHash->find(b->IP);
72
73         if (i == this->BanHash->end())
74         {
75                 // err..
76                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveHit(): I got asked to remove a hit that wasn't in the hash(?)");
77         }
78         else
79         {
80                 this->BanHash->erase(i);
81         }
82
83         delete b;
84         return true;
85 }
86
87 unsigned int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
88 {
89         int removed = 0;
90
91         BanCacheHash::iterator safei;
92
93         if (positive)
94                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type);
95         else
96                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing negative hits for " + type);
97
98         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
99         {
100                 safei = n;
101                 safei++;
102
103                 BanCacheHit *b = n->second;
104
105                 /* Safe to delete items here through iterator 'n' */
106                 if (b->Type == type || !positive) // if removing negative hits, ignore type..
107                 {
108                         if ((positive && !b->Reason.empty()) || b->Reason.empty())
109                         {
110                                 /* we need to remove this one. */
111                                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + b->IP);
112                                 delete b;
113                                 b = NULL;
114                                 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
115                                 removed++;
116                         }
117                 }
118
119                 /* End of safe section */
120                 n = safei;
121         }
122
123
124         return removed;
125 }
126
127 void BanCacheManager::RehashCache()
128 {
129         BanCacheHash* NewHash = new BanCacheHash();
130
131         BanCacheHash::iterator safei;
132         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
133         {
134                 safei = n;
135                 safei++;
136
137                 /* Safe to delete items here through iterator 'n' */
138                 BanCacheHit *b = n->second;
139
140                 if (ServerInstance->Time() > b->Expiry)
141                 {
142                         /* we need to remove this one. */
143                         delete b;
144                         b = NULL;
145                         BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
146                 }
147                 else
148                 {
149                         /* Actually inserts a std::pair */
150                         NewHash->insert(*n);
151                 }
152
153                 /* End of safe section */
154
155                 n = safei;
156         }
157
158         delete BanHash;
159         BanHash = NewHash;
160 }