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 /** Remove all instances of the mode from a channel.
179 * @param channel The channel to remove all instances of the mode from
181 virtual void RemoveMode(chanrec* channel)
184 channel->GetExt(infokey, el);
187 irc::modestacker modestack(false);
188 std::deque<std::string> stackresult;
189 const char* mode_junk[MAXMODES+2];
190 mode_junk[0] = channel->name;
191 userrec* n = new userrec(ServerInstance);
192 n->SetFd(FD_MAGIC_NUMBER);
193 for (modelist::iterator it = el->begin(); it != el->end(); it++)
195 modestack.Push(this->GetModeChar(), assign(it->mask));
197 while (modestack.GetStackedLine(stackresult))
199 for (size_t j = 0; j < stackresult.size(); j++)
201 mode_junk[j+1] = stackresult[j].c_str();
203 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, n);
212 virtual void RemoveMode(userrec* user)
214 /* Listmodes dont get set on users */
217 /** Perform a rehash of this mode's configuration data
219 virtual void DoRehash()
221 ConfigReader Conf(ServerInstance);
225 for (int i = 0; i < Conf.Enumerate(configtag); i++)
227 // For each <banlist> tag
229 limit.mask = Conf.ReadValue(configtag, "chan", i);
230 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
232 if (limit.mask.size() && limit.limit > 0)
233 chanlimits.push_back(limit);
235 if (chanlimits.size() == 0)
240 chanlimits.push_back(limit);
244 /** Populate the Implements list with the correct events for a List Mode
246 virtual void DoImplements(char* List)
248 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
251 /** Handle the list mode.
254 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
256 // Try and grab the list
258 channel->GetExt(infokey, el);
262 // If there was no list
267 channel->Extend(infokey, el);
272 ModeParser::CleanMask(parameter);
274 // Check if the item already exists in the list
275 for (modelist::iterator it = el->begin(); it != el->end(); it++)
277 if (parameter == it->mask)
279 /* Give a subclass a chance to error about this */
280 TellAlreadyOnList(source, channel, parameter);
282 // it does, deny the change
283 return MODEACTION_DENY;
287 unsigned int maxsize = 0;
289 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
291 if (match(channel->name, it->mask.c_str()))
293 // We have a pattern matching the channel...
294 maxsize = el->size();
295 if (maxsize < it->limit)
297 /* Ok, it *could* be allowed, now give someone subclassing us
298 * a chance to validate the parameter.
299 * The param is passed by reference, so they can both modify it
300 * and tell us if we allow it or not.
302 * eg, the subclass could:
304 * 2) 'fix' parameter and then allow
307 if (ValidateParam(source, channel, parameter))
309 // And now add the mask onto the list...
311 e.mask = assign(parameter);
312 e.nick = source->nick;
313 e.time = stringtime();
316 return MODEACTION_ALLOW;
320 /* If they deny it they have the job of giving an error message */
321 return MODEACTION_DENY;
327 /* List is full, give subclass a chance to send a custom message */
328 if (!TellListTooLong(source, channel, parameter))
330 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
334 return MODEACTION_DENY;
338 // We're taking the mode off
341 for (modelist::iterator it = el->begin(); it != el->end(); it++)
343 if (parameter == it->mask)
348 channel->Shrink(infokey);
351 return MODEACTION_ALLOW;
354 /* Tried to remove something that wasn't set */
355 TellNotSet(source, channel, parameter);
357 return MODEACTION_DENY;
361 /* Hmm, taking an exception off a non-existant list, DIE */
362 TellNotSet(source, channel, parameter);
364 return MODEACTION_DENY;
367 return MODEACTION_DENY;
370 /** Get Extensible key for this mode
372 virtual std::string& GetInfoKey()
377 /** Handle channel deletion.
379 * @param chan Channel being deleted
381 virtual void DoChannelDelete(chanrec* chan)
384 chan->GetExt(infokey, list);
388 chan->Shrink(infokey);
393 /** Syncronize channel item list with another server.
395 * @param chan Channel to syncronize
396 * @param proto Protocol module pointer
397 * @param opaque Opaque connection handle
399 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
402 chan->GetExt(infokey, list);
403 irc::modestacker modestack(true);
404 std::deque<std::string> stackresult;
407 for (modelist::iterator it = list->begin(); it != list->end(); it++)
409 modestack.Push(std::string(1, mode)[0], assign(it->mask));
412 while (modestack.GetStackedLine(stackresult))
414 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
415 std::string line = mode_join.GetJoined();
416 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
420 /** Clean up module on unload
421 * @param target_type Type of target to clean
422 * @param item Item to clean
424 virtual void DoCleanup(int target_type, void* item)
428 /** Validate parameters.
429 * Overridden by implementing module.
430 * @param source Source user adding the parameter
431 * @param channel Channel the parameter is being added to
432 * @param parameter The actual parameter being added
433 * @return true if the parameter is valid
435 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
440 /** Tell the user the list is too long.
441 * Overridden by implementing module.
442 * @param source Source user adding the parameter
443 * @param channel Channel the parameter is being added to
444 * @param parameter The actual parameter being added
447 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
452 /** Tell the user an item is already on the list.
453 * Overridden by implementing module.
454 * @param source Source user adding the parameter
455 * @param channel Channel the parameter is being added to
456 * @param parameter The actual parameter being added
458 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
462 /** Tell the user that the parameter is not in the list.
463 * Overridden by implementing module.
464 * @param source Source user removing the parameter
465 * @param channel Channel the parameter is being removed from
466 * @param parameter The actual parameter being removed
468 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)