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