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