]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Workaround for std::list::size() having linear complexity on some implementations
[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 /* add a client connection to the sockets list */
28 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
29 {
30         /* NOTE: Calling this one parameter constructor for User automatically
31          * allocates a new UUID and places it in the hash_map.
32          */
33         LocalUser* New = NULL;
34         try
35         {
36                 New = new LocalUser(socket, client, server);
37         }
38         catch (...)
39         {
40                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkeys have been unleashed.");
41                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
42                 return;
43         }
44         UserIOHandler* eh = &New->eh;
45
46         /* Give each of the modules an attempt to hook the user for I/O */
47         FOREACH_MOD(I_OnHookIO, OnHookIO(eh, via));
48
49         if (eh->GetIOHook())
50         {
51                 try
52                 {
53                         eh->GetIOHook()->OnStreamSocketAccept(eh, client, server);
54                 }
55                 catch (CoreException& modexcept)
56                 {
57                         ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
58                 }
59         }
60
61         ServerInstance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
62
63         this->unregistered_count++;
64
65         /* The users default nick is their UUID */
66         New->nick = New->uuid;
67         (*(this->clientlist))[New->nick] = New;
68
69         New->registered = REG_NONE;
70         New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
71         New->lastping = 1;
72
73         ServerInstance->Users->AddLocalClone(New);
74         ServerInstance->Users->AddGlobalClone(New);
75
76         New->localuseriter = this->local_users.insert(local_users.end(), New);
77         local_count++;
78
79         if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
80         {
81                 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
82                 this->QuitUser(New,"No more connections allowed");
83                 return;
84         }
85
86         /*
87          * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
88          * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
89          */
90         New->SetClass();
91
92         /*
93          * Check connect class settings and initialise settings into User.
94          * This will be done again after DNS resolution. -- w00t
95          */
96         New->CheckClass();
97         if (New->quitting)
98                 return;
99
100         /*
101          * even with bancache, we still have to keep User::exempt current.
102          * besides that, if we get a positive bancache hit, we still won't fuck
103          * them over if they are exempt. -- w00t
104          */
105         New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
106
107         if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
108         {
109                 if (!b->Type.empty() && !New->exempt)
110                 {
111                         /* user banned */
112                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
113                         if (!ServerInstance->Config->MoronBanner.empty())
114                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
115                         this->QuitUser(New, b->Reason);
116                         return;
117                 }
118                 else
119                 {
120                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
121                 }
122         }
123         else
124         {
125                 if (!New->exempt)
126                 {
127                         XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
128
129                         if (r)
130                         {
131                                 r->Apply(New);
132                                 return;
133                         }
134                 }
135         }
136
137         if (!ServerInstance->SE->AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
138         {
139                 ServerInstance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
140                 this->QuitUser(New, "Internal error handling connection");
141         }
142
143         /* NOTE: even if dns lookups are *off*, we still need to display this.
144          * BOPM and other stuff requires it.
145          */
146         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
147         if (ServerInstance->Config->RawLog)
148                 New->WriteServ("NOTICE Auth :*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
149
150         FOREACH_MOD(I_OnSetUserIP,OnSetUserIP(New));
151         if (New->quitting)
152                 return;
153
154         FOREACH_MOD(I_OnUserInit,OnUserInit(New));
155
156         if (ServerInstance->Config->NoUserDns)
157         {
158                 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
159                 New->dns_done = true;
160         }
161         else
162         {
163                 New->StartDNSLookup();
164         }
165 }
166
167 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
168 {
169         if (user->quitting)
170         {
171                 ServerInstance->Logs->Log("USERS", DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
172                 return;
173         }
174
175         if (IS_SERVER(user))
176         {
177                 ServerInstance->Logs->Log("USERS", DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
178                 return;
179         }
180
181         user->quitting = true;
182
183         ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
184         user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
185
186         std::string reason;
187         std::string oper_reason;
188         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
189         if (operreason && *operreason)
190                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
191         else
192                 oper_reason = quitreason;
193
194         ServerInstance->GlobalCulls.AddItem(user);
195
196         if (user->registered == REG_ALL)
197         {
198                 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
199                 user->WriteCommonQuit(reason, oper_reason);
200         }
201
202         if (user->registered != REG_ALL)
203                 if (ServerInstance->Users->unregistered_count)
204                         ServerInstance->Users->unregistered_count--;
205
206         if (IS_LOCAL(user))
207         {
208                 LocalUser* lu = IS_LOCAL(user);
209                 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(lu));
210                 lu->eh.Close();
211         }
212
213         /*
214          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
215          * if they were an oper with +s +qQ.
216          */
217         if (user->registered == REG_ALL)
218         {
219                 if (IS_LOCAL(user))
220                 {
221                         if (!user->quietquit)
222                         {
223                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]",
224                                         user->GetFullRealHost().c_str(), user->GetIPString(), oper_reason.c_str());
225                         }
226                 }
227                 else
228                 {
229                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
230                         {
231                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s (%s) [%s]",
232                                         user->server.c_str(), user->GetFullRealHost().c_str(), user->GetIPString(), oper_reason.c_str());
233                         }
234                 }
235                 user->AddToWhoWas();
236         }
237
238         user_hash::iterator iter = this->clientlist->find(user->nick);
239
240         if (iter != this->clientlist->end())
241                 this->clientlist->erase(iter);
242         else
243                 ServerInstance->Logs->Log("USERS", DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
244
245         ServerInstance->Users->uuidlist->erase(user->uuid);
246 }
247
248
249 void UserManager::AddLocalClone(User *user)
250 {
251         local_clones[user->GetCIDRMask()]++;
252 }
253
254 void UserManager::AddGlobalClone(User *user)
255 {
256         global_clones[user->GetCIDRMask()]++;
257 }
258
259 void UserManager::RemoveCloneCounts(User *user)
260 {
261         if (IS_LOCAL(user))
262         {
263                 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
264                 if (x != local_clones.end())
265                 {
266                         x->second--;
267                         if (!x->second)
268                         {
269                                 local_clones.erase(x);
270                         }
271                 }
272         }
273
274         clonemap::iterator y = global_clones.find(user->GetCIDRMask());
275         if (y != global_clones.end())
276         {
277                 y->second--;
278                 if (!y->second)
279                 {
280                         global_clones.erase(y);
281                 }
282         }
283 }
284
285 unsigned long UserManager::GlobalCloneCount(User *user)
286 {
287         clonemap::iterator x = global_clones.find(user->GetCIDRMask());
288         if (x != global_clones.end())
289                 return x->second;
290         else
291                 return 0;
292 }
293
294 unsigned long UserManager::LocalCloneCount(User *user)
295 {
296         clonemap::iterator x = local_clones.find(user->GetCIDRMask());
297         if (x != local_clones.end())
298                 return x->second;
299         else
300                 return 0;
301 }
302
303 /* this function counts all users connected, wether they are registered or NOT. */
304 unsigned int UserManager::UserCount()
305 {
306         /*
307          * XXX: Todo:
308          *  As part of this restructuring, move clientlist/etc fields into usermanager.
309          *      -- w00t
310          */
311         return this->clientlist->size();
312 }
313
314 /* this counts only registered users, so that the percentages in /MAP don't mess up */
315 unsigned int UserManager::RegisteredUserCount()
316 {
317         return this->clientlist->size() - this->UnregisteredUserCount();
318 }
319
320 /* return how many users are opered */
321 unsigned int UserManager::OperCount()
322 {
323         return this->all_opers.size();
324 }
325
326 /* return how many users are unregistered */
327 unsigned int UserManager::UnregisteredUserCount()
328 {
329         return this->unregistered_count;
330 }
331
332 /* return how many local registered users there are */
333 unsigned int UserManager::LocalUserCount()
334 {
335         /* Doesnt count unregistered clients */
336         return (this->local_count - this->UnregisteredUserCount());
337 }
338
339 void UserManager::ServerNoticeAll(const char* text, ...)
340 {
341         if (!text)
342                 return;
343
344         char textbuffer[MAXBUF];
345         char formatbuffer[MAXBUF];
346         va_list argsPtr;
347         va_start (argsPtr, text);
348         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
349         va_end(argsPtr);
350
351         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
352
353         for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
354         {
355                 User* t = *i;
356                 t->WriteServ(std::string(formatbuffer));
357         }
358 }
359
360 void UserManager::ServerPrivmsgAll(const char* text, ...)
361 {
362         if (!text)
363                 return;
364
365         char textbuffer[MAXBUF];
366         char formatbuffer[MAXBUF];
367         va_list argsPtr;
368         va_start (argsPtr, text);
369         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
370         va_end(argsPtr);
371
372         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
373
374         for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
375         {
376                 User* t = *i;
377                 t->WriteServ(std::string(formatbuffer));
378         }
379 }
380
381
382 /* return how many users have a given mode e.g. 'a' */
383 int UserManager::ModeCount(const char mode)
384 {
385         int c = 0;
386         for(user_hash::iterator i = clientlist->begin(); i != clientlist->end(); ++i)
387         {
388                 User* u = i->second;
389                 if (u->modes[mode-65])
390                         c++;
391         }
392         return c;
393 }