1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
22 #include "helperfuncs.h"
35 ListData() : list_start(0), list_position(0), list_ended(false) {};
36 ListData(long pos, time_t t) : list_start(t), list_position(pos), list_ended(false) {};
39 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
41 typedef std::vector<userrec *> UserList;
43 class ListTimer : public InspTimer
48 ListTimer(long interval, Server* Me) : InspTimer(interval,TIME), Srv(Me)
52 virtual void Tick(time_t TIME)
54 log(DEBUG,"*** Timer tick!");
63 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
67 * - Get where they are up to
68 * - If it's > GetChannelCount, erase them from the iterator, set go_again to true
69 * - If not, spool the next 20 channels
71 userrec* u = (userrec*)(*iter);
72 ListData* ld = (ListData*)u->GetExt("safelist_cache");
73 if (ld->list_position > Srv->GetChannelCount())
75 u->Shrink("safelist_cache");
77 listusers.erase(iter);
82 log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
84 /* Attempt to fill up to half the user's sendq with /LIST output */
88 log(DEBUG,"Channel %ld",ld->list_position);
89 chan = Srv->GetChannelIndex(ld->list_position);
93 /* Increment total plus linefeed */
94 int counter = snprintf(buffer,MAXBUF,"322 %s %s %d :[+%s] %s",u->nick,chan->name,usercount_i(chan),chanmodes(chan,has_channel(u,chan)),chan->topic);
95 amount_sent += counter + 4 + Srv->GetServerName().length();
96 log(DEBUG,"m_safelist.so: Sent %ld of safe %ld / 2",amount_sent,u->sendqmax);
97 WriteServ_NoFormat(u->fd,buffer);
103 ld->list_ended = true;
104 WriteServ(u->fd,"323 %s :End of channel list.",u->nick);
110 while ((chan != NULL) && (amount_sent < (u->sendqmax / 2)));
114 ListTimer* MyTimer = new ListTimer(1,Srv);
115 Srv->AddTimer(MyTimer);
119 class ModuleSafeList : public Module
124 UserList listusers; /* vector of people doing a /list */
126 ModuleSafeList(Server* Me) : Module::Module(Me)
130 MyTimer = new ListTimer(1,Srv);
131 Srv->AddTimer(MyTimer);
134 virtual ~ModuleSafeList()
138 virtual Version GetVersion()
140 return Version(1,0,0,0,VF_VENDOR);
143 void Implements(char* List)
145 List[I_OnPreCommand] List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
150 * Intercept the LIST command.
152 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated)
154 /* If the command doesnt appear to be valid, we dont want to mess with it. */
158 if (command == "LIST")
160 return this->HandleList(parameters, pcnt, user);
167 * Handle (override) the LIST command.
169 int HandleList(char** parameters, int pcnt, userrec* user)
171 /* First, let's check if the user is currently /list'ing */
172 ListData *ld = (ListData*)user->GetExt("safelist_cache");
176 /* user is already /list'ing, we don't want to do shit. */
180 time_t* last_list_time = (time_t*)user->GetExt("safelist_last");
183 if (TIME < (*last_list_time)+60)
185 WriteServ(user->fd,"NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
189 delete last_list_time;
190 user->Shrink("safelist_last");
194 * start at channel 0! ;)
196 ld = new ListData(0,TIME);
197 user->Extend("safelist_cache", (char*)ld);
198 listusers.push_back(user);
200 time_t* llt = new time_t;
202 user->Extend("safelist_last",(char*)llt);
204 WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
206 * If we can, we try and fill up the user's sendq right now with the first batch of channels,
207 * which on a small net, may be ALL of them.
209 this->OnBackgroundTimer(TIME);
213 virtual void OnCleanup(int target_type, void* item)
215 if(target_type == TYPE_USER)
217 userrec* u = (userrec*)item;
218 ListData* ld = (ListData*)u->GetExt("safelist_cache");
221 u->Shrink("safelist_cache");
224 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
226 userrec* u2 = (userrec*)(*iter);
229 listusers.erase(iter);
233 time_t* last_list_time = (time_t*)u->GetExt("safelist_last");
236 delete last_list_time;
237 u->Shrink("safelist_last");
242 virtual void On005Numeric(std::string &output)
244 output.append(" SAFELIST");
247 virtual void OnUserQuit(userrec* user, std::string message)
249 this->OnCleanup(TYPE_USER,user);
256 /******************************************************************************************************/
258 class ModuleSafeListFactory : public ModuleFactory
261 ModuleSafeListFactory()
265 ~ModuleSafeListFactory()
269 virtual Module * CreateModule(Server* Me)
271 return new ModuleSafeList(Me);
276 extern "C" void * init_module( void )
278 return new ModuleSafeListFactory;