1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 /** Stores a cached ban entry.
18 * Each ban has one of these hashed in a hash_map to make for faster removal
19 * of already-banned users in the case that they try to reconnect. As no wildcard
20 * matching is done on these IPs, the speed of the system is improved. These cache
21 * entries expire every few hours, which is a reasonable expiry for any reasonable
24 class CoreExport BanCacheHit : public classbase
27 InspIRCd *ServerInstance;
29 /** Type of cached ban
32 /** Reason, shown as quit message
35 /** IP to match against, no wildcards here (of course)
38 /** Time that the ban expires at
42 BanCacheHit(InspIRCd *Instance, const std::string &ip, const std::string &type, const std::string &reason)
44 ServerInstance = Instance;
46 this->Reason = reason;
48 this->Expiry = time(NULL) + 86400; // a day. this might seem long, but entries will be removed as glines/etc expire.
51 // overridden to allow custom time
52 BanCacheHit(InspIRCd *Instance, const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
54 ServerInstance = Instance;
56 this->Reason = reason;
58 this->Expiry = time(NULL) + seconds;
62 /* A container of ban cache items.
63 * must be defined after class BanCacheHit.
66 typedef nspace::hash_map<std::string, BanCacheHit*, nspace::hash<std::string> > BanCacheHash;
68 typedef nspace::hash_map<std::string, BanCacheHit*, nspace::hash_compare<std::string, std::less<std::string> > > BanCacheHash;
71 /** A manager for ban cache, which allocates and deallocates and checks cached bans.
73 class CoreExport BanCacheManager : public classbase
76 BanCacheHash* BanHash;
77 InspIRCd* ServerInstance;
80 /** Creates and adds a Ban Cache item.
81 * @param ip The IP the item is for.
82 * @param type The type of ban cache item. std::string. .empty() means it's a negative match (user is allowed freely).
83 * @param reason The reason for the ban. Left .empty() if it's a negative match.
85 BanCacheHit *AddHit(const std::string &ip, const std::string &type, const std::string &reason);
87 // Overridden to allow an optional number of seconds before expiry
88 BanCacheHit *AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds);
89 BanCacheHit *GetHit(const std::string &ip);
90 bool RemoveHit(BanCacheHit *b);
92 /** Removes all entries of a given type, either positive or negative. Returns the number of hits removed.
93 * @param type The type of bancache entries to remove (e.g. 'G')
94 * @param positive Remove either positive (true) or negative (false) hits.
96 unsigned int RemoveEntries(const std::string &type, bool positive);
98 BanCacheManager(InspIRCd *Instance)
100 this->ServerInstance = Instance;
101 this->BanHash = new BanCacheHash();