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