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)
71 channel->GetExt(infokey, el);
74 for (modelist::iterator it = el->begin(); it != el->end(); it++)
76 if(parameter == it->mask)
78 return std::make_pair(true, parameter);
82 return std::make_pair(false, parameter);
85 virtual void DisplayList(userrec* user, chanrec* channel)
88 channel->GetExt(infokey, el);
91 for(modelist::iterator it = el->begin(); it != el->end(); it++)
93 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());
96 user->WriteServ( "%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
99 virtual void DoRehash()
101 ConfigReader Conf(ServerInstance);
105 for(int i = 0; i < Conf.Enumerate(configtag); i++)
107 // For each <banlist> tag
109 limit.mask = Conf.ReadValue(configtag, "chan", i);
110 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
112 if(limit.mask.size() && limit.limit > 0)
113 chanlimits.push_back(limit);
115 if(chanlimits.size() == 0)
120 chanlimits.push_back(limit);
124 virtual void DoImplements(char* List)
126 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
129 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
131 // Try and grab the list
133 channel->GetExt(infokey, el);
137 // If there was no list
142 channel->Extend(infokey, el);
147 ModeParser::CleanMask(parameter);
149 // Check if the item already exists in the list
150 for (modelist::iterator it = el->begin(); it != el->end(); it++)
152 if(parameter == it->mask)
154 /* Give a subclass a chance to error about this */
155 TellAlreadyOnList(source, channel, parameter);
157 // it does, deny the change
158 return MODEACTION_DENY;
162 unsigned int maxsize = 0;
164 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
166 if (match(channel->name, it->mask.c_str()))
168 // We have a pattern matching the channel...
169 maxsize = el->size();
170 if (maxsize < it->limit)
172 /* Ok, it *could* be allowed, now give someone subclassing us
173 * a chance to validate the parameter.
174 * The param is passed by reference, so they can both modify it
175 * and tell us if we allow it or not.
177 * eg, the subclass could:
179 * 2) 'fix' parameter and then allow
182 if(ValidateParam(source, channel, parameter))
184 // And now add the mask onto the list...
187 e.nick = source->nick;
188 e.time = stringtime();
191 return MODEACTION_ALLOW;
195 /* If they deny it they have the job of giving an error message */
196 return MODEACTION_DENY;
202 /* List is full, give subclass a chance to send a custom message */
203 if(!TellListTooLong(source, channel, parameter))
205 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
209 return MODEACTION_DENY;
213 // We're taking the mode off
216 for (modelist::iterator it = el->begin(); it != el->end(); it++)
218 if(parameter == it->mask)
223 channel->Shrink(infokey);
226 return MODEACTION_ALLOW;
230 /* Tried to remove something that wasn't set */
231 TellNotSet(source, channel, parameter);
235 return MODEACTION_DENY;
239 // Hmm, taking an exception off a non-existant list, DIE
241 return MODEACTION_DENY;
244 return MODEACTION_DENY;
247 virtual std::string& GetInfoKey()
252 virtual void DoChannelDelete(chanrec* chan)
255 chan->GetExt(infokey, list);
259 chan->Shrink(infokey);
264 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
267 chan->GetExt(infokey, list);
270 for (modelist::iterator it = list->begin(); it != list->end(); it++)
272 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
277 virtual void DoCleanup(int target_type, void* item)
279 if (target_type == TYPE_CHANNEL)
281 chanrec* chan = (chanrec*)item;
284 chan->GetExt(infokey, list);
288 chan->Shrink(infokey);
294 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
299 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
304 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
308 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)