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
17 /** Get the time as a string
19 inline std::string stringtime()
21 std::ostringstream TIME;
26 /** An item in a listmode's list
28 class ListItem : public classbase
36 /** The number of items a listmode's list may contain
38 class ListLimit : public classbase
45 /** Items stored in the channel's list
47 typedef std::vector<ListItem> modelist;
48 /** Max items per channel by name
50 typedef std::vector<ListLimit> limitlist;
52 /** A request used to check if a user is on a channel's list or not
54 class ListModeRequest : public Request
60 /** Check if a user is on a channel's list.
61 * The Event::Send() event returns true if the user is on the channel's list.
62 * @param sender Sending module
63 * @param target Target module
64 * @param u User to check against
65 * @param c Channel to check against
67 ListModeRequest(Module* sender, Module* target, User* u, Channel* c) : Request(sender, target, "LM_CHECKLIST"), user(u), chan(c)
78 /** The base class for list modes, should be inherited.
80 class ListModeBase : public ModeHandler
86 /** Numeric to use when outputting the list
88 unsigned int listnumeric;
89 /** Numeric to indicate end of list
91 unsigned int endoflistnumeric;
92 /** String to send for end of list
94 std::string endofliststring;
95 /** Automatically tidy up entries
98 /** Config tag to check for max items per channel
100 std::string configtag;
101 /** Limits on a per-channel basis read from the tag
102 * specified in ListModeBase::configtag
104 limitlist chanlimits;
108 * @param Instance The creator of this class
109 * @param modechar Mode character
110 * @param eolstr End of list string
111 * @pram lnum List numeric
112 * @param eolnum End of list numeric
113 * @param autotidy Automatically tidy list entries on add
114 * @param ctag Configuration tag to get limits from
116 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy, const std::string &ctag = "banlist")
117 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
120 infokey = "listbase_mode_" + std::string(1, mode) + "_list";
125 std::pair<bool,std::string> ModeSet(User*, User*, Channel* channel, const std::string ¶meter)
128 channel->GetExt(infokey, el);
131 for (modelist::iterator it = el->begin(); it != el->end(); it++)
133 if(parameter == it->mask)
135 return std::make_pair(true, parameter);
139 return std::make_pair(false, parameter);
142 /** Display the list for this mode
143 * @param user The user to send the list to
144 * @param channel The channel the user is requesting the list for
146 virtual void DisplayList(User* user, Channel* channel)
149 channel->GetExt(infokey, el);
152 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
154 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());
157 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
160 virtual void DisplayEmptyList(User* user, Channel* channel)
162 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
165 /** Remove all instances of the mode from a channel.
167 * @param channel The channel to remove all instances of the mode from
169 virtual void RemoveMode(Channel* channel, irc::modestacker* stack)
172 channel->GetExt(infokey, el);
175 irc::modestacker modestack(ServerInstance, false);
176 std::deque<std::string> stackresult;
177 std::vector<std::string> mode_junk;
178 mode_junk.push_back(channel->name);
180 for (modelist::iterator it = el->begin(); it != el->end(); it++)
183 stack->Push(this->GetModeChar(), it->mask);
185 modestack.Push(this->GetModeChar(), it->mask);
191 while (modestack.GetStackedLine(stackresult))
193 for (size_t j = 0; j < stackresult.size(); j++)
195 mode_junk.push_back(stackresult[j]);
198 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
205 virtual void RemoveMode(User*, irc::modestacker* stack)
207 /* Listmodes dont get set on users */
210 /** Perform a rehash of this mode's configuration data
212 virtual void DoRehash()
214 ConfigReader Conf(ServerInstance);
218 for (int i = 0; i < Conf.Enumerate(configtag); i++)
220 // For each <banlist> tag
222 limit.mask = Conf.ReadValue(configtag, "chan", i);
223 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
225 if (limit.mask.size() && limit.limit > 0)
226 chanlimits.push_back(limit);
228 if (chanlimits.size() == 0)
233 chanlimits.push_back(limit);
237 /** Populate the Implements list with the correct events for a List Mode
239 virtual void DoImplements(Module* m)
241 Implementation eventlist[] = { I_OnChannelDelete, I_OnSyncChannel, I_OnCleanup, I_OnRehash };
242 ServerInstance->Modules->Attach(eventlist, m, 4);
245 /** Handle the list mode.
248 virtual ModeAction OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding, bool servermode)
250 // Try and grab the list
252 channel->GetExt(infokey, el);
256 // If there was no list
261 channel->Extend(infokey, el);
266 ModeParser::CleanMask(parameter);
268 // Check if the item already exists in the list
269 for (modelist::iterator it = el->begin(); it != el->end(); it++)
271 if (parameter == it->mask)
273 /* Give a subclass a chance to error about this */
274 TellAlreadyOnList(source, channel, parameter);
276 // it does, deny the change
277 return MODEACTION_DENY;
281 unsigned int maxsize = 0;
283 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
285 if (InspIRCd::Match(channel->name, it->mask))
287 // We have a pattern matching the channel...
288 maxsize = el->size();
289 if (IS_LOCAL(source) || (maxsize < it->limit))
291 /* Ok, it *could* be allowed, now give someone subclassing us
292 * a chance to validate the parameter.
293 * The param is passed by reference, so they can both modify it
294 * and tell us if we allow it or not.
296 * eg, the subclass could:
298 * 2) 'fix' parameter and then allow
301 if (ValidateParam(source, channel, parameter))
303 // And now add the mask onto the list...
306 e.nick = servermode ? ServerInstance->Config->ServerName : source->nick;
307 e.time = stringtime();
310 return MODEACTION_ALLOW;
314 /* If they deny it they have the job of giving an error message */
315 return MODEACTION_DENY;
321 /* List is full, give subclass a chance to send a custom message */
322 if (!TellListTooLong(source, channel, parameter))
324 source->WriteNumeric(478, "%s %s %s :Channel ban/ignore list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
328 return MODEACTION_DENY;
332 // We're taking the mode off
335 for (modelist::iterator it = el->begin(); it != el->end(); it++)
337 if (parameter == it->mask)
342 channel->Shrink(infokey);
345 return MODEACTION_ALLOW;
348 /* Tried to remove something that wasn't set */
349 TellNotSet(source, channel, parameter);
351 return MODEACTION_DENY;
355 /* Hmm, taking an exception off a non-existant list, DIE */
356 TellNotSet(source, channel, parameter);
358 return MODEACTION_DENY;
361 return MODEACTION_DENY;
364 /** Get Extensible key for this mode
366 virtual std::string& GetInfoKey()
371 /** Handle channel deletion.
373 * @param chan Channel being deleted
375 virtual void DoChannelDelete(Channel* chan)
378 chan->GetExt(infokey, mlist);
382 chan->Shrink(infokey);
387 /** Syncronize channel item list with another server.
389 * @param chan Channel to syncronize
390 * @param proto Protocol module pointer
391 * @param opaque Opaque connection handle
393 virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
396 chan->GetExt(infokey, mlist);
397 irc::modestacker modestack(ServerInstance, true);
398 std::deque<std::string> stackresult;
401 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
403 modestack.Push(std::string(1, mode)[0], it->mask);
406 while (modestack.GetStackedLine(stackresult))
408 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
409 std::string line = mode_join.GetJoined();
410 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
414 /** Clean up module on unload
415 * @param target_type Type of target to clean
416 * @param item Item to clean
418 virtual void DoCleanup(int, void*)
422 /** Validate parameters.
423 * Overridden by implementing module.
424 * @param source Source user adding the parameter
425 * @param channel Channel the parameter is being added to
426 * @param parameter The actual parameter being added
427 * @return true if the parameter is valid
429 virtual bool ValidateParam(User*, Channel*, std::string&)
434 /** Tell the user the list is too long.
435 * Overridden by implementing module.
436 * @param source Source user adding the parameter
437 * @param channel Channel the parameter is being added to
438 * @param parameter The actual parameter being added
441 virtual bool TellListTooLong(User*, Channel*, std::string&)
446 /** Tell the user an item is already on the list.
447 * Overridden by implementing module.
448 * @param source Source user adding the parameter
449 * @param channel Channel the parameter is being added to
450 * @param parameter The actual parameter being added
452 virtual void TellAlreadyOnList(User*, Channel*, std::string&)
456 /** Tell the user that the parameter is not in the list.
457 * Overridden by implementing module.
458 * @param source Source user removing the parameter
459 * @param channel Channel the parameter is being removed from
460 * @param parameter The actual parameter being removed
462 virtual void TellNotSet(User*, Channel*, std::string&)