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