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 */
45 * To create a timer which recurs every second, we inherit from InspTimer.
46 * InspTimer is only one-shot however, so at the end of each Tick() we simply
47 * insert another of ourselves into the pending queue :)
49 class ListTimer : public InspTimer
59 ListTimer(long interval, Server* Me) : InspTimer(interval,TIME), Srv(Me)
63 virtual void Tick(time_t TIME)
70 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
74 * - Get where they are up to
75 * - If it's > GetChannelCount, erase them from the iterator, set go_again to true
76 * - If not, spool the next 20 channels
78 userrec* u = (userrec*)(*iter);
79 ListData* ld = (ListData*)u->GetExt("safelist_cache");
80 if (ld->list_position > Srv->GetChannelCount())
82 u->Shrink("safelist_cache");
84 listusers.erase(iter);
89 log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
91 /* Attempt to fill up to half the user's sendq with /LIST output */
95 log(DEBUG,"Channel %ld",ld->list_position);
96 if (!ld->list_position)
97 WriteServ(u->fd,"321 %s Channel :Users Name",u->nick);
98 chan = Srv->GetChannelIndex(ld->list_position);
100 if ((chan) && (((!(chan->binarymodes & CM_PRIVATE)) && (!(chan->binarymodes & CM_SECRET))) || (has_channel(u,chan))))
102 /* Increment total plus linefeed */
103 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);
104 amount_sent += counter + 4 + Srv->GetServerName().length();
105 log(DEBUG,"m_safelist.so: Sent %ld of safe %ld / 4",amount_sent,u->sendqmax);
106 WriteServ_NoFormat(u->fd,buffer);
114 ld->list_ended = true;
115 WriteServ(u->fd,"323 %s :End of channel list.",u->nick);
122 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
126 ListTimer* MyTimer = new ListTimer(1,Srv);
127 Srv->AddTimer(MyTimer);
131 class ModuleSafeList : public Module
137 ModuleSafeList(Server* Me) : Module::Module(Me)
141 MyTimer = new ListTimer(1,Srv);
142 Srv->AddTimer(MyTimer);
145 virtual ~ModuleSafeList()
149 virtual Version GetVersion()
151 return Version(1,0,0,0,VF_VENDOR);
154 void Implements(char* List)
156 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
161 * Intercept the LIST command.
163 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated)
165 /* If the command doesnt appear to be valid, we dont want to mess with it. */
169 if (command == "LIST")
171 return this->HandleList(parameters, pcnt, user);
178 * Handle (override) the LIST command.
180 int HandleList(char** parameters, int pcnt, userrec* user)
182 /* First, let's check if the user is currently /list'ing */
183 ListData *ld = (ListData*)user->GetExt("safelist_cache");
187 /* user is already /list'ing, we don't want to do shit. */
191 time_t* last_list_time = (time_t*)user->GetExt("safelist_last");
194 if (TIME < (*last_list_time)+60)
196 WriteServ(user->fd,"NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
200 delete last_list_time;
201 user->Shrink("safelist_last");
205 * start at channel 0! ;)
207 ld = new ListData(0,TIME);
208 user->Extend("safelist_cache", (char*)ld);
209 listusers.push_back(user);
211 time_t* llt = new time_t;
213 user->Extend("safelist_last",(char*)llt);
218 virtual void OnCleanup(int target_type, void* item)
220 if(target_type == TYPE_USER)
222 userrec* u = (userrec*)item;
223 ListData* ld = (ListData*)u->GetExt("safelist_cache");
226 u->Shrink("safelist_cache");
229 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
231 userrec* u2 = (userrec*)(*iter);
234 listusers.erase(iter);
238 time_t* last_list_time = (time_t*)u->GetExt("safelist_last");
241 delete last_list_time;
242 u->Shrink("safelist_last");
247 virtual void On005Numeric(std::string &output)
249 output.append(" SAFELIST");
252 virtual void OnUserQuit(userrec* user, std::string message)
254 this->OnCleanup(TYPE_USER,user);
261 /******************************************************************************************************/
263 class ModuleSafeListFactory : public ModuleFactory
266 ModuleSafeListFactory()
270 ~ModuleSafeListFactory()
274 virtual Module * CreateModule(Server* Me)
276 return new ModuleSafeList(Me);
281 extern "C" void * init_module( void )
283 return new ModuleSafeListFactory;