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