]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
a3c8bc3052c364c7954d0071bbed6dc89c02d6a6
[user/henk/code/inspircd.git] / src / userprocess.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 /* $Core: libIRCDuserprocess */
15
16 #include "inspircd.h"
17 #include "wildcard.h"
18 #include "xline.h"
19 #include "socketengine.h"
20 #include "command_parse.h"
21
22 void FloodQuitUserHandler::Call(User* current)
23 {
24         Server->Log(DEFAULT,"Excess flood from: %s@%s", current->ident, current->host);
25         Server->SNO->WriteToSnoMask('f',"Excess flood from: %s%s%s@%s",
26                         current->registered == REG_ALL ? current->nick : "",
27                         current->registered == REG_ALL ? "!" : "", current->ident, current->host);
28         User::QuitUser(Server, current, "Excess flood");
29         if (current->registered != REG_ALL)
30         {
31                 Server->XLines->add_zline(120, Server->Config->ServerName, "Flood from unregistered connection", current->GetIPString());
32                 Server->XLines->apply_lines(APPLY_ZLINES);
33         }
34 }
35
36 void ProcessUserHandler::Call(User* cu)
37 {
38         int result = EAGAIN;
39
40         if (cu->GetFd() == FD_MAGIC_NUMBER)
41                 return;
42
43         char* ReadBuffer = Server->GetReadBuffer();
44
45         if (Server->Config->GetIOHook(cu->GetPort()))
46         {
47                 int result2 = 0;
48                 int MOD_RESULT = 0;
49
50                 try
51                 {
52                         MOD_RESULT = Server->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,Server->Config->NetBufferSize,result2);
53                 }
54                 catch (CoreException& modexcept)
55                 {
56                         Server->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
57                 }
58
59                 if (MOD_RESULT < 0)
60                 {
61                         result = -EAGAIN;
62                 }
63                 else
64                 {
65                         result = result2;
66                 }
67         }
68         else
69         {
70                 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
71         }
72
73         if ((result) && (result != -EAGAIN))
74         {
75                 User *current;
76                 int currfd;
77
78                 Server->stats->statsRecv += result;
79                 /*
80                  * perform a check on the raw buffer as an array (not a string!) to remove
81                  * character 0 which is illegal in the RFC - replace them with spaces.
82                  */
83
84                 for (int checker = 0; checker < result; checker++)
85                 {
86                         if (ReadBuffer[checker] == 0)
87                                 ReadBuffer[checker] = ' ';
88                 }
89
90                 if (result > 0)
91                         ReadBuffer[result] = '\0';
92
93                 current = cu;
94                 currfd = current->GetFd();
95
96                 // add the data to the users buffer
97                 if (result > 0)
98                 {
99                         if (!current->AddBuffer(ReadBuffer))
100                         {
101                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
102                                 if (current->registered == REG_ALL)
103                                 {
104                                         // Make sure they arn't flooding long lines.
105                                         if (Server->Time() > current->reset_due)
106                                         {
107                                                 current->reset_due = Server->Time() + current->MyClass->GetThreshold();
108                                                 current->lines_in = 0;
109                                         }
110
111                                         current->lines_in++;
112
113                                         if (current->MyClass->GetFlood() && current->lines_in > current->MyClass->GetFlood())
114                                                 Server->FloodQuitUser(current);
115                                         else
116                                         {
117                                                 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over %d chars) Please shorten it.", current->nick, MAXBUF-2);
118                                                 current->recvq.clear();
119                                         }
120                                 }
121                                 else
122                                         Server->FloodQuitUser(current);
123
124                                 return;
125                         }
126
127                         /* If user is over penalty, dont process here, just build up */
128                         if (!current->OverPenalty)
129                                 Server->Parser->DoLines(current);
130
131                         return;
132                 }
133
134                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
135                 {
136                         User::QuitUser(Server, cu, errno ? strerror(errno) : "EOF from client");
137                         return;
138                 }
139         }
140
141         // result EAGAIN means nothing read
142         else if ((result == EAGAIN) || (result == -EAGAIN))
143         {
144                 /* do nothing */
145         }
146         else if (result == 0)
147         {
148                 User::QuitUser(Server, cu, "Connection closed");
149                 return;
150         }
151 }
152
153 /**
154  * This function is called once a second from the mainloop.
155  * It is intended to do background checking on all the user structs, e.g.
156  * stuff like ping checks, registration timeouts, etc.
157  */
158 void InspIRCd::DoBackgroundUserStuff()
159 {
160         /*
161          * loop over all local users..
162          */
163         for (std::vector<User*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
164         {
165                 User *curr = *count2;
166
167                 if (curr->Penalty)
168                 {
169                         curr->Penalty--;
170                         if (curr->Penalty < 10)
171                                 Parser->DoLines(curr, true);
172                 }
173
174                 if (curr->OverPenalty)
175                 {
176                         if (curr->sendq.empty())
177                                 curr->OverPenalty = false;
178                 }
179
180                 if ((curr->registered != REG_ALL) && (TIME > curr->timeout))
181                 {
182                         /*
183                          * registration timeout -- didnt send USER/NICK/HOST
184                          * in the time specified in their connection class.
185                          */
186                         curr->muted = true;
187                         User::QuitUser(this, curr, "Registration timeout");
188                         continue;
189                 }
190
191                 /*
192                  * `ready` means that the user has provided NICK/USER(/PASS), and all modules agree
193                  * that the user is okay to proceed. The one thing we are then waiting for now is DNS...
194                  */
195                 bool ready = ((curr->registered == REG_NICKUSER) && AllModulesReportReady(curr));
196
197                 if (ready)
198                 {
199                         if (curr->dns_done)
200                         {
201                                 /* DNS passed, connect the user */
202                                 curr->FullConnect();
203                                 continue;
204                         }
205                 }
206
207                 // It's time to PING this user. Send them a ping.
208                 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
209                 {
210                         // This user didn't answer the last ping, remove them
211                         if (!curr->lastping)
212                         {
213                                 time_t time = this->Time(false) - (curr->nping - curr->MyClass->GetPingTime());
214                                 char message[MAXBUF];
215                                 snprintf(message, MAXBUF, "Ping timeout: %ld second%s", (long)time, time > 1 ? "s" : "");
216                                 curr->muted = true;
217                                 curr->lastping = 1;
218                                 curr->nping = TIME + curr->MyClass->GetPingTime();
219                                 User::QuitUser(this, curr, message);
220                                 continue;
221                         }
222                         curr->Write("PING :%s",this->Config->ServerName);
223                         curr->lastping = 0;
224                         curr->nping = TIME  +curr->MyClass->GetPingTime();
225                 }
226         }
227 }
228