]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cull_list.cpp
61324c45459d516eba929ea22caaf1c5e98fba55
[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 "inspircd_io.h"
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/errno.h>
25 #include <time.h>
26 #include <string>
27 #ifdef GCC3
28 #include <ext/hash_map>
29 #else
30 #include <hash_map>
31 #endif
32 #include <map>
33 #include <sstream>
34 #include <vector>
35 #include <deque>
36 #include "users.h"
37 #include "ctables.h"
38 #include "globals.h"
39 #include "modules.h"
40 #include "dynamic.h"
41 #include "wildcard.h"
42 #include "message.h"
43 #include "commands.h"
44 #include "xline.h"
45 #include "inspstring.h"
46 #include "inspircd.h"
47 #include "helperfuncs.h"
48 #include "hashcomp.h"
49 #include "typedefs.h"
50 #include "cull_list.h"
51
52 extern InspIRCd* ServerInstance;
53 extern user_hash clientlist;
54
55 /*
56  * In current implementation of CullList, this isn't used. It did odd things with a lot of sockets.
57  */
58 bool CullList::IsValid(userrec* user)
59 {
60         time_t esignon = 0;
61         std::map<userrec*,time_t>::iterator es = exempt.find(user);
62         if (es != exempt.end())
63                 esignon = es->second;
64
65         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
66         {
67                 /*
68                  * BUGFIX
69                  *
70                  * Because there is an undetermined period of time between a user existing,
71                  * and this function being called, we have to check for the following condition:
72                  *
73                  * Between CullList::AddItem(u) being called, and CullList::IsValid(u) being called,
74                  * the user with the pointer u has quit, but only to be REPLACED WITH A NEW USER WHO
75                  * BECAUSE OF ALLOCATION RULES, HAS THE SAME MEMORY ADDRESS! To prevent this, we
76                  * cross reference each pointer to the user's signon time, and if the signon times
77                  * do not match, we return false here to indicate this user is NOT valid as it
78                  * seems to differ from the pointer snapshot we got a few seconds earlier. Should
79                  * prevent a few random crashes during netsplits.
80                  */
81                 if (user == u->second)
82                         return (u->second->signon == esignon);
83         }
84         return false;
85 }
86
87 CullItem::CullItem(userrec* u, std::string &r)
88 {
89         this->user = u;
90         this->reason = r;
91 }
92
93 CullItem::CullItem(userrec* u, const char* r)
94 {
95         this->user = u;
96         this->reason = r;
97 }
98
99 CullItem::~CullItem()
100 {
101 }
102
103 userrec* CullItem::GetUser()
104 {
105         return this->user;
106 }
107
108 std::string& CullItem::GetReason()
109 {
110         return this->reason;
111 }
112
113 CullList::CullList()
114 {
115         list.clear();
116         exempt.clear();
117 }
118
119 void CullList::AddItem(userrec* user, std::string &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 void CullList::AddItem(userrec* user, const char* reason)
130 {
131         if (exempt.find(user) == exempt.end())
132         {
133                 CullItem item(user,reason);
134                 list.push_back(item);
135                 exempt[user] = user->signon;
136         }
137 }
138
139 int CullList::Apply()
140 {
141         int n = 0;
142         while (list.size())
143         {
144                 std::vector<CullItem>::iterator a = list.begin();
145
146                 kill_link(a->GetUser(), a->GetReason().c_str());
147                 list.erase(list.begin());
148         }
149         return n;
150 }