]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Remove now-unused quitmsg/operquitmsg fields from User
[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         std::string reason;
180         std::string oper_reason;
181         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
182         if (operreason && *operreason)
183                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
184         else
185                 oper_reason = quitreason;
186
187         ServerInstance->GlobalCulls.AddItem(user);
188
189         if (user->registered == REG_ALL)
190         {
191                 FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
192                 user->PurgeEmptyChannels();
193                 user->WriteCommonQuit(reason, oper_reason);
194         }
195
196         FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(user));
197
198         user->UpdateNickHash(user->uuid.c_str());
199
200         user_hash::iterator iter = this->clientlist->find(user->uuid);
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                 if (!user->sendq.empty())
209                         user->FlushWriteBuf();
210
211                 if (user->GetIOHook())
212                 {
213                         try
214                         {
215                                 user->GetIOHook()->OnRawSocketClose(user->GetFd());
216                         }
217                         catch (CoreException& modexcept)
218                         {
219                                 ServerInstance->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
220                         }
221                 }
222
223                 ServerInstance->SE->DelFd(user);
224                 user->CloseSocket();
225         }
226
227         /*
228          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
229          * if they were an oper with +sn +qQ.
230          */
231         if (user->registered == REG_ALL)
232         {
233                 if (IS_LOCAL(user))
234                 {
235                         if (!user->quietquit)
236                         {
237                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",
238                                         user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
239                         }
240                 }
241                 else
242                 {
243                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
244                         {
245                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",
246                                         user->server, user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
247                         }
248                 }
249                 user->AddToWhoWas();
250         }
251
252         if (iter != this->clientlist->end())
253                 this->clientlist->erase(iter);
254         else
255                 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
256
257         if (IS_LOCAL(user))
258         {
259                 std::vector<User*>::iterator x = find(local_users.begin(),local_users.end(),user);
260                 if (x != local_users.end())
261                         local_users.erase(x);
262                 else
263                         ServerInstance->Logs->Log("USERS", DEBUG, "Failed to remove user from vector");
264         }
265 }
266
267
268 void UserManager::AddLocalClone(User *user)
269 {
270         int range = 32;
271         clonemap::iterator x;
272         switch (user->client_sa.sa.sa_family)
273         {
274                 case AF_INET6:
275                         range = ServerInstance->Config->c_ipv6_range;
276                 break;
277                 case AF_INET:
278                         range = ServerInstance->Config->c_ipv4_range;
279                 break;
280         }
281
282         x = local_clones.find(user->GetCIDRMask(range));
283         if (x != local_clones.end())
284                 x->second++;
285         else
286                 local_clones[user->GetCIDRMask(range)] = 1;
287 }
288
289 void UserManager::AddGlobalClone(User *user)
290 {
291         int range = 32;
292         clonemap::iterator x;
293         switch (user->client_sa.sa.sa_family)
294         {
295                 case AF_INET6:
296                         range = ServerInstance->Config->c_ipv6_range;
297                 break;
298                 case AF_INET:
299                         range = ServerInstance->Config->c_ipv4_range;
300                 break;
301         }
302
303         x = global_clones.find(user->GetCIDRMask(range));
304         if (x != global_clones.end())
305                 x->second++;
306         else
307                 global_clones[user->GetCIDRMask(range)] = 1;
308 }
309
310 void UserManager::RemoveCloneCounts(User *user)
311 {
312         int range = 32;
313         switch (user->client_sa.sa.sa_family)
314         {
315                 case AF_INET6:
316                         range = ServerInstance->Config->c_ipv6_range;
317                 break;
318                 case AF_INET:
319                         range = ServerInstance->Config->c_ipv4_range;
320                 break;
321         }
322
323         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
324         if (x != local_clones.end())
325         {
326                 x->second--;
327                 if (!x->second)
328                 {
329                         local_clones.erase(x);
330                 }
331         }
332
333         clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
334         if (y != global_clones.end())
335         {
336                 y->second--;
337                 if (!y->second)
338                 {
339                         global_clones.erase(y);
340                 }
341         }
342 }
343
344 unsigned long UserManager::GlobalCloneCount(User *user)
345 {
346         int range = 32;
347         switch (user->client_sa.sa.sa_family)
348         {
349                 case AF_INET6:
350                         range = ServerInstance->Config->c_ipv6_range;
351                 break;
352                 case AF_INET:
353                         range = ServerInstance->Config->c_ipv4_range;
354                 break;
355         }
356         clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
357         if (x != global_clones.end())
358                 return x->second;
359         else
360                 return 0;
361 }
362
363 unsigned long UserManager::LocalCloneCount(User *user)
364 {
365         int range = 32;
366         switch (user->client_sa.sa.sa_family)
367         {
368                 case AF_INET6:
369                         range = ServerInstance->Config->c_ipv6_range;
370                 break;
371                 case AF_INET:
372                         range = ServerInstance->Config->c_ipv4_range;
373                 break;
374         }
375         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
376         if (x != local_clones.end())
377                 return x->second;
378         else
379                 return 0;
380 }
381
382 /* this function counts all users connected, wether they are registered or NOT. */
383 unsigned int UserManager::UserCount()
384 {
385         /*
386          * XXX: Todo:
387          *  As part of this restructuring, move clientlist/etc fields into usermanager.
388          *      -- w00t
389          */
390         return this->clientlist->size();
391 }
392
393 /* this counts only registered users, so that the percentages in /MAP don't mess up */
394 unsigned int UserManager::RegisteredUserCount()
395 {
396         return this->clientlist->size() - this->UnregisteredUserCount();
397 }
398
399 /* return how many users are opered */
400 unsigned int UserManager::OperCount()
401 {
402         return this->all_opers.size();
403 }
404
405 /* return how many users are unregistered */
406 unsigned int UserManager::UnregisteredUserCount()
407 {
408         return this->unregistered_count;
409 }
410
411 /* return how many local registered users there are */
412 unsigned int UserManager::LocalUserCount()
413 {
414         /* Doesnt count unregistered clients */
415         return (this->local_users.size() - this->UnregisteredUserCount());
416 }
417
418 void UserManager::ServerNoticeAll(const char* text, ...)
419 {
420         if (!text)
421                 return;
422
423         char textbuffer[MAXBUF];
424         char formatbuffer[MAXBUF];
425         va_list argsPtr;
426         va_start (argsPtr, text);
427         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
428         va_end(argsPtr);
429
430         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
431
432         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
433         {
434                 User* t = *i;
435                 t->WriteServ(std::string(formatbuffer));
436         }
437 }
438
439 void UserManager::ServerPrivmsgAll(const char* text, ...)
440 {
441         if (!text)
442                 return;
443
444         char textbuffer[MAXBUF];
445         char formatbuffer[MAXBUF];
446         va_list argsPtr;
447         va_start (argsPtr, text);
448         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
449         va_end(argsPtr);
450
451         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
452
453         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
454         {
455                 User* t = *i;
456                 t->WriteServ(std::string(formatbuffer));
457         }
458 }
459
460 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
461 {
462         char textbuffer[MAXBUF];
463         int modelen;
464         va_list argsPtr;
465
466         if (!text || !modes || !flags)
467         {
468                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
469                 return;
470         }
471
472         va_start(argsPtr, text);
473         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
474         va_end(argsPtr);
475         modelen = strlen(modes);
476
477         if (flags == WM_AND)
478         {
479                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
480                 {
481                         User* t = *i;
482                         bool send_to_user = true;
483
484                         for (int n = 0; n < modelen; n++)
485                         {
486                                 if (!t->IsModeSet(modes[n]))
487                                 {
488                                         send_to_user = false;
489                                         break;
490                                 }
491                         }
492                         if (send_to_user)
493                         {
494                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
495                         }
496                 }
497         }
498         else if (flags == WM_OR)
499         {
500                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
501                 {
502                         User* t = *i;
503                         bool send_to_user = false;
504
505                         for (int n = 0; n < modelen; n++)
506                         {
507                                 if (t->IsModeSet(modes[n]))
508                                 {
509                                         send_to_user = true;
510                                         break;
511                                 }
512                         }
513
514                         if (send_to_user)
515                         {
516                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
517                         }
518                 }
519         }
520 }
521
522 /* return how many users have a given mode e.g. 'a' */
523 int UserManager::ModeCount(const char mode)
524 {
525         ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
526
527         if (mh)
528                 return mh->GetCount();
529         else
530                 return 0;
531 }