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