]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/bancache.cpp
Add bancache expiry stuff, currently records expire 60 seconds after creation. Live...
[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         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
81         {
82                 safei = n;
83                 safei++;
84
85                 BanCacheHit *b = n->second;
86
87                 /* Safe to delete items here through iterator 'n' */
88                 if (b->Type == type)
89                 {
90                         if ((positive && !b->Reason.empty()) || b->Reason.empty())
91                         {
92                                 /* we need to remove this one. */
93                                 delete b;
94                                 b = NULL;
95                                 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
96                                 removed++;
97                         }
98                 }
99
100                 /* End of safe section */
101                 n = safei;
102         }
103
104
105         return removed;
106 }
107
108 void BanCacheManager::RehashCache()
109 {
110         BanCacheHash* NewHash = new BanCacheHash();
111
112         BanCacheHash::iterator safei;
113         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
114         {
115                 safei = n;
116                 safei++;
117
118                 /* Safe to delete items here through iterator 'n' */
119                 BanCacheHit *b = n->second;
120
121                 if (time(NULL) > b->Expiry)
122                 {
123                         /* we need to remove this one. */
124                         delete b;
125                         b = NULL;
126                         BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
127                 }
128                 else
129                 {
130                         /* Actually inserts a std::pair */
131                         NewHash->insert(*n);
132                 }
133
134                 /* End of safe section */
135
136                 n = safei;
137         }
138
139         delete BanHash;
140         BanHash = NewHash;
141 }