1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #include "configreader.h"
19 #include "socketengine.h"
21 #include "command_parse.h"
22 #include "cull_list.h"
24 void InspIRCd::ProcessUser(userrec* cu)
28 if (cu->GetFd() == FD_MAGIC_NUMBER)
31 if (this->Config->GetIOHook(cu->GetPort()))
38 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
40 catch (CoreException& modexcept)
42 this->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
56 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
59 if ((result) && (result != -EAGAIN))
65 this->stats->statsRecv += result;
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
73 for (int checker = 0; checker < result; checker++)
75 if (ReadBuffer[checker] == 0)
76 ReadBuffer[checker] = ' ';
80 ReadBuffer[result] = '\0';
83 currfd = current->GetFd();
85 // add the data to the users buffer
88 if (!current->AddBuffer(ReadBuffer))
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)
93 // Make sure they arn't flooding long lines.
94 if (TIME > current->reset_due)
96 current->reset_due = TIME + current->threshold;
97 current->lines_in = 0;
102 if (current->lines_in > current->flood)
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");
111 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
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);
126 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
128 if (current->registered == REG_ALL)
130 current->SetWriteError("RecvQ exceeded");
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);
143 // while there are complete lines to process...
144 while (current->BufferIsReady())
146 if (TIME > current->reset_due)
148 current->reset_due = TIME + current->threshold;
149 current->lines_in = 0;
152 if (++current->lines_in > current->flood)
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");
160 if ((++floodlines > current->flood) && (current->flood != 0))
162 if (current->registered == REG_ALL)
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");
170 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
171 XLines->apply_lines(APPLY_ZLINES);
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();
181 if (single_line.length() > 512)
182 single_line.resize(512);
184 EventHandler* old_comp = this->SE->GetRef(currfd);
186 this->Parser->ProcessBuffer(single_line,current);
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)
193 EventHandler* new_comp = this->SE->GetRef(currfd);
195 if (new_comp != old_comp)
202 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
204 cu->SetWriteError(strerror(errno));
209 // result EAGAIN means nothing read
210 else if ((result == EAGAIN) || (result == -EAGAIN))
214 else if (result == 0)
216 cu->SetWriteError("Client exited");
222 * This function is called once a second from the mainloop.
223 * It is intended to do background checking on all the user structs, e.g.
224 * stuff like ping checks, registration timeouts, etc.
226 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
228 /* Is it time yet? */
229 if (TIME < next_call)
233 CullList GlobalGoners(this);
235 /* Time we actually need to call this again */
236 const time_t DUMMY_VALUE = 32768;
237 next_call = TIME + DUMMY_VALUE;
239 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
240 for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
242 if (count2 >= this->local_users.size())
245 userrec* curr = this->local_users[count2];
250 * registration timeout -- didnt send USER/NICK/HOST
251 * in the time specified in their connection class.
253 if ((TIME > curr->timeout) && (curr->registered != REG_ALL))
255 GlobalGoners.AddItem(curr,"Registration timeout");
260 if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))
261 next_call = curr->timeout;
265 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
266 * say this user is ok to proceed, fully connect them.
268 bool ready = AllModulesReportReady(curr);
269 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))
271 curr->dns_done = true;
272 this->stats->statsDnsBad++;
273 curr->FullConnect(&GlobalGoners);
278 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))
279 next_call = curr->signon;
282 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))
284 curr->FullConnect(&GlobalGoners);
289 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))
290 next_call = curr->signon + this->Config->dns_timeout;
293 // It's time to PING this user. Send them a ping.
294 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
296 // This user didn't answer the last ping, remove them
299 /* Everybody loves boobies. */
300 time_t time = this->Time(false) - (curr->nping - curr->pingmax);
301 std::string boobies = "Ping timeout: " + ConvToStr(time) + " second" + (time > 1 ? "s" : "");
302 GlobalGoners.AddItem(curr, boobies);
304 curr->nping = TIME+curr->pingmax;
307 curr->Write("PING :%s",this->Config->ServerName);
309 curr->nping = TIME+curr->pingmax;
313 if ((curr->registered == REG_ALL) && (next_call > curr->nping))
314 next_call = curr->nping;
319 /* If theres nothing to do, trigger in the next second, something might come up */
320 time_t delta = next_call - TIME;
321 if (delta == DUMMY_VALUE)
323 next_call = TIME + 1;
327 /* Remove all the queued users who are due to be quit, free memory used. */
328 GlobalGoners.Apply();