]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connectban.cpp
a1ff0816c2baa6ab45c4cc208b2d6f1843914904
[user/henk/code/inspircd.git] / src / modules / m_connectban.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 #include "inspircd.h"
15 #include "xline.h"
16
17 /* $ModDesc: Throttles the connections of any users who try connect flood */
18
19 class ModuleQuitBan : public Module
20 {
21  private:
22         clonemap connects;
23         unsigned int threshold;
24         unsigned int banduration;
25  public:
26         ModuleQuitBan(InspIRCd* Me) : Module(Me)
27         {
28                 Implementation eventlist[] = { I_OnUserConnect, I_OnGarbageCollect, I_OnRehash };
29                 ServerInstance->Modules->Attach(eventlist, this, 3);
30                 OnRehash(NULL, "");
31         }
32         
33         virtual ~ModuleQuitBan()
34         {
35         }
36         
37         virtual Version GetVersion()
38         {
39                 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
40         }
41
42         virtual void OnRehash(User* user, const std::string &parameter)
43         {
44                 ConfigReader Conf(ServerInstance);
45                 std::string duration;
46
47                 threshold = Conf.ReadInteger("connectban", "threshold", 0, true);
48
49                 if (threshold == 0)
50                         threshold = 10;
51
52                 duration = Conf.ReadValue("connectban", "duration", 0, true);
53
54                 if (duration.empty())
55                         duration = "10m";
56
57                 banduration = ServerInstance->Duration(duration);
58         }
59
60         virtual void OnUserConnect(User *u)
61         {
62                 clonemap::iterator i = connects.find(u->GetIPString());
63
64                 if (i != connects.end())
65                 {
66                         i->second++;
67                         ServerInstance->Logs->Log("m_connectban",DEBUG, "Count for IP is now %d", i->second);
68
69                         if (i->second >= threshold)
70                         {
71                                 // Create zline for set duration.
72                                 ZLine* zl = new ZLine(ServerInstance, ServerInstance->Time(), banduration, ServerInstance->Config->ServerName, "Connect flooding", u->GetIPString());
73                                 if (ServerInstance->XLines->AddLine(zl,NULL))
74                                         ServerInstance->XLines->ApplyLines();
75                                 else
76                                         delete zl;
77
78                                 ServerInstance->SNO->WriteToSnoMask('x', "Connect flooding from IP %s (%d)", u->GetIPString(), threshold);
79                                 connects.erase(i);
80                         }
81                 }
82                 else
83                 {
84                         connects[u->GetIPString()] = 1;
85                         ServerInstance->Logs->Log("m_quitban",DEBUG, "Added new record");
86                 }
87         }
88
89         virtual void OnGarbageCollect()
90         {
91                 ServerInstance->Logs->Log("m_quitban",DEBUG, "Clearing map.");
92                 connects.clear();
93         }
94 };
95
96 MODULE_INIT(ModuleQuitBan)