1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 * ---------------------------------------------------
18 /* add a client connection to the sockets list */
19 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
21 /* NOTE: Calling this one parameter constructor for User automatically
22 * allocates a new UUID and places it in the hash_map.
24 LocalUser* New = NULL;
27 New = new LocalUser(socket, client, server);
31 ServerInstance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
32 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
35 UserIOHandler* eh = &New->eh;
37 /* Give each of the modules an attempt to hook the user for I/O */
38 FOREACH_MOD(I_OnHookIO, OnHookIO(eh, via));
44 eh->GetIOHook()->OnStreamSocketAccept(eh, client, server);
46 catch (CoreException& modexcept)
48 ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
52 ServerInstance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
54 this->unregistered_count++;
56 /* The users default nick is their UUID */
57 New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
58 (*(this->clientlist))[New->nick] = New;
60 New->ident.assign("unknown");
62 New->registered = REG_NONE;
63 New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
66 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
67 New->dhost.assign(New->GetIPString(), 0, 64);
68 New->host.assign(New->GetIPString(), 0, 64);
70 ServerInstance->Users->AddLocalClone(New);
71 ServerInstance->Users->AddGlobalClone(New);
73 this->local_users.push_back(New);
75 if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
77 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
78 this->QuitUser(New,"No more connections allowed");
83 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
84 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
89 * Check connect class settings and initialise settings into User.
90 * This will be done again after DNS resolution. -- w00t
97 * even with bancache, we still have to keep User::exempt current.
98 * besides that, if we get a positive bancache hit, we still won't fuck
99 * them over if they are exempt. -- w00t
101 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
103 if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
105 if (!b->Type.empty() && !New->exempt)
108 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
109 if (!ServerInstance->Config->MoronBanner.empty())
110 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
111 this->QuitUser(New, b->Reason);
116 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
123 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
133 if (!ServerInstance->SE->AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
135 ServerInstance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
136 this->QuitUser(New, "Internal error handling connection");
139 /* NOTE: even if dns lookups are *off*, we still need to display this.
140 * BOPM and other stuff requires it.
142 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
143 if (ServerInstance->Config->RawLog)
144 New->WriteServ("NOTICE Auth :*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
146 FOREACH_MOD(I_OnUserInit,OnUserInit(New));
148 if (ServerInstance->Config->NoUserDns)
150 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
151 New->dns_done = true;
155 New->StartDNSLookup();
159 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
163 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
169 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a fake user (%s)", user->nick.c_str());
173 user->quitting = true;
175 ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), 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());
179 std::string oper_reason;
180 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
181 if (operreason && *operreason)
182 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
184 oper_reason = quitreason;
186 ServerInstance->GlobalCulls.AddItem(user);
188 if (user->registered == REG_ALL)
190 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
191 user->WriteCommonQuit(reason, oper_reason);
194 if (user->registered != REG_ALL)
195 if (ServerInstance->Users->unregistered_count)
196 ServerInstance->Users->unregistered_count--;
200 LocalUser* lu = IS_LOCAL(user);
201 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(lu));
206 * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
207 * if they were an oper with +sn +qQ.
209 if (user->registered == REG_ALL)
213 if (!user->quietquit)
215 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s] (%s)",
216 user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str(), user->GetIPString());
221 if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
223 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s] (%s)",
224 user->server.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str(), user->GetIPString());
230 user_hash::iterator iter = this->clientlist->find(user->nick);
232 if (iter != this->clientlist->end())
233 this->clientlist->erase(iter);
235 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
237 ServerInstance->Users->uuidlist->erase(user->uuid);
241 void UserManager::AddLocalClone(User *user)
243 clonemap::iterator x;
244 x = local_clones.find(user->GetCIDRMask());
245 if (x != local_clones.end())
248 local_clones[user->GetCIDRMask()] = 1;
251 void UserManager::AddGlobalClone(User *user)
253 clonemap::iterator x;
255 x = global_clones.find(user->GetCIDRMask());
256 if (x != global_clones.end())
259 global_clones[user->GetCIDRMask()] = 1;
262 void UserManager::RemoveCloneCounts(User *user)
266 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
267 if (x != local_clones.end())
272 local_clones.erase(x);
277 clonemap::iterator y = global_clones.find(user->GetCIDRMask());
278 if (y != global_clones.end())
283 global_clones.erase(y);
288 unsigned long UserManager::GlobalCloneCount(User *user)
290 clonemap::iterator x = global_clones.find(user->GetCIDRMask());
291 if (x != global_clones.end())
297 unsigned long UserManager::LocalCloneCount(User *user)
299 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
300 if (x != local_clones.end())
306 /* this function counts all users connected, wether they are registered or NOT. */
307 unsigned int UserManager::UserCount()
311 * As part of this restructuring, move clientlist/etc fields into usermanager.
314 return this->clientlist->size();
317 /* this counts only registered users, so that the percentages in /MAP don't mess up */
318 unsigned int UserManager::RegisteredUserCount()
320 return this->clientlist->size() - this->UnregisteredUserCount();
323 /* return how many users are opered */
324 unsigned int UserManager::OperCount()
326 return this->all_opers.size();
329 /* return how many users are unregistered */
330 unsigned int UserManager::UnregisteredUserCount()
332 return this->unregistered_count;
335 /* return how many local registered users there are */
336 unsigned int UserManager::LocalUserCount()
338 /* Doesnt count unregistered clients */
339 return (this->local_users.size() - this->UnregisteredUserCount());
342 void UserManager::ServerNoticeAll(const char* text, ...)
347 char textbuffer[MAXBUF];
348 char formatbuffer[MAXBUF];
350 va_start (argsPtr, text);
351 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
354 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
356 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
359 t->WriteServ(std::string(formatbuffer));
363 void UserManager::ServerPrivmsgAll(const char* text, ...)
368 char textbuffer[MAXBUF];
369 char formatbuffer[MAXBUF];
371 va_start (argsPtr, text);
372 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
375 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
377 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
380 t->WriteServ(std::string(formatbuffer));
385 /* return how many users have a given mode e.g. 'a' */
386 int UserManager::ModeCount(const char mode)
389 for(user_hash::iterator i = clientlist->begin(); i != clientlist->end(); ++i)
392 if (u->modes[mode-65])