]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Snomask qQ: Swap quit message and IP for easier parsing by bots or scripts
[user/henk/code/inspircd.git] / src / usermanager.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "xline.h"
25 #include "bancache.h"
26
27 /* add a client connection to the sockets list */
28 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
29 {
30         /* NOTE: Calling this one parameter constructor for User automatically
31          * allocates a new UUID and places it in the hash_map.
32          */
33         LocalUser* New = NULL;
34         try
35         {
36                 New = new LocalUser(socket, client, server);
37         }
38         catch (...)
39         {
40                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
41                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
42                 return;
43         }
44         UserIOHandler* eh = &New->eh;
45
46         /* Give each of the modules an attempt to hook the user for I/O */
47         FOREACH_MOD(I_OnHookIO, OnHookIO(eh, via));
48
49         if (eh->GetIOHook())
50         {
51                 try
52                 {
53                         eh->GetIOHook()->OnStreamSocketAccept(eh, client, server);
54                 }
55                 catch (CoreException& modexcept)
56                 {
57                         ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
58                 }
59         }
60
61         ServerInstance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
62
63         this->unregistered_count++;
64
65         /* The users default nick is their UUID */
66         New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
67         (*(this->clientlist))[New->nick] = New;
68
69         New->ident.assign("unknown");
70
71         New->registered = REG_NONE;
72         New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
73         New->lastping = 1;
74
75         /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
76         New->dhost.assign(New->GetIPString(), 0, 64);
77         New->host.assign(New->GetIPString(), 0, 64);
78
79         ServerInstance->Users->AddLocalClone(New);
80         ServerInstance->Users->AddGlobalClone(New);
81
82         this->local_users.push_back(New);
83
84         if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
85         {
86                 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
87                 this->QuitUser(New,"No more connections allowed");
88                 return;
89         }
90
91         /*
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
94          */
95         New->SetClass();
96
97         /*
98          * Check connect class settings and initialise settings into User.
99          * This will be done again after DNS resolution. -- w00t
100          */
101         New->CheckClass();
102         if (New->quitting)
103                 return;
104
105         /*
106          * even with bancache, we still have to keep User::exempt current.
107          * besides that, if we get a positive bancache hit, we still won't fuck
108          * them over if they are exempt. -- w00t
109          */
110         New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
111
112         if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
113         {
114                 if (!b->Type.empty() && !New->exempt)
115                 {
116                         /* user banned */
117                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
118                         if (!ServerInstance->Config->MoronBanner.empty())
119                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
120                         this->QuitUser(New, b->Reason);
121                         return;
122                 }
123                 else
124                 {
125                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
126                 }
127         }
128         else
129         {
130                 if (!New->exempt)
131                 {
132                         XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
133
134                         if (r)
135                         {
136                                 r->Apply(New);
137                                 return;
138                         }
139                 }
140         }
141
142         if (!ServerInstance->SE->AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
143         {
144                 ServerInstance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
145                 this->QuitUser(New, "Internal error handling connection");
146         }
147
148         /* NOTE: even if dns lookups are *off*, we still need to display this.
149          * BOPM and other stuff requires it.
150          */
151         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
152         if (ServerInstance->Config->RawLog)
153                 New->WriteServ("NOTICE Auth :*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
154
155         FOREACH_MOD(I_OnUserInit,OnUserInit(New));
156
157         if (ServerInstance->Config->NoUserDns)
158         {
159                 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
160                 New->dns_done = true;
161         }
162         else
163         {
164                 New->StartDNSLookup();
165         }
166 }
167
168 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
169 {
170         if (user->quitting)
171         {
172                 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                 return;
174         }
175
176         if (IS_SERVER(user))
177         {
178                 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a fake user (%s)", user->nick.c_str());
179                 return;
180         }
181
182         user->quitting = true;
183
184         ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
185         user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
186
187         std::string reason;
188         std::string oper_reason;
189         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
190         if (operreason && *operreason)
191                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
192         else
193                 oper_reason = quitreason;
194
195         ServerInstance->GlobalCulls.AddItem(user);
196
197         if (user->registered == REG_ALL)
198         {
199                 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
200                 user->WriteCommonQuit(reason, oper_reason);
201         }
202
203         if (user->registered != REG_ALL)
204                 if (ServerInstance->Users->unregistered_count)
205                         ServerInstance->Users->unregistered_count--;
206
207         if (IS_LOCAL(user))
208         {
209                 LocalUser* lu = IS_LOCAL(user);
210                 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(lu));
211                 lu->eh.Close();
212         }
213
214         /*
215          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
216          * if they were an oper with +s +qQ.
217          */
218         if (user->registered == REG_ALL)
219         {
220                 if (IS_LOCAL(user))
221                 {
222                         if (!user->quietquit)
223                         {
224                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s (%s) [%s]",
225                                         user->nick.c_str(), user->ident.c_str(), user->host.c_str(), user->GetIPString(), oper_reason.c_str());
226                         }
227                 }
228                 else
229                 {
230                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
231                         {
232                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s (%s) [%s]",
233                                         user->server.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), user->GetIPString(), oper_reason.c_str());
234                         }
235                 }
236                 user->AddToWhoWas();
237         }
238
239         user_hash::iterator iter = this->clientlist->find(user->nick);
240
241         if (iter != this->clientlist->end())
242                 this->clientlist->erase(iter);
243         else
244                 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
245
246         ServerInstance->Users->uuidlist->erase(user->uuid);
247 }
248
249
250 void UserManager::AddLocalClone(User *user)
251 {
252         clonemap::iterator x;
253         x = local_clones.find(user->GetCIDRMask());
254         if (x != local_clones.end())
255                 x->second++;
256         else
257                 local_clones[user->GetCIDRMask()] = 1;
258 }
259
260 void UserManager::AddGlobalClone(User *user)
261 {
262         clonemap::iterator x;
263
264         x = global_clones.find(user->GetCIDRMask());
265         if (x != global_clones.end())
266                 x->second++;
267         else
268                 global_clones[user->GetCIDRMask()] = 1;
269 }
270
271 void UserManager::RemoveCloneCounts(User *user)
272 {
273         if (IS_LOCAL(user))
274         {
275                 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
276                 if (x != local_clones.end())
277                 {
278                         x->second--;
279                         if (!x->second)
280                         {
281                                 local_clones.erase(x);
282                         }
283                 }
284         }
285
286         clonemap::iterator y = global_clones.find(user->GetCIDRMask());
287         if (y != global_clones.end())
288         {
289                 y->second--;
290                 if (!y->second)
291                 {
292                         global_clones.erase(y);
293                 }
294         }
295 }
296
297 unsigned long UserManager::GlobalCloneCount(User *user)
298 {
299         clonemap::iterator x = global_clones.find(user->GetCIDRMask());
300         if (x != global_clones.end())
301                 return x->second;
302         else
303                 return 0;
304 }
305
306 unsigned long UserManager::LocalCloneCount(User *user)
307 {
308         clonemap::iterator x = local_clones.find(user->GetCIDRMask());
309         if (x != local_clones.end())
310                 return x->second;
311         else
312                 return 0;
313 }
314
315 /* this function counts all users connected, wether they are registered or NOT. */
316 unsigned int UserManager::UserCount()
317 {
318         /*
319          * XXX: Todo:
320          *  As part of this restructuring, move clientlist/etc fields into usermanager.
321          *      -- w00t
322          */
323         return this->clientlist->size();
324 }
325
326 /* this counts only registered users, so that the percentages in /MAP don't mess up */
327 unsigned int UserManager::RegisteredUserCount()
328 {
329         return this->clientlist->size() - this->UnregisteredUserCount();
330 }
331
332 /* return how many users are opered */
333 unsigned int UserManager::OperCount()
334 {
335         return this->all_opers.size();
336 }
337
338 /* return how many users are unregistered */
339 unsigned int UserManager::UnregisteredUserCount()
340 {
341         return this->unregistered_count;
342 }
343
344 /* return how many local registered users there are */
345 unsigned int UserManager::LocalUserCount()
346 {
347         /* Doesnt count unregistered clients */
348         return (this->local_users.size() - this->UnregisteredUserCount());
349 }
350
351 void UserManager::ServerNoticeAll(const char* text, ...)
352 {
353         if (!text)
354                 return;
355
356         char textbuffer[MAXBUF];
357         char formatbuffer[MAXBUF];
358         va_list argsPtr;
359         va_start (argsPtr, text);
360         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
361         va_end(argsPtr);
362
363         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
364
365         for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
366         {
367                 User* t = *i;
368                 t->WriteServ(std::string(formatbuffer));
369         }
370 }
371
372 void UserManager::ServerPrivmsgAll(const char* text, ...)
373 {
374         if (!text)
375                 return;
376
377         char textbuffer[MAXBUF];
378         char formatbuffer[MAXBUF];
379         va_list argsPtr;
380         va_start (argsPtr, text);
381         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
382         va_end(argsPtr);
383
384         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
385
386         for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
387         {
388                 User* t = *i;
389                 t->WriteServ(std::string(formatbuffer));
390         }
391 }
392
393
394 /* return how many users have a given mode e.g. 'a' */
395 int UserManager::ModeCount(const char mode)
396 {
397         int c = 0;
398         for(user_hash::iterator i = clientlist->begin(); i != clientlist->end(); ++i)
399         {
400                 User* u = i->second;
401                 if (u->modes[mode-65])
402                         c++;
403         }
404         return c;
405 }