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