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 * ---------------------------------------------------
20 /* add a client connection to the sockets list */
21 void UserManager::AddUser(InspIRCd* Instance, int socket, ClientListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
23 /* NOTE: Calling this one parameter constructor for User automatically
24 * allocates a new UUID and places it in the hash_map.
29 New = new User(Instance);
33 Instance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
34 Instance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
39 memcpy(&New->client_sa, client, sizeof(irc::sockets::sockaddrs));
40 memcpy(&New->server_sa, server, sizeof(irc::sockets::sockaddrs));
42 /* Give each of the modules an attempt to hook the user for I/O */
43 FOREACH_MOD_I(Instance, I_OnHookIO, OnHookIO(New, via));
49 New->GetIOHook()->OnRawSocketAccept(socket, client, server);
51 catch (CoreException& modexcept)
53 ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
57 Instance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
59 this->unregistered_count++;
61 (*(this->clientlist))[New->uuid] = New;
63 /* The users default nick is their UUID */
64 New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
66 New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
67 New->ident.assign("unknown");
69 New->registered = REG_NONE;
70 New->signon = Instance->Time() + Instance->Config->dns_timeout;
73 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
74 New->dhost.assign(New->GetIPString(), 0, 64);
75 New->host.assign(New->GetIPString(), 0, 64);
77 Instance->Users->AddLocalClone(New);
78 Instance->Users->AddGlobalClone(New);
81 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
82 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
84 ConnectClass* i = New->SetClass();
88 this->QuitUser(New, "Access denied by configuration");
93 * Check connect class settings and initialise settings into User.
94 * This will be done again after DNS resolution. -- w00t
98 this->local_users.push_back(New);
100 if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)Instance->SE->GetMaxFds()))
102 Instance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
103 this->QuitUser(New,"No more connections allowed");
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 = (Instance->XLines->MatchesLine("E",New) != NULL);
114 if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
116 if (!b->Type.empty() && !New->exempt)
119 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
120 if (*Instance->Config->MoronBanner)
121 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), Instance->Config->MoronBanner);
122 this->QuitUser(New, b->Reason);
127 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
134 XLine* r = Instance->XLines->MatchesLine("Z",New);
144 if (!Instance->SE->AddFd(New))
146 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
147 this->QuitUser(New, "Internal error handling connection");
150 /* NOTE: even if dns lookups are *off*, we still need to display this.
151 * BOPM and other stuff requires it.
153 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
155 if (Instance->Config->NoUserDns)
157 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
158 New->dns_done = true;
162 New->StartDNSLookup();
166 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
170 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
176 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a fake user (%s)", user->nick.c_str());
182 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a fake user (%s)", user->nick.c_str());
186 user->quitting = true;
188 ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s '%s'", 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 : quitreason.c_str());
192 std::string oper_reason;
193 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
194 if (operreason && *operreason)
195 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
197 oper_reason = quitreason;
199 ServerInstance->GlobalCulls.AddItem(user);
201 if (user->registered == REG_ALL)
203 FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
204 user->PurgeEmptyChannels();
205 user->WriteCommonQuit(reason, oper_reason);
208 FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(user));
210 user->UpdateNickHash(user->uuid.c_str());
212 user_hash::iterator iter = this->clientlist->find(user->uuid);
214 if (user->registered != REG_ALL)
215 if (ServerInstance->Users->unregistered_count)
216 ServerInstance->Users->unregistered_count--;
220 if (!user->sendq.empty())
221 user->FlushWriteBuf();
223 if (user->GetIOHook())
227 user->GetIOHook()->OnRawSocketClose(user->GetFd());
229 catch (CoreException& modexcept)
231 ServerInstance->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
235 ServerInstance->SE->DelFd(user);
240 * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
241 * if they were an oper with +sn +qQ.
243 if (user->registered == REG_ALL)
247 if (!user->quietquit)
249 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",
250 user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
255 if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
257 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",
258 user->server, user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
264 if (iter != this->clientlist->end())
265 this->clientlist->erase(iter);
267 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
271 std::vector<User*>::iterator x = find(local_users.begin(),local_users.end(),user);
272 if (x != local_users.end())
273 local_users.erase(x);
275 ServerInstance->Logs->Log("USERS", DEBUG, "Failed to remove user from vector");
280 void UserManager::AddLocalClone(User *user)
283 clonemap::iterator x;
284 switch (user->client_sa.sa.sa_family)
287 range = ServerInstance->Config->c_ipv6_range;
290 range = ServerInstance->Config->c_ipv4_range;
294 x = local_clones.find(user->GetCIDRMask(range));
295 if (x != local_clones.end())
298 local_clones[user->GetCIDRMask(range)] = 1;
301 void UserManager::AddGlobalClone(User *user)
304 clonemap::iterator x;
305 switch (user->client_sa.sa.sa_family)
308 range = ServerInstance->Config->c_ipv6_range;
311 range = ServerInstance->Config->c_ipv4_range;
315 x = global_clones.find(user->GetCIDRMask(range));
316 if (x != global_clones.end())
319 global_clones[user->GetCIDRMask(range)] = 1;
322 void UserManager::RemoveCloneCounts(User *user)
325 switch (user->client_sa.sa.sa_family)
328 range = ServerInstance->Config->c_ipv6_range;
331 range = ServerInstance->Config->c_ipv4_range;
335 clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
336 if (x != local_clones.end())
341 local_clones.erase(x);
345 clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
346 if (y != global_clones.end())
351 global_clones.erase(y);
356 unsigned long UserManager::GlobalCloneCount(User *user)
359 switch (user->client_sa.sa.sa_family)
362 range = ServerInstance->Config->c_ipv6_range;
365 range = ServerInstance->Config->c_ipv4_range;
368 clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
369 if (x != global_clones.end())
375 unsigned long UserManager::LocalCloneCount(User *user)
378 switch (user->client_sa.sa.sa_family)
381 range = ServerInstance->Config->c_ipv6_range;
384 range = ServerInstance->Config->c_ipv4_range;
387 clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
388 if (x != local_clones.end())
394 /* this function counts all users connected, wether they are registered or NOT. */
395 unsigned int UserManager::UserCount()
399 * As part of this restructuring, move clientlist/etc fields into usermanager.
402 return this->clientlist->size();
405 /* this counts only registered users, so that the percentages in /MAP don't mess up */
406 unsigned int UserManager::RegisteredUserCount()
408 return this->clientlist->size() - this->UnregisteredUserCount();
411 /* return how many users are opered */
412 unsigned int UserManager::OperCount()
414 return this->all_opers.size();
417 /* return how many users are unregistered */
418 unsigned int UserManager::UnregisteredUserCount()
420 return this->unregistered_count;
423 /* return how many local registered users there are */
424 unsigned int UserManager::LocalUserCount()
426 /* Doesnt count unregistered clients */
427 return (this->local_users.size() - this->UnregisteredUserCount());
430 void UserManager::ServerNoticeAll(const char* text, ...)
435 char textbuffer[MAXBUF];
436 char formatbuffer[MAXBUF];
438 va_start (argsPtr, text);
439 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
442 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
444 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
447 t->WriteServ(std::string(formatbuffer));
451 void UserManager::ServerPrivmsgAll(const char* text, ...)
456 char textbuffer[MAXBUF];
457 char formatbuffer[MAXBUF];
459 va_start (argsPtr, text);
460 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
463 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
465 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
468 t->WriteServ(std::string(formatbuffer));
472 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
474 char textbuffer[MAXBUF];
478 if (!text || !modes || !flags)
480 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
484 va_start(argsPtr, text);
485 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
487 modelen = strlen(modes);
491 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
494 bool send_to_user = true;
496 for (int n = 0; n < modelen; n++)
498 if (!t->IsModeSet(modes[n]))
500 send_to_user = false;
506 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
510 else if (flags == WM_OR)
512 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
515 bool send_to_user = false;
517 for (int n = 0; n < modelen; n++)
519 if (t->IsModeSet(modes[n]))
528 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
534 /* return how many users have a given mode e.g. 'a' */
535 int UserManager::ModeCount(const char mode)
537 ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
540 return mh->GetCount();