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