]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cull_list.cpp
Fixes for snotice quit reasons
[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                 std::string reason = a->GetReason();
91                 std::string oper_reason = a->GetOperReason();
92
93                 if (reason.length() > MAXQUIT - 1)
94                         reason.resize(MAXQUIT - 1);
95                 if (oper_reason.length() > MAXQUIT - 1)
96                         oper_reason.resize(MAXQUIT - 1);
97
98                 if (a->GetUser()->registered != REG_ALL)
99                         if (ServerInstance->unregistered_count)
100                                 ServerInstance->unregistered_count--;
101
102                 if (IS_LOCAL(a->GetUser()))
103                 {
104                         a->GetUser()->Write("ERROR :Closing link (%s@%s) [%s]", a->GetUser()->ident, a->GetUser()->host, oper_reason.c_str());
105                         if ((!a->GetUser()->sendq.empty()) && (!(*a->GetUser()->GetWriteError())))
106                                 a->GetUser()->FlushWriteBuf();
107                 }
108
109                 if (a->GetUser()->registered == REG_ALL)
110                 {
111                         a->GetUser()->PurgeEmptyChannels();
112                         a->GetUser()->WriteCommonQuit(reason, oper_reason);
113                         FOREACH_MOD_I(ServerInstance,I_OnUserQuit,OnUserQuit(a->GetUser(), reason));
114                 }
115
116                 FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(a->GetUser()));
117
118                 if (IS_LOCAL(a->GetUser()))
119                 {
120                         if (ServerInstance->Config->GetIOHook(a->GetUser()->GetPort()))
121                         {
122                                 try
123                                 {
124                                         ServerInstance->Config->GetIOHook(a->GetUser()->GetPort())->OnRawSocketClose(a->GetUser()->GetFd());
125                                 }
126                                 catch (CoreException& modexcept)
127                                 {
128                                         ServerInstance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
129                                 }
130                         }
131
132                         ServerInstance->SE->DelFd(a->GetUser());
133                         a->GetUser()->CloseSocket();
134                 }
135
136                 /*
137                  * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
138                  * if they were an oper with +sn +qQ.
139                  */
140                 if (a->GetUser()->registered == REG_ALL)
141                 {
142                         if (IS_LOCAL(a->GetUser()))
143                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",a->GetUser()->nick,a->GetUser()->ident,a->GetUser()->host,oper_reason.c_str());
144                         else
145                                 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());
146                         a->GetUser()->AddToWhoWas();
147                 }
148
149                 if (iter != ServerInstance->clientlist->end())
150                 {
151                         if (IS_LOCAL(a->GetUser()))
152                         {
153                                 std::vector<userrec*>::iterator x = find(ServerInstance->local_users.begin(),ServerInstance->local_users.end(),a->GetUser());
154                                 if (x != ServerInstance->local_users.end())
155                                         ServerInstance->local_users.erase(x);
156                         }
157                         ServerInstance->clientlist->erase(iter);
158                         DELETE(a->GetUser());
159                 }
160
161                 list.erase(list.begin());
162                 exempt.erase(exemptiter);
163         }
164         return n;
165 }
166