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;
42 UserList listusers; /* vector of people doing a /list */
44 class ListTimer : public InspTimer
49 ListTimer(long interval, Server* Me) : InspTimer(interval,TIME), Srv(Me)
53 virtual void Tick(time_t TIME)
55 log(DEBUG,"*** Timer tick!");
64 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
68 * - Get where they are up to
69 * - If it's > GetChannelCount, erase them from the iterator, set go_again to true
70 * - If not, spool the next 20 channels
72 userrec* u = (userrec*)(*iter);
73 ListData* ld = (ListData*)u->GetExt("safelist_cache");
74 if (ld->list_position > Srv->GetChannelCount())
76 u->Shrink("safelist_cache");
78 listusers.erase(iter);
83 log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
85 /* Attempt to fill up to half the user's sendq with /LIST output */
89 log(DEBUG,"Channel %ld",ld->list_position);
90 if (!ld->list_position)
91 WriteServ(u->fd,"321 %s Channel :Users Name",u->nick);
92 chan = Srv->GetChannelIndex(ld->list_position);
96 /* Increment total plus linefeed */
97 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);
98 amount_sent += counter + 4 + Srv->GetServerName().length();
99 log(DEBUG,"m_safelist.so: Sent %ld of safe %ld / 2",amount_sent,u->sendqmax);
100 WriteServ_NoFormat(u->fd,buffer);
106 ld->list_ended = true;
107 WriteServ(u->fd,"323 %s :End of channel list.",u->nick);
113 while ((chan != NULL) && (amount_sent < (u->sendqmax / 2)));
117 ListTimer* MyTimer = new ListTimer(1,Srv);
118 Srv->AddTimer(MyTimer);
122 class ModuleSafeList : public Module
128 ModuleSafeList(Server* Me) : Module::Module(Me)
132 MyTimer = new ListTimer(1,Srv);
133 Srv->AddTimer(MyTimer);
136 virtual ~ModuleSafeList()
140 virtual Version GetVersion()
142 return Version(1,0,0,0,VF_VENDOR);
145 void Implements(char* List)
147 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
152 * Intercept the LIST command.
154 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated)
156 /* If the command doesnt appear to be valid, we dont want to mess with it. */
160 if (command == "LIST")
162 return this->HandleList(parameters, pcnt, user);
169 * Handle (override) the LIST command.
171 int HandleList(char** parameters, int pcnt, userrec* user)
173 /* First, let's check if the user is currently /list'ing */
174 ListData *ld = (ListData*)user->GetExt("safelist_cache");
178 /* user is already /list'ing, we don't want to do shit. */
182 time_t* last_list_time = (time_t*)user->GetExt("safelist_last");
185 if (TIME < (*last_list_time)+60)
187 WriteServ(user->fd,"NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
191 delete last_list_time;
192 user->Shrink("safelist_last");
196 * start at channel 0! ;)
198 ld = new ListData(0,TIME);
199 user->Extend("safelist_cache", (char*)ld);
200 listusers.push_back(user);
202 time_t* llt = new time_t;
204 user->Extend("safelist_last",(char*)llt);
209 virtual void OnCleanup(int target_type, void* item)
211 if(target_type == TYPE_USER)
213 userrec* u = (userrec*)item;
214 ListData* ld = (ListData*)u->GetExt("safelist_cache");
217 u->Shrink("safelist_cache");
220 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
222 userrec* u2 = (userrec*)(*iter);
225 listusers.erase(iter);
229 time_t* last_list_time = (time_t*)u->GetExt("safelist_last");
232 delete last_list_time;
233 u->Shrink("safelist_last");
238 virtual void On005Numeric(std::string &output)
240 output.append(" SAFELIST");
243 virtual void OnUserQuit(userrec* user, std::string message)
245 this->OnCleanup(TYPE_USER,user);
252 /******************************************************************************************************/
254 class ModuleSafeListFactory : public ModuleFactory
257 ModuleSafeListFactory()
261 ~ModuleSafeListFactory()
265 virtual Module * CreateModule(Server* Me)
267 return new ModuleSafeList(Me);
272 extern "C" void * init_module( void )
274 return new ModuleSafeListFactory;