1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef INSPIRCD_LISTMODE_PROVIDER
15 #define INSPIRCD_LISTMODE_PROVIDER
27 /** Get the time as a string
29 inline std::string stringtime()
31 std::ostringstream TIME;
36 /** An item in a listmode's list
38 class ListItem : public classbase
46 /** The number of items a listmode's list may contain
48 class ListLimit : public classbase
55 /** Items stored in the channel's list
57 typedef std::vector<ListItem> modelist;
58 /** Max items per channel by name
60 typedef std::vector<ListLimit> limitlist;
62 /** A request used to check if a user is on a channel's list or not
64 class ListModeRequest : public Request
70 /** Check if a user is on a channel's list.
71 * The Event::Send() event returns true if the user is on the channel's list.
72 * @param sender Sending module
73 * @param target Target module
74 * @param u User to check against
75 * @param c Channel to check against
77 ListModeRequest(Module* sender, Module* target, userrec* u, chanrec* c) : Request(sender, target, "LM_CHECKLIST"), user(u), chan(c)
88 /** The base class for list modes, should be inherited.
90 class ListModeBase : public ModeHandler
96 /** Numeric to use when outputting the list
98 std::string listnumeric;
99 /** Numeric to indicate end of list
101 std::string endoflistnumeric;
102 /** String to send for end of list
104 std::string endofliststring;
105 /** Automatically tidy up entries
108 /** Config tag to check for max items per channel
110 std::string configtag;
111 /** Limits on a per-channel basis read from the tag
112 * specified in ListModeBase::configtag
114 limitlist chanlimits;
118 * @param Instance The creator of this class
119 * @param modechar Mode character
120 * @param eolstr End of list string
121 * @pram lnum List numeric
122 * @param eolnum End of list numeric
123 * @param autotidy Automatically tidy list entries on add
124 * @param ctag Configuration tag to get limits from
126 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
127 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
130 infokey = "listbase_mode_" + std::string(1, mode) + "_list";
135 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
138 channel->GetExt(infokey, el);
141 for (modelist::iterator it = el->begin(); it != el->end(); it++)
143 if(parameter == it->mask)
145 return std::make_pair(true, parameter);
149 return std::make_pair(false, parameter);
152 /** Display the list for this mode
153 * @param user The user to send the list to
154 * @param channel The channel the user is requesting the list for
156 virtual void DisplayList(userrec* user, chanrec* channel)
159 channel->GetExt(infokey, el);
162 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
164 user->WriteServ("%s %s %s %s %s %s", listnumeric.c_str(), user->nick, channel->name, it->mask.c_str(), it->nick.c_str(), it->time.c_str());
167 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
170 virtual void DisplayEmptyList(userrec* user, chanrec* channel)
172 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
175 /** Remove all instances of the mode from a channel.
177 * @param channel The channel to remove all instances of the mode from
179 virtual void RemoveMode(chanrec* channel)
182 channel->GetExt(infokey, el);
185 irc::modestacker modestack(false);
186 std::deque<std::string> stackresult;
187 const char* mode_junk[MAXMODES+2];
188 mode_junk[0] = channel->name;
189 userrec* n = new userrec(ServerInstance);
190 n->SetFd(FD_MAGIC_NUMBER);
191 for (modelist::iterator it = el->begin(); it != el->end(); it++)
193 modestack.Push(this->GetModeChar(), assign(it->mask));
195 while (modestack.GetStackedLine(stackresult))
197 for (size_t j = 0; j < stackresult.size(); j++)
199 mode_junk[j+1] = stackresult[j].c_str();
201 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, n);
210 virtual void RemoveMode(userrec* user)
212 /* Listmodes dont get set on users */
215 /** Perform a rehash of this mode's configuration data
217 virtual void DoRehash()
219 ConfigReader Conf(ServerInstance);
223 for (int i = 0; i < Conf.Enumerate(configtag); i++)
225 // For each <banlist> tag
227 limit.mask = Conf.ReadValue(configtag, "chan", i);
228 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
230 if (limit.mask.size() && limit.limit > 0)
231 chanlimits.push_back(limit);
233 if (chanlimits.size() == 0)
238 chanlimits.push_back(limit);
242 /** Populate the Implements list with the correct events for a List Mode
244 virtual void DoImplements(char* List)
246 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
249 /** Handle the list mode.
252 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
254 // Try and grab the list
256 channel->GetExt(infokey, el);
260 // If there was no list
265 channel->Extend(infokey, el);
270 ModeParser::CleanMask(parameter);
272 // Check if the item already exists in the list
273 for (modelist::iterator it = el->begin(); it != el->end(); it++)
275 if (parameter == it->mask)
277 /* Give a subclass a chance to error about this */
278 TellAlreadyOnList(source, channel, parameter);
280 // it does, deny the change
281 return MODEACTION_DENY;
285 unsigned int maxsize = 0;
287 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
289 if (match(channel->name, it->mask.c_str()))
291 // We have a pattern matching the channel...
292 maxsize = el->size();
293 if (maxsize < it->limit)
295 /* Ok, it *could* be allowed, now give someone subclassing us
296 * a chance to validate the parameter.
297 * The param is passed by reference, so they can both modify it
298 * and tell us if we allow it or not.
300 * eg, the subclass could:
302 * 2) 'fix' parameter and then allow
305 if (ValidateParam(source, channel, parameter))
307 // And now add the mask onto the list...
309 e.mask = assign(parameter);
310 e.nick = source->nick;
311 e.time = stringtime();
314 return MODEACTION_ALLOW;
318 /* If they deny it they have the job of giving an error message */
319 return MODEACTION_DENY;
325 /* List is full, give subclass a chance to send a custom message */
326 if (!TellListTooLong(source, channel, parameter))
328 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
332 return MODEACTION_DENY;
336 // We're taking the mode off
339 for (modelist::iterator it = el->begin(); it != el->end(); it++)
341 if (parameter == it->mask)
346 channel->Shrink(infokey);
349 return MODEACTION_ALLOW;
352 /* Tried to remove something that wasn't set */
353 TellNotSet(source, channel, parameter);
355 return MODEACTION_DENY;
359 /* Hmm, taking an exception off a non-existant list, DIE */
360 TellNotSet(source, channel, parameter);
362 return MODEACTION_DENY;
365 return MODEACTION_DENY;
368 /** Get Extensible key for this mode
370 virtual std::string& GetInfoKey()
375 /** Handle channel deletion.
377 * @param chan Channel being deleted
379 virtual void DoChannelDelete(chanrec* chan)
382 chan->GetExt(infokey, list);
386 chan->Shrink(infokey);
391 /** Syncronize channel item list with another server.
393 * @param chan Channel to syncronize
394 * @param proto Protocol module pointer
395 * @param opaque Opaque connection handle
397 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
400 chan->GetExt(infokey, list);
401 irc::modestacker modestack(true);
402 std::deque<std::string> stackresult;
405 for (modelist::iterator it = list->begin(); it != list->end(); it++)
407 modestack.Push(std::string(1, mode)[0], assign(it->mask));
410 while (modestack.GetStackedLine(stackresult))
412 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
413 std::string line = mode_join.GetJoined();
414 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
418 /** Clean up module on unload
419 * @param target_type Type of target to clean
420 * @param item Item to clean
422 virtual void DoCleanup(int target_type, void* item)
426 /** Validate parameters.
427 * Overridden by implementing module.
428 * @param source Source user adding the parameter
429 * @param channel Channel the parameter is being added to
430 * @param parameter The actual parameter being added
431 * @return true if the parameter is valid
433 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
438 /** Tell the user the list is too long.
439 * Overridden by implementing module.
440 * @param source Source user adding the parameter
441 * @param channel Channel the parameter is being added to
442 * @param parameter The actual parameter being added
445 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
450 /** Tell the user an item is already on the list.
451 * Overridden by implementing module.
452 * @param source Source user adding the parameter
453 * @param channel Channel the parameter is being added to
454 * @param parameter The actual parameter being added
456 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
460 /** Tell the user that the parameter is not in the list.
461 * Overridden by implementing module.
462 * @param source Source user removing the parameter
463 * @param channel Channel the parameter is being removed from
464 * @param parameter The actual parameter being removed
466 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)