]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
Add fine-grained command flood controls
[user/henk/code/inspircd.git] / src / userprocess.cpp
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 /* $Core */
15
16 #include "inspircd.h"
17 #include "xline.h"
18 #include "socketengine.h"
19 #include "command_parse.h"
20
21 void FloodQuitUserHandler::Call(User* current)
22 {
23         ServerInstance->Logs->Log("USERS",DEFAULT,"Excess flood from: %s@%s", current->ident.c_str(), current->host.c_str());
24         ServerInstance->SNO->WriteToSnoMask('f',"Excess flood from: %s%s%s@%s",
25                         current->registered == REG_ALL ? current->nick.c_str() : "",
26                         current->registered == REG_ALL ? "!" : "", current->ident.c_str(), current->host.c_str());
27         ServerInstance->Users->QuitUser(current, "Excess flood");
28
29         if (current->registered != REG_ALL)
30         {
31                 ZLine* zl = new ZLine(ServerInstance->Time(), 0, ServerInstance->Config->ServerName, "Flood from unregistered connection", current->GetIPString());
32                 if (ServerInstance->XLines->AddLine(zl,NULL))
33                         ServerInstance->XLines->ApplyLines();
34                 else
35                         delete zl;
36         }
37 }
38
39 /**
40  * This function is called once a second from the mainloop.
41  * It is intended to do background checking on all the user structs, e.g.
42  * stuff like ping checks, registration timeouts, etc.
43  */
44 void InspIRCd::DoBackgroundUserStuff()
45 {
46         /*
47          * loop over all local users..
48          */
49         std::vector<LocalUser*>::reverse_iterator count2 = this->Users->local_users.rbegin();
50         while (count2 != this->Users->local_users.rend())
51         {
52                 LocalUser *curr = *count2;
53                 count2++;
54
55                 if (curr->quitting)
56                         continue;
57
58                 if (curr->CommandFloodPenalty)
59                 {
60                         unsigned int rate = curr->MyClass->GetCommandRate();
61                         if (curr->CommandFloodPenalty > rate)
62                                 curr->CommandFloodPenalty -= rate;
63                         else
64                                 curr->CommandFloodPenalty = 0;
65                         curr->eh.OnDataReady();
66                 }
67
68                 switch (curr->registered)
69                 {
70                         case REG_ALL:
71                                 if (TIME > curr->nping)
72                                 {
73                                         // This user didn't answer the last ping, remove them
74                                         if (!curr->lastping)
75                                         {
76                                                 time_t time = this->Time() - (curr->nping - curr->MyClass->GetPingTime());
77                                                 char message[MAXBUF];
78                                                 snprintf(message, MAXBUF, "Ping timeout: %ld second%s", (long)time, time > 1 ? "s" : "");
79                                                 curr->lastping = 1;
80                                                 curr->nping = TIME + curr->MyClass->GetPingTime();
81                                                 this->Users->QuitUser(curr, message);
82                                                 continue;
83                                         }
84
85                                         curr->Write("PING :%s",this->Config->ServerName.c_str());
86                                         curr->lastping = 0;
87                                         curr->nping = TIME  +curr->MyClass->GetPingTime();
88                                 }
89                                 break;
90                         case REG_NICKUSER:
91                                 if (AllModulesReportReady(curr) && curr->dns_done)
92                                 {
93                                         /* User has sent NICK/USER, modules are okay, DNS finished. */
94                                         curr->FullConnect();
95                                         continue;
96                                 }
97                                 break;
98                 }
99
100                 if (curr->registered != REG_ALL && (TIME > (curr->age + curr->MyClass->GetRegTimeout())))
101                 {
102                         /*
103                          * registration timeout -- didnt send USER/NICK/HOST
104                          * in the time specified in their connection class.
105                          */
106                         this->Users->QuitUser(curr, "Registration timeout");
107                         continue;
108                 }
109         }
110 }
111