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, int port, bool iscached, irc::sockets::sockaddrs* ip, const std::string &targetip)
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->ip, ip, sizeof(*ip));
40 // change the port number of their stored sockaddr to be the server port rather than the client port
41 if (New->ip.sa.sa_family == AF_INET6)
42 New->ip.in6.sin6_port = port;
44 New->ip.in4.sin_port = port;
47 /* Give each of the modules an attempt to hook the user for I/O */
48 FOREACH_MOD_I(Instance, I_OnHookUserIO, OnHookUserIO(New, targetip));
54 New->GetIOHook()->OnRawSocketAccept(socket, New->GetIPString(), port);
56 catch (CoreException& modexcept)
58 ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
62 Instance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
64 this->unregistered_count++;
66 (*(this->clientlist))[New->uuid] = New;
68 /* The users default nick is their UUID */
69 New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
71 New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
72 New->ident.assign("unknown");
74 New->registered = REG_NONE;
75 New->signon = Instance->Time() + Instance->Config->dns_timeout;
78 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
79 New->dhost.assign(New->GetIPString(), 0, 64);
80 New->host.assign(New->GetIPString(), 0, 64);
82 Instance->Users->AddLocalClone(New);
83 Instance->Users->AddGlobalClone(New);
86 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
87 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
89 ConnectClass* i = New->SetClass();
93 this->QuitUser(New, "Access denied by configuration");
98 * Check connect class settings and initialise settings into User.
99 * This will be done again after DNS resolution. -- w00t
103 this->local_users.push_back(New);
105 if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)Instance->SE->GetMaxFds()))
107 Instance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
108 this->QuitUser(New,"No more connections allowed");
113 * even with bancache, we still have to keep User::exempt current.
114 * besides that, if we get a positive bancache hit, we still won't fuck
115 * them over if they are exempt. -- w00t
117 New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
119 if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
121 if (!b->Type.empty() && !New->exempt)
124 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
125 if (*Instance->Config->MoronBanner)
126 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), Instance->Config->MoronBanner);
127 this->QuitUser(New, b->Reason);
132 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
139 XLine* r = Instance->XLines->MatchesLine("Z",New);
149 if (!Instance->SE->AddFd(New))
151 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
152 this->QuitUser(New, "Internal error handling connection");
155 /* NOTE: even if dns lookups are *off*, we still need to display this.
156 * BOPM and other stuff requires it.
158 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
160 if (Instance->Config->NoUserDns)
162 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
163 New->dns_done = true;
167 New->StartDNSLookup();
171 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
175 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
179 user->quitting = true;
181 ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s '%s'", user->nick.c_str(), quitreason.c_str());
182 user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
184 user->quietquit = false;
185 user->quitmsg = quitreason;
188 std::string oper_reason;
189 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
192 user->operquitmsg = quitreason;
193 oper_reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
197 user->operquitmsg = operreason;
198 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
201 ServerInstance->GlobalCulls.AddItem(user);
203 if (user->registered == REG_ALL)
205 FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
206 user->PurgeEmptyChannels();
207 user->WriteCommonQuit(reason, oper_reason);
210 FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(user));
212 // Move the user onto their UID, to allow nick to be reused immediately
213 user->UpdateNickHash(user->uuid.c_str());
217 void UserManager::AddLocalClone(User *user)
220 clonemap::iterator x;
221 switch (user->ip.sa.sa_family)
224 range = ServerInstance->Config->c_ipv6_range;
227 range = ServerInstance->Config->c_ipv4_range;
231 x = local_clones.find(user->GetCIDRMask(range));
232 if (x != local_clones.end())
235 local_clones[user->GetCIDRMask(range)] = 1;
238 void UserManager::AddGlobalClone(User *user)
241 clonemap::iterator x;
242 switch (user->ip.sa.sa_family)
245 range = ServerInstance->Config->c_ipv6_range;
248 range = ServerInstance->Config->c_ipv4_range;
252 x = global_clones.find(user->GetCIDRMask(range));
253 if (x != global_clones.end())
256 global_clones[user->GetCIDRMask(range)] = 1;
259 void UserManager::RemoveCloneCounts(User *user)
262 switch (user->ip.sa.sa_family)
265 range = ServerInstance->Config->c_ipv6_range;
268 range = ServerInstance->Config->c_ipv4_range;
272 clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
273 if (x != local_clones.end())
278 local_clones.erase(x);
282 clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
283 if (y != global_clones.end())
288 global_clones.erase(y);
293 unsigned long UserManager::GlobalCloneCount(User *user)
296 switch (user->ip.sa.sa_family)
299 range = ServerInstance->Config->c_ipv6_range;
302 range = ServerInstance->Config->c_ipv4_range;
305 clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
306 if (x != global_clones.end())
312 unsigned long UserManager::LocalCloneCount(User *user)
315 switch (user->ip.sa.sa_family)
318 range = ServerInstance->Config->c_ipv6_range;
321 range = ServerInstance->Config->c_ipv4_range;
324 clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
325 if (x != local_clones.end())
331 /* this function counts all users connected, wether they are registered or NOT. */
332 unsigned int UserManager::UserCount()
336 * As part of this restructuring, move clientlist/etc fields into usermanager.
339 return this->clientlist->size();
342 /* this counts only registered users, so that the percentages in /MAP don't mess up */
343 unsigned int UserManager::RegisteredUserCount()
345 return this->clientlist->size() - this->UnregisteredUserCount();
348 /* return how many users are opered */
349 unsigned int UserManager::OperCount()
351 return this->all_opers.size();
354 /* return how many users are unregistered */
355 unsigned int UserManager::UnregisteredUserCount()
357 return this->unregistered_count;
360 /* return how many local registered users there are */
361 unsigned int UserManager::LocalUserCount()
363 /* Doesnt count unregistered clients */
364 return (this->local_users.size() - this->UnregisteredUserCount());
367 void UserManager::ServerNoticeAll(const char* text, ...)
372 char textbuffer[MAXBUF];
373 char formatbuffer[MAXBUF];
375 va_start (argsPtr, text);
376 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
379 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
381 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
384 t->WriteServ(std::string(formatbuffer));
388 void UserManager::ServerPrivmsgAll(const char* text, ...)
393 char textbuffer[MAXBUF];
394 char formatbuffer[MAXBUF];
396 va_start (argsPtr, text);
397 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
400 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
402 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
405 t->WriteServ(std::string(formatbuffer));
409 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
411 char textbuffer[MAXBUF];
415 if (!text || !modes || !flags)
417 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
421 va_start(argsPtr, text);
422 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
424 modelen = strlen(modes);
428 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
431 bool send_to_user = true;
433 for (int n = 0; n < modelen; n++)
435 if (!t->IsModeSet(modes[n]))
437 send_to_user = false;
443 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
447 else if (flags == WM_OR)
449 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
452 bool send_to_user = false;
454 for (int n = 0; n < modelen; n++)
456 if (t->IsModeSet(modes[n]))
465 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
471 /* return how many users have a given mode e.g. 'a' */
472 int UserManager::ModeCount(const char mode)
474 ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
477 return mh->GetCount();