]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/listmode.h
Convert InspIRCd::SetSignals to a static function.
[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::vector<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         /** The default maximum list size. */
67         static const unsigned int DEFAULT_LIST_SIZE = 100;
68
69         /** Finds the limit of modes that can be placed on the given channel name according to the config
70          * @param channame The channel name to find the limit for
71          * @return The maximum number of modes of this type that we allow to be set on the given channel name
72          */
73         unsigned int FindLimit(const std::string& channame);
74
75         /** Returns the limit on the given channel for this mode.
76          * If the limit is cached then the cached value is returned,
77          * otherwise the limit is determined using FindLimit() and cached
78          * for later queries before it is returned
79          * @param channame The channel name to find the limit for
80          * @param cd The ChanData associated with channel channame
81          * @return The maximum number of modes of this type that we allow to be set on the given channel
82          */
83         unsigned int GetLimitInternal(const std::string& channame, ChanData* cd);
84
85  protected:
86         /** Numeric to use when outputting the list
87          */
88         unsigned int listnumeric;
89
90         /** Numeric to indicate end of list
91          */
92         unsigned int endoflistnumeric;
93
94         /** String to send for end of list
95          */
96         std::string endofliststring;
97
98         /** Automatically tidy up entries
99          */
100         bool tidy;
101
102         /** Limits on a per-channel basis read from the \<listmode>
103          * config tag.
104          */
105         limitlist chanlimits;
106
107         /** Storage key
108          */
109         SimpleExtItem<ChanData> extItem;
110
111  public:
112         /** Constructor.
113          * @param Creator The creator of this class
114          * @param Name Mode name
115          * @param modechar Mode character
116          * @param eolstr End of list string
117          * @param lnum List numeric
118          * @param eolnum End of list numeric
119          * @param autotidy Automatically tidy list entries on add
120          */
121         ListModeBase(Module* Creator, const std::string& Name, char modechar, const std::string& eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy);
122
123         /** Determines whether some channels have longer lists than others. */
124         bool HasVariableLength() const { return chanlimits.size() > 1; }
125
126         /** Get limit of this mode on a channel
127          * @param channel The channel to inspect
128          * @return Maximum number of modes of this type that can be placed on the given channel
129          */
130         unsigned int GetLimit(Channel* channel);
131
132         /** Gets the lower list limit for this listmode.
133          */
134         unsigned int GetLowerLimit();
135
136         /** Retrieves the list of all modes set on the given channel
137          * @param channel Channel to get the list from
138          * @return A list with all modes of this type set on the given channel, can be NULL
139          */
140         ModeList* GetList(Channel* channel);
141
142         /** Display the list for this mode
143          * See mode.h
144          * @param user The user to send the list to
145          * @param channel The channel the user is requesting the list for
146          */
147         void DisplayList(User* user, Channel* channel) CXX11_OVERRIDE;
148
149         /** Tell a user that a list contains no elements.
150          * Sends 'eolnum' numeric with text 'eolstr', unless overridden (see constructor)
151          * @param user The user issuing the command
152          * @param channel The channel that has the empty list
153          * See mode.h
154          */
155         void DisplayEmptyList(User* user, Channel* channel) CXX11_OVERRIDE;
156
157         /** Remove all instances of the mode from a channel.
158          * Populates the given modestack with modes that remove every instance of
159          * this mode from the channel.
160          * See mode.h for more details.
161          * @param channel The channel to remove all instances of the mode from
162          * @param changelist Mode change list to populate with the removal of this mode
163          */
164         void RemoveMode(Channel* channel, Modes::ChangeList& changelist) CXX11_OVERRIDE;
165
166         /** Perform a rehash of this mode's configuration data
167          */
168         void DoRehash();
169
170         /** Handle the list mode.
171          * See mode.h
172          */
173         ModeAction OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding) CXX11_OVERRIDE;
174
175         /** Validate parameters.
176          * Overridden by implementing module.
177          * @param user Source user adding the parameter
178          * @param channel Channel the parameter is being added to
179          * @param parameter The actual parameter being added
180          * @return true if the parameter is valid
181          */
182         virtual bool ValidateParam(User* user, Channel* channel, std::string& parameter);
183
184         /** In the event that the mode should be given a parameter, and no parameter was provided, this method is called.
185          * This allows you to give special information to the user, or handle this any way you like.
186          * @param user The user issuing the mode change
187          * @param dest For user mode changes, the target of the mode. For channel mode changes, NULL.
188          * @param channel For channel mode changes, the target of the mode. For user mode changes, NULL.
189          * See mode.h
190          */
191         virtual void OnParameterMissing(User* user, User* dest, Channel* channel) CXX11_OVERRIDE;
192
193         /** Tell the user the list is too long.
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 TellListTooLong(User* source, Channel* channel, std::string& parameter);
200
201         /** Tell the user an item is already on the list.
202          * Overridden by implementing module.
203          * @param source Source user adding the parameter
204          * @param channel Channel the parameter is being added to
205          * @param parameter The actual parameter being added
206          */
207         virtual void TellAlreadyOnList(User* source, Channel* channel, std::string& parameter);
208
209         /** Tell the user that the parameter is not in the list.
210          * Overridden by implementing module.
211          * @param source Source user removing the parameter
212          * @param channel Channel the parameter is being removed from
213          * @param parameter The actual parameter being removed
214          */
215         virtual void TellNotSet(User* source, Channel* channel, std::string& parameter);
216 };
217
218 inline ListModeBase::ModeList* ListModeBase::GetList(Channel* channel)
219 {
220         ChanData* cd = extItem.get(channel);
221         if (!cd)
222                 return NULL;
223
224         return &cd->list;
225 }