]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/bancache.h
win: Last part of support required for VS2010:
[user/henk/code/inspircd.git] / include / bancache.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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 : public classbase
25 {
26  private:
27         InspIRCd *ServerInstance;
28  public:
29         /** Type of cached ban
30          */
31         std::string Type;
32         /** Reason, shown as quit message
33          */
34         std::string Reason;
35         /** IP to match against, no wildcards here (of course)
36          */
37         std::string IP;
38         /** Time that the ban expires at
39          */
40         time_t Expiry;
41
42         BanCacheHit(InspIRCd *Instance, const std::string &ip, const std::string &type, const std::string &reason)
43         {
44                 ServerInstance = Instance;
45                 this->Type = type;
46                 this->Reason = reason;
47                 this->IP = ip;
48                 this->Expiry = ServerInstance->Time() + 86400; // a day. this might seem long, but entries will be removed as glines/etc expire.
49         }
50
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)
53         {
54                 ServerInstance = Instance;
55                 this->Type = type;
56                 this->Reason = reason;
57                 this->IP = ip;
58                 this->Expiry = ServerInstance->Time() + seconds;
59         }
60 };
61
62 /* A container of ban cache items.
63  * must be defined after class BanCacheHit.
64  */
65 #if defined(WINDOWS) && !defined(HASHMAP_DEPRECATED)
66 typedef nspace::hash_map<std::string, BanCacheHit*, nspace::hash_compare<std::string, std::less<std::string> > > BanCacheHash;
67 #else
68 typedef nspace::hash_map<std::string, BanCacheHit*, nspace::hash<std::string> > BanCacheHash;
69 #endif
70
71 /** A manager for ban cache, which allocates and deallocates and checks cached bans.
72  */
73 class CoreExport BanCacheManager : public classbase
74 {
75  private:
76         BanCacheHash* BanHash;
77         InspIRCd* ServerInstance;
78  public:
79
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.
84          */
85         BanCacheHit *AddHit(const std::string &ip, const std::string &type, const std::string &reason);
86
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);
91
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.
95          */
96         unsigned int RemoveEntries(const std::string &type, bool positive);
97
98         BanCacheManager(InspIRCd *Instance)
99         {
100                 this->ServerInstance = Instance;
101                 this->BanHash = new BanCacheHash();
102         }
103
104         void RehashCache();
105 };
106
107 #endif