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