1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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
27 /* $ModDesc: Provides support for easily creating listmodes, stores the time set, the user, and a parameter. */
29 /* Updated to use the <banlist> config tag if it exists */
30 /* Written by Om <omster@gmail.com>, December 2005. */
31 /* Based on code previously written by Om - April 2005 */
32 /* Updated to new API July 8th 2006 by Brain */
33 /* Originally based on m_chanprotect and m_silence */
35 inline std::string stringtime()
37 std::ostringstream TIME;
42 /** An item in a listmode's list
44 class ListItem : public classbase
52 /** The number of items a listmode's list may contain
54 class ListLimit : public classbase
61 // Just defining the type we use for the exception list here...
62 typedef std::vector<ListItem> modelist;
63 typedef std::vector<ListLimit> limitlist;
65 class ListModeRequest : public Request
71 ListModeRequest(Module* sender, Module* target, userrec* u, chanrec* c) : Request(sender, target, "LM_CHECKLIST"), user(u), chan(c)
80 /** The base class for listmodes defined by u_listmode.h
82 class ListModeBase : public ModeHandler
86 std::string listnumeric;
87 std::string endoflistnumeric;
88 std::string endofliststring;
90 std::string configtag;
94 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
95 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
98 infokey = "listbase_mode_" + std::string(1, mode) + "_list";
101 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
104 channel->GetExt(infokey, el);
105 irc::string csp(assign(parameter));
108 for (modelist::iterator it = el->begin(); it != el->end(); it++)
112 return std::make_pair(true, parameter);
116 return std::make_pair(false, parameter);
119 virtual void DisplayList(userrec* user, chanrec* channel)
122 channel->GetExt(infokey, el);
125 for(modelist::iterator it = el->begin(); it != el->end(); it++)
127 user->WriteServ("%s %s %s %s %s %s", listnumeric.c_str(), user->nick, channel->name, it->mask.c_str(), it->nick.c_str(), it->time.c_str());
130 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
133 virtual void RemoveMode(chanrec* channel)
135 ServerInstance->Log(DEBUG,"Removing listmode base from %s %s",channel->name,infokey.c_str());
137 channel->GetExt(infokey, el);
140 ServerInstance->Log(DEBUG,"Channel is extended with a list");
141 irc::modestacker modestack(false);
142 std::deque<std::string> stackresult;
143 const char* mode_junk[MAXMODES+1];
144 mode_junk[0] = channel->name;
145 userrec* n = new userrec(ServerInstance);
146 n->SetFd(FD_MAGIC_NUMBER);
147 for(modelist::iterator it = el->begin(); it != el->end(); it++)
149 modestack.Push(this->GetModeChar(), assign(it->mask));
151 while (modestack.GetStackedLine(stackresult))
153 for (size_t j = 0; j < stackresult.size(); j++)
155 mode_junk[j+1] = stackresult[j].c_str();
157 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, n);
164 virtual void RemoveMode(userrec* user)
166 /* Listmodes dont get set on users */
169 virtual void DoRehash()
171 ConfigReader Conf(ServerInstance);
175 for(int i = 0; i < Conf.Enumerate(configtag); i++)
177 // For each <banlist> tag
179 limit.mask = Conf.ReadValue(configtag, "chan", i);
180 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
182 if(limit.mask.size() && limit.limit > 0)
183 chanlimits.push_back(limit);
185 if(chanlimits.size() == 0)
190 chanlimits.push_back(limit);
194 virtual void DoImplements(char* List)
196 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
199 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
201 // Try and grab the list
203 channel->GetExt(infokey, el);
207 // If there was no list
212 channel->Extend(infokey, el);
217 ModeParser::CleanMask(parameter);
219 // Check if the item already exists in the list
220 for (modelist::iterator it = el->begin(); it != el->end(); it++)
222 if(parameter == it->mask)
224 /* Give a subclass a chance to error about this */
225 TellAlreadyOnList(source, channel, parameter);
227 // it does, deny the change
228 return MODEACTION_DENY;
232 unsigned int maxsize = 0;
234 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
236 if (match(channel->name, it->mask.c_str()))
238 // We have a pattern matching the channel...
239 maxsize = el->size();
240 if (maxsize < it->limit)
242 /* Ok, it *could* be allowed, now give someone subclassing us
243 * a chance to validate the parameter.
244 * The param is passed by reference, so they can both modify it
245 * and tell us if we allow it or not.
247 * eg, the subclass could:
249 * 2) 'fix' parameter and then allow
252 if(ValidateParam(source, channel, parameter))
254 // And now add the mask onto the list...
256 e.mask = assign(parameter);
257 e.nick = source->nick;
258 e.time = stringtime();
261 return MODEACTION_ALLOW;
265 /* If they deny it they have the job of giving an error message */
266 return MODEACTION_DENY;
272 /* List is full, give subclass a chance to send a custom message */
273 if(!TellListTooLong(source, channel, parameter))
275 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
279 return MODEACTION_DENY;
283 // We're taking the mode off
286 for (modelist::iterator it = el->begin(); it != el->end(); it++)
288 if(parameter == it->mask)
293 channel->Shrink(infokey);
296 return MODEACTION_ALLOW;
299 /* Tried to remove something that wasn't set */
300 TellNotSet(source, channel, parameter);
302 return MODEACTION_DENY;
306 /* Hmm, taking an exception off a non-existant list, DIE */
307 TellNotSet(source, channel, parameter);
309 return MODEACTION_DENY;
312 return MODEACTION_DENY;
315 virtual std::string& GetInfoKey()
320 virtual void DoChannelDelete(chanrec* chan)
323 chan->GetExt(infokey, list);
327 chan->Shrink(infokey);
332 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
335 chan->GetExt(infokey, list);
336 irc::modestacker modestack(true);
337 std::deque<std::string> stackresult;
340 for (modelist::iterator it = list->begin(); it != list->end(); it++)
342 modestack.Push(std::string(1, mode)[0], assign(it->mask));
345 while (modestack.GetStackedLine(stackresult))
347 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
348 std::string line = mode_join.GetJoined();
349 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
353 virtual void DoCleanup(int target_type, void* item)
357 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
362 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
367 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
371 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)