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);
107 for (modelist::iterator it = el->begin(); it != el->end(); it++)
109 if(parameter == it->mask)
111 return std::make_pair(true, parameter);
115 return std::make_pair(false, parameter);
118 virtual void DisplayList(userrec* user, chanrec* channel)
121 channel->GetExt(infokey, el);
124 for (modelist::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
126 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());
129 user->WriteServ("%s %s %s :%s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
132 virtual void RemoveMode(chanrec* channel)
135 channel->GetExt(infokey, el);
138 irc::modestacker modestack(false);
139 std::deque<std::string> stackresult;
140 const char* mode_junk[MAXMODES+1];
141 mode_junk[0] = channel->name;
142 userrec* n = new userrec(ServerInstance);
143 n->SetFd(FD_MAGIC_NUMBER);
144 for (modelist::iterator it = el->begin(); it != el->end(); it++)
146 modestack.Push(this->GetModeChar(), assign(it->mask));
148 while (modestack.GetStackedLine(stackresult))
150 for (size_t j = 0; j < stackresult.size(); j++)
152 mode_junk[j+1] = stackresult[j].c_str();
154 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, n);
161 virtual void RemoveMode(userrec* user)
163 /* Listmodes dont get set on users */
166 virtual void DoRehash()
168 ConfigReader Conf(ServerInstance);
172 for (int i = 0; i < Conf.Enumerate(configtag); i++)
174 // For each <banlist> tag
176 limit.mask = Conf.ReadValue(configtag, "chan", i);
177 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
179 if (limit.mask.size() && limit.limit > 0)
180 chanlimits.push_back(limit);
182 if (chanlimits.size() == 0)
187 chanlimits.push_back(limit);
191 virtual void DoImplements(char* List)
193 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
196 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
198 // Try and grab the list
200 channel->GetExt(infokey, el);
204 // If there was no list
209 channel->Extend(infokey, el);
214 ModeParser::CleanMask(parameter);
216 // Check if the item already exists in the list
217 for (modelist::iterator it = el->begin(); it != el->end(); it++)
219 if (parameter == it->mask)
221 /* Give a subclass a chance to error about this */
222 TellAlreadyOnList(source, channel, parameter);
224 // it does, deny the change
225 return MODEACTION_DENY;
229 unsigned int maxsize = 0;
231 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
233 if (match(channel->name, it->mask.c_str()))
235 // We have a pattern matching the channel...
236 maxsize = el->size();
237 if (maxsize < it->limit)
239 /* Ok, it *could* be allowed, now give someone subclassing us
240 * a chance to validate the parameter.
241 * The param is passed by reference, so they can both modify it
242 * and tell us if we allow it or not.
244 * eg, the subclass could:
246 * 2) 'fix' parameter and then allow
249 if (ValidateParam(source, channel, parameter))
251 // And now add the mask onto the list...
253 e.mask = assign(parameter);
254 e.nick = source->nick;
255 e.time = stringtime();
258 return MODEACTION_ALLOW;
262 /* If they deny it they have the job of giving an error message */
263 return MODEACTION_DENY;
269 /* List is full, give subclass a chance to send a custom message */
270 if (!TellListTooLong(source, channel, parameter))
272 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
276 return MODEACTION_DENY;
280 // We're taking the mode off
283 for (modelist::iterator it = el->begin(); it != el->end(); it++)
285 if (parameter == it->mask)
290 channel->Shrink(infokey);
293 return MODEACTION_ALLOW;
296 /* Tried to remove something that wasn't set */
297 TellNotSet(source, channel, parameter);
299 return MODEACTION_DENY;
303 /* Hmm, taking an exception off a non-existant list, DIE */
304 TellNotSet(source, channel, parameter);
306 return MODEACTION_DENY;
309 return MODEACTION_DENY;
312 virtual std::string& GetInfoKey()
317 virtual void DoChannelDelete(chanrec* chan)
320 chan->GetExt(infokey, list);
324 chan->Shrink(infokey);
329 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
332 chan->GetExt(infokey, list);
333 irc::modestacker modestack(true);
334 std::deque<std::string> stackresult;
337 for (modelist::iterator it = list->begin(); it != list->end(); it++)
339 modestack.Push(std::string(1, mode)[0], assign(it->mask));
342 while (modestack.GetStackedLine(stackresult))
344 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
345 std::string line = mode_join.GetJoined();
346 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
350 virtual void DoCleanup(int target_type, void* item)
354 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
359 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
364 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
368 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)