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"
24 #include "configreader.h"
29 extern InspIRCd* ServerInstance;
31 class ListData : public classbase
38 ListData() : list_start(0), list_position(0), list_ended(false) {};
39 ListData(long pos, time_t t) : list_start(t), list_position(pos), list_ended(false) {};
42 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
44 typedef std::vector<userrec *> UserList;
45 UserList listusers; /* vector of people doing a /list */
48 * To create a timer which recurs every second, we inherit from InspTimer.
49 * InspTimer is only one-shot however, so at the end of each Tick() we simply
50 * insert another of ourselves into the pending queue :)
52 class ListTimer : public InspTimer
62 ListTimer(long interval) : InspTimer(interval,TIME)
66 virtual void Tick(time_t TIME)
73 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
77 * - Get where they are up to
78 * - If it's more than total number of channels, erase
79 * them from the iterator, set go_again to true
80 * - If not, spool more channels
82 userrec* u = (userrec*)(*iter);
84 u->GetExt("safelist_cache", ld);
85 if ((size_t)ld->list_position > ServerInstance->chanlist.size())
87 u->Shrink("safelist_cache");
89 listusers.erase(iter);
94 log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
96 /* Attempt to fill up to half the user's sendq with /LIST output */
100 log(DEBUG,"Channel %ld",ld->list_position);
101 if (!ld->list_position)
102 u->WriteServ("321 %s Channel :Users Name",u->nick);
103 chan = Srv->GetChannelIndex(ld->list_position);
105 bool has_user = (chan && chan->HasUser(u));
106 if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
108 long users = chan->GetUserCounter();
111 int counter = snprintf(buffer,MAXBUF,"322 %s %s %ld :[+%s] %s",u->nick,chan->name,users,chan->ChanModes(has_user),chan->topic);
112 /* Increment total plus linefeed */
113 amount_sent += counter + 4 + strlen(ServerInstance->Config->ServerName);
114 log(DEBUG,"m_safelist.so: Sent %ld of safe %ld / 4",amount_sent,u->sendqmax);
115 u->WriteServ(std::string(buffer));
124 ld->list_ended = true;
125 u->WriteServ("323 %s :End of channel list.",u->nick);
132 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
136 ListTimer* MyTimer = new ListTimer(1);
137 ServerInstance->Timers->AddTimer(MyTimer);
141 class ModuleSafeList : public Module
147 ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
149 MyTimer = new ListTimer(1);
150 ServerInstance->Timers->AddTimer(MyTimer);
153 virtual ~ModuleSafeList()
157 virtual Version GetVersion()
159 return Version(1,0,0,0,VF_VENDOR);
162 void Implements(char* List)
164 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
169 * Intercept the LIST command.
171 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
173 /* If the command doesnt appear to be valid, we dont want to mess with it. */
177 if (command == "LIST")
179 return this->HandleList(parameters, pcnt, user);
186 * Handle (override) the LIST command.
188 int HandleList(const char** parameters, int pcnt, userrec* user)
190 /* First, let's check if the user is currently /list'ing */
192 user->GetExt("safelist_cache", ld);
196 /* user is already /list'ing, we don't want to do shit. */
200 time_t* last_list_time;
201 user->GetExt("safelist_last", last_list_time);
204 if (TIME < (*last_list_time)+60)
206 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
210 DELETE(last_list_time);
211 user->Shrink("safelist_last");
215 * start at channel 0! ;)
217 ld = new ListData(0,TIME);
218 user->Extend("safelist_cache", ld);
219 listusers.push_back(user);
221 time_t* llt = new time_t;
223 user->Extend("safelist_last", llt);
228 virtual void OnCleanup(int target_type, void* item)
230 if(target_type == TYPE_USER)
232 userrec* u = (userrec*)item;
234 u->GetExt("safelist_cache", ld);
237 u->Shrink("safelist_cache");
240 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
242 userrec* u2 = (userrec*)(*iter);
245 listusers.erase(iter);
249 time_t* last_list_time;
250 u->GetExt("safelist_last", last_list_time);
253 DELETE(last_list_time);
254 u->Shrink("safelist_last");
259 virtual void On005Numeric(std::string &output)
261 output.append(" SAFELIST");
264 virtual void OnUserQuit(userrec* user, const std::string &message)
266 this->OnCleanup(TYPE_USER,user);
273 /******************************************************************************************************/
275 class ModuleSafeListFactory : public ModuleFactory
278 ModuleSafeListFactory()
282 ~ModuleSafeListFactory()
286 virtual Module * CreateModule(InspIRCd* Me)
288 return new ModuleSafeList(Me);
293 extern "C" void * init_module( void )
295 return new ModuleSafeListFactory;