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 * ---------------------------------------------------
24 #include "configreader.h"
27 class ListData : public classbase
34 ListData() : list_start(0), list_position(0), list_ended(false) {};
35 ListData(long pos, time_t t) : list_start(t), list_position(pos), list_ended(false) {};
38 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
40 typedef std::vector<userrec *> UserList;
41 UserList listusers; /* vector of people doing a /list */
44 * To create a timer which recurs every second, we inherit from InspTimer.
45 * InspTimer is only one-shot however, so at the end of each Tick() we simply
46 * insert another of ourselves into the pending queue :)
48 class ListTimer : public InspTimer
54 InspIRCd* ServerInstance;
58 ListTimer(InspIRCd* Instance, long interval) : InspTimer(interval,Instance->Time()), ServerInstance(Instance)
62 virtual void Tick(time_t TIME)
69 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
73 * - Get where they are up to
74 * - If it's more than total number of channels, erase
75 * them from the iterator, set go_again to true
76 * - If not, spool more channels
78 userrec* u = (userrec*)(*iter);
80 u->GetExt("safelist_cache", ld);
81 if ((size_t)ld->list_position > ServerInstance->chanlist.size())
83 u->Shrink("safelist_cache");
85 listusers.erase(iter);
90 ServerInstance->Log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
92 /* Attempt to fill up to half the user's sendq with /LIST output */
96 ServerInstance->Log(DEBUG,"Channel %ld",ld->list_position);
97 if (!ld->list_position)
98 u->WriteServ("321 %s Channel :Users Name",u->nick);
99 chan = ServerInstance->GetChannelIndex(ld->list_position);
101 bool has_user = (chan && chan->HasUser(u));
102 if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
104 long users = chan->GetUserCounter();
107 int counter = snprintf(buffer,MAXBUF,"322 %s %s %ld :[+%s] %s",u->nick,chan->name,users,chan->ChanModes(has_user),chan->topic);
108 /* Increment total plus linefeed */
109 amount_sent += counter + 4 + strlen(ServerInstance->Config->ServerName);
110 ServerInstance->Log(DEBUG,"m_safelist.so: Sent %ld of safe %ld / 4",amount_sent,u->sendqmax);
111 u->WriteServ(std::string(buffer));
120 ld->list_ended = true;
121 u->WriteServ("323 %s :End of channel list.",u->nick);
128 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
132 ListTimer* MyTimer = new ListTimer(ServerInstance,1);
133 ServerInstance->Timers->AddTimer(MyTimer);
137 class ModuleSafeList : public Module
143 ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
145 MyTimer = new ListTimer(ServerInstance,1);
146 ServerInstance->Timers->AddTimer(MyTimer);
149 virtual ~ModuleSafeList()
153 virtual Version GetVersion()
155 return Version(1,0,0,0,VF_VENDOR);
158 void Implements(char* List)
160 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
165 * Intercept the LIST command.
167 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
169 /* If the command doesnt appear to be valid, we dont want to mess with it. */
173 if (command == "LIST")
175 return this->HandleList(parameters, pcnt, user);
182 * Handle (override) the LIST command.
184 int HandleList(const char** parameters, int pcnt, userrec* user)
186 /* First, let's check if the user is currently /list'ing */
188 user->GetExt("safelist_cache", ld);
192 /* user is already /list'ing, we don't want to do shit. */
196 time_t* last_list_time;
197 user->GetExt("safelist_last", last_list_time);
200 if (ServerInstance->Time() < (*last_list_time)+60)
202 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
206 DELETE(last_list_time);
207 user->Shrink("safelist_last");
211 * start at channel 0! ;)
213 ld = new ListData(0,ServerInstance->Time());
214 user->Extend("safelist_cache", ld);
215 listusers.push_back(user);
217 time_t* llt = new time_t;
218 *llt = ServerInstance->Time();
219 user->Extend("safelist_last", llt);
224 virtual void OnCleanup(int target_type, void* item)
226 if(target_type == TYPE_USER)
228 userrec* u = (userrec*)item;
230 u->GetExt("safelist_cache", ld);
233 u->Shrink("safelist_cache");
236 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
238 userrec* u2 = (userrec*)(*iter);
241 listusers.erase(iter);
245 time_t* last_list_time;
246 u->GetExt("safelist_last", last_list_time);
249 DELETE(last_list_time);
250 u->Shrink("safelist_last");
255 virtual void On005Numeric(std::string &output)
257 output.append(" SAFELIST");
260 virtual void OnUserQuit(userrec* user, const std::string &message)
262 this->OnCleanup(TYPE_USER,user);
269 /******************************************************************************************************/
271 class ModuleSafeListFactory : public ModuleFactory
274 ModuleSafeListFactory()
278 ~ModuleSafeListFactory()
282 virtual Module * CreateModule(InspIRCd* Me)
284 return new ModuleSafeList(Me);
289 extern "C" void * init_module( void )
291 return new ModuleSafeListFactory;