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