1 #ifndef INSPIRCD_LISTMODE_PROVIDER
2 #define INSPIRCD_LISTMODE_PROVIDER
11 #include "helperfuncs.h"
13 /* $ModDesc: Provides support for easily creating listmodes, stores the time set, the user, and a parameter. */
15 /* Written by Om <omster@gmail.com>, December 2005. */
16 /* Based on code previously written by Om - April 2005 */
17 /* Originally based on m_chanprotect and m_silence */
19 inline std::string stringtime()
21 std::ostringstream TIME;
41 // Just defining the type we use for the excpetion list here...
42 typedef std::vector<ListItem> modelist;
43 typedef std::vector<ListLimit> limitlist;
45 class ListModeBaseModule : public Module
51 std::string listnumeric;
52 std::string endoflistnumeric;
53 std::string endofliststring;
59 ListModeBaseModule(Server* serv, char modechar, std::string eolstr, std::string lnum, std::string eolnum) : Module::Module(serv)
62 Conf = new ConfigReader;
65 endoflistnumeric = eolnum;
66 endofliststring = eolstr;
69 infokey = "exceptionbase_mode_" + std::string(1, mode) + "_list";
70 Srv->AddExtendedListMode(modechar);
73 virtual void OnRehash(std::string param)
76 Conf = new ConfigReader;
80 for(int i = 0; i < Conf->Enumerate("banlist"); i++)
82 // For each <banlist> tag
84 limit.mask = Conf->ReadValue("banlist", "chan", i);
85 limit.limit = Conf->ReadInteger("banlist", "limit", i, true);
87 if(limit.mask.size() && limit.limit > 0)
89 chanlimits.push_back(limit);
90 log(DEBUG, "m_exceptionbase.so: Read channel listmode limit of %u for mask '%s'", limit.limit, limit.mask.c_str());
94 log(DEBUG, "m_exceptionbase.so: Invalid tag");
98 if(chanlimits.size() == 0)
103 chanlimits.push_back(limit);
107 void DoImplements(char* List)
109 List[I_OnExtendedMode] = List[I_OnSendList] = List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
112 virtual int OnExtendedMode(userrec *user, void *target, char modechar, int type, bool mode_on, string_list ¶ms)
114 // First, check it's our mode
115 if ((modechar == mode) && (type == MT_CHANNEL))
117 Srv->Log(DEBUG, "m_exceptionbase.so: General listmode handler called, handling mode '" + std::string(1, mode) + "'");
118 chanrec* chan = (chanrec*)target;
120 // Try and grab the list
121 modelist* el = (modelist*)chan->GetExt(infokey);
125 // If there was no list
129 Srv->Log(DEBUG, "m_exceptionbase.so: Creating new list");
131 chan->Extend(infokey, (char*)el);
134 if(!Srv->IsValidMask(params[0]))
136 Srv->Log(DEBUG, "m_exceptionbase.so: Banmask was invalid, returning -1");
140 for (modelist::iterator it = el->begin(); it != el->end(); it++)
142 Srv->Log(DEBUG, "m_exceptionbase.so: Iterating over exception list, current mask: " + it->mask);
143 if(params[0] == it->mask)
145 Srv->Log(DEBUG, "m_exceptionbase.so: Someone tried to set an exception which was already set, returning -1");
150 for(limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
152 if(Srv->MatchText(chan->name, it->mask))
154 // We have a pattern matching the channel...
155 if(el->size() < it->limit)
157 // And now add the mask onto the list...
161 e.time = stringtime();
163 Srv->Log(DEBUG, "m_exceptionbase.so: All checks passed, adding exception mask to list and returning 1");
171 WriteServ(user->fd, "478 %s %s %s :Channel ban/ignore list is full", user->nick, chan->name, params[0].c_str());
172 log(DEBUG, "m_exceptionbase.so: %s tried to set mask %s on %s but the list is full (max %d)", user->nick, params[0].c_str(), chan->name, maxlist);
177 // We're taking the mode off
180 for (modelist::iterator it = el->begin(); it != el->end(); it++)
182 Srv->Log(DEBUG, "m_exceptionbase.so: Removing mode, iterating over exception list, current mask: " + it->mask);
183 if(params[0] == it->mask)
185 Srv->Log(DEBUG, "m_exceptionbase.so: Found match for removal of exception, removing and returning 1");
189 Srv->Log(DEBUG, "m_exceptionbase.so: Erased the last entry on the exception list, removing the list");
190 chan->Shrink(infokey);
196 Srv->Log(DEBUG, "m_exceptionbase.so: No match found for attempted removing of exception, returning -1");
201 // Hmm, taking an exception off a non-existant list, DIE
202 Srv->Log(DEBUG, "m_exceptionbase.so: Attempted removal of an exception, when there was no exception list created, returning -1");
211 virtual void OnSendList(userrec* user, chanrec* chan, char modechar)
215 modelist* el = (modelist*)chan->GetExt(infokey);
216 Srv->Log(DEBUG, "m_exceptionbase.so: " + std::string(user->nick)+" is listing listmodes on "+std::string(chan->name));
219 for(modelist::iterator it = el->begin(); it != el->end(); it++)
221 WriteServ(user->fd, "%s %s %s %s %s %s", listnumeric.c_str(), user->nick, chan->name, it->mask.c_str(), it->nick.c_str(), it->time.c_str());
225 WriteServ(user->fd, "%s %s %s %s", endoflistnumeric.c_str(), user->nick, chan->name, endofliststring.c_str());
229 virtual void OnChannelDelete(chanrec* chan)
231 modelist* list = (modelist*)chan->GetExt(infokey);
235 chan->Shrink(infokey);
240 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
242 modelist* list = (modelist*)chan->GetExt(infokey);
245 for(modelist::iterator it = list->begin(); it != list->end(); it++)
247 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
252 virtual void OnCleanup(int target_type, void* item)
254 if (target_type == TYPE_CHANNEL)
256 chanrec* chan = (chanrec*)item;
258 modelist* list = (modelist*)chan->GetExt(infokey);
262 chan->Shrink(infokey);