]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/bancache.cpp
It compiles; ship it.
[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
24         if (this->BanHash->find(ip) != this->BanHash->end()) // can't have two cache entries on the same IP, sorry..
25                 return NULL;
26
27         b = new BanCacheHit(ServerInstance, ip, type, reason);
28
29         this->BanHash->insert(std::make_pair(ip, b));
30         return b;
31 }
32
33 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
34 {
35         BanCacheHash::iterator i = this->BanHash->find(ip);
36
37         if (i == this->BanHash->end())
38                 return NULL; // free and safe
39         else
40                 return i->second; // hit.
41 }
42
43 bool BanCacheManager::RemoveHit(BanCacheHit *b)
44 {
45         BanCacheHash::iterator i;
46
47         if (!b)
48                 return false; // I don't think so.
49
50         i = this->BanHash->find(b->IP);
51
52         if (i == this->BanHash->end())
53         {
54                 // err..
55                 ServerInstance->Log(DEBUG, "BanCacheManager::RemoveHit(): I got asked to remove a hit that wasn't in the hash(?)");
56         }
57         else
58         {
59                 this->BanHash->erase(i);
60         }
61
62         delete b;
63         return true;
64 }
65
66 int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
67 {
68         int removed = 0;
69
70         BanCacheHash::iterator safei;
71
72         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
73         {
74                 safei = n;
75                 safei++;
76
77                 BanCacheHit *b = n->second;
78
79                 /* Safe to delete items here through iterator 'n' */
80                 if (b->Type == type)
81                 {
82                         if ((positive && !b->Reason.empty()) || b->Reason.empty())
83                         {
84                                 /* we need to remove this one. */
85                                 delete b;
86                                 b = NULL;
87                                 BanHash->erase(n);
88                                 removed++;
89                         }
90                 }
91
92                 /* End of safe section */
93                 n = safei;
94         }
95
96
97         return removed;
98 }
99
100 void BanCacheManager::RehashCache()
101 {
102         BanCacheHash* NewHash = new BanCacheHash();
103
104         BanCacheHash::iterator safei;
105         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
106         {
107                 safei = n;
108                 safei++;
109
110                 /* Safe to delete items here through iterator 'n' */
111
112                 /* Actually inserts a std::pair */
113                 NewHash->insert(*n);
114
115                 /* End of safe section */
116
117                 n = safei;
118         }
119
120         delete BanHash;
121         BanHash = NewHash;
122 }