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