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::AddClient(InspIRCd* Instance, int socket, int port, bool iscached, int socketfamily, sockaddr* ip)
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 /* Give each of the modules an attempt to hook the user for I/O */
48 FOREACH_MOD_I(Instance, I_OnHookUserIO, OnHookUserIO(New));
54 New->io->OnRawSocketAccept(socket, ipaddr, 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);
66 this->unregistered_count++;
68 (*(this->clientlist))[New->uuid] = New;
70 /* The users default nick is their UUID */
71 strlcpy(New->nick, New->uuid, NICKMAX - 1);
73 New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
74 /* We don't need range checking here, we KNOW 'unknown\0' will fit into the ident field. */
75 strcpy(New->ident, "unknown");
77 New->registered = REG_NONE;
78 New->signon = Instance->Time() + Instance->Config->dns_timeout;
81 New->SetSockAddr(socketfamily, ipaddr, port);
85 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
86 for (const char* temp = New->GetIPString(); *temp && j < 64; temp++, j++)
87 New->dhost[j] = New->host[j] = *temp;
88 New->dhost[j] = New->host[j] = 0;
90 Instance->Users->AddLocalClone(New);
91 Instance->Users->AddGlobalClone(New);
94 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
95 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
97 ConnectClass* i = New->SetClass();
101 User::QuitUser(Instance, New, "Access denied by configuration");
106 * Check connect class settings and initialise settings into User.
107 * This will be done again after DNS resolution. -- w00t
111 this->local_users.push_back(New);
113 if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= MAXCLIENTS))
115 Instance->SNO->WriteToSnoMask('A', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
116 User::QuitUser(Instance, New,"No more connections allowed");
122 * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
123 * its a pretty big but for the moment valid assumption:
124 * file descriptors are handed out starting at 0, and are recycled as theyre freed.
125 * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
126 * irc server at once (or the irc server otherwise initiating this many connections, files etc)
127 * which for the time being is a physical impossibility (even the largest networks dont have more
128 * than about 10,000 users on ONE server!)
131 if ((unsigned int)socket >= MAX_DESCRIPTORS)
133 User::QuitUser(Instance, New, "Server is full");
138 * even with bancache, we still have to keep User::exempt current.
139 * besides that, if we get a positive bancache hit, we still won't fuck
140 * them over if they are exempt. -- w00t
142 New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
144 if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
146 if (!b->Type.empty() && !New->exempt)
149 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
150 if (*Instance->Config->MoronBanner)
151 New->WriteServ("NOTICE %s :*** %s", New->nick, Instance->Config->MoronBanner);
152 User::QuitUser(Instance, New, b->Reason);
157 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
164 XLine* r = Instance->XLines->MatchesLine("Z",New);
174 if (!Instance->SE->AddFd(New))
176 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
177 User::QuitUser(Instance, New, "Internal error handling connection");
180 /* NOTE: even if dns lookups are *off*, we still need to display this.
181 * BOPM and other stuff requires it.
183 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
185 if (Instance->Config->NoUserDns)
187 New->dns_done = true;
191 New->StartDNSLookup();
195 void UserManager::AddLocalClone(User *user)
197 clonemap::iterator x = local_clones.find(user->GetIPString());
198 if (x != local_clones.end())
201 local_clones[user->GetIPString()] = 1;
204 void UserManager::AddGlobalClone(User *user)
206 clonemap::iterator y = global_clones.find(user->GetIPString());
207 if (y != global_clones.end())
210 global_clones[user->GetIPString()] = 1;
213 void UserManager::RemoveCloneCounts(User *user)
215 clonemap::iterator x = local_clones.find(user->GetIPString());
216 if (x != local_clones.end())
221 local_clones.erase(x);
225 clonemap::iterator y = global_clones.find(user->GetIPString());
226 if (y != global_clones.end())
231 global_clones.erase(y);
236 unsigned long UserManager::GlobalCloneCount(User *user)
238 clonemap::iterator x = global_clones.find(user->GetIPString());
239 if (x != global_clones.end())
245 unsigned long UserManager::LocalCloneCount(User *user)
247 clonemap::iterator x = local_clones.find(user->GetIPString());
248 if (x != local_clones.end())
254 /* this function counts all users connected, wether they are registered or NOT. */
255 unsigned int UserManager::UserCount()
259 * As part of this restructuring, move clientlist/etc fields into usermanager.
262 return this->clientlist->size();
265 /* this counts only registered users, so that the percentages in /MAP don't mess up */
266 unsigned int UserManager::RegisteredUserCount()
268 return this->clientlist->size() - this->UnregisteredUserCount();
271 /* return how many users are opered */
272 unsigned int UserManager::OperCount()
274 return this->all_opers.size();
277 /* return how many users are unregistered */
278 unsigned int UserManager::UnregisteredUserCount()
280 return this->unregistered_count;
283 /* return how many local registered users there are */
284 unsigned int UserManager::LocalUserCount()
286 /* Doesnt count unregistered clients */
287 return (this->local_users.size() - this->UnregisteredUserCount());
290 void UserManager::ServerNoticeAll(const char* text, ...)
295 char textbuffer[MAXBUF];
296 char formatbuffer[MAXBUF];
298 va_start (argsPtr, text);
299 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
302 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
304 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
307 t->WriteServ(std::string(formatbuffer));
311 void UserManager::ServerPrivmsgAll(const char* text, ...)
316 char textbuffer[MAXBUF];
317 char formatbuffer[MAXBUF];
319 va_start (argsPtr, text);
320 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
323 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
325 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
328 t->WriteServ(std::string(formatbuffer));
332 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
334 char textbuffer[MAXBUF];
338 if (!text || !modes || !flags)
340 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
344 va_start(argsPtr, text);
345 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
347 modelen = strlen(modes);
351 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
354 bool send_to_user = true;
356 for (int n = 0; n < modelen; n++)
358 if (!t->IsModeSet(modes[n]))
360 send_to_user = false;
366 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
370 else if (flags == WM_OR)
372 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
375 bool send_to_user = false;
377 for (int n = 0; n < modelen; n++)
379 if (t->IsModeSet(modes[n]))
388 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
394 /* return how many users have a given mode e.g. 'a' */
395 int UserManager::ModeCount(const char mode)
397 ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
400 return mh->GetCount();