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 "configreader.h"
26 /** Holds a users m_safelist state
28 class ListData : public classbase
34 const std::string glob;
36 ListData() : list_start(0), list_position(0), list_ended(false) {};
37 ListData(long pos, time_t t, const std::string &pattern) : list_start(t), list_position(pos), list_ended(false), glob(pattern) {};
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 */
44 class ListTimer *timer;
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
56 InspIRCd* ServerInstance;
57 const std::string glob;
58 size_t ServerNameSize;
62 ListTimer(InspIRCd* Instance, long interval) : InspTimer(interval,Instance->Time()), ServerInstance(Instance)
64 ServerNameSize = 4 + strlen(ServerInstance->Config->ServerName);
67 virtual void Tick(time_t TIME)
74 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
78 * - Get where they are up to
79 * - If it's more than total number of channels, erase
80 * them from the iterator, set go_again to true
81 * - If not, spool more channels
83 userrec* u = (userrec*)(*iter);
85 u->GetExt("safelist_cache", ld);
86 if ((size_t)ld->list_position > ServerInstance->chanlist.size())
88 u->Shrink("safelist_cache");
90 listusers.erase(iter);
95 ServerInstance->Log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
97 /* Attempt to fill up to 25% the user's sendq with /LIST output */
101 ServerInstance->Log(DEBUG,"Channel %ld",ld->list_position);
102 if (!ld->list_position)
103 u->WriteServ("321 %s Channel :Users Name",u->nick);
104 chan = ServerInstance->GetChannelIndex(ld->list_position);
106 bool has_user = (chan && chan->HasUser(u));
107 if ((chan) && (chan->modes[CM_PRIVATE]))
109 bool display = match(chan->name, ld->glob.c_str());
110 long users = chan->GetUserCounter();
111 if ((users) && (display))
113 int counter = snprintf(buffer, MAXBUF, "322 %s *", u->nick);
114 amount_sent += counter + ServerNameSize;
115 ServerInstance->Log(DEBUG, "m_safelist.so: Sent %ld of safe %ld / 4", amount_sent, u->sendqmax);
116 u->WriteServ(std::string(buffer));
119 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
121 bool display = match(chan->name, ld->glob.c_str());
122 long users = chan->GetUserCounter();
124 if ((users) && (display))
126 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",u->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
127 /* Increment total plus linefeed */
128 amount_sent += counter + ServerNameSize;
129 ServerInstance->Log(DEBUG, "m_safelist.so: Sent %ld of safe %ld / 4", amount_sent, u->sendqmax);
130 u->WriteServ(std::string(buffer));
139 ld->list_ended = true;
140 u->WriteServ("323 %s :End of channel list.",u->nick);
147 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
151 if (listusers.size())
153 timer = new ListTimer(ServerInstance,1);
154 ServerInstance->Timers->AddTimer(timer);
163 class ModuleSafeList : public Module
166 ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
171 virtual ~ModuleSafeList()
174 ServerInstance->Timers->DelTimer(timer);
177 virtual Version GetVersion()
179 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
182 void Implements(char* List)
184 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
189 * Intercept the LIST command.
191 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
193 /* If the command doesnt appear to be valid, we dont want to mess with it. */
197 if (command == "LIST")
199 return this->HandleList(parameters, pcnt, user);
206 * Handle (override) the LIST command.
208 int HandleList(const char** parameters, int pcnt, userrec* user)
210 /* First, let's check if the user is currently /list'ing */
212 user->GetExt("safelist_cache", ld);
216 /* user is already /list'ing, we don't want to do shit. */
220 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
221 if ((pcnt == 1) && (*parameters[0] == '<'))
224 time_t* last_list_time;
225 user->GetExt("safelist_last", last_list_time);
228 if (ServerInstance->Time() < (*last_list_time)+60)
230 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
231 user->WriteServ("321 %s Channel :Users Name",user->nick);
232 user->WriteServ("323 %s :End of channel list.",user->nick);
236 DELETE(last_list_time);
237 user->Shrink("safelist_last");
241 * start at channel 0! ;)
243 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
244 user->Extend("safelist_cache", ld);
245 listusers.push_back(user);
247 time_t* llt = new time_t;
248 *llt = ServerInstance->Time();
249 user->Extend("safelist_last", llt);
253 timer = new ListTimer(ServerInstance,1);
254 ServerInstance->Timers->AddTimer(timer);
260 virtual void OnCleanup(int target_type, void* item)
262 if(target_type == TYPE_USER)
264 userrec* u = (userrec*)item;
266 u->GetExt("safelist_cache", ld);
269 u->Shrink("safelist_cache");
272 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
274 userrec* u2 = (userrec*)(*iter);
277 listusers.erase(iter);
281 time_t* last_list_time;
282 u->GetExt("safelist_last", last_list_time);
285 DELETE(last_list_time);
286 u->Shrink("safelist_last");
291 virtual void On005Numeric(std::string &output)
293 output.append(" SAFELIST");
296 virtual void OnUserQuit(userrec* user, const std::string &message)
298 this->OnCleanup(TYPE_USER,user);
304 class ModuleSafeListFactory : public ModuleFactory
307 ModuleSafeListFactory()
311 ~ModuleSafeListFactory()
315 virtual Module * CreateModule(InspIRCd* Me)
317 return new ModuleSafeList(Me);
322 extern "C" void * init_module( void )
324 return new ModuleSafeListFactory;