]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cull_list.cpp
Fix for bug #199 (Feature request) submitted by owine. Ended up adding an extra param...
[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 "users.h"
16 #include "cull_list.h"
17
18 CullItem::CullItem(userrec* u, std::string &r, const char* o_reason)
19 {
20         this->user = u;
21         this->reason = r;
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(userrec* u, const char* r, const char* o_reason)
30 {
31         this->user = u;
32         this->reason = r;
33         /* Seperate oper reason not set, use the user reason */
34         if (*o_reason)
35                 this->oper_reason = o_reason;
36         else
37                 this->oper_reason = r;
38 }
39
40 CullItem::~CullItem()
41 {
42 }
43
44 userrec* CullItem::GetUser()
45 {
46         return this->user;
47 }
48
49 std::string& CullItem::GetReason()
50 {
51         return this->reason;
52 }
53
54 std::string& CullItem::GetOperReason()
55 {
56         return this->oper_reason;
57 }
58
59 CullList::CullList(InspIRCd* Instance) : ServerInstance(Instance)
60 {
61         list.clear();
62         exempt.clear();
63 }
64
65 void CullList::AddItem(userrec* user, std::string &reason, const char* o_reason)
66 {
67         AddItem(user, reason.c_str(), o_reason);
68 }
69
70
71 void CullList::AddItem(userrec* user, const char* reason, const char* o_reason)
72 {
73         if (exempt.find(user) == exempt.end())
74         {
75                 CullItem item(user, reason, o_reason);
76                 list.push_back(item);
77                 exempt[user] = user;
78         }
79 }
80
81 int CullList::Apply()
82 {
83         int n = list.size();
84         while (list.size())
85         {
86                 std::vector<CullItem>::iterator a = list.begin();
87
88                 user_hash::iterator iter = ServerInstance->clientlist->find(a->GetUser()->nick);
89                 std::map<userrec*, userrec*>::iterator exemptiter = exempt.find(a->GetUser());
90                 const char* preset_reason = a->GetUser()->GetOperQuit();
91                 std::string reason = a->GetReason();
92                 std::string oper_reason = *preset_reason ? preset_reason : a->GetOperReason();
93
94                 if (reason.length() > MAXQUIT - 1)
95                         reason.resize(MAXQUIT - 1);
96                 if (oper_reason.length() > MAXQUIT - 1)
97                         oper_reason.resize(MAXQUIT - 1);
98
99                 if (a->GetUser()->registered != REG_ALL)
100                         if (ServerInstance->unregistered_count)
101                                 ServerInstance->unregistered_count--;
102
103                 if (IS_LOCAL(a->GetUser()))
104                 {
105                         a->GetUser()->Write("ERROR :Closing link (%s@%s) [%s]", a->GetUser()->ident, a->GetUser()->host, oper_reason.c_str());
106                         if ((!a->GetUser()->sendq.empty()) && (!(*a->GetUser()->GetWriteError())))
107                                 a->GetUser()->FlushWriteBuf();
108                 }
109
110                 if (a->GetUser()->registered == REG_ALL)
111                 {
112                         a->GetUser()->PurgeEmptyChannels();
113                         a->GetUser()->WriteCommonQuit(reason, oper_reason);
114                         FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(a->GetUser(), reason, oper_reason));
115                 }
116
117                 FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(a->GetUser()));
118
119                 if (IS_LOCAL(a->GetUser()))
120                 {
121                         if (ServerInstance->Config->GetIOHook(a->GetUser()->GetPort()))
122                         {
123                                 try
124                                 {
125                                         ServerInstance->Config->GetIOHook(a->GetUser()->GetPort())->OnRawSocketClose(a->GetUser()->GetFd());
126                                 }
127                                 catch (CoreException& modexcept)
128                                 {
129                                         ServerInstance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
130                                 }
131                         }
132
133                         ServerInstance->SE->DelFd(a->GetUser());
134                         a->GetUser()->CloseSocket();
135                 }
136
137                 /*
138                  * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
139                  * if they were an oper with +sn +qQ.
140                  */
141                 if (a->GetUser()->registered == REG_ALL)
142                 {
143                         if (IS_LOCAL(a->GetUser()))
144                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",a->GetUser()->nick,a->GetUser()->ident,a->GetUser()->host,oper_reason.c_str());
145                         else
146                         {
147                                 if (!ServerInstance->SilentULine(a->GetUser()->server))
148                                         ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",a->GetUser()->server,a->GetUser()->nick,a->GetUser()->ident,a->GetUser()->host,oper_reason.c_str());
149                         }
150                         a->GetUser()->AddToWhoWas();
151                 }
152
153                 if (iter != ServerInstance->clientlist->end())
154                 {
155                         if (IS_LOCAL(a->GetUser()))
156                         {
157                                 std::vector<userrec*>::iterator x = find(ServerInstance->local_users.begin(),ServerInstance->local_users.end(),a->GetUser());
158                                 if (x != ServerInstance->local_users.end())
159                                         ServerInstance->local_users.erase(x);
160                         }
161                         ServerInstance->clientlist->erase(iter);
162                         DELETE(a->GetUser());
163                 }
164
165                 list.erase(list.begin());
166                 exempt.erase(exemptiter);
167         }
168         return n;
169 }
170