1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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
17 /** Get the time as a string
19 inline std::string stringtime()
21 std::ostringstream TIME;
22 TIME << ServerInstance->Time();
26 /** An item in a listmode's list
36 /** The number of items a listmode's list may contain
45 /** Items stored in the channel's list
47 typedef std::list<ListItem> modelist;
48 /** Max items per channel by name
50 typedef std::list<ListLimit> limitlist;
52 /** The base class for list modes, should be inherited.
54 class ListModeBase : public ModeHandler
57 /** Numeric to use when outputting the list
59 unsigned int listnumeric;
60 /** Numeric to indicate end of list
62 unsigned int endoflistnumeric;
63 /** String to send for end of list
65 std::string endofliststring;
66 /** Automatically tidy up entries
69 /** Config tag to check for max items per channel
71 std::string configtag;
72 /** Limits on a per-channel basis read from the tag
73 * specified in ListModeBase::configtag
80 SimpleExtItem<modelist> extItem;
83 * @param Instance The creator of this class
84 * @param modechar Mode character
85 * @param eolstr End of list string
86 * @pram lnum List numeric
87 * @param eolnum End of list numeric
88 * @param autotidy Automatically tidy list entries on add
89 * @param ctag Configuration tag to get limits from
91 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")
92 : ModeHandler(Creator, Name, modechar, PARAM_ALWAYS, MODETYPE_CHANNEL),
93 listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy),
94 configtag(ctag), extItem("listbase_mode_" + name + "_list", Creator)
98 ServerInstance->Extensions.Register(&extItem);
103 std::pair<bool,std::string> ModeSet(User*, User*, Channel* channel, const std::string ¶meter)
105 modelist* el = extItem.get(channel);
108 for (modelist::iterator it = el->begin(); it != el->end(); it++)
110 if(parameter == it->mask)
112 return std::make_pair(true, parameter);
116 return std::make_pair(false, parameter);
119 /** Display the list for this mode
120 * @param user The user to send the list to
121 * @param channel The channel the user is requesting the list for
123 virtual void DisplayList(User* user, Channel* channel)
125 modelist* el = extItem.get(channel);
128 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
130 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());
133 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
136 virtual void DisplayEmptyList(User* user, Channel* channel)
138 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
141 /** Remove all instances of the mode from a channel.
143 * @param channel The channel to remove all instances of the mode from
145 virtual void RemoveMode(Channel* channel, irc::modestacker* stack)
147 modelist* el = extItem.get(channel);
150 irc::modestacker modestack(false);
152 for (modelist::iterator it = el->begin(); it != el->end(); it++)
155 stack->Push(this->GetModeChar(), it->mask);
157 modestack.Push(this->GetModeChar(), it->mask);
163 std::vector<std::string> stackresult;
164 stackresult.push_back(channel->name);
165 while (modestack.GetStackedLine(stackresult))
167 ServerInstance->SendMode(stackresult, ServerInstance->FakeClient);
169 stackresult.push_back(channel->name);
176 virtual void RemoveMode(User*, irc::modestacker* stack)
178 /* Listmodes dont get set on users */
181 /** Perform a rehash of this mode's configuration data
183 virtual void DoRehash()
185 ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
189 for (ConfigIter i = tags.first; i != tags.second; i++)
191 // For each <banlist> tag
192 ConfigTag* c = i->second;
194 limit.mask = c->getString("chan");
195 limit.limit = c->getInt("limit");
197 if (limit.mask.size() && limit.limit > 0)
198 chanlimits.push_back(limit);
200 if (chanlimits.size() == 0)
205 chanlimits.push_back(limit);
209 /** Populate the Implements list with the correct events for a List Mode
211 virtual void DoImplements(Module* m)
213 Implementation eventlist[] = { I_OnSyncChannel, I_OnRehash };
214 ServerInstance->Modules->Attach(eventlist, m, 2);
217 /** Handle the list mode.
220 virtual ModeAction OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding)
222 // Try and grab the list
223 modelist* el = extItem.get(channel);
228 ModeParser::CleanMask(parameter);
230 if (parameter.length() > 250)
231 return MODEACTION_DENY;
233 // If there was no list
238 extItem.set(channel, el);
241 // Check if the item already exists in the list
242 for (modelist::iterator it = el->begin(); it != el->end(); it++)
244 if (parameter == it->mask)
246 /* Give a subclass a chance to error about this */
247 TellAlreadyOnList(source, channel, parameter);
249 // it does, deny the change
250 return MODEACTION_DENY;
254 unsigned int maxsize = 0;
256 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
258 if (InspIRCd::Match(channel->name, it->mask))
260 // We have a pattern matching the channel...
261 maxsize = el->size();
262 if (!IS_LOCAL(source) || (maxsize < it->limit))
264 /* Ok, it *could* be allowed, now give someone subclassing us
265 * a chance to validate the parameter.
266 * The param is passed by reference, so they can both modify it
267 * and tell us if we allow it or not.
269 * eg, the subclass could:
271 * 2) 'fix' parameter and then allow
274 if (ValidateParam(source, channel, parameter))
276 // And now add the mask onto the list...
279 e.nick = source->nick;
280 e.time = stringtime();
283 return MODEACTION_ALLOW;
287 /* If they deny it they have the job of giving an error message */
288 return MODEACTION_DENY;
294 /* List is full, give subclass a chance to send a custom message */
295 if (!TellListTooLong(source, channel, parameter))
297 source->WriteNumeric(478, "%s %s %s :Channel ban/ignore list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
301 return MODEACTION_DENY;
305 // We're taking the mode off
308 for (modelist::iterator it = el->begin(); it != el->end(); it++)
310 if (parameter == it->mask)
315 extItem.unset(channel);
317 return MODEACTION_ALLOW;
320 /* Tried to remove something that wasn't set */
321 TellNotSet(source, channel, parameter);
323 return MODEACTION_DENY;
327 /* Hmm, taking an exception off a non-existant list, DIE */
328 TellNotSet(source, channel, parameter);
330 return MODEACTION_DENY;
333 return MODEACTION_DENY;
336 /** Syncronize channel item list with another server.
338 * @param chan Channel to syncronize
339 * @param proto Protocol module pointer
340 * @param opaque Opaque connection handle
342 virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
344 modelist* mlist = extItem.get(chan);
345 irc::modestacker modestack(true);
346 std::vector<std::string> stackresult;
347 std::vector<TranslateType> types;
348 types.push_back(TR_TEXT);
351 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
353 modestack.Push(std::string(1, mode)[0], it->mask);
356 while (modestack.GetStackedLine(stackresult))
358 types.assign(stackresult.size(), this->GetTranslateType());
359 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
364 /** Clean up module on unload
365 * @param target_type Type of target to clean
366 * @param item Item to clean
368 virtual void DoCleanup(int, void*)
372 /** Validate parameters.
373 * Overridden by implementing module.
374 * @param source Source user adding the parameter
375 * @param channel Channel the parameter is being added to
376 * @param parameter The actual parameter being added
377 * @return true if the parameter is valid
379 virtual bool ValidateParam(User*, Channel*, std::string&)
384 /** Tell the user the list is too long.
385 * Overridden by implementing module.
386 * @param source Source user adding the parameter
387 * @param channel Channel the parameter is being added to
388 * @param parameter The actual parameter being added
391 virtual bool TellListTooLong(User*, Channel*, std::string&)
396 /** Tell the user an item is already on the list.
397 * Overridden by implementing module.
398 * @param source Source user adding the parameter
399 * @param channel Channel the parameter is being added to
400 * @param parameter The actual parameter being added
402 virtual void TellAlreadyOnList(User*, Channel*, std::string&)
406 /** Tell the user that the parameter is not in the list.
407 * Overridden by implementing module.
408 * @param source Source user removing the parameter
409 * @param channel Channel the parameter is being removed from
410 * @param parameter The actual parameter being removed
412 virtual void TellNotSet(User*, Channel*, std::string&)