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