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 = "listbase_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 irc::modestacker modestack(false);
113 std::deque<std::string> stackresult;
114 const char* mode_junk[MAXMODES+1];
115 mode_junk[0] = channel->name;
116 userrec* n = new userrec(ServerInstance);
117 n->SetFd(FD_MAGIC_NUMBER);
118 for(modelist::iterator it = el->begin(); it != el->end(); it++)
120 modestack.Push(this->GetModeChar(), it->mask);
122 while (modestack.GetStackedLine(stackresult))
124 for (size_t j = 0; j < stackresult.size(); j++)
126 mode_junk[j+1] = stackresult[j].c_str();
128 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, n);
135 virtual void RemoveMode(userrec* user)
137 /* Listmodes dont get set on users */
140 virtual void DoRehash()
142 ConfigReader Conf(ServerInstance);
146 for(int i = 0; i < Conf.Enumerate(configtag); i++)
148 // For each <banlist> tag
150 limit.mask = Conf.ReadValue(configtag, "chan", i);
151 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
153 if(limit.mask.size() && limit.limit > 0)
154 chanlimits.push_back(limit);
156 if(chanlimits.size() == 0)
161 chanlimits.push_back(limit);
165 virtual void DoImplements(char* List)
167 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
170 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
172 // Try and grab the list
174 channel->GetExt(infokey, el);
178 // If there was no list
183 channel->Extend(infokey, el);
188 ModeParser::CleanMask(parameter);
190 // Check if the item already exists in the list
191 for (modelist::iterator it = el->begin(); it != el->end(); it++)
193 if(parameter == it->mask)
195 /* Give a subclass a chance to error about this */
196 TellAlreadyOnList(source, channel, parameter);
198 // it does, deny the change
199 return MODEACTION_DENY;
203 unsigned int maxsize = 0;
205 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
207 if (match(channel->name, it->mask.c_str()))
209 // We have a pattern matching the channel...
210 maxsize = el->size();
211 if (maxsize < it->limit)
213 /* Ok, it *could* be allowed, now give someone subclassing us
214 * a chance to validate the parameter.
215 * The param is passed by reference, so they can both modify it
216 * and tell us if we allow it or not.
218 * eg, the subclass could:
220 * 2) 'fix' parameter and then allow
223 if(ValidateParam(source, channel, parameter))
225 // And now add the mask onto the list...
228 e.nick = source->nick;
229 e.time = stringtime();
232 return MODEACTION_ALLOW;
236 /* If they deny it they have the job of giving an error message */
237 return MODEACTION_DENY;
243 /* List is full, give subclass a chance to send a custom message */
244 if(!TellListTooLong(source, channel, parameter))
246 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
250 return MODEACTION_DENY;
254 // We're taking the mode off
257 for (modelist::iterator it = el->begin(); it != el->end(); it++)
259 if(parameter == it->mask)
264 channel->Shrink(infokey);
267 return MODEACTION_ALLOW;
270 /* Tried to remove something that wasn't set */
271 TellNotSet(source, channel, parameter);
273 return MODEACTION_DENY;
277 /* Hmm, taking an exception off a non-existant list, DIE */
278 TellNotSet(source, channel, parameter);
280 return MODEACTION_DENY;
283 return MODEACTION_DENY;
286 virtual std::string& GetInfoKey()
291 virtual void DoChannelDelete(chanrec* chan)
294 chan->GetExt(infokey, list);
298 chan->Shrink(infokey);
303 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
306 chan->GetExt(infokey, list);
307 irc::modestacker modestack(true);
308 std::deque<std::string> stackresult;
311 for (modelist::iterator it = list->begin(); it != list->end(); it++)
313 modestack.Push(std::string(1, mode)[0], it->mask);
316 while (modestack.GetStackedLine(stackresult))
318 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
319 std::string line = mode_join.GetJoined();
320 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, line);
324 virtual void DoCleanup(int target_type, void* item)
328 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
333 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
338 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
342 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)