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), Srv(Me)
52 virtual void Tick(time_t TIME)
54 log(DEBUG,"*** Timer tick!");
55 ListTimer* MyTimer = new ListTimer(1,Srv);
56 Srv->AddTimer(MyTimer);
60 class ModuleSafeList : public Module
65 UserList listusers; /* vector of people doing a /list */
67 ModuleSafeList(Server* Me) : Module::Module(Me)
71 MyTimer = new ListTimer(1,Srv);
72 Srv->AddTimer(MyTimer);
75 virtual ~ModuleSafeList()
79 virtual Version GetVersion()
81 return Version(1,0,0,0,VF_VENDOR);
84 void Implements(char* List)
86 List[I_OnPreCommand] = List[I_OnBackgroundTimer] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
91 * Intercept the LIST command.
93 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated)
95 /* If the command doesnt appear to be valid, we dont want to mess with it. */
99 if (command == "LIST")
101 return this->HandleList(parameters, pcnt, user);
107 * OnBackgroundTimer()
108 * Spool off safelist information to users currently doing /list.
110 virtual void OnBackgroundTimer(time_t curtime)
112 bool go_again = true;
119 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
123 * - Get where they are up to
124 * - If it's > GetChannelCount, erase them from the iterator, set go_again to true
125 * - If not, spool the next 20 channels
127 userrec* u = (userrec*)(*iter);
128 ListData* ld = (ListData*)u->GetExt("safelist_cache");
129 if (ld->list_position > Srv->GetChannelCount())
131 u->Shrink("safelist_cache");
133 listusers.erase(iter);
138 log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
140 /* Attempt to fill up to half the user's sendq with /LIST output */
141 long amount_sent = 0;
144 log(DEBUG,"Channel %ld",ld->list_position);
145 chan = Srv->GetChannelIndex(ld->list_position);
149 /* Increment total plus linefeed */
150 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);
151 amount_sent += counter + 4 + Srv->GetServerName().length();
152 log(DEBUG,"m_safelist.so: Sent %ld of safe %ld / 2",amount_sent,u->sendqmax);
153 WriteServ_NoFormat(u->fd,buffer);
159 ld->list_ended = true;
160 WriteServ(u->fd,"323 %s :End of channel list.",u->nick);
166 while ((chan != NULL) && (amount_sent < (u->sendqmax / 2)));
173 * Handle (override) the LIST command.
175 int HandleList(char** parameters, int pcnt, userrec* user)
177 /* First, let's check if the user is currently /list'ing */
178 ListData *ld = (ListData*)user->GetExt("safelist_cache");
182 /* user is already /list'ing, we don't want to do shit. */
186 time_t* last_list_time = (time_t*)user->GetExt("safelist_last");
189 if (TIME < (*last_list_time)+60)
191 WriteServ(user->fd,"NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
195 delete last_list_time;
196 user->Shrink("safelist_last");
200 * start at channel 0! ;)
202 ld = new ListData(0,TIME);
203 user->Extend("safelist_cache", (char*)ld);
204 listusers.push_back(user);
206 time_t* llt = new time_t;
208 user->Extend("safelist_last",(char*)llt);
210 WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
212 * If we can, we try and fill up the user's sendq right now with the first batch of channels,
213 * which on a small net, may be ALL of them.
215 this->OnBackgroundTimer(TIME);
219 virtual void OnCleanup(int target_type, void* item)
221 if(target_type == TYPE_USER)
223 userrec* u = (userrec*)item;
224 ListData* ld = (ListData*)u->GetExt("safelist_cache");
227 u->Shrink("safelist_cache");
230 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
232 userrec* u2 = (userrec*)(*iter);
235 listusers.erase(iter);
239 time_t* last_list_time = (time_t*)u->GetExt("safelist_last");
242 delete last_list_time;
243 u->Shrink("safelist_last");
248 virtual void On005Numeric(std::string &output)
250 output.append(" SAFELIST");
253 virtual void OnUserQuit(userrec* user, std::string message)
255 this->OnCleanup(TYPE_USER,user);
262 /******************************************************************************************************/
264 class ModuleSafeListFactory : public ModuleFactory
267 ModuleSafeListFactory()
271 ~ModuleSafeListFactory()
275 virtual Module * CreateModule(Server* Me)
277 return new ModuleSafeList(Me);
282 extern "C" void * init_module( void )
284 return new ModuleSafeListFactory;