bancache.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "inspircd.h"
00017 #include "bancache.h"
00018
00019 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason)
00020 {
00021 BanCacheHit *b;
00022
00023 if (this->BanHash->find(ip) != this->BanHash->end())
00024 return NULL;
00025
00026 b = new BanCacheHit(ServerInstance, ip, type, reason);
00027
00028 this->BanHash->insert(std::make_pair(ip, b));
00029 return b;
00030 }
00031
00032 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
00033 {
00034 BanCacheHit *b;
00035
00036 if (this->BanHash->find(ip) != this->BanHash->end())
00037 return NULL;
00038
00039 b = new BanCacheHit(ServerInstance, ip, type, reason, seconds);
00040
00041 this->BanHash->insert(std::make_pair(ip, b));
00042 return b;
00043 }
00044
00045 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
00046 {
00047 BanCacheHash::iterator i = this->BanHash->find(ip);
00048
00049 if (i == this->BanHash->end())
00050 return NULL;
00051 else
00052 {
00053 if (time(NULL) > i->second->Expiry)
00054 {
00055 ServerInstance->Logs->Log("BANCACHE", DEBUG, "Hit on " + ip + " is out of date, removing!");
00056 RemoveHit(i->second);
00057 return NULL;
00058 }
00059
00060 return i->second;
00061 }
00062 }
00063
00064 bool BanCacheManager::RemoveHit(BanCacheHit *b)
00065 {
00066 BanCacheHash::iterator i;
00067
00068 if (!b)
00069 return false;
00070
00071 i = this->BanHash->find(b->IP);
00072
00073 if (i == this->BanHash->end())
00074 {
00075
00076 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveHit(): I got asked to remove a hit that wasn't in the hash(?)");
00077 }
00078 else
00079 {
00080 this->BanHash->erase(i);
00081 }
00082
00083 delete b;
00084 return true;
00085 }
00086
00087 unsigned int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
00088 {
00089 int removed = 0;
00090
00091 BanCacheHash::iterator safei;
00092
00093 if (positive)
00094 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type);
00095 else
00096 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing negative hits for " + type);
00097
00098 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
00099 {
00100 safei = n;
00101 safei++;
00102
00103 BanCacheHit *b = n->second;
00104
00105
00106 if (b->Type == type || !positive)
00107 {
00108 if ((positive && !b->Reason.empty()) || b->Reason.empty())
00109 {
00110
00111 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + b->IP);
00112 delete b;
00113 b = NULL;
00114 BanHash->erase(n);
00115 removed++;
00116 }
00117 }
00118
00119
00120 n = safei;
00121 }
00122
00123
00124 return removed;
00125 }
00126
00127 void BanCacheManager::RehashCache()
00128 {
00129 BanCacheHash* NewHash = new BanCacheHash();
00130
00131 BanCacheHash::iterator safei;
00132 for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
00133 {
00134 safei = n;
00135 safei++;
00136
00137
00138 BanCacheHit *b = n->second;
00139
00140 if (time(NULL) > b->Expiry)
00141 {
00142
00143 delete b;
00144 b = NULL;
00145 BanHash->erase(n);
00146 }
00147 else
00148 {
00149
00150 NewHash->insert(*n);
00151 }
00152
00153
00154
00155 n = safei;
00156 }
00157
00158 delete BanHash;
00159 BanHash = NewHash;
00160 }