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 /* Updated to use the <banlist> config tag if it exists */
16 /* Written by Om <omster@gmail.com>, December 2005. */
17 /* Based on code previously written by Om - April 2005 */
18 /* Originally based on m_chanprotect and m_silence */
20 inline std::string stringtime()
22 std::ostringstream TIME;
42 // Just defining the type we use for the exception list here...
43 typedef std::vector<ListItem> modelist;
44 typedef std::vector<ListLimit> limitlist;
46 class ListModeBaseModule : public Module
51 std::string listnumeric;
52 std::string endoflistnumeric;
53 std::string endofliststring;
59 ListModeBaseModule(Server* serv, char modechar, const std::string &eolstr, const std::string &lnum, const 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(const std::string ¶m)
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 unsigned int maxsize = 0;
152 for(limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
154 if(Srv->MatchText(chan->name, it->mask))
156 // We have a pattern matching the channel...
157 maxsize = el->size();
158 if(maxsize < it->limit)
160 // And now add the mask onto the list...
164 e.time = stringtime();
166 Srv->Log(DEBUG, "m_exceptionbase.so: All checks passed, adding exception mask to list and returning 1");
174 WriteServ(user->fd, "478 %s %s %s :Channel ban/ignore list is full", user->nick, chan->name, params[0].c_str());
175 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, maxsize);
180 // We're taking the mode off
183 for (modelist::iterator it = el->begin(); it != el->end(); it++)
185 Srv->Log(DEBUG, "m_exceptionbase.so: Removing mode, iterating over exception list, current mask: " + it->mask);
186 if(params[0] == it->mask)
188 Srv->Log(DEBUG, "m_exceptionbase.so: Found match for removal of exception, removing and returning 1");
192 Srv->Log(DEBUG, "m_exceptionbase.so: Erased the last entry on the exception list, removing the list");
193 chan->Shrink(infokey);
199 Srv->Log(DEBUG, "m_exceptionbase.so: No match found for attempted removing of exception, returning -1");
204 // Hmm, taking an exception off a non-existant list, DIE
205 Srv->Log(DEBUG, "m_exceptionbase.so: Attempted removal of an exception, when there was no exception list created, returning -1");
214 virtual void OnSendList(userrec* user, chanrec* chan, char modechar)
218 modelist* el = (modelist*)chan->GetExt(infokey);
219 Srv->Log(DEBUG, "m_exceptionbase.so: " + std::string(user->nick)+" is listing listmodes on "+std::string(chan->name));
222 for(modelist::iterator it = el->begin(); it != el->end(); it++)
224 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());
228 WriteServ(user->fd, "%s %s %s %s", endoflistnumeric.c_str(), user->nick, chan->name, endofliststring.c_str());
232 virtual void OnChannelDelete(chanrec* chan)
234 modelist* list = (modelist*)chan->GetExt(infokey);
238 chan->Shrink(infokey);
243 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
245 modelist* list = (modelist*)chan->GetExt(infokey);
248 for(modelist::iterator it = list->begin(); it != list->end(); it++)
250 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
255 virtual void OnCleanup(int target_type, void* item)
257 if (target_type == TYPE_CHANNEL)
259 chanrec* chan = (chanrec*)item;
261 modelist* list = (modelist*)chan->GetExt(infokey);
265 chan->Shrink(infokey);