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 class ListItem : public classbase
38 class ListLimit : public classbase
45 // Just defining the type we use for the exception list here...
46 typedef std::vector<ListItem> modelist;
47 typedef std::vector<ListLimit> limitlist;
49 class ListModeBase : public ModeHandler
53 std::string listnumeric;
54 std::string endoflistnumeric;
55 std::string endofliststring;
57 std::string configtag;
61 ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
62 : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
65 infokey = "exceptionbase_mode_" + std::string(1, mode) + "_list";
68 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
71 channel->GetExt(infokey, el);
74 for (modelist::iterator it = el->begin(); it != el->end(); it++)
76 if(parameter == it->mask)
78 return std::make_pair(true, parameter);
82 return std::make_pair(false, parameter);
85 virtual void DisplayList(userrec* user, chanrec* channel)
88 channel->GetExt(infokey, el);
91 for(modelist::iterator it = el->begin(); it != el->end(); it++)
93 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());
96 user->WriteServ("%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
99 virtual void RemoveMode(chanrec* channel)
101 ServerInstance->Log(DEBUG,"Removing listmode base from %s %s",channel->name,infokey.c_str());
103 channel->GetExt(infokey, el);
106 ServerInstance->Log(DEBUG,"Channel is extended with a list");
107 char moderemove[MAXBUF];
108 userrec* n = new userrec(ServerInstance);
109 n->SetFd(FD_MAGIC_NUMBER);
111 /* Make a copy of it, because we cant change the list whilst iterating over it */
112 for(modelist::iterator it = el->begin(); it != el->end(); it++)
116 for(modelist::iterator it = copy.begin(); it != copy.end(); it++)
118 ServerInstance->Log(DEBUG,"Remove item %s",it->mask.c_str());
119 sprintf(moderemove,"-%c",this->GetModeChar());
120 const char* parameters[] = { channel->name, moderemove, it->mask.c_str() };
121 ServerInstance->SendMode(parameters, 3, n);
127 virtual void RemoveMode(userrec* user)
129 /* Listmodes dont get set on users */
132 virtual void DoRehash()
134 ConfigReader Conf(ServerInstance);
138 for(int i = 0; i < Conf.Enumerate(configtag); i++)
140 // For each <banlist> tag
142 limit.mask = Conf.ReadValue(configtag, "chan", i);
143 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
145 if(limit.mask.size() && limit.limit > 0)
146 chanlimits.push_back(limit);
148 if(chanlimits.size() == 0)
153 chanlimits.push_back(limit);
157 virtual void DoImplements(char* List)
159 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
162 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
164 // Try and grab the list
166 channel->GetExt(infokey, el);
170 // If there was no list
175 channel->Extend(infokey, el);
180 ModeParser::CleanMask(parameter);
182 // Check if the item already exists in the list
183 for (modelist::iterator it = el->begin(); it != el->end(); it++)
185 if(parameter == it->mask)
187 /* Give a subclass a chance to error about this */
188 TellAlreadyOnList(source, channel, parameter);
190 // it does, deny the change
191 return MODEACTION_DENY;
195 unsigned int maxsize = 0;
197 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
199 if (match(channel->name, it->mask.c_str()))
201 // We have a pattern matching the channel...
202 maxsize = el->size();
203 if (maxsize < it->limit)
205 /* Ok, it *could* be allowed, now give someone subclassing us
206 * a chance to validate the parameter.
207 * The param is passed by reference, so they can both modify it
208 * and tell us if we allow it or not.
210 * eg, the subclass could:
212 * 2) 'fix' parameter and then allow
215 if(ValidateParam(source, channel, parameter))
217 // And now add the mask onto the list...
220 e.nick = source->nick;
221 e.time = stringtime();
224 return MODEACTION_ALLOW;
228 /* If they deny it they have the job of giving an error message */
229 return MODEACTION_DENY;
235 /* List is full, give subclass a chance to send a custom message */
236 if(!TellListTooLong(source, channel, parameter))
238 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
242 return MODEACTION_DENY;
246 // We're taking the mode off
249 for (modelist::iterator it = el->begin(); it != el->end(); it++)
251 if(parameter == it->mask)
256 channel->Shrink(infokey);
259 return MODEACTION_ALLOW;
262 /* Tried to remove something that wasn't set */
263 TellNotSet(source, channel, parameter);
265 return MODEACTION_DENY;
269 /* Hmm, taking an exception off a non-existant list, DIE */
270 TellNotSet(source, channel, parameter);
272 return MODEACTION_DENY;
275 return MODEACTION_DENY;
278 virtual std::string& GetInfoKey()
283 virtual void DoChannelDelete(chanrec* chan)
286 chan->GetExt(infokey, list);
290 chan->Shrink(infokey);
295 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
298 chan->GetExt(infokey, list);
301 for (modelist::iterator it = list->begin(); it != list->end(); it++)
303 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
308 virtual void DoCleanup(int target_type, void* item)
312 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
317 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
322 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
326 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)