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);
47 this->Log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
49 catch (ModuleException& modexcept)
51 this->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
65 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
68 this->Log(DEBUG,"Read result: %d",result);
70 if ((result) && (result != -EAGAIN))
76 this->stats->statsRecv += result;
78 * perform a check on the raw buffer as an array (not a string!) to remove
79 * character 0 which is illegal in the RFC - replace them with spaces.
80 * XXX - no garauntee there's not \0's in the middle of the data,
81 * and no reason for it to be terminated either. -- Om
84 for (int checker = 0; checker < result; checker++)
86 if (ReadBuffer[checker] == 0)
87 ReadBuffer[checker] = ' ';
91 ReadBuffer[result] = '\0';
94 currfd = current->GetFd();
96 // add the data to the users buffer
99 if (!current->AddBuffer(ReadBuffer))
101 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
102 if (current->registered == REG_ALL)
104 // Make sure they arn't flooding long lines.
105 if (TIME > current->reset_due)
107 current->reset_due = TIME + current->threshold;
108 current->lines_in = 0;
113 if (current->lines_in > current->flood)
115 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
116 this->WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
117 userrec::QuitUser(this, current,"Excess flood");
122 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
128 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
129 this->Log(DEFAULT,"Excess flood from: %s",current->GetIPString());
130 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
131 XLines->apply_lines(APPLY_ZLINES);
137 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
139 if (current->registered == REG_ALL)
141 userrec::QuitUser(this, current,"RecvQ exceeded");
145 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
146 this->Log(DEFAULT,"Excess flood from: %s",current->GetIPString());
147 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
148 XLines->apply_lines(APPLY_ZLINES);
154 // while there are complete lines to process...
155 while (current->BufferIsReady())
157 if (TIME > current->reset_due)
159 current->reset_due = TIME + current->threshold;
160 current->lines_in = 0;
163 if (++current->lines_in > current->flood)
165 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
166 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
167 userrec::QuitUser(this, current,"Excess flood");
171 if ((++floodlines > current->flood) && (current->flood != 0))
173 if (current->registered == REG_ALL)
175 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
176 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
177 userrec::QuitUser(this, current,"Excess flood");
181 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
182 XLines->apply_lines(APPLY_ZLINES);
188 // use GetBuffer to copy single lines into the sanitized string
189 std::string single_line = current->GetBuffer();
190 current->bytes_in += single_line.length();
192 if (single_line.length() > 512)
193 single_line.resize(512);
195 EventHandler* old_comp = this->SE->GetRef(currfd);
197 this->Parser->ProcessBuffer(single_line,current);
199 * look for the user's record in case it's changed... if theyve quit,
200 * we cant do anything more with their buffer, so bail.
201 * there used to be an ugly, slow loop here. Now we have a reference
202 * table, life is much easier (and FASTER)
204 EventHandler* new_comp = this->SE->GetRef(currfd);
205 if (old_comp != new_comp)
211 /* The user is still here, flush their buffer */
212 current->FlushWriteBuf();
219 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
221 this->Log(DEBUG,"killing: %s",cu->nick);
222 userrec::QuitUser(this,cu,strerror(errno));
227 // result EAGAIN means nothing read
228 else if ((result == EAGAIN) || (result == -EAGAIN))
232 else if (result == 0)
234 this->Log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
235 userrec::QuitUser(this,cu,"Client exited");
236 this->Log(DEBUG,"Bailing from client exit");
242 * This function is called once a second from the mainloop.
243 * It is intended to do background checking on all the user structs, e.g.
244 * stuff like ping checks, registration timeouts, etc. This function is
245 * also responsible for checking if InspSocket derived classes are timed out.
247 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
249 CullList GlobalGoners(this);
251 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
252 for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
254 if (count2 >= this->local_users.size())
257 userrec* curr = this->local_users[count2];
262 * registration timeout -- didnt send USER/NICK/HOST
263 * in the time specified in their connection class.
265 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
267 this->Log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
268 //ZapThisDns(curr->fd);
269 GlobalGoners.AddItem(curr,"Registration timeout");
273 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
274 * say this user is ok to proceed, fully connect them.
276 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
278 curr->dns_done = true;
279 //ZapThisDns(curr->fd);
280 this->stats->statsDnsBad++;
281 curr->FullConnect(&GlobalGoners);
284 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
286 this->Log(DEBUG,"dns done, registered=3, and modules ready, OK");
287 curr->FullConnect(&GlobalGoners);
288 //ZapThisDns(curr->fd);
291 // It's time to PING this user. Send them a ping.
292 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
294 // This user didn't answer the last ping, remove them
297 GlobalGoners.AddItem(curr,"Ping timeout");
299 curr->nping = TIME+curr->pingmax;
302 curr->Write("PING :%s",this->Config->ServerName);
304 curr->nping = TIME+curr->pingmax;
308 * We can flush the write buffer as the last thing we do, because if they
309 * match any of the above conditions its no use flushing their buffer anyway.
312 curr->FlushWriteBuf();
313 if (*curr->GetWriteError())
315 GlobalGoners.AddItem(curr,curr->GetWriteError());
323 /* Remove all the queued users who are due to be quit, free memory used. */
324 GlobalGoners.Apply();