1 #ifndef INSPIRCD_LISTMODE_PROVIDER
2 #define INSPIRCD_LISTMODE_PROVIDER
11 #include "helperfuncs.h"
15 /* $ModDesc: Provides support for easily creating listmodes, stores the time set, the user, and a parameter. */
17 /* Updated to use the <banlist> config tag if it exists */
18 /* Written by Om <omster@gmail.com>, December 2005. */
19 /* Based on code previously written by Om - April 2005 */
20 /* Updated to new API July 8th 2006 by Brain */
21 /* Originally based on m_chanprotect and m_silence */
23 inline std::string stringtime()
25 std::ostringstream TIME;
30 class ListItem : public classbase
38 class ListLimit : public classbase
45 // Just defining the type we use for the exception list here...
46 typedef std::vector<ListItem> modelist;
47 typedef std::vector<ListLimit> limitlist;
49 class ListModeBase : public ModeHandler
53 std::string listnumeric;
54 std::string endoflistnumeric;
55 std::string endofliststring;
57 std::string configtag;
61 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
62 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
65 infokey = "exceptionbase_mode_" + std::string(1, mode) + "_list";
68 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
70 log(DEBUG,"ListMode: ModeSet()");
72 channel->GetExt(infokey, el);
75 for (modelist::iterator it = el->begin(); it != el->end(); it++)
77 if(parameter == it->mask)
79 log(DEBUG,"Found item %s",parameter.c_str());
80 return std::make_pair(true, parameter);
84 log(DEBUG,"Didnt find item %s",parameter.c_str());
85 return std::make_pair(false, parameter);
88 virtual void DisplayList(userrec* user, chanrec* channel)
91 channel->GetExt(infokey, el);
94 for(modelist::iterator it = el->begin(); it != el->end(); it++)
96 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());
99 user->WriteServ( "%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
102 virtual void DoRehash()
108 for(int i = 0; i < Conf.Enumerate(configtag); i++)
110 // For each <banlist> tag
112 limit.mask = Conf.ReadValue(configtag, "chan", i);
113 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
115 if(limit.mask.size() && limit.limit > 0)
117 chanlimits.push_back(limit);
118 log(DEBUG, "Read channel listmode limit of %u for mask '%s'", limit.limit, limit.mask.c_str());
122 log(DEBUG, "Invalid tag");
125 if(chanlimits.size() == 0)
130 chanlimits.push_back(limit);
134 virtual void DoImplements(char* List)
136 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
139 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
141 // Try and grab the list
143 channel->GetExt(infokey, el);
147 // If there was no list
152 channel->Extend(infokey, el);
157 ModeParser::CleanMask(parameter);
159 // Check if the item already exists in the list
160 for (modelist::iterator it = el->begin(); it != el->end(); it++)
162 if(parameter == it->mask)
164 /* Give a subclass a chance to error about this */
165 TellAlreadyOnList(source, channel, parameter);
167 // it does, deny the change
168 return MODEACTION_DENY;
172 unsigned int maxsize = 0;
174 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
176 if (match(channel->name, it->mask.c_str()))
178 // We have a pattern matching the channel...
179 maxsize = el->size();
180 if (maxsize < it->limit)
182 /* Ok, it *could* be allowed, now give someone subclassing us
183 * a chance to validate the parameter.
184 * The param is passed by reference, so they can both modify it
185 * and tell us if we allow it or not.
187 * eg, the subclass could:
189 * 2) 'fix' parameter and then allow
192 if(ValidateParam(source, channel, parameter))
194 // And now add the mask onto the list...
197 e.nick = source->nick;
198 e.time = stringtime();
201 return MODEACTION_ALLOW;
205 /* If they deny it they have the job of giving an error message */
206 return MODEACTION_DENY;
212 /* List is full, give subclass a chance to send a custom message */
213 if(!TellListTooLong(source, channel, parameter))
215 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
219 return MODEACTION_DENY;
223 // We're taking the mode off
226 for (modelist::iterator it = el->begin(); it != el->end(); it++)
228 if(parameter == it->mask)
233 channel->Shrink(infokey);
236 return MODEACTION_ALLOW;
240 /* Tried to remove something that wasn't set */
241 TellNotSet(source, channel, parameter);
245 return MODEACTION_DENY;
249 // Hmm, taking an exception off a non-existant list, DIE
251 return MODEACTION_DENY;
254 return MODEACTION_DENY;
257 virtual std::string& GetInfoKey()
262 virtual void DoChannelDelete(chanrec* chan)
265 chan->GetExt(infokey, list);
269 chan->Shrink(infokey);
274 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
277 chan->GetExt(infokey, list);
280 for (modelist::iterator it = list->begin(); it != list->end(); it++)
282 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
287 virtual void DoCleanup(int target_type, void* item)
289 if (target_type == TYPE_CHANNEL)
291 chanrec* chan = (chanrec*)item;
294 chan->GetExt(infokey, list);
298 chan->Shrink(infokey);
304 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
309 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
314 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
318 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)