]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/bancache.h
917d2567a4b50ccd2aa1c515002e204da235c5fd
[user/henk/code/inspircd.git] / include / bancache.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __BANCACHE_H
15 #define __BANCACHE_H
16
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
22  * sized network.
23  */
24 class CoreExport BanCacheHit
25 {
26  public:
27         /** Type of cached ban
28          */
29         std::string Type;
30         /** Reason, shown as quit message
31          */
32         std::string Reason;
33         /** IP to match against, no wildcards here (of course)
34          */
35         std::string IP;
36         /** Time that the ban expires at
37          */
38         time_t Expiry;
39
40         BanCacheHit(const std::string &ip, const std::string &type, const std::string &reason)
41         {
42                 this->Type = type;
43                 this->Reason = reason;
44                 this->IP = ip;
45                 this->Expiry = ServerInstance->Time() + 86400; // a day. this might seem long, but entries will be removed as glines/etc expire.
46         }
47
48         // overridden to allow custom time
49         BanCacheHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
50         {
51                 this->Type = type;
52                 this->Reason = reason;
53                 this->IP = ip;
54                 this->Expiry = ServerInstance->Time() + seconds;
55         }
56 };
57
58 /* A container of ban cache items.
59  * must be defined after class BanCacheHit.
60  */
61 #if defined(WINDOWS) && !defined(HASHMAP_DEPRECATED)
62 typedef nspace::hash_map<std::string, BanCacheHit*, nspace::hash_compare<std::string, std::less<std::string> > > BanCacheHash;
63 #else
64 typedef nspace::hash_map<std::string, BanCacheHit*, nspace::hash<std::string> > BanCacheHash;
65 #endif
66
67 /** A manager for ban cache, which allocates and deallocates and checks cached bans.
68  */
69 class CoreExport BanCacheManager
70 {
71  private:
72         BanCacheHash* BanHash;
73  public:
74
75         /** Creates and adds a Ban Cache item.
76          * @param ip The IP the item is for.
77          * @param type The type of ban cache item. std::string. .empty() means it's a negative match (user is allowed freely).
78          * @param reason The reason for the ban. Left .empty() if it's a negative match.
79          */
80         BanCacheHit *AddHit(const std::string &ip, const std::string &type, const std::string &reason);
81
82         // Overridden to allow an optional number of seconds before expiry
83         BanCacheHit *AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds);
84         BanCacheHit *GetHit(const std::string &ip);
85         bool RemoveHit(BanCacheHit *b);
86
87         /** Removes all entries of a given type, either positive or negative. Returns the number of hits removed.
88          * @param type The type of bancache entries to remove (e.g. 'G')
89          * @param positive Remove either positive (true) or negative (false) hits.
90          */
91         unsigned int RemoveEntries(const std::string &type, bool positive);
92
93         BanCacheManager()
94         {
95                 this->BanHash = new BanCacheHash();
96         }
97         ~BanCacheManager();
98         void RehashCache();
99 };
100
101 #endif