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->Log(DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
34 Instance->SNO->WriteToSnoMask('A', "WARNING *** Duplicate UUID allocated!");
38 Instance->Log(DEBUG,"New user fd: %d", socket);
42 this->unregistered_count++;
46 if (socketfamily == AF_INET6)
47 inet_ntop(AF_INET6, &((const sockaddr_in6*)ip)->sin6_addr, ipaddr, sizeof(ipaddr));
50 inet_ntop(AF_INET, &((const sockaddr_in*)ip)->sin_addr, ipaddr, sizeof(ipaddr));
52 (*(this->clientlist))[New->uuid] = New;
54 /* The users default nick is their UUID */
55 strlcpy(New->nick, New->uuid, NICKMAX - 1);
57 New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
58 /* We don't need range checking here, we KNOW 'unknown\0' will fit into the ident field. */
59 strcpy(New->ident, "unknown");
61 New->registered = REG_NONE;
62 New->signon = Instance->Time() + Instance->Config->dns_timeout;
65 New->SetSockAddr(socketfamily, ipaddr, port);
69 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
70 for (const char* temp = New->GetIPString(); *temp && j < 64; temp++, j++)
71 New->dhost[j] = New->host[j] = *temp;
72 New->dhost[j] = New->host[j] = 0;
74 Instance->Users->AddLocalClone(New);
75 Instance->Users->AddGlobalClone(New);
78 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
79 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
81 ConnectClass* i = New->SetClass();
85 User::QuitUser(Instance, New, "Access denied by configuration");
90 * Check connect class settings and initialise settings into User.
91 * This will be done again after DNS resolution. -- w00t
95 this->local_users.push_back(New);
97 if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= MAXCLIENTS))
99 Instance->SNO->WriteToSnoMask('A', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
100 User::QuitUser(Instance, New,"No more connections allowed");
106 * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
107 * its a pretty big but for the moment valid assumption:
108 * file descriptors are handed out starting at 0, and are recycled as theyre freed.
109 * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
110 * irc server at once (or the irc server otherwise initiating this many connections, files etc)
111 * which for the time being is a physical impossibility (even the largest networks dont have more
112 * than about 10,000 users on ONE server!)
115 if ((unsigned int)socket >= MAX_DESCRIPTORS)
117 User::QuitUser(Instance, New, "Server is full");
122 * even with bancache, we still have to keep User::exempt current.
123 * besides that, if we get a positive bancache hit, we still won't fuck
124 * them over if they are exempt. -- w00t
126 New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
128 if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
130 if (!b->Type.empty() && !New->exempt)
133 Instance->Log(DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
134 if (*Instance->Config->MoronBanner)
135 New->WriteServ("NOTICE %s :*** %s", New->nick, Instance->Config->MoronBanner);
136 User::QuitUser(Instance, New, b->Reason);
141 Instance->Log(DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
148 XLine* r = Instance->XLines->MatchesLine("Z",New);
158 if (!Instance->SE->AddFd(New))
160 Instance->Log(DEBUG,"Internal error on new connection");
161 User::QuitUser(Instance, New, "Internal error handling connection");
164 /* NOTE: even if dns lookups are *off*, we still need to display this.
165 * BOPM and other stuff requires it.
167 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
169 if (Instance->Config->NoUserDns)
171 New->dns_done = true;
175 New->StartDNSLookup();
179 void UserManager::AddLocalClone(User *user)
181 clonemap::iterator x = local_clones.find(user->GetIPString());
182 if (x != local_clones.end())
185 local_clones[user->GetIPString()] = 1;
188 void UserManager::AddGlobalClone(User *user)
190 clonemap::iterator y = global_clones.find(user->GetIPString());
191 if (y != global_clones.end())
194 global_clones[user->GetIPString()] = 1;
197 void UserManager::RemoveCloneCounts(User *user)
199 clonemap::iterator x = local_clones.find(user->GetIPString());
200 if (x != local_clones.end())
205 local_clones.erase(x);
209 clonemap::iterator y = global_clones.find(user->GetIPString());
210 if (y != global_clones.end())
215 global_clones.erase(y);
220 unsigned long UserManager::GlobalCloneCount(User *user)
222 clonemap::iterator x = global_clones.find(user->GetIPString());
223 if (x != global_clones.end())
229 unsigned long UserManager::LocalCloneCount(User *user)
231 clonemap::iterator x = local_clones.find(user->GetIPString());
232 if (x != local_clones.end())
238 /* this function counts all users connected, wether they are registered or NOT. */
239 unsigned int UserManager::UserCount()
243 * As part of this restructuring, move clientlist/etc fields into usermanager.
246 return this->clientlist->size();
249 /* this counts only registered users, so that the percentages in /MAP don't mess up */
250 unsigned int UserManager::RegisteredUserCount()
252 return this->clientlist->size() - this->UnregisteredUserCount();
255 /* return how many users are opered */
256 unsigned int UserManager::OperCount()
258 return this->all_opers.size();
261 /* return how many users are unregistered */
262 unsigned int UserManager::UnregisteredUserCount()
264 return this->unregistered_count;
267 /* return how many local registered users there are */
268 unsigned int UserManager::LocalUserCount()
270 /* Doesnt count unregistered clients */
271 return (this->local_users.size() - this->UnregisteredUserCount());
274 void UserManager::ServerNoticeAll(char* text, ...)
279 char textbuffer[MAXBUF];
280 char formatbuffer[MAXBUF];
282 va_start (argsPtr, text);
283 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
286 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
288 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
291 t->WriteServ(std::string(formatbuffer));
295 void UserManager::ServerPrivmsgAll(char* text, ...)
300 char textbuffer[MAXBUF];
301 char formatbuffer[MAXBUF];
303 va_start (argsPtr, text);
304 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
307 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
309 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
312 t->WriteServ(std::string(formatbuffer));
316 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
318 char textbuffer[MAXBUF];
322 if (!text || !modes || !flags)
324 ServerInstance->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
328 va_start(argsPtr, text);
329 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
331 modelen = strlen(modes);
335 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
338 bool send_to_user = true;
340 for (int n = 0; n < modelen; n++)
342 if (!t->IsModeSet(modes[n]))
344 send_to_user = false;
350 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
354 else if (flags == WM_OR)
356 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
359 bool send_to_user = false;
361 for (int n = 0; n < modelen; n++)
363 if (t->IsModeSet(modes[n]))
372 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
378 /* return how many users have a given mode e.g. 'a' */
379 int UserManager::ModeCount(const char mode)
381 ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
384 return mh->GetCount();