1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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, sockaddr* 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!");
40 if (ip->sa_family == AF_INET6)
41 inet_ntop(AF_INET6, &((const sockaddr_in6*)ip)->sin6_addr, ipaddr, sizeof(ipaddr));
44 inet_ntop(AF_INET, &((const sockaddr_in*)ip)->sin_addr, ipaddr, sizeof(ipaddr));
47 New->SetSockAddr(ip->sa_family, ipaddr, port);
49 /* Give each of the modules an attempt to hook the user for I/O */
50 FOREACH_MOD_I(Instance, I_OnHookUserIO, OnHookUserIO(New, targetip));
56 New->GetIOHook()->OnRawSocketAccept(socket, ipaddr, port);
58 catch (CoreException& modexcept)
60 ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
64 Instance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
66 this->unregistered_count++;
68 (*(this->clientlist))[New->uuid] = New;
70 /* The users default nick is their UUID */
71 New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
73 New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
74 New->ident.assign("unknown");
76 New->registered = REG_NONE;
77 New->signon = Instance->Time() + Instance->Config->dns_timeout;
80 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
81 New->dhost.assign(New->GetIPString(), 0, 64);
82 New->host.assign(New->GetIPString(), 0, 64);
84 Instance->Users->AddLocalClone(New);
85 Instance->Users->AddGlobalClone(New);
88 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
89 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
91 ConnectClass* i = New->SetClass();
95 this->QuitUser(New, "Access denied by configuration");
100 * Check connect class settings and initialise settings into User.
101 * This will be done again after DNS resolution. -- w00t
105 this->local_users.push_back(New);
107 if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)Instance->SE->GetMaxFds()))
109 Instance->SNO->WriteToSnoMask('A', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
110 this->QuitUser(New,"No more connections allowed");
115 * even with bancache, we still have to keep User::exempt current.
116 * besides that, if we get a positive bancache hit, we still won't fuck
117 * them over if they are exempt. -- w00t
119 New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
121 if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
123 if (!b->Type.empty() && !New->exempt)
126 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
127 if (*Instance->Config->MoronBanner)
128 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), Instance->Config->MoronBanner);
129 this->QuitUser(New, b->Reason);
134 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
141 XLine* r = Instance->XLines->MatchesLine("Z",New);
151 if (!Instance->SE->AddFd(New))
153 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
154 this->QuitUser(New, "Internal error handling connection");
157 /* NOTE: even if dns lookups are *off*, we still need to display this.
158 * BOPM and other stuff requires it.
160 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
162 if (Instance->Config->NoUserDns)
164 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
165 New->dns_done = true;
169 New->StartDNSLookup();
173 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
177 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
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());
183 user->quitting = true;
184 user->quietquit = false;
185 user->quitmsg = quitreason;
188 user->operquitmsg = quitreason;
190 user->operquitmsg = operreason;
192 ServerInstance->GlobalCulls.AddItem(user);
196 void UserManager::AddLocalClone(User *user)
199 clonemap::iterator x;
200 switch (user->GetProtocolFamily())
202 #ifdef SUPPORT_IP6LINKS
205 range = ServerInstance->Config->c_ipv6_range;
211 range = ServerInstance->Config->c_ipv4_range;
216 x = local_clones.find(user->GetCIDRMask(range));
217 if (x != local_clones.end())
220 local_clones[user->GetCIDRMask(range)] = 1;
223 void UserManager::AddGlobalClone(User *user)
226 clonemap::iterator x;
227 switch (user->GetProtocolFamily())
229 #ifdef SUPPORT_IP6LINKS
232 range = ServerInstance->Config->c_ipv6_range;
238 range = ServerInstance->Config->c_ipv4_range;
243 x = global_clones.find(user->GetCIDRMask(range));
244 if (x != global_clones.end())
247 global_clones[user->GetCIDRMask(range)] = 1;
250 void UserManager::RemoveCloneCounts(User *user)
253 switch (user->GetProtocolFamily())
255 #ifdef SUPPORT_IP6LINKS
258 range = ServerInstance->Config->c_ipv6_range;
264 range = ServerInstance->Config->c_ipv4_range;
269 clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
270 if (x != local_clones.end())
275 local_clones.erase(x);
279 clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
280 if (y != global_clones.end())
285 global_clones.erase(y);
290 unsigned long UserManager::GlobalCloneCount(User *user)
293 switch (user->GetProtocolFamily())
295 #ifdef SUPPORT_IP6LINKS
298 range = ServerInstance->Config->c_ipv6_range;
304 range = ServerInstance->Config->c_ipv4_range;
308 clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
309 if (x != global_clones.end())
315 unsigned long UserManager::LocalCloneCount(User *user)
318 switch (user->GetProtocolFamily())
320 #ifdef SUPPORT_IP6LINKS
323 range = ServerInstance->Config->c_ipv6_range;
329 range = ServerInstance->Config->c_ipv4_range;
333 clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
334 if (x != local_clones.end())
340 /* this function counts all users connected, wether they are registered or NOT. */
341 unsigned int UserManager::UserCount()
345 * As part of this restructuring, move clientlist/etc fields into usermanager.
348 return this->clientlist->size();
351 /* this counts only registered users, so that the percentages in /MAP don't mess up */
352 unsigned int UserManager::RegisteredUserCount()
354 return this->clientlist->size() - this->UnregisteredUserCount();
357 /* return how many users are opered */
358 unsigned int UserManager::OperCount()
360 return this->all_opers.size();
363 /* return how many users are unregistered */
364 unsigned int UserManager::UnregisteredUserCount()
366 return this->unregistered_count;
369 /* return how many local registered users there are */
370 unsigned int UserManager::LocalUserCount()
372 /* Doesnt count unregistered clients */
373 return (this->local_users.size() - this->UnregisteredUserCount());
376 void UserManager::ServerNoticeAll(const char* text, ...)
381 char textbuffer[MAXBUF];
382 char formatbuffer[MAXBUF];
384 va_start (argsPtr, text);
385 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
388 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
390 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
393 t->WriteServ(std::string(formatbuffer));
397 void UserManager::ServerPrivmsgAll(const char* text, ...)
402 char textbuffer[MAXBUF];
403 char formatbuffer[MAXBUF];
405 va_start (argsPtr, text);
406 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
409 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
411 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
414 t->WriteServ(std::string(formatbuffer));
418 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
420 char textbuffer[MAXBUF];
424 if (!text || !modes || !flags)
426 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
430 va_start(argsPtr, text);
431 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
433 modelen = strlen(modes);
437 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
440 bool send_to_user = true;
442 for (int n = 0; n < modelen; n++)
444 if (!t->IsModeSet(modes[n]))
446 send_to_user = false;
452 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
456 else if (flags == WM_OR)
458 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
461 bool send_to_user = false;
463 for (int n = 0; n < modelen; n++)
465 if (t->IsModeSet(modes[n]))
474 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
480 /* return how many users have a given mode e.g. 'a' */
481 int UserManager::ModeCount(const char mode)
483 ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
486 return mh->GetCount();