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 * ---------------------------------------------------
14 /* $Core: libIRCDusermanager */
20 /* add a client connection to the sockets list */
21 void UserManager::AddUser(InspIRCd* Instance, int socket, int port, bool iscached, int socketfamily, 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 (socketfamily == 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(socketfamily, 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->io->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, NICKMAX - 1);
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->host.assign(New->GetIPString(), 64));
83 Instance->Users->AddLocalClone(New);
84 Instance->Users->AddGlobalClone(New);
87 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
88 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
90 ConnectClass* i = New->SetClass();
94 this->QuitUser(New, "Access denied by configuration");
99 * Check connect class settings and initialise settings into User.
100 * This will be done again after DNS resolution. -- w00t
104 this->local_users.push_back(New);
106 if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)Instance->SE->GetMaxFds()))
108 Instance->SNO->WriteToSnoMask('A', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
109 this->QuitUser(New,"No more connections allowed");
115 * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
116 * its a pretty big but for the moment valid assumption:
117 * file descriptors are handed out starting at 0, and are recycled as theyre freed.
118 * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
119 * irc server at once (or the irc server otherwise initiating this many connections, files etc)
120 * which for the time being is a physical impossibility (even the largest networks dont have more
121 * than about 10,000 users on ONE server!)
123 if (socket >= Instance->SE->GetMaxFds())
125 this->QuitUser(New, "Server is full");
130 * even with bancache, we still have to keep User::exempt current.
131 * besides that, if we get a positive bancache hit, we still won't fuck
132 * them over if they are exempt. -- w00t
134 New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
136 if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
138 if (!b->Type.empty() && !New->exempt)
141 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
142 if (*Instance->Config->MoronBanner)
143 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), Instance->Config->MoronBanner);
144 this->QuitUser(New, b->Reason);
149 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
156 XLine* r = Instance->XLines->MatchesLine("Z",New);
166 if (!Instance->SE->AddFd(New))
168 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
169 this->QuitUser(New, "Internal error handling connection");
172 /* NOTE: even if dns lookups are *off*, we still need to display this.
173 * BOPM and other stuff requires it.
175 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
177 if (Instance->Config->NoUserDns)
179 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
180 New->dns_done = true;
184 New->StartDNSLookup();
188 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
190 ServerInstance->Logs->Log("USERS", DEBUG,"QuitUser: %s '%s'", user->nick.c_str(), quitreason.c_str());
191 user->Write("ERROR :Closing link (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
192 user->quietquit = false;
193 user->quitmsg = quitreason;
196 user->operquitmsg = quitreason;
198 user->operquitmsg = operreason;
200 ServerInstance->GlobalCulls.AddItem(user);
204 void UserManager::AddLocalClone(User *user)
206 clonemap::iterator x = local_clones.find(user->GetIPString());
207 if (x != local_clones.end())
210 local_clones[user->GetIPString()] = 1;
213 void UserManager::AddGlobalClone(User *user)
215 clonemap::iterator y = global_clones.find(user->GetIPString());
216 if (y != global_clones.end())
219 global_clones[user->GetIPString()] = 1;
222 void UserManager::RemoveCloneCounts(User *user)
224 clonemap::iterator x = local_clones.find(user->GetIPString());
225 if (x != local_clones.end())
230 local_clones.erase(x);
234 clonemap::iterator y = global_clones.find(user->GetIPString());
235 if (y != global_clones.end())
240 global_clones.erase(y);
245 unsigned long UserManager::GlobalCloneCount(User *user)
247 clonemap::iterator x = global_clones.find(user->GetIPString());
248 if (x != global_clones.end())
254 unsigned long UserManager::LocalCloneCount(User *user)
256 clonemap::iterator x = local_clones.find(user->GetIPString());
257 if (x != local_clones.end())
263 /* this function counts all users connected, wether they are registered or NOT. */
264 unsigned int UserManager::UserCount()
268 * As part of this restructuring, move clientlist/etc fields into usermanager.
271 return this->clientlist->size();
274 /* this counts only registered users, so that the percentages in /MAP don't mess up */
275 unsigned int UserManager::RegisteredUserCount()
277 return this->clientlist->size() - this->UnregisteredUserCount();
280 /* return how many users are opered */
281 unsigned int UserManager::OperCount()
283 return this->all_opers.size();
286 /* return how many users are unregistered */
287 unsigned int UserManager::UnregisteredUserCount()
289 return this->unregistered_count;
292 /* return how many local registered users there are */
293 unsigned int UserManager::LocalUserCount()
295 /* Doesnt count unregistered clients */
296 return (this->local_users.size() - this->UnregisteredUserCount());
299 void UserManager::ServerNoticeAll(const char* text, ...)
304 char textbuffer[MAXBUF];
305 char formatbuffer[MAXBUF];
307 va_start (argsPtr, text);
308 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
311 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
313 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
316 t->WriteServ(std::string(formatbuffer));
320 void UserManager::ServerPrivmsgAll(const char* text, ...)
325 char textbuffer[MAXBUF];
326 char formatbuffer[MAXBUF];
328 va_start (argsPtr, text);
329 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
332 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
334 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
337 t->WriteServ(std::string(formatbuffer));
341 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
343 char textbuffer[MAXBUF];
347 if (!text || !modes || !flags)
349 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
353 va_start(argsPtr, text);
354 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
356 modelen = strlen(modes);
360 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
363 bool send_to_user = true;
365 for (int n = 0; n < modelen; n++)
367 if (!t->IsModeSet(modes[n]))
369 send_to_user = false;
375 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
379 else if (flags == WM_OR)
381 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
384 bool send_to_user = false;
386 for (int n = 0; n < modelen; n++)
388 if (t->IsModeSet(modes[n]))
397 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
403 /* return how many users have a given mode e.g. 'a' */
404 int UserManager::ModeCount(const char mode)
406 ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
409 return mh->GetCount();