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(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
23 /* NOTE: Calling this one parameter constructor for User automatically
24 * allocates a new UUID and places it in the hash_map.
26 LocalUser* New = NULL;
29 New = new LocalUser();
33 ServerInstance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
34 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
39 memcpy(&New->client_sa, client, sizeof(irc::sockets::sockaddrs));
40 memcpy(&New->server_sa, server, sizeof(irc::sockets::sockaddrs));
42 /* Give each of the modules an attempt to hook the user for I/O */
43 FOREACH_MOD(I_OnHookIO, OnHookIO(New, via));
49 New->GetIOHook()->OnStreamSocketAccept(New, client, server);
51 catch (CoreException& modexcept)
53 ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
57 ServerInstance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
59 this->unregistered_count++;
61 (*(this->clientlist))[New->uuid] = New;
63 /* The users default nick is their UUID */
64 New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
66 New->server = ServerInstance->Config->ServerName;
67 New->ident.assign("unknown");
69 New->registered = REG_NONE;
70 New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
73 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
74 New->dhost.assign(New->GetIPString(), 0, 64);
75 New->host.assign(New->GetIPString(), 0, 64);
77 ServerInstance->Users->AddLocalClone(New);
78 ServerInstance->Users->AddGlobalClone(New);
81 * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
82 * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
87 * Check connect class settings and initialise settings into User.
88 * This will be done again after DNS resolution. -- w00t
92 this->local_users.push_back(New);
94 if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
96 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
97 this->QuitUser(New,"No more connections allowed");
102 * even with bancache, we still have to keep User::exempt current.
103 * besides that, if we get a positive bancache hit, we still won't fuck
104 * them over if they are exempt. -- w00t
106 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
108 if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
110 if (!b->Type.empty() && !New->exempt)
113 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
114 if (!ServerInstance->Config->MoronBanner.empty())
115 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
116 this->QuitUser(New, b->Reason);
121 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
128 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
138 if (!ServerInstance->SE->AddFd(New, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
140 ServerInstance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
141 this->QuitUser(New, "Internal error handling connection");
144 /* NOTE: even if dns lookups are *off*, we still need to display this.
145 * BOPM and other stuff requires it.
147 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
149 if (ServerInstance->Config->NoUserDns)
151 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
152 New->dns_done = true;
156 New->StartDNSLookup();
160 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
164 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
170 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a fake user (%s)", user->nick.c_str());
174 user->quitting = true;
176 ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
177 user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
180 std::string oper_reason;
181 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
182 if (operreason && *operreason)
183 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
185 oper_reason = quitreason;
187 ServerInstance->GlobalCulls.AddItem(user);
189 if (user->registered == REG_ALL)
191 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
192 user->WriteCommonQuit(reason, oper_reason);
195 if (user->registered != REG_ALL)
196 if (ServerInstance->Users->unregistered_count)
197 ServerInstance->Users->unregistered_count--;
201 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(IS_LOCAL(user)));
203 if (user->GetIOHook())
207 user->GetIOHook()->OnStreamSocketClose(user);
209 catch (CoreException& modexcept)
211 ServerInstance->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
215 ServerInstance->SE->DelFd(user);
217 // user->Close() will set fd to -1; this breaks IS_LOCAL. Fix
218 user->SetFd(INT_MAX);
222 * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
223 * if they were an oper with +sn +qQ.
225 if (user->registered == REG_ALL)
229 if (!user->quietquit)
231 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",
232 user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
237 if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
239 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",
240 user->server.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
246 user_hash::iterator iter = this->clientlist->find(user->nick);
248 if (iter != this->clientlist->end())
249 this->clientlist->erase(iter);
251 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
255 void UserManager::AddLocalClone(User *user)
257 clonemap::iterator x;
258 x = local_clones.find(user->GetCIDRMask());
259 if (x != local_clones.end())
262 local_clones[user->GetCIDRMask()] = 1;
265 void UserManager::AddGlobalClone(User *user)
267 clonemap::iterator x;
269 x = global_clones.find(user->GetCIDRMask());
270 if (x != global_clones.end())
273 global_clones[user->GetCIDRMask()] = 1;
276 void UserManager::RemoveCloneCounts(User *user)
278 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
279 if (x != local_clones.end())
284 local_clones.erase(x);
288 clonemap::iterator y = global_clones.find(user->GetCIDRMask());
289 if (y != global_clones.end())
294 global_clones.erase(y);
299 unsigned long UserManager::GlobalCloneCount(User *user)
301 clonemap::iterator x = global_clones.find(user->GetCIDRMask());
302 if (x != global_clones.end())
308 unsigned long UserManager::LocalCloneCount(User *user)
310 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
311 if (x != local_clones.end())
317 /* this function counts all users connected, wether they are registered or NOT. */
318 unsigned int UserManager::UserCount()
322 * As part of this restructuring, move clientlist/etc fields into usermanager.
325 return this->clientlist->size();
328 /* this counts only registered users, so that the percentages in /MAP don't mess up */
329 unsigned int UserManager::RegisteredUserCount()
331 return this->clientlist->size() - this->UnregisteredUserCount();
334 /* return how many users are opered */
335 unsigned int UserManager::OperCount()
337 return this->all_opers.size();
340 /* return how many users are unregistered */
341 unsigned int UserManager::UnregisteredUserCount()
343 return this->unregistered_count;
346 /* return how many local registered users there are */
347 unsigned int UserManager::LocalUserCount()
349 /* Doesnt count unregistered clients */
350 return (this->local_users.size() - this->UnregisteredUserCount());
353 void UserManager::ServerNoticeAll(const char* text, ...)
358 char textbuffer[MAXBUF];
359 char formatbuffer[MAXBUF];
361 va_start (argsPtr, text);
362 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
365 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
367 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
370 t->WriteServ(std::string(formatbuffer));
374 void UserManager::ServerPrivmsgAll(const char* text, ...)
379 char textbuffer[MAXBUF];
380 char formatbuffer[MAXBUF];
382 va_start (argsPtr, text);
383 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
386 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
388 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
391 t->WriteServ(std::string(formatbuffer));
395 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
397 char textbuffer[MAXBUF];
401 if (!text || !modes || !flags)
403 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
407 va_start(argsPtr, text);
408 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
410 modelen = strlen(modes);
414 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
417 bool send_to_user = true;
419 for (int n = 0; n < modelen; n++)
421 if (!t->IsModeSet(modes[n]))
423 send_to_user = false;
429 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
433 else if (flags == WM_OR)
435 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
438 bool send_to_user = false;
440 for (int n = 0; n < modelen; n++)
442 if (t->IsModeSet(modes[n]))
451 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
457 /* return how many users have a given mode e.g. 'a' */
458 int UserManager::ModeCount(const char mode)
460 ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
463 return mh->GetCount();