]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cull_list.cpp
b7254fd0b0d3bd3ba3b4a6fed6fe69fd2986257c
[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 bool CullList::IsValid(userrec* user)
56 {
57         time_t esignon = 0;
58         std::map<userrec*,time_t>::iterator es = exempt.find(user);
59         if (es != exempt.end())
60                 esignon = es->second;
61
62         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
63         {
64                 /*
65                  * BUGFIX
66                  *
67                  * Because there is an undetermined period of time between a user existing,
68                  * and this function being called, we have to check for the following condition:
69                  *
70                  * Between CullList::AddItem(u) being called, and CullList::IsValid(u) being called,
71                  * the user with the pointer u has quit, but only to be REPLACED WITH A NEW USER WHO
72                  * BECAUSE OF ALLOCATION RULES, HAS THE SAME MEMORY ADDRESS! To prevent this, we
73                  * cross reference each pointer to the user's signon time, and if the signon times
74                  * do not match, we return false here to indicate this user is NOT valid as it
75                  * seems to differ from the pointer snapshot we got a few seconds earlier. Should
76                  * prevent a few random crashes during netsplits.
77                  */
78                 if (user == u->second)
79                         return (u->second->signon == esignon);
80         }
81         return false;
82 }
83
84 CullItem::CullItem(userrec* u, std::string &r)
85 {
86         this->user = u;
87         this->reason = r;
88 }
89
90 CullItem::CullItem(userrec* u, const char* r)
91 {
92         this->user = u;
93         this->reason = r;
94 }
95
96 userrec* CullItem::GetUser()
97 {
98         return this->user;
99 }
100
101 std::string CullItem::GetReason()
102 {
103         return this->reason;
104 }
105
106 CullList::CullList()
107 {
108         list.clear();
109         exempt.clear();
110 }
111
112 void CullList::AddItem(userrec* user, std::string &reason)
113 {
114         if (exempt.find(user) == exempt.end())
115         {
116                 CullItem item(user,reason);
117                 list.push_back(item);
118                 exempt[user] = user->signon;
119         }
120 }
121
122 void CullList::AddItem(userrec* user, const char* reason)
123 {
124         if (exempt.find(user) == exempt.end())
125         {
126                 CullItem item(user,reason);
127                 list.push_back(item);
128                 exempt[user] = user->signon;
129         }
130 }
131
132 int CullList::Apply()
133 {
134         int n = 0;
135         while (list.size())
136         {
137                 std::vector<CullItem>::iterator a = list.begin();
138                 userrec* u = a->GetUser();
139                 /* Because ServerInstance->DoOneIteration can
140                  * take the user away from us in the middle of
141                  * our operation, we should check to see if this
142                  * pointer is still valid by iterating the hash.
143                  * It's expensive, yes, but the DoOneIteration
144                  * call stops it being horrendously bad.
145                  */
146                 if (IsValid(u))
147                 {
148                         kill_link(u,a->GetReason().c_str());
149                         list.erase(list.begin());
150                         /* So that huge numbers of quits dont block,
151                          * we yield back to our mainloop every 15
152                          * iterations.
153                          * The DoOneIteration call basically acts
154                          * like a software threading mechanism.
155                          */
156                         if (((n++) % 15) == 0)
157                         {
158                                 ServerInstance->DoOneIteration(false);
159                         }
160                 }
161         }
162         return n;
163 }