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