]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
Remove more debug
[user/henk/code/inspircd.git] / src / userprocess.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 #include "inspircd.h"
15 #include "wildcard.h"
16 #include "xline.h"
17 #include "socketengine.h"
18 #include "command_parse.h"
19
20 void FloodQuitUserHandler::Call(User* current)
21 {
22         Server->Log(DEFAULT,"Excess flood from: %s@%s", current->ident, current->host);
23         Server->SNO->WriteToSnoMask('f',"Excess flood from: %s%s%s@%s",
24                         current->registered == REG_ALL ? current->nick : "",
25                         current->registered == REG_ALL ? "!" : "", current->ident, current->host);
26         User::QuitUser(Server, current, "Excess flood");
27         if (current->registered != REG_ALL)
28         {
29                 Server->XLines->add_zline(120, Server->Config->ServerName, "Flood from unregistered connection", current->GetIPString());
30                 Server->XLines->apply_lines(APPLY_ZLINES);
31         }
32 }
33
34 void ProcessUserHandler::Call(User* cu)
35 {
36         int result = EAGAIN;
37
38         if (cu->GetFd() == FD_MAGIC_NUMBER)
39                 return;
40
41         char* ReadBuffer = Server->GetReadBuffer();
42
43         if (Server->Config->GetIOHook(cu->GetPort()))
44         {
45                 int result2 = 0;
46                 int MOD_RESULT = 0;
47
48                 try
49                 {
50                         MOD_RESULT = Server->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,Server->Config->NetBufferSize,result2);
51                 }
52                 catch (CoreException& modexcept)
53                 {
54                         Server->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
55                 }
56
57                 if (MOD_RESULT < 0)
58                 {
59                         result = -EAGAIN;
60                 }
61                 else
62                 {
63                         result = result2;
64                 }
65         }
66         else
67         {
68                 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
69         }
70
71         if ((result) && (result != -EAGAIN))
72         {
73                 User *current;
74                 int currfd;
75
76                 Server->stats->statsRecv += result;
77                 /*
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                  */
81
82                 for (int checker = 0; checker < result; checker++)
83                 {
84                         if (ReadBuffer[checker] == 0)
85                                 ReadBuffer[checker] = ' ';
86                 }
87
88                 if (result > 0)
89                         ReadBuffer[result] = '\0';
90
91                 current = cu;
92                 currfd = current->GetFd();
93
94                 // add the data to the users buffer
95                 if (result > 0)
96                 {
97                         if (!current->AddBuffer(ReadBuffer))
98                         {
99                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
100                                 if (current->registered == REG_ALL)
101                                 {
102                                         // Make sure they arn't flooding long lines.
103                                         if (Server->Time() > current->reset_due)
104                                         {
105                                                 current->reset_due = Server->Time() + current->threshold;
106                                                 current->lines_in = 0;
107                                         }
108
109                                         current->lines_in++;
110
111                                         if (current->flood && current->lines_in > current->flood)
112                                                 Server->FloodQuitUser(current);
113                                         else
114                                         {
115                                                 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over %d chars) Please shorten it.", current->nick, MAXBUF-2);
116                                                 current->recvq.clear();
117                                         }
118                                 }
119                                 else
120                                         Server->FloodQuitUser(current);
121
122                                 return;
123                         }
124
125                         /* If user is over penalty, dont process here, just build up */
126                         if (!current->OverPenalty)
127                                 Server->Parser->DoLines(current);
128
129                         return;
130                 }
131
132                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
133                 {
134                         User::QuitUser(Server, cu, errno ? strerror(errno) : "EOF from client");
135                         return;
136                 }
137         }
138
139         // result EAGAIN means nothing read
140         else if ((result == EAGAIN) || (result == -EAGAIN))
141         {
142                 /* do nothing */
143         }
144         else if (result == 0)
145         {
146                 User::QuitUser(Server, cu, "Connection closed");
147                 return;
148         }
149 }
150
151 /**
152  * This function is called once a second from the mainloop.
153  * It is intended to do background checking on all the user structs, e.g.
154  * stuff like ping checks, registration timeouts, etc.
155  */
156 void InspIRCd::DoBackgroundUserStuff()
157 {
158         /*
159          * loop over all local users..
160          */
161         for (std::vector<User*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
162         {
163                 User *curr = *count2;
164
165                 if (curr->Penalty)
166                 {
167                         curr->Penalty--;
168                         if (curr->Penalty < 10)
169                                 Parser->DoLines(curr, true);
170                 }
171
172                 if (curr->OverPenalty)
173                 {
174                         if (curr->sendq.empty())
175                                 curr->OverPenalty = false;
176                 }
177
178                 if ((curr->registered != REG_ALL) && (TIME > curr->timeout))
179                 {
180                         /*
181                          * registration timeout -- didnt send USER/NICK/HOST
182                          * in the time specified in their connection class.
183                          */
184                         curr->muted = true;
185                         User::QuitUser(this, curr, "Registration timeout");
186                         continue;
187                 }
188
189                 /*
190                  * `ready` means that the user has provided NICK/USER(/PASS), and all modules agree
191                  * that the user is okay to proceed. The one thing we are then waiting for is DNS, which we do here...
192                  */
193                 bool ready = ((curr->registered == REG_NICKUSER) && AllModulesReportReady(curr));
194
195                 if (ready)
196                 {
197                         if (!curr->dns_done)
198                         {
199                                 /*
200                                  * DNS isn't done yet?
201                                  * Cool. Check for timeout.
202                                  */
203                                 if (TIME > curr->signon)
204                                 {
205                                         /* FZZZZZZZZT, timeout! */
206                                         curr->WriteServ("NOTICE Auth :*** Could not resolve your hostname: Request timed out; using your IP address (%s) instead.", curr->GetIPString());
207                                         curr->dns_done = true;
208                                         this->stats->statsDnsBad++;
209                                         curr->FullConnect();
210                                         continue;
211                                 }
212                         }
213                         else
214                         {
215                                 /* DNS passed, connect the user */
216                                 curr->FullConnect();
217                                 continue;
218                         }
219                 }
220
221                 // It's time to PING this user. Send them a ping.
222                 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
223                 {
224                         // This user didn't answer the last ping, remove them
225                         if (!curr->lastping)
226                         {
227                                 time_t time = this->Time(false) - (curr->nping - curr->pingmax);
228                                 char message[MAXBUF];
229                                 snprintf(message, MAXBUF, "Ping timeout: %ld second%s", (long)time, time > 1 ? "s" : "");
230                                 curr->muted = true;
231                                 curr->lastping = 1;
232                                 curr->nping = TIME+curr->pingmax;
233                                 User::QuitUser(this, curr, message);
234                                 continue;
235                         }
236                         curr->Write("PING :%s",this->Config->ServerName);
237                         curr->lastping = 0;
238                         curr->nping = TIME+curr->pingmax;
239                 }
240         }
241 }
242