1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 /* Now with added unF! ;) */
21 #include "configreader.h"
26 #include "socketengine.h"
27 #include "userprocess.h"
29 #include "command_parse.h"
30 #include "cull_list.h"
32 void InspIRCd::ProcessUser(userrec* cu)
36 if (cu->GetFd() == FD_MAGIC_NUMBER)
39 if (this->Config->GetIOHook(cu->GetPort()))
46 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
48 catch (ModuleException& modexcept)
50 this->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
64 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
67 this->Log(DEBUG,"Read result: %d",result);
69 if ((result) && (result != -EAGAIN))
75 this->stats->statsRecv += result;
77 * perform a check on the raw buffer as an array (not a string!) to remove
78 * character 0 which is illegal in the RFC - replace them with spaces.
79 * XXX - no garauntee there's not \0's in the middle of the data,
80 * and no reason for it to be terminated either. -- Om
83 for (int checker = 0; checker < result; checker++)
85 if (ReadBuffer[checker] == 0)
86 ReadBuffer[checker] = ' ';
90 ReadBuffer[result] = '\0';
93 currfd = current->GetFd();
95 // add the data to the users buffer
98 if (!current->AddBuffer(ReadBuffer))
100 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
101 if (current->registered == REG_ALL)
103 // Make sure they arn't flooding long lines.
104 if (TIME > current->reset_due)
106 current->reset_due = TIME + current->threshold;
107 current->lines_in = 0;
112 if (current->lines_in > current->flood)
114 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
115 this->SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
116 userrec::QuitUser(this, current,"Excess flood");
121 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
127 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
128 this->SNO->WriteToSnoMask('f',"Excess flood from: %s",current->GetIPString());
129 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
130 XLines->apply_lines(APPLY_ZLINES);
136 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
138 if (current->registered == REG_ALL)
140 userrec::QuitUser(this, current,"RecvQ exceeded");
144 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
145 this->SNO->WriteToSnoMask('f',"Excess flood from: %s",current->GetIPString());
146 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
147 XLines->apply_lines(APPLY_ZLINES);
153 // while there are complete lines to process...
154 while (current->BufferIsReady())
156 if (TIME > current->reset_due)
158 current->reset_due = TIME + current->threshold;
159 current->lines_in = 0;
162 if (++current->lines_in > current->flood)
164 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
165 this->SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
166 userrec::QuitUser(this, current,"Excess flood");
170 if ((++floodlines > current->flood) && (current->flood != 0))
172 if (current->registered == REG_ALL)
174 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
175 SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
176 userrec::QuitUser(this, current,"Excess flood");
180 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
181 XLines->apply_lines(APPLY_ZLINES);
187 // use GetBuffer to copy single lines into the sanitized string
188 std::string single_line = current->GetBuffer();
189 current->bytes_in += single_line.length();
191 if (single_line.length() > 512)
192 single_line.resize(512);
194 EventHandler* old_comp = this->SE->GetRef(currfd);
196 this->Parser->ProcessBuffer(single_line,current);
198 * look for the user's record in case it's changed... if theyve quit,
199 * we cant do anything more with their buffer, so bail.
200 * there used to be an ugly, slow loop here. Now we have a reference
201 * table, life is much easier (and FASTER)
203 EventHandler* new_comp = this->SE->GetRef(currfd);
204 if (old_comp != new_comp)
210 /* The user is still here, flush their buffer */
211 current->FlushWriteBuf();
218 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
220 this->Log(DEBUG,"killing: %s",cu->nick);
221 userrec::QuitUser(this,cu,strerror(errno));
226 // result EAGAIN means nothing read
227 else if ((result == EAGAIN) || (result == -EAGAIN))
231 else if (result == 0)
233 this->Log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
234 userrec::QuitUser(this,cu,"Client exited");
235 this->Log(DEBUG,"Bailing from client exit");
241 * This function is called once a second from the mainloop.
242 * It is intended to do background checking on all the user structs, e.g.
243 * stuff like ping checks, registration timeouts, etc. This function is
244 * also responsible for checking if InspSocket derived classes are timed out.
246 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
248 /* Is it time yet? */
249 if (TIME < next_call)
252 CullList GlobalGoners(this);
254 /* Time we actually need to call this again */
255 const time_t DUMMY_VALUE = 32768;
256 next_call = TIME + DUMMY_VALUE;
258 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
259 for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
261 if (count2 >= this->local_users.size())
264 userrec* curr = this->local_users[count2];
269 * registration timeout -- didnt send USER/NICK/HOST
270 * in the time specified in their connection class.
272 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
274 this->Log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
275 //ZapThisDns(curr->fd);
276 GlobalGoners.AddItem(curr,"Registration timeout");
281 if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))
282 next_call = curr->timeout;
286 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
287 * say this user is ok to proceed, fully connect them.
289 bool ready = AllModulesReportReady(curr);
290 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))
292 curr->dns_done = true;
293 //ZapThisDns(curr->fd);
294 this->stats->statsDnsBad++;
295 curr->FullConnect(&GlobalGoners);
300 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))
301 next_call = curr->signon;
304 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))
306 this->Log(DEBUG,"dns done, registered=3, and modules ready, OK");
307 curr->FullConnect(&GlobalGoners);
308 //ZapThisDns(curr->fd);
313 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))
314 next_call = curr->signon + this->Config->dns_timeout;
317 // It's time to PING this user. Send them a ping.
318 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
320 // This user didn't answer the last ping, remove them
323 /* Everybody loves boobies. */
324 time_t time = this->Time() - (curr->nping - curr->pingmax);
325 std::string boobies = "Ping timeout: " + ConvToStr(time) + " second" + (time > 1 ? "s" : "");
326 GlobalGoners.AddItem(curr, boobies);
328 curr->nping = TIME+curr->pingmax;
331 curr->Write("PING :%s",this->Config->ServerName);
333 curr->nping = TIME+curr->pingmax;
337 if ((curr->registered == REG_ALL) && (next_call > curr->nping))
338 next_call = curr->nping;
342 * We can flush the write buffer as the last thing we do, because if they
343 * match any of the above conditions its no use flushing their buffer anyway.
346 curr->FlushWriteBuf();
347 if (*curr->GetWriteError())
349 GlobalGoners.AddItem(curr,curr->GetWriteError());
356 /* If theres nothing to do, trigger in the next second, something might come up */
357 time_t delta = next_call - TIME;
358 if (delta == DUMMY_VALUE)
360 next_call = TIME + 1;
364 this->Log(DEBUG,"Time now is %lu, next time we actually need to call this is %lu (%lu secs from now)", TIME, next_call, delta);
367 /* Remove all the queued users who are due to be quit, free memory used. */
368 GlobalGoners.Apply();