]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Move user quit logic out of cull list
[user/henk/code/inspircd.git] / src / usermanager.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "xline.h"
18 #include "bancache.h"
19
20 /* add a client connection to the sockets list */
21 void UserManager::AddUser(InspIRCd* Instance, int socket, ClientListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
22 {
23         /* NOTE: Calling this one parameter constructor for User automatically
24          * allocates a new UUID and places it in the hash_map.
25          */
26         User* New = NULL;
27         try
28         {
29                 New = new User(Instance);
30         }
31         catch (...)
32         {
33                 Instance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
34                 Instance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
35                 return;
36         }
37
38         New->SetFd(socket);
39         memcpy(&New->client_sa, client, sizeof(irc::sockets::sockaddrs));
40         memcpy(&New->server_sa, server, sizeof(irc::sockets::sockaddrs));
41
42         /* Give each of the modules an attempt to hook the user for I/O */
43         FOREACH_MOD_I(Instance, I_OnHookIO, OnHookIO(New, via));
44
45         if (New->GetIOHook())
46         {
47                 try
48                 {
49                         New->GetIOHook()->OnRawSocketAccept(socket, client, server);
50                 }
51                 catch (CoreException& modexcept)
52                 {
53                         ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
54                 }
55         }
56
57         Instance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
58
59         this->unregistered_count++;
60
61         (*(this->clientlist))[New->uuid] = New;
62
63         /* The users default nick is their UUID */
64         New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
65
66         New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
67         New->ident.assign("unknown");
68
69         New->registered = REG_NONE;
70         New->signon = Instance->Time() + Instance->Config->dns_timeout;
71         New->lastping = 1;
72
73         /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
74         New->dhost.assign(New->GetIPString(), 0, 64);
75         New->host.assign(New->GetIPString(), 0, 64);
76
77         Instance->Users->AddLocalClone(New);
78         Instance->Users->AddGlobalClone(New);
79
80         /*
81          * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
82          * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
83          */
84         ConnectClass* i = New->SetClass();
85
86         if (!i)
87         {
88                 this->QuitUser(New, "Access denied by configuration");
89                 return;
90         }
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
98         this->local_users.push_back(New);
99
100         if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)Instance->SE->GetMaxFds()))
101         {
102                 Instance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
103                 this->QuitUser(New,"No more connections allowed");
104                 return;
105         }
106
107         /*
108          * even with bancache, we still have to keep User::exempt current.
109          * besides that, if we get a positive bancache hit, we still won't fuck
110          * them over if they are exempt. -- w00t
111          */
112         New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
113
114         if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
115         {
116                 if (!b->Type.empty() && !New->exempt)
117                 {
118                         /* user banned */
119                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
120                         if (*Instance->Config->MoronBanner)
121                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), Instance->Config->MoronBanner);
122                         this->QuitUser(New, b->Reason);
123                         return;
124                 }
125                 else
126                 {
127                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
128                 }
129         }
130         else
131         {
132                 if (!New->exempt)
133                 {
134                         XLine* r = Instance->XLines->MatchesLine("Z",New);
135
136                         if (r)
137                         {
138                                 r->Apply(New);
139                                 return;
140                         }
141                 }
142         }
143
144         if (!Instance->SE->AddFd(New))
145         {
146                 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
147                 this->QuitUser(New, "Internal error handling connection");
148         }
149
150         /* NOTE: even if dns lookups are *off*, we still need to display this.
151          * BOPM and other stuff requires it.
152          */
153         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
154
155         if (Instance->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",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         user->quitting = true;
175
176         ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s '%s'", user->nick.c_str(), quitreason.c_str());
177         user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
178
179         user->quietquit = false;
180         user->quitmsg = quitreason;
181
182         std::string reason;
183         std::string oper_reason;
184         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
185         if (!*operreason)
186         {
187                 user->operquitmsg = quitreason;
188                 oper_reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
189         }
190         else
191         {
192                 user->operquitmsg = operreason;
193                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
194         }
195
196         ServerInstance->GlobalCulls.AddItem(user);
197
198         if (user->registered == REG_ALL)
199         {
200                 FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
201                 user->PurgeEmptyChannels();
202                 user->WriteCommonQuit(reason, oper_reason);
203         }
204
205         FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(user));
206
207         user->UpdateNickHash(user->uuid.c_str());
208
209         user_hash::iterator iter = this->clientlist->find(user->uuid);
210
211         if (user->registered != REG_ALL)
212                 if (ServerInstance->Users->unregistered_count)
213                         ServerInstance->Users->unregistered_count--;
214
215         if (IS_LOCAL(user))
216         {
217                 if (!user->sendq.empty())
218                         user->FlushWriteBuf();
219
220                 if (user->GetIOHook())
221                 {
222                         try
223                         {
224                                 user->GetIOHook()->OnRawSocketClose(user->GetFd());
225                         }
226                         catch (CoreException& modexcept)
227                         {
228                                 ServerInstance->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
229                         }
230                 }
231
232                 ServerInstance->SE->DelFd(user);
233                 user->CloseSocket();
234         }
235
236         /*
237          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
238          * if they were an oper with +sn +qQ.
239          */
240         if (user->registered == REG_ALL)
241         {
242                 if (IS_LOCAL(user))
243                 {
244                         if (!user->quietquit)
245                         {
246                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",
247                                         user->nick.c_str(), user->ident.c_str(), user->host.c_str(), user->operquitmsg.c_str());
248                         }
249                 }
250                 else
251                 {
252                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
253                         {
254                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",
255                                         user->server, user->nick.c_str(), user->ident.c_str(), user->host.c_str(), user->operquitmsg.c_str());
256                         }
257                 }
258                 user->AddToWhoWas();
259         }
260
261         if (iter != this->clientlist->end())
262                 this->clientlist->erase(iter);
263         else
264                 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
265
266         if (IS_LOCAL(user))
267         {
268                 std::vector<User*>::iterator x = find(local_users.begin(),local_users.end(),user);
269                 if (x != local_users.end())
270                         local_users.erase(x);
271                 else
272                         ServerInstance->Logs->Log("USERS", DEBUG, "Failed to remove user from vector");
273         }
274 }
275
276
277 void UserManager::AddLocalClone(User *user)
278 {
279         int range = 32;
280         clonemap::iterator x;
281         switch (user->client_sa.sa.sa_family)
282         {
283                 case AF_INET6:
284                         range = ServerInstance->Config->c_ipv6_range;
285                 break;
286                 case AF_INET:
287                         range = ServerInstance->Config->c_ipv4_range;
288                 break;
289         }
290
291         x = local_clones.find(user->GetCIDRMask(range));
292         if (x != local_clones.end())
293                 x->second++;
294         else
295                 local_clones[user->GetCIDRMask(range)] = 1;
296 }
297
298 void UserManager::AddGlobalClone(User *user)
299 {
300         int range = 32;
301         clonemap::iterator x;
302         switch (user->client_sa.sa.sa_family)
303         {
304                 case AF_INET6:
305                         range = ServerInstance->Config->c_ipv6_range;
306                 break;
307                 case AF_INET:
308                         range = ServerInstance->Config->c_ipv4_range;
309                 break;
310         }
311
312         x = global_clones.find(user->GetCIDRMask(range));
313         if (x != global_clones.end())
314                 x->second++;
315         else
316                 global_clones[user->GetCIDRMask(range)] = 1;
317 }
318
319 void UserManager::RemoveCloneCounts(User *user)
320 {
321         int range = 32;
322         switch (user->client_sa.sa.sa_family)
323         {
324                 case AF_INET6:
325                         range = ServerInstance->Config->c_ipv6_range;
326                 break;
327                 case AF_INET:
328                         range = ServerInstance->Config->c_ipv4_range;
329                 break;
330         }
331
332         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
333         if (x != local_clones.end())
334         {
335                 x->second--;
336                 if (!x->second)
337                 {
338                         local_clones.erase(x);
339                 }
340         }
341
342         clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
343         if (y != global_clones.end())
344         {
345                 y->second--;
346                 if (!y->second)
347                 {
348                         global_clones.erase(y);
349                 }
350         }
351 }
352
353 unsigned long UserManager::GlobalCloneCount(User *user)
354 {
355         int range = 32;
356         switch (user->client_sa.sa.sa_family)
357         {
358                 case AF_INET6:
359                         range = ServerInstance->Config->c_ipv6_range;
360                 break;
361                 case AF_INET:
362                         range = ServerInstance->Config->c_ipv4_range;
363                 break;
364         }
365         clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
366         if (x != global_clones.end())
367                 return x->second;
368         else
369                 return 0;
370 }
371
372 unsigned long UserManager::LocalCloneCount(User *user)
373 {
374         int range = 32;
375         switch (user->client_sa.sa.sa_family)
376         {
377                 case AF_INET6:
378                         range = ServerInstance->Config->c_ipv6_range;
379                 break;
380                 case AF_INET:
381                         range = ServerInstance->Config->c_ipv4_range;
382                 break;
383         }
384         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
385         if (x != local_clones.end())
386                 return x->second;
387         else
388                 return 0;
389 }
390
391 /* this function counts all users connected, wether they are registered or NOT. */
392 unsigned int UserManager::UserCount()
393 {
394         /*
395          * XXX: Todo:
396          *  As part of this restructuring, move clientlist/etc fields into usermanager.
397          *      -- w00t
398          */
399         return this->clientlist->size();
400 }
401
402 /* this counts only registered users, so that the percentages in /MAP don't mess up */
403 unsigned int UserManager::RegisteredUserCount()
404 {
405         return this->clientlist->size() - this->UnregisteredUserCount();
406 }
407
408 /* return how many users are opered */
409 unsigned int UserManager::OperCount()
410 {
411         return this->all_opers.size();
412 }
413
414 /* return how many users are unregistered */
415 unsigned int UserManager::UnregisteredUserCount()
416 {
417         return this->unregistered_count;
418 }
419
420 /* return how many local registered users there are */
421 unsigned int UserManager::LocalUserCount()
422 {
423         /* Doesnt count unregistered clients */
424         return (this->local_users.size() - this->UnregisteredUserCount());
425 }
426
427 void UserManager::ServerNoticeAll(const char* text, ...)
428 {
429         if (!text)
430                 return;
431
432         char textbuffer[MAXBUF];
433         char formatbuffer[MAXBUF];
434         va_list argsPtr;
435         va_start (argsPtr, text);
436         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
437         va_end(argsPtr);
438
439         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
440
441         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
442         {
443                 User* t = *i;
444                 t->WriteServ(std::string(formatbuffer));
445         }
446 }
447
448 void UserManager::ServerPrivmsgAll(const char* text, ...)
449 {
450         if (!text)
451                 return;
452
453         char textbuffer[MAXBUF];
454         char formatbuffer[MAXBUF];
455         va_list argsPtr;
456         va_start (argsPtr, text);
457         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
458         va_end(argsPtr);
459
460         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
461
462         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
463         {
464                 User* t = *i;
465                 t->WriteServ(std::string(formatbuffer));
466         }
467 }
468
469 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
470 {
471         char textbuffer[MAXBUF];
472         int modelen;
473         va_list argsPtr;
474
475         if (!text || !modes || !flags)
476         {
477                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
478                 return;
479         }
480
481         va_start(argsPtr, text);
482         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
483         va_end(argsPtr);
484         modelen = strlen(modes);
485
486         if (flags == WM_AND)
487         {
488                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
489                 {
490                         User* t = *i;
491                         bool send_to_user = true;
492
493                         for (int n = 0; n < modelen; n++)
494                         {
495                                 if (!t->IsModeSet(modes[n]))
496                                 {
497                                         send_to_user = false;
498                                         break;
499                                 }
500                         }
501                         if (send_to_user)
502                         {
503                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
504                         }
505                 }
506         }
507         else if (flags == WM_OR)
508         {
509                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
510                 {
511                         User* t = *i;
512                         bool send_to_user = false;
513
514                         for (int n = 0; n < modelen; n++)
515                         {
516                                 if (t->IsModeSet(modes[n]))
517                                 {
518                                         send_to_user = true;
519                                         break;
520                                 }
521                         }
522
523                         if (send_to_user)
524                         {
525                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
526                         }
527                 }
528         }
529 }
530
531 /* return how many users have a given mode e.g. 'a' */
532 int UserManager::ModeCount(const char mode)
533 {
534         ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
535
536         if (mh)
537                 return mh->GetCount();
538         else
539                 return 0;
540 }