]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cull_list.cpp
Cache the culllist' GetUser() return. Looks cleaner.
[user/henk/code/inspircd.git] / src / cull_list.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 #include "inspircd.h"
15 #include "cull_list.h"
16
17 CullItem::CullItem(User* u, std::string &r, const char* o_reason)
18 {
19         this->user = u;
20         this->reason = r;
21         this->silent = false;
22         /* Seperate oper reason not set, use the user reason */
23         if (*o_reason)
24                 this->oper_reason = o_reason;
25         else
26                 this->oper_reason = r;
27 }
28
29 CullItem::CullItem(User* u, const char* r, const char* o_reason)
30 {
31         this->user = u;
32         this->reason = r;
33         this->silent = false;
34         /* Seperate oper reason not set, use the user reason */
35         if (*o_reason)
36                 this->oper_reason = o_reason;
37         else
38                 this->oper_reason = r;
39 }
40
41 void CullItem::MakeSilent()
42 {
43         this->silent = true;
44 }
45
46 bool CullItem::IsSilent()
47 {
48         return this->silent;
49 }
50
51 CullItem::~CullItem()
52 {
53 }
54
55 User* CullItem::GetUser()
56 {
57         return this->user;
58 }
59
60 std::string& CullItem::GetReason()
61 {
62         return this->reason;
63 }
64
65 std::string& CullItem::GetOperReason()
66 {
67         return this->oper_reason;
68 }
69
70 CullList::CullList(InspIRCd* Instance) : ServerInstance(Instance)
71 {
72         list.clear();
73         exempt.clear();
74 }
75
76 void CullList::AddItem(User* user, std::string &reason, const char* o_reason)
77 {
78         AddItem(user, reason.c_str(), o_reason);
79 }
80
81
82 void CullList::AddItem(User* user, const char* reason, const char* o_reason)
83 {
84         if (exempt.find(user) == exempt.end())
85         {
86                 CullItem item(user, reason, o_reason);
87                 list.push_back(item);
88                 exempt[user] = user;
89         }
90 }
91
92 void CullList::MakeSilent(User* user)
93 {
94         for (std::vector<CullItem>::iterator a = list.begin(); a != list.end(); ++a)
95         {
96                 if (a->GetUser() == user)
97                 {
98                         a->MakeSilent();
99                         break;
100                 }
101         }
102         return;
103 }
104
105 int CullList::Apply()
106 {
107         int n = list.size();
108         while (list.size())
109         {
110                 std::vector<CullItem>::iterator a = list.begin();
111
112                 User *u = a->GetUser();
113                 user_hash::iterator iter = ServerInstance->clientlist->find(u->nick);
114                 std::map<User*, User*>::iterator exemptiter = exempt.find(u);
115                 const char* preset_reason = u->GetOperQuit();
116                 std::string reason = a->GetReason();
117                 std::string oper_reason = *preset_reason ? preset_reason : a->GetOperReason();
118
119                 if (reason.length() > MAXQUIT - 1)
120                         reason.resize(MAXQUIT - 1);
121                 if (oper_reason.length() > MAXQUIT - 1)
122                         oper_reason.resize(MAXQUIT - 1);
123
124                 if (u->registered != REG_ALL)
125                         if (ServerInstance->unregistered_count)
126                                 ServerInstance->unregistered_count--;
127
128                 if (IS_LOCAL(u))
129                 {
130                         if ((!u->sendq.empty()) && (!(*u->GetWriteError())))
131                                 u->FlushWriteBuf();
132                 }
133
134                 if (u->registered == REG_ALL)
135                 {
136                         FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(u, reason, oper_reason));
137                         u->PurgeEmptyChannels();
138                         u->WriteCommonQuit(reason, oper_reason);
139                 }
140
141                 FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(u));
142
143                 if (IS_LOCAL(u))
144                 {
145                         if (ServerInstance->Config->GetIOHook(u->GetPort()))
146                         {
147                                 try
148                                 {
149                                         ServerInstance->Config->GetIOHook(u->GetPort())->OnRawSocketClose(u->GetFd());
150                                 }
151                                 catch (CoreException& modexcept)
152                                 {
153                                         ServerInstance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
154                                 }
155                         }
156
157                         ServerInstance->SE->DelFd(u);
158                         u->CloseSocket();
159                 }
160
161                 /*
162                  * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
163                  * if they were an oper with +sn +qQ.
164                  */
165                 if (u->registered == REG_ALL)
166                 {
167                         if (IS_LOCAL(u))
168                         {
169                                 if (!a->IsSilent())
170                                 {
171                                         ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",u->nick,u->ident,u->host,oper_reason.c_str());
172                                 }
173                         }
174                         else
175                         {
176                                 if ((!ServerInstance->SilentULine(u->server)) && (!a->IsSilent()))
177                                 {
178                                         ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",u->server,u->nick,u->ident,u->host,oper_reason.c_str());
179                                 }
180                         }
181                         u->AddToWhoWas();
182                 }
183
184                 if (iter != ServerInstance->clientlist->end())
185                 {
186                         if (IS_LOCAL(u))
187                         {
188                                 std::vector<User*>::iterator x = find(ServerInstance->local_users.begin(),ServerInstance->local_users.end(),u);
189                                 if (x != ServerInstance->local_users.end())
190                                         ServerInstance->local_users.erase(x);
191                         }
192                         ServerInstance->clientlist->erase(iter);
193                         delete u;
194                 }
195
196                 list.erase(list.begin());
197                 exempt.erase(exemptiter);
198         }
199         return n;
200 }
201