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"
29 class ListData : public classbase
36 ListData() : list_start(0), list_position(0), list_ended(false) {};
37 ListData(long pos, time_t t) : list_start(t), list_position(pos), list_ended(false) {};
40 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
42 typedef std::vector<userrec *> UserList;
43 UserList listusers; /* vector of people doing a /list */
46 * To create a timer which recurs every second, we inherit from InspTimer.
47 * InspTimer is only one-shot however, so at the end of each Tick() we simply
48 * insert another of ourselves into the pending queue :)
50 class ListTimer : public InspTimer
60 ListTimer(long interval, Server* Me) : InspTimer(interval,TIME), Srv(Me)
64 virtual void Tick(time_t TIME)
71 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
75 * - Get where they are up to
76 * - If it's > GetChannelCount, erase them from the iterator, set go_again to true
77 * - If not, spool more channels
79 userrec* u = (userrec*)(*iter);
81 u->GetExt("safelist_cache", ld);
82 if (ld->list_position > Srv->GetChannelCount())
84 u->Shrink("safelist_cache");
86 listusers.erase(iter);
91 log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
93 /* Attempt to fill up to half the user's sendq with /LIST output */
97 log(DEBUG,"Channel %ld",ld->list_position);
98 if (!ld->list_position)
99 u->WriteServ("321 %s Channel :Users Name",u->nick);
100 chan = Srv->GetChannelIndex(ld->list_position);
102 bool has_user = (chan && chan->HasUser(u));
103 if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
105 long users = chan->GetUserCounter();
108 int counter = snprintf(buffer,MAXBUF,"322 %s %s %ld :[+%s] %s",u->nick,chan->name,users,chanmodes(chan,has_user),chan->topic);
109 /* Increment total plus linefeed */
110 amount_sent += counter + 4 + Srv->GetServerName().length();
111 log(DEBUG,"m_safelist.so: Sent %ld of safe %ld / 4",amount_sent,u->sendqmax);
112 u->WriteServ(std::string(buffer));
121 ld->list_ended = true;
122 u->WriteServ("323 %s :End of channel list.",u->nick);
129 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
133 ListTimer* MyTimer = new ListTimer(1,Srv);
134 Srv->AddTimer(MyTimer);
138 class ModuleSafeList : public Module
144 ModuleSafeList(Server* Me) : Module::Module(Me)
148 MyTimer = new ListTimer(1,Srv);
149 Srv->AddTimer(MyTimer);
152 virtual ~ModuleSafeList()
156 virtual Version GetVersion()
158 return Version(1,0,0,0,VF_VENDOR);
161 void Implements(char* List)
163 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
168 * Intercept the LIST command.
170 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
172 /* If the command doesnt appear to be valid, we dont want to mess with it. */
176 if (command == "LIST")
178 return this->HandleList(parameters, pcnt, user);
185 * Handle (override) the LIST command.
187 int HandleList(const char** parameters, int pcnt, userrec* user)
189 /* First, let's check if the user is currently /list'ing */
191 user->GetExt("safelist_cache", ld);
195 /* user is already /list'ing, we don't want to do shit. */
199 time_t* last_list_time;
200 user->GetExt("safelist_last", last_list_time);
203 if (TIME < (*last_list_time)+60)
205 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
209 DELETE(last_list_time);
210 user->Shrink("safelist_last");
214 * start at channel 0! ;)
216 ld = new ListData(0,TIME);
217 user->Extend("safelist_cache", ld);
218 listusers.push_back(user);
220 time_t* llt = new time_t;
222 user->Extend("safelist_last", llt);
227 virtual void OnCleanup(int target_type, void* item)
229 if(target_type == TYPE_USER)
231 userrec* u = (userrec*)item;
233 u->GetExt("safelist_cache", ld);
236 u->Shrink("safelist_cache");
239 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
241 userrec* u2 = (userrec*)(*iter);
244 listusers.erase(iter);
248 time_t* last_list_time;
249 u->GetExt("safelist_last", last_list_time);
252 DELETE(last_list_time);
253 u->Shrink("safelist_last");
258 virtual void On005Numeric(std::string &output)
260 output.append(" SAFELIST");
263 virtual void OnUserQuit(userrec* user, const std::string &message)
265 this->OnCleanup(TYPE_USER,user);
272 /******************************************************************************************************/
274 class ModuleSafeListFactory : public ModuleFactory
277 ModuleSafeListFactory()
281 ~ModuleSafeListFactory()
285 virtual Module * CreateModule(Server* Me)
287 return new ModuleSafeList(Me);
292 extern "C" void * init_module( void )
294 return new ModuleSafeListFactory;