]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/listmode.h
ListModeBase: Cache max items per channel
[user/henk/code/inspircd.git] / include / listmode.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #pragma once
20
21 /** The base class for list modes, should be inherited.
22  */
23 class ListModeBase : public ModeHandler
24 {
25  public:
26         /** An item in a listmode's list
27          */
28         struct ListItem
29         {
30                 std::string setter;
31                 std::string mask;
32                 time_t time;
33                 ListItem(const std::string& Mask, const std::string& Setter, time_t Time)
34                         : setter(Setter), mask(Mask), time(Time) { }
35         };
36
37         /** Items stored in the channel's list
38          */
39         typedef std::list<ListItem> ModeList;
40
41  private:
42         class ChanData
43         {
44         public:
45                 ModeList list;
46                 int maxitems;
47
48                 ChanData() : maxitems(-1) { }
49         };
50
51         /** The number of items a listmode's list may contain
52          */
53         struct ListLimit
54         {
55                 std::string mask;
56                 unsigned int limit;
57                 ListLimit(const std::string& Mask, unsigned int Limit) : mask(Mask), limit(Limit) { }
58                 bool operator==(const ListLimit& other) const { return (this->mask == other.mask && this->limit == other.limit); }
59         };
60
61         /** Max items per channel by name
62          */
63         typedef std::vector<ListLimit> limitlist;
64
65         /** Finds the limit of modes that can be placed on the given channel name according to the config
66          * @param channame The channel name to find the limit for
67          * @return The maximum number of modes of this type that we allow to be set on the given channel name
68          */
69         unsigned int FindLimit(const std::string& channame);
70
71         /** Returns the limit on the given channel for this mode.
72          * If the limit is cached then the cached value is returned,
73          * otherwise the limit is determined using FindLimit() and cached
74          * for later queries before it is returned
75          * @param channame The channel name to find the limit for
76          * @param cd The ChanData associated with channel channame
77          * @return The maximum number of modes of this type that we allow to be set on the given channel
78          */
79         unsigned int GetLimitInternal(const std::string& channame, ChanData* cd);
80
81  protected:
82         /** Numeric to use when outputting the list
83          */
84         unsigned int listnumeric;
85         /** Numeric to indicate end of list
86          */
87         unsigned int endoflistnumeric;
88         /** String to send for end of list
89          */
90         std::string endofliststring;
91         /** Automatically tidy up entries
92          */
93         bool tidy;
94         /** Config tag to check for max items per channel
95          */
96         std::string configtag;
97         /** Limits on a per-channel basis read from the tag
98          * specified in ListModeBase::configtag
99          */
100         limitlist chanlimits;
101
102         /** Storage key
103          */
104         SimpleExtItem<ChanData> extItem;
105
106  public:
107         /** Constructor.
108          * @param Instance The creator of this class
109          * @param modechar Mode character
110          * @param eolstr End of list string
111          * @param lnum List numeric
112          * @param eolnum End of list numeric
113          * @param autotidy Automatically tidy list entries on add
114          * @param ctag Configuration tag to get limits from
115          */
116         ListModeBase(Module* Creator, const std::string& Name, char modechar, const std::string &eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy, const std::string &ctag = "banlist");
117
118         /** Get limit of this mode on a channel
119          * @param channel The channel to inspect
120          * @return Maximum number of modes of this type that can be placed on the given channel
121          */
122         unsigned int GetLimit(Channel* channel);
123
124         /** Retrieves the list of all modes set on the given channel
125          * @param channel Channel to get the list from
126          * @return A list with all modes of this type set on the given channel, can be NULL
127          */
128         ModeList* GetList(Channel* channel);
129
130         /** Display the list for this mode
131          * See mode.h
132          * @param user The user to send the list to
133          * @param channel The channel the user is requesting the list for
134          */
135         virtual void DisplayList(User* user, Channel* channel);
136
137         /** Tell a user that a list contains no elements.
138          * Sends 'eolnum' numeric with text 'eolstr', unless overridden (see constructor)
139          * @param user The user issuing the command
140          * @param channel The channel that has the empty list
141          * See mode.h
142          */
143         virtual void DisplayEmptyList(User* user, Channel* channel);
144
145         /** Remove all instances of the mode from a channel.
146          * See mode.h
147          * @param channel The channel to remove all instances of the mode from
148          */
149         virtual void RemoveMode(Channel* channel, irc::modestacker* stack);
150
151         /** Listmodes don't get set on users, no-op
152         */
153         virtual void RemoveMode(User*, irc::modestacker* stack);
154
155         /** Perform a rehash of this mode's configuration data
156          */
157         virtual void DoRehash();
158
159         /** Populate the Implements list with the correct events for a List Mode
160          */
161         virtual void DoImplements(Module* m);
162
163         /** Handle the list mode.
164          * See mode.h
165          */
166         virtual ModeAction OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding);
167
168         /** Syncronize channel item list with another server.
169          * See modules.h
170          * @param chan Channel to syncronize
171          * @param proto Protocol module pointer
172          * @param opaque Opaque connection handle
173          */
174         virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque);
175
176         /** Validate parameters.
177          * Overridden by implementing module.
178          * @param source Source user adding the parameter
179          * @param channel Channel the parameter is being added to
180          * @param parameter The actual parameter being added
181          * @return true if the parameter is valid
182          */
183         virtual bool ValidateParam(User* user, Channel* channel, std::string& parameter);
184
185         /** Tell the user the list is too long.
186          * Overridden by implementing module.
187          * @param source Source user adding the parameter
188          * @param channel Channel the parameter is being added to
189          * @param parameter The actual parameter being added
190          */
191         virtual void TellListTooLong(User* source, Channel* channel, std::string& parameter);
192
193         /** Tell the user an item is already on the list.
194          * Overridden by implementing module.
195          * @param source Source user adding the parameter
196          * @param channel Channel the parameter is being added to
197          * @param parameter The actual parameter being added
198          */
199         virtual void TellAlreadyOnList(User* source, Channel* channel, std::string& parameter);
200
201         /** Tell the user that the parameter is not in the list.
202          * Overridden by implementing module.
203          * @param source Source user removing the parameter
204          * @param channel Channel the parameter is being removed from
205          * @param parameter The actual parameter being removed
206          */
207         virtual void TellNotSet(User* source, Channel* channel, std::string& parameter);
208 };
209
210 inline ListModeBase::ModeList* ListModeBase::GetList(Channel* channel)
211 {
212         ChanData* cd = extItem.get(channel);
213         if (!cd)
214                 return NULL;
215
216         return &cd->list;
217 }