]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cull_list.cpp
Convert connection::host
[user/henk/code/inspircd.git] / src / cull_list.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDcull_list */
15
16 #include "inspircd.h"
17 #include "cull_list.h"
18
19 CullList::CullList(InspIRCd* Instance) : ServerInstance(Instance)
20 {
21         list.clear();
22 }
23
24 void CullList::AddItem(User* user)
25 {
26         if (user->quitting)
27         {
28                 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
29                 return;
30         }
31
32         user->quitting = true;
33         list.push_back(user);
34 }
35
36 void CullList::MakeSilent(User* user)
37 {
38         user->quietquit = true;
39         return;
40 }
41
42 int CullList::Apply()
43 {
44         int n = list.size();
45         int i = 0;
46
47         while (list.size() && i++ != 100)
48         {
49                 std::vector<User *>::iterator a = list.begin();
50
51                 User *u = (*a);
52                 user_hash::iterator iter = ServerInstance->Users->clientlist->find(u->nick);
53                 const std::string& preset_reason = u->GetOperQuit();
54                 std::string reason;
55                 std::string oper_reason;
56
57                 reason.assign(u->quitmsg, 0, MAXQUIT - 1);
58                 oper_reason.assign(preset_reason.empty() ? preset_reason : u->operquitmsg, 0, MAXQUIT - 1);
59
60                 if (u->registered != REG_ALL)
61                         if (ServerInstance->Users->unregistered_count)
62                                 ServerInstance->Users->unregistered_count--;
63
64                 if (IS_LOCAL(u))
65                 {
66                         if ((!u->sendq.empty()) && (!(*u->GetWriteError())))
67                                 u->FlushWriteBuf();
68                 }
69
70                 if (u->registered == REG_ALL)
71                 {
72                         FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(u, reason, oper_reason));
73                         u->PurgeEmptyChannels();
74                         u->WriteCommonQuit(reason, oper_reason);
75                 }
76
77                 FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(u));
78
79                 if (IS_LOCAL(u))
80                 {
81                         if (u->io)
82                         {
83                                 try
84                                 {
85                                         u->io->OnRawSocketClose(u->GetFd());
86                                 }
87                                 catch (CoreException& modexcept)
88                                 {
89                                         ServerInstance->Logs->Log("CULLLIST",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
90                                 }
91                         }
92
93                         ServerInstance->SE->DelFd(u);
94                         u->CloseSocket();
95                 }
96
97                 /*
98                  * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
99                  * if they were an oper with +sn +qQ.
100                  */
101                 if (u->registered == REG_ALL)
102                 {
103                         if (IS_LOCAL(u))
104                         {
105                                 if (!u->quietquit)
106                                 {
107                                         ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]", u->nick.c_str(), u->ident.c_str(), u->host.c_str(), oper_reason.c_str());
108                                 }
109                         }
110                         else
111                         {
112                                 if ((!ServerInstance->SilentULine(u->server)) && (!u->quietquit))
113                                 {
114                                         ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]", u->server, u->nick.c_str(), u->ident.c_str(), u->host.c_str(), oper_reason.c_str());
115                                 }
116                         }
117                         u->AddToWhoWas();
118                 }
119
120                 bool deleteu = true;
121
122                 if (iter != ServerInstance->Users->clientlist->end())
123                 {
124                         ServerInstance->Users->clientlist->erase(iter);
125                 }
126                 else
127                 {
128                         /*
129                          * Trying to track down Jason's issue.. this should never happen obviously.
130                          */
131                         ServerInstance->Logs->Log("CULLLIST", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic?");
132 //                      deleteu = false;
133 // actually, delete them anyway.. the local vector is the real problem here
134                 }
135
136                 if (IS_LOCAL(u))
137                 {
138                         std::vector<User*>::iterator x = find(ServerInstance->Users->local_users.begin(),ServerInstance->Users->local_users.end(),u);
139                         if (x != ServerInstance->Users->local_users.end())
140                                 ServerInstance->Users->local_users.erase(x);
141                         else
142                         {
143                                 /*
144                                  * This code is in here to monitor an issue of Jason's, where it seems to be trying to quit already quit users.
145                                  * The only way that can happen is if this find fails, so log it just in case.
146                                  * Also, (perhaps incorrectly, but oh well), return here so we don't delete the user and then start trampling
147                                  * on deleted memory, which leads to big problems..
148                                  */
149                                 ServerInstance->Logs->Log("CULLLIST", DEBUG, "Failed to remove user from vector, we're all gonna die!!! Not deleting the user to save our sanity");
150                                 deleteu = false;
151                         }
152                 }
153
154                 if (deleteu)
155                 {
156                         delete u;
157                 }
158
159                 list.erase(list.begin());
160         }
161
162         return n;
163 }
164