]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cull_list.cpp
e6b105925b39a2cbe5c06f37a6569b07889e60b1
[user/henk/code/inspircd.git] / src / cull_list.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include <string>
22 #include <map>
23 #include <sstream>
24 #include <vector>
25 #include <deque>
26 #include "users.h"
27 #include "ctables.h"
28 #include "globals.h"
29 #include "modules.h"
30 #include "dynamic.h"
31 #include "wildcard.h"
32 #include "message.h"
33 #include "commands.h"
34 #include "xline.h"
35 #include "inspstring.h"
36 #include "inspircd.h"
37 #include "helperfuncs.h"
38 #include "hashcomp.h"
39 #include "typedefs.h"
40 #include "cull_list.h"
41
42 extern InspIRCd* ServerInstance;
43 extern user_hash clientlist;
44
45 /*
46  * In current implementation of CullList, this isn't used. It did odd things with a lot of sockets.
47  */
48 bool CullList::IsValid(userrec* user)
49 {
50         time_t esignon = 0;
51         std::map<userrec*,time_t>::iterator es = exempt.find(user);
52         if (es != exempt.end())
53                 esignon = es->second;
54
55         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
56         {
57                 /*
58                  * BUGFIX
59                  *
60                  * Because there is an undetermined period of time between a user existing,
61                  * and this function being called, we have to check for the following condition:
62                  *
63                  * Between CullList::AddItem(u) being called, and CullList::IsValid(u) being called,
64                  * the user with the pointer u has quit, but only to be REPLACED WITH A NEW USER WHO
65                  * BECAUSE OF ALLOCATION RULES, HAS THE SAME MEMORY ADDRESS! To prevent this, we
66                  * cross reference each pointer to the user's signon time, and if the signon times
67                  * do not match, we return false here to indicate this user is NOT valid as it
68                  * seems to differ from the pointer snapshot we got a few seconds earlier. Should
69                  * prevent a few random crashes during netsplits.
70                  */
71                 if (user == u->second)
72                         return (u->second->signon == esignon);
73         }
74         return false;
75 }
76
77 CullItem::CullItem(userrec* u, std::string &r)
78 {
79         this->user = u;
80         this->reason = r;
81 }
82
83 CullItem::CullItem(userrec* u, const char* r)
84 {
85         this->user = u;
86         this->reason = r;
87 }
88
89 CullItem::~CullItem()
90 {
91 }
92
93 userrec* CullItem::GetUser()
94 {
95         return this->user;
96 }
97
98 std::string& CullItem::GetReason()
99 {
100         return this->reason;
101 }
102
103 CullList::CullList()
104 {
105         list.clear();
106         exempt.clear();
107 }
108
109 void CullList::AddItem(userrec* user, std::string &reason)
110 {
111         if (exempt.find(user) == exempt.end())
112         {
113                 CullItem item(user,reason);
114                 list.push_back(item);
115                 exempt[user] = user->signon;
116         }
117 }
118
119 void CullList::AddItem(userrec* user, const char* reason)
120 {
121         if (exempt.find(user) == exempt.end())
122         {
123                 CullItem item(user,reason);
124                 list.push_back(item);
125                 exempt[user] = user->signon;
126         }
127 }
128
129 int CullList::Apply()
130 {
131         int n = list.size();
132         while (list.size())
133         {
134                 std::vector<CullItem>::iterator a = list.begin();
135
136                 userrec::QuitUser(a->GetUser(), a->GetReason().c_str());
137                 list.erase(list.begin());
138         }
139         return n;
140 }