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
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, Channel* channel) : Request(sender, target, "LM_CHECKLIST_EX"), user(NULL), literal(literalstr), extban(extbanchar), chan(channel)
86 /** Check if a literal string is on a channel's list, optionally using an extban char.
87 * The Event::Send() event returns the ban string if the user is on the channel's list,
88 * or NULL if the user is not on the list.
89 * @param sender Sending module
90 * @param target Target module
91 * @param User to check against, e.g. "Bob!Bobbertson@weeblshouse"
92 * @param extbanchar Extended ban character to use for the match, or a null char if not using extban
94 ListModeRequest(Module* sender, Module* target, User* u, char extbanchar, Channel* channel) : Request(sender, target, "LM_CHECKLIST_EX"), user(u), literal(""), extban(extbanchar), chan(channel)
105 /** The base class for list modes, should be inherited.
107 class ListModeBase : public ModeHandler
113 /** Numeric to use when outputting the list
115 unsigned int listnumeric;
116 /** Numeric to indicate end of list
118 unsigned int endoflistnumeric;
119 /** String to send for end of list
121 std::string endofliststring;
122 /** Automatically tidy up entries
125 /** Config tag to check for max items per channel
127 std::string configtag;
128 /** Limits on a per-channel basis read from the tag
129 * specified in ListModeBase::configtag
131 limitlist chanlimits;
135 * @param Instance The creator of this class
136 * @param modechar Mode character
137 * @param eolstr End of list string
138 * @pram lnum List numeric
139 * @param eolnum End of list numeric
140 * @param autotidy Automatically tidy list entries on add
141 * @param ctag Configuration tag to get limits from
143 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy, const std::string &ctag = "banlist")
144 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
147 infokey = "listbase_mode_" + std::string(1, mode) + "_list";
152 std::pair<bool,std::string> ModeSet(User*, User*, Channel* channel, const std::string ¶meter)
155 channel->GetExt(infokey, el);
158 for (modelist::iterator it = el->begin(); it != el->end(); it++)
160 if(parameter == it->mask)
162 return std::make_pair(true, parameter);
166 return std::make_pair(false, parameter);
169 /** Display the list for this mode
170 * @param user The user to send the list to
171 * @param channel The channel the user is requesting the list for
173 virtual void DisplayList(User* user, Channel* channel)
176 channel->GetExt(infokey, el);
179 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
181 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());
184 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
187 virtual void DisplayEmptyList(User* user, Channel* channel)
189 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
192 /** Remove all instances of the mode from a channel.
194 * @param channel The channel to remove all instances of the mode from
196 virtual void RemoveMode(Channel* channel, irc::modestacker* stack)
199 channel->GetExt(infokey, el);
202 irc::modestacker modestack(ServerInstance, false);
203 std::deque<std::string> stackresult;
204 std::vector<std::string> mode_junk;
205 mode_junk.push_back(channel->name);
207 for (modelist::iterator it = el->begin(); it != el->end(); it++)
210 stack->Push(this->GetModeChar(), it->mask);
212 modestack.Push(this->GetModeChar(), it->mask);
218 while (modestack.GetStackedLine(stackresult))
220 for (size_t j = 0; j < stackresult.size(); j++)
222 mode_junk.push_back(stackresult[j]);
225 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
232 virtual void RemoveMode(User*, irc::modestacker* stack)
234 /* Listmodes dont get set on users */
237 /** Perform a rehash of this mode's configuration data
239 virtual void DoRehash()
241 ConfigReader Conf(ServerInstance);
245 for (int i = 0; i < Conf.Enumerate(configtag); i++)
247 // For each <banlist> tag
249 limit.mask = Conf.ReadValue(configtag, "chan", i);
250 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
252 if (limit.mask.size() && limit.limit > 0)
253 chanlimits.push_back(limit);
255 if (chanlimits.size() == 0)
260 chanlimits.push_back(limit);
264 /** Populate the Implements list with the correct events for a List Mode
266 virtual void DoImplements(Module* m)
268 Implementation eventlist[] = { I_OnChannelDelete, I_OnSyncChannel, I_OnCleanup, I_OnRehash, I_OnRequest };
269 ServerInstance->Modules->Attach(eventlist, m, 5);
272 /** Handle the list mode.
275 virtual ModeAction OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding, bool servermode)
277 // Try and grab the list
279 channel->GetExt(infokey, el);
283 // If there was no list
288 channel->Extend(infokey, el);
293 ModeParser::CleanMask(parameter);
295 // Check if the item already exists in the list
296 for (modelist::iterator it = el->begin(); it != el->end(); it++)
298 if (parameter == it->mask)
300 /* Give a subclass a chance to error about this */
301 TellAlreadyOnList(source, channel, parameter);
303 // it does, deny the change
304 return MODEACTION_DENY;
308 unsigned int maxsize = 0;
310 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
312 if (InspIRCd::Match(channel->name, it->mask))
314 // We have a pattern matching the channel...
315 maxsize = el->size();
316 if (IS_LOCAL(source) || (maxsize < it->limit))
318 /* Ok, it *could* be allowed, now give someone subclassing us
319 * a chance to validate the parameter.
320 * The param is passed by reference, so they can both modify it
321 * and tell us if we allow it or not.
323 * eg, the subclass could:
325 * 2) 'fix' parameter and then allow
328 if (ValidateParam(source, channel, parameter))
330 // And now add the mask onto the list...
333 e.nick = servermode ? ServerInstance->Config->ServerName : source->nick;
334 e.time = stringtime();
337 return MODEACTION_ALLOW;
341 /* If they deny it they have the job of giving an error message */
342 return MODEACTION_DENY;
348 /* List is full, give subclass a chance to send a custom message */
349 if (!TellListTooLong(source, channel, parameter))
351 source->WriteNumeric(478, "%s %s %s :Channel ban/ignore list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
355 return MODEACTION_DENY;
359 // We're taking the mode off
362 for (modelist::iterator it = el->begin(); it != el->end(); it++)
364 if (parameter == it->mask)
369 channel->Shrink(infokey);
372 return MODEACTION_ALLOW;
375 /* Tried to remove something that wasn't set */
376 TellNotSet(source, channel, parameter);
378 return MODEACTION_DENY;
382 /* Hmm, taking an exception off a non-existant list, DIE */
383 TellNotSet(source, channel, parameter);
385 return MODEACTION_DENY;
388 return MODEACTION_DENY;
391 /** Get Extensible key for this mode
393 virtual std::string& GetInfoKey()
398 /** Handle channel deletion.
400 * @param chan Channel being deleted
402 virtual void DoChannelDelete(Channel* chan)
405 chan->GetExt(infokey, mlist);
409 chan->Shrink(infokey);
414 /** Syncronize channel item list with another server.
416 * @param chan Channel to syncronize
417 * @param proto Protocol module pointer
418 * @param opaque Opaque connection handle
420 virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
423 chan->GetExt(infokey, mlist);
424 irc::modestacker modestack(ServerInstance, true);
425 std::deque<std::string> stackresult;
428 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
430 modestack.Push(std::string(1, mode)[0], it->mask);
433 while (modestack.GetStackedLine(stackresult))
435 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
436 std::string line = mode_join.GetJoined();
437 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
441 /** Clean up module on unload
442 * @param target_type Type of target to clean
443 * @param item Item to clean
445 virtual void DoCleanup(int, void*)
449 /** Validate parameters.
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
454 * @return true if the parameter is valid
456 virtual bool ValidateParam(User*, Channel*, std::string&)
461 /** Tell the user the list is too long.
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
468 virtual bool TellListTooLong(User*, Channel*, std::string&)
473 /** Tell the user an item is already on the list.
474 * Overridden by implementing module.
475 * @param source Source user adding the parameter
476 * @param channel Channel the parameter is being added to
477 * @param parameter The actual parameter being added
479 virtual void TellAlreadyOnList(User*, Channel*, std::string&)
483 /** Tell the user that the parameter is not in the list.
484 * Overridden by implementing module.
485 * @param source Source user removing the parameter
486 * @param channel Channel the parameter is being removed from
487 * @param parameter The actual parameter being removed
489 virtual void TellNotSet(User*, Channel*, std::string&)
493 virtual const char* DoOnRequest(Request* request)
495 ListModeRequest* LM = (ListModeRequest*)request;
496 if (strcmp("LM_CHECKLIST", request->GetId()) == 0)
499 LM->chan->GetExt(GetInfoKey(), mlist);
502 std::string mask = LM->user->nick + "!" + LM->user->ident + "@" + LM->user->GetIPString();
503 for (modelist::iterator it = mlist->begin(); it != mlist->end(); ++it)
505 if (InspIRCd::Match(LM->user->GetFullRealHost(), it->mask) || InspIRCd::Match(LM->user->GetFullHost(), it->mask) || (InspIRCd::MatchCIDR(mask, it->mask)))
506 return it->mask.c_str();
511 else if (strcmp("LM_CHECKLIST_EX", request->GetId()) == 0)
514 LM->chan->GetExt(GetInfoKey(), mlist);
518 LM->literal = LM->user->nick + "!" + LM->user->ident + "@" + LM->user->GetIPString();
520 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
522 if (LM->extban && it->mask.length() > 1 && it->mask[0] == LM->extban && it->mask[1] == ':')
524 static std::string ext = it->mask.substr(2);
527 if (InspIRCd::Match(LM->user->GetFullRealHost(), ext) || InspIRCd::Match(LM->user->GetFullHost(), ext) || (InspIRCd::MatchCIDR(LM->literal, ext)))
528 return it->mask.c_str();
530 else if (InspIRCd::Match(LM->literal, ext))
531 return it->mask.c_str();
537 if (InspIRCd::Match(LM->user->GetFullRealHost(), it->mask) || InspIRCd::Match(LM->user->GetFullHost(), it->mask) || (InspIRCd::MatchCIDR(LM->literal, it->mask)))
538 return it->mask.c_str();
540 else if (InspIRCd::Match(LM->literal, it->mask))
541 return it->mask.c_str();