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 /** Get the time as a string
29 inline std::string stringtime()
31 std::ostringstream TIME;
36 /** An item in a listmode's list
38 class ListItem : public classbase
46 /** The number of items a listmode's list may contain
48 class ListLimit : public classbase
55 /** Items stored in the channel's list
57 typedef std::vector<ListItem> modelist;
58 /** Max items per channel by name
60 typedef std::vector<ListLimit> limitlist;
62 /** A request used to check if a user is on a channel's list or not
64 class ListModeRequest : public Request
70 /** Check if a user is on a channel's list.
71 * The Event::Send() event returns true if the user is on the channel's list.
72 * @param sender Sending module
73 * @param target Target module
74 * @param u User to check against
75 * @param c Channel to check against
77 ListModeRequest(Module* sender, Module* target, userrec* u, chanrec* c) : Request(sender, target, "LM_CHECKLIST"), user(u), chan(c)
88 /** The base class for list modes, should be inherited.
90 class ListModeBase : public ModeHandler
96 /** Numeric to use when outputting the list
98 std::string listnumeric;
99 /** Numeric to indicate end of list
101 std::string endoflistnumeric;
102 /** String to send for end of list
104 std::string endofliststring;
105 /** Automatically tidy up entries
108 /** Config tag to check for max items per channel
110 std::string configtag;
111 /** Limits on a per-channel basis read from the tag
112 * specified in ListModeBase::configtag
114 limitlist chanlimits;
118 * @param Instance The creator of this class
119 * @param modechar Mode character
120 * @param eolstr End of list string
121 * @pram lnum List numeric
122 * @param eolnum End of list numeric
123 * @param autotidy Automatically tidy list entries on add
124 * @param ctag Configuration tag to get limits from
126 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
127 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
130 infokey = "listbase_mode_" + std::string(1, mode) + "_list";
135 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
138 channel->GetExt(infokey, el);
141 for (modelist::iterator it = el->begin(); it != el->end(); it++)
143 if(parameter == it->mask)
145 return std::make_pair(true, parameter);
149 return std::make_pair(false, parameter);
152 /** Display the list for this mode
153 * @param user The user to send the list to
154 * @param channel The channel the user is requesting the list for
156 virtual void DisplayList(userrec* user, chanrec* channel)
159 channel->GetExt(infokey, el);
162 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
164 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());
167 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
170 virtual void DisplayEmptyList(userrec* user, chanrec* channel)
172 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
175 /** Remove all instances of the mode from a channel.
177 * @param channel The channel to remove all instances of the mode from
179 virtual void RemoveMode(chanrec* channel)
182 channel->GetExt(infokey, el);
185 irc::modestacker modestack(false);
186 std::deque<std::string> stackresult;
187 const char* mode_junk[MAXMODES+2];
188 mode_junk[0] = channel->name;
190 for (modelist::iterator it = el->begin(); it != el->end(); it++)
192 modestack.Push(this->GetModeChar(), assign(it->mask));
195 while (modestack.GetStackedLine(stackresult))
197 for (size_t j = 0; j < stackresult.size(); j++)
199 mode_junk[j+1] = stackresult[j].c_str();
202 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, ServerInstance->FakeClient);
209 virtual void RemoveMode(userrec* user)
211 /* Listmodes dont get set on users */
214 /** Perform a rehash of this mode's configuration data
216 virtual void DoRehash()
218 ConfigReader Conf(ServerInstance);
222 for (int i = 0; i < Conf.Enumerate(configtag); i++)
224 // For each <banlist> tag
226 limit.mask = Conf.ReadValue(configtag, "chan", i);
227 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
229 if (limit.mask.size() && limit.limit > 0)
230 chanlimits.push_back(limit);
232 if (chanlimits.size() == 0)
237 chanlimits.push_back(limit);
241 /** Populate the Implements list with the correct events for a List Mode
243 virtual void DoImplements(char* List)
245 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
248 /** Handle the list mode.
251 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
253 // Try and grab the list
255 channel->GetExt(infokey, el);
259 // If there was no list
264 channel->Extend(infokey, el);
269 ModeParser::CleanMask(parameter);
271 // Check if the item already exists in the list
272 for (modelist::iterator it = el->begin(); it != el->end(); it++)
274 if (parameter == it->mask)
276 /* Give a subclass a chance to error about this */
277 TellAlreadyOnList(source, channel, parameter);
279 // it does, deny the change
280 return MODEACTION_DENY;
284 unsigned int maxsize = 0;
286 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
288 if (match(channel->name, it->mask.c_str()))
290 // We have a pattern matching the channel...
291 maxsize = el->size();
292 if (maxsize < it->limit)
294 /* Ok, it *could* be allowed, now give someone subclassing us
295 * a chance to validate the parameter.
296 * The param is passed by reference, so they can both modify it
297 * and tell us if we allow it or not.
299 * eg, the subclass could:
301 * 2) 'fix' parameter and then allow
304 if (ValidateParam(source, channel, parameter))
306 // And now add the mask onto the list...
308 e.mask = assign(parameter);
309 e.nick = source->nick;
310 e.time = stringtime();
313 return MODEACTION_ALLOW;
317 /* If they deny it they have the job of giving an error message */
318 return MODEACTION_DENY;
324 /* List is full, give subclass a chance to send a custom message */
325 if (!TellListTooLong(source, channel, parameter))
327 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
331 return MODEACTION_DENY;
335 // We're taking the mode off
338 for (modelist::iterator it = el->begin(); it != el->end(); it++)
340 if (parameter == it->mask)
345 channel->Shrink(infokey);
348 return MODEACTION_ALLOW;
351 /* Tried to remove something that wasn't set */
352 TellNotSet(source, channel, parameter);
354 return MODEACTION_DENY;
358 /* Hmm, taking an exception off a non-existant list, DIE */
359 TellNotSet(source, channel, parameter);
361 return MODEACTION_DENY;
364 return MODEACTION_DENY;
367 /** Get Extensible key for this mode
369 virtual std::string& GetInfoKey()
374 /** Handle channel deletion.
376 * @param chan Channel being deleted
378 virtual void DoChannelDelete(chanrec* chan)
381 chan->GetExt(infokey, list);
385 chan->Shrink(infokey);
390 /** Syncronize channel item list with another server.
392 * @param chan Channel to syncronize
393 * @param proto Protocol module pointer
394 * @param opaque Opaque connection handle
396 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
399 chan->GetExt(infokey, list);
400 irc::modestacker modestack(true);
401 std::deque<std::string> stackresult;
404 for (modelist::iterator it = list->begin(); it != list->end(); it++)
406 modestack.Push(std::string(1, mode)[0], assign(it->mask));
409 while (modestack.GetStackedLine(stackresult))
411 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
412 std::string line = mode_join.GetJoined();
413 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
417 /** Clean up module on unload
418 * @param target_type Type of target to clean
419 * @param item Item to clean
421 virtual void DoCleanup(int target_type, void* item)
425 /** Validate parameters.
426 * Overridden by implementing module.
427 * @param source Source user adding the parameter
428 * @param channel Channel the parameter is being added to
429 * @param parameter The actual parameter being added
430 * @return true if the parameter is valid
432 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
437 /** Tell the user the list is too long.
438 * Overridden by implementing module.
439 * @param source Source user adding the parameter
440 * @param channel Channel the parameter is being added to
441 * @param parameter The actual parameter being added
444 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
449 /** Tell the user an item is already on the list.
450 * Overridden by implementing module.
451 * @param source Source user adding the parameter
452 * @param channel Channel the parameter is being added to
453 * @param parameter The actual parameter being added
455 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
459 /** Tell the user that the parameter is not in the list.
460 * Overridden by implementing module.
461 * @param source Source user removing the parameter
462 * @param channel Channel the parameter is being removed from
463 * @param parameter The actual parameter being removed
465 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)