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, 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 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);
134 ModeParser::CleanMask(parameter);
136 // Check if the item already exists in the list
137 for (modelist::iterator it = el->begin(); it != el->end(); it++)
139 if(parameter == it->mask)
141 /* Give a subclass a chance to error about this */
142 TellAlreadyOnList(source, channel, parameter);
144 // it does, deny the change
145 return MODEACTION_DENY;
149 unsigned int maxsize = 0;
151 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
153 if (Srv->MatchText(channel->name, it->mask))
155 // We have a pattern matching the channel...
156 maxsize = el->size();
157 if (maxsize < it->limit)
159 /* Ok, it *could* be allowed, now give someone subclassing us
160 * a chance to validate the parameter.
161 * The param is passed by reference, so they can both modify it
162 * and tell us if we allow it or not.
164 * eg, the subclass could:
166 * 2) 'fix' parameter and then allow
169 if(ValidateParam(source, channel, parameter))
171 // And now add the mask onto the list...
174 e.nick = source->nick;
175 e.time = stringtime();
178 return MODEACTION_ALLOW;
182 /* If they deny it they have the job of giving an error message */
183 return MODEACTION_DENY;
189 /* List is full, give subclass a chance to send a custom message */
190 if(!TellListTooLong(source, channel, parameter))
192 WriteServ(source->fd, "478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
196 return MODEACTION_DENY;
200 // We're taking the mode off
203 for (modelist::iterator it = el->begin(); it != el->end(); it++)
205 if(parameter == it->mask)
210 channel->Shrink(infokey);
213 return MODEACTION_ALLOW;
217 /* Tried to remove something that wasn't set */
218 TellNotSet(source, channel, parameter);
222 return MODEACTION_DENY;
226 // Hmm, taking an exception off a non-existant list, DIE
228 return MODEACTION_DENY;
231 return MODEACTION_DENY;
234 virtual std::string& GetInfoKey()
239 virtual void DoChannelDelete(chanrec* chan)
241 modelist* list = (modelist*)chan->GetExt(infokey);
245 chan->Shrink(infokey);
250 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
252 modelist* list = (modelist*)chan->GetExt(infokey);
255 for (modelist::iterator it = list->begin(); it != list->end(); it++)
257 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
262 virtual void DoCleanup(int target_type, void* item)
264 if (target_type == TYPE_CHANNEL)
266 chanrec* chan = (chanrec*)item;
268 modelist* list = (modelist*)chan->GetExt(infokey);
272 chan->Shrink(infokey);
278 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
283 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
288 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
292 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)