]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/u_listmode.h
Merge pull request #1136 from Adam-/insp20+dccallow
[user/henk/code/inspircd.git] / src / modules / u_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 #ifndef INSPIRCD_LISTMODE_PROVIDER
21 #define INSPIRCD_LISTMODE_PROVIDER
22
23 /** Get the time as a string
24  */
25 inline std::string stringtime()
26 {
27         std::ostringstream TIME;
28         TIME << ServerInstance->Time();
29         return TIME.str();
30 }
31
32 /** An item in a listmode's list
33  */
34 class ListItem
35 {
36 public:
37         std::string nick;
38         std::string mask;
39         std::string time;
40 };
41
42 /** The number of items a listmode's list may contain
43  */
44 class ListLimit
45 {
46 public:
47         std::string mask;
48         unsigned int limit;
49 };
50
51 /** Items stored in the channel's list
52  */
53 typedef std::list<ListItem> modelist;
54 /** Max items per channel by name
55  */
56 typedef std::list<ListLimit> limitlist;
57
58 /** The base class for list modes, should be inherited.
59  */
60 class ListModeBase : public ModeHandler
61 {
62  protected:
63         /** Numeric to use when outputting the list
64          */
65         unsigned int listnumeric;
66         /** Numeric to indicate end of list
67          */
68         unsigned int endoflistnumeric;
69         /** String to send for end of list
70          */
71         std::string endofliststring;
72         /** Automatically tidy up entries
73          */
74         bool tidy;
75         /** Config tag to check for max items per channel
76          */
77         std::string configtag;
78         /** Limits on a per-channel basis read from the tag
79          * specified in ListModeBase::configtag
80          */
81         limitlist chanlimits;
82
83  public:
84         /** Storage key
85          */
86         SimpleExtItem<modelist> extItem;
87
88         /** Constructor.
89          * @param Instance The creator of this class
90          * @param modechar Mode character
91          * @param eolstr End of list string
92          * @pram lnum List numeric
93          * @param eolnum End of list numeric
94          * @param autotidy Automatically tidy list entries on add
95          * @param ctag Configuration tag to get limits from
96          */
97         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")
98                 : ModeHandler(Creator, Name, modechar, PARAM_ALWAYS, MODETYPE_CHANNEL), 
99                 listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy),
100                 configtag(ctag), extItem("listbase_mode_" + name + "_list", Creator)
101         {
102                 list = true;
103         }
104
105         /** See mode.h
106          */
107         std::pair<bool,std::string> ModeSet(User*, User*, Channel* channel, const std::string &parameter)
108         {
109                 modelist* el = extItem.get(channel);
110                 if (el)
111                 {
112                         for (modelist::iterator it = el->begin(); it != el->end(); it++)
113                         {
114                                 if(parameter == it->mask)
115                                 {
116                                         return std::make_pair(true, parameter);
117                                 }
118                         }
119                 }
120                 return std::make_pair(false, parameter);
121         }
122
123         /** Display the list for this mode
124          * @param user The user to send the list to
125          * @param channel The channel the user is requesting the list for
126          */
127         virtual void DisplayList(User* user, Channel* channel)
128         {
129                 modelist* el = extItem.get(channel);
130                 if (el)
131                 {
132                         for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
133                         {
134                                 user->WriteNumeric(listnumeric, "%s %s %s %s %s", user->nick.c_str(), channel->name.c_str(), it->mask.c_str(), (it->nick.length() ? it->nick.c_str() : ServerInstance->Config->ServerName.c_str()), it->time.c_str());
135                         }
136                 }
137                 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
138         }
139
140         virtual void DisplayEmptyList(User* user, Channel* channel)
141         {
142                 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
143         }
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                 modelist* el = extItem.get(channel);
152                 if (el)
153                 {
154                         irc::modestacker modestack(false);
155
156                         for (modelist::iterator it = el->begin(); it != el->end(); it++)
157                         {
158                                 if (stack)
159                                         stack->Push(this->GetModeChar(), it->mask);
160                                 else
161                                         modestack.Push(this->GetModeChar(), it->mask);
162                         }
163
164                         if (stack)
165                                 return;
166
167                         std::vector<std::string> stackresult;
168                         stackresult.push_back(channel->name);
169                         while (modestack.GetStackedLine(stackresult))
170                         {
171                                 ServerInstance->SendMode(stackresult, ServerInstance->FakeClient);
172                                 stackresult.clear();
173                                 stackresult.push_back(channel->name);
174                         }
175                 }
176         }
177
178         /** See mode.h
179          */
180         virtual void RemoveMode(User*, irc::modestacker* stack)
181         {
182                 /* Listmodes dont get set on users */
183         }
184
185         /** Perform a rehash of this mode's configuration data
186          */
187         virtual void DoRehash()
188         {
189                 ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
190
191                 chanlimits.clear();
192
193                 for (ConfigIter i = tags.first; i != tags.second; i++)
194                 {
195                         // For each <banlist> tag
196                         ConfigTag* c = i->second;
197                         ListLimit limit;
198                         limit.mask = c->getString("chan");
199                         limit.limit = c->getInt("limit");
200
201                         if (limit.mask.size() && limit.limit > 0)
202                                 chanlimits.push_back(limit);
203                 }
204
205                 // Add the default entry. This is inserted last so if the user specifies a
206                 // wildcard record in the config it will take precedence over this entry.
207                 ListLimit limit;
208                 limit.mask = "*";
209                 limit.limit = 64;
210                 chanlimits.push_back(limit);
211         }
212
213         /** Populate the Implements list with the correct events for a List Mode
214          */
215         virtual void DoImplements(Module* m)
216         {
217                 ServerInstance->Modules->AddService(extItem);
218                 this->DoRehash();
219                 Implementation eventlist[] = { I_OnSyncChannel, I_OnRehash };
220                 ServerInstance->Modules->Attach(eventlist, m, sizeof(eventlist)/sizeof(Implementation));
221         }
222
223         /** Handle the list mode.
224          * See mode.h
225          */
226         virtual ModeAction OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
227         {
228                 // Try and grab the list
229                 modelist* el = extItem.get(channel);
230
231                 if (adding)
232                 {
233                         if (tidy)
234                                 ModeParser::CleanMask(parameter);
235
236                         if (parameter.length() > 250)
237                                 return MODEACTION_DENY;
238
239                         // If there was no list
240                         if (!el)
241                         {
242                                 // Make one
243                                 el = new modelist;
244                                 extItem.set(channel, el);
245                         }
246
247                         // Check if the item already exists in the list
248                         for (modelist::iterator it = el->begin(); it != el->end(); it++)
249                         {
250                                 if (parameter == it->mask)
251                                 {
252                                         /* Give a subclass a chance to error about this */
253                                         TellAlreadyOnList(source, channel, parameter);
254
255                                         // it does, deny the change
256                                         return MODEACTION_DENY;
257                                 }
258                         }
259
260                         unsigned int maxsize = 0;
261
262                         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
263                         {
264                                 if (InspIRCd::Match(channel->name, it->mask))
265                                 {
266                                         // We have a pattern matching the channel...
267                                         maxsize = el->size();
268                                         if (!IS_LOCAL(source) || (maxsize < it->limit))
269                                         {
270                                                 /* Ok, it *could* be allowed, now give someone subclassing us
271                                                  * a chance to validate the parameter.
272                                                  * The param is passed by reference, so they can both modify it
273                                                  * and tell us if we allow it or not.
274                                                  *
275                                                  * eg, the subclass could:
276                                                  * 1) allow
277                                                  * 2) 'fix' parameter and then allow
278                                                  * 3) deny
279                                                  */
280                                                 if (ValidateParam(source, channel, parameter))
281                                                 {
282                                                         // And now add the mask onto the list...
283                                                         ListItem e;
284                                                         e.mask = parameter;
285                                                         e.nick = source->nick;
286                                                         e.time = stringtime();
287
288                                                         el->push_back(e);
289                                                         return MODEACTION_ALLOW;
290                                                 }
291                                                 else
292                                                 {
293                                                         /* If they deny it they have the job of giving an error message */
294                                                         return MODEACTION_DENY;
295                                                 }
296                                         }
297                                         else
298                                                 break;
299                                 }
300                         }
301
302                         /* List is full, give subclass a chance to send a custom message */
303                         if (!TellListTooLong(source, channel, parameter))
304                         {
305                                 source->WriteNumeric(478, "%s %s %s :Channel ban/ignore list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
306                         }
307
308                         parameter.clear();
309                         return MODEACTION_DENY;
310                 }
311                 else
312                 {
313                         // We're taking the mode off
314                         if (el)
315                         {
316                                 for (modelist::iterator it = el->begin(); it != el->end(); it++)
317                                 {
318                                         if (parameter == it->mask)
319                                         {
320                                                 el->erase(it);
321                                                 if (el->empty())
322                                                 {
323                                                         extItem.unset(channel);
324                                                 }
325                                                 return MODEACTION_ALLOW;
326                                         }
327                                 }
328                                 /* Tried to remove something that wasn't set */
329                                 TellNotSet(source, channel, parameter);
330                                 parameter.clear();
331                                 return MODEACTION_DENY;
332                         }
333                         else
334                         {
335                                 /* Hmm, taking an exception off a non-existant list, DIE */
336                                 TellNotSet(source, channel, parameter);
337                                 parameter.clear();
338                                 return MODEACTION_DENY;
339                         }
340                 }
341                 return MODEACTION_DENY;
342         }
343
344         /** Syncronize channel item list with another server.
345          * See modules.h
346          * @param chan Channel to syncronize
347          * @param proto Protocol module pointer
348          * @param opaque Opaque connection handle
349          */
350         virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
351         {
352                 modelist* mlist = extItem.get(chan);
353                 irc::modestacker modestack(true);
354                 std::vector<std::string> stackresult;
355                 std::vector<TranslateType> types;
356                 types.push_back(TR_TEXT);
357                 if (mlist)
358                 {
359                         for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
360                         {
361                                 modestack.Push(std::string(1, mode)[0], it->mask);
362                         }
363                 }
364                 while (modestack.GetStackedLine(stackresult))
365                 {
366                         types.assign(stackresult.size(), this->GetTranslateType());
367                         proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
368                         stackresult.clear();
369                 }
370         }
371
372         /** Clean up module on unload
373          * @param target_type Type of target to clean
374          * @param item Item to clean
375          */
376         virtual void DoCleanup(int, void*)
377         {
378         }
379
380         /** Validate parameters.
381          * Overridden by implementing module.
382          * @param source Source user adding the parameter
383          * @param channel Channel the parameter is being added to
384          * @param parameter The actual parameter being added
385          * @return true if the parameter is valid
386          */
387         virtual bool ValidateParam(User*, Channel*, std::string&)
388         {
389                 return true;
390         }
391
392         /** Tell the user the list is too long.
393          * Overridden by implementing module.
394          * @param source Source user adding the parameter
395          * @param channel Channel the parameter is being added to
396          * @param parameter The actual parameter being added
397          * @return Ignored
398          */
399         virtual bool TellListTooLong(User*, Channel*, std::string&)
400         {
401                 return false;
402         }
403
404         /** Tell the user an item is already on the list.
405          * Overridden by implementing module.
406          * @param source Source user adding the parameter
407          * @param channel Channel the parameter is being added to
408          * @param parameter The actual parameter being added
409          */
410         virtual void TellAlreadyOnList(User*, Channel*, std::string&)
411         {
412         }
413
414         /** Tell the user that the parameter is not in the list.
415          * Overridden by implementing module.
416          * @param source Source user removing the parameter
417          * @param channel Channel the parameter is being removed from
418          * @param parameter The actual parameter being removed
419          */
420         virtual void TellNotSet(User*, Channel*, std::string&)
421         {
422         }
423 };
424
425 #endif