]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/u_listmode.h
0f5903e53acc3543ffc185d2a247146254ade24d
[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                 if (chanlimits.empty())
205                 {
206                         ListLimit limit;
207                         limit.mask = "*";
208                         limit.limit = 64;
209                         chanlimits.push_back(limit);
210                 }
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                                 }
298                         }
299
300                         /* List is full, give subclass a chance to send a custom message */
301                         if (!TellListTooLong(source, channel, parameter))
302                         {
303                                 source->WriteNumeric(478, "%s %s %s :Channel ban/ignore list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
304                         }
305
306                         parameter.clear();
307                         return MODEACTION_DENY;
308                 }
309                 else
310                 {
311                         // We're taking the mode off
312                         if (el)
313                         {
314                                 for (modelist::iterator it = el->begin(); it != el->end(); it++)
315                                 {
316                                         if (parameter == it->mask)
317                                         {
318                                                 el->erase(it);
319                                                 if (el->empty())
320                                                 {
321                                                         extItem.unset(channel);
322                                                 }
323                                                 return MODEACTION_ALLOW;
324                                         }
325                                 }
326                                 /* Tried to remove something that wasn't set */
327                                 TellNotSet(source, channel, parameter);
328                                 parameter.clear();
329                                 return MODEACTION_DENY;
330                         }
331                         else
332                         {
333                                 /* Hmm, taking an exception off a non-existant list, DIE */
334                                 TellNotSet(source, channel, parameter);
335                                 parameter.clear();
336                                 return MODEACTION_DENY;
337                         }
338                 }
339                 return MODEACTION_DENY;
340         }
341
342         /** Syncronize channel item list with another server.
343          * See modules.h
344          * @param chan Channel to syncronize
345          * @param proto Protocol module pointer
346          * @param opaque Opaque connection handle
347          */
348         virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
349         {
350                 modelist* mlist = extItem.get(chan);
351                 irc::modestacker modestack(true);
352                 std::vector<std::string> stackresult;
353                 std::vector<TranslateType> types;
354                 types.push_back(TR_TEXT);
355                 if (mlist)
356                 {
357                         for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
358                         {
359                                 modestack.Push(std::string(1, mode)[0], it->mask);
360                         }
361                 }
362                 while (modestack.GetStackedLine(stackresult))
363                 {
364                         types.assign(stackresult.size(), this->GetTranslateType());
365                         proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
366                         stackresult.clear();
367                 }
368         }
369
370         /** Clean up module on unload
371          * @param target_type Type of target to clean
372          * @param item Item to clean
373          */
374         virtual void DoCleanup(int, void*)
375         {
376         }
377
378         /** Validate parameters.
379          * Overridden by implementing module.
380          * @param source Source user adding the parameter
381          * @param channel Channel the parameter is being added to
382          * @param parameter The actual parameter being added
383          * @return true if the parameter is valid
384          */
385         virtual bool ValidateParam(User*, Channel*, std::string&)
386         {
387                 return true;
388         }
389
390         /** Tell the user the list is too long.
391          * Overridden by implementing module.
392          * @param source Source user adding the parameter
393          * @param channel Channel the parameter is being added to
394          * @param parameter The actual parameter being added
395          * @return Ignored
396          */
397         virtual bool TellListTooLong(User*, Channel*, std::string&)
398         {
399                 return false;
400         }
401
402         /** Tell the user an item is already on the list.
403          * Overridden by implementing module.
404          * @param source Source user adding the parameter
405          * @param channel Channel the parameter is being added to
406          * @param parameter The actual parameter being added
407          */
408         virtual void TellAlreadyOnList(User*, Channel*, std::string&)
409         {
410         }
411
412         /** Tell the user that the parameter is not in the list.
413          * Overridden by implementing module.
414          * @param source Source user removing the parameter
415          * @param channel Channel the parameter is being removed from
416          * @param parameter The actual parameter being removed
417          */
418         virtual void TellNotSet(User*, Channel*, std::string&)
419         {
420         }
421 };
422
423 #endif