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, const std::string &ctag = "banlist")
61 : ModeHandler(modechar, 1, 1, true, MODETYPE_CHANNEL, false), Srv(serv), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), 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, (char*)el);
133 ModeParser::CleanMask(parameter);
135 // Check if the item already exists in the list
136 for (modelist::iterator it = el->begin(); it != el->end(); it++)
138 if(parameter == it->mask)
140 // it does, deny the change
142 return MODEACTION_DENY;
146 unsigned int maxsize = 0;
148 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
150 if (Srv->MatchText(channel->name, it->mask))
152 // We have a pattern matching the channel...
153 maxsize = el->size();
154 if (maxsize < it->limit)
156 // And now add the mask onto the list...
159 e.nick = source->nick;
160 e.time = stringtime();
163 return MODEACTION_ALLOW;
169 WriteServ(source->fd, "478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
171 return MODEACTION_DENY;
175 // We're taking the mode off
178 for (modelist::iterator it = el->begin(); it != el->end(); it++)
180 if(parameter == it->mask)
185 channel->Shrink(infokey);
188 return MODEACTION_ALLOW;
192 return MODEACTION_DENY;
196 // Hmm, taking an exception off a non-existant list, DIE
198 return MODEACTION_DENY;
201 return MODEACTION_DENY;
204 virtual std::string& GetInfoKey()
209 virtual void DoChannelDelete(chanrec* chan)
211 modelist* list = (modelist*)chan->GetExt(infokey);
215 chan->Shrink(infokey);
220 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
222 modelist* list = (modelist*)chan->GetExt(infokey);
225 for (modelist::iterator it = list->begin(); it != list->end(); it++)
227 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
232 virtual void DoCleanup(int target_type, void* item)
234 if (target_type == TYPE_CHANNEL)
236 chanrec* chan = (chanrec*)item;
238 modelist* list = (modelist*)chan->GetExt(infokey);
242 chan->Shrink(infokey);