1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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(InspIRCd* Instance)
21 std::ostringstream TIME;
22 TIME << Instance->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::list<ListItem> modelist;
48 /** Max items per channel by name
50 typedef std::list<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.length() ? it->nick.c_str() : ServerInstance->Config->ServerName), 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);
204 for (modelist::iterator it = el->begin(); it != el->end(); it++)
207 stack->Push(this->GetModeChar(), it->mask);
209 modestack.Push(this->GetModeChar(), it->mask);
215 std::vector<std::string> stackresult;
216 stackresult.push_back(channel->name);
217 while (modestack.GetStackedLine(stackresult))
219 ServerInstance->SendMode(stackresult, ServerInstance->FakeClient);
221 stackresult.push_back(channel->name);
228 virtual void RemoveMode(User*, irc::modestacker* stack)
230 /* Listmodes dont get set on users */
233 /** Perform a rehash of this mode's configuration data
235 virtual void DoRehash()
237 ConfigReader Conf(ServerInstance);
241 for (int i = 0; i < Conf.Enumerate(configtag); i++)
243 // For each <banlist> tag
245 limit.mask = Conf.ReadValue(configtag, "chan", i);
246 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
248 if (limit.mask.size() && limit.limit > 0)
249 chanlimits.push_back(limit);
251 if (chanlimits.size() == 0)
256 chanlimits.push_back(limit);
260 /** Populate the Implements list with the correct events for a List Mode
262 virtual void DoImplements(Module* m)
264 Implementation eventlist[] = { I_OnChannelDelete, I_OnSyncChannel, I_OnCleanup, I_OnRehash, I_OnRequest };
265 ServerInstance->Modules->Attach(eventlist, m, 5);
268 /** Handle the list mode.
271 virtual ModeAction OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding, bool servermode)
273 // Try and grab the list
275 channel->GetExt(infokey, el);
279 // If there was no list
284 channel->Extend(infokey, el);
289 ModeParser::CleanMask(parameter);
291 // Check if the item already exists in the list
292 for (modelist::iterator it = el->begin(); it != el->end(); it++)
294 if (parameter == it->mask)
296 /* Give a subclass a chance to error about this */
297 TellAlreadyOnList(source, channel, parameter);
299 // it does, deny the change
300 return MODEACTION_DENY;
304 unsigned int maxsize = 0;
306 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
308 if (InspIRCd::Match(channel->name, it->mask))
310 // We have a pattern matching the channel...
311 maxsize = el->size();
312 if (IS_LOCAL(source) || (maxsize < it->limit))
314 /* Ok, it *could* be allowed, now give someone subclassing us
315 * a chance to validate the parameter.
316 * The param is passed by reference, so they can both modify it
317 * and tell us if we allow it or not.
319 * eg, the subclass could:
321 * 2) 'fix' parameter and then allow
324 if (ValidateParam(source, channel, parameter))
326 // And now add the mask onto the list...
329 e.nick = servermode ? ServerInstance->Config->ServerName : source->nick;
330 e.time = stringtime(ServerInstance);
333 return MODEACTION_ALLOW;
337 /* If they deny it they have the job of giving an error message */
338 return MODEACTION_DENY;
344 /* List is full, give subclass a chance to send a custom message */
345 if (!TellListTooLong(source, channel, parameter))
347 source->WriteNumeric(478, "%s %s %s :Channel ban/ignore list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
351 return MODEACTION_DENY;
355 // We're taking the mode off
358 for (modelist::iterator it = el->begin(); it != el->end(); it++)
360 if (parameter == it->mask)
365 channel->Shrink(infokey);
368 return MODEACTION_ALLOW;
371 /* Tried to remove something that wasn't set */
372 TellNotSet(source, channel, parameter);
374 return MODEACTION_DENY;
378 /* Hmm, taking an exception off a non-existant list, DIE */
379 TellNotSet(source, channel, parameter);
381 return MODEACTION_DENY;
384 return MODEACTION_DENY;
387 /** Get Extensible key for this mode
389 virtual std::string& GetInfoKey()
394 /** Handle channel deletion.
396 * @param chan Channel being deleted
398 virtual void DoChannelDelete(Channel* chan)
401 chan->GetExt(infokey, mlist);
405 chan->Shrink(infokey);
410 /** Syncronize channel item list with another server.
412 * @param chan Channel to syncronize
413 * @param proto Protocol module pointer
414 * @param opaque Opaque connection handle
416 virtual void DoSyncChannel(Channel* chan, Module* proto, void* opaque)
419 chan->GetExt(infokey, mlist);
420 irc::modestacker modestack(ServerInstance, true);
421 std::vector<std::string> stackresult;
422 std::vector<TranslateType> types;
423 types.push_back(TR_TEXT);
426 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
428 modestack.Push(std::string(1, mode)[0], it->mask);
431 while (modestack.GetStackedLine(stackresult))
433 types.assign(stackresult.size(), this->GetTranslateType());
434 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
439 /** Clean up module on unload
440 * @param target_type Type of target to clean
441 * @param item Item to clean
443 virtual void DoCleanup(int, void*)
447 /** Validate parameters.
448 * Overridden by implementing module.
449 * @param source Source user adding the parameter
450 * @param channel Channel the parameter is being added to
451 * @param parameter The actual parameter being added
452 * @return true if the parameter is valid
454 virtual bool ValidateParam(User*, Channel*, std::string&)
459 /** Tell the user the list is too long.
460 * Overridden by implementing module.
461 * @param source Source user adding the parameter
462 * @param channel Channel the parameter is being added to
463 * @param parameter The actual parameter being added
466 virtual bool TellListTooLong(User*, Channel*, std::string&)
471 /** Tell the user an item is already on the list.
472 * Overridden by implementing module.
473 * @param source Source user adding the parameter
474 * @param channel Channel the parameter is being added to
475 * @param parameter The actual parameter being added
477 virtual void TellAlreadyOnList(User*, Channel*, std::string&)
481 /** Tell the user that the parameter is not in the list.
482 * Overridden by implementing module.
483 * @param source Source user removing the parameter
484 * @param channel Channel the parameter is being removed from
485 * @param parameter The actual parameter being removed
487 virtual void TellNotSet(User*, Channel*, std::string&)
491 virtual const char* DoOnRequest(Request* request)
493 ListModeRequest* LM = (ListModeRequest*)request;
494 if (strcmp("LM_CHECKLIST", request->GetId()) == 0)
497 LM->chan->GetExt(GetInfoKey(), mlist);
500 std::string mask = LM->user->nick + "!" + LM->user->ident + "@" + LM->user->GetIPString();
501 for (modelist::iterator it = mlist->begin(); it != mlist->end(); ++it)
503 if (InspIRCd::Match(LM->user->GetFullRealHost(), it->mask) || InspIRCd::Match(LM->user->GetFullHost(), it->mask) || (InspIRCd::MatchCIDR(mask, it->mask)))
504 return it->mask.c_str();
509 else if (strcmp("LM_CHECKLIST_EX", request->GetId()) == 0)
512 LM->chan->GetExt(GetInfoKey(), mlist);
518 LM->literal = LM->user->nick + "!" + LM->user->ident + "@" + LM->user->GetIPString();
521 for (modelist::iterator it = mlist->begin(); it != mlist->end(); it++)
523 if (LM->extban && it->mask.length() > 1 && it->mask[0] == LM->extban && it->mask[1] == ':')
525 std::string ext = it->mask.substr(2);
528 if (InspIRCd::Match(LM->user->GetFullRealHost(), ext) || InspIRCd::Match(LM->user->GetFullHost(), ext) || (InspIRCd::MatchCIDR(LM->literal, ext)))
530 return it->mask.c_str();
533 else if (InspIRCd::Match(LM->literal, ext))
535 return it->mask.c_str();
542 if (InspIRCd::Match(LM->user->GetFullRealHost(), it->mask) || InspIRCd::Match(LM->user->GetFullHost(), it->mask) || (InspIRCd::MatchCIDR(LM->literal, it->mask)))
544 return it->mask.c_str();
547 else if (InspIRCd::Match(LM->literal, it->mask))
549 return it->mask.c_str();