]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Convert snomask 'A' into snomask 'a' for local announcements, add snomask 'A' for...
[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, int port, bool iscached, sockaddr* ip, const std::string &targetip)
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         char ipaddr[MAXBUF];
39 #ifdef IPV6
40         if (ip->sa_family == AF_INET6)
41                 inet_ntop(AF_INET6, &((const sockaddr_in6*)ip)->sin6_addr, ipaddr, sizeof(ipaddr));
42         else
43 #endif
44                 inet_ntop(AF_INET, &((const sockaddr_in*)ip)->sin_addr, ipaddr, sizeof(ipaddr));
45
46         New->SetFd(socket);
47         New->SetSockAddr(ip->sa_family, ipaddr, port);
48
49         /* Give each of the modules an attempt to hook the user for I/O */
50         FOREACH_MOD_I(Instance, I_OnHookUserIO, OnHookUserIO(New, targetip));
51
52         if (New->GetIOHook())
53         {
54                 try
55                 {
56                         New->GetIOHook()->OnRawSocketAccept(socket, ipaddr, port);
57                 }
58                 catch (CoreException& modexcept)
59                 {
60                         ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
61                 }
62         }
63
64         Instance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
65
66         this->unregistered_count++;
67
68         (*(this->clientlist))[New->uuid] = New;
69
70         /* The users default nick is their UUID */
71         New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
72
73         New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
74         New->ident.assign("unknown");
75
76         New->registered = REG_NONE;
77         New->signon = Instance->Time() + Instance->Config->dns_timeout;
78         New->lastping = 1;
79
80         /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
81         New->dhost.assign(New->GetIPString(), 0, 64);
82         New->host.assign(New->GetIPString(), 0, 64);
83
84         Instance->Users->AddLocalClone(New);
85         Instance->Users->AddGlobalClone(New);
86
87         /*
88          * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
89          * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
90          */
91         ConnectClass* i = New->SetClass();
92
93         if (!i)
94         {
95                 this->QuitUser(New, "Access denied by configuration");
96                 return;
97         }
98
99         /*
100          * Check connect class settings and initialise settings into User.
101          * This will be done again after DNS resolution. -- w00t
102          */
103         New->CheckClass();
104
105         this->local_users.push_back(New);
106
107         if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)Instance->SE->GetMaxFds()))
108         {
109                 Instance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
110                 this->QuitUser(New,"No more connections allowed");
111                 return;
112         }
113
114         /*
115          * even with bancache, we still have to keep User::exempt current.
116          * besides that, if we get a positive bancache hit, we still won't fuck
117          * them over if they are exempt. -- w00t
118          */
119         New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
120
121         if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
122         {
123                 if (!b->Type.empty() && !New->exempt)
124                 {
125                         /* user banned */
126                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
127                         if (*Instance->Config->MoronBanner)
128                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), Instance->Config->MoronBanner);
129                         this->QuitUser(New, b->Reason);
130                         return;
131                 }
132                 else
133                 {
134                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
135                 }
136         }
137         else
138         {
139                 if (!New->exempt)
140                 {
141                         XLine* r = Instance->XLines->MatchesLine("Z",New);
142
143                         if (r)
144                         {
145                                 r->Apply(New);
146                                 return;
147                         }
148                 }
149         }
150
151         if (!Instance->SE->AddFd(New))
152         {
153                 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
154                 this->QuitUser(New, "Internal error handling connection");
155         }
156
157         /* NOTE: even if dns lookups are *off*, we still need to display this.
158          * BOPM and other stuff requires it.
159          */
160         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
161
162         if (Instance->Config->NoUserDns)
163         {
164                 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
165                 New->dns_done = true;
166         }
167         else
168         {
169                 New->StartDNSLookup();
170         }
171 }
172
173 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
174 {
175         if (user->quitting)
176         {
177                 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
178                 return;
179         }
180
181         user->quitting = true;
182
183         ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s '%s'", 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         user->quietquit = false;
187         user->quitmsg = quitreason;
188
189         std::string reason;
190         std::string oper_reason;
191         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
192         if (!*operreason)
193         {
194                 user->operquitmsg = quitreason;
195                 oper_reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
196         }
197         else
198         {
199                 user->operquitmsg = operreason;
200                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
201         }
202
203         ServerInstance->GlobalCulls.AddItem(user);
204
205         if (user->registered == REG_ALL)
206         {
207                 FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
208                 user->PurgeEmptyChannels();
209                 user->WriteCommonQuit(reason, oper_reason);
210         }
211
212         FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(user));
213
214         // Move the user onto their UID, to allow nick to be reused immediately
215         user->UpdateNickHash(user->uuid.c_str());
216 }
217
218
219 void UserManager::AddLocalClone(User *user)
220 {
221         int range = 32;
222         clonemap::iterator x;
223         switch (user->GetProtocolFamily())
224         {
225 #ifdef SUPPORT_IP6LINKS
226                 case AF_INET6:
227                 {
228                         range = ServerInstance->Config->c_ipv6_range;
229                 }
230                 break;
231 #endif
232                 case AF_INET:
233                 {
234                         range = ServerInstance->Config->c_ipv4_range;
235                 }
236                 break;
237         }
238
239         x = local_clones.find(user->GetCIDRMask(range));
240         if (x != local_clones.end())
241                 x->second++;
242         else
243                 local_clones[user->GetCIDRMask(range)] = 1;
244 }
245
246 void UserManager::AddGlobalClone(User *user)
247 {
248         int range = 32;
249         clonemap::iterator x;
250         switch (user->GetProtocolFamily())
251         {
252 #ifdef SUPPORT_IP6LINKS
253                 case AF_INET6:
254                 {
255                         range = ServerInstance->Config->c_ipv6_range;
256                 }
257                 break;
258 #endif
259                 case AF_INET:
260                 {
261                         range = ServerInstance->Config->c_ipv4_range;
262                 }
263                 break;
264         }
265
266         x = global_clones.find(user->GetCIDRMask(range));
267         if (x != global_clones.end())
268                 x->second++;
269         else
270                 global_clones[user->GetCIDRMask(range)] = 1;
271 }
272
273 void UserManager::RemoveCloneCounts(User *user)
274 {
275         int range = 32;
276         switch (user->GetProtocolFamily())
277         {
278 #ifdef SUPPORT_IP6LINKS
279                 case AF_INET6:
280                 {
281                         range = ServerInstance->Config->c_ipv6_range;
282                 }
283                 break;
284 #endif
285                 case AF_INET:
286                 {
287                         range = ServerInstance->Config->c_ipv4_range;
288                 }
289                 break;
290         }
291
292         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
293         if (x != local_clones.end())
294         {
295                 x->second--;
296                 if (!x->second)
297                 {
298                         local_clones.erase(x);
299                 }
300         }
301
302         clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
303         if (y != global_clones.end())
304         {
305                 y->second--;
306                 if (!y->second)
307                 {
308                         global_clones.erase(y);
309                 }
310         }
311 }
312
313 unsigned long UserManager::GlobalCloneCount(User *user)
314 {
315         int range = 32;
316         switch (user->GetProtocolFamily())
317         {
318 #ifdef SUPPORT_IP6LINKS
319                 case AF_INET6:
320                 {
321                         range = ServerInstance->Config->c_ipv6_range;
322                 }
323                 break;
324 #endif
325                 case AF_INET:
326                 {
327                         range = ServerInstance->Config->c_ipv4_range;
328                 }
329                 break;
330         }
331         clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
332         if (x != global_clones.end())
333                 return x->second;
334         else
335                 return 0;
336 }
337
338 unsigned long UserManager::LocalCloneCount(User *user)
339 {
340         int range = 32;
341         switch (user->GetProtocolFamily())
342         {
343 #ifdef SUPPORT_IP6LINKS
344                 case AF_INET6:
345                 {
346                         range = ServerInstance->Config->c_ipv6_range;
347                 }
348                 break;
349 #endif
350                 case AF_INET:
351                 {
352                         range = ServerInstance->Config->c_ipv4_range;
353                 }
354                 break;
355         }
356         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
357         if (x != local_clones.end())
358                 return x->second;
359         else
360                 return 0;
361 }
362
363 /* this function counts all users connected, wether they are registered or NOT. */
364 unsigned int UserManager::UserCount()
365 {
366         /*
367          * XXX: Todo:
368          *  As part of this restructuring, move clientlist/etc fields into usermanager.
369          *      -- w00t
370          */
371         return this->clientlist->size();
372 }
373
374 /* this counts only registered users, so that the percentages in /MAP don't mess up */
375 unsigned int UserManager::RegisteredUserCount()
376 {
377         return this->clientlist->size() - this->UnregisteredUserCount();
378 }
379
380 /* return how many users are opered */
381 unsigned int UserManager::OperCount()
382 {
383         return this->all_opers.size();
384 }
385
386 /* return how many users are unregistered */
387 unsigned int UserManager::UnregisteredUserCount()
388 {
389         return this->unregistered_count;
390 }
391
392 /* return how many local registered users there are */
393 unsigned int UserManager::LocalUserCount()
394 {
395         /* Doesnt count unregistered clients */
396         return (this->local_users.size() - this->UnregisteredUserCount());
397 }
398
399 void UserManager::ServerNoticeAll(const char* text, ...)
400 {
401         if (!text)
402                 return;
403
404         char textbuffer[MAXBUF];
405         char formatbuffer[MAXBUF];
406         va_list argsPtr;
407         va_start (argsPtr, text);
408         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
409         va_end(argsPtr);
410
411         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
412
413         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
414         {
415                 User* t = *i;
416                 t->WriteServ(std::string(formatbuffer));
417         }
418 }
419
420 void UserManager::ServerPrivmsgAll(const char* text, ...)
421 {
422         if (!text)
423                 return;
424
425         char textbuffer[MAXBUF];
426         char formatbuffer[MAXBUF];
427         va_list argsPtr;
428         va_start (argsPtr, text);
429         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
430         va_end(argsPtr);
431
432         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
433
434         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
435         {
436                 User* t = *i;
437                 t->WriteServ(std::string(formatbuffer));
438         }
439 }
440
441 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
442 {
443         char textbuffer[MAXBUF];
444         int modelen;
445         va_list argsPtr;
446
447         if (!text || !modes || !flags)
448         {
449                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
450                 return;
451         }
452
453         va_start(argsPtr, text);
454         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
455         va_end(argsPtr);
456         modelen = strlen(modes);
457
458         if (flags == WM_AND)
459         {
460                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
461                 {
462                         User* t = *i;
463                         bool send_to_user = true;
464
465                         for (int n = 0; n < modelen; n++)
466                         {
467                                 if (!t->IsModeSet(modes[n]))
468                                 {
469                                         send_to_user = false;
470                                         break;
471                                 }
472                         }
473                         if (send_to_user)
474                         {
475                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
476                         }
477                 }
478         }
479         else if (flags == WM_OR)
480         {
481                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
482                 {
483                         User* t = *i;
484                         bool send_to_user = false;
485
486                         for (int n = 0; n < modelen; n++)
487                         {
488                                 if (t->IsModeSet(modes[n]))
489                                 {
490                                         send_to_user = true;
491                                         break;
492                                 }
493                         }
494
495                         if (send_to_user)
496                         {
497                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
498                         }
499                 }
500         }
501 }
502
503 /* return how many users have a given mode e.g. 'a' */
504 int UserManager::ModeCount(const char mode)
505 {
506         ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
507
508         if (mh)
509                 return mh->GetCount();
510         else
511                 return 0;
512 }