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