1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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, irc::sockets::sockaddrs* 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!");
39 memcpy(&New->ip, ip, sizeof(*ip));
41 /* Give each of the modules an attempt to hook the user for I/O */
42 FOREACH_MOD_I(Instance, I_OnHookUserIO, OnHookUserIO(New, targetip));
48 New->GetIOHook()->OnRawSocketAccept(socket, New->GetIPString(), port);
50 catch (CoreException& modexcept)
52 ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
56 Instance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
58 this->unregistered_count++;
60 (*(this->clientlist))[New->uuid] = New;
62 /* The users default nick is their UUID */
63 New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
65 New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
66 New->ident.assign("unknown");
68 New->registered = REG_NONE;
69 New->signon = Instance->Time() + Instance->Config->dns_timeout;
72 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
73 New->dhost.assign(New->GetIPString(), 0, 64);
74 New->host.assign(New->GetIPString(), 0, 64);
76 Instance->Users->AddLocalClone(New);
77 Instance->Users->AddGlobalClone(New);
80 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
81 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
83 ConnectClass* i = New->SetClass();
87 this->QuitUser(New, "Access denied by configuration");
92 * Check connect class settings and initialise settings into User.
93 * This will be done again after DNS resolution. -- w00t
97 this->local_users.push_back(New);
99 if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)Instance->SE->GetMaxFds()))
101 Instance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
102 this->QuitUser(New,"No more connections allowed");
107 * even with bancache, we still have to keep User::exempt current.
108 * besides that, if we get a positive bancache hit, we still won't fuck
109 * them over if they are exempt. -- w00t
111 New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
113 if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
115 if (!b->Type.empty() && !New->exempt)
118 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
119 if (*Instance->Config->MoronBanner)
120 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), Instance->Config->MoronBanner);
121 this->QuitUser(New, b->Reason);
126 Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
133 XLine* r = Instance->XLines->MatchesLine("Z",New);
143 if (!Instance->SE->AddFd(New))
145 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
146 this->QuitUser(New, "Internal error handling connection");
149 /* NOTE: even if dns lookups are *off*, we still need to display this.
150 * BOPM and other stuff requires it.
152 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
154 if (Instance->Config->NoUserDns)
156 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
157 New->dns_done = true;
161 New->StartDNSLookup();
165 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
169 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
173 user->quitting = true;
175 ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s '%s'", user->nick.c_str(), quitreason.c_str());
176 user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
178 user->quietquit = false;
179 user->quitmsg = quitreason;
182 std::string oper_reason;
183 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
186 user->operquitmsg = quitreason;
187 oper_reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
191 user->operquitmsg = operreason;
192 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
195 ServerInstance->GlobalCulls.AddItem(user);
197 if (user->registered == REG_ALL)
199 FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
200 user->PurgeEmptyChannels();
201 user->WriteCommonQuit(reason, oper_reason);
204 FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(user));
206 // Move the user onto their UID, to allow nick to be reused immediately
207 user->UpdateNickHash(user->uuid.c_str());
211 void UserManager::AddLocalClone(User *user)
214 clonemap::iterator x;
215 switch (user->ip.sa.sa_family)
218 range = ServerInstance->Config->c_ipv6_range;
221 range = ServerInstance->Config->c_ipv4_range;
225 x = local_clones.find(user->GetCIDRMask(range));
226 if (x != local_clones.end())
229 local_clones[user->GetCIDRMask(range)] = 1;
232 void UserManager::AddGlobalClone(User *user)
235 clonemap::iterator x;
236 switch (user->ip.sa.sa_family)
239 range = ServerInstance->Config->c_ipv6_range;
242 range = ServerInstance->Config->c_ipv4_range;
246 x = global_clones.find(user->GetCIDRMask(range));
247 if (x != global_clones.end())
250 global_clones[user->GetCIDRMask(range)] = 1;
253 void UserManager::RemoveCloneCounts(User *user)
256 switch (user->ip.sa.sa_family)
259 range = ServerInstance->Config->c_ipv6_range;
262 range = ServerInstance->Config->c_ipv4_range;
266 clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
267 if (x != local_clones.end())
272 local_clones.erase(x);
276 clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
277 if (y != global_clones.end())
282 global_clones.erase(y);
287 unsigned long UserManager::GlobalCloneCount(User *user)
290 switch (user->ip.sa.sa_family)
293 range = ServerInstance->Config->c_ipv6_range;
296 range = ServerInstance->Config->c_ipv4_range;
299 clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
300 if (x != global_clones.end())
306 unsigned long UserManager::LocalCloneCount(User *user)
309 switch (user->ip.sa.sa_family)
312 range = ServerInstance->Config->c_ipv6_range;
315 range = ServerInstance->Config->c_ipv4_range;
318 clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
319 if (x != local_clones.end())
325 /* this function counts all users connected, wether they are registered or NOT. */
326 unsigned int UserManager::UserCount()
330 * As part of this restructuring, move clientlist/etc fields into usermanager.
333 return this->clientlist->size();
336 /* this counts only registered users, so that the percentages in /MAP don't mess up */
337 unsigned int UserManager::RegisteredUserCount()
339 return this->clientlist->size() - this->UnregisteredUserCount();
342 /* return how many users are opered */
343 unsigned int UserManager::OperCount()
345 return this->all_opers.size();
348 /* return how many users are unregistered */
349 unsigned int UserManager::UnregisteredUserCount()
351 return this->unregistered_count;
354 /* return how many local registered users there are */
355 unsigned int UserManager::LocalUserCount()
357 /* Doesnt count unregistered clients */
358 return (this->local_users.size() - this->UnregisteredUserCount());
361 void UserManager::ServerNoticeAll(const char* text, ...)
366 char textbuffer[MAXBUF];
367 char formatbuffer[MAXBUF];
369 va_start (argsPtr, text);
370 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
373 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
375 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
378 t->WriteServ(std::string(formatbuffer));
382 void UserManager::ServerPrivmsgAll(const char* text, ...)
387 char textbuffer[MAXBUF];
388 char formatbuffer[MAXBUF];
390 va_start (argsPtr, text);
391 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
394 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
396 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
399 t->WriteServ(std::string(formatbuffer));
403 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
405 char textbuffer[MAXBUF];
409 if (!text || !modes || !flags)
411 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
415 va_start(argsPtr, text);
416 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
418 modelen = strlen(modes);
422 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
425 bool send_to_user = true;
427 for (int n = 0; n < modelen; n++)
429 if (!t->IsModeSet(modes[n]))
431 send_to_user = false;
437 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
441 else if (flags == WM_OR)
443 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
446 bool send_to_user = false;
448 for (int n = 0; n < modelen; n++)
450 if (t->IsModeSet(modes[n]))
459 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
465 /* return how many users have a given mode e.g. 'a' */
466 int UserManager::ModeCount(const char mode)
468 ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
471 return mh->GetCount();