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 /* Updated to use the <banlist> config tag if it exists
28 * Written by Om <omster@gmail.com>, December 2005.
29 * Based on code previously written by Om - April 2005
30 * Updated to new API July 8th 2006 by Brain
31 * Originally based on m_chanprotect and m_silence
34 /** Get the time as a string
36 inline std::string stringtime()
38 std::ostringstream TIME;
43 /** An item in a listmode's list
45 class ListItem : public classbase
53 /** The number of items a listmode's list may contain
55 class ListLimit : public classbase
62 /** Items stored in the channel's list
64 typedef std::vector<ListItem> modelist;
65 /** Max items per channel by name
67 typedef std::vector<ListLimit> limitlist;
69 /** A request used to check if a user is on a channel's list or not
71 class ListModeRequest : public Request
77 /** Check if a user is on a channel's list.
78 * The Event::Send() event returns true if the user is on the channel's list.
79 * @param sender Sending module
80 * @param target Target module
81 * @param u User to check against
82 * @param c Channel to check against
84 ListModeRequest(Module* sender, Module* target, userrec* u, chanrec* c) : Request(sender, target, "LM_CHECKLIST"), user(u), chan(c)
95 /** The base class for list modes, should be inherited.
97 class ListModeBase : public ModeHandler
103 /** Numeric to use when outputting the list
105 std::string listnumeric;
106 /** Numeric to indicate end of list
108 std::string endoflistnumeric;
109 /** String to send for end of list
111 std::string endofliststring;
112 /** Automatically tidy up entries
115 /** Config tag to check for max items per channel
117 std::string configtag;
118 /** Limits on a per-channel basis read from the tag
119 * specified in ListModeBase::configtag
121 limitlist chanlimits;
125 * @param Instance The creator of this class
126 * @param modechar Mode character
127 * @param eolstr End of list string
128 * @pram lnum List numeric
129 * @param eolnum End of list numeric
130 * @param autotidy Automatically tidy list entries on add
131 * @param ctag Configuration tag to get limits from
133 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
134 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
137 infokey = "listbase_mode_" + std::string(1, mode) + "_list";
142 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
145 channel->GetExt(infokey, el);
148 for (modelist::iterator it = el->begin(); it != el->end(); it++)
150 if(parameter == it->mask)
152 return std::make_pair(true, parameter);
156 return std::make_pair(false, parameter);
159 /** Display the list for this mode
160 * @param user The user to send the list to
161 * @param channel The channel the user is requesting the list for
163 virtual void DisplayList(userrec* user, chanrec* channel)
166 channel->GetExt(infokey, el);
169 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
171 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());
174 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
177 virtual void DisplayEmptyList(userrec* user, chanrec* channel)
179 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
182 /** Remove all instances of the mode from a channel.
184 * @param channel The channel to remove all instances of the mode from
186 virtual void RemoveMode(chanrec* channel)
189 channel->GetExt(infokey, el);
192 irc::modestacker modestack(false);
193 std::deque<std::string> stackresult;
194 const char* mode_junk[MAXMODES+2];
195 mode_junk[0] = channel->name;
196 userrec* n = new userrec(ServerInstance);
197 n->SetFd(FD_MAGIC_NUMBER);
198 for (modelist::iterator it = el->begin(); it != el->end(); it++)
200 modestack.Push(this->GetModeChar(), assign(it->mask));
202 while (modestack.GetStackedLine(stackresult))
204 for (size_t j = 0; j < stackresult.size(); j++)
206 mode_junk[j+1] = stackresult[j].c_str();
208 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, n);
217 virtual void RemoveMode(userrec* user)
219 /* Listmodes dont get set on users */
222 /** Perform a rehash of this mode's configuration data
224 virtual void DoRehash()
226 ConfigReader Conf(ServerInstance);
230 for (int i = 0; i < Conf.Enumerate(configtag); i++)
232 // For each <banlist> tag
234 limit.mask = Conf.ReadValue(configtag, "chan", i);
235 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
237 if (limit.mask.size() && limit.limit > 0)
238 chanlimits.push_back(limit);
240 if (chanlimits.size() == 0)
245 chanlimits.push_back(limit);
249 /** Populate the Implements list with the correct events for a List Mode
251 virtual void DoImplements(char* List)
253 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
256 /** Handle the list mode.
259 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
261 // Try and grab the list
263 channel->GetExt(infokey, el);
267 // If there was no list
272 channel->Extend(infokey, el);
277 ModeParser::CleanMask(parameter);
279 // Check if the item already exists in the list
280 for (modelist::iterator it = el->begin(); it != el->end(); it++)
282 if (parameter == it->mask)
284 /* Give a subclass a chance to error about this */
285 TellAlreadyOnList(source, channel, parameter);
287 // it does, deny the change
288 return MODEACTION_DENY;
292 unsigned int maxsize = 0;
294 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
296 if (match(channel->name, it->mask.c_str()))
298 // We have a pattern matching the channel...
299 maxsize = el->size();
300 if (maxsize < it->limit)
302 /* Ok, it *could* be allowed, now give someone subclassing us
303 * a chance to validate the parameter.
304 * The param is passed by reference, so they can both modify it
305 * and tell us if we allow it or not.
307 * eg, the subclass could:
309 * 2) 'fix' parameter and then allow
312 if (ValidateParam(source, channel, parameter))
314 // And now add the mask onto the list...
316 e.mask = assign(parameter);
317 e.nick = source->nick;
318 e.time = stringtime();
321 return MODEACTION_ALLOW;
325 /* If they deny it they have the job of giving an error message */
326 return MODEACTION_DENY;
332 /* List is full, give subclass a chance to send a custom message */
333 if (!TellListTooLong(source, channel, parameter))
335 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
339 return MODEACTION_DENY;
343 // We're taking the mode off
346 for (modelist::iterator it = el->begin(); it != el->end(); it++)
348 if (parameter == it->mask)
353 channel->Shrink(infokey);
356 return MODEACTION_ALLOW;
359 /* Tried to remove something that wasn't set */
360 TellNotSet(source, channel, parameter);
362 return MODEACTION_DENY;
366 /* Hmm, taking an exception off a non-existant list, DIE */
367 TellNotSet(source, channel, parameter);
369 return MODEACTION_DENY;
372 return MODEACTION_DENY;
375 /** Get Extensible key for this mode
377 virtual std::string& GetInfoKey()
382 /** Handle channel deletion.
384 * @param chan Channel being deleted
386 virtual void DoChannelDelete(chanrec* chan)
389 chan->GetExt(infokey, list);
393 chan->Shrink(infokey);
398 /** Syncronize channel item list with another server.
400 * @param chan Channel to syncronize
401 * @param proto Protocol module pointer
402 * @param opaque Opaque connection handle
404 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
407 chan->GetExt(infokey, list);
408 irc::modestacker modestack(true);
409 std::deque<std::string> stackresult;
412 for (modelist::iterator it = list->begin(); it != list->end(); it++)
414 modestack.Push(std::string(1, mode)[0], assign(it->mask));
417 while (modestack.GetStackedLine(stackresult))
419 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
420 std::string line = mode_join.GetJoined();
421 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
425 /** Clean up module on unload
426 * @param target_type Type of target to clean
427 * @param item Item to clean
429 virtual void DoCleanup(int target_type, void* item)
433 /** Validate parameters.
434 * Overridden by implementing module.
435 * @param source Source user adding the parameter
436 * @param channel Channel the parameter is being added to
437 * @param parameter The actual parameter being added
438 * @return true if the parameter is valid
440 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
445 /** Tell the user the list is too long.
446 * Overridden by implementing module.
447 * @param source Source user adding the parameter
448 * @param channel Channel the parameter is being added to
449 * @param parameter The actual parameter being added
452 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
457 /** Tell the user an item is already on the list.
458 * Overridden by implementing module.
459 * @param source Source user adding the parameter
460 * @param channel Channel the parameter is being added to
461 * @param parameter The actual parameter being added
463 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
467 /** Tell the user that the parameter is not in the list.
468 * Overridden by implementing module.
469 * @param source Source user removing the parameter
470 * @param channel Channel the parameter is being removed from
471 * @param parameter The actual parameter being removed
473 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)