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
25 /** Get the time as a string
27 inline std::string stringtime()
29 std::ostringstream TIME;
34 /** An item in a listmode's list
36 class ListItem : public classbase
44 /** The number of items a listmode's list may contain
46 class ListLimit : public classbase
53 /** Items stored in the channel's list
55 typedef std::vector<ListItem> modelist;
56 /** Max items per channel by name
58 typedef std::vector<ListLimit> limitlist;
60 /** A request used to check if a user is on a channel's list or not
62 class ListModeRequest : public Request
68 /** Check if a user is on a channel's list.
69 * The Event::Send() event returns true if the user is on the channel's list.
70 * @param sender Sending module
71 * @param target Target module
72 * @param u User to check against
73 * @param c Channel to check against
75 ListModeRequest(Module* sender, Module* target, User* u, Channel* c) : Request(sender, target, "LM_CHECKLIST"), user(u), chan(c)
86 /** The base class for list modes, should be inherited.
88 class ListModeBase : public ModeHandler
94 /** Numeric to use when outputting the list
96 std::string listnumeric;
97 /** Numeric to indicate end of list
99 std::string endoflistnumeric;
100 /** String to send for end of list
102 std::string endofliststring;
103 /** Automatically tidy up entries
106 /** Config tag to check for max items per channel
108 std::string configtag;
109 /** Limits on a per-channel basis read from the tag
110 * specified in ListModeBase::configtag
112 limitlist chanlimits;
116 * @param Instance The creator of this class
117 * @param modechar Mode character
118 * @param eolstr End of list string
119 * @pram lnum List numeric
120 * @param eolnum End of list numeric
121 * @param autotidy Automatically tidy list entries on add
122 * @param ctag Configuration tag to get limits from
124 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
125 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
128 infokey = "listbase_mode_" + std::string(1, mode) + "_list";
133 std::pair<bool,std::string> ModeSet(User*, User*, Channel* channel, const std::string ¶meter)
136 channel->GetExt(infokey, el);
139 for (modelist::iterator it = el->begin(); it != el->end(); it++)
141 if(parameter == it->mask)
143 return std::make_pair(true, parameter);
147 return std::make_pair(false, parameter);
150 /** Display the list for this mode
151 * @param user The user to send the list to
152 * @param channel The channel the user is requesting the list for
154 virtual void DisplayList(User* user, Channel* channel)
157 channel->GetExt(infokey, el);
160 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
162 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());
165 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
168 virtual void DisplayEmptyList(User* user, Channel* channel)
170 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
173 /** Remove all instances of the mode from a channel.
175 * @param channel The channel to remove all instances of the mode from
177 virtual void RemoveMode(Channel* channel)
180 channel->GetExt(infokey, el);
183 irc::modestacker modestack(false);
184 std::deque<std::string> stackresult;
185 const char* mode_junk[MAXMODES+2];
186 mode_junk[0] = channel->name;
188 for (modelist::iterator it = el->begin(); it != el->end(); it++)
190 modestack.Push(this->GetModeChar(), assign(it->mask));
193 while (modestack.GetStackedLine(stackresult))
195 for (size_t j = 0; j < stackresult.size(); j++)
197 mode_junk[j+1] = stackresult[j].c_str();
200 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, ServerInstance->FakeClient);
207 virtual void RemoveMode(User*)
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)
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.c_str()))
289 // We have a pattern matching the channel...
290 maxsize = el->size();
291 if (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...
307 e.mask = assign(parameter);
308 e.nick = 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->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, 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, list);
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, list);
399 irc::modestacker modestack(true);
400 std::deque<std::string> stackresult;
403 for (modelist::iterator it = list->begin(); it != list->end(); it++)
405 modestack.Push(std::string(1, mode)[0], assign(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&)