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
17 /** Get the time as a string
19 inline std::string stringtime()
21 std::ostringstream TIME;
26 /** An item in a listmode's list
28 class ListItem : public classbase
36 /** The number of items a listmode's list may contain
38 class ListLimit : public classbase
45 /** Items stored in the channel's list
47 typedef std::vector<ListItem> modelist;
48 /** Max items per channel by name
50 typedef std::vector<ListLimit> limitlist;
52 /** A request used to check if a user is on a channel's list or not
54 class ListModeRequest : public Request
58 const std::string literal;
62 /** Check if a user is on a channel's list.
63 * The Event::Send() event returns the ban string if the user is on the channel's list,
64 * or NULL if the user is not on the list.
65 * @param sender Sending module
66 * @param target Target module
67 * @param u User to check against
68 * @param c Channel to check against
70 ListModeRequest(Module* sender, Module* target, User* u, Channel* c) : Request(sender, target, "LM_CHECKLIST"), user(u), literal(""), extban(0), chan(c)
74 /** Check if a literal string is on a channel's list, optionally using an extban char.
75 * The Event::Send() event returns the ban string if the user is on the channel's list,
76 * or NULL if the user is not on the list.
77 * @param sender Sending module
78 * @param target Target module
79 * @param literalstr String to check against, e.g. "Bob!Bobbertson@weeblshouse"
80 * @param extbanchar Extended ban character to use for the match, or a null char if not using extban
82 ListModeRequest(Module* sender, Module* target, std::string literalstr, char extbanchar) : Request(sender, target, "LM_CHECKLIST_EX"), literal(literalstr), extban(extbanchar)
93 /** The base class for list modes, should be inherited.
95 class ListModeBase : public ModeHandler
101 /** Numeric to use when outputting the list
103 unsigned int listnumeric;
104 /** Numeric to indicate end of list
106 unsigned int endoflistnumeric;
107 /** String to send for end of list
109 std::string endofliststring;
110 /** Automatically tidy up entries
113 /** Config tag to check for max items per channel
115 std::string configtag;
116 /** Limits on a per-channel basis read from the tag
117 * specified in ListModeBase::configtag
119 limitlist chanlimits;
123 * @param Instance The creator of this class
124 * @param modechar Mode character
125 * @param eolstr End of list string
126 * @pram lnum List numeric
127 * @param eolnum End of list numeric
128 * @param autotidy Automatically tidy list entries on add
129 * @param ctag Configuration tag to get limits from
131 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy, const std::string &ctag = "banlist")
132 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
135 infokey = "listbase_mode_" + std::string(1, mode) + "_list";
140 std::pair<bool,std::string> ModeSet(User*, User*, Channel* channel, const std::string ¶meter)
143 channel->GetExt(infokey, el);
146 for (modelist::iterator it = el->begin(); it != el->end(); it++)
148 if(parameter == it->mask)
150 return std::make_pair(true, parameter);
154 return std::make_pair(false, parameter);
157 /** Display the list for this mode
158 * @param user The user to send the list to
159 * @param channel The channel the user is requesting the list for
161 virtual void DisplayList(User* user, Channel* channel)
164 channel->GetExt(infokey, el);
167 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
169 user->WriteNumeric(listnumeric, "%s %s %s %s %s", user->nick.c_str(), channel->name.c_str(), it->mask.c_str(), it->nick.c_str(), it->time.c_str());
172 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
175 virtual void DisplayEmptyList(User* user, Channel* channel)
177 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
180 /** Remove all instances of the mode from a channel.
182 * @param channel The channel to remove all instances of the mode from
184 virtual void RemoveMode(Channel* channel, irc::modestacker* stack)
187 channel->GetExt(infokey, el);
190 irc::modestacker modestack(ServerInstance, false);
191 std::deque<std::string> stackresult;
192 std::vector<std::string> mode_junk;
193 mode_junk.push_back(channel->name);
195 for (modelist::iterator it = el->begin(); it != el->end(); it++)
198 stack->Push(this->GetModeChar(), it->mask);
200 modestack.Push(this->GetModeChar(), it->mask);
206 while (modestack.GetStackedLine(stackresult))
208 for (size_t j = 0; j < stackresult.size(); j++)
210 mode_junk.push_back(stackresult[j]);
213 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
220 virtual void RemoveMode(User*, irc::modestacker* stack)
222 /* Listmodes dont get set on users */
225 /** Perform a rehash of this mode's configuration data
227 virtual void DoRehash()
229 ConfigReader Conf(ServerInstance);
233 for (int i = 0; i < Conf.Enumerate(configtag); i++)
235 // For each <banlist> tag
237 limit.mask = Conf.ReadValue(configtag, "chan", i);
238 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
240 if (limit.mask.size() && limit.limit > 0)
241 chanlimits.push_back(limit);
243 if (chanlimits.size() == 0)
248 chanlimits.push_back(limit);
252 /** Populate the Implements list with the correct events for a List Mode
254 virtual void DoImplements(Module* m)
256 Implementation eventlist[] = { I_OnChannelDelete, I_OnSyncChannel, I_OnCleanup, I_OnRehash, I_OnRequest };
257 ServerInstance->Modules->Attach(eventlist, m, 5);
260 /** Handle the list mode.
263 virtual ModeAction OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding, bool servermode)
265 // Try and grab the list
267 channel->GetExt(infokey, el);
271 // If there was no list
276 channel->Extend(infokey, el);
281 ModeParser::CleanMask(parameter);
283 // Check if the item already exists in the list
284 for (modelist::iterator it = el->begin(); it != el->end(); it++)
286 if (parameter == it->mask)
288 /* Give a subclass a chance to error about this */
289 TellAlreadyOnList(source, channel, parameter);
291 // it does, deny the change
292 return MODEACTION_DENY;
296 unsigned int maxsize = 0;
298 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
300 if (InspIRCd::Match(channel->name, it->mask))
302 // We have a pattern matching the channel...
303 maxsize = el->size();
304 if (IS_LOCAL(source) || (maxsize < it->limit))
306 /* Ok, it *could* be allowed, now give someone subclassing us
307 * a chance to validate the parameter.
308 * The param is passed by reference, so they can both modify it
309 * and tell us if we allow it or not.
311 * eg, the subclass could:
313 * 2) 'fix' parameter and then allow
316 if (ValidateParam(source, channel, parameter))
318 // And now add the mask onto the list...
321 e.nick = servermode ? ServerInstance->Config->ServerName : source->nick;
322 e.time = stringtime();
325 return MODEACTION_ALLOW;
329 /* If they deny it they have the job of giving an error message */
330 return MODEACTION_DENY;
336 /* List is full, give subclass a chance to send a custom message */
337 if (!TellListTooLong(source, channel, parameter))
339 source->WriteNumeric(478, "%s %s %s :Channel ban/ignore list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
343 return MODEACTION_DENY;
347 // We're taking the mode off
350 for (modelist::iterator it = el->begin(); it != el->end(); it++)
352 if (parameter == it->mask)
357 channel->Shrink(infokey);
360 return MODEACTION_ALLOW;
363 /* Tried to remove something that wasn't set */
364 TellNotSet(source, channel, parameter);
366 return MODEACTION_DENY;
370 /* Hmm, taking an exception off a non-existant list, DIE */
371 TellNotSet(source, channel, parameter);
373 return MODEACTION_DENY;
376 return MODEACTION_DENY;
379 /** Get Extensible key for this mode
381 virtual std::string& GetInfoKey()
386 /** Handle channel deletion.
388 * @param chan Channel being deleted
390 virtual void DoChannelDelete(Channel* chan)
393 chan->GetExt(infokey, mlist);
397 chan->Shrink(infokey);
402 /** Syncronize channel item list with another server.
404 * @param chan Channel to syncronize
405 * @param proto Protocol module pointer
406 * @param opaque Opaque connection handle
408 virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
411 chan->GetExt(infokey, mlist);
412 irc::modestacker modestack(ServerInstance, true);
413 std::deque<std::string> stackresult;
416 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
418 modestack.Push(std::string(1, mode)[0], it->mask);
421 while (modestack.GetStackedLine(stackresult))
423 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
424 std::string line = mode_join.GetJoined();
425 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
429 /** Clean up module on unload
430 * @param target_type Type of target to clean
431 * @param item Item to clean
433 virtual void DoCleanup(int, void*)
437 /** Validate parameters.
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
442 * @return true if the parameter is valid
444 virtual bool ValidateParam(User*, Channel*, std::string&)
449 /** Tell the user the list is too long.
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
456 virtual bool TellListTooLong(User*, Channel*, std::string&)
461 /** Tell the user an item is already on the list.
462 * Overridden by implementing module.
463 * @param source Source user adding the parameter
464 * @param channel Channel the parameter is being added to
465 * @param parameter The actual parameter being added
467 virtual void TellAlreadyOnList(User*, Channel*, std::string&)
471 /** Tell the user that the parameter is not in the list.
472 * Overridden by implementing module.
473 * @param source Source user removing the parameter
474 * @param channel Channel the parameter is being removed from
475 * @param parameter The actual parameter being removed
477 virtual void TellNotSet(User*, Channel*, std::string&)
481 virtual const char* DoOnRequest(Request* request)
483 ListModeRequest* LM = (ListModeRequest*)request;
484 if (strcmp("LM_CHECKLIST", request->GetId()) == 0)
487 LM->chan->GetExt(GetInfoKey(), mlist);
490 std::string mask = std::string(LM->user->nick) + "!" + LM->user->ident + "@" + LM->user->GetIPString();
491 for (modelist::iterator it = mlist->begin(); it != mlist->end(); ++it)
493 if (InspIRCd::Match(LM->user->GetFullRealHost(), it->mask) || InspIRCd::Match(LM->user->GetFullHost(), it->mask) || (InspIRCd::MatchCIDR(mask, it->mask)))
494 return it->mask.c_str();
499 else if (strcmp("LM_CHECKLIST_EX", request->GetId()) == 0)
502 LM->chan->GetExt(GetInfoKey(), mlist);
505 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
507 if (LM->extban && it->mask.length() > 1 && it->mask[0] == LM->extban && it->mask[1] == ':')
509 if (InspIRCd::Match(LM->literal.substr(2), it->mask))
510 return it->mask.c_str();
514 if (InspIRCd::Match(LM->literal, it->mask))
515 return it->mask.c_str();