]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cull_list.cpp
8dd1f6ae142347b212cedcf401e34c717fca5972
[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 /* $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(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         while (list.size())
111         {
112                 std::vector<CullItem>::iterator a = list.begin();
113
114                 User *u = a->GetUser();
115                 user_hash::iterator iter = ServerInstance->clientlist->find(u->nick);
116                 std::map<User*, User*>::iterator exemptiter = exempt.find(u);
117                 const char* preset_reason = u->GetOperQuit();
118                 std::string reason = a->GetReason();
119                 std::string oper_reason = *preset_reason ? preset_reason : a->GetOperReason();
120
121                 if (reason.length() > MAXQUIT - 1)
122                         reason.resize(MAXQUIT - 1);
123                 if (oper_reason.length() > MAXQUIT - 1)
124                         oper_reason.resize(MAXQUIT - 1);
125
126                 if (u->registered != REG_ALL)
127                         if (ServerInstance->unregistered_count)
128                                 ServerInstance->unregistered_count--;
129
130                 if (IS_LOCAL(u))
131                 {
132                         if ((!u->sendq.empty()) && (!(*u->GetWriteError())))
133                                 u->FlushWriteBuf();
134                 }
135
136                 if (u->registered == REG_ALL)
137                 {
138                         FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(u, reason, oper_reason));
139                         u->PurgeEmptyChannels();
140                         u->WriteCommonQuit(reason, oper_reason);
141                 }
142
143                 FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(u));
144
145                 if (IS_LOCAL(u))
146                 {
147                         if (ServerInstance->Config->GetIOHook(u->GetPort()))
148                         {
149                                 try
150                                 {
151                                         ServerInstance->Config->GetIOHook(u->GetPort())->OnRawSocketClose(u->GetFd());
152                                 }
153                                 catch (CoreException& modexcept)
154                                 {
155                                         ServerInstance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
156                                 }
157                         }
158
159                         ServerInstance->SE->DelFd(u);
160                         u->CloseSocket();
161                 }
162
163                 /*
164                  * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
165                  * if they were an oper with +sn +qQ.
166                  */
167                 if (u->registered == REG_ALL)
168                 {
169                         if (IS_LOCAL(u))
170                         {
171                                 if (!a->IsSilent())
172                                 {
173                                         ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",u->nick,u->ident,u->host,oper_reason.c_str());
174                                 }
175                         }
176                         else
177                         {
178                                 if ((!ServerInstance->SilentULine(u->server)) && (!a->IsSilent()))
179                                 {
180                                         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());
181                                 }
182                         }
183                         u->AddToWhoWas();
184                 }
185
186                 if (iter != ServerInstance->clientlist->end())
187                 {
188                         if (IS_LOCAL(u))
189                         {
190                                 std::vector<User*>::iterator x = find(ServerInstance->local_users.begin(),ServerInstance->local_users.end(),u);
191                                 if (x != ServerInstance->local_users.end())
192                                         ServerInstance->local_users.erase(x);
193                         }
194                         ServerInstance->clientlist->erase(iter);
195                         delete u;
196                 }
197
198                 list.erase(list.begin());
199                 exempt.erase(exemptiter);
200         }
201         return n;
202 }
203