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