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)
70 channel->GetExt(infokey, el);
73 for (modelist::iterator it = el->begin(); it != el->end(); it++)
75 if(parameter == it->mask)
77 return std::make_pair(true, parameter);
81 return std::make_pair(false, parameter);
84 virtual void DisplayList(userrec* user, chanrec* channel)
87 channel->GetExt(infokey, el);
90 for(modelist::iterator it = el->begin(); it != el->end(); it++)
92 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());
95 WriteServ(user->fd, "%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
98 virtual void DoRehash()
104 for(int i = 0; i < Conf.Enumerate(configtag); i++)
106 // For each <banlist> tag
108 limit.mask = Conf.ReadValue(configtag, "chan", i);
109 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
111 if(limit.mask.size() && limit.limit > 0)
113 chanlimits.push_back(limit);
114 log(DEBUG, "Read channel listmode limit of %u for mask '%s'", limit.limit, limit.mask.c_str());
118 log(DEBUG, "Invalid tag");
121 if(chanlimits.size() == 0)
126 chanlimits.push_back(limit);
130 virtual void DoImplements(char* List)
132 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
135 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
137 // Try and grab the list
139 channel->GetExt(infokey, el);
143 // If there was no list
148 channel->Extend(infokey, el);
153 ModeParser::CleanMask(parameter);
155 // Check if the item already exists in the list
156 for (modelist::iterator it = el->begin(); it != el->end(); it++)
158 if(parameter == it->mask)
160 /* Give a subclass a chance to error about this */
161 TellAlreadyOnList(source, channel, parameter);
163 // it does, deny the change
164 return MODEACTION_DENY;
168 unsigned int maxsize = 0;
170 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
172 if (Srv->MatchText(channel->name, it->mask))
174 // We have a pattern matching the channel...
175 maxsize = el->size();
176 if (maxsize < it->limit)
178 /* Ok, it *could* be allowed, now give someone subclassing us
179 * a chance to validate the parameter.
180 * The param is passed by reference, so they can both modify it
181 * and tell us if we allow it or not.
183 * eg, the subclass could:
185 * 2) 'fix' parameter and then allow
188 if(ValidateParam(source, channel, parameter))
190 // And now add the mask onto the list...
193 e.nick = source->nick;
194 e.time = stringtime();
197 return MODEACTION_ALLOW;
201 /* If they deny it they have the job of giving an error message */
202 return MODEACTION_DENY;
208 /* List is full, give subclass a chance to send a custom message */
209 if(!TellListTooLong(source, channel, parameter))
211 WriteServ(source->fd, "478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
215 return MODEACTION_DENY;
219 // We're taking the mode off
222 for (modelist::iterator it = el->begin(); it != el->end(); it++)
224 if(parameter == it->mask)
229 channel->Shrink(infokey);
232 return MODEACTION_ALLOW;
236 /* Tried to remove something that wasn't set */
237 TellNotSet(source, channel, parameter);
241 return MODEACTION_DENY;
245 // Hmm, taking an exception off a non-existant list, DIE
247 return MODEACTION_DENY;
250 return MODEACTION_DENY;
253 virtual std::string& GetInfoKey()
258 virtual void DoChannelDelete(chanrec* chan)
261 chan->GetExt(infokey, list);
265 chan->Shrink(infokey);
270 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
273 chan->GetExt(infokey, list);
276 for (modelist::iterator it = list->begin(); it != list->end(); it++)
278 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
283 virtual void DoCleanup(int target_type, void* item)
285 if (target_type == TYPE_CHANNEL)
287 chanrec* chan = (chanrec*)item;
290 chan->GetExt(infokey, list);
294 chan->Shrink(infokey);
300 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
305 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
310 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
314 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)