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/>.
29 class WriteCommonQuit : public User::ForEachNeighborHandler
34 void Execute(LocalUser* user) CXX11_OVERRIDE
36 user->Write(user->IsOper() ? operline : line);
40 WriteCommonQuit(User* user, const std::string& msg, const std::string& opermsg)
41 : line(":" + user->GetFullHost() + " QUIT :")
46 user->ForEachNeighbor(*this, false);
51 UserManager::UserManager()
53 , unregistered_count(0)
57 UserManager::~UserManager()
59 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); ++i)
65 /* add a client connection to the sockets list */
66 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
68 /* NOTE: Calling this one parameter constructor for User automatically
69 * allocates a new UUID and places it in the hash_map.
71 LocalUser* const New = new LocalUser(socket, client, server);
72 UserIOHandler* eh = &New->eh;
74 // If this listener has an IO hook provider set then tell it about the connection
76 via->iohookprov->OnAccept(eh, client, server);
78 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "New user fd: %d", socket);
80 this->unregistered_count++;
82 /* The users default nick is their UUID */
83 New->nick = New->uuid;
84 this->clientlist[New->nick] = New;
86 New->registered = REG_NONE;
87 New->signon = ServerInstance->Time();
92 this->local_users.push_front(New);
94 if (this->local_users.size() > ServerInstance->Config->SoftLimit)
96 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
97 this->QuitUser(New,"No more connections allowed");
102 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
103 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
108 * Check connect class settings and initialise settings into User.
109 * This will be done again after DNS resolution. -- w00t
111 New->CheckClass(ServerInstance->Config->CCOnConnect);
116 * even with bancache, we still have to keep User::exempt current.
117 * besides that, if we get a positive bancache hit, we still won't fuck
118 * them over if they are exempt. -- w00t
120 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
122 BanCacheHit* const b = ServerInstance->BanCache.GetHit(New->GetIPString());
125 if (!b->Type.empty() && !New->exempt)
128 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Positive hit for " + New->GetIPString());
129 if (!ServerInstance->Config->XLineMessage.empty())
130 New->WriteNumeric(ERR_YOUREBANNEDCREEP, ServerInstance->Config->XLineMessage);
131 this->QuitUser(New, b->Reason);
136 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Negative hit for " + New->GetIPString());
143 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
153 if (!SocketEngine::AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
155 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "Internal error on new connection");
156 this->QuitUser(New, "Internal error handling connection");
159 if (ServerInstance->Config->RawLog)
160 New->WriteNotice("*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
162 FOREACH_MOD(OnSetUserIP, (New));
166 FOREACH_MOD(OnUserInit, (New));
169 void UserManager::QuitUser(User* user, const std::string& quitreason, const std::string* operreason)
173 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
179 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
183 user->quitting = true;
185 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
186 user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), operreason ? operreason->c_str() : quitreason.c_str());
189 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
191 operreason = &reason;
193 ServerInstance->GlobalCulls.AddItem(user);
195 if (user->registered == REG_ALL)
197 FOREACH_MOD(OnUserQuit, (user, reason, *operreason));
198 WriteCommonQuit(user, reason, *operreason);
201 unregistered_count--;
205 LocalUser* lu = IS_LOCAL(user);
206 FOREACH_MOD(OnUserDisconnect, (lu));
209 if (lu->registered == REG_ALL)
210 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]", user->GetFullRealHost().c_str(), user->GetIPString().c_str(), operreason->c_str());
211 local_users.erase(lu);
214 if (!clientlist.erase(user->nick))
215 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
217 uuidlist.erase(user->uuid);
218 user->PurgeEmptyChannels();
221 void UserManager::AddClone(User* user)
223 CloneCounts& counts = clonemap[user->GetCIDRMask()];
229 void UserManager::RemoveCloneCounts(User *user)
231 CloneMap::iterator it = clonemap.find(user->GetCIDRMask());
232 if (it != clonemap.end())
234 CloneCounts& counts = it->second;
236 if (counts.global == 0)
238 // No more users from this IP, remove entry from the map
248 void UserManager::RehashCloneCounts()
252 const user_hash& hash = ServerInstance->Users.GetUsers();
253 for (user_hash::const_iterator i = hash.begin(); i != hash.end(); ++i)
260 const UserManager::CloneCounts& UserManager::GetCloneCounts(User* user) const
262 CloneMap::const_iterator it = clonemap.find(user->GetCIDRMask());
263 if (it != clonemap.end())
266 return zeroclonecounts;
269 void UserManager::ServerNoticeAll(const char* text, ...)
272 VAFORMAT(message, text, text);
273 message = "NOTICE $" + ServerInstance->Config->ServerName + " :" + message;
275 for (LocalList::const_iterator i = local_users.begin(); i != local_users.end(); ++i)
278 t->WriteServ(message);
282 /* this returns true when all modules are satisfied that the user should be allowed onto the irc server
283 * (until this returns true, a user will block in the waiting state, waiting to connect up to the
284 * registration timeout maximum seconds)
286 bool UserManager::AllModulesReportReady(LocalUser* user)
289 FIRST_MOD_RESULT(OnCheckReady, res, (user));
290 return (res == MOD_RES_PASSTHRU);
294 * This function is called once a second from the mainloop.
295 * It is intended to do background checking on all the user structs, e.g.
296 * stuff like ping checks, registration timeouts, etc.
298 void UserManager::DoBackgroundUserStuff()
301 * loop over all local users..
303 for (LocalList::iterator i = local_users.begin(); i != local_users.end(); )
305 // It's possible that we quit the user below due to ping timeout etc. and QuitUser() removes it from the list
306 LocalUser* curr = *i;
309 if (curr->CommandFloodPenalty || curr->eh.getSendQSize())
311 unsigned int rate = curr->MyClass->GetCommandRate();
312 if (curr->CommandFloodPenalty > rate)
313 curr->CommandFloodPenalty -= rate;
315 curr->CommandFloodPenalty = 0;
316 curr->eh.OnDataReady();
319 switch (curr->registered)
322 if (ServerInstance->Time() >= curr->nping)
324 // This user didn't answer the last ping, remove them
327 time_t time = ServerInstance->Time() - (curr->nping - curr->MyClass->GetPingTime());
328 const std::string message = "Ping timeout: " + ConvToStr(time) + (time != 1 ? " seconds" : " second");
329 this->QuitUser(curr, message);
333 curr->Write("PING :" + ServerInstance->Config->ServerName);
335 curr->nping = ServerInstance->Time() + curr->MyClass->GetPingTime();
339 if (AllModulesReportReady(curr))
341 /* User has sent NICK/USER, modules are okay, DNS finished. */
346 // If the user has been quit in OnCheckReady then we shouldn't
347 // quit them again for having a registration timeout.
353 if (curr->registered != REG_ALL && curr->MyClass && (ServerInstance->Time() > (curr->signon + curr->MyClass->GetRegTimeout())))
356 * registration timeout -- didnt send USER/NICK/HOST
357 * in the time specified in their connection class.
359 this->QuitUser(curr, "Registration timeout");
365 already_sent_t UserManager::NextAlreadySentId()
367 if (++already_sent_id == 0)
369 // Wrapped around, reset the already_sent ids of all users
371 for (LocalList::iterator i = local_users.begin(); i != local_users.end(); ++i)
373 LocalUser* user = *i;
374 user->already_sent = 0;
377 return already_sent_id;