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)
102 channel->GetExt(infokey, el);
105 char moderemove[MAXBUF];
106 userrec* n = new userrec(ServerInstance);
107 n->SetFd(FD_MAGIC_NUMBER);
108 for(modelist::iterator it = el->begin(); it != el->end(); it++)
110 sprintf(moderemove,"-%c",this->GetModeChar());
111 const char* parameters[] = { channel->name, moderemove, it->mask.c_str() };
112 ServerInstance->SendMode(parameters, 3, n);
118 virtual void RemoveMode(userrec* user)
120 /* Listmodes dont get set on users */
123 virtual void DoRehash()
125 ConfigReader Conf(ServerInstance);
129 for(int i = 0; i < Conf.Enumerate(configtag); i++)
131 // For each <banlist> tag
133 limit.mask = Conf.ReadValue(configtag, "chan", i);
134 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
136 if(limit.mask.size() && limit.limit > 0)
137 chanlimits.push_back(limit);
139 if(chanlimits.size() == 0)
144 chanlimits.push_back(limit);
148 virtual void DoImplements(char* List)
150 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
153 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
155 // Try and grab the list
157 channel->GetExt(infokey, el);
161 // If there was no list
166 channel->Extend(infokey, el);
171 ModeParser::CleanMask(parameter);
173 // Check if the item already exists in the list
174 for (modelist::iterator it = el->begin(); it != el->end(); it++)
176 if(parameter == it->mask)
178 /* Give a subclass a chance to error about this */
179 TellAlreadyOnList(source, channel, parameter);
181 // it does, deny the change
182 return MODEACTION_DENY;
186 unsigned int maxsize = 0;
188 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
190 if (match(channel->name, it->mask.c_str()))
192 // We have a pattern matching the channel...
193 maxsize = el->size();
194 if (maxsize < it->limit)
196 /* Ok, it *could* be allowed, now give someone subclassing us
197 * a chance to validate the parameter.
198 * The param is passed by reference, so they can both modify it
199 * and tell us if we allow it or not.
201 * eg, the subclass could:
203 * 2) 'fix' parameter and then allow
206 if(ValidateParam(source, channel, parameter))
208 // And now add the mask onto the list...
211 e.nick = source->nick;
212 e.time = stringtime();
215 return MODEACTION_ALLOW;
219 /* If they deny it they have the job of giving an error message */
220 return MODEACTION_DENY;
226 /* List is full, give subclass a chance to send a custom message */
227 if(!TellListTooLong(source, channel, parameter))
229 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
233 return MODEACTION_DENY;
237 // We're taking the mode off
240 for (modelist::iterator it = el->begin(); it != el->end(); it++)
242 if(parameter == it->mask)
247 channel->Shrink(infokey);
250 return MODEACTION_ALLOW;
254 /* Tried to remove something that wasn't set */
255 TellNotSet(source, channel, parameter);
259 return MODEACTION_DENY;
263 // Hmm, taking an exception off a non-existant list, DIE
265 return MODEACTION_DENY;
268 return MODEACTION_DENY;
271 virtual std::string& GetInfoKey()
276 virtual void DoChannelDelete(chanrec* chan)
279 chan->GetExt(infokey, list);
283 chan->Shrink(infokey);
288 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
291 chan->GetExt(infokey, list);
294 for (modelist::iterator it = list->begin(); it != list->end(); it++)
296 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
301 virtual void DoCleanup(int target_type, void* item)
303 if (target_type == TYPE_CHANNEL)
305 chanrec* chan = (chanrec*)item;
308 chan->GetExt(infokey, list);
312 chan->Shrink(infokey);
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)