]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Change OnHookUserIO to OnHookIO, making it usable for more than User* and less picky...
[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         // Move the user onto their UID, to allow nick to be reused immediately
208         user->UpdateNickHash(user->uuid.c_str());
209 }
210
211
212 void UserManager::AddLocalClone(User *user)
213 {
214         int range = 32;
215         clonemap::iterator x;
216         switch (user->client_sa.sa.sa_family)
217         {
218                 case AF_INET6:
219                         range = ServerInstance->Config->c_ipv6_range;
220                 break;
221                 case AF_INET:
222                         range = ServerInstance->Config->c_ipv4_range;
223                 break;
224         }
225
226         x = local_clones.find(user->GetCIDRMask(range));
227         if (x != local_clones.end())
228                 x->second++;
229         else
230                 local_clones[user->GetCIDRMask(range)] = 1;
231 }
232
233 void UserManager::AddGlobalClone(User *user)
234 {
235         int range = 32;
236         clonemap::iterator x;
237         switch (user->client_sa.sa.sa_family)
238         {
239                 case AF_INET6:
240                         range = ServerInstance->Config->c_ipv6_range;
241                 break;
242                 case AF_INET:
243                         range = ServerInstance->Config->c_ipv4_range;
244                 break;
245         }
246
247         x = global_clones.find(user->GetCIDRMask(range));
248         if (x != global_clones.end())
249                 x->second++;
250         else
251                 global_clones[user->GetCIDRMask(range)] = 1;
252 }
253
254 void UserManager::RemoveCloneCounts(User *user)
255 {
256         int range = 32;
257         switch (user->client_sa.sa.sa_family)
258         {
259                 case AF_INET6:
260                         range = ServerInstance->Config->c_ipv6_range;
261                 break;
262                 case AF_INET:
263                         range = ServerInstance->Config->c_ipv4_range;
264                 break;
265         }
266
267         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
268         if (x != local_clones.end())
269         {
270                 x->second--;
271                 if (!x->second)
272                 {
273                         local_clones.erase(x);
274                 }
275         }
276
277         clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
278         if (y != global_clones.end())
279         {
280                 y->second--;
281                 if (!y->second)
282                 {
283                         global_clones.erase(y);
284                 }
285         }
286 }
287
288 unsigned long UserManager::GlobalCloneCount(User *user)
289 {
290         int range = 32;
291         switch (user->client_sa.sa.sa_family)
292         {
293                 case AF_INET6:
294                         range = ServerInstance->Config->c_ipv6_range;
295                 break;
296                 case AF_INET:
297                         range = ServerInstance->Config->c_ipv4_range;
298                 break;
299         }
300         clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
301         if (x != global_clones.end())
302                 return x->second;
303         else
304                 return 0;
305 }
306
307 unsigned long UserManager::LocalCloneCount(User *user)
308 {
309         int range = 32;
310         switch (user->client_sa.sa.sa_family)
311         {
312                 case AF_INET6:
313                         range = ServerInstance->Config->c_ipv6_range;
314                 break;
315                 case AF_INET:
316                         range = ServerInstance->Config->c_ipv4_range;
317                 break;
318         }
319         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
320         if (x != local_clones.end())
321                 return x->second;
322         else
323                 return 0;
324 }
325
326 /* this function counts all users connected, wether they are registered or NOT. */
327 unsigned int UserManager::UserCount()
328 {
329         /*
330          * XXX: Todo:
331          *  As part of this restructuring, move clientlist/etc fields into usermanager.
332          *      -- w00t
333          */
334         return this->clientlist->size();
335 }
336
337 /* this counts only registered users, so that the percentages in /MAP don't mess up */
338 unsigned int UserManager::RegisteredUserCount()
339 {
340         return this->clientlist->size() - this->UnregisteredUserCount();
341 }
342
343 /* return how many users are opered */
344 unsigned int UserManager::OperCount()
345 {
346         return this->all_opers.size();
347 }
348
349 /* return how many users are unregistered */
350 unsigned int UserManager::UnregisteredUserCount()
351 {
352         return this->unregistered_count;
353 }
354
355 /* return how many local registered users there are */
356 unsigned int UserManager::LocalUserCount()
357 {
358         /* Doesnt count unregistered clients */
359         return (this->local_users.size() - this->UnregisteredUserCount());
360 }
361
362 void UserManager::ServerNoticeAll(const char* text, ...)
363 {
364         if (!text)
365                 return;
366
367         char textbuffer[MAXBUF];
368         char formatbuffer[MAXBUF];
369         va_list argsPtr;
370         va_start (argsPtr, text);
371         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
372         va_end(argsPtr);
373
374         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
375
376         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
377         {
378                 User* t = *i;
379                 t->WriteServ(std::string(formatbuffer));
380         }
381 }
382
383 void UserManager::ServerPrivmsgAll(const char* text, ...)
384 {
385         if (!text)
386                 return;
387
388         char textbuffer[MAXBUF];
389         char formatbuffer[MAXBUF];
390         va_list argsPtr;
391         va_start (argsPtr, text);
392         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
393         va_end(argsPtr);
394
395         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
396
397         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
398         {
399                 User* t = *i;
400                 t->WriteServ(std::string(formatbuffer));
401         }
402 }
403
404 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
405 {
406         char textbuffer[MAXBUF];
407         int modelen;
408         va_list argsPtr;
409
410         if (!text || !modes || !flags)
411         {
412                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
413                 return;
414         }
415
416         va_start(argsPtr, text);
417         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
418         va_end(argsPtr);
419         modelen = strlen(modes);
420
421         if (flags == WM_AND)
422         {
423                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
424                 {
425                         User* t = *i;
426                         bool send_to_user = true;
427
428                         for (int n = 0; n < modelen; n++)
429                         {
430                                 if (!t->IsModeSet(modes[n]))
431                                 {
432                                         send_to_user = false;
433                                         break;
434                                 }
435                         }
436                         if (send_to_user)
437                         {
438                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
439                         }
440                 }
441         }
442         else if (flags == WM_OR)
443         {
444                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
445                 {
446                         User* t = *i;
447                         bool send_to_user = false;
448
449                         for (int n = 0; n < modelen; n++)
450                         {
451                                 if (t->IsModeSet(modes[n]))
452                                 {
453                                         send_to_user = true;
454                                         break;
455                                 }
456                         }
457
458                         if (send_to_user)
459                         {
460                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
461                         }
462                 }
463         }
464 }
465
466 /* return how many users have a given mode e.g. 'a' */
467 int UserManager::ModeCount(const char mode)
468 {
469         ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
470
471         if (mh)
472                 return mh->GetCount();
473         else
474                 return 0;
475 }