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/>.
28 UserManager::UserManager()
29 : clientlist(new user_hash)
30 , uuidlist(new user_hash)
31 , unregistered_count(0), local_count(0)
35 UserManager::~UserManager()
37 for (user_hash::iterator i = clientlist->begin(); i != clientlist->end(); ++i)
46 /* add a client connection to the sockets list */
47 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
49 /* NOTE: Calling this one parameter constructor for User automatically
50 * allocates a new UUID and places it in the hash_map.
52 LocalUser* New = NULL;
55 New = new LocalUser(socket, client, server);
59 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "*** WTF *** Duplicated UUID! -- Crack smoking monkeys have been unleashed.");
60 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
63 UserIOHandler* eh = &New->eh;
65 /* Give each of the modules an attempt to hook the user for I/O */
66 FOREACH_MOD(OnHookIO, (eh, via));
72 eh->GetIOHook()->OnStreamSocketAccept(eh, client, server);
74 catch (CoreException& modexcept)
76 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "%s threw an exception: %s", modexcept.GetSource().c_str(), modexcept.GetReason().c_str());
80 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "New user fd: %d", socket);
82 this->unregistered_count++;
84 /* The users default nick is their UUID */
85 New->nick = New->uuid;
86 (*(this->clientlist))[New->nick] = New;
88 New->registered = REG_NONE;
89 New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
92 ServerInstance->Users->AddLocalClone(New);
93 ServerInstance->Users->AddGlobalClone(New);
95 New->localuseriter = this->local_users.insert(local_users.end(), New);
98 if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
100 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
101 this->QuitUser(New,"No more connections allowed");
106 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
107 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
112 * Check connect class settings and initialise settings into User.
113 * This will be done again after DNS resolution. -- w00t
115 New->CheckClass(ServerInstance->Config->CCOnConnect);
120 * even with bancache, we still have to keep User::exempt current.
121 * besides that, if we get a positive bancache hit, we still won't fuck
122 * them over if they are exempt. -- w00t
124 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
126 if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
128 if (!b->Type.empty() && !New->exempt)
131 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Positive hit for " + New->GetIPString());
132 if (!ServerInstance->Config->XLineMessage.empty())
133 New->WriteNotice("*** " + ServerInstance->Config->XLineMessage);
134 this->QuitUser(New, b->Reason);
139 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Negative hit for " + New->GetIPString());
146 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
156 if (!ServerInstance->SE->AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
158 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "Internal error on new connection");
159 this->QuitUser(New, "Internal error handling connection");
162 if (ServerInstance->Config->RawLog)
163 New->WriteNotice("*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
165 FOREACH_MOD(OnSetUserIP, (New));
169 FOREACH_MOD(OnUserInit, (New));
172 void UserManager::QuitUser(User* user, const std::string& quitreason, const std::string* operreason)
176 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
182 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
186 user->quitting = true;
188 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
189 user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), operreason ? operreason->c_str() : quitreason.c_str());
192 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
194 operreason = &reason;
196 ServerInstance->GlobalCulls.AddItem(user);
198 if (user->registered == REG_ALL)
200 FOREACH_MOD(OnUserQuit, (user, reason, *operreason));
201 user->WriteCommonQuit(reason, *operreason);
204 unregistered_count--;
208 LocalUser* lu = IS_LOCAL(user);
209 FOREACH_MOD(OnUserDisconnect, (lu));
212 if (lu->registered == REG_ALL)
213 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]", user->GetFullRealHost().c_str(), user->GetIPString().c_str(), operreason->c_str());
216 user_hash::iterator iter = this->clientlist->find(user->nick);
218 if (iter != this->clientlist->end())
219 this->clientlist->erase(iter);
221 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
223 uuidlist->erase(user->uuid);
226 void UserManager::AddLocalClone(User *user)
228 local_clones[user->GetCIDRMask()]++;
231 void UserManager::AddGlobalClone(User *user)
233 global_clones[user->GetCIDRMask()]++;
236 void UserManager::RemoveCloneCounts(User *user)
240 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
241 if (x != local_clones.end())
246 local_clones.erase(x);
251 clonemap::iterator y = global_clones.find(user->GetCIDRMask());
252 if (y != global_clones.end())
257 global_clones.erase(y);
262 unsigned long UserManager::GlobalCloneCount(User *user)
264 clonemap::iterator x = global_clones.find(user->GetCIDRMask());
265 if (x != global_clones.end())
271 unsigned long UserManager::LocalCloneCount(User *user)
273 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
274 if (x != local_clones.end())
280 void UserManager::ServerNoticeAll(const char* text, ...)
283 VAFORMAT(message, text, text);
284 message = "NOTICE $" + ServerInstance->Config->ServerName + " :" + message;
286 for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
289 t->WriteServ(message);
293 void UserManager::GarbageCollect()
295 // Reset the already_sent IDs so we don't wrap it around and drop a message
296 LocalUser::already_sent_id = 0;
297 for (LocalUserList::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
299 (**i).already_sent = 0;
300 (**i).RemoveExpiredInvites();
304 /* this returns true when all modules are satisfied that the user should be allowed onto the irc server
305 * (until this returns true, a user will block in the waiting state, waiting to connect up to the
306 * registration timeout maximum seconds)
308 bool UserManager::AllModulesReportReady(LocalUser* user)
311 FIRST_MOD_RESULT(OnCheckReady, res, (user));
312 return (res == MOD_RES_PASSTHRU);
316 * This function is called once a second from the mainloop.
317 * It is intended to do background checking on all the user structs, e.g.
318 * stuff like ping checks, registration timeouts, etc.
320 void UserManager::DoBackgroundUserStuff()
323 * loop over all local users..
325 for (LocalUserList::iterator i = local_users.begin(); i != local_users.end(); ++i)
327 LocalUser* curr = *i;
332 if (curr->CommandFloodPenalty || curr->eh.getSendQSize())
334 unsigned int rate = curr->MyClass->GetCommandRate();
335 if (curr->CommandFloodPenalty > rate)
336 curr->CommandFloodPenalty -= rate;
338 curr->CommandFloodPenalty = 0;
339 curr->eh.OnDataReady();
342 switch (curr->registered)
345 if (ServerInstance->Time() > curr->nping)
347 // This user didn't answer the last ping, remove them
350 time_t time = ServerInstance->Time() - (curr->nping - curr->MyClass->GetPingTime());
351 const std::string message = "Ping timeout: " + ConvToStr(time) + (time == 1 ? " seconds" : " second");
352 this->QuitUser(curr, message);
356 curr->Write("PING :" + ServerInstance->Config->ServerName);
358 curr->nping = ServerInstance->Time() + curr->MyClass->GetPingTime();
362 if (AllModulesReportReady(curr))
364 /* User has sent NICK/USER, modules are okay, DNS finished. */
371 if (curr->registered != REG_ALL && (ServerInstance->Time() > (curr->age + curr->MyClass->GetRegTimeout())))
374 * registration timeout -- didnt send USER/NICK/HOST
375 * in the time specified in their connection class.
377 this->QuitUser(curr, "Registration timeout");