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 CullList GlobalGoners(this);
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 //ZapThisDns(curr->fd);
268 GlobalGoners.AddItem(curr,"Registration timeout");
272 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
273 * say this user is ok to proceed, fully connect them.
275 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
277 curr->dns_done = true;
278 //ZapThisDns(curr->fd);
279 this->stats->statsDnsBad++;
280 curr->FullConnect(&GlobalGoners);
283 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
285 this->Log(DEBUG,"dns done, registered=3, and modules ready, OK");
286 curr->FullConnect(&GlobalGoners);
287 //ZapThisDns(curr->fd);
290 // It's time to PING this user. Send them a ping.
291 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
293 // This user didn't answer the last ping, remove them
296 GlobalGoners.AddItem(curr,"Ping timeout");
298 curr->nping = TIME+curr->pingmax;
301 curr->Write("PING :%s",this->Config->ServerName);
303 curr->nping = TIME+curr->pingmax;
307 * We can flush the write buffer as the last thing we do, because if they
308 * match any of the above conditions its no use flushing their buffer anyway.
311 curr->FlushWriteBuf();
312 if (*curr->GetWriteError())
314 GlobalGoners.AddItem(curr,curr->GetWriteError());
322 /* Remove all the queued users who are due to be quit, free memory used. */
323 GlobalGoners.Apply();