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