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);
110 for(modelist::iterator it = el->begin(); it != el->end(); it++)
112 ServerInstance->Log(DEBUG,"Remove item %s",it->mask.c_str());
113 sprintf(moderemove,"-%c",this->GetModeChar());
114 const char* parameters[] = { channel->name, moderemove, it->mask.c_str() };
115 ServerInstance->SendMode(parameters, 3, n);
121 virtual void RemoveMode(userrec* user)
123 /* Listmodes dont get set on users */
126 virtual void DoRehash()
128 ConfigReader Conf(ServerInstance);
132 for(int i = 0; i < Conf.Enumerate(configtag); i++)
134 // For each <banlist> tag
136 limit.mask = Conf.ReadValue(configtag, "chan", i);
137 limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
139 if(limit.mask.size() && limit.limit > 0)
140 chanlimits.push_back(limit);
142 if(chanlimits.size() == 0)
147 chanlimits.push_back(limit);
151 virtual void DoImplements(char* List)
153 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
156 virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
158 // Try and grab the list
160 channel->GetExt(infokey, el);
164 // If there was no list
169 channel->Extend(infokey, el);
174 ModeParser::CleanMask(parameter);
176 // Check if the item already exists in the list
177 for (modelist::iterator it = el->begin(); it != el->end(); it++)
179 if(parameter == it->mask)
181 /* Give a subclass a chance to error about this */
182 TellAlreadyOnList(source, channel, parameter);
184 // it does, deny the change
185 return MODEACTION_DENY;
189 unsigned int maxsize = 0;
191 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
193 if (match(channel->name, it->mask.c_str()))
195 // We have a pattern matching the channel...
196 maxsize = el->size();
197 if (maxsize < it->limit)
199 /* Ok, it *could* be allowed, now give someone subclassing us
200 * a chance to validate the parameter.
201 * The param is passed by reference, so they can both modify it
202 * and tell us if we allow it or not.
204 * eg, the subclass could:
206 * 2) 'fix' parameter and then allow
209 if(ValidateParam(source, channel, parameter))
211 // And now add the mask onto the list...
214 e.nick = source->nick;
215 e.time = stringtime();
218 return MODEACTION_ALLOW;
222 /* If they deny it they have the job of giving an error message */
223 return MODEACTION_DENY;
229 /* List is full, give subclass a chance to send a custom message */
230 if(!TellListTooLong(source, channel, parameter))
232 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
236 return MODEACTION_DENY;
240 // We're taking the mode off
243 for (modelist::iterator it = el->begin(); it != el->end(); it++)
245 if(parameter == it->mask)
250 channel->Shrink(infokey);
253 return MODEACTION_ALLOW;
257 /* Tried to remove something that wasn't set */
258 TellNotSet(source, channel, parameter);
262 return MODEACTION_DENY;
266 // Hmm, taking an exception off a non-existant list, DIE
268 return MODEACTION_DENY;
271 return MODEACTION_DENY;
274 virtual std::string& GetInfoKey()
279 virtual void DoChannelDelete(chanrec* chan)
282 chan->GetExt(infokey, list);
286 chan->Shrink(infokey);
291 virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
294 chan->GetExt(infokey, list);
297 for (modelist::iterator it = list->begin(); it != list->end(); it++)
299 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
304 virtual void DoCleanup(int target_type, void* item)
308 virtual bool ValidateParam(userrec* source, chanrec* channel, std::string ¶meter)
313 virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string ¶meter)
318 virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string ¶meter)
322 virtual void TellNotSet(userrec* source, chanrec* channel, std::string ¶meter)