]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
Remove some debug
[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 #include "configreader.h"
15 #include "users.h"
16 #include "modules.h"
17 #include "wildcard.h"
18 #include "xline.h"
19 #include "socketengine.h"
20 #include "inspircd.h"
21 #include "command_parse.h"
22 #include "cull_list.h"
23
24 void InspIRCd::ProcessUser(userrec* cu)
25 {
26         int result = EAGAIN;
27
28         if (cu->GetFd() == FD_MAGIC_NUMBER)
29                 return;
30
31         if (this->Config->GetIOHook(cu->GetPort()))
32         {
33                 int result2 = 0;
34                 int MOD_RESULT = 0;
35
36                 try
37                 {
38                         MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
39                 }
40                 catch (CoreException& modexcept)
41                 {
42                         this->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
43                 }
44
45                 if (MOD_RESULT < 0)
46                 {
47                         result = -EAGAIN;
48                 }
49                 else
50                 {
51                         result = result2;
52                 }
53         }
54         else
55         {
56                 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
57         }
58
59         if ((result) && (result != -EAGAIN))
60         {
61                 userrec *current;
62                 int currfd;
63                 int floodlines = 0;
64
65                 this->stats->statsRecv += result;
66                 /*
67                  * perform a check on the raw buffer as an array (not a string!) to remove
68                  * character 0 which is illegal in the RFC - replace them with spaces.
69                  * XXX - no garauntee there's not \0's in the middle of the data,
70                  *       and no reason for it to be terminated either. -- Om
71                  */
72
73                 for (int checker = 0; checker < result; checker++)
74                 {
75                         if (ReadBuffer[checker] == 0)
76                                 ReadBuffer[checker] = ' ';
77                 }
78
79                 if (result > 0)
80                         ReadBuffer[result] = '\0';
81
82                 current = cu;
83                 currfd = current->GetFd();
84
85                 // add the data to the users buffer
86                 if (result > 0)
87                 {
88                         if (!current->AddBuffer(ReadBuffer))
89                         {
90                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
91                                 if (current->registered == REG_ALL)
92                                 {
93                                         // Make sure they arn't flooding long lines.
94                                         if (TIME > current->reset_due)
95                                         {
96                                                 current->reset_due = TIME + current->threshold;
97                                                 current->lines_in = 0;
98                                         }
99
100                                         current->lines_in++;
101
102                                         if (current->lines_in > current->flood)
103                                         {
104                                                 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
105                                                 this->SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
106                                                 current->SetWriteError("Excess flood");
107                                                 return;
108                                         }
109                                         else
110                                         {
111                                                 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
112                                                 current->recvq = "";
113                                         }
114                                 }
115                                 else
116                                 {
117                                         this->WriteOpers("*** Excess flood from %s",current->GetIPString());
118                                         this->SNO->WriteToSnoMask('f',"Excess flood from: %s",current->GetIPString());
119                                         XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
120                                         XLines->apply_lines(APPLY_ZLINES);
121                                 }
122
123                                 return;
124                         }
125
126                         if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
127                         {
128                                 if (current->registered == REG_ALL)
129                                 {
130                                         current->SetWriteError("RecvQ exceeded");
131                                 }
132                                 else
133                                 {
134                                         this->WriteOpers("*** Excess flood from %s",current->GetIPString());
135                                         this->SNO->WriteToSnoMask('f',"Excess flood from: %s",current->GetIPString());
136                                         XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
137                                         XLines->apply_lines(APPLY_ZLINES);
138                                 }
139
140                                 return;
141                         }
142
143                         // while there are complete lines to process...
144                         while (current->BufferIsReady())
145                         {
146                                 if (TIME > current->reset_due)
147                                 {
148                                         current->reset_due = TIME + current->threshold;
149                                         current->lines_in = 0;
150                                 }
151
152                                 if (++current->lines_in > current->flood)
153                                 {
154                                         this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
155                                         this->SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
156                                         current->SetWriteError("Excess flood");
157                                         return;
158                                 }
159
160                                 if ((++floodlines > current->flood) && (current->flood != 0))
161                                 {
162                                         if (current->registered == REG_ALL)
163                                         {
164                                                 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
165                                                 SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
166                                                 current->SetWriteError("Excess flood");
167                                         }
168                                         else
169                                         {
170                                                 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
171                                                 XLines->apply_lines(APPLY_ZLINES);
172                                         }
173
174                                         return;
175                                 }
176
177                                 // use GetBuffer to copy single lines into the sanitized string
178                                 std::string single_line = current->GetBuffer();
179                                 current->bytes_in += single_line.length();
180                                 current->cmds_in++;
181                                 if (single_line.length() > 512)
182                                         single_line.resize(512);
183
184                                 EventHandler* old_comp = this->SE->GetRef(currfd);
185
186                                 this->Parser->ProcessBuffer(single_line,current);
187                                 /*
188                                  * look for the user's record in case it's changed... if theyve quit,
189                                  * we cant do anything more with their buffer, so bail.
190                                  * there used to be an ugly, slow loop here. Now we have a reference
191                                  * table, life is much easier (and FASTER)
192                                  */
193                                 EventHandler* new_comp = this->SE->GetRef(currfd);
194
195                                 if (new_comp != old_comp)
196                                         return;
197                         }
198
199                         return;
200                 }
201
202                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
203                 {
204                         this->Log(DEBUG,"killing: %s",cu->nick);
205                         cu->SetWriteError(strerror(errno));
206                         return;
207                 }
208         }
209
210         // result EAGAIN means nothing read
211         else if ((result == EAGAIN) || (result == -EAGAIN))
212         {
213                 /* do nothing */
214         }
215         else if (result == 0)
216         {
217                 cu->SetWriteError("Client exited");
218                 this->Log(DEBUG,"Bailing from client exit");
219                 return;
220         }
221 }
222
223 /**
224  * This function is called once a second from the mainloop.
225  * It is intended to do background checking on all the user structs, e.g.
226  * stuff like ping checks, registration timeouts, etc.
227  */
228 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
229 {
230         /* Is it time yet? */
231         if (TIME < next_call)
232                 return;
233         else
234         {
235                 CullList GlobalGoners(this);
236         
237                 /* Time we actually need to call this again */
238                 const time_t DUMMY_VALUE = 32768;
239                 next_call = TIME + DUMMY_VALUE;
240         
241                 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
242                 for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
243                 {
244                         if (count2 >= this->local_users.size())
245                                 break;
246         
247                         userrec* curr = this->local_users[count2];
248         
249                         if (curr)
250                         {
251                                 /*
252                                  * registration timeout -- didnt send USER/NICK/HOST
253                                  * in the time specified in their connection class.
254                                  */
255                                 if ((TIME > curr->timeout) && (curr->registered != REG_ALL))
256                                 {
257                                         this->Log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
258                                         GlobalGoners.AddItem(curr,"Registration timeout");
259                                         continue;
260                                 }
261                                 else
262                                 {
263                                         if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))
264                                                 next_call = curr->timeout;
265                                 }
266         
267                                 /*
268                                  * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
269                                  * say this user is ok to proceed, fully connect them.
270                                  */
271                                 bool ready = AllModulesReportReady(curr);
272                                 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))
273                                 {
274                                         curr->dns_done = true;
275                                         this->stats->statsDnsBad++;
276                                         curr->FullConnect(&GlobalGoners);
277                                         continue;
278                                 }
279                                 else
280                                 {
281                                         if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))
282                                                 next_call = curr->signon;
283                                 }
284         
285                                 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))
286                                 {
287                                         this->Log(DEBUG,"dns done, registered=3, and modules ready, OK");
288                                         curr->FullConnect(&GlobalGoners);
289                                         continue;
290                                 }
291                                 else
292                                 {
293                                         if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))
294                                                 next_call = curr->signon + this->Config->dns_timeout;
295                                 }
296         
297                                 // It's time to PING this user. Send them a ping.
298                                 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
299                                 {
300                                         // This user didn't answer the last ping, remove them
301                                         if (!curr->lastping)
302                                         {
303                                                 /* Everybody loves boobies. */
304                                                 time_t time = this->Time(false) - (curr->nping - curr->pingmax);
305                                                 std::string boobies = "Ping timeout: " + ConvToStr(time) + " second" + (time > 1 ? "s" : ""); 
306                                                 GlobalGoners.AddItem(curr, boobies);
307                                                 curr->lastping = 1;
308                                                 curr->nping = TIME+curr->pingmax;
309                                                 continue;
310                                         }
311                                         curr->Write("PING :%s",this->Config->ServerName);
312                                         curr->lastping = 0;
313                                         curr->nping = TIME+curr->pingmax;
314                                 }
315                                 else
316                                 {
317                                         if ((curr->registered == REG_ALL) && (next_call > curr->nping))
318                                                 next_call = curr->nping;
319                                 }
320                         }
321                 }
322         
323                 /* If theres nothing to do, trigger in the next second, something might come up */
324                 time_t delta = next_call - TIME;
325                 if (delta == DUMMY_VALUE)
326                 {
327                         next_call = TIME + 1;
328                         delta = 1;
329                 }
330         
331                 /* Remove all the queued users who are due to be quit, free memory used. */
332                 GlobalGoners.Apply();
333         }
334 }
335