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)
107 std::pair<bool,std::string> ModeSet(User*, User*, Channel* channel, const std::string ¶meter)
109 modelist* el = extItem.get(channel);
112 for (modelist::iterator it = el->begin(); it != el->end(); it++)
114 if(parameter == it->mask)
116 return std::make_pair(true, parameter);
120 return std::make_pair(false, parameter);
123 /** Display the list for this mode
124 * @param user The user to send the list to
125 * @param channel The channel the user is requesting the list for
127 virtual void DisplayList(User* user, Channel* channel)
129 modelist* el = extItem.get(channel);
132 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
134 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());
137 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
140 virtual void DisplayEmptyList(User* user, Channel* channel)
142 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
145 /** Remove all instances of the mode from a channel.
147 * @param channel The channel to remove all instances of the mode from
149 virtual void RemoveMode(Channel* channel, irc::modestacker* stack)
151 modelist* el = extItem.get(channel);
154 irc::modestacker modestack(false);
156 for (modelist::iterator it = el->begin(); it != el->end(); it++)
159 stack->Push(this->GetModeChar(), it->mask);
161 modestack.Push(this->GetModeChar(), it->mask);
167 std::vector<std::string> stackresult;
168 stackresult.push_back(channel->name);
169 while (modestack.GetStackedLine(stackresult))
171 ServerInstance->SendMode(stackresult, ServerInstance->FakeClient);
173 stackresult.push_back(channel->name);
180 virtual void RemoveMode(User*, irc::modestacker* stack)
182 /* Listmodes dont get set on users */
185 /** Perform a rehash of this mode's configuration data
187 virtual void DoRehash()
189 ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
193 for (ConfigIter i = tags.first; i != tags.second; i++)
195 // For each <banlist> tag
196 ConfigTag* c = i->second;
198 limit.mask = c->getString("chan");
199 limit.limit = c->getInt("limit");
201 if (limit.mask.size() && limit.limit > 0)
202 chanlimits.push_back(limit);
205 // Add the default entry. This is inserted last so if the user specifies a
206 // wildcard record in the config it will take precedence over this entry.
210 chanlimits.push_back(limit);
213 /** Populate the Implements list with the correct events for a List Mode
215 virtual void DoImplements(Module* m)
217 ServerInstance->Modules->AddService(extItem);
219 Implementation eventlist[] = { I_OnSyncChannel, I_OnRehash };
220 ServerInstance->Modules->Attach(eventlist, m, sizeof(eventlist)/sizeof(Implementation));
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;
302 /* List is full, give subclass a chance to send a custom message */
303 if (!TellListTooLong(source, channel, parameter))
305 source->WriteNumeric(478, "%s %s %s :Channel ban/ignore list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
309 return MODEACTION_DENY;
313 // We're taking the mode off
316 for (modelist::iterator it = el->begin(); it != el->end(); it++)
318 if (parameter == it->mask)
323 extItem.unset(channel);
325 return MODEACTION_ALLOW;
328 /* Tried to remove something that wasn't set */
329 TellNotSet(source, channel, parameter);
331 return MODEACTION_DENY;
335 /* Hmm, taking an exception off a non-existant list, DIE */
336 TellNotSet(source, channel, parameter);
338 return MODEACTION_DENY;
341 return MODEACTION_DENY;
344 /** Syncronize channel item list with another server.
346 * @param chan Channel to syncronize
347 * @param proto Protocol module pointer
348 * @param opaque Opaque connection handle
350 virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
352 modelist* mlist = extItem.get(chan);
353 irc::modestacker modestack(true);
354 std::vector<std::string> stackresult;
355 std::vector<TranslateType> types;
356 types.push_back(TR_TEXT);
359 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
361 modestack.Push(std::string(1, mode)[0], it->mask);
364 while (modestack.GetStackedLine(stackresult))
366 types.assign(stackresult.size(), this->GetTranslateType());
367 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
372 /** Clean up module on unload
373 * @param target_type Type of target to clean
374 * @param item Item to clean
376 virtual void DoCleanup(int, void*)
380 /** Validate parameters.
381 * Overridden by implementing module.
382 * @param source Source user adding the parameter
383 * @param channel Channel the parameter is being added to
384 * @param parameter The actual parameter being added
385 * @return true if the parameter is valid
387 virtual bool ValidateParam(User*, Channel*, std::string&)
392 /** Tell the user the list is too long.
393 * Overridden by implementing module.
394 * @param source Source user adding the parameter
395 * @param channel Channel the parameter is being added to
396 * @param parameter The actual parameter being added
399 virtual bool TellListTooLong(User*, Channel*, std::string&)
404 /** Tell the user an item is already on the list.
405 * Overridden by implementing module.
406 * @param source Source user adding the parameter
407 * @param channel Channel the parameter is being added to
408 * @param parameter The actual parameter being added
410 virtual void TellAlreadyOnList(User*, Channel*, std::string&)
414 /** Tell the user that the parameter is not in the list.
415 * Overridden by implementing module.
416 * @param source Source user removing the parameter
417 * @param channel Channel the parameter is being removed from
418 * @param parameter The actual parameter being removed
420 virtual void TellNotSet(User*, Channel*, std::string&)