]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/cull_list.h
e20e4556a24a0032571168b6fd6552398a7be6ff
[user/henk/code/inspircd.git] / include / cull_list.h
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 #ifndef __CULLLIST_H__
18 #define __CULLLIST_H__
19
20 // include the common header files
21
22 #include <string>
23 #include <deque>
24 #include <vector>
25 #include "users.h"
26 #include "channels.h"
27
28 class InspIRCd;
29
30 /** The CullItem class holds a user and their quitmessage,
31  * and is used internally by the CullList class to compile
32  * a list of users which are to be culled when a long
33  * operation (such as a netsplit) has completed.
34  */
35 class CullItem : public classbase
36 {
37  private:
38         /** Holds a pointer to the user,
39          * must be valid and can be a local or remote user.
40          */
41         userrec* user;
42         /** Holds the quit reason to use for this user.
43          */
44         std::string reason;
45  public:
46         /** Constrcutor.
47          * Initializes the CullItem with a user pointer
48          * and their quit reason
49          * @param u The user to add
50          * @param r The quit reason of the added user
51          */
52         CullItem(userrec* u, std::string &r);
53         CullItem(userrec* u, const char* r);
54
55         ~CullItem();
56
57         /** Returns a pointer to the user
58          */
59         userrec* GetUser();
60         /** Returns the user's quit reason
61          */
62         std::string& GetReason();
63 };
64
65 /** The CullList class can be used by modules, and is used
66  * by the core, to compile large lists of users in preperation
67  * to quitting them all at once. This is faster than quitting
68  * them within the loop, as the loops become tighter with
69  * little or no comparisons within them. The CullList class
70  * operates by allowing the programmer to push users onto
71  * the list, each with a seperate quit reason, and then, once
72  * the list is complete, call a method to flush the list,
73  * quitting all the users upon it. A CullList may hold local
74  * or remote users, but it may only hold each user once. If
75  * you attempt to add the same user twice, then the second
76  * attempt will be ignored.
77  */
78 class CullList : public classbase
79 {
80  private:
81          /** Creator of this CullList
82           */
83          InspIRCd* ServerInstance;
84          /** Holds a list of users being quit.
85           * See the information for CullItem for
86           * more information.
87           */
88          std::vector<CullItem> list;
89          /** A list of users who have already been
90           * placed on the list, as a map for fast
91           * reference. When deleting an item, the
92           * time_t value stored here must match
93           * the one of the actual userrec, otherwise
94           * we don't delete it (its a different user)
95           */
96          std::map<userrec*,time_t> exempt;
97          
98          /** Check if a user pointer is valid
99           * (e.g. it exists in the user hash)
100           */
101          bool IsValid(userrec* user);
102  public:
103          /** Constructor.
104           * Clears the CullList::list and CullList::exempt
105           * items.
106           * @param Instance Creator of this CullList object
107           */
108          CullList(InspIRCd* Instance);
109          /** Adds a user to the cull list for later
110           * removal via QUIT.
111           * @param user The user to add
112           * @param reason The quit reason of the user being added
113           */
114          void AddItem(userrec* user, std::string &reason);
115          void AddItem(userrec* user, const char* reason);
116          /** Applies the cull list, quitting all the users
117           * on the list with their quit reasons all at once.
118           * This is a very fast operation compared to
119           * iterating the user list and comparing each one,
120           * especially if there are multiple comparisons
121           * to be done, or recursion.
122           * @returns The number of users removed from IRC.
123           */
124          int Apply();
125 };
126
127 #endif