]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cull_list.cpp
Missing externs
[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         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
58         {
59                 userrec* u2 = (userrec*)*u;
60                 if (user == u2)
61                         return true;
62         }
63         return false;
64 }
65
66 CullItem::CullItem(userrec* u, std::string r)
67 {
68         this->user = u;
69         this->reason = r;
70 }
71
72 userrec* CullItem::GetUser()
73 {
74         return this->user;
75 }
76
77 std::string CullItem::GetReason()
78 {
79         return this->reason;
80 }
81
82 CullList::CullList()
83 {
84         list.clear();
85         exempt.clear();
86 }
87
88 void CullList::AddItem(userrec* user, std::string reason)
89 {
90         if (exempt.find(user) == exempt.end())
91         {
92                 CullItem item(user,reason);
93                 list.push_back(item);
94                 names.push_back(user->nick);
95                 exempt[user] = 1;
96         }
97 }
98
99 int CullList::Apply()
100 {
101         int n = 0;
102         while (list.size())
103         {
104                 std::vector<CullItem>::iterator a = list.begin();
105                 userrec* u = a->GetUser();
106                 /* Because ServerInstance->DoOneIteration can
107                  * take the user away from us in the middle of
108                  * our operation, we should check to see if this
109                  * pointer is still valid by iterating the hash.
110                  * It's expensive, yes, but the DoOneIteration
111                  * call stops it being horrendously bad.
112                  */
113                 if (IsValid(u))
114                 {
115                         kill_link(u,a->GetReason().c_str());
116                         list.erase(list.begin());
117                         /* So that huge numbers of quits dont block,
118                          * we yield back to our mainloop every 15
119                          * iterations.
120                          * The DoOneIteration call basically acts
121                          * like a software threading mechanism.
122                          */
123                         if (((n++) % 15) == 0)
124                         {
125                                 ServerInstance->DoOneIteration(false);
126                         }
127                 }
128         }
129         return n;
130 }