2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef INSPIRCD_LISTMODE_PROVIDER
21 #define INSPIRCD_LISTMODE_PROVIDER
23 /** Get the time as a string
25 inline std::string stringtime()
27 std::ostringstream TIME;
28 TIME << ServerInstance->Time();
32 /** An item in a listmode's list
42 /** The number of items a listmode's list may contain
51 /** Items stored in the channel's list
53 typedef std::list<ListItem> modelist;
54 /** Max items per channel by name
56 typedef std::list<ListLimit> limitlist;
58 /** The base class for list modes, should be inherited.
60 class ListModeBase : public ModeHandler
63 /** Numeric to use when outputting the list
65 unsigned int listnumeric;
66 /** Numeric to indicate end of list
68 unsigned int endoflistnumeric;
69 /** String to send for end of list
71 std::string endofliststring;
72 /** Automatically tidy up entries
75 /** Config tag to check for max items per channel
77 std::string configtag;
78 /** Limits on a per-channel basis read from the tag
79 * specified in ListModeBase::configtag
86 SimpleExtItem<modelist> extItem;
89 * @param Instance The creator of this class
90 * @param modechar Mode character
91 * @param eolstr End of list string
92 * @pram lnum List numeric
93 * @param eolnum End of list numeric
94 * @param autotidy Automatically tidy list entries on add
95 * @param ctag Configuration tag to get limits from
97 ListModeBase(Module* Creator, const std::string& Name, char modechar, const std::string &eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy, const std::string &ctag = "banlist")
98 : ModeHandler(Creator, Name, modechar, PARAM_ALWAYS, MODETYPE_CHANNEL),
99 listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy),
100 configtag(ctag), extItem("listbase_mode_" + name + "_list", Creator)
104 ServerInstance->Extensions.Register(&extItem);
109 std::pair<bool,std::string> ModeSet(User*, User*, Channel* channel, const std::string ¶meter)
111 modelist* el = extItem.get(channel);
114 for (modelist::iterator it = el->begin(); it != el->end(); it++)
116 if(parameter == it->mask)
118 return std::make_pair(true, parameter);
122 return std::make_pair(false, parameter);
125 /** Display the list for this mode
126 * @param user The user to send the list to
127 * @param channel The channel the user is requesting the list for
129 virtual void DisplayList(User* user, Channel* channel)
131 modelist* el = extItem.get(channel);
134 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
136 user->WriteNumeric(listnumeric, "%s %s %s %s %s", user->nick.c_str(), channel->name.c_str(), it->mask.c_str(), (it->nick.length() ? it->nick.c_str() : ServerInstance->Config->ServerName.c_str()), it->time.c_str());
139 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
142 virtual void DisplayEmptyList(User* user, Channel* channel)
144 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
147 /** Remove all instances of the mode from a channel.
149 * @param channel The channel to remove all instances of the mode from
151 virtual void RemoveMode(Channel* channel, irc::modestacker* stack)
153 modelist* el = extItem.get(channel);
156 irc::modestacker modestack(false);
158 for (modelist::iterator it = el->begin(); it != el->end(); it++)
161 stack->Push(this->GetModeChar(), it->mask);
163 modestack.Push(this->GetModeChar(), it->mask);
169 std::vector<std::string> stackresult;
170 stackresult.push_back(channel->name);
171 while (modestack.GetStackedLine(stackresult))
173 ServerInstance->SendMode(stackresult, ServerInstance->FakeClient);
175 stackresult.push_back(channel->name);
182 virtual void RemoveMode(User*, irc::modestacker* stack)
184 /* Listmodes dont get set on users */
187 /** Perform a rehash of this mode's configuration data
189 virtual void DoRehash()
191 ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
195 for (ConfigIter i = tags.first; i != tags.second; i++)
197 // For each <banlist> tag
198 ConfigTag* c = i->second;
200 limit.mask = c->getString("chan");
201 limit.limit = c->getInt("limit");
203 if (limit.mask.size() && limit.limit > 0)
204 chanlimits.push_back(limit);
206 if (chanlimits.size() == 0)
211 chanlimits.push_back(limit);
215 /** Populate the Implements list with the correct events for a List Mode
217 virtual void DoImplements(Module* m)
219 Implementation eventlist[] = { I_OnSyncChannel, I_OnRehash };
220 ServerInstance->Modules->Attach(eventlist, m, 2);
223 /** Handle the list mode.
226 virtual ModeAction OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding)
228 // Try and grab the list
229 modelist* el = extItem.get(channel);
234 ModeParser::CleanMask(parameter);
236 if (parameter.length() > 250)
237 return MODEACTION_DENY;
239 // If there was no list
244 extItem.set(channel, el);
247 // Check if the item already exists in the list
248 for (modelist::iterator it = el->begin(); it != el->end(); it++)
250 if (parameter == it->mask)
252 /* Give a subclass a chance to error about this */
253 TellAlreadyOnList(source, channel, parameter);
255 // it does, deny the change
256 return MODEACTION_DENY;
260 unsigned int maxsize = 0;
262 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
264 if (InspIRCd::Match(channel->name, it->mask))
266 // We have a pattern matching the channel...
267 maxsize = el->size();
268 if (!IS_LOCAL(source) || (maxsize < it->limit))
270 /* Ok, it *could* be allowed, now give someone subclassing us
271 * a chance to validate the parameter.
272 * The param is passed by reference, so they can both modify it
273 * and tell us if we allow it or not.
275 * eg, the subclass could:
277 * 2) 'fix' parameter and then allow
280 if (ValidateParam(source, channel, parameter))
282 // And now add the mask onto the list...
285 e.nick = source->nick;
286 e.time = stringtime();
289 return MODEACTION_ALLOW;
293 /* If they deny it they have the job of giving an error message */
294 return MODEACTION_DENY;
300 /* List is full, give subclass a chance to send a custom message */
301 if (!TellListTooLong(source, channel, parameter))
303 source->WriteNumeric(478, "%s %s %s :Channel ban/ignore list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
307 return MODEACTION_DENY;
311 // We're taking the mode off
314 for (modelist::iterator it = el->begin(); it != el->end(); it++)
316 if (parameter == it->mask)
321 extItem.unset(channel);
323 return MODEACTION_ALLOW;
326 /* Tried to remove something that wasn't set */
327 TellNotSet(source, channel, parameter);
329 return MODEACTION_DENY;
333 /* Hmm, taking an exception off a non-existant list, DIE */
334 TellNotSet(source, channel, parameter);
336 return MODEACTION_DENY;
339 return MODEACTION_DENY;
342 /** Syncronize channel item list with another server.
344 * @param chan Channel to syncronize
345 * @param proto Protocol module pointer
346 * @param opaque Opaque connection handle
348 virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
350 modelist* mlist = extItem.get(chan);
351 irc::modestacker modestack(true);
352 std::vector<std::string> stackresult;
353 std::vector<TranslateType> types;
354 types.push_back(TR_TEXT);
357 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
359 modestack.Push(std::string(1, mode)[0], it->mask);
362 while (modestack.GetStackedLine(stackresult))
364 types.assign(stackresult.size(), this->GetTranslateType());
365 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
370 /** Clean up module on unload
371 * @param target_type Type of target to clean
372 * @param item Item to clean
374 virtual void DoCleanup(int, void*)
378 /** Validate parameters.
379 * Overridden by implementing module.
380 * @param source Source user adding the parameter
381 * @param channel Channel the parameter is being added to
382 * @param parameter The actual parameter being added
383 * @return true if the parameter is valid
385 virtual bool ValidateParam(User*, Channel*, std::string&)
390 /** Tell the user the list is too long.
391 * Overridden by implementing module.
392 * @param source Source user adding the parameter
393 * @param channel Channel the parameter is being added to
394 * @param parameter The actual parameter being added
397 virtual bool TellListTooLong(User*, Channel*, std::string&)
402 /** Tell the user an item is already on the list.
403 * Overridden by implementing module.
404 * @param source Source user adding the parameter
405 * @param channel Channel the parameter is being added to
406 * @param parameter The actual parameter being added
408 virtual void TellAlreadyOnList(User*, Channel*, std::string&)
412 /** Tell the user that the parameter is not in the list.
413 * Overridden by implementing module.
414 * @param source Source user removing the parameter
415 * @param channel Channel the parameter is being removed from
416 * @param parameter The actual parameter being removed
418 virtual void TellNotSet(User*, Channel*, std::string&)