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 : unregistered_count(0)
33 UserManager::~UserManager()
35 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); ++i)
41 /* add a client connection to the sockets list */
42 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
44 /* NOTE: Calling this one parameter constructor for User automatically
45 * allocates a new UUID and places it in the hash_map.
47 LocalUser* New = NULL;
50 New = new LocalUser(socket, client, server);
54 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "*** WTF *** Duplicated UUID! -- Crack smoking monkeys have been unleashed.");
55 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
58 UserIOHandler* eh = &New->eh;
60 // If this listener has an IO hook provider set then tell it about the connection
62 via->iohookprov->OnAccept(eh, client, server);
64 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "New user fd: %d", socket);
66 this->unregistered_count++;
68 /* The users default nick is their UUID */
69 New->nick = New->uuid;
70 this->clientlist[New->nick] = New;
72 New->registered = REG_NONE;
73 New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
76 ServerInstance->Users->AddLocalClone(New);
77 ServerInstance->Users->AddGlobalClone(New);
79 this->local_users.push_front(New);
81 if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)SocketEngine::GetMaxFds()))
83 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
84 this->QuitUser(New,"No more connections allowed");
89 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
90 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
95 * Check connect class settings and initialise settings into User.
96 * This will be done again after DNS resolution. -- w00t
98 New->CheckClass(ServerInstance->Config->CCOnConnect);
103 * even with bancache, we still have to keep User::exempt current.
104 * besides that, if we get a positive bancache hit, we still won't fuck
105 * them over if they are exempt. -- w00t
107 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
109 if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
111 if (!b->Type.empty() && !New->exempt)
114 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Positive hit for " + New->GetIPString());
115 if (!ServerInstance->Config->XLineMessage.empty())
116 New->WriteNotice("*** " + ServerInstance->Config->XLineMessage);
117 this->QuitUser(New, b->Reason);
122 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Negative hit for " + New->GetIPString());
129 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
139 if (!SocketEngine::AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
141 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "Internal error on new connection");
142 this->QuitUser(New, "Internal error handling connection");
145 if (ServerInstance->Config->RawLog)
146 New->WriteNotice("*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
148 FOREACH_MOD(OnSetUserIP, (New));
152 FOREACH_MOD(OnUserInit, (New));
155 void UserManager::QuitUser(User* user, const std::string& quitreason, const std::string* operreason)
159 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
165 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
169 user->quitting = true;
171 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
172 user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), operreason ? operreason->c_str() : quitreason.c_str());
175 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
177 operreason = &reason;
179 ServerInstance->GlobalCulls.AddItem(user);
181 if (user->registered == REG_ALL)
183 FOREACH_MOD(OnUserQuit, (user, reason, *operreason));
184 user->WriteCommonQuit(reason, *operreason);
187 unregistered_count--;
191 LocalUser* lu = IS_LOCAL(user);
192 FOREACH_MOD(OnUserDisconnect, (lu));
195 if (lu->registered == REG_ALL)
196 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]", user->GetFullRealHost().c_str(), user->GetIPString().c_str(), operreason->c_str());
199 if (!clientlist.erase(user->nick))
200 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
202 uuidlist.erase(user->uuid);
203 user->PurgeEmptyChannels();
206 void UserManager::AddLocalClone(User *user)
208 local_clones[user->GetCIDRMask()]++;
211 void UserManager::AddGlobalClone(User *user)
213 global_clones[user->GetCIDRMask()]++;
216 void UserManager::RemoveCloneCounts(User *user)
220 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
221 if (x != local_clones.end())
226 local_clones.erase(x);
231 clonemap::iterator y = global_clones.find(user->GetCIDRMask());
232 if (y != global_clones.end())
237 global_clones.erase(y);
242 unsigned long UserManager::GlobalCloneCount(User *user)
244 clonemap::iterator x = global_clones.find(user->GetCIDRMask());
245 if (x != global_clones.end())
251 unsigned long UserManager::LocalCloneCount(User *user)
253 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
254 if (x != local_clones.end())
260 void UserManager::ServerNoticeAll(const char* text, ...)
263 VAFORMAT(message, text, text);
264 message = "NOTICE $" + ServerInstance->Config->ServerName + " :" + message;
266 for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
269 t->WriteServ(message);
273 void UserManager::GarbageCollect()
275 // Reset the already_sent IDs so we don't wrap it around and drop a message
276 LocalUser::already_sent_id = 0;
277 for (LocalUserList::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
279 (**i).already_sent = 0;
280 (**i).RemoveExpiredInvites();
284 /* this returns true when all modules are satisfied that the user should be allowed onto the irc server
285 * (until this returns true, a user will block in the waiting state, waiting to connect up to the
286 * registration timeout maximum seconds)
288 bool UserManager::AllModulesReportReady(LocalUser* user)
291 FIRST_MOD_RESULT(OnCheckReady, res, (user));
292 return (res == MOD_RES_PASSTHRU);
296 * This function is called once a second from the mainloop.
297 * It is intended to do background checking on all the user structs, e.g.
298 * stuff like ping checks, registration timeouts, etc.
300 void UserManager::DoBackgroundUserStuff()
303 * loop over all local users..
305 for (LocalUserList::iterator i = local_users.begin(); i != local_users.end(); ++i)
307 LocalUser* curr = *i;
312 if (curr->CommandFloodPenalty || curr->eh.getSendQSize())
314 unsigned int rate = curr->MyClass->GetCommandRate();
315 if (curr->CommandFloodPenalty > rate)
316 curr->CommandFloodPenalty -= rate;
318 curr->CommandFloodPenalty = 0;
319 curr->eh.OnDataReady();
322 switch (curr->registered)
325 if (ServerInstance->Time() > curr->nping)
327 // This user didn't answer the last ping, remove them
330 time_t time = ServerInstance->Time() - (curr->nping - curr->MyClass->GetPingTime());
331 const std::string message = "Ping timeout: " + ConvToStr(time) + (time != 1 ? " seconds" : " second");
332 this->QuitUser(curr, message);
336 curr->Write("PING :" + ServerInstance->Config->ServerName);
338 curr->nping = ServerInstance->Time() + curr->MyClass->GetPingTime();
342 if (AllModulesReportReady(curr))
344 /* User has sent NICK/USER, modules are okay, DNS finished. */
351 if (curr->registered != REG_ALL && (ServerInstance->Time() > (curr->age + curr->MyClass->GetRegTimeout())))
354 * registration timeout -- didnt send USER/NICK/HOST
355 * in the time specified in their connection class.
357 this->QuitUser(curr, "Registration timeout");