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 virtual void DisplayList(userrec* user, chanrec* channel)
70 channel->GetExt(infokey, el);
73 for(modelist::iterator it = el->begin(); it != el->end(); it++)
75 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());
78 WriteServ(user->fd, "%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
81 virtual void DoRehash()
87 for(int i = 0; i < Conf.Enumerate(configtag); i++)
89 // For each <banlist> tag
91 limit.mask = Conf.ReadValue(configtag, "chan", i);
92 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
94 if(limit.mask.size() && limit.limit > 0)
96 chanlimits.push_back(limit);
97 log(DEBUG, "Read channel listmode limit of %u for mask '%s'", limit.limit, limit.mask.c_str());
101 log(DEBUG, "Invalid tag");
104 if(chanlimits.size() == 0)
109 chanlimits.push_back(limit);
113 virtual void DoImplements(char* List)
115 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
118 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
120 // Try and grab the list
122 channel->GetExt(infokey, el);
126 // If there was no list
131 channel->Extend(infokey, el);
136 ModeParser::CleanMask(parameter);
138 // Check if the item already exists in the list
139 for (modelist::iterator it = el->begin(); it != el->end(); it++)
141 if(parameter == it->mask)
143 /* Give a subclass a chance to error about this */
144 TellAlreadyOnList(source, channel, parameter);
146 // it does, deny the change
147 return MODEACTION_DENY;
151 unsigned int maxsize = 0;
153 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
155 if (Srv->MatchText(channel->name, it->mask))
157 // We have a pattern matching the channel...
158 maxsize = el->size();
159 if (maxsize < it->limit)
161 /* Ok, it *could* be allowed, now give someone subclassing us
162 * a chance to validate the parameter.
163 * The param is passed by reference, so they can both modify it
164 * and tell us if we allow it or not.
166 * eg, the subclass could:
168 * 2) 'fix' parameter and then allow
171 if(ValidateParam(source, channel, parameter))
173 // And now add the mask onto the list...
176 e.nick = source->nick;
177 e.time = stringtime();
180 return MODEACTION_ALLOW;
184 /* If they deny it they have the job of giving an error message */
185 return MODEACTION_DENY;
191 /* List is full, give subclass a chance to send a custom message */
192 if(!TellListTooLong(source, channel, parameter))
194 WriteServ(source->fd, "478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
198 return MODEACTION_DENY;
202 // We're taking the mode off
205 for (modelist::iterator it = el->begin(); it != el->end(); it++)
207 if(parameter == it->mask)
212 channel->Shrink(infokey);
215 return MODEACTION_ALLOW;
219 /* Tried to remove something that wasn't set */
220 TellNotSet(source, channel, parameter);
224 return MODEACTION_DENY;
228 // Hmm, taking an exception off a non-existant list, DIE
230 return MODEACTION_DENY;
233 return MODEACTION_DENY;
236 virtual std::string& GetInfoKey()
241 virtual void DoChannelDelete(chanrec* chan)
244 chan->GetExt(infokey, list);
248 chan->Shrink(infokey);
253 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
256 chan->GetExt(infokey, list);
259 for (modelist::iterator it = list->begin(); it != list->end(); it++)
261 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
266 virtual void DoCleanup(int target_type, void* item)
268 if (target_type == TYPE_CHANNEL)
270 chanrec* chan = (chanrec*)item;
273 chan->GetExt(infokey, list);
277 chan->Shrink(infokey);
283 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
288 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
293 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
297 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)