1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "configreader.h"
21 /** Holds a users m_safelist state
23 class ListData : public classbase
29 const std::string glob;
31 ListData() : list_start(0), list_position(0), list_ended(false) {};
32 ListData(long pos, time_t t, const std::string &pattern) : list_start(t), list_position(pos), list_ended(false), glob(pattern) {};
35 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
37 typedef std::vector<userrec *> UserList;
38 UserList listusers; /* vector of people doing a /list */
39 class ListTimer *timer;
41 /** To create a timer which recurs every second, we inherit from InspTimer.
42 * InspTimer is only one-shot however, so at the end of each Tick() we simply
43 * insert another of ourselves into the pending queue :)
45 class ListTimer : public InspTimer
51 InspIRCd* ServerInstance;
52 const std::string glob;
53 size_t ServerNameSize;
57 ListTimer(InspIRCd* Instance, long interval) : InspTimer(interval,Instance->Time()), ServerInstance(Instance)
59 ServerNameSize = 4 + strlen(ServerInstance->Config->ServerName);
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 /* Attempt to fill up to 25% the user's sendq with /LIST output */
94 if (!ld->list_position)
95 u->WriteServ("321 %s Channel :Users Name",u->nick);
96 chan = ServerInstance->GetChannelIndex(ld->list_position);
98 bool has_user = (chan && chan->HasUser(u));
99 if ((chan) && (chan->modes[CM_PRIVATE]))
101 bool display = match(chan->name, ld->glob.c_str());
102 long users = chan->GetUserCounter();
103 if ((users) && (display))
105 int counter = snprintf(buffer, MAXBUF, "322 %s *", u->nick);
106 amount_sent += counter + ServerNameSize;
107 u->WriteServ(std::string(buffer));
110 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
112 bool display = match(chan->name, ld->glob.c_str());
113 long users = chan->GetUserCounter();
115 if ((users) && (display))
117 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",u->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
118 /* Increment total plus linefeed */
119 amount_sent += counter + ServerNameSize;
120 u->WriteServ(std::string(buffer));
129 ld->list_ended = true;
130 u->WriteServ("323 %s :End of channel list.",u->nick);
137 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
141 if (listusers.size())
143 timer = new ListTimer(ServerInstance,1);
144 ServerInstance->Timers->AddTimer(timer);
153 class ModuleSafeList : public Module
156 ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
161 virtual ~ModuleSafeList()
164 ServerInstance->Timers->DelTimer(timer);
167 virtual Version GetVersion()
169 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
172 void Implements(char* List)
174 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
179 * Intercept the LIST command.
181 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
183 /* If the command doesnt appear to be valid, we dont want to mess with it. */
187 if (command == "LIST")
189 return this->HandleList(parameters, pcnt, user);
196 * Handle (override) the LIST command.
198 int HandleList(const char** parameters, int pcnt, userrec* user)
200 /* First, let's check if the user is currently /list'ing */
202 user->GetExt("safelist_cache", ld);
206 /* user is already /list'ing, we don't want to do shit. */
210 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
211 if ((pcnt == 1) && (*parameters[0] == '<'))
214 time_t* last_list_time;
215 user->GetExt("safelist_last", last_list_time);
218 if (ServerInstance->Time() < (*last_list_time)+60)
220 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
221 user->WriteServ("321 %s Channel :Users Name",user->nick);
222 user->WriteServ("323 %s :End of channel list.",user->nick);
226 DELETE(last_list_time);
227 user->Shrink("safelist_last");
231 * start at channel 0! ;)
233 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
234 user->Extend("safelist_cache", ld);
235 listusers.push_back(user);
237 time_t* llt = new time_t;
238 *llt = ServerInstance->Time();
239 user->Extend("safelist_last", llt);
243 timer = new ListTimer(ServerInstance,1);
244 ServerInstance->Timers->AddTimer(timer);
250 virtual void OnCleanup(int target_type, void* item)
252 if(target_type == TYPE_USER)
254 userrec* u = (userrec*)item;
256 u->GetExt("safelist_cache", ld);
259 u->Shrink("safelist_cache");
262 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
264 userrec* u2 = (userrec*)(*iter);
267 listusers.erase(iter);
271 time_t* last_list_time;
272 u->GetExt("safelist_last", last_list_time);
275 DELETE(last_list_time);
276 u->Shrink("safelist_last");
281 virtual void On005Numeric(std::string &output)
283 output.append(" SAFELIST");
286 virtual void OnUserQuit(userrec* user, const std::string &message)
288 this->OnCleanup(TYPE_USER,user);
294 class ModuleSafeListFactory : public ModuleFactory
297 ModuleSafeListFactory()
301 ~ModuleSafeListFactory()
305 virtual Module * CreateModule(InspIRCd* Me)
307 return new ModuleSafeList(Me);
312 extern "C" void * init_module( void )
314 return new ModuleSafeListFactory;