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 /* add a client connection to the sockets list */
28 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
30 /* NOTE: Calling this one parameter constructor for User automatically
31 * allocates a new UUID and places it in the hash_map.
33 LocalUser* New = NULL;
36 New = new LocalUser(socket, client, server);
40 ServerInstance->Logs->Log("USERS", LOG_DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkeys have been unleashed.");
41 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
44 UserIOHandler* eh = &New->eh;
46 /* Give each of the modules an attempt to hook the user for I/O */
47 FOREACH_MOD(I_OnHookIO, OnHookIO(eh, via));
53 eh->GetIOHook()->OnStreamSocketAccept(eh, client, server);
55 catch (CoreException& modexcept)
57 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
61 ServerInstance->Logs->Log("USERS", LOG_DEBUG,"New user fd: %d", socket);
63 this->unregistered_count++;
65 /* The users default nick is their UUID */
66 New->nick = New->uuid;
67 (*(this->clientlist))[New->nick] = New;
69 New->registered = REG_NONE;
70 New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
73 ServerInstance->Users->AddLocalClone(New);
74 ServerInstance->Users->AddGlobalClone(New);
76 New->localuseriter = this->local_users.insert(local_users.end(), New);
78 if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
80 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
81 this->QuitUser(New,"No more connections allowed");
86 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
87 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
92 * Check connect class settings and initialise settings into User.
93 * This will be done again after DNS resolution. -- w00t
100 * even with bancache, we still have to keep User::exempt current.
101 * besides that, if we get a positive bancache hit, we still won't fuck
102 * them over if they are exempt. -- w00t
104 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
106 if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
108 if (!b->Type.empty() && !New->exempt)
111 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Positive hit for " + New->GetIPString());
112 if (!ServerInstance->Config->MoronBanner.empty())
113 New->WriteNotice("*** " + ServerInstance->Config->MoronBanner);
114 this->QuitUser(New, b->Reason);
119 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Negative hit for " + New->GetIPString());
126 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
136 if (!ServerInstance->SE->AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
138 ServerInstance->Logs->Log("USERS", LOG_DEBUG,"Internal error on new connection");
139 this->QuitUser(New, "Internal error handling connection");
142 if (ServerInstance->Config->RawLog)
143 New->WriteNotice("*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
145 FOREACH_MOD(I_OnSetUserIP,OnSetUserIP(New));
149 FOREACH_MOD(I_OnUserInit,OnUserInit(New));
152 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
156 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
162 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
166 user->quitting = true;
168 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
169 user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
172 std::string oper_reason;
173 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
174 if (operreason && *operreason)
175 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
177 oper_reason = quitreason;
179 ServerInstance->GlobalCulls.AddItem(user);
181 if (user->registered == REG_ALL)
183 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
184 user->WriteCommonQuit(reason, oper_reason);
187 if (user->registered != REG_ALL)
188 if (ServerInstance->Users->unregistered_count)
189 ServerInstance->Users->unregistered_count--;
193 LocalUser* lu = IS_LOCAL(user);
194 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(lu));
199 * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
200 * if they were an oper with +s +qQ.
202 if (user->registered == REG_ALL)
206 if (!user->quietquit)
208 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]",
209 user->GetFullRealHost().c_str(), user->GetIPString().c_str(), oper_reason.c_str());
214 if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
216 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s (%s) [%s]",
217 user->server.c_str(), user->GetFullRealHost().c_str(), user->GetIPString().c_str(), oper_reason.c_str());
222 user_hash::iterator iter = this->clientlist->find(user->nick);
224 if (iter != this->clientlist->end())
225 this->clientlist->erase(iter);
227 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
229 ServerInstance->Users->uuidlist->erase(user->uuid);
233 void UserManager::AddLocalClone(User *user)
235 local_clones[user->GetCIDRMask()]++;
238 void UserManager::AddGlobalClone(User *user)
240 global_clones[user->GetCIDRMask()]++;
243 void UserManager::RemoveCloneCounts(User *user)
247 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
248 if (x != local_clones.end())
253 local_clones.erase(x);
258 clonemap::iterator y = global_clones.find(user->GetCIDRMask());
259 if (y != global_clones.end())
264 global_clones.erase(y);
269 unsigned long UserManager::GlobalCloneCount(User *user)
271 clonemap::iterator x = global_clones.find(user->GetCIDRMask());
272 if (x != global_clones.end())
278 unsigned long UserManager::LocalCloneCount(User *user)
280 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
281 if (x != local_clones.end())
287 /* this function counts all users connected, wether they are registered or NOT. */
288 unsigned int UserManager::UserCount()
292 * As part of this restructuring, move clientlist/etc fields into usermanager.
295 return this->clientlist->size();
298 /* this counts only registered users, so that the percentages in /MAP don't mess up */
299 unsigned int UserManager::RegisteredUserCount()
301 return this->clientlist->size() - this->UnregisteredUserCount();
304 /* return how many users are opered */
305 unsigned int UserManager::OperCount()
307 return this->all_opers.size();
310 /* return how many users are unregistered */
311 unsigned int UserManager::UnregisteredUserCount()
313 return this->unregistered_count;
316 /* return how many local registered users there are */
317 unsigned int UserManager::LocalUserCount()
319 /* Doesnt count unregistered clients */
320 return (this->local_users.size() - this->UnregisteredUserCount());
323 void UserManager::ServerNoticeAll(const char* text, ...)
325 char textbuffer[MAXBUF];
327 va_start (argsPtr, text);
328 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
331 const std::string message = "NOTICE $" + ServerInstance->Config->ServerName + " :" + textbuffer;
333 for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
336 t->WriteServ(message);
340 /* return how many users have a given mode e.g. 'a' */
341 int UserManager::ModeCount(const char mode)
344 for(user_hash::iterator i = clientlist->begin(); i != clientlist->end(); ++i)
347 if (u->modes[mode-65])
353 void UserManager::GarbageCollect()
355 // Reset the already_sent IDs so we don't wrap it around and drop a message
356 LocalUser::already_sent_id = 0;
357 for (LocalUserList::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
359 (**i).already_sent = 0;
360 (**i).RemoveExpiredInvites();