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 /** The base class for listmodes defined by u_listmode.h
67 class ListModeBase : public ModeHandler
71 std::string listnumeric;
72 std::string endoflistnumeric;
73 std::string endofliststring;
75 std::string configtag;
79 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
80 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
83 infokey = "listbase_mode_" + std::string(1, mode) + "_list";
86 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
89 channel->GetExt(infokey, el);
92 for (modelist::iterator it = el->begin(); it != el->end(); it++)
94 if(parameter == it->mask)
96 return std::make_pair(true, parameter);
100 return std::make_pair(false, parameter);
103 virtual void DisplayList(userrec* user, chanrec* channel)
106 channel->GetExt(infokey, el);
109 for(modelist::iterator it = el->begin(); it != el->end(); it++)
111 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());
114 user->WriteServ("%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
117 virtual void RemoveMode(chanrec* channel)
119 ServerInstance->Log(DEBUG,"Removing listmode base from %s %s",channel->name,infokey.c_str());
121 channel->GetExt(infokey, el);
124 ServerInstance->Log(DEBUG,"Channel is extended with a list");
125 irc::modestacker modestack(false);
126 std::deque<std::string> stackresult;
127 const char* mode_junk[MAXMODES+1];
128 mode_junk[0] = channel->name;
129 userrec* n = new userrec(ServerInstance);
130 n->SetFd(FD_MAGIC_NUMBER);
131 for(modelist::iterator it = el->begin(); it != el->end(); it++)
133 modestack.Push(this->GetModeChar(), it->mask);
135 while (modestack.GetStackedLine(stackresult))
137 for (size_t j = 0; j < stackresult.size(); j++)
139 mode_junk[j+1] = stackresult[j].c_str();
141 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, n);
148 virtual void RemoveMode(userrec* user)
150 /* Listmodes dont get set on users */
153 virtual void DoRehash()
155 ConfigReader Conf(ServerInstance);
159 for(int i = 0; i < Conf.Enumerate(configtag); i++)
161 // For each <banlist> tag
163 limit.mask = Conf.ReadValue(configtag, "chan", i);
164 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
166 if(limit.mask.size() && limit.limit > 0)
167 chanlimits.push_back(limit);
169 if(chanlimits.size() == 0)
174 chanlimits.push_back(limit);
178 virtual void DoImplements(char* List)
180 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
183 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
185 // Try and grab the list
187 channel->GetExt(infokey, el);
191 // If there was no list
196 channel->Extend(infokey, el);
201 ModeParser::CleanMask(parameter);
203 // Check if the item already exists in the list
204 for (modelist::iterator it = el->begin(); it != el->end(); it++)
206 if(parameter == it->mask)
208 /* Give a subclass a chance to error about this */
209 TellAlreadyOnList(source, channel, parameter);
211 // it does, deny the change
212 return MODEACTION_DENY;
216 unsigned int maxsize = 0;
218 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
220 if (match(channel->name, it->mask.c_str()))
222 // We have a pattern matching the channel...
223 maxsize = el->size();
224 if (maxsize < it->limit)
226 /* Ok, it *could* be allowed, now give someone subclassing us
227 * a chance to validate the parameter.
228 * The param is passed by reference, so they can both modify it
229 * and tell us if we allow it or not.
231 * eg, the subclass could:
233 * 2) 'fix' parameter and then allow
236 if(ValidateParam(source, channel, parameter))
238 // And now add the mask onto the list...
241 e.nick = source->nick;
242 e.time = stringtime();
245 return MODEACTION_ALLOW;
249 /* If they deny it they have the job of giving an error message */
250 return MODEACTION_DENY;
256 /* List is full, give subclass a chance to send a custom message */
257 if(!TellListTooLong(source, channel, parameter))
259 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
263 return MODEACTION_DENY;
267 // We're taking the mode off
270 for (modelist::iterator it = el->begin(); it != el->end(); it++)
272 if(parameter == it->mask)
277 channel->Shrink(infokey);
280 return MODEACTION_ALLOW;
283 /* Tried to remove something that wasn't set */
284 TellNotSet(source, channel, parameter);
286 return MODEACTION_DENY;
290 /* Hmm, taking an exception off a non-existant list, DIE */
291 TellNotSet(source, channel, parameter);
293 return MODEACTION_DENY;
296 return MODEACTION_DENY;
299 virtual std::string& GetInfoKey()
304 virtual void DoChannelDelete(chanrec* chan)
307 chan->GetExt(infokey, list);
311 chan->Shrink(infokey);
316 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
319 chan->GetExt(infokey, list);
320 irc::modestacker modestack(true);
321 std::deque<std::string> stackresult;
324 for (modelist::iterator it = list->begin(); it != list->end(); it++)
326 modestack.Push(std::string(1, mode)[0], it->mask);
329 while (modestack.GetStackedLine(stackresult))
331 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
332 std::string line = mode_join.GetJoined();
333 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
337 virtual void DoCleanup(int target_type, void* item)
341 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
346 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
351 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
355 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)