]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/listmode.h
Use IsCTCP in blockcolor for ignoring CTCPs.
[user/henk/code/inspircd.git] / include / listmode.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 B00mX0r <b00mx0r@aureus.pw>
5  *   Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #pragma once
23
24 /** The base class for list modes, should be inherited.
25  */
26 class CoreExport ListModeBase : public ModeHandler
27 {
28  public:
29         /** An item in a listmode's list
30          */
31         struct ListItem
32         {
33                 std::string setter;
34                 std::string mask;
35                 time_t time;
36                 ListItem(const std::string& Mask, const std::string& Setter, time_t Time)
37                         : setter(Setter), mask(Mask), time(Time) { }
38         };
39
40         /** Items stored in the channel's list
41          */
42         typedef std::vector<ListItem> ModeList;
43
44  private:
45         class ChanData
46         {
47         public:
48                 ModeList list;
49                 int maxitems;
50
51                 ChanData() : maxitems(-1) { }
52         };
53
54         /** The number of items a listmode's list may contain
55          */
56         struct ListLimit
57         {
58                 std::string mask;
59                 unsigned int limit;
60                 ListLimit(const std::string& Mask, unsigned int Limit) : mask(Mask), limit(Limit) { }
61                 bool operator==(const ListLimit& other) const { return (this->mask == other.mask && this->limit == other.limit); }
62         };
63
64         /** Max items per channel by name
65          */
66         typedef std::vector<ListLimit> limitlist;
67
68         /** The default maximum list size. */
69         static const unsigned int DEFAULT_LIST_SIZE = 100;
70
71         /** Finds the limit of modes that can be placed on the given channel name according to the config
72          * @param channame The channel name to find the limit for
73          * @return The maximum number of modes of this type that we allow to be set on the given channel name
74          */
75         unsigned int FindLimit(const std::string& channame);
76
77         /** Returns the limit on the given channel for this mode.
78          * If the limit is cached then the cached value is returned,
79          * otherwise the limit is determined using FindLimit() and cached
80          * for later queries before it is returned
81          * @param channame The channel name to find the limit for
82          * @param cd The ChanData associated with channel channame
83          * @return The maximum number of modes of this type that we allow to be set on the given channel
84          */
85         unsigned int GetLimitInternal(const std::string& channame, ChanData* cd);
86
87  protected:
88         /** Numeric to use when outputting the list
89          */
90         unsigned int listnumeric;
91
92         /** Numeric to indicate end of list
93          */
94         unsigned int endoflistnumeric;
95
96         /** String to send for end of list
97          */
98         std::string endofliststring;
99
100         /** Automatically tidy up entries
101          */
102         bool tidy;
103
104         /** Limits on a per-channel basis read from the \<listmode>
105          * config tag.
106          */
107         limitlist chanlimits;
108
109         /** Storage key
110          */
111         SimpleExtItem<ChanData> extItem;
112
113  public:
114         /** Constructor.
115          * @param Creator The creator of this class
116          * @param Name Mode name
117          * @param modechar Mode character
118          * @param eolstr End of list string
119          * @param lnum List numeric
120          * @param eolnum End of list numeric
121          * @param autotidy Automatically tidy list entries on add
122          */
123         ListModeBase(Module* Creator, const std::string& Name, char modechar, const std::string& eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy);
124
125         /** Determines whether some channels have longer lists than others. */
126         bool HasVariableLength() const { return chanlimits.size() > 1; }
127
128         /** Get limit of this mode on a channel
129          * @param channel The channel to inspect
130          * @return Maximum number of modes of this type that can be placed on the given channel
131          */
132         unsigned int GetLimit(Channel* channel);
133
134         /** Gets the lower list limit for this listmode.
135          */
136         unsigned int GetLowerLimit();
137
138         /** Retrieves the list of all modes set on the given channel
139          * @param channel Channel to get the list from
140          * @return A list with all modes of this type set on the given channel, can be NULL
141          */
142         ModeList* GetList(Channel* channel);
143
144         /** Display the list for this mode
145          * See mode.h
146          * @param user The user to send the list to
147          * @param channel The channel the user is requesting the list for
148          */
149         void DisplayList(User* user, Channel* channel) CXX11_OVERRIDE;
150
151         /** Tell a user that a list contains no elements.
152          * Sends 'eolnum' numeric with text 'eolstr', unless overridden (see constructor)
153          * @param user The user issuing the command
154          * @param channel The channel that has the empty list
155          * See mode.h
156          */
157         void DisplayEmptyList(User* user, Channel* channel) CXX11_OVERRIDE;
158
159         /** Remove all instances of the mode from a channel.
160          * Populates the given modestack with modes that remove every instance of
161          * this mode from the channel.
162          * See mode.h for more details.
163          * @param channel The channel to remove all instances of the mode from
164          * @param changelist Mode change list to populate with the removal of this mode
165          */
166         void RemoveMode(Channel* channel, Modes::ChangeList& changelist) CXX11_OVERRIDE;
167
168         /** Perform a rehash of this mode's configuration data
169          */
170         void DoRehash();
171
172         /** Handle the list mode.
173          * See mode.h
174          */
175         ModeAction OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding) CXX11_OVERRIDE;
176
177         /** Validate parameters.
178          * Overridden by implementing module.
179          * @param user 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         /** In the event that the mode should be given a parameter, and no parameter was provided, this method is called.
187          * This allows you to give special information to the user, or handle this any way you like.
188          * @param user The user issuing the mode change
189          * @param dest For user mode changes, the target of the mode. For channel mode changes, NULL.
190          * @param channel For channel mode changes, the target of the mode. For user mode changes, NULL.
191          * See mode.h
192          */
193         virtual void OnParameterMissing(User* user, User* dest, Channel* channel) CXX11_OVERRIDE;
194
195         /** Tell the user the list is too long.
196          * Overridden by implementing module.
197          * @param source Source user adding the parameter
198          * @param channel Channel the parameter is being added to
199          * @param parameter The actual parameter being added
200          */
201         virtual void TellListTooLong(User* source, Channel* channel, std::string& parameter);
202
203         /** Tell the user an item is already on the list.
204          * Overridden by implementing module.
205          * @param source Source user adding the parameter
206          * @param channel Channel the parameter is being added to
207          * @param parameter The actual parameter being added
208          */
209         virtual void TellAlreadyOnList(User* source, Channel* channel, std::string& parameter);
210
211         /** Tell the user that the parameter is not in the list.
212          * Overridden by implementing module.
213          * @param source Source user removing the parameter
214          * @param channel Channel the parameter is being removed from
215          * @param parameter The actual parameter being removed
216          */
217         virtual void TellNotSet(User* source, Channel* channel, std::string& parameter);
218 };
219
220 inline ListModeBase::ModeList* ListModeBase::GetList(Channel* channel)
221 {
222         ChanData* cd = extItem.get(channel);
223         if (!cd)
224                 return NULL;
225
226         return &cd->list;
227 }