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)
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 // If this listener has an IO hook provider set then tell it about the connection
67 via->iohookprov->OnAccept(eh, client, server);
69 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "New user fd: %d", socket);
71 this->unregistered_count++;
73 /* The users default nick is their UUID */
74 New->nick = New->uuid;
75 (*(this->clientlist))[New->nick] = New;
77 New->registered = REG_NONE;
78 New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
81 ServerInstance->Users->AddLocalClone(New);
82 ServerInstance->Users->AddGlobalClone(New);
84 this->local_users.push_front(New);
86 if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)SocketEngine::GetMaxFds()))
88 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
89 this->QuitUser(New,"No more connections allowed");
94 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
95 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
100 * Check connect class settings and initialise settings into User.
101 * This will be done again after DNS resolution. -- w00t
103 New->CheckClass(ServerInstance->Config->CCOnConnect);
108 * even with bancache, we still have to keep User::exempt current.
109 * besides that, if we get a positive bancache hit, we still won't fuck
110 * them over if they are exempt. -- w00t
112 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
114 if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
116 if (!b->Type.empty() && !New->exempt)
119 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Positive hit for " + New->GetIPString());
120 if (!ServerInstance->Config->XLineMessage.empty())
121 New->WriteNotice("*** " + ServerInstance->Config->XLineMessage);
122 this->QuitUser(New, b->Reason);
127 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Negative hit for " + New->GetIPString());
134 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
144 if (!SocketEngine::AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
146 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "Internal error on new connection");
147 this->QuitUser(New, "Internal error handling connection");
150 if (ServerInstance->Config->RawLog)
151 New->WriteNotice("*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
153 FOREACH_MOD(OnSetUserIP, (New));
157 FOREACH_MOD(OnUserInit, (New));
160 void UserManager::QuitUser(User* user, const std::string& quitreason, const std::string* operreason)
164 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
170 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
174 user->quitting = true;
176 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
177 user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), operreason ? operreason->c_str() : quitreason.c_str());
180 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
182 operreason = &reason;
184 ServerInstance->GlobalCulls.AddItem(user);
186 if (user->registered == REG_ALL)
188 FOREACH_MOD(OnUserQuit, (user, reason, *operreason));
189 user->WriteCommonQuit(reason, *operreason);
192 unregistered_count--;
196 LocalUser* lu = IS_LOCAL(user);
197 FOREACH_MOD(OnUserDisconnect, (lu));
200 if (lu->registered == REG_ALL)
201 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]", user->GetFullRealHost().c_str(), user->GetIPString().c_str(), operreason->c_str());
204 user_hash::iterator iter = this->clientlist->find(user->nick);
206 if (iter != this->clientlist->end())
207 this->clientlist->erase(iter);
209 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
211 uuidlist->erase(user->uuid);
212 user->PurgeEmptyChannels();
215 void UserManager::AddLocalClone(User *user)
217 local_clones[user->GetCIDRMask()]++;
220 void UserManager::AddGlobalClone(User *user)
222 global_clones[user->GetCIDRMask()]++;
225 void UserManager::RemoveCloneCounts(User *user)
229 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
230 if (x != local_clones.end())
235 local_clones.erase(x);
240 clonemap::iterator y = global_clones.find(user->GetCIDRMask());
241 if (y != global_clones.end())
246 global_clones.erase(y);
251 unsigned long UserManager::GlobalCloneCount(User *user)
253 clonemap::iterator x = global_clones.find(user->GetCIDRMask());
254 if (x != global_clones.end())
260 unsigned long UserManager::LocalCloneCount(User *user)
262 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
263 if (x != local_clones.end())
269 void UserManager::ServerNoticeAll(const char* text, ...)
272 VAFORMAT(message, text, text);
273 message = "NOTICE $" + ServerInstance->Config->ServerName + " :" + message;
275 for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
278 t->WriteServ(message);
282 void UserManager::GarbageCollect()
284 // Reset the already_sent IDs so we don't wrap it around and drop a message
285 LocalUser::already_sent_id = 0;
286 for (LocalUserList::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
288 (**i).already_sent = 0;
289 (**i).RemoveExpiredInvites();
293 /* this returns true when all modules are satisfied that the user should be allowed onto the irc server
294 * (until this returns true, a user will block in the waiting state, waiting to connect up to the
295 * registration timeout maximum seconds)
297 bool UserManager::AllModulesReportReady(LocalUser* user)
300 FIRST_MOD_RESULT(OnCheckReady, res, (user));
301 return (res == MOD_RES_PASSTHRU);
305 * This function is called once a second from the mainloop.
306 * It is intended to do background checking on all the user structs, e.g.
307 * stuff like ping checks, registration timeouts, etc.
309 void UserManager::DoBackgroundUserStuff()
312 * loop over all local users..
314 for (LocalUserList::iterator i = local_users.begin(); i != local_users.end(); ++i)
316 LocalUser* curr = *i;
321 if (curr->CommandFloodPenalty || curr->eh.getSendQSize())
323 unsigned int rate = curr->MyClass->GetCommandRate();
324 if (curr->CommandFloodPenalty > rate)
325 curr->CommandFloodPenalty -= rate;
327 curr->CommandFloodPenalty = 0;
328 curr->eh.OnDataReady();
331 switch (curr->registered)
334 if (ServerInstance->Time() > curr->nping)
336 // This user didn't answer the last ping, remove them
339 time_t time = ServerInstance->Time() - (curr->nping - curr->MyClass->GetPingTime());
340 const std::string message = "Ping timeout: " + ConvToStr(time) + (time != 1 ? " seconds" : " second");
341 this->QuitUser(curr, message);
345 curr->Write("PING :" + ServerInstance->Config->ServerName);
347 curr->nping = ServerInstance->Time() + curr->MyClass->GetPingTime();
351 if (AllModulesReportReady(curr))
353 /* User has sent NICK/USER, modules are okay, DNS finished. */
360 if (curr->registered != REG_ALL && (ServerInstance->Time() > (curr->age + curr->MyClass->GetRegTimeout())))
363 * registration timeout -- didnt send USER/NICK/HOST
364 * in the time specified in their connection class.
366 this->QuitUser(curr, "Registration timeout");