]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
Use a switch (cleaner, probably more efficient) for user status here, also, we can...
[user/henk/code/inspircd.git] / src / userprocess.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDuserprocess */
15
16 #include "inspircd.h"
17 #include "wildcard.h"
18 #include "xline.h"
19 #include "socketengine.h"
20 #include "command_parse.h"
21
22 void FloodQuitUserHandler::Call(User* current)
23 {
24         Server->Logs->Log("USERS",DEFAULT,"Excess flood from: %s@%s", current->ident.c_str(), current->host.c_str());
25         Server->SNO->WriteToSnoMask('f',"Excess flood from: %s%s%s@%s",
26                         current->registered == REG_ALL ? current->nick.c_str() : "",
27                         current->registered == REG_ALL ? "!" : "", current->ident.c_str(), current->host.c_str());
28         Server->Users->QuitUser(current, "Excess flood");
29
30         if (current->registered != REG_ALL)
31         {
32                 ZLine* zl = new ZLine(Server, Server->Time(), 0, Server->Config->ServerName, "Flood from unregistered connection", current->GetIPString());
33                 if (Server->XLines->AddLine(zl,NULL))
34                         Server->XLines->ApplyLines();
35                 else
36                         delete zl;
37         }
38 }
39
40 void ProcessUserHandler::Call(User* cu)
41 {
42         int result = EAGAIN;
43
44         if (cu->GetFd() == FD_MAGIC_NUMBER)
45                 return;
46
47         char* ReadBuffer = Server->GetReadBuffer();
48
49         if (cu->io)
50         {
51                 int result2 = 0;
52                 int MOD_RESULT = 0;
53
54                 try
55                 {
56                         MOD_RESULT = cu->io->OnRawSocketRead(cu->GetFd(),ReadBuffer,Server->Config->NetBufferSize,result2);
57                 }
58                 catch (CoreException& modexcept)
59                 {
60                         Server->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
61                 }
62
63                 if (MOD_RESULT < 0)
64                 {
65                         result = -EAGAIN;
66                 }
67                 else
68                 {
69                         result = result2;
70                 }
71         }
72         else
73         {
74                 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
75         }
76
77         if ((result) && (result != -EAGAIN))
78         {
79                 User *current;
80                 int currfd;
81
82                 Server->stats->statsRecv += result;
83                 /*
84                  * perform a check on the raw buffer as an array (not a string!) to remove
85                  * character 0 which is illegal in the RFC - replace them with spaces.
86                  */
87
88                 for (int checker = 0; checker < result; checker++)
89                 {
90                         if (ReadBuffer[checker] == 0)
91                                 ReadBuffer[checker] = ' ';
92                 }
93
94                 if (result > 0)
95                         ReadBuffer[result] = '\0';
96
97                 current = cu;
98                 currfd = current->GetFd();
99
100                 // add the data to the users buffer
101                 if (result > 0)
102                 {
103                         if (!current->AddBuffer(ReadBuffer))
104                         {
105                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
106                                 if (current->registered == REG_ALL)
107                                 {
108                                         if (current->MyClass)
109                                         {
110                                                 // Make sure they arn't flooding long lines.
111                                                 if (Server->Time() > current->reset_due)
112                                                 {
113                                                         current->reset_due = Server->Time() + current->MyClass->GetThreshold();
114                                                         current->lines_in = 0;
115                                                 }
116
117                                                 current->lines_in++;
118
119                                                 if (current->MyClass->GetFlood() && current->lines_in > current->MyClass->GetFlood())
120                                                         Server->FloodQuitUser(current);
121                                                 else
122                                                 {
123                                                         current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over %d chars) Please shorten it.", current->nick.c_str(), MAXBUF-2);
124                                                         current->recvq.clear();
125                                                 }
126                                         }
127                                 }
128                                 else
129                                         Server->FloodQuitUser(current);
130
131                                 return;
132                         }
133
134                         /* If user is over penalty, dont process here, just build up */
135                         if (!current->OverPenalty)
136                                 Server->Parser->DoLines(current);
137
138                         return;
139                 }
140
141                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
142                 {
143                         Server->Users->QuitUser(cu, errno ? strerror(errno) : "EOF from client");
144                         return;
145                 }
146         }
147
148         // result EAGAIN means nothing read
149         else if ((result == EAGAIN) || (result == -EAGAIN))
150         {
151                 /* do nothing */
152         }
153         else if (result == 0)
154         {
155                 Server->Users->QuitUser(cu, "Connection closed");
156                 return;
157         }
158 }
159
160 /**
161  * This function is called once a second from the mainloop.
162  * It is intended to do background checking on all the user structs, e.g.
163  * stuff like ping checks, registration timeouts, etc.
164  */
165 void InspIRCd::DoBackgroundUserStuff()
166 {
167         /*
168          * loop over all local users..
169          */
170         for (std::vector<User*>::iterator count2 = this->Users->local_users.begin(); count2 != this->Users->local_users.end(); count2++)
171         {
172                 User *curr = *count2;
173
174                 if (curr->quitting)
175                         continue;
176
177                 if (curr->Penalty)
178                 {
179                         curr->Penalty--;
180                         if (curr->Penalty < 10)
181                                 Parser->DoLines(curr, true);
182                 }
183
184                 if (curr->OverPenalty)
185                 {
186                         if (curr->sendq.empty())
187                                 curr->OverPenalty = false;
188                 }
189
190                 switch (curr->registered)
191                 {
192                         case REG_ALL:
193                                 if (TIME > curr->nping)
194                                 {
195                                         // This user didn't answer the last ping, remove them
196                                         if (!curr->lastping)
197                                         {
198                                                 time_t time = this->Time() - (curr->nping - curr->MyClass->GetPingTime());
199                                                 char message[MAXBUF];
200                                                 snprintf(message, MAXBUF, "Ping timeout: %ld second%s", (long)time, time > 1 ? "s" : "");
201                                                 curr->lastping = 1;
202                                                 curr->nping = TIME + curr->MyClass->GetPingTime();
203                                                 this->Users->QuitUser(curr, message);
204                                                 continue;
205                                         }
206
207                                         curr->Write("PING :%s",this->Config->ServerName);
208                                         curr->lastping = 0;
209                                         curr->nping = TIME  +curr->MyClass->GetPingTime();
210                                 }
211                                 break;
212                         case REG_NICKUSER:
213                                 if (AllModulesReportReady(curr) && curr->dns_done)
214                                 {
215                                         /* User has sent NICK/USER, modules are okay, DNS finished. */
216                                         curr->FullConnect();
217                                         continue;
218                                 }
219                                 break;
220                 }
221
222                 if (curr->registered != REG_ALL && (TIME > (curr->age + curr->MyClass->GetRegTimeout())))
223                 {
224                         /*
225                          * registration timeout -- didnt send USER/NICK/HOST
226                          * in the time specified in their connection class.
227                          */
228                         this->Users->QuitUser(curr, "Registration timeout");
229                         continue;
230                 }
231         }
232 }
233