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 std::string listnumeric;
91 /** Numeric to indicate end of list
93 std::string 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, const std::string &lnum, const std::string &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->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());
159 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
162 virtual void DisplayEmptyList(User* user, Channel* channel)
164 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, 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)
174 channel->GetExt(infokey, el);
177 irc::modestacker modestack(false);
178 std::deque<std::string> stackresult;
179 const char* mode_junk[MAXMODES+2];
180 mode_junk[0] = channel->name;
182 for (modelist::iterator it = el->begin(); it != el->end(); it++)
184 modestack.Push(this->GetModeChar(), assign(it->mask));
187 while (modestack.GetStackedLine(stackresult))
189 for (size_t j = 0; j < stackresult.size(); j++)
191 mode_junk[j+1] = stackresult[j].c_str();
194 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, ServerInstance->FakeClient);
201 virtual void RemoveMode(User*)
203 /* Listmodes dont get set on users */
206 /** Perform a rehash of this mode's configuration data
208 virtual void DoRehash()
210 ConfigReader Conf(ServerInstance);
214 for (int i = 0; i < Conf.Enumerate(configtag); i++)
216 // For each <banlist> tag
218 limit.mask = Conf.ReadValue(configtag, "chan", i);
219 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
221 if (limit.mask.size() && limit.limit > 0)
222 chanlimits.push_back(limit);
224 if (chanlimits.size() == 0)
229 chanlimits.push_back(limit);
233 /** Populate the Implements list with the correct events for a List Mode
235 virtual void DoImplements(Module* m)
237 Implementation eventlist[] = { I_OnChannelDelete, I_OnSyncChannel, I_OnCleanup, I_OnRehash };
238 ServerInstance->Modules->Attach(eventlist, m, 4);
241 /** Handle the list mode.
244 virtual ModeAction OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding, bool servermode)
246 // Try and grab the list
248 channel->GetExt(infokey, el);
252 // If there was no list
257 channel->Extend(infokey, el);
262 ModeParser::CleanMask(parameter);
264 // Check if the item already exists in the list
265 for (modelist::iterator it = el->begin(); it != el->end(); it++)
267 if (parameter == it->mask)
269 /* Give a subclass a chance to error about this */
270 TellAlreadyOnList(source, channel, parameter);
272 // it does, deny the change
273 return MODEACTION_DENY;
277 unsigned int maxsize = 0;
279 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
281 if (match(channel->name, it->mask.c_str()))
283 // We have a pattern matching the channel...
284 maxsize = el->size();
285 if (maxsize < it->limit)
287 /* Ok, it *could* be allowed, now give someone subclassing us
288 * a chance to validate the parameter.
289 * The param is passed by reference, so they can both modify it
290 * and tell us if we allow it or not.
292 * eg, the subclass could:
294 * 2) 'fix' parameter and then allow
297 if (ValidateParam(source, channel, parameter))
299 // And now add the mask onto the list...
301 e.mask = assign(parameter);
302 e.nick = servermode ? ServerInstance->Config->ServerName : source->nick;
303 e.time = stringtime();
306 return MODEACTION_ALLOW;
310 /* If they deny it they have the job of giving an error message */
311 return MODEACTION_DENY;
317 /* List is full, give subclass a chance to send a custom message */
318 if (!TellListTooLong(source, channel, parameter))
320 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
324 return MODEACTION_DENY;
328 // We're taking the mode off
331 for (modelist::iterator it = el->begin(); it != el->end(); it++)
333 if (parameter == it->mask)
338 channel->Shrink(infokey);
341 return MODEACTION_ALLOW;
344 /* Tried to remove something that wasn't set */
345 TellNotSet(source, channel, parameter);
347 return MODEACTION_DENY;
351 /* Hmm, taking an exception off a non-existant list, DIE */
352 TellNotSet(source, channel, parameter);
354 return MODEACTION_DENY;
357 return MODEACTION_DENY;
360 /** Get Extensible key for this mode
362 virtual std::string& GetInfoKey()
367 /** Handle channel deletion.
369 * @param chan Channel being deleted
371 virtual void DoChannelDelete(Channel* chan)
374 chan->GetExt(infokey, mlist);
378 chan->Shrink(infokey);
383 /** Syncronize channel item list with another server.
385 * @param chan Channel to syncronize
386 * @param proto Protocol module pointer
387 * @param opaque Opaque connection handle
389 virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
392 chan->GetExt(infokey, mlist);
393 irc::modestacker modestack(true);
394 std::deque<std::string> stackresult;
397 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
399 modestack.Push(std::string(1, mode)[0], assign(it->mask));
402 while (modestack.GetStackedLine(stackresult))
404 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
405 std::string line = mode_join.GetJoined();
406 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
410 /** Clean up module on unload
411 * @param target_type Type of target to clean
412 * @param item Item to clean
414 virtual void DoCleanup(int, void*)
418 /** Validate parameters.
419 * Overridden by implementing module.
420 * @param source Source user adding the parameter
421 * @param channel Channel the parameter is being added to
422 * @param parameter The actual parameter being added
423 * @return true if the parameter is valid
425 virtual bool ValidateParam(User*, Channel*, std::string&)
430 /** Tell the user the list is too long.
431 * Overridden by implementing module.
432 * @param source Source user adding the parameter
433 * @param channel Channel the parameter is being added to
434 * @param parameter The actual parameter being added
437 virtual bool TellListTooLong(User*, Channel*, std::string&)
442 /** Tell the user an item is already on the list.
443 * Overridden by implementing module.
444 * @param source Source user adding the parameter
445 * @param channel Channel the parameter is being added to
446 * @param parameter The actual parameter being added
448 virtual void TellAlreadyOnList(User*, Channel*, std::string&)
452 /** Tell the user that the parameter is not in the list.
453 * Overridden by implementing module.
454 * @param source Source user removing the parameter
455 * @param channel Channel the parameter is being removed from
456 * @param parameter The actual parameter being removed
458 virtual void TellNotSet(User*, Channel*, std::string&)