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);
205 if (new_comp != old_comp)
212 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
214 this->Log(DEBUG,"killing: %s",cu->nick);
215 userrec::QuitUser(this,cu,strerror(errno));
220 // result EAGAIN means nothing read
221 else if ((result == EAGAIN) || (result == -EAGAIN))
225 else if (result == 0)
227 this->Log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
228 userrec::QuitUser(this,cu,"Client exited");
229 this->Log(DEBUG,"Bailing from client exit");
235 * This function is called once a second from the mainloop.
236 * It is intended to do background checking on all the user structs, e.g.
237 * stuff like ping checks, registration timeouts, etc. This function is
238 * also responsible for checking if InspSocket derived classes are timed out.
240 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
242 /* Is it time yet? */
243 if (TIME < next_call)
246 CullList GlobalGoners(this);
248 /* Time we actually need to call this again */
249 const time_t DUMMY_VALUE = 32768;
250 next_call = TIME + DUMMY_VALUE;
252 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
253 for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
255 if (count2 >= this->local_users.size())
258 userrec* curr = this->local_users[count2];
263 * registration timeout -- didnt send USER/NICK/HOST
264 * in the time specified in their connection class.
266 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
268 this->Log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
269 //ZapThisDns(curr->fd);
270 GlobalGoners.AddItem(curr,"Registration timeout");
275 if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))
276 next_call = curr->timeout;
280 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
281 * say this user is ok to proceed, fully connect them.
283 bool ready = AllModulesReportReady(curr);
284 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))
286 curr->dns_done = true;
287 //ZapThisDns(curr->fd);
288 this->stats->statsDnsBad++;
289 curr->FullConnect(&GlobalGoners);
294 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))
295 next_call = curr->signon;
298 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))
300 this->Log(DEBUG,"dns done, registered=3, and modules ready, OK");
301 curr->FullConnect(&GlobalGoners);
302 //ZapThisDns(curr->fd);
307 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))
308 next_call = curr->signon + this->Config->dns_timeout;
311 // It's time to PING this user. Send them a ping.
312 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
314 // This user didn't answer the last ping, remove them
317 /* Everybody loves boobies. */
318 time_t time = this->Time() - (curr->nping - curr->pingmax);
319 std::string boobies = "Ping timeout: " + ConvToStr(time) + " second" + (time > 1 ? "s" : "");
320 GlobalGoners.AddItem(curr, boobies);
322 curr->nping = TIME+curr->pingmax;
325 curr->Write("PING :%s",this->Config->ServerName);
327 curr->nping = TIME+curr->pingmax;
331 if ((curr->registered == REG_ALL) && (next_call > curr->nping))
332 next_call = curr->nping;
336 * We can flush the write buffer as the last thing we do, because if they
337 * match any of the above conditions its no use flushing their buffer anyway.
340 curr->FlushWriteBuf();
341 if (*curr->GetWriteError())
343 GlobalGoners.AddItem(curr,curr->GetWriteError());
350 /* If theres nothing to do, trigger in the next second, something might come up */
351 time_t delta = next_call - TIME;
352 if (delta == DUMMY_VALUE)
354 next_call = TIME + 1;
358 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);
361 /* Remove all the queued users who are due to be quit, free memory used. */
362 GlobalGoners.Apply();