1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "configreader.h"
21 /** Holds a users m_safelist state
23 class ListData : public classbase
29 const std::string glob;
33 ListData() : list_start(0), list_position(0), list_ended(false) {};
34 ListData(long pos, time_t t, const std::string &pattern, int mi, int ma) : list_start(t), list_position(pos), list_ended(false), glob(pattern), minusers(mi), maxusers(ma) {};
37 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
39 class ModuleSafeList : public Module
42 size_t ServerNameSize;
46 ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
51 virtual ~ModuleSafeList()
55 virtual void OnRehash(userrec* user, const std::string ¶meter)
57 ConfigReader MyConf(ServerInstance);
58 ThrottleSecs = MyConf.ReadInteger("safelist", "throttle", "60", 0, true);
59 LimitList = MyConf.ReadInteger("safelist", "maxlisters", "50", 0, true);
60 ServerNameSize = strlen(ServerInstance->Config->ServerName) + 4;
64 virtual Version GetVersion()
66 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
69 void Implements(char* List)
71 List[I_OnBufferFlushed] = List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnRehash] = 1;
76 * Intercept the LIST command.
78 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
80 /* If the command doesnt appear to be valid, we dont want to mess with it. */
84 if (command == "LIST")
86 return this->HandleList(parameters, pcnt, user);
93 * Handle (override) the LIST command.
95 int HandleList(const char** parameters, int pcnt, userrec* user)
97 int minusers = 0, maxusers = 0;
99 if (global_listing >= LimitList)
101 user->WriteServ("NOTICE %s :*** Server load is currently too heavy. Please try again later.", user->nick);
102 user->WriteServ("321 %s Channel :Users Name",user->nick);
103 user->WriteServ("323 %s :End of channel list.",user->nick);
107 /* First, let's check if the user is currently /list'ing */
109 user->GetExt("safelist_cache", ld);
113 /* user is already /list'ing, we don't want to do shit. */
117 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
120 if (*parameters[0] == '<')
122 maxusers = atoi(parameters[0]+1);
123 ServerInstance->Log(DEBUG,"Max users: %d", maxusers);
126 else if (*parameters[0] == '>')
128 minusers = atoi(parameters[0]+1);
129 ServerInstance->Log(DEBUG,"Min users: %d", minusers);
134 time_t* last_list_time;
135 user->GetExt("safelist_last", last_list_time);
138 if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
140 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
141 user->WriteServ("321 %s Channel :Users Name",user->nick);
142 user->WriteServ("323 %s :End of channel list.",user->nick);
146 DELETE(last_list_time);
147 user->Shrink("safelist_last");
152 * start at channel 0! ;)
154 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*", minusers, maxusers);
155 user->Extend("safelist_cache", ld);
157 time_t* llt = new time_t;
158 *llt = ServerInstance->Time();
159 user->Extend("safelist_last", llt);
161 user->WriteServ("321 %s Channel :Users Name",user->nick);
168 virtual void OnBufferFlushed(userrec* user)
172 if (user->GetExt("safelist_cache", ld))
174 chanrec* chan = NULL;
175 long amount_sent = 0;
178 chan = ServerInstance->GetChannelIndex(ld->list_position);
179 bool has_user = (chan && chan->HasUser(user));
180 long users = chan ? chan->GetUserCounter() : 0;
182 bool too_few = (ld->minusers && (users <= ld->minusers));
183 bool too_many = (ld->maxusers && (users >= ld->maxusers));
185 if (chan && (too_many || too_few))
191 if ((chan) && (chan->modes[CM_PRIVATE]))
193 bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
194 if ((users) && (display))
196 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
197 amount_sent += counter + ServerNameSize;
198 user->WriteServ(std::string(buffer));
201 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
203 bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
204 if ((users) && (display))
206 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
207 amount_sent += counter + ServerNameSize;
208 user->WriteServ(std::string(buffer));
217 ld->list_ended = true;
218 user->WriteServ("323 %s :End of channel list.",user->nick);
224 while ((chan != NULL) && (amount_sent < (user->sendqmax / 4)));
227 user->Shrink("safelist_cache");
234 virtual void OnCleanup(int target_type, void* item)
236 if(target_type == TYPE_USER)
238 userrec* u = (userrec*)item;
240 u->GetExt("safelist_cache", ld);
243 u->Shrink("safelist_cache");
247 time_t* last_list_time;
248 u->GetExt("safelist_last", last_list_time);
251 DELETE(last_list_time);
252 u->Shrink("safelist_last");
257 virtual void On005Numeric(std::string &output)
259 output.append(" SAFELIST");
262 virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
264 this->OnCleanup(TYPE_USER,user);
270 class ModuleSafeListFactory : public ModuleFactory
273 ModuleSafeListFactory()
277 ~ModuleSafeListFactory()
281 virtual Module * CreateModule(InspIRCd* Me)
283 return new ModuleSafeList(Me);
288 extern "C" void * init_module( void )
290 return new ModuleSafeListFactory;