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);
68 this->unregistered_count++;
70 (*(this->clientlist))[New->uuid] = New;
72 /* The users default nick is their UUID */
73 strlcpy(New->nick, New->uuid, NICKMAX - 1);
75 New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
76 /* We don't need range checking here, we KNOW 'unknown\0' will fit into the ident field. */
77 strcpy(New->ident, "unknown");
79 New->registered = REG_NONE;
80 New->signon = Instance->Time() + Instance->Config->dns_timeout;
83 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
84 for (const char* temp = New->GetIPString(); *temp && j < 64; temp++, j++)
85 New->dhost[j] = New->host[j] = *temp;
86 New->dhost[j] = New->host[j] = 0;
88 Instance->Users->AddLocalClone(New);
89 Instance->Users->AddGlobalClone(New);
92 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
93 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
95 ConnectClass* i = New->SetClass();
99 this->QuitUser(New, "Access denied by configuration");
104 * Check connect class settings and initialise settings into User.
105 * This will be done again after DNS resolution. -- w00t
109 this->local_users.push_back(New);
111 if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)Instance->SE->GetMaxFds()))
113 Instance->SNO->WriteToSnoMask('A', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
114 this->QuitUser(New,"No more connections allowed");
120 * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
121 * its a pretty big but for the moment valid assumption:
122 * file descriptors are handed out starting at 0, and are recycled as theyre freed.
123 * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
124 * irc server at once (or the irc server otherwise initiating this many connections, files etc)
125 * which for the time being is a physical impossibility (even the largest networks dont have more
126 * than about 10,000 users on ONE server!)
128 if (socket >= Instance->SE->GetMaxFds())
130 this->QuitUser(New, "Server is full");
135 * even with bancache, we still have to keep User::exempt current.
136 * besides that, if we get a positive bancache hit, we still won't fuck
137 * them over if they are exempt. -- w00t
139 New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
141 if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
143 if (!b->Type.empty() && !New->exempt)
146 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
147 if (*Instance->Config->MoronBanner)
148 New->WriteServ("NOTICE %s :*** %s", New->nick, Instance->Config->MoronBanner);
149 this->QuitUser(New, b->Reason);
154 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
161 XLine* r = Instance->XLines->MatchesLine("Z",New);
171 if (!Instance->SE->AddFd(New))
173 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
174 this->QuitUser(New, "Internal error handling connection");
177 /* NOTE: even if dns lookups are *off*, we still need to display this.
178 * BOPM and other stuff requires it.
180 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
182 if (Instance->Config->NoUserDns)
184 New->dns_done = true;
188 New->StartDNSLookup();
192 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
194 ServerInstance->Logs->Log("USERS", DEBUG,"QuitUser: %s '%s'", user->nick, quitreason.c_str());
195 user->Write("ERROR :Closing link (%s@%s) [%s]", user->ident, user->host, *operreason ? operreason : quitreason.c_str());
196 user->quietquit = false;
197 user->quitmsg = quitreason;
200 user->operquitmsg = quitreason;
202 user->operquitmsg = operreason;
204 ServerInstance->GlobalCulls.AddItem(user);
208 void UserManager::AddLocalClone(User *user)
210 clonemap::iterator x = local_clones.find(user->GetIPString());
211 if (x != local_clones.end())
214 local_clones[user->GetIPString()] = 1;
217 void UserManager::AddGlobalClone(User *user)
219 clonemap::iterator y = global_clones.find(user->GetIPString());
220 if (y != global_clones.end())
223 global_clones[user->GetIPString()] = 1;
226 void UserManager::RemoveCloneCounts(User *user)
228 clonemap::iterator x = local_clones.find(user->GetIPString());
229 if (x != local_clones.end())
234 local_clones.erase(x);
238 clonemap::iterator y = global_clones.find(user->GetIPString());
239 if (y != global_clones.end())
244 global_clones.erase(y);
249 unsigned long UserManager::GlobalCloneCount(User *user)
251 clonemap::iterator x = global_clones.find(user->GetIPString());
252 if (x != global_clones.end())
258 unsigned long UserManager::LocalCloneCount(User *user)
260 clonemap::iterator x = local_clones.find(user->GetIPString());
261 if (x != local_clones.end())
267 /* this function counts all users connected, wether they are registered or NOT. */
268 unsigned int UserManager::UserCount()
272 * As part of this restructuring, move clientlist/etc fields into usermanager.
275 return this->clientlist->size();
278 /* this counts only registered users, so that the percentages in /MAP don't mess up */
279 unsigned int UserManager::RegisteredUserCount()
281 return this->clientlist->size() - this->UnregisteredUserCount();
284 /* return how many users are opered */
285 unsigned int UserManager::OperCount()
287 return this->all_opers.size();
290 /* return how many users are unregistered */
291 unsigned int UserManager::UnregisteredUserCount()
293 return this->unregistered_count;
296 /* return how many local registered users there are */
297 unsigned int UserManager::LocalUserCount()
299 /* Doesnt count unregistered clients */
300 return (this->local_users.size() - this->UnregisteredUserCount());
303 void UserManager::ServerNoticeAll(const char* text, ...)
308 char textbuffer[MAXBUF];
309 char formatbuffer[MAXBUF];
311 va_start (argsPtr, text);
312 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
315 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
317 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
320 t->WriteServ(std::string(formatbuffer));
324 void UserManager::ServerPrivmsgAll(const char* text, ...)
329 char textbuffer[MAXBUF];
330 char formatbuffer[MAXBUF];
332 va_start (argsPtr, text);
333 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
336 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
338 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
341 t->WriteServ(std::string(formatbuffer));
345 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
347 char textbuffer[MAXBUF];
351 if (!text || !modes || !flags)
353 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
357 va_start(argsPtr, text);
358 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
360 modelen = strlen(modes);
364 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
367 bool send_to_user = true;
369 for (int n = 0; n < modelen; n++)
371 if (!t->IsModeSet(modes[n]))
373 send_to_user = false;
379 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
383 else if (flags == WM_OR)
385 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
388 bool send_to_user = false;
390 for (int n = 0; n < modelen; n++)
392 if (t->IsModeSet(modes[n]))
401 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
407 /* return how many users have a given mode e.g. 'a' */
408 int UserManager::ModeCount(const char mode)
410 ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
413 return mh->GetCount();