1 #ifndef INSPIRCD_LISTMODE_PROVIDER
2 #define INSPIRCD_LISTMODE_PROVIDER
15 /* $ModDesc: Provides support for easily creating listmodes, stores the time set, the user, and a parameter. */
17 /* Updated to use the <banlist> config tag if it exists */
18 /* Written by Om <omster@gmail.com>, December 2005. */
19 /* Based on code previously written by Om - April 2005 */
20 /* Updated to new API July 8th 2006 by Brain */
21 /* Originally based on m_chanprotect and m_silence */
23 inline std::string stringtime()
25 std::ostringstream TIME;
30 /** An item in a listmode's list
32 class ListItem : public classbase
40 /** The number of items a listmode's list may contain
42 class ListLimit : public classbase
49 // Just defining the type we use for the exception list here...
50 typedef std::vector<ListItem> modelist;
51 typedef std::vector<ListLimit> limitlist;
53 /** The base class for listmodes defined by u_listmode.h
55 class ListModeBase : public ModeHandler
59 std::string listnumeric;
60 std::string endoflistnumeric;
61 std::string endofliststring;
63 std::string configtag;
67 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
68 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
71 infokey = "exceptionbase_mode_" + std::string(1, mode) + "_list";
74 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
77 channel->GetExt(infokey, el);
80 for (modelist::iterator it = el->begin(); it != el->end(); it++)
82 if(parameter == it->mask)
84 return std::make_pair(true, parameter);
88 return std::make_pair(false, parameter);
91 virtual void DisplayList(userrec* user, chanrec* channel)
94 channel->GetExt(infokey, el);
97 for(modelist::iterator it = el->begin(); it != el->end(); it++)
99 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());
102 user->WriteServ("%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
105 virtual void RemoveMode(chanrec* channel)
107 ServerInstance->Log(DEBUG,"Removing listmode base from %s %s",channel->name,infokey.c_str());
109 channel->GetExt(infokey, el);
112 ServerInstance->Log(DEBUG,"Channel is extended with a list");
113 char moderemove[MAXBUF];
114 userrec* n = new userrec(ServerInstance);
115 n->SetFd(FD_MAGIC_NUMBER);
117 /* Make a copy of it, because we cant change the list whilst iterating over it */
118 for(modelist::iterator it = el->begin(); it != el->end(); it++)
122 for(modelist::iterator it = copy.begin(); it != copy.end(); it++)
124 ServerInstance->Log(DEBUG,"Remove item %s",it->mask.c_str());
125 sprintf(moderemove,"-%c",this->GetModeChar());
126 const char* parameters[] = { channel->name, moderemove, it->mask.c_str() };
127 ServerInstance->SendMode(parameters, 3, n);
133 virtual void RemoveMode(userrec* user)
135 /* Listmodes dont get set on users */
138 virtual void DoRehash()
140 ConfigReader Conf(ServerInstance);
144 for(int i = 0; i < Conf.Enumerate(configtag); i++)
146 // For each <banlist> tag
148 limit.mask = Conf.ReadValue(configtag, "chan", i);
149 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
151 if(limit.mask.size() && limit.limit > 0)
152 chanlimits.push_back(limit);
154 if(chanlimits.size() == 0)
159 chanlimits.push_back(limit);
163 virtual void DoImplements(char* List)
165 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
168 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
170 // Try and grab the list
172 channel->GetExt(infokey, el);
176 // If there was no list
181 channel->Extend(infokey, el);
186 ModeParser::CleanMask(parameter);
188 // Check if the item already exists in the list
189 for (modelist::iterator it = el->begin(); it != el->end(); it++)
191 if(parameter == it->mask)
193 /* Give a subclass a chance to error about this */
194 TellAlreadyOnList(source, channel, parameter);
196 // it does, deny the change
197 return MODEACTION_DENY;
201 unsigned int maxsize = 0;
203 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
205 if (match(channel->name, it->mask.c_str()))
207 // We have a pattern matching the channel...
208 maxsize = el->size();
209 if (maxsize < it->limit)
211 /* Ok, it *could* be allowed, now give someone subclassing us
212 * a chance to validate the parameter.
213 * The param is passed by reference, so they can both modify it
214 * and tell us if we allow it or not.
216 * eg, the subclass could:
218 * 2) 'fix' parameter and then allow
221 if(ValidateParam(source, channel, parameter))
223 // And now add the mask onto the list...
226 e.nick = source->nick;
227 e.time = stringtime();
230 return MODEACTION_ALLOW;
234 /* If they deny it they have the job of giving an error message */
235 return MODEACTION_DENY;
241 /* List is full, give subclass a chance to send a custom message */
242 if(!TellListTooLong(source, channel, parameter))
244 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
248 return MODEACTION_DENY;
252 // We're taking the mode off
255 for (modelist::iterator it = el->begin(); it != el->end(); it++)
257 if(parameter == it->mask)
262 channel->Shrink(infokey);
265 return MODEACTION_ALLOW;
268 /* Tried to remove something that wasn't set */
269 TellNotSet(source, channel, parameter);
271 return MODEACTION_DENY;
275 /* Hmm, taking an exception off a non-existant list, DIE */
276 TellNotSet(source, channel, parameter);
278 return MODEACTION_DENY;
281 return MODEACTION_DENY;
284 virtual std::string& GetInfoKey()
289 virtual void DoChannelDelete(chanrec* chan)
292 chan->GetExt(infokey, list);
296 chan->Shrink(infokey);
301 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
304 chan->GetExt(infokey, list);
307 for (modelist::iterator it = list->begin(); it != list->end(); it++)
309 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
314 virtual void DoCleanup(int target_type, void* item)
318 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
323 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
328 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
332 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)