]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Merge insp20
[user/henk/code/inspircd.git] / src / usermanager.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "xline.h"
25 #include "bancache.h"
26
27 UserManager::UserManager()
28         : unregistered_count(0), local_count(0)
29 {
30 }
31
32 /* add a client connection to the sockets list */
33 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
34 {
35         /* NOTE: Calling this one parameter constructor for User automatically
36          * allocates a new UUID and places it in the hash_map.
37          */
38         LocalUser* New = NULL;
39         try
40         {
41                 New = new LocalUser(socket, client, server);
42         }
43         catch (...)
44         {
45                 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "*** WTF *** Duplicated UUID! -- Crack smoking monkeys have been unleashed.");
46                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
47                 return;
48         }
49         UserIOHandler* eh = &New->eh;
50
51         /* Give each of the modules an attempt to hook the user for I/O */
52         FOREACH_MOD(I_OnHookIO, OnHookIO(eh, via));
53
54         if (eh->GetIOHook())
55         {
56                 try
57                 {
58                         eh->GetIOHook()->OnStreamSocketAccept(eh, client, server);
59                 }
60                 catch (CoreException& modexcept)
61                 {
62                         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
63                 }
64         }
65
66         ServerInstance->Logs->Log("USERS", LOG_DEBUG, "New user fd: %d", socket);
67
68         this->unregistered_count++;
69
70         /* The users default nick is their UUID */
71         New->nick = New->uuid;
72         (*(this->clientlist))[New->nick] = New;
73
74         New->registered = REG_NONE;
75         New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
76         New->lastping = 1;
77
78         ServerInstance->Users->AddLocalClone(New);
79         ServerInstance->Users->AddGlobalClone(New);
80
81         New->localuseriter = this->local_users.insert(local_users.end(), New);
82         local_count++;
83
84         if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
85         {
86                 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
87                 this->QuitUser(New,"No more connections allowed");
88                 return;
89         }
90
91         /*
92          * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
93          * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
94          */
95         New->SetClass();
96
97         /*
98          * Check connect class settings and initialise settings into User.
99          * This will be done again after DNS resolution. -- w00t
100          */
101         New->CheckClass();
102         if (New->quitting)
103                 return;
104
105         /*
106          * even with bancache, we still have to keep User::exempt current.
107          * besides that, if we get a positive bancache hit, we still won't fuck
108          * them over if they are exempt. -- w00t
109          */
110         New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
111
112         if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
113         {
114                 if (!b->Type.empty() && !New->exempt)
115                 {
116                         /* user banned */
117                         ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Positive hit for " + New->GetIPString());
118                         if (!ServerInstance->Config->MoronBanner.empty())
119                                 New->WriteNotice("*** " +  ServerInstance->Config->MoronBanner);
120                         this->QuitUser(New, b->Reason);
121                         return;
122                 }
123                 else
124                 {
125                         ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Negative hit for " + New->GetIPString());
126                 }
127         }
128         else
129         {
130                 if (!New->exempt)
131                 {
132                         XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
133
134                         if (r)
135                         {
136                                 r->Apply(New);
137                                 return;
138                         }
139                 }
140         }
141
142         if (!ServerInstance->SE->AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
143         {
144                 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "Internal error on new connection");
145                 this->QuitUser(New, "Internal error handling connection");
146         }
147
148         if (ServerInstance->Config->RawLog)
149                 New->WriteNotice("*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
150
151         FOREACH_MOD(I_OnSetUserIP,OnSetUserIP(New));
152         if (New->quitting)
153                 return;
154
155         FOREACH_MOD(I_OnUserInit,OnUserInit(New));
156 }
157
158 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
159 {
160         if (user->quitting)
161         {
162                 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
163                 return;
164         }
165
166         if (IS_SERVER(user))
167         {
168                 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
169                 return;
170         }
171
172         user->quitting = true;
173
174         ServerInstance->Logs->Log("USERS", LOG_DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
175         user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
176
177         std::string reason;
178         std::string oper_reason;
179         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
180         if (operreason && *operreason)
181                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
182         else
183                 oper_reason = quitreason;
184
185         ServerInstance->GlobalCulls.AddItem(user);
186
187         if (user->registered == REG_ALL)
188         {
189                 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
190                 user->WriteCommonQuit(reason, oper_reason);
191         }
192
193         if (user->registered != REG_ALL)
194                 if (ServerInstance->Users->unregistered_count)
195                         ServerInstance->Users->unregistered_count--;
196
197         if (IS_LOCAL(user))
198         {
199                 LocalUser* lu = IS_LOCAL(user);
200                 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(lu));
201                 lu->eh.Close();
202         }
203
204         /*
205          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
206          * if they were an oper with +s +qQ.
207          */
208         if (user->registered == REG_ALL)
209         {
210                 if (IS_LOCAL(user))
211                 {
212                         if (!user->quietquit)
213                         {
214                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]",
215                                         user->GetFullRealHost().c_str(), user->GetIPString().c_str(), oper_reason.c_str());
216                         }
217                 }
218                 else
219                 {
220                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
221                         {
222                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s (%s) [%s]",
223                                         user->server.c_str(), user->GetFullRealHost().c_str(), user->GetIPString().c_str(), oper_reason.c_str());
224                         }
225                 }
226         }
227
228         user_hash::iterator iter = this->clientlist->find(user->nick);
229
230         if (iter != this->clientlist->end())
231                 this->clientlist->erase(iter);
232         else
233                 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
234
235         ServerInstance->Users->uuidlist->erase(user->uuid);
236 }
237
238
239 void UserManager::AddLocalClone(User *user)
240 {
241         local_clones[user->GetCIDRMask()]++;
242 }
243
244 void UserManager::AddGlobalClone(User *user)
245 {
246         global_clones[user->GetCIDRMask()]++;
247 }
248
249 void UserManager::RemoveCloneCounts(User *user)
250 {
251         if (IS_LOCAL(user))
252         {
253                 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
254                 if (x != local_clones.end())
255                 {
256                         x->second--;
257                         if (!x->second)
258                         {
259                                 local_clones.erase(x);
260                         }
261                 }
262         }
263
264         clonemap::iterator y = global_clones.find(user->GetCIDRMask());
265         if (y != global_clones.end())
266         {
267                 y->second--;
268                 if (!y->second)
269                 {
270                         global_clones.erase(y);
271                 }
272         }
273 }
274
275 unsigned long UserManager::GlobalCloneCount(User *user)
276 {
277         clonemap::iterator x = global_clones.find(user->GetCIDRMask());
278         if (x != global_clones.end())
279                 return x->second;
280         else
281                 return 0;
282 }
283
284 unsigned long UserManager::LocalCloneCount(User *user)
285 {
286         clonemap::iterator x = local_clones.find(user->GetCIDRMask());
287         if (x != local_clones.end())
288                 return x->second;
289         else
290                 return 0;
291 }
292
293 /* this function counts all users connected, wether they are registered or NOT. */
294 unsigned int UserManager::UserCount()
295 {
296         /*
297          * XXX: Todo:
298          *  As part of this restructuring, move clientlist/etc fields into usermanager.
299          *      -- w00t
300          */
301         return this->clientlist->size();
302 }
303
304 /* this counts only registered users, so that the percentages in /MAP don't mess up */
305 unsigned int UserManager::RegisteredUserCount()
306 {
307         return this->clientlist->size() - this->UnregisteredUserCount();
308 }
309
310 /* return how many users are opered */
311 unsigned int UserManager::OperCount()
312 {
313         return this->all_opers.size();
314 }
315
316 /* return how many users are unregistered */
317 unsigned int UserManager::UnregisteredUserCount()
318 {
319         return this->unregistered_count;
320 }
321
322 /* return how many local registered users there are */
323 unsigned int UserManager::LocalUserCount()
324 {
325         /* Doesnt count unregistered clients */
326         return (this->local_count - this->UnregisteredUserCount());
327 }
328
329 void UserManager::ServerNoticeAll(const char* text, ...)
330 {
331         std::string message;
332         VAFORMAT(message, text, text);
333         message = "NOTICE $" + ServerInstance->Config->ServerName + " :" + message;
334
335         for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
336         {
337                 User* t = *i;
338                 t->WriteServ(message);
339         }
340 }
341
342 /* return how many users have a given mode e.g. 'a' */
343 int UserManager::ModeCount(const char mode)
344 {
345         int c = 0;
346         for(user_hash::iterator i = clientlist->begin(); i != clientlist->end(); ++i)
347         {
348                 User* u = i->second;
349                 if (u->modes[mode-65])
350                         c++;
351         }
352         return c;
353 }
354
355 void UserManager::GarbageCollect()
356 {
357         // Reset the already_sent IDs so we don't wrap it around and drop a message
358         LocalUser::already_sent_id = 0;
359         for (LocalUserList::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
360         {
361                 (**i).already_sent = 0;
362                 (**i).RemoveExpiredInvites();
363         }
364 }