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