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