1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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
19 /** Get the time as a string
21 inline std::string stringtime()
23 std::ostringstream TIME;
28 /** An item in a listmode's list
30 class ListItem : public classbase
38 /** The number of items a listmode's list may contain
40 class ListLimit : public classbase
47 /** Items stored in the channel's list
49 typedef std::vector<ListItem> modelist;
50 /** Max items per channel by name
52 typedef std::vector<ListLimit> limitlist;
54 /** A request used to check if a user is on a channel's list or not
56 class ListModeRequest : public Request
62 /** Check if a user is on a channel's list.
63 * The Event::Send() event returns true if the user is on the channel's list.
64 * @param sender Sending module
65 * @param target Target module
66 * @param u User to check against
67 * @param c Channel to check against
69 ListModeRequest(Module* sender, Module* target, User* u, Channel* c) : Request(sender, target, "LM_CHECKLIST"), user(u), chan(c)
80 /** The base class for list modes, should be inherited.
82 class ListModeBase : public ModeHandler
88 /** Numeric to use when outputting the list
90 unsigned int listnumeric;
91 /** Numeric to indicate end of list
93 unsigned int endoflistnumeric;
94 /** String to send for end of list
96 std::string endofliststring;
97 /** Automatically tidy up entries
100 /** Config tag to check for max items per channel
102 std::string configtag;
103 /** Limits on a per-channel basis read from the tag
104 * specified in ListModeBase::configtag
106 limitlist chanlimits;
110 * @param Instance The creator of this class
111 * @param modechar Mode character
112 * @param eolstr End of list string
113 * @pram lnum List numeric
114 * @param eolnum End of list numeric
115 * @param autotidy Automatically tidy list entries on add
116 * @param ctag Configuration tag to get limits from
118 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy, const std::string &ctag = "banlist")
119 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
122 infokey = "listbase_mode_" + std::string(1, mode) + "_list";
127 std::pair<bool,std::string> ModeSet(User*, User*, Channel* channel, const std::string ¶meter)
130 channel->GetExt(infokey, el);
133 for (modelist::iterator it = el->begin(); it != el->end(); it++)
135 if(parameter == it->mask)
137 return std::make_pair(true, parameter);
141 return std::make_pair(false, parameter);
144 /** Display the list for this mode
145 * @param user The user to send the list to
146 * @param channel The channel the user is requesting the list for
148 virtual void DisplayList(User* user, Channel* channel)
151 channel->GetExt(infokey, el);
154 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
156 user->WriteNumeric(listnumeric, "%s %s %s %s %s", user->nick.c_str(), channel->name.c_str(), it->mask.c_str(), it->nick.c_str(), it->time.c_str());
159 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
162 virtual void DisplayEmptyList(User* user, Channel* channel)
164 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
167 /** Remove all instances of the mode from a channel.
169 * @param channel The channel to remove all instances of the mode from
171 virtual void RemoveMode(Channel* channel, irc::modestacker* stack)
174 channel->GetExt(infokey, el);
177 irc::modestacker modestack(ServerInstance, false);
178 std::deque<std::string> stackresult;
179 std::vector<std::string> mode_junk;
180 mode_junk.push_back(channel->name);
182 for (modelist::iterator it = el->begin(); it != el->end(); it++)
185 stack->Push(this->GetModeChar(), it->mask);
187 modestack.Push(this->GetModeChar(), it->mask);
193 while (modestack.GetStackedLine(stackresult))
195 for (size_t j = 0; j < stackresult.size(); j++)
197 mode_junk.push_back(stackresult[j]);
200 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
207 virtual void RemoveMode(User*, irc::modestacker* stack)
209 /* Listmodes dont get set on users */
212 /** Perform a rehash of this mode's configuration data
214 virtual void DoRehash()
216 ConfigReader Conf(ServerInstance);
220 for (int i = 0; i < Conf.Enumerate(configtag); i++)
222 // For each <banlist> tag
224 limit.mask = Conf.ReadValue(configtag, "chan", i);
225 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
227 if (limit.mask.size() && limit.limit > 0)
228 chanlimits.push_back(limit);
230 if (chanlimits.size() == 0)
235 chanlimits.push_back(limit);
239 /** Populate the Implements list with the correct events for a List Mode
241 virtual void DoImplements(Module* m)
243 Implementation eventlist[] = { I_OnChannelDelete, I_OnSyncChannel, I_OnCleanup, I_OnRehash };
244 ServerInstance->Modules->Attach(eventlist, m, 4);
247 /** Handle the list mode.
250 virtual ModeAction OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding, bool servermode)
252 // Try and grab the list
254 channel->GetExt(infokey, el);
258 // If there was no list
263 channel->Extend(infokey, el);
268 ModeParser::CleanMask(parameter);
270 // Check if the item already exists in the list
271 for (modelist::iterator it = el->begin(); it != el->end(); it++)
273 if (parameter == it->mask)
275 /* Give a subclass a chance to error about this */
276 TellAlreadyOnList(source, channel, parameter);
278 // it does, deny the change
279 return MODEACTION_DENY;
283 unsigned int maxsize = 0;
285 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
287 if (match(channel->name, it->mask))
289 // We have a pattern matching the channel...
290 maxsize = el->size();
291 if (IS_LOCAL(source) || (maxsize < it->limit))
293 /* Ok, it *could* be allowed, now give someone subclassing us
294 * a chance to validate the parameter.
295 * The param is passed by reference, so they can both modify it
296 * and tell us if we allow it or not.
298 * eg, the subclass could:
300 * 2) 'fix' parameter and then allow
303 if (ValidateParam(source, channel, parameter))
305 // And now add the mask onto the list...
308 e.nick = servermode ? ServerInstance->Config->ServerName : source->nick;
309 e.time = stringtime();
312 return MODEACTION_ALLOW;
316 /* If they deny it they have the job of giving an error message */
317 return MODEACTION_DENY;
323 /* List is full, give subclass a chance to send a custom message */
324 if (!TellListTooLong(source, channel, parameter))
326 source->WriteNumeric(478, "%s %s %s :Channel ban/ignore list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
330 return MODEACTION_DENY;
334 // We're taking the mode off
337 for (modelist::iterator it = el->begin(); it != el->end(); it++)
339 if (parameter == it->mask)
344 channel->Shrink(infokey);
347 return MODEACTION_ALLOW;
350 /* Tried to remove something that wasn't set */
351 TellNotSet(source, channel, parameter);
353 return MODEACTION_DENY;
357 /* Hmm, taking an exception off a non-existant list, DIE */
358 TellNotSet(source, channel, parameter);
360 return MODEACTION_DENY;
363 return MODEACTION_DENY;
366 /** Get Extensible key for this mode
368 virtual std::string& GetInfoKey()
373 /** Handle channel deletion.
375 * @param chan Channel being deleted
377 virtual void DoChannelDelete(Channel* chan)
380 chan->GetExt(infokey, mlist);
384 chan->Shrink(infokey);
389 /** Syncronize channel item list with another server.
391 * @param chan Channel to syncronize
392 * @param proto Protocol module pointer
393 * @param opaque Opaque connection handle
395 virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
398 chan->GetExt(infokey, mlist);
399 irc::modestacker modestack(ServerInstance, true);
400 std::deque<std::string> stackresult;
403 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
405 modestack.Push(std::string(1, mode)[0], it->mask);
408 while (modestack.GetStackedLine(stackresult))
410 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
411 std::string line = mode_join.GetJoined();
412 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
416 /** Clean up module on unload
417 * @param target_type Type of target to clean
418 * @param item Item to clean
420 virtual void DoCleanup(int, void*)
424 /** Validate parameters.
425 * Overridden by implementing module.
426 * @param source Source user adding the parameter
427 * @param channel Channel the parameter is being added to
428 * @param parameter The actual parameter being added
429 * @return true if the parameter is valid
431 virtual bool ValidateParam(User*, Channel*, std::string&)
436 /** Tell the user the list is too long.
437 * Overridden by implementing module.
438 * @param source Source user adding the parameter
439 * @param channel Channel the parameter is being added to
440 * @param parameter The actual parameter being added
443 virtual bool TellListTooLong(User*, Channel*, std::string&)
448 /** Tell the user an item is already on the list.
449 * Overridden by implementing module.
450 * @param source Source user adding the parameter
451 * @param channel Channel the parameter is being added to
452 * @param parameter The actual parameter being added
454 virtual void TellAlreadyOnList(User*, Channel*, std::string&)
458 /** Tell the user that the parameter is not in the list.
459 * Overridden by implementing module.
460 * @param source Source user removing the parameter
461 * @param channel Channel the parameter is being removed from
462 * @param parameter The actual parameter being removed
464 virtual void TellNotSet(User*, Channel*, std::string&)