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;
28 class ListItem : public classbase
36 class ListLimit : public classbase
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
52 std::string listnumeric;
53 std::string endoflistnumeric;
54 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, bool autotidy, const std::string &ctag = "banlist")
61 : ModeHandler(modechar, 1, 1, true, MODETYPE_CHANNEL, false), Srv(serv), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
64 infokey = "exceptionbase_mode_" + std::string(1, mode) + "_list";
67 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
69 log(DEBUG,"ListMode: ModeSet()");
71 channel->GetExt(infokey, el);
74 for (modelist::iterator it = el->begin(); it != el->end(); it++)
76 if(parameter == it->mask)
78 log(DEBUG,"Found item %s",parameter.c_str());
79 return std::make_pair(true, parameter);
83 log(DEBUG,"Didnt find item %s",parameter.c_str());
84 return std::make_pair(false, parameter);
87 virtual void DisplayList(userrec* user, chanrec* channel)
90 channel->GetExt(infokey, el);
93 for(modelist::iterator it = el->begin(); it != el->end(); it++)
95 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());
98 user->WriteServ( "%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
101 virtual void DoRehash()
107 for(int i = 0; i < Conf.Enumerate(configtag); i++)
109 // For each <banlist> tag
111 limit.mask = Conf.ReadValue(configtag, "chan", i);
112 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
114 if(limit.mask.size() && limit.limit > 0)
116 chanlimits.push_back(limit);
117 log(DEBUG, "Read channel listmode limit of %u for mask '%s'", limit.limit, limit.mask.c_str());
121 log(DEBUG, "Invalid tag");
124 if(chanlimits.size() == 0)
129 chanlimits.push_back(limit);
133 virtual void DoImplements(char* List)
135 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
138 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
140 // Try and grab the list
142 channel->GetExt(infokey, el);
146 // If there was no list
151 channel->Extend(infokey, el);
156 ModeParser::CleanMask(parameter);
158 // Check if the item already exists in the list
159 for (modelist::iterator it = el->begin(); it != el->end(); it++)
161 if(parameter == it->mask)
163 /* Give a subclass a chance to error about this */
164 TellAlreadyOnList(source, channel, parameter);
166 // it does, deny the change
167 return MODEACTION_DENY;
171 unsigned int maxsize = 0;
173 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
175 if (Srv->MatchText(channel->name, it->mask))
177 // We have a pattern matching the channel...
178 maxsize = el->size();
179 if (maxsize < it->limit)
181 /* Ok, it *could* be allowed, now give someone subclassing us
182 * a chance to validate the parameter.
183 * The param is passed by reference, so they can both modify it
184 * and tell us if we allow it or not.
186 * eg, the subclass could:
188 * 2) 'fix' parameter and then allow
191 if(ValidateParam(source, channel, parameter))
193 // And now add the mask onto the list...
196 e.nick = source->nick;
197 e.time = stringtime();
200 return MODEACTION_ALLOW;
204 /* If they deny it they have the job of giving an error message */
205 return MODEACTION_DENY;
211 /* List is full, give subclass a chance to send a custom message */
212 if(!TellListTooLong(source, channel, parameter))
214 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
218 return MODEACTION_DENY;
222 // We're taking the mode off
225 for (modelist::iterator it = el->begin(); it != el->end(); it++)
227 if(parameter == it->mask)
232 channel->Shrink(infokey);
235 return MODEACTION_ALLOW;
239 /* Tried to remove something that wasn't set */
240 TellNotSet(source, channel, parameter);
244 return MODEACTION_DENY;
248 // Hmm, taking an exception off a non-existant list, DIE
250 return MODEACTION_DENY;
253 return MODEACTION_DENY;
256 virtual std::string& GetInfoKey()
261 virtual void DoChannelDelete(chanrec* chan)
264 chan->GetExt(infokey, list);
268 chan->Shrink(infokey);
273 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
276 chan->GetExt(infokey, list);
279 for (modelist::iterator it = list->begin(); it != list->end(); it++)
281 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
286 virtual void DoCleanup(int target_type, void* item)
288 if (target_type == TYPE_CHANNEL)
290 chanrec* chan = (chanrec*)item;
293 chan->GetExt(infokey, list);
297 chan->Shrink(infokey);
303 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
308 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
313 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
317 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)