1 #ifndef INSPIRCD_LISTMODE_PROVIDER
2 #define INSPIRCD_LISTMODE_PROVIDER
14 /* $ModDesc: Provides support for easily creating listmodes, stores the time set, the user, and a parameter. */
16 /* Updated to use the <banlist> config tag if it exists */
17 /* Written by Om <omster@gmail.com>, December 2005. */
18 /* Based on code previously written by Om - April 2005 */
19 /* Updated to new API July 8th 2006 by Brain */
20 /* Originally based on m_chanprotect and m_silence */
22 inline std::string stringtime()
24 std::ostringstream TIME;
29 /** An item in a listmode's list
31 class ListItem : public classbase
39 /** The number of items a listmode's list may contain
41 class ListLimit : public classbase
48 // Just defining the type we use for the exception list here...
49 typedef std::vector<ListItem> modelist;
50 typedef std::vector<ListLimit> limitlist;
52 /** The base class for listmodes defined by u_listmode.h
54 class ListModeBase : public ModeHandler
58 std::string listnumeric;
59 std::string endoflistnumeric;
60 std::string endofliststring;
62 std::string configtag;
66 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
67 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
70 infokey = "exceptionbase_mode_" + std::string(1, mode) + "_list";
73 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
76 channel->GetExt(infokey, el);
79 for (modelist::iterator it = el->begin(); it != el->end(); it++)
81 if(parameter == it->mask)
83 return std::make_pair(true, parameter);
87 return std::make_pair(false, parameter);
90 virtual void DisplayList(userrec* user, chanrec* channel)
93 channel->GetExt(infokey, el);
96 for(modelist::iterator it = el->begin(); it != el->end(); it++)
98 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());
101 user->WriteServ("%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
104 virtual void RemoveMode(chanrec* channel)
106 ServerInstance->Log(DEBUG,"Removing listmode base from %s %s",channel->name,infokey.c_str());
108 channel->GetExt(infokey, el);
111 ServerInstance->Log(DEBUG,"Channel is extended with a list");
112 char moderemove[MAXBUF];
113 userrec* n = new userrec(ServerInstance);
114 n->SetFd(FD_MAGIC_NUMBER);
116 /* Make a copy of it, because we cant change the list whilst iterating over it */
117 for(modelist::iterator it = el->begin(); it != el->end(); it++)
121 for(modelist::iterator it = copy.begin(); it != copy.end(); it++)
123 ServerInstance->Log(DEBUG,"Remove item %s",it->mask.c_str());
124 sprintf(moderemove,"-%c",this->GetModeChar());
125 const char* parameters[] = { channel->name, moderemove, it->mask.c_str() };
126 ServerInstance->SendMode(parameters, 3, n);
132 virtual void RemoveMode(userrec* user)
134 /* Listmodes dont get set on users */
137 virtual void DoRehash()
139 ConfigReader Conf(ServerInstance);
143 for(int i = 0; i < Conf.Enumerate(configtag); i++)
145 // For each <banlist> tag
147 limit.mask = Conf.ReadValue(configtag, "chan", i);
148 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
150 if(limit.mask.size() && limit.limit > 0)
151 chanlimits.push_back(limit);
153 if(chanlimits.size() == 0)
158 chanlimits.push_back(limit);
162 virtual void DoImplements(char* List)
164 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
167 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
169 // Try and grab the list
171 channel->GetExt(infokey, el);
175 // If there was no list
180 channel->Extend(infokey, el);
185 ModeParser::CleanMask(parameter);
187 // Check if the item already exists in the list
188 for (modelist::iterator it = el->begin(); it != el->end(); it++)
190 if(parameter == it->mask)
192 /* Give a subclass a chance to error about this */
193 TellAlreadyOnList(source, channel, parameter);
195 // it does, deny the change
196 return MODEACTION_DENY;
200 unsigned int maxsize = 0;
202 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
204 if (match(channel->name, it->mask.c_str()))
206 // We have a pattern matching the channel...
207 maxsize = el->size();
208 if (maxsize < it->limit)
210 /* Ok, it *could* be allowed, now give someone subclassing us
211 * a chance to validate the parameter.
212 * The param is passed by reference, so they can both modify it
213 * and tell us if we allow it or not.
215 * eg, the subclass could:
217 * 2) 'fix' parameter and then allow
220 if(ValidateParam(source, channel, parameter))
222 // And now add the mask onto the list...
225 e.nick = source->nick;
226 e.time = stringtime();
229 return MODEACTION_ALLOW;
233 /* If they deny it they have the job of giving an error message */
234 return MODEACTION_DENY;
240 /* List is full, give subclass a chance to send a custom message */
241 if(!TellListTooLong(source, channel, parameter))
243 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
247 return MODEACTION_DENY;
251 // We're taking the mode off
254 for (modelist::iterator it = el->begin(); it != el->end(); it++)
256 if(parameter == it->mask)
261 channel->Shrink(infokey);
264 return MODEACTION_ALLOW;
267 /* Tried to remove something that wasn't set */
268 TellNotSet(source, channel, parameter);
270 return MODEACTION_DENY;
274 /* Hmm, taking an exception off a non-existant list, DIE */
275 TellNotSet(source, channel, parameter);
277 return MODEACTION_DENY;
280 return MODEACTION_DENY;
283 virtual std::string& GetInfoKey()
288 virtual void DoChannelDelete(chanrec* chan)
291 chan->GetExt(infokey, list);
295 chan->Shrink(infokey);
300 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
303 chan->GetExt(infokey, list);
304 irc::modestacker modestack(true);
305 std::deque<std::string> stackresult;
308 for (modelist::iterator it = list->begin(); it != list->end(); it++)
310 modestack.Push(std::string(1, mode)[0], it->mask);
313 while (modestack.GetStackedLine(stackresult))
315 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
316 std::string line = mode_join.GetJoined();
317 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
321 virtual void DoCleanup(int target_type, void* item)
325 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
330 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
335 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
339 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)