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