1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 /* add a client connection to the sockets list */
19 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
21 /* NOTE: Calling this one parameter constructor for User automatically
22 * allocates a new UUID and places it in the hash_map.
24 LocalUser* New = NULL;
27 New = new LocalUser(socket, client, server);
31 ServerInstance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
32 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
36 /* Give each of the modules an attempt to hook the user for I/O */
37 FOREACH_MOD(I_OnHookIO, OnHookIO(New, via));
43 New->GetIOHook()->OnStreamSocketAccept(New, client, server);
45 catch (CoreException& modexcept)
47 ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
51 ServerInstance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
53 this->unregistered_count++;
55 /* The users default nick is their UUID */
56 New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
57 (*(this->clientlist))[New->nick] = New;
59 New->ident.assign("unknown");
61 New->registered = REG_NONE;
62 New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
65 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
66 New->dhost.assign(New->GetIPString(), 0, 64);
67 New->host.assign(New->GetIPString(), 0, 64);
69 ServerInstance->Users->AddLocalClone(New);
70 ServerInstance->Users->AddGlobalClone(New);
73 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
74 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
79 * Check connect class settings and initialise settings into User.
80 * This will be done again after DNS resolution. -- w00t
84 this->local_users.push_back(New);
86 if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->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 * even with bancache, we still have to keep User::exempt current.
95 * besides that, if we get a positive bancache hit, we still won't fuck
96 * them over if they are exempt. -- w00t
98 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
100 if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
102 if (!b->Type.empty() && !New->exempt)
105 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
106 if (!ServerInstance->Config->MoronBanner.empty())
107 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
108 this->QuitUser(New, b->Reason);
113 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
120 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
130 if (!ServerInstance->SE->AddFd(New, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
132 ServerInstance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
133 this->QuitUser(New, "Internal error handling connection");
136 /* NOTE: even if dns lookups are *off*, we still need to display this.
137 * BOPM and other stuff requires it.
139 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
141 if (ServerInstance->Config->NoUserDns)
143 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
144 New->dns_done = true;
148 New->StartDNSLookup();
152 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
156 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
162 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a fake user (%s)", user->nick.c_str());
166 user->quitting = true;
168 ServerInstance->Logs->Log("USERS", 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));
200 lu->GetIOHook()->OnStreamSocketClose(lu);
202 catch (CoreException& modexcept)
204 ServerInstance->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
208 ServerInstance->SE->DelFd(lu);
213 * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
214 * if they were an oper with +sn +qQ.
216 if (user->registered == REG_ALL)
220 if (!user->quietquit)
222 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",
223 user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
228 if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
230 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",
231 user->server.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
237 user_hash::iterator iter = this->clientlist->find(user->nick);
239 if (iter != this->clientlist->end())
240 this->clientlist->erase(iter);
242 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
246 void UserManager::AddLocalClone(User *user)
248 clonemap::iterator x;
249 x = local_clones.find(user->GetCIDRMask());
250 if (x != local_clones.end())
253 local_clones[user->GetCIDRMask()] = 1;
256 void UserManager::AddGlobalClone(User *user)
258 clonemap::iterator x;
260 x = global_clones.find(user->GetCIDRMask());
261 if (x != global_clones.end())
264 global_clones[user->GetCIDRMask()] = 1;
267 void UserManager::RemoveCloneCounts(User *user)
271 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
272 if (x != local_clones.end())
277 local_clones.erase(x);
282 clonemap::iterator y = global_clones.find(user->GetCIDRMask());
283 if (y != global_clones.end())
288 global_clones.erase(y);
293 unsigned long UserManager::GlobalCloneCount(User *user)
295 clonemap::iterator x = global_clones.find(user->GetCIDRMask());
296 if (x != global_clones.end())
302 unsigned long UserManager::LocalCloneCount(User *user)
304 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
305 if (x != local_clones.end())
311 /* this function counts all users connected, wether they are registered or NOT. */
312 unsigned int UserManager::UserCount()
316 * As part of this restructuring, move clientlist/etc fields into usermanager.
319 return this->clientlist->size();
322 /* this counts only registered users, so that the percentages in /MAP don't mess up */
323 unsigned int UserManager::RegisteredUserCount()
325 return this->clientlist->size() - this->UnregisteredUserCount();
328 /* return how many users are opered */
329 unsigned int UserManager::OperCount()
331 return this->all_opers.size();
334 /* return how many users are unregistered */
335 unsigned int UserManager::UnregisteredUserCount()
337 return this->unregistered_count;
340 /* return how many local registered users there are */
341 unsigned int UserManager::LocalUserCount()
343 /* Doesnt count unregistered clients */
344 return (this->local_users.size() - this->UnregisteredUserCount());
347 void UserManager::ServerNoticeAll(const char* text, ...)
352 char textbuffer[MAXBUF];
353 char formatbuffer[MAXBUF];
355 va_start (argsPtr, text);
356 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
359 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
361 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
364 t->WriteServ(std::string(formatbuffer));
368 void UserManager::ServerPrivmsgAll(const char* text, ...)
373 char textbuffer[MAXBUF];
374 char formatbuffer[MAXBUF];
376 va_start (argsPtr, text);
377 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
380 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
382 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
385 t->WriteServ(std::string(formatbuffer));
390 /* return how many users have a given mode e.g. 'a' */
391 int UserManager::ModeCount(const char mode)
393 ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
396 return mh->GetCount();