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"
28 #include "command_parse.h"
29 #include "cull_list.h"
31 void InspIRCd::ProcessUser(userrec* cu)
35 if (cu->GetFd() == FD_MAGIC_NUMBER)
38 if (this->Config->GetIOHook(cu->GetPort()))
45 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
47 catch (ModuleException& modexcept)
49 this->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
63 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
66 this->Log(DEBUG,"Read result: %d",result);
68 if ((result) && (result != -EAGAIN))
74 this->stats->statsRecv += result;
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
82 for (int checker = 0; checker < result; checker++)
84 if (ReadBuffer[checker] == 0)
85 ReadBuffer[checker] = ' ';
89 ReadBuffer[result] = '\0';
92 currfd = current->GetFd();
94 // add the data to the users buffer
97 if (!current->AddBuffer(ReadBuffer))
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)
102 // Make sure they arn't flooding long lines.
103 if (TIME > current->reset_due)
105 current->reset_due = TIME + current->threshold;
106 current->lines_in = 0;
111 if (current->lines_in > current->flood)
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 userrec::QuitUser(this, current,"Excess flood");
120 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
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);
135 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
137 if (current->registered == REG_ALL)
139 userrec::QuitUser(this, current,"RecvQ exceeded");
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);
152 // while there are complete lines to process...
153 while (current->BufferIsReady())
155 if (TIME > current->reset_due)
157 current->reset_due = TIME + current->threshold;
158 current->lines_in = 0;
161 if (++current->lines_in > current->flood)
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 userrec::QuitUser(this, current,"Excess flood");
169 if ((++floodlines > current->flood) && (current->flood != 0))
171 if (current->registered == REG_ALL)
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 userrec::QuitUser(this, current,"Excess flood");
179 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
180 XLines->apply_lines(APPLY_ZLINES);
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();
190 if (single_line.length() > 512)
191 single_line.resize(512);
193 EventHandler* old_comp = this->SE->GetRef(currfd);
195 this->Parser->ProcessBuffer(single_line,current);
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)
202 EventHandler* new_comp = this->SE->GetRef(currfd);
204 if (new_comp != old_comp)
211 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
213 this->Log(DEBUG,"killing: %s",cu->nick);
214 userrec::QuitUser(this,cu,strerror(errno));
219 // result EAGAIN means nothing read
220 else if ((result == EAGAIN) || (result == -EAGAIN))
224 else if (result == 0)
226 userrec::QuitUser(this,cu,"Client exited");
227 this->Log(DEBUG,"Bailing from client exit");
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.
237 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
239 /* Is it time yet? */
240 if (TIME < next_call)
244 CullList GlobalGoners(this);
246 /* Time we actually need to call this again */
247 const time_t DUMMY_VALUE = 32768;
248 next_call = TIME + DUMMY_VALUE;
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++)
253 if (count2 >= this->local_users.size())
256 userrec* curr = this->local_users[count2];
261 * registration timeout -- didnt send USER/NICK/HOST
262 * in the time specified in their connection class.
264 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
266 this->Log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
267 GlobalGoners.AddItem(curr,"Registration timeout");
272 if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))
273 next_call = curr->timeout;
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.
280 bool ready = AllModulesReportReady(curr);
281 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))
283 curr->dns_done = true;
284 this->stats->statsDnsBad++;
285 curr->FullConnect(&GlobalGoners);
290 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))
291 next_call = curr->signon;
294 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))
296 this->Log(DEBUG,"dns done, registered=3, and modules ready, OK");
297 curr->FullConnect(&GlobalGoners);
302 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))
303 next_call = curr->signon + this->Config->dns_timeout;
306 // It's time to PING this user. Send them a ping.
307 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
309 // This user didn't answer the last ping, remove them
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);
317 curr->nping = TIME+curr->pingmax;
320 curr->Write("PING :%s",this->Config->ServerName);
322 curr->nping = TIME+curr->pingmax;
326 if ((curr->registered == REG_ALL) && (next_call > curr->nping))
327 next_call = curr->nping;
331 * We can flush the write buffer as the last thing we do, because if they
332 * match any of the above conditions its no use flushing their buffer anyway.
335 curr->FlushWriteBuf();
336 if (*curr->GetWriteError())
338 GlobalGoners.AddItem(curr,curr->GetWriteError());
345 /* If theres nothing to do, trigger in the next second, something might come up */
346 time_t delta = next_call - TIME;
347 if (delta == DUMMY_VALUE)
349 next_call = TIME + 1;
353 /* Remove all the queued users who are due to be quit, free memory used. */
354 GlobalGoners.Apply();