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