1 #ifndef INSPIRCD_LISTMODE_PROVIDER
2 #define INSPIRCD_LISTMODE_PROVIDER
11 #include "helperfuncs.h"
13 /* $ModDesc: Provides support for easily creating listmodes, stores the time set, the user, and a parameter. */
15 /* Updated to use the <banlist> config tag if it exists */
16 /* Written by Om <omster@gmail.com>, December 2005. */
17 /* Based on code previously written by Om - April 2005 */
18 /* Updated to new API July 8th 2006 by Brain */
19 /* Originally based on m_chanprotect and m_silence */
21 inline std::string stringtime()
23 std::ostringstream TIME;
43 // Just defining the type we use for the exception list here...
44 typedef std::vector<ListItem> modelist;
45 typedef std::vector<ListLimit> limitlist;
47 class ListModeBase : public ModeHandler
53 std::string listnumeric;
54 std::string endoflistnumeric;
55 std::string endofliststring;
56 std::string configtag;
60 ListModeBase(Server* serv, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, const std::string &ctag = "banlist")
61 : ModeHandler(modechar, 1, 1, true, MODETYPE_CHANNEL, false), Srv(serv), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), configtag(ctag)
64 infokey = "exceptionbase_mode_" + std::string(1, mode) + "_list";
67 virtual void DisplayList(userrec* user, chanrec* channel)
69 modelist* el = (modelist*)channel->GetExt(infokey);
72 for(modelist::iterator it = el->begin(); it != el->end(); it++)
74 WriteServ(user->fd, "%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());
77 WriteServ(user->fd, "%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
80 virtual void DoRehash()
86 for(int i = 0; i < Conf.Enumerate(configtag); i++)
88 // For each <banlist> tag
90 limit.mask = Conf.ReadValue(configtag, "chan", i);
91 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
93 if(limit.mask.size() && limit.limit > 0)
95 chanlimits.push_back(limit);
96 log(DEBUG, "Read channel listmode limit of %u for mask '%s'", limit.limit, limit.mask.c_str());
100 log(DEBUG, "Invalid tag");
103 if(chanlimits.size() == 0)
108 chanlimits.push_back(limit);
112 virtual void DoImplements(char* List)
114 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
117 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
119 // Try and grab the list
120 modelist* el = (modelist*)channel->GetExt(infokey);
124 // If there was no list
129 channel->Extend(infokey, el);
133 ModeParser::CleanMask(parameter);
135 // Check if the item already exists in the list
136 for (modelist::iterator it = el->begin(); it != el->end(); it++)
138 if(parameter == it->mask)
140 /* Give a subclass a chance to error about this */
141 TellAlreadyOnList(source, channel, parameter);
143 // it does, deny the change
144 return MODEACTION_DENY;
148 unsigned int maxsize = 0;
150 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
152 if (Srv->MatchText(channel->name, it->mask))
154 // We have a pattern matching the channel...
155 maxsize = el->size();
156 if (maxsize < it->limit)
158 /* Ok, it *could* be allowed, now give someone subclassing us
159 * a chance to validate the parameter.
160 * The param is passed by reference, so they can both modify it
161 * and tell us if we allow it or not.
163 * eg, the subclass could:
165 * 2) 'fix' parameter and then allow
168 if(ValidateParam(source, channel, parameter))
170 // And now add the mask onto the list...
173 e.nick = source->nick;
174 e.time = stringtime();
177 return MODEACTION_ALLOW;
181 /* If they deny it they have the job of giving an error message */
182 return MODEACTION_DENY;
188 /* List is full, give subclass a chance to send a custom message */
189 if(!TellListTooLong(source, channel, parameter))
191 WriteServ(source->fd, "478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
195 return MODEACTION_DENY;
199 // We're taking the mode off
202 for (modelist::iterator it = el->begin(); it != el->end(); it++)
204 if(parameter == it->mask)
209 channel->Shrink(infokey);
212 return MODEACTION_ALLOW;
216 /* Tried to remove something that wasn't set */
217 TellNotSet(source, channel, parameter);
221 return MODEACTION_DENY;
225 // Hmm, taking an exception off a non-existant list, DIE
227 return MODEACTION_DENY;
230 return MODEACTION_DENY;
233 virtual std::string& GetInfoKey()
238 virtual void DoChannelDelete(chanrec* chan)
240 modelist* list = (modelist*)chan->GetExt(infokey);
244 chan->Shrink(infokey);
249 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
251 modelist* list = (modelist*)chan->GetExt(infokey);
254 for (modelist::iterator it = list->begin(); it != list->end(); it++)
256 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
261 virtual void DoCleanup(int target_type, void* item)
263 if (target_type == TYPE_CHANNEL)
265 chanrec* chan = (chanrec*)item;
267 modelist* list = (modelist*)chan->GetExt(infokey);
271 chan->Shrink(infokey);
277 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
282 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
287 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
291 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)