]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cull_list.cpp
bfa37e51a77458253cfdbc13121ea73dc6da21b2
[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
44 /*
45  * In current implementation of CullList, this isn't used. It did odd things with a lot of sockets.
46  */
47 bool CullList::IsValid(userrec* user)
48 {
49         time_t esignon = 0;
50         std::map<userrec*,time_t>::iterator es = exempt.find(user);
51         if (es != exempt.end())
52                 esignon = es->second;
53
54         for (user_hash::iterator u = ServerInstance->clientlist.begin(); u != ServerInstance->clientlist.end(); u++)
55         {
56                 /*
57                  * BUGFIX
58                  *
59                  * Because there is an undetermined period of time between a user existing,
60                  * and this function being called, we have to check for the following condition:
61                  *
62                  * Between CullList::AddItem(u) being called, and CullList::IsValid(u) being called,
63                  * the user with the pointer u has quit, but only to be REPLACED WITH A NEW USER WHO
64                  * BECAUSE OF ALLOCATION RULES, HAS THE SAME MEMORY ADDRESS! To prevent this, we
65                  * cross reference each pointer to the user's signon time, and if the signon times
66                  * do not match, we return false here to indicate this user is NOT valid as it
67                  * seems to differ from the pointer snapshot we got a few seconds earlier. Should
68                  * prevent a few random crashes during netsplits.
69                  */
70                 if (user == u->second)
71                         return (u->second->signon == esignon);
72         }
73         return false;
74 }
75
76 CullItem::CullItem(userrec* u, std::string &r)
77 {
78         this->user = u;
79         this->reason = r;
80 }
81
82 CullItem::CullItem(userrec* u, const char* r)
83 {
84         this->user = u;
85         this->reason = r;
86 }
87
88 CullItem::~CullItem()
89 {
90 }
91
92 userrec* CullItem::GetUser()
93 {
94         return this->user;
95 }
96
97 std::string& CullItem::GetReason()
98 {
99         return this->reason;
100 }
101
102 CullList::CullList()
103 {
104         list.clear();
105         exempt.clear();
106 }
107
108 void CullList::AddItem(userrec* user, std::string &reason)
109 {
110         if (exempt.find(user) == exempt.end())
111         {
112                 CullItem item(user,reason);
113                 list.push_back(item);
114                 exempt[user] = user->signon;
115         }
116 }
117
118 void CullList::AddItem(userrec* user, const char* reason)
119 {
120         if (exempt.find(user) == exempt.end())
121         {
122                 CullItem item(user,reason);
123                 list.push_back(item);
124                 exempt[user] = user->signon;
125         }
126 }
127
128 int CullList::Apply()
129 {
130         int n = list.size();
131         while (list.size())
132         {
133                 std::vector<CullItem>::iterator a = list.begin();
134
135                 userrec::QuitUser(a->GetUser(), a->GetReason().c_str());
136                 list.erase(list.begin());
137         }
138         return n;
139 }