2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 UserManager::UserManager()
28 : unregistered_count(0), local_count(0)
32 /* add a client connection to the sockets list */
33 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
35 /* NOTE: Calling this one parameter constructor for User automatically
36 * allocates a new UUID and places it in the hash_map.
38 LocalUser* New = NULL;
41 New = new LocalUser(socket, client, server);
45 ServerInstance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkeys have been unleashed.");
46 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
49 UserIOHandler* eh = &New->eh;
51 /* Give each of the modules an attempt to hook the user for I/O */
52 FOREACH_MOD(I_OnHookIO, OnHookIO(eh, via));
58 eh->GetIOHook()->OnStreamSocketAccept(eh, client, server);
60 catch (CoreException& modexcept)
62 ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
66 ServerInstance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
68 this->unregistered_count++;
70 /* The users default nick is their UUID */
71 New->nick = New->uuid;
72 (*(this->clientlist))[New->nick] = New;
74 New->registered = REG_NONE;
75 New->signon = ServerInstance->Time();
78 ServerInstance->Users->AddLocalClone(New);
79 ServerInstance->Users->AddGlobalClone(New);
81 New->localuseriter = this->local_users.insert(local_users.end(), New);
84 if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
86 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
87 this->QuitUser(New,"No more connections allowed");
92 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
93 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
98 * Check connect class settings and initialise settings into User.
99 * This will be done again after DNS resolution. -- w00t
106 * even with bancache, we still have to keep User::exempt current.
107 * besides that, if we get a positive bancache hit, we still won't fuck
108 * them over if they are exempt. -- w00t
110 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
112 if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
114 if (!b->Type.empty() && !New->exempt)
117 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
118 if (!ServerInstance->Config->MoronBanner.empty())
119 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
120 this->QuitUser(New, b->Reason);
125 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
132 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
142 if (!ServerInstance->SE->AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
144 ServerInstance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
145 this->QuitUser(New, "Internal error handling connection");
149 /* NOTE: even if dns lookups are *off*, we still need to display this.
150 * BOPM and other stuff requires it.
152 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
153 if (ServerInstance->Config->RawLog)
154 New->WriteServ("NOTICE Auth :*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
156 FOREACH_MOD(I_OnSetUserIP,OnSetUserIP(New));
160 FOREACH_MOD(I_OnUserInit,OnUserInit(New));
162 if (ServerInstance->Config->NoUserDns)
164 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
165 New->dns_done = true;
169 New->StartDNSLookup();
173 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
177 ServerInstance->Logs->Log("USERS", DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
183 ServerInstance->Logs->Log("USERS", DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
187 user->quitting = true;
189 ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
190 user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
193 std::string oper_reason;
194 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
195 if (operreason && *operreason)
196 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
198 oper_reason = quitreason;
200 ServerInstance->GlobalCulls.AddItem(user);
202 if (user->registered == REG_ALL)
204 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
205 user->WriteCommonQuit(reason, oper_reason);
208 if (user->registered != REG_ALL)
209 if (ServerInstance->Users->unregistered_count)
210 ServerInstance->Users->unregistered_count--;
214 LocalUser* lu = IS_LOCAL(user);
215 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(lu));
220 * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
221 * if they were an oper with +s +qQ.
223 if (user->registered == REG_ALL)
227 if (!user->quietquit)
229 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]",
230 user->GetFullRealHost().c_str(), user->GetIPString(), oper_reason.c_str());
235 if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
237 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s (%s) [%s]",
238 user->server.c_str(), user->GetFullRealHost().c_str(), user->GetIPString(), oper_reason.c_str());
244 user_hash::iterator iter = this->clientlist->find(user->nick);
246 if (iter != this->clientlist->end())
247 this->clientlist->erase(iter);
249 ServerInstance->Logs->Log("USERS", DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
251 ServerInstance->Users->uuidlist->erase(user->uuid);
255 void UserManager::AddLocalClone(User *user)
257 local_clones[user->GetCIDRMask()]++;
260 void UserManager::AddGlobalClone(User *user)
262 global_clones[user->GetCIDRMask()]++;
265 void UserManager::RemoveCloneCounts(User *user)
269 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
270 if (x != local_clones.end())
275 local_clones.erase(x);
280 clonemap::iterator y = global_clones.find(user->GetCIDRMask());
281 if (y != global_clones.end())
286 global_clones.erase(y);
291 void UserManager::RehashCloneCounts()
293 local_clones.clear();
294 global_clones.clear();
296 const user_hash& hash = *ServerInstance->Users->clientlist;
297 for (user_hash::const_iterator i = hash.begin(); i != hash.end(); ++i)
307 unsigned long UserManager::GlobalCloneCount(User *user)
309 clonemap::iterator x = global_clones.find(user->GetCIDRMask());
310 if (x != global_clones.end())
316 unsigned long UserManager::LocalCloneCount(User *user)
318 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
319 if (x != local_clones.end())
325 /* this function counts all users connected, wether they are registered or NOT. */
326 unsigned int UserManager::UserCount()
330 * As part of this restructuring, move clientlist/etc fields into usermanager.
333 return this->clientlist->size();
336 /* this counts only registered users, so that the percentages in /MAP don't mess up */
337 unsigned int UserManager::RegisteredUserCount()
339 return this->clientlist->size() - this->UnregisteredUserCount();
342 /* return how many users are opered */
343 unsigned int UserManager::OperCount()
345 return this->all_opers.size();
348 /* return how many users are unregistered */
349 unsigned int UserManager::UnregisteredUserCount()
351 return this->unregistered_count;
354 /* return how many local registered users there are */
355 unsigned int UserManager::LocalUserCount()
357 /* Doesnt count unregistered clients */
358 return (this->local_count - this->UnregisteredUserCount());
361 void UserManager::ServerNoticeAll(const char* text, ...)
366 char textbuffer[MAXBUF];
367 char formatbuffer[MAXBUF];
369 va_start (argsPtr, text);
370 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
373 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
375 for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
378 t->WriteServ(std::string(formatbuffer));
382 void UserManager::ServerPrivmsgAll(const char* text, ...)
387 char textbuffer[MAXBUF];
388 char formatbuffer[MAXBUF];
390 va_start (argsPtr, text);
391 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
394 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
396 for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
399 t->WriteServ(std::string(formatbuffer));
404 /* return how many users have a given mode e.g. 'a' */
405 int UserManager::ModeCount(const char mode)
408 for(user_hash::iterator i = clientlist->begin(); i != clientlist->end(); ++i)
411 if (u->modes[mode-65])