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